Coverage for biobb_chemistry/ambertools/reduce_add_hydrogens.py: 75%

110 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 09:28 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the ReduceAddHydrogens class and the command line interface.""" 

4import argparse 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools.file_utils import launchlogger 

9from biobb_chemistry.ambertools.common import get_binary_path, check_input_path, check_output_path 

10 

11 

12class ReduceAddHydrogens(BiobbObject): 

13 """ 

14 | biobb_chemistry ReduceAddHydrogens 

15 | This class is a wrapper of the `Ambertools <http://ambermd.org/doc12/AmberTools12.pdf>`_ reduce module for adding hydrogens to a given structure. 

16 | Reduce is a program for `adding or removing hydrogens to a Protein DataBank (PDB) molecular structure file <http://ambermd.org/doc12/AmberTools12.pdf>`_. 

17 

18 Args: 

19 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). 

20 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). 

21 properties (dic - Python dictionary object containing the tool parameters, not input/output files): 

22 * **flip** (*bool*) - (False) add H and rotate and flip NQH groups 

23 * **noflip** (*bool*) - (False) add H and rotate groups with no NQH flips 

24 * **nuclear** (*bool*) - (False) use nuclear X-H distances rather than default electron cloud distances 

25 * **nooh** (*bool*) - (False) remove hydrogens on OH and SH groups 

26 * **oh** (*bool*) - (True) add hydrogens on OH and SH groups (default) 

27 * **his** (*bool*) - (False) create NH hydrogens on HIS rings (usually used with -HIS) 

28 * **noheth** (*bool*) - (False) do not attempt to add NH proton on Het groups 

29 * **rotnh3** (*bool*) - (True) allow lysine NH3 to rotate (default) 

30 * **norotnh3** (*bool*) - (False) do not allow lysine NH3 to rotate 

31 * **rotexist** (*bool*) - (False) allow existing rotatable groups (OH, SH, Met-CH3) to rotate 

32 * **rotexoh** (*bool*) - (False) allow existing OH & SH groups to rotate 

33 * **allalt** (*bool*) - (True) process adjustments for all conformations (default) 

34 * **onlya** (*bool*) - (False) only adjust 'A' conformations 

35 * **charges** (*bool*) - (False) output charge state for appropriate hydrogen records 

36 * **dorotmet** (*bool*) - (False) allow methionine methyl groups to rotate (not recommended) 

37 * **noadjust** (*bool*) - (False) do not process any rot or flip adjustments 

38 * **metal_bump** (*float*) - (None) [0~5|0.005] H 'bumps' metals at radius plus this 

39 * **non_metal_bump** (*float*) - (None) [0~5|0.005] 'bumps' nonmetal at radius plus this 

40 * **build** (*bool*) - (False) add H, including His sc NH, then rotate and flip groups (except for pre-existing methionine methyl hydrogens) 

41 * **binary_path** (*str*) - ("reduce") Path to the reduce executable binary. 

42 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

43 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

44 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

45 * **container_path** (*str*) - (None) Container path definition. 

46 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

47 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

48 * **container_working_dir** (*str*) - (None) Container working directory definition. 

49 * **container_user_id** (*str*) - (None) Container user_id definition. 

50 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

51 

52 Examples: 

53 This is a use example of how to use the building block from Python:: 

54 

55 from biobb_chemistry.ambertools.reduce_add_hydrogens import reduce_add_hydrogens 

56 prop = { 

57 'flip': False, 

58 'charges': True, 

59 'build': False 

60 } 

61 reduce_add_hydrogens(input_path='/path/to/myStructure.pdb', 

62 output_path='/path/to/newStructure.pdb', 

63 properties=prop) 

64 

65 Info: 

66 * wrapped_software: 

67 * name: AmberTools Reduce 

68 * version: >=20.0 

69 * license: GNU 

70 * ontology: 

71 * name: EDAM 

72 * schema: http://edamontology.org/EDAM.owl 

73 

