Coverage for biobb_godmd / godmd / godmd_run.py: 22%

73 statements  

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

1#!/usr/bin/env python3 

2 

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

4 

5import shutil 

6from pathlib import Path, PurePath 

7from typing import Optional 

8 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools.file_utils import launchlogger 

11 

12from biobb_godmd.godmd.common import check_input_path, check_output_path 

13 

14 

15class GOdMDRun(BiobbObject): 

16 """ 

17 | biobb_godmd GOdMDRun 

18 | Wrapper of the `GOdMD tool <http://mmb.irbbarcelona.org/GOdMD/>`_ module. 

19 | Computes conformational transition trajectories for proteins using GOdMD tool. 

20 

21 Args: 

22 input_pdb_orig_path (str): Input PDB file to be used as origin in the conformational transition. File type: input. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/data/godmd/1ake_A.pdb>`_. Accepted formats: pdb (edam:format_1476). 

23 input_pdb_target_path (str): Input PDB file to be used as target in the conformational transition. File type: input. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/data/godmd/4ake_A.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 input_aln_orig_path (str): Input GOdMD alignment file corresponding to the origin structure of the conformational transition. File type: input. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/data/godmd/1ake_A.aln>`_. Accepted formats: aln (edam:format_2330), txt (edam:format_2330). 

25 input_aln_target_path (str): Input GOdMD alignment file corresponding to the target structure of the conformational transition. File type: input. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/data/godmd/4ake_A.aln>`_. Accepted formats: aln (edam:format_2330), txt (edam:format_2330). 

26 input_config_path (str) (Optional): Input GOdMD configuration file. File type: input. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/data/godmd/params.in>`_. Accepted formats: in (edam:format_2330), txt (edam:format_2330). 

27 output_log_path (str): Output log file. File type: output. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/reference/godmd/godmd.log>`_. Accepted formats: log (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

28 output_ene_path (str): Output energy file. File type: output. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/reference/godmd/godmd_ene.out>`_. Accepted formats: log (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

29 output_trj_path (str): Output trajectory file. File type: output. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/reference/godmd/godmd_trj.mdcrd>`_. Accepted formats: trj (edam:format_3878), crd (edam:format_3878), mdcrd (edam:format_3878), x (edam:format_3878). 

30 output_pdb_path (str): Output structure file. File type: output. `Sample file <https://github.com/bioexcel/biobb_godmd/raw/main/biobb_godmd/test/reference/godmd/godmd_pdb.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

32 * **godmdin** (*dict*) - ({}) GOdMD options specification. 

33 * **binary_path** (*str*) - ("discrete") Binary path. 

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

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

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

37 

38 Examples: 

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

40 

41 from biobb_godmd.godmd.godmd_run import godmd_run 

42 prop = { 

43 'remove_tmp': True 

44 } 

45 godmd_run( input_pdb_orig_path='/path/to/pdb_orig.pdb', 

46 input_pdb_target_path='/path/to/pdb_target.pdb', 

47 input_aln_orig_path='/path/to/aln_orig.aln', 

48 input_aln_target_path='/path/to/aln_target.aln', 

49 output_log_path='/path/to/godmd_log.log', 

50 output_ene_path='/path/to/godmd_ene.txt', 

51 output_trj_path='/path/to/godmd_trj.mdcrd', 

52 output_pdb_path='/path/to/godmd_pdb.pdb', 

53 properties=prop) 

54 

55 Info: 

56 * wrapped_software: 

57 * name: GOdMD 

58 * version: >=1.0 

59 * license: Apache-2.0 

60 * ontology: 

61 * name: EDAM 

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

63 

64 """ 

65 

66 def __init__( 

67 self, 

68 input_pdb_orig_path: str, 

69 input_pdb_target_path: str, 

70 input_aln_orig_path: str, 

71 input_aln_target_path: str, 

72 input_config_path: Optional[str], 

73 output_log_path: str, 

74 output_ene_path: str, 

75 output_trj_path: str, 

76 output_pdb_path: str, 

77 properties: Optional[dict] = None, 

78 **kwargs, 

79 ) -> None: 

80 properties = properties or {} 

81 

82 # Call parent class constructor 

83 super().__init__(properties) 

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

85 

86 # Input/Output files 

87 self.io_dict = { 

88 "in": { 

89 "input_pdb_orig_path": input_pdb_orig_path, 

90 "input_pdb_target_path": input_pdb_target_path, 

91 "input_aln_orig_path": input_aln_orig_path, 

92 "input_aln_target_path": input_aln_target_path, 

93 "input_config_path": input_config_path, 

94 }, 

95 "out": { 

96 "output_log_path": output_log_path, 

97 "output_ene_path": output_ene_path, 

98 "output_trj_path": output_trj_path, 

99 "output_pdb_path": output_pdb_path, 

100 }, 

101 } 

