Coverage for biobb_model/model/fix_pdb.py: 73%
59 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 11:32 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 11:32 +0000
1#!/usr/bin/env python3
3"""Module containing the FixPdb 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_model.model.fix_pdb_utils import Structure, generate_map_online
15class FixPdb(BiobbObject):
16 """
17 | biobb_model FixPdb
18 | Class to renumerate residues in a PDB structure according to a reference sequence from UniProt.
19 | Fix the residue numbering in a PDB structure according to a reference sequence from UniProt.
21 Args:
22 input_pdb_path (str): Input PDB file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_model/raw/master/biobb_model/test/data/model/2ki5.pdb>`_. Accepted formats: pdb (edam:format_1476).
23 output_pdb_path (str): Output PDB file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_model/raw/master/biobb_model/test/reference/model/output_pdb_path.pdb>`_. Accepted formats: pdb (edam:format_1476).
24 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
25 * **forced_uniprot_references** (*str*) - (None) Set the UniProt accessions for sequences to be used as reference.
26 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
27 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
28 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
29 Examples:
30 This is a use example of how to use the building block from Python::
31 from biobb_model.model.fix_pdb import fix_pdb
32 prop = { 'forced_uniprot_references': ["P00533"] }
33 fix_pdb(input_pdb_path='/path/to/myStructure.pdb', output_pdb_path='/path/to/newStructure.pdb', properties=prop)
34 Info:
35 * wrapped_software:
36 * name: In house
37 * license: Apache-2.0
38 * ontology:
39 * name: EDAM
40 * schema: http://edamontology.org/EDAM.owl
41 """
43 def __init__(
44 self,
45 input_pdb_path: str,
46 output_pdb_path: str,
47 properties: Optional[dict] = None,
48 **kwargs,
49 ) -> None:
50 properties = properties or {}
52 # Call parent class constructor
53 super().__init__(properties)
54 self.locals_var_dict = locals().copy()
56 # Input/Output files
57 self.io_dict = {
58 "in": {"input_pdb_path": input_pdb_path},
59 "out": {"output_pdb_path": output_pdb_path},
60 }
62 # Properties specific for BB
63 self.forced_uniprot_references = properties.get("forced_uniprot_references")
64 # If input forced uniprot references is a string and not a list then convert it
65 if isinstance(self.forced_uniprot_references, str):
66 self.forced_uniprot_references = self.forced_uniprot_references.split(" ")
68 # Check the properties
69 self.check_properties(properties)
70 self.check_arguments()
72 @launchlogger
73 def launch(self) -> int:
74 """Execute the :class:`FixPdb <model.fix_pdb.FixPdb>` object."""
76 # Setup Biobb
77 if self.check_restart():
78 return 0
80 # Run code
81 self.return_code = 0
83 # Get the user arguments
84 input_pdb_path = self.io_dict["in"]["input_pdb_path"]
85 output_pdb_path = self.io_dict["out"]["output_pdb_path"]
86 forced_uniprot_references = self.forced_uniprot_references
88 # Read and parse the input pdb file
89 structure = Structure.from_pdb_file(input_pdb_path)
91 # Add protein chains in case they are missing
92 chains = structure.chains
93 if len(chains) == 0 or (
94 len(chains) == 1 and (chains[0].name == " " or chains[0].name == "X")
95 ):
96 structure.raw_protein_chainer()
98 # Run all the mapping function
99 # mapping: Optional[dict[Any, Any]] = {}
100 # if forced_uniprot_references:
101 mapping = generate_map_online(structure, forced_uniprot_references) # type: ignore
103 # In case something went wrong with the mapping stop here
104 if not mapping:
105 self.return_code = -1
106 return self.return_code
108 # Change residue numbers in the structure according to the mapping results
109 mapped_residue_numbers = mapping["residue_reference_numbers"]
110 for r, residue in enumerate(structure.residues):
111 mapped_residue_number = mapped_residue_numbers[r]
112 if mapped_residue_number is None:
113 continue
114 residue.number = mapped_residue_number
116 # Write the modified structure to a new pdb file
117 structure.generate_pdb_file(output_pdb_path)
119 print("Fixed :)")
121 # Remove temporal files
122 # self.tmp_files.extend([self.stage_io_dict.get("unique_dir", "")])
123 self.remove_tmp_files()
125 self.check_arguments(output_files_created=True, raise_exception=False)
126 return self.return_code
129def fix_pdb(
130 input_pdb_path: str,
131 output_pdb_path: str,
132 properties: Optional[dict] = None,
133 **kwargs,
134) -> int:
135 """Create :class:`FixPdb <model.fix_pdb.FixPdb>` class and
136 execute the :meth:`launch() <model.fix_pdb.FixPdb.launch>` method."""
137 return FixPdb(
138 input_pdb_path=input_pdb_path,
139 output_pdb_path=output_pdb_path,
140 properties=properties,
141 **kwargs,
142 ).launch()
144 fix_pdb.__doc__ = FixPdb.__doc__
147def main():
148 parser = argparse.ArgumentParser(
149 description="Model the missing atoms in the backbone of a PDB structure.",
150 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999),
151 )
152 parser.add_argument(
153 "-c",
154 "--config",
155 required=False,
156 help="This file can be a YAML file, JSON file or JSON string",
157 )
159 # Specific args of each building block
160 required_args = parser.add_argument_group("required arguments")
161 required_args.add_argument(
162 "-i", "--input_pdb_path", required=True, help="Input PDB file name"
163 )
164 required_args.add_argument(
165 "-o", "--output_pdb_path", required=True, help="Output PDB file name"
166 )
168 args = parser.parse_args()
169 config = args.config if args.config else None
170 properties = settings.ConfReader(config=config).get_prop_dic()
172 # Specific call of each building block
173 fix_pdb(
174 input_pdb_path=args.input_pdb_path,
175 output_pdb_path=args.output_pdb_path,
176 properties=properties,
177 )
180if __name__ == "__main__":
181 main()