Coverage for biobb_io/api/api_binding_site.py: 78%
45 statements
« prev ^ index » next coverage.py v7.5.3, created at 2024-06-14 18:20 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2024-06-14 18:20 +0000
1#!/usr/bin/env python
3"""Module containing the ApiBindingSite class and the command line interface."""
4import argparse
5import json
6from biobb_common.generic.biobb_object import BiobbObject
7from biobb_common.configuration import settings
8from biobb_common.tools import file_utils as fu
9from biobb_common.tools.file_utils import launchlogger
10from biobb_io.api.common import check_output_path, check_mandatory_property, download_binding_site, write_json
13class ApiBindingSite(BiobbObject):
14 """
15 | biobb_io ApiBindingSite
16 | 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.
17 | 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.
19 Args:
20 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).
21 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
22 * **pdb_code** (*str*) - (None) RSCB PDB code.
23 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
24 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
25 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
27 Examples:
28 This is a use example of how to use the building block from Python::
30 from biobb_io.api.api_binding_site import api_binding_site
31 prop = {
32 'pdb_code': '4i23'
33 }
34 api_binding_site(output_json_path='/path/to/newBindingSites.json',
35 properties=prop)
37 Info:
38 * wrapped_software:
39 * name: PDBe REST API
40 * license: Apache-2.0
41 * ontology:
42 * name: EDAM
43 * schema: http://edamontology.org/EDAM.owl
45 """
47 def __init__(self, output_json_path,
48 properties=None, **kwargs) -> None:
49 properties = properties or {}
51 # Call parent class constructor
52 super().__init__(properties)
53 self.locals_var_dict = locals().copy()
55 # Input/Output files
56 self.io_dict = {
57 "out": {"output_json_path": output_json_path}
58 }
60 # Properties specific for BB
61 self.pdb_code = properties.get('pdb_code', None)
62 self.properties = properties
64 # Check the properties
65 self.check_properties(properties)
66 self.check_arguments()
68 def check_data_params(self, out_log, err_log):
69 """ Checks all the input/output paths and parameters """
70 self.output_json_path = check_output_path(self.io_dict["out"]["output_json_path"], "output_json_path", False, out_log, self.__class__.__name__)
72 @launchlogger
73 def launch(self) -> int:
74 """Execute the :class:`ApiBindingSite <api.api_binding_site.ApiBindingSite>` api.api_binding_site.ApiBindingSite object."""
76 # check input/output paths and parameters
77 self.check_data_params(self.out_log, self.err_log)
79 # Setup Biobb
80 if self.check_restart():
81 return 0
82 # self.stage_files()
84 check_mandatory_property(self.pdb_code, 'pdb_code', self.out_log, self.__class__.__name__)
86 self.pdb_code = self.pdb_code.strip().lower()
87 url = "https://www.ebi.ac.uk/pdbe/api/pdb/entry/binding_sites/%s"
89 # get JSON object
90 json_string = download_binding_site(self.pdb_code, url, self.out_log, self.global_log)
92 # get number of binding sites
93 fu.log('%d binding sites found' % (len(json.loads(json_string)[self.pdb_code])), self.out_log, self.global_log)
95 # write JSON file
96 write_json(json_string, self.output_json_path, self.out_log, self.global_log)
98 self.check_arguments(output_files_created=True, raise_exception=False)
100 return 0
103def api_binding_site(output_json_path: str, properties: dict = None, **kwargs) -> int:
104 """Execute the :class:`ApiBindingSite <api.api_binding_site.ApiBindingSite>` class and
105 execute the :meth:`launch() <api.api_binding_site.ApiBindingSite.launch>` method."""
107 return ApiBindingSite(output_json_path=output_json_path,
108 properties=properties, **kwargs).launch()
111def main():
112 """Command line execution of this building block. Please check the command line documentation."""
113 parser = argparse.ArgumentParser(description="This class is a wrapper for the PDBe REST API Binding Sites endpoint", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
114 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string")
116 # Specific args of each building block
117 required_args = parser.add_argument_group('required arguments')
118 required_args.add_argument('-o', '--output_json_path', required=True, help="Path to the JSON file with the binding sites for the requested structure. Accepted formats: json.")
120 args = parser.parse_args()
121 config = args.config if args.config else None
122 properties = settings.ConfReader(config=config).get_prop_dic()
124 # Specific call of each building block
125 api_binding_site(output_json_path=args.output_json_path,
126 properties=properties)
129if __name__ == '__main__':
130 main()