102 

103 # Properties specific for BB 

104 self.properties = properties 

105 self.godmdin = {k: str(v) for k, v in properties.get("godmdin", dict()).items()} 

106 self.binary_path = properties.get("binary_path", "discrete") 

107 

108 # Check the properties 

109 self.check_properties(properties) 

110 # self.check_arguments() 

111 

112 def check_data_params(self, out_log, out_err): 

113 """Checks input/output paths correctness""" 

114 

115 # Check input(s) 

116 self.io_dict["in"]["input_pdb_orig_path"] = check_input_path( 

117 self.io_dict["in"]["input_pdb_orig_path"], 

118 "input_pdb_orig_path", 

119 False, 

120 out_log, 

121 self.__class__.__name__, 

122 ) 

123 self.io_dict["in"]["input_pdb_target_path"] = check_input_path( 

124 self.io_dict["in"]["input_pdb_target_path"], 

125 "input_pdb_target_path", 

126 False, 

127 out_log, 

128 self.__class__.__name__, 

129 ) 

130 self.io_dict["in"]["input_aln_orig_path"] = check_input_path( 

131 self.io_dict["in"]["input_aln_orig_path"], 

132 "input_aln_orig_path", 

133 False, 

134 out_log, 

135 self.__class__.__name__, 

136 ) 

137 self.io_dict["in"]["input_aln_target_path"] = check_input_path( 

138 self.io_dict["in"]["input_aln_target_path"], 

139 "input_aln_target_path", 

140 False, 

141 out_log, 

142 self.__class__.__name__, 

143 ) 

144 self.io_dict["in"]["input_config_path"] = check_input_path( 

145 self.io_dict["in"]["input_config_path"], 

146 "input_config_path", 

147 True, 

148 out_log, 

149 self.__class__.__name__, 

150 ) 

151 

152 # Check output(s) 

153 self.io_dict["out"]["output_log_path"] = check_output_path( 

154 self.io_dict["out"]["output_log_path"], 

155 "output_log_path", 

156 False, 

157 out_log, 

158 self.__class__.__name__, 

159 ) 

160 self.io_dict["out"]["output_ene_path"] = check_output_path( 

161 self.io_dict["out"]["output_ene_path"], 

162 "output_ene_path", 

163 False, 

164 out_log, 

165 self.__class__.__name__, 

166 ) 

167 self.io_dict["out"]["output_trj_path"] = check_output_path( 

168 self.io_dict["out"]["output_trj_path"], 

169 "output_trj_path", 

170 False, 

171 out_log, 

172 self.__class__.__name__, 

173 ) 

174 self.io_dict["out"]["output_pdb_path"] = check_output_path( 

175 self.io_dict["out"]["output_pdb_path"], 

176 "output_pdb_path", 

177 False, 

178 out_log, 

179 self.__class__.__name__, 

180 ) 

181 

182 def create_godmdin(self, path: Optional[str] = None) -> str: 

183 """Creates a GOdMD configuration file (godmdin) using the properties file settings""" 

184 godmdin_list = [] 

185 

186 self.output_godmdin_path = path 

187 

188 if self.io_dict["in"]["input_config_path"]: 

189 # GOdMD input parameters read from an input godmdin file 

190 with open(self.io_dict["in"]["input_config_path"]) as input_params: 

191 for line in input_params: 

192 if "=" in line: 

193 godmdin_list.append(line.upper()) 

194 else: 

195 # Pre-configured simulation type parameters 

196 godmdin_list.append(" TSNAP = 500 ! BioBB GOdMD default params \n") 

197 godmdin_list.append(" TEMP = 300 ! BioBB GOdMD default params \n") 

198 godmdin_list.append(" SEED = 2525 ! BioBB GOdMD default params \n") 

199 godmdin_list.append(" ENER_EVO_SIZE = 20 ! BioBB GOdMD default params \n") 

200 godmdin_list.append(" NBLOC = 10000 ! BioBB GOdMD default params \n") 

201 godmdin_list.append( 

202 " ERRORACCEPTABLE = 1.5 ! BioBB GOdMD default params \n" 

203 ) 

204 

205 # Adding the rest of parameters in the config file to the mdin file 

206 # if the parameter has already been added replace the value 

