Coverage for biobb_io/api/api_binding_site.py: 95%
37 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-04 08:31 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-04 08:31 +0000
1#!/usr/bin/env python
3"""Module containing the ApiBindingSite class and the command line interface."""
5import json
6from typing import Optional
7from biobb_common.generic.biobb_object import BiobbObject
8from biobb_common.tools import file_utils as fu
9from biobb_common.tools.file_utils import launchlogger
11from biobb_io.api.common import (
12 check_mandatory_property,
13 check_output_path,
14 download_binding_site,
15 write_json,
16)
19class ApiBindingSite(BiobbObject):
20 """
21 | biobb_io ApiBindingSite
22 | This class is a wrapper for the `PDBe REST API <https://www.ebi.ac.uk/pdbe/api/doc/#pdb_apidiv_call_16_calltitle>`_ Binding Sites endpoint.
23 | This call provides details on binding sites in the entry as per STRUCT_SITE records in PDB files, such as ligand, residues in the site, description of the site, etc.
25 Args:
26 output_json_path (str): Path to the JSON file with the binding sites for the requested structure. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_binding_site.json>`_. Accepted formats: json (edam:format_3464).
27 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
28 * **pdb_code** (*str*) - (None) RSCB PDB code.
29 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
30 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
31 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
33 Examples:
34 This is a use example of how to use the building block from Python::
36 from biobb_io.api.api_binding_site import api_binding_site
37 prop = {
38 'pdb_code': '4i23'
39 }
40 api_binding_site(output_json_path='/path/to/newBindingSites.json',
41 properties=prop)
43 Info:
44 * wrapped_software:
45 * name: PDBe REST API
46 * license: Apache-2.0
47 * ontology:
48 * name: EDAM
49 * schema: http://edamontology.org/EDAM.owl
51 """
53 def __init__(self, output_json_path, properties=None, **kwargs) -> None:
54 properties = properties or {}
56 # Call parent class constructor
57 super().__init__(properties)
58 self.locals_var_dict = locals().copy()
60 # Input/Output files
61 self.io_dict = {"out": {"output_json_path": output_json_path}}
63 # Properties specific for BB
64 self.pdb_code = properties.get("pdb_code", None)
65 self.properties = properties
67 # Check the properties
68 self.check_properties(properties)
69 self.check_arguments()
71 def check_data_params(self, out_log, err_log):
72 """Checks all the input/output paths and parameters"""
73 self.output_json_path = check_output_path(
74 self.io_dict["out"]["output_json_path"],
75 "output_json_path",
76 False,
77 out_log,
78 self.__class__.__name__,
79 )
81 @launchlogger
82 def launch(self) -> int:
83 """Execute the :class:`ApiBindingSite <api.api_binding_site.ApiBindingSite>` api.api_binding_site.ApiBindingSite object."""
85 # check input/output paths and parameters
86 self.check_data_params(self.out_log, self.err_log)
88 # Setup Biobb
89 if self.check_restart():
90 return 0
91 # self.stage_files()
93 check_mandatory_property(
94 self.pdb_code, "pdb_code", self.out_log, self.__class__.__name__
95 )
97 self.pdb_code = self.pdb_code.strip().lower()
98 url = "https://www.ebi.ac.uk/pdbe/api/pdb/entry/binding_sites/%s"
100 # get JSON object
101 json_string = download_binding_site(
102 self.pdb_code, url, self.out_log, self.global_log
103 )
105 # get number of binding sites
106 fu.log(
107 "%d binding sites found" % (len(json.loads(json_string)[self.pdb_code])),
108 self.out_log,
109 self.global_log,
110 )
112 # write JSON file
113 write_json(json_string, self.output_json_path, self.out_log, self.global_log)
115 self.check_arguments(output_files_created=True, raise_exception=False)
117 return 0
120def api_binding_site(
121 output_json_path: str, properties: Optional[dict] = None, **kwargs
122) -> int:
123 """Execute the :class:`ApiBindingSite <api.api_binding_site.ApiBindingSite>` class and
124 execute the :meth:`launch() <api.api_binding_site.ApiBindingSite.launch>` method."""
125 return ApiBindingSite(**dict(locals())).launch()
128api_binding_site.__doc__ = ApiBindingSite.__doc__
129main = ApiBindingSite.get_main(api_binding_site, "This class is a wrapper for the PDBe REST API Binding Sites endpoint")
131if __name__ == "__main__":
132 main()