Coverage for biobb_godmd/godmd/godmd_run.py: 83%
78 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-05-28 10:14 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-05-28 10:14 +0000
1#!/usr/bin/env python3
3"""Module containing the GOdMDRun class and the command line interface."""
5import shutil
6from pathlib import Path, PurePath
7from typing import Optional
9from biobb_common.generic.biobb_object import BiobbObject
10from biobb_common.tools.file_utils import launchlogger
12from biobb_godmd.godmd.common import check_input_path, check_output_path
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.
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 * **container_path** (*str*) - (None) Container path definition.
38 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition.
39 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
40 * **container_working_dir** (*str*) - (None) Container working directory definition.
41 * **container_user_id** (*str*) - (None) Container user_id definition.
42 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
44 Examples:
45 This is a use example of how to use the building block from Python::
47 from biobb_godmd.godmd.godmd_run import godmd_run
48 prop = {
49 'remove_tmp': True
50 }
51 godmd_run( input_pdb_orig_path='/path/to/pdb_orig.pdb',
52 input_pdb_target_path='/path/to/pdb_target.pdb',
53 input_aln_orig_path='/path/to/aln_orig.aln',
54 input_aln_target_path='/path/to/aln_target.aln',
55 output_log_path='/path/to/godmd_log.log',
56 output_ene_path='/path/to/godmd_ene.txt',
57 output_trj_path='/path/to/godmd_trj.mdcrd',
58 output_pdb_path='/path/to/godmd_pdb.pdb',
59 properties=prop)
61 Info:
62 * wrapped_software:
63 * name: GOdMD
64 * version: >=1.0
65 * license: Apache-2.0
66 * ontology:
67 * name: EDAM
68 * schema: http://edamontology.org/EDAM.owl
70 """
72 def __init__(
73 self,
74 input_pdb_orig_path: str,
75 input_pdb_target_path: str,
76 input_aln_orig_path: str,
77 input_aln_target_path: str,
78 input_config_path: Optional[str],
79 output_log_path: str,
80 output_ene_path: str,
81 output_trj_path: str,
82 output_pdb_path: str,
83 properties: Optional[dict] = None,
84 **kwargs,
85 ) -> None:
86 properties = properties or {}
88 # Call parent class constructor
89 super().__init__(properties)
90 self.locals_var_dict = locals().copy()
92 # Input/Output files
93 self.io_dict = {
94 "in": {
95 "input_pdb_orig_path": input_pdb_orig_path,
96 "input_pdb_target_path": input_pdb_target_path,
97 "input_aln_orig_path": input_aln_orig_path,
98 "input_aln_target_path": input_aln_target_path,
99 "input_config_path": input_config_path,
100 },
101 "out": {
102 "output_log_path": output_log_path,
103 "output_ene_path": output_ene_path,
104 "output_trj_path": output_trj_path,
105 "output_pdb_path": output_pdb_path,
106 },
107 }
109 # Properties specific for BB
110 self.properties = properties
111 self.godmdin = {k: str(v) for k, v in properties.get("godmdin", dict()).items()}
112 self.binary_path = properties.get("binary_path", "discrete")
114 # Check the properties
115 self.check_properties(properties)
116 # self.check_arguments()
118 def check_data_params(self, out_log, out_err):
119 """Checks input/output paths correctness"""
121 # Check input(s)
122 self.io_dict["in"]["input_pdb_orig_path"] = check_input_path(
123 self.io_dict["in"]["input_pdb_orig_path"],
124 "input_pdb_orig_path",
125 False,
126 out_log,
127 self.__class__.__name__,
128 )
129 self.io_dict["in"]["input_pdb_target_path"] = check_input_path(
130 self.io_dict["in"]["input_pdb_target_path"],
131 "input_pdb_target_path",
132 False,
133 out_log,
134 self.__class__.__name__,
135 )
136 self.io_dict["in"]["input_aln_orig_path"] = check_input_path(
137 self.io_dict["in"]["input_aln_orig_path"],
138 "input_aln_orig_path",
139 False,
140 out_log,
141 self.__class__.__name__,
142 )
143 self.io_dict["in"]["input_aln_target_path"] = check_input_path(
144 self.io_dict["in"]["input_aln_target_path"],
145 "input_aln_target_path",
146 False,
147 out_log,
148 self.__class__.__name__,
149 )
150 self.io_dict["in"]["input_config_path"] = check_input_path(
151 self.io_dict["in"]["input_config_path"],
152 "input_config_path",
153 True,
154 out_log,
155 self.__class__.__name__,
156 )
158 # Check output(s)
159 self.io_dict["out"]["output_log_path"] = check_output_path(
160 self.io_dict["out"]["output_log_path"],
161 "output_log_path",
162 False,
163 out_log,
164 self.__class__.__name__,
165 )
166 self.io_dict["out"]["output_ene_path"] = check_output_path(
167 self.io_dict["out"]["output_ene_path"],
168 "output_ene_path",
169 False,
170 out_log,
171 self.__class__.__name__,
172 )
173 self.io_dict["out"]["output_trj_path"] = check_output_path(
174 self.io_dict["out"]["output_trj_path"],
175 "output_trj_path",
176 False,
177 out_log,
178 self.__class__.__name__,
179 )
180 self.io_dict["out"]["output_pdb_path"] = check_output_path(
181 self.io_dict["out"]["output_pdb_path"],
182 "output_pdb_path",
183 False,
184 out_log,
185 self.__class__.__name__,
186 )
188 def create_godmdin(self, path: Optional[str] = None) -> str:
189 """Creates a GOdMD configuration file (godmdin) using the properties file settings"""
190 godmdin_list = []
192 self.output_godmdin_path = path
194 if self.io_dict["in"]["input_config_path"]:
195 # GOdMD input parameters read from an input godmdin file
196 with open(self.io_dict["in"]["input_config_path"]) as input_params:
197 for line in input_params:
198 if "=" in line:
199 godmdin_list.append(line.upper())
200 else:
201 # Pre-configured simulation type parameters
202 godmdin_list.append(" TSNAP = 500 ! BioBB GOdMD default params \n")
203 godmdin_list.append(" TEMP = 300 ! BioBB GOdMD default params \n")
204 godmdin_list.append(" SEED = 2525 ! BioBB GOdMD default params \n")
205 godmdin_list.append(" ENER_EVO_SIZE = 20 ! BioBB GOdMD default params \n")
206 godmdin_list.append(" NBLOC = 10000 ! BioBB GOdMD default params \n")
207 godmdin_list.append(
208 " ERRORACCEPTABLE = 1.5 ! BioBB GOdMD default params \n"
209 )
211 # Adding the rest of parameters in the config file to the mdin file
212 # if the parameter has already been added replace the value
213 parameter_keys = [parameter.split("=")[0].strip() for parameter in godmdin_list]
214 for k, v in self.godmdin.items():
215 config_parameter_key = str(k).strip().upper()
216 if config_parameter_key in parameter_keys:
217 godmdin_list[parameter_keys.index(config_parameter_key)] = (
218 "\t" + config_parameter_key + " = " + str(v) + " ! BioBB property \n"
219 )
220 else:
221 godmdin_list.append(
222 "\t" + config_parameter_key + " = " + str(v) + " ! BioBB property \n"
223 )
225 # Writing MD configuration file (mdin)
226 with open(str(self.output_godmdin_path), "w") as godmdin:
227 # GOdMDIN parameters added by the biobb_godmd module
228 godmdin.write(
229 "!This godmdin file has been created by the biobb_godmd module from the BioBB library \n\n"
230 )
232 godmdin.write("&INPUT\n")
234 # MD config parameters
235 for line in godmdin_list:
236 godmdin.write(line)
238 godmdin.write("&END\n")
240 return str(self.output_godmdin_path)
242 @launchlogger
243 def launch(self):
244 """Launches the execution of the GOdMDRun module."""
246 # check input/output paths and parameters
247 self.check_data_params(self.out_log, self.err_log)
249 # Setup Biobb
250 if self.check_restart():
251 return 0
252 self.stage_files()
254 if self.container_path:
255 working_dir = self.container_volume_path if self.container_volume_path else "/data"
256 else:
257 working_dir = self.stage_io_dict.get("unique_dir", "")
259 # Creating GOdMD input file
260 self.output_godmdin_path = self.create_godmdin(
261 path=str(Path(self.stage_io_dict["unique_dir"]).joinpath("godmd.in"))
262 )
264 # Command line
265 # discrete -i $fileName.in -pdbin $pdbch1 -pdbtarg $pdbch2 -ener $fileName.ene -trj $fileName.crd -p1 $alignFile1 -p2 $alignFile2 -o $fileName.log >& $fileName.out
266 self.cmd = [
267 "cd",
268 working_dir,
269 ";",
270 self.binary_path,
271 "-i",
272 PurePath(self.output_godmdin_path).name,
273 "-pdbin",
274 PurePath(self.stage_io_dict["in"]["input_pdb_orig_path"]).name,
275 "-pdbtarg",
276 PurePath(self.stage_io_dict["in"]["input_pdb_target_path"]).name,
277 "-p1",
278 PurePath(self.stage_io_dict["in"]["input_aln_orig_path"]).name,
279 "-p2",
280 PurePath(self.stage_io_dict["in"]["input_aln_target_path"]).name,
281 "-o",
282 PurePath(self.stage_io_dict["out"]["output_log_path"]).name,
283 "-ener",
284 PurePath(self.stage_io_dict["out"]["output_ene_path"]).name,
285 "-trj",
286 PurePath(self.stage_io_dict["out"]["output_trj_path"]).name,
287 ]
289 # Run Biobb block
290 self.run_biobb()
292 # Stage the fixed-name output file so copy_to_host can remap it correctly.
293 generated_output = Path(self.stage_io_dict.get("unique_dir", "")).joinpath("reference.pdb")
294 staged_output = Path(self.stage_io_dict.get("unique_dir", "")).joinpath(
295 Path(self.stage_io_dict["out"]["output_pdb_path"]).name
296 )
297 shutil.copy2(str(generated_output), str(staged_output))
299 # Copy files to host
300 self.copy_to_host()
302 # Remove temporary folder(s)
303 self.remove_tmp_files()
305 self.check_arguments(output_files_created=True, raise_exception=False)
307 return self.return_code
310def godmd_run(
311 input_pdb_orig_path: str,
312 input_pdb_target_path: str,
313 input_aln_orig_path: str,
314 input_aln_target_path: str,
315 output_log_path: str,
316 output_ene_path: str,
317 output_trj_path: str,
318 output_pdb_path: str,
319 input_config_path: Optional[str] = None,
320 properties: Optional[dict] = None,
321 **kwargs,
322) -> int:
323 """Create :class:`GOdMDRun <godmd.godmd_run.GOdMDRun>`godmd.godmd_run.GOdMDRun class and
324 execute :meth:`launch() <godmd.godmd_run.GOdMDRun.launch>` method"""
325 return GOdMDRun(**dict(locals())).launch()
328godmd_run.__doc__ = GOdMDRun.__doc__
329main = GOdMDRun.get_main(godmd_run, "Computing conformational transition trajectories for proteins using GOdMD tool.")
331if __name__ == "__main__":
332 main()