Coverage for biobb_io/api/ideal_sdf.py: 77%
43 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-10 15:33 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-10 15:33 +0000
1#!/usr/bin/env python
3"""Module containing the IdealSdf class and the command line interface."""
5import argparse
6from typing import Optional
8from biobb_common.configuration import settings
9from biobb_common.generic.biobb_object import BiobbObject
10from biobb_common.tools.file_utils import launchlogger
12from biobb_io.api.common import (
13 check_mandatory_property,
14 check_output_path,
15 download_ideal_sdf,
16 write_sdf,
17)
20class IdealSdf(BiobbObject):
21 """
22 | biobb_io IdealSdf
23 | This class is a wrapper for downloading an ideal SDF ligand from the Protein Data Bank.
24 | Wrapper for the `Protein Data Bank in Europe <https://www.ebi.ac.uk/pdbe/>`_ and the `Protein Data Bank <https://www.rcsb.org/>`_ for downloading a single ideal SDF ligand.
26 Args:
27 output_sdf_path (str): Path to the output SDF file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/ref_output.sdf>`_. Accepted formats: sdf (edam:format_3814).
28 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
29 * **ligand_code** (*str*) - (None) RSCB PDB ligand code.
30 * **api_id** (*str*) - ("pdbe") Identifier of the PDB REST API from which the SDF structure will be downloaded. Values: pdbe (`PDB in Europe REST API <https://www.ebi.ac.uk/pdbe/pdbe-rest-api>`_), pdb (`RCSB PDB REST API <https://data.rcsb.org/>`_).
31 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
32 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
33 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
35 Examples:
36 This is a use example of how to use the building block from Python::
38 from biobb_io.api.ideal_sdf import ideal_sdf
39 prop = {
40 'ligand_code': 'HYZ',
41 'api_id': 'pdbe'
42 }
43 ideal_sdf(output_sdf_path='/path/to/newStructure.sdf',
44 properties=prop)
46 Info:
47 * wrapped_software:
48 * name: Protein Data Bank
49 * license: Apache-2.0
50 * ontology:
51 * name: EDAM
52 * schema: http://edamontology.org/EDAM.owl
54 """
56 def __init__(self, output_sdf_path, properties=None, **kwargs) -> None:
57 properties = properties or {}
59 # Call parent class constructor
60 super().__init__(properties)
61 self.locals_var_dict = locals().copy()
63 # Input/Output files
64 self.io_dict = {"out": {"output_sdf_path": output_sdf_path}}
66 # Properties specific for BB
67 self.api_id = properties.get("api_id", "pdbe")
68 self.ligand_code = properties.get("ligand_code", None)
69 self.properties = properties
71 # Check the properties
72 self.check_properties(properties)
73 self.check_arguments()
75 def check_data_params(self, out_log, err_log):
76 """Checks all the input/output paths and parameters"""
77 self.output_sdf_path = check_output_path(
78 self.io_dict["out"]["output_sdf_path"],
79 "output_sdf_path",
80 False,
81 out_log,
82 self.__class__.__name__,
83 )
85 @launchlogger
86 def launch(self) -> int:
87 """Execute the :class:`IdealSdf <api.ideal_sdf.IdealSdf>` api.ideal_sdf.IdealSdf object."""
89 # check input/output paths and parameters
90 self.check_data_params(self.out_log, self.err_log)
92 # Setup Biobb
93 if self.check_restart():
94 return 0
96 check_mandatory_property(
97 self.ligand_code, "ligand_code", self.out_log, self.__class__.__name__
98 )
100 self.ligand_code = self.ligand_code.strip()
102 # Downloading PDB file
103 sdf_string = download_ideal_sdf(
104 self.ligand_code, self.api_id, self.out_log, self.global_log
105 )
106 write_sdf(sdf_string, self.output_sdf_path, self.out_log, self.global_log)
108 self.check_arguments(output_files_created=True, raise_exception=False)
110 return 0
113def ideal_sdf(output_sdf_path: str, properties: Optional[dict] = None, **kwargs) -> int:
114 """Execute the :class:`IdealSdf <api.ideal_sdf.IdealSdf>` class and
115 execute the :meth:`launch() <api.ideal_sdf.IdealSdf.launch>` method."""
117 return IdealSdf(
118 output_sdf_path=output_sdf_path, properties=properties, **kwargs
119 ).launch()
122def main():
123 """Command line execution of this building block. Please check the command line documentation."""
124 parser = argparse.ArgumentParser(
125 description="This class is a wrapper for downloading an ideal SDF ligand from the Protein Data Bank.",
126 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999),
127 )
128 parser.add_argument(
129 "-c",
130 "--config",
131 required=False,
132 help="This file can be a YAML file, JSON file or JSON string",
133 )
135 # Specific args of each building block
136 required_args = parser.add_argument_group("required arguments")
137 required_args.add_argument(
138 "-o",
139 "--output_sdf_path",
140 required=True,
141 help="Path to the output SDF file. Accepted formats: sdf.",
142 )
144 args = parser.parse_args()
145 config = args.config if args.config else None
146 properties = settings.ConfReader(config=config).get_prop_dic()
148 # Specific call of each building block
149 ideal_sdf(output_sdf_path=args.output_sdf_path, properties=properties)
152if __name__ == "__main__":
153 main()