Coverage for biobb_chemistry / ambertools / reduce_add_hydrogens.py: 83%
99 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 12:49 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 12:49 +0000
1#!/usr/bin/env python3
3"""Module containing the ReduceAddHydrogens class and the command line interface."""
4from typing import Optional
5from biobb_common.generic.biobb_object import BiobbObject
6from biobb_common.tools.file_utils import launchlogger
7from biobb_chemistry.ambertools.common import get_binary_path, check_input_path, check_output_path
10class ReduceAddHydrogens(BiobbObject):
11 """
12 | biobb_chemistry ReduceAddHydrogens
13 | This class is a wrapper of the `Ambertools <http://ambermd.org/doc12/AmberTools12.pdf>`_ reduce module for adding hydrogens to a given structure.
14 | Reduce is a program for `adding or removing hydrogens to a Protein DataBank (PDB) molecular structure file <http://ambermd.org/doc12/AmberTools12.pdf>`_.
16 Args:
17 input_path (str): Path to the input file. File type: input. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/data/ambertools/reduce.no.H.pdb>`_. Accepted formats: pdb (edam:format_1476).
18 output_path (str): Path to the output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/ambertools/ref_reduce.add.pdb>`_. Accepted formats: pdb (edam:format_1476).
19 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
20 * **flip** (*bool*) - (False) add H and rotate and flip NQH groups
21 * **noflip** (*bool*) - (False) add H and rotate groups with no NQH flips
22 * **nuclear** (*bool*) - (False) use nuclear X-H distances rather than default electron cloud distances
23 * **nooh** (*bool*) - (False) remove hydrogens on OH and SH groups
24 * **oh** (*bool*) - (True) add hydrogens on OH and SH groups (default)
25 * **his** (*bool*) - (False) create NH hydrogens on HIS rings (usually used with -HIS)
26 * **noheth** (*bool*) - (False) do not attempt to add NH proton on Het groups
27 * **rotnh3** (*bool*) - (True) allow lysine NH3 to rotate (default)
28 * **norotnh3** (*bool*) - (False) do not allow lysine NH3 to rotate
29 * **rotexist** (*bool*) - (False) allow existing rotatable groups (OH, SH, Met-CH3) to rotate
30 * **rotexoh** (*bool*) - (False) allow existing OH & SH groups to rotate
31 * **allalt** (*bool*) - (True) process adjustments for all conformations (default)
32 * **onlya** (*bool*) - (False) only adjust 'A' conformations
33 * **charges** (*bool*) - (False) output charge state for appropriate hydrogen records
34 * **dorotmet** (*bool*) - (False) allow methionine methyl groups to rotate (not recommended)
35 * **noadjust** (*bool*) - (False) do not process any rot or flip adjustments
36 * **metal_bump** (*float*) - (None) [0~5|0.005] H 'bumps' metals at radius plus this
37 * **non_metal_bump** (*float*) - (None) [0~5|0.005] 'bumps' nonmetal at radius plus this
38 * **build** (*bool*) - (False) add H, including His sc NH, then rotate and flip groups (except for pre-existing methionine methyl hydrogens)
39 * **binary_path** (*str*) - ("reduce") Path to the reduce executable binary.
40 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
41 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
42 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
43 * **container_path** (*str*) - (None) Container path definition.
44 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition.
45 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
46 * **container_working_dir** (*str*) - (None) Container working directory definition.
47 * **container_user_id** (*str*) - (None) Container user_id definition.
48 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
50 Examples:
51 This is a use example of how to use the building block from Python::
53 from biobb_chemistry.ambertools.reduce_add_hydrogens import reduce_add_hydrogens
54 prop = {
55 'flip': False,
56 'charges': True,
57 'build': False
58 }
59 reduce_add_hydrogens(input_path='/path/to/myStructure.pdb',
60 output_path='/path/to/newStructure.pdb',
61 properties=prop)
63 Info:
64 * wrapped_software:
65 * name: AmberTools Reduce
66 * version: >=20.0
67 * license: GNU
68 * ontology:
69 * name: EDAM
70 * schema: http://edamontology.org/EDAM.owl
72 """
74 def __init__(self, input_path, output_path,
75 properties=None, **kwargs) -> None:
76 properties = properties or {}
78 # Call parent class constructor
79 super().__init__(properties)
80 self.locals_var_dict = locals().copy()
82 # Input/Output files
83 self.io_dict = {
84 "in": {"input_path": input_path},
85 "out": {"output_path": output_path}
86 }
88 # Properties specific for BB
89 self.flip = properties.get('flip', False)
90 self.noflip = properties.get('noflip', False)
91 self.nuclear = properties.get('nuclear', False)
92 self.nooh = properties.get('nooh', False)
93 self.oh = properties.get('oh', True)
94 self.his = properties.get('his', False)
95 self.noheth = properties.get('noheth', False)
96 self.rotnh3 = properties.get('rotnh3', True)
97 self.norotnh3 = properties.get('norotnh3', False)
98 self.rotexist = properties.get('rotexist', False)
99 self.rotexoh = properties.get('rotexoh', False)
100 self.allalt = properties.get('allalt', True)
101 self.onlya = properties.get('onlya', False)
102 self.charges = properties.get('charges', False)
103 self.dorotmet = properties.get('dorotmet', False)
104 self.noadjust = properties.get('noadjust', False)
105 self.build = properties.get('build', False)
106 self.metal_bump = properties.get('metal_bump', None)
107 self.non_metal_bump = properties.get('non_metal_bump', None)
108 self.binary_path = get_binary_path(properties, 'binary_path')
109 self.properties = properties
111 # Check the properties
112 self.check_properties(properties)
113 self.check_arguments()
115 def check_data_params(self, out_log, err_log):
116 """ Checks all the input/output paths and parameters """
117 self.io_dict["in"]["input_path"] = check_input_path(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__)
118 self.io_dict["out"]["output_path"] = check_output_path(self.io_dict["out"]["output_path"], out_log, self.__class__.__name__)
120 def create_cmd(self, container_io_dict, out_log, err_log):
121 """Creates the command line instruction using the properties file settings"""
122 instructions_list = []
124 # executable path
125 instructions_list.append(self.binary_path)
127 if self.flip:
128 instructions_list.append('-FLIP')
129 if self.noflip:
130 instructions_list.append('-NOFLIP')
131 if self.nuclear:
132 instructions_list.append('-NUClear')
133 if self.nooh:
134 instructions_list.append('-NOOH')
135 if self.oh:
136 instructions_list.append('-OH')
137 if self.his:
138 instructions_list.append('-HIS')
139 if self.noheth:
140 instructions_list.append('-NOHETh')
141 if self.rotnh3:
142 instructions_list.append('-ROTNH3')
143 if self.norotnh3:
144 instructions_list.append('-NOROTNH3')
145 if self.rotexist:
146 instructions_list.append('-ROTEXist')
147 if self.rotexoh:
148 instructions_list.append('-ROTEXOH')
149 if self.allalt:
150 instructions_list.append('-ALLALT')
151 if self.onlya:
152 instructions_list.append('-ONLYA')
153 if self.charges:
154 instructions_list.append('-CHARGEs')
155 if self.dorotmet:
156 instructions_list.append('-DOROTMET')
157 if self.noadjust:
158 instructions_list.append('-NOADJust')
159 if self.build:
160 instructions_list.append('-BUILD')
162 if self.metal_bump:
163 instructions_list.append('-METALBump ' + str(self.metal_bump))
165 if self.non_metal_bump:
166 instructions_list.append('-NONMETALBump ' + str(self.non_metal_bump))
168 instructions_list.append(container_io_dict["in"]["input_path"])
169 instructions_list.append('>')
170 instructions_list.append(container_io_dict["out"]["output_path"])
172 return instructions_list
174 @launchlogger
175 def launch(self) -> int:
176 """Execute the :class:`ReduceAddHydrogens <ambertools.reduce_add_hydrogens.ReduceAddHydrogens>` ambertools.reduce_add_hydrogens.ReduceAddHydrogens object."""
178 # check input/output paths and parameters
179 self.check_data_params(self.out_log, self.err_log)
181 # Setup Biobb
182 if self.check_restart():
183 return 0
184 self.stage_files()
186 # create command line instruction
187 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log)
189 # Run Biobb block
190 self.run_biobb()
192 # Copy files to host
193 self.copy_to_host()
195 # remove temporary folder(s)
196 self.remove_tmp_files()
198 self.check_arguments(output_files_created=True, raise_exception=False)
200 return self.return_code
203def reduce_add_hydrogens(input_path: str, output_path: str, properties: Optional[dict] = None, **kwargs) -> int:
204 """Create the :class:`ReduceAddHydrogens <ambertools.reduce_add_hydrogens.ReduceAddHydrogens>` class and
205 execute the :meth:`launch() <ambertools.reduce_add_hydrogens.ReduceAddHydrogens.launch>` method."""
206 return ReduceAddHydrogens(**dict(locals())).launch()
209reduce_add_hydrogens.__doc__ = ReduceAddHydrogens.__doc__
210main = ReduceAddHydrogens.get_main(reduce_add_hydrogens, "Adds hydrogen atoms to small molecules.")
212if __name__ == '__main__':
213 main()