74 """ 

75 

76 def __init__(self, input_path, output_path, 

77 properties=None, **kwargs) -> None: 

78 properties = properties or {} 

79 

80 # Call parent class constructor 

81 super().__init__(properties) 

82 self.locals_var_dict = locals().copy() 

83 

84 # Input/Output files 

85 self.io_dict = { 

86 "in": {"input_path": input_path}, 

87 "out": {"output_path": output_path} 

88 } 

89 

90 # Properties specific for BB 

91 self.flip = properties.get('flip', False) 

92 self.noflip = properties.get('noflip', False) 

93 self.nuclear = properties.get('nuclear', False) 

94 self.nooh = properties.get('nooh', False) 

95 self.oh = properties.get('oh', True) 

96 self.his = properties.get('his', False) 

97 self.noheth = properties.get('noheth', False) 

98 self.rotnh3 = properties.get('rotnh3', True) 

99 self.norotnh3 = properties.get('norotnh3', False) 

100 self.rotexist = properties.get('rotexist', False) 

101 self.rotexoh = properties.get('rotexoh', False) 

102 self.allalt = properties.get('allalt', True) 

103 self.onlya = properties.get('onlya', False) 

104 self.charges = properties.get('charges', False) 

105 self.dorotmet = properties.get('dorotmet', False) 

106 self.noadjust = properties.get('noadjust', False) 

107 self.build = properties.get('build', False) 

108 self.metal_bump = properties.get('metal_bump', None) 

109 self.non_metal_bump = properties.get('non_metal_bump', None) 

110 self.binary_path = get_binary_path(properties, 'binary_path') 

111 self.properties = properties 

112 

113 # Check the properties 

114 self.check_properties(properties) 

115 self.check_arguments() 

116 

117 def check_data_params(self, out_log, err_log): 

118 """ Checks all the input/output paths and parameters """ 

119 self.io_dict["in"]["input_path"] = check_input_path(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__) 

120 self.io_dict["out"]["output_path"] = check_output_path(self.io_dict["out"]["output_path"], out_log, self.__class__.__name__) 

121 

122 def create_cmd(self, container_io_dict, out_log, err_log): 

123 """Creates the command line instruction using the properties file settings""" 

124 instructions_list = [] 

125 

126 # executable path 

127 instructions_list.append(self.binary_path) 

128 

129 if self.flip: 

130 instructions_list.append('-FLIP') 

131 if self.noflip: 

132 instructions_list.append('-NOFLIP') 

133 if self.nuclear: 

134 instructions_list.append('-NUClear') 

135 if self.nooh: 

136 instructions_list.append('-NOOH') 

137 if self.oh: 

138 instructions_list.append('-OH') 

139 if self.his: 

140 instructions_list.append('-HIS') 

141 if self.noheth: 

142 instructions_list.append('-NOHETh') 

143 if self.rotnh3: 

144 instructions_list.append('-ROTNH3') 

145 if self.norotnh3: 

146 instructions_list.append('-NOROTNH3') 

147 if self.rotexist: 

148 instructions_list.append('-ROTEXist') 

149 if self.rotexoh: 

150 instructions_list.append('-ROTEXOH') 

151 if self.allalt: 

152 instructions_list.append('-ALLALT') 

153 if self.onlya: 

154 instructions_list.append('-ONLYA') 

155 if self.charges: 

156 instructions_list.append('-CHARGEs') 

157 if self.dorotmet: 

158 instructions_list.append('-DOROTMET') 

159 if self.noadjust: 

160 instructions_list.append('-NOADJust') 

161 if self.build: 

162 instructions_list.append('-BUILD') 

163 

164 if self.metal_bump: 

165 instructions_list.append('-METALBump ' + str(self.metal_bump)) 

166 

167 if self.non_metal_bump: 

168 instructions_list.append('-NONMETALBump ' + str(self.non_metal_bump)) 

169 

170 instructions_list.append(container_io_dict["in"]["input_path"]) 

171 instructions_list.append('>') 

172 instructions_list.append(container_io_dict["out"]["output_path"]) 

173 

174 return instructions_list 

175 

176 @launchlogger 

177 def launch(self) -> int: 

178 """Execute the :class:`ReduceAddHydrogens <ambertools.reduce_add_hydrogens.ReduceAddHydrogens>` ambertools.reduce_add_hydrogens.ReduceAddHydrogens object.""" 

179 

180 # check input/output paths and parameters 

181 self.check_data_params(self.out_log, self.err_log) 

182 

183 # Setup Biobb 

184 if self.check_restart(): 

185 return 0 

186 self.stage_files() 

187 

188 # create command line instruction 

189 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log) 

190 

191 # Run Biobb block 

192 self.run_biobb() 

193 

194 # Copy files to host 

195 self.copy_to_host() 

196 

197 # remove temporary folder(s) 

198 '''self.tmp_files.extend([ 

199 self.stage_io_dict.get("unique_dir", "") 

200 ])''' 

201 self.remove_tmp_files() 

202 

203 self.check_arguments(output_files_created=True, raise_exception=False) 

204 

205 return self.return_code 

206 

207 

208def reduce_add_hydrogens(input_path: str, output_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

209 """Execute the :class:`ReduceAddHydrogens <ambertools.reduce_add_hydrogens.ReduceAddHydrogens>` class and 

210 execute the :meth:`launch() <ambertools.reduce_add_hydrogens.ReduceAddHydrogens.launch>` method.""" 

211 

212 return ReduceAddHydrogens(input_path=input_path, 

213 output_path=output_path, 

214 properties=properties, **kwargs).launch() 

215 

216 reduce_add_hydrogens.__doc__ = ReduceAddHydrogens.__doc__ 

217 

218 

219def main(): 

220 """Command line execution of this building block. Please check the command line documentation.""" 

221 parser = argparse.ArgumentParser(description="Adds hydrogen atoms to small molecules.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

222 parser.add_argument('--config', required=False, help='Configuration file') 

223 

224 # Specific args of each building block 

225 required_args = parser.add_argument_group('required arguments') 

226 required_args.add_argument('--input_path', required=True, help='Path to the input file. Accepted formats: pdb.') 

227 required_args.add_argument('--output_path', required=True, help='Path to the output file. Accepted formats: pdb.') 

228 

229 args = parser.parse_args() 

230 args.config = args.config or "{}" 

231 properties = settings.ConfReader(config=args.config).get_prop_dic() 

232 

233 # Specific call of each building block 

234 reduce_add_hydrogens(input_path=args.input_path, 

235 output_path=args.output_path, 

236 properties=properties) 

237 

238 

239if __name__ == '__main__': 

240 main()