Coverage for biobb_io/api/structure_info.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 StructureInfo 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_str_info,
16 write_json,
17)
20class StructureInfo(BiobbObject):
21 """
22 | biobb_io StructureInfo
23 | This class is a wrapper for getting all the available information of a structure from the Protein Data Bank.
24 | Wrapper for the `MMB PDB mirror <http://mmb.irbbarcelona.org/api/>`_ for getting all the available information of a structure from the Protein Data Bank.
26 Args:
27 output_json_path (str): Path to the output JSON file with all the structure information. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/ref_str_info.json>`_. Accepted formats: json (edam:format_3464).
28 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
29 * **pdb_code** (*str*) - (None) RSCB PDB structure code.
30 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
31 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
32 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
34 Examples:
35 This is a use example of how to use the building block from Python::
37 from biobb_io.api.structure_info import structure_info
38 prop = {
39 'pdb_code': '2vgb'
40 }
41 structure_info(output_json_path='/path/to/newStructure.sdf',
42 properties=prop)
44 Info:
45 * wrapped_software:
46 * name: Protein Data Bank
47 * license: Apache-2.0
48 * ontology:
49 * name: EDAM
50 * schema: http://edamontology.org/EDAM.owl
52 """
54 def __init__(self, output_json_path, properties=None, **kwargs) -> None:
55 properties = properties or {}
57 # Call parent class constructor
58 super().__init__(properties)
59 self.locals_var_dict = locals().copy()
61 # Input/Output files
62 self.io_dict = {"out": {"output_json_path": output_json_path}}
64 # Properties specific for BB
65 self.pdb_code = properties.get("pdb_code", None)
66 self.properties = properties
68 # Check the properties
69 self.check_properties(properties)
70 self.check_arguments()
72 def check_data_params(self, out_log, err_log):
73 """Checks all the input/output paths and parameters"""
74 self.output_json_path = check_output_path(
75 self.io_dict["out"]["output_json_path"],
76 "output_json_path",
77 False,
78 out_log,
79 self.__class__.__name__,
80 )
82 @launchlogger
83 def launch(self) -> int:
84 """Execute the :class:`StructureInfo <api.structure_info.StructureInfo>` api.structure_info.StructureInfo object."""
86 # check input/output paths and parameters
87 self.check_data_params(self.out_log, self.err_log)
89 # Setup Biobb
90 if self.check_restart():
91 return 0
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 = "http://mmb.irbbarcelona.org/api/pdb/%s.json"
100 # Downloading PDB file
101 json_string = download_str_info(
102 self.pdb_code, url, self.out_log, self.global_log
103 )
104 write_json(json_string, self.output_json_path, self.out_log, self.global_log)
106 self.check_arguments(output_files_created=True, raise_exception=False)
108 return 0
111def structure_info(
112 output_json_path: str, properties: Optional[dict] = None, **kwargs
113) -> int:
114 """Execute the :class:`StructureInfo <api.structure_info.StructureInfo>` class and
115 execute the :meth:`launch() <api.structure_info.StructureInfo.launch>` method."""
117 return StructureInfo(
118 output_json_path=output_json_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 getting all the available information of a structure 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_json_path",
140 required=True,
141 help="Path to the output JSON file with all the structure information. Accepted formats: json.",
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 structure_info(output_json_path=args.output_json_path, properties=properties)
152if __name__ == "__main__":
153 main()