Coverage for biobb_structure_utils / utils / str_check_add_hydrogens.py: 91%
53 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 13:23 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 13:23 +0000
1#!/usr/bin/env python3
3"""Module containing the StrCheckAddHydrogens class and the command line interface."""
5from typing import Optional
7from biobb_common.generic.biobb_object import BiobbObject
8from biobb_common.tools.file_utils import launchlogger
10from biobb_structure_utils.utils.common import (
11 check_input_path,
12 check_output_end,
13 check_output_path_pdbqt,
14)
17class StrCheckAddHydrogens(BiobbObject):
18 """
19 | biobb_structure_utils StrCheckAddHydrogens
20 | This class is a wrapper of the Structure Checking tool to add hydrogens to a 3D structure.
21 | Wrapper for the `Structure Checking <https://github.com/bioexcel/biobb_structure_checking>`_ tool to add hydrogens to a 3D structure.
23 Args:
24 input_structure_path (str): Input structure file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/str_no_H.pdb>`_. Accepted formats: pdb (edam:format_1476).
25 output_structure_path (str): Output structure file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/ref_str_H.pdbqt>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476).
26 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
27 * **charges** (*bool*) - (False) Whether or not to add charges to the output file. If True the output is in PDBQT format.
28 * **mode** (*string*) - (auto) Selection mode. Values: auto, list, ph
29 * **ph** (*float*) - (7.4) [0~14|0.1] Add hydrogens appropriate for pH. Only in case mode ph selected.
30 * **list** (*string*) - ("") List of residues to modify separated by commas (i.e HISA234HID,HISB33HIE). Only in case mode list selected.
31 * **keep_canonical_resnames** (*bool*) - (False) Whether or not keep canonical residue names
32 * **binary_path** (*string*) - ("check_structure") path to the check_structure application
33 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
34 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
35 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
37 Examples:
38 This is a use example of how to use the building block from Python::
40 from biobb_structure_utils.utils.str_check_add_hydrogens import str_check_add_hydrogens
41 prop = {
42 'charges': False,
43 'mode': 'auto'
44 }
45 str_check_add_hydrogens(input_structure_path='/path/to/myInputStr.pdb',
46 output_structure_path='/path/to/newStructure.pdb',
47 properties=prop)
49 Info:
50 * wrapped_software:
51 * name: Structure Checking from MDWeb
52 * version: >=3.0.3
53 * license: Apache-2.0
54 * ontology:
55 * name: EDAM
56 * schema: http://edamontology.org/EDAM.owl
58 """
60 def __init__(
61 self, input_structure_path, output_structure_path, properties=None, **kwargs
62 ) -> None:
63 properties = properties or {}
65 # Call parent class constructor
66 super().__init__(properties)
67 self.locals_var_dict = locals().copy()
69 # Input/Output files
70 self.io_dict = {
71 "in": {"input_structure_path": input_structure_path},
72 "out": {"output_structure_path": output_structure_path},
73 }
75 # Properties specific for BB
76 self.binary_path = properties.get("binary_path", "check_structure")
77 self.charges = properties.get("charges", False)
78 self.mode = properties.get("mode", "auto")
79 self.ph = properties.get("ph", 7.4)
80 self.list = properties.get("list", "")
81 self.keep_canonical_resnames = properties.get("keep_canonical_resnames", False)
82 self.properties = properties
84 # Check the properties
85 self.check_properties(properties)
86 self.check_arguments()
88 @launchlogger
89 def launch(self) -> int:
90 """Execute the :class:`StrCheckAddHydrogens <utils.str_check_add_hydrogens.StrCheckAddHydrogens>` utils.str_check_add_hydrogens.StrCheckAddHydrogens object."""
92 self.io_dict["in"]["input_structure_path"] = check_input_path(
93 self.io_dict["in"]["input_structure_path"],
94 self.out_log,
95 self.__class__.__name__,
96 )
97 self.io_dict["out"]["output_structure_path"] = check_output_path_pdbqt(
98 self.io_dict["out"]["output_structure_path"],
99 self.out_log,
100 self.__class__.__name__,
101 )
103 # Setup Biobb
104 if self.check_restart():
105 return 0
106 self.stage_files()
108 self.cmd = [
109 self.binary_path,
110 "-i",
111 self.stage_io_dict["in"]["input_structure_path"],
112 "-o",
113 self.stage_io_dict["out"]["output_structure_path"],
114 "--non_interactive",
115 "--force_save",
116 ]
118 if self.keep_canonical_resnames:
119 self.cmd.append("--keep_canonical_resnames")
121 self.cmd.extend(["command_list", "--list", "'add_hydrogen"])
123 if self.charges:
124 self.cmd.append("--add_charges")
125 self.cmd.append("ADT")
127 if self.mode:
128 self.cmd.extend(["--add_mode", self.mode])
129 if self.mode == "ph":
130 self.cmd.extend(["--pH", self.ph])
131 if self.mode == "list":
132 self.cmd.extend(["--list", self.list])
133 else:
134 self.cmd.extend(["--add_mode", "None"])
136 self.cmd.append("'")
137 # Run Biobb block
138 self.run_biobb()
140 # Copy files to host
141 self.copy_to_host()
143 check_output_end(self.io_dict["out"]["output_structure_path"], self.out_log)
145 # Remove temporal files
146 self.remove_tmp_files()
148 self.check_arguments(output_files_created=True, raise_exception=False)
150 return self.return_code
153def str_check_add_hydrogens(
154 input_structure_path: str,
155 output_structure_path: str,
156 properties: Optional[dict] = None,
157 **kwargs,
158) -> int:
159 """Create the :class:`StrCheckAddHydrogens <utils.str_check_add_hydrogens.StrCheckAddHydrogens>` class and
160 execute the :meth:`launch() <utils.str_check_add_hydrogens.StrCheckAddHydrogens.launch>` method."""
161 return StrCheckAddHydrogens(**dict(locals())).launch()
164str_check_add_hydrogens.__doc__ = StrCheckAddHydrogens.__doc__
165main = StrCheckAddHydrogens.get_main(str_check_add_hydrogens, "Class to add hydrogens to a 3D structure.")
167if __name__ == "__main__":
168 main()