207 parameter_keys = [parameter.split("=")[0].strip() for parameter in godmdin_list] 

208 for k, v in self.godmdin.items(): 

209 config_parameter_key = str(k).strip().upper() 

210 if config_parameter_key in parameter_keys: 

211 godmdin_list[parameter_keys.index(config_parameter_key)] = ( 

212 "\t" + config_parameter_key + " = " + str(v) + " ! BioBB property \n" 

213 ) 

214 else: 

215 godmdin_list.append( 

216 "\t" + config_parameter_key + " = " + str(v) + " ! BioBB property \n" 

217 ) 

218 

219 # Writing MD configuration file (mdin) 

220 with open(str(self.output_godmdin_path), "w") as godmdin: 

221 # GOdMDIN parameters added by the biobb_godmd module 

222 godmdin.write( 

223 "!This godmdin file has been created by the biobb_godmd module from the BioBB library \n\n" 

224 ) 

225 

226 godmdin.write("&INPUT\n") 

227 

228 # MD config parameters 

229 for line in godmdin_list: 

230 godmdin.write(line) 

231 

232 godmdin.write("&END\n") 

233 

234 return str(self.output_godmdin_path) 

235 

236 @launchlogger 

237 def launch(self): 

238 """Launches the execution of the GOdMDRun module.""" 

239 

240 # check input/output paths and parameters 

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

242 

243 # Setup Biobb 

244 if self.check_restart(): 

245 return 0 

246 self.stage_files() 

247 

248 # Creating GOdMD input file 

249 self.output_godmdin_path = self.create_godmdin( 

250 path=str(Path(self.stage_io_dict["unique_dir"]).joinpath("godmd.in")) 

251 ) 

252 

253 # Command line 

254 # discrete -i $fileName.in -pdbin $pdbch1 -pdbtarg $pdbch2 -ener $fileName.ene -trj $fileName.crd -p1 $alignFile1 -p2 $alignFile2 -o $fileName.log >& $fileName.out 

255 self.cmd = [ 

256 "cd", 

257 self.stage_io_dict["unique_dir"], 

258 ";", 

259 self.binary_path, 

260 "-i", 

261 "godmd.in", 

262 "-pdbin", 

263 PurePath(self.stage_io_dict["in"]["input_pdb_orig_path"]).name, 

264 "-pdbtarg", 

265 PurePath(self.stage_io_dict["in"]["input_pdb_target_path"]).name, 

266 "-p1", 

267 PurePath(self.stage_io_dict["in"]["input_aln_orig_path"]).name, 

268 "-p2", 

269 PurePath(self.stage_io_dict["in"]["input_aln_target_path"]).name, 

270 "-o", 

271 PurePath(self.stage_io_dict["out"]["output_log_path"]).name, 

272 "-ener", 

273 PurePath(self.stage_io_dict["out"]["output_ene_path"]).name, 

274 "-trj", 

275 PurePath(self.stage_io_dict["out"]["output_trj_path"]).name, 

276 ] 

277 

278 # Run Biobb block 

279 self.run_biobb() 

280 

281 # Copy outputs from temporary folder to output path 

282 shutil.copy2( 

283 str(Path(self.stage_io_dict["unique_dir"]).joinpath("reference.pdb")), 

284 PurePath(self.io_dict["out"]["output_pdb_path"]), 

285 ) 

286 

287 # Copy files to host 

288 self.copy_to_host() 

289 

290 # Remove temporary folder(s) 

291 self.remove_tmp_files() 

292 

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

294 

295 return self.return_code 

296 

297 

298def godmd_run( 

299 input_pdb_orig_path: str, 

300 input_pdb_target_path: str, 

301 input_aln_orig_path: str, 

302 input_aln_target_path: str, 

303 output_log_path: str, 

304 output_ene_path: str, 

305 output_trj_path: str, 

306 output_pdb_path: str, 

307 input_config_path: Optional[str] = None, 

308 properties: Optional[dict] = None, 

309 **kwargs, 

310) -> int: 

311 """Create :class:`GOdMDRun <godmd.godmd_run.GOdMDRun>`godmd.godmd_run.GOdMDRun class and 

312 execute :meth:`launch() <godmd.godmd_run.GOdMDRun.launch>` method""" 

313 return GOdMDRun(**dict(locals())).launch() 

314 

315 

316godmd_run.__doc__ = GOdMDRun.__doc__ 

317main = GOdMDRun.get_main(godmd_run, "Computing conformational transition trajectories for proteins using GOdMD tool.") 

318 

319if __name__ == "__main__": 

320 main()