Coverage for biobb_chemistry / babelm / babel_minimize.py: 97%

73 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-22 12:49 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5from pathlib import PurePath 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_chemistry.babelm.common import check_minimize_property, check_input_path_minimize, check_output_path_minimize 

9 

10 

11class BabelMinimize(BiobbObject): 

12 """ 

13 | biobb_chemistry BabelMinimize 

14 | This class is a wrapper of the Open Babel tool. 

15 | Energetically minimizes small molecules. Open Babel is a chemical toolbox designed to speak the many languages of chemical data. It's an open, collaborative project allowing anyone to search, convert, analyze, or store data from molecular modeling, chemistry, solid-state materials, biochemistry, or related areas. `Visit the official page <http://openbabel.org/wiki/Main_Page>`_. 

16 

17 Args: 

18 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/babelm/babel.minimize.pdb>`_. Accepted formats: pdb (edam:format_1476), mol2 (edam:format_3816). 

19 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/babelm/ref_babel.minimize.pdb>`_. Accepted formats: pdb (edam:format_1476), mol2 (edam:format_3816). 

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

21 * **criteria** (*float*) - (1e-6) Convergence criteria 

22 * **method** (*str*) - ("cg") Method. Values: cg (conjugate gradients algorithm), sd (steepest descent algorithm). 

23 * **force_field** (*str*) - (None) Force field. Values: GAFF (General Amber Force Field), Ghemical (Ghemical force field), MMFF94 (MMFF94 force field), MMFF94s (MMFF94s force field), UFF (Universal Force Field). 

24 * **hydrogens** (*bool*) - (False) Add hydrogen atoms. 

25 * **steps** (*int*) - (2500) [0~5000|1] Maximum number of steps. 

26 * **cutoff** (*bool*) - (False) Use cut-off. 

27 * **rvdw** (*float*) - (6.0) [0~50|1.0] VDW cut-off distance. 

28 * **rele** (*float*) - (10.0) [0~50|1.0] Electrostatic cut-off distance. 

29 * **frequency** (*int*) - (10) [0~50|1] Frequency to update the non-bonded pairs. 

30 * **binary_path** (*str*) - ("obminimize") Path to the obminimize executable binary. 

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

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

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

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

35 * **container_image** (*str*) - ('informaticsmatters/obabel:latest') Container image definition. 

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

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

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

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

40 

41 Examples: 

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

43 

44 from biobb_chemistry.babelm.babel_minimize import babel_minimize 

45 prop = { 

46 'criteria': 1e-6, 

47 'method': 'cg', 

48 'force_field': 'GAFF' 

49 } 

50 babel_minimize(input_path='/path/to/myStructure.mol2', 

51 output_path='/path/to/newStructure.mol2', 

52 properties=prop) 

53 

54 Info: 

55 * wrapped_software: 

56 * name: Open Babel 

57 * version: 2.4.1 

58 * license: GNU 

59 * ontology: 

60 * name: EDAM 

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

62 

63 """ 

64 

65 def __init__(self, input_path, output_path, 

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

67 properties = properties or {} 

68 

69 # Call parent class constructor 

70 super().__init__(properties) 

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

72 

73 # Input/Output files 

74 self.io_dict = { 

75 "in": {"input_path": input_path}, 

76 "out": {"output_path": output_path} 

77 } 

78 

79 # Properties specific for BB 

80 self.criteria = properties.get('criteria', '') 

81 self.method = properties.get('method', '') 

82 self.force_field = properties.get('force_field', '') 

83 self.hydrogens = properties.get('hydrogens', '') 

84 self.steps = properties.get('steps', '') 

85 self.cutoff = properties.get('cutoff', '') 

86 self.rvdw = properties.get('rvdw', '') 

87 self.rele = properties.get('rele', '') 

88 self.frequency = properties.get('frequency', '') 

89 self.binary_path = properties.get('binary_path', 'obminimize') 

90 self.properties = properties 

91 

92 # Check the properties 

93 self.check_properties(properties) 

94 self.check_arguments() 

95 

96 def check_data_params(self, out_log, err_log): 

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

98 self.io_dict["in"]["input_path"] = check_input_path_minimize(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__) 

99 self.io_dict["out"]["output_path"] = check_output_path_minimize(self.io_dict["out"]["output_path"], out_log, self.__class__.__name__) 

100 

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

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

103 instructions_list = [] 

104 

105 # executable path 

106 instructions_list.append(self.binary_path) 

107 

108 # check all properties 

109 if check_minimize_property("criteria", self.criteria, out_log): 

110 instructions_list.append('-c ' + str(self.criteria)) 

111 

112 if check_minimize_property("method", self.method, out_log): 

113 instructions_list.append('-' + self.method) 

114 

115 if check_minimize_property("force_field", self.force_field, out_log): 

116 instructions_list.append('-ff ' + self.force_field) 

117 

118 if check_minimize_property("hydrogens", self.hydrogens, out_log): 

119 instructions_list.append('-h') 

120 

121 if check_minimize_property("steps", self.steps, out_log): 

122 instructions_list.append('-n ' + str(self.steps)) 

123 

124 if check_minimize_property("cutoff", self.cutoff, out_log): 

125 instructions_list.append('-cut') 

126 

127 if check_minimize_property("rvdw", self.rvdw, out_log): 

128 instructions_list.append('-rvdw ' + str(self.rvdw)) 

129 

130 if check_minimize_property("rele", self.rele, out_log): 

131 instructions_list.append('-rele ' + str(self.rele)) 

132 

133 if check_minimize_property("frequency", self.frequency, out_log): 

134 instructions_list.append('-pf ' + str(self.frequency)) 

135 

136 iextension = PurePath(container_io_dict["in"]["input_path"]).suffix 

137 oextension = PurePath(container_io_dict["out"]["output_path"]).suffix 

138 

139 instructions_list.append('-i' + iextension[1:] + ' ' + container_io_dict["in"]["input_path"]) 

140 

141 instructions_list.append('-o' + oextension[1:]) 

142 

143 instructions_list.append('>') 

144 

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

146 

147 return instructions_list 

148 

149 @launchlogger 

150 def launch(self) -> int: 

151 """Execute the :class:`BabelMinimize <babelm.babel_minimize.BabelMinimize>` babelm.babel_minimize.BabelMinimize object.""" 

152 

153 # check input/output paths and parameters 

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

155 

156 # Setup Biobb 

157 if self.check_restart(): 

158 return 0 

159 self.stage_files() 

160 

161 # create command line instruction 

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

163 

164 # Run Biobb block 

165 self.run_biobb() 

166 

167 # Copy files to host 

168 self.copy_to_host() 

169 

170 # remove temporary folder(s) 

171 self.remove_tmp_files() 

172 

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

174 

175 return self.return_code 

176 

177 

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

179 """Create the :class:`BabelMinimize <babelm.babel_minimize.BabelMinimize>` class and 

180 execute the :meth:`launch() <babelm.babel_minimize.BabelMinimize.launch>` method.""" 

181 return BabelMinimize(**dict(locals())).launch() 

182 

183 

184babel_minimize.__doc__ = BabelMinimize.__doc__ 

185main = BabelMinimize.get_main(babel_minimize, "Energetically minimize small molecules.") 

186 

187 

188if __name__ == '__main__': 

189 main()