Coverage for biobb_flexdyn / flexdyn / imod_imode.py: 95%
39 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-16 13:07 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-16 13:07 +0000
1#!/usr/bin/env python3
3"""Module containing the imode class and the command line interface."""
4from typing import Optional
5import shutil
6from pathlib import PurePath
7from biobb_common.tools import file_utils as fu
8from biobb_common.generic.biobb_object import BiobbObject
9from biobb_common.tools.file_utils import launchlogger
12class ImodImode(BiobbObject):
13 """
14 | biobb_flexdyn imod_imode
15 | Wrapper of the imode tool
16 | Compute the normal modes of a macromolecule using the imode tool from the iMODS package.
18 Args:
19 input_pdb_path (str): Input PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/data/flexdyn/structure.pdb>`_. Accepted formats: pdb (edam:format_1476).
20 output_dat_path (str): Output dat with normal modes. File type: output. `Sample file <https://github.com/bioexcel/biobb_flexdyn/raw/master/biobb_flexdyn/test/reference/flexdyn/imod_imode_evecs.dat>`_. Accepted formats: dat (edam:format_1637), txt (edam:format_2330).
21 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
22 * **cg** (*int*) - (2) Coarse-Grained model. Values: 0 (CA), 1 (C5), 2 (Heavy atoms).
23 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
24 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
25 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
27 Examples:
28 This is a use example of how to use the building block from Python::
30 from biobb_flexdyn.flexdyn.imod_imode import imod_imode
31 prop = {
32 'cg' : 2
33 }
34 imod_imode( input_pdb_path='/path/to/structure.pdb',
35 output_dat_path='/path/to/output_evecs.dat',
36 properties=prop)
38 Info:
39 * wrapped_software:
40 * name: iMODS
41 * version: >=1.0.4
42 * license: other
43 * ontology:
44 * name: EDAM
45 * schema: http://edamontology.org/EDAM.owl
47 """
49 def __init__(self, input_pdb_path: str, output_dat_path: str,
50 properties: Optional[dict] = None, **kwargs) -> None:
52 properties = properties or {}
54 # Call parent class constructor
55 super().__init__(properties)
56 self.locals_var_dict = locals().copy()
58 # Input/Output files
59 self.io_dict = {
60 'in': {'input_pdb_path': input_pdb_path},
61 'out': {'output_dat_path': output_dat_path}
62 }
64 # Properties specific for BB
65 self.properties = properties
66 self.binary_path = properties.get('binary_path', 'imode_gcc')
68 self.cg = properties.get('cg', 2)
70 # Check the properties
71 self.check_properties(properties)
72 self.check_arguments()
74 @launchlogger
75 def launch(self):
76 """Launches the execution of the FlexDyn iMOD imode module."""
78 # Setup Biobb
79 if self.check_restart():
80 return 0
81 # self.stage_files()
83 # Manually creating a Sandbox to avoid issues with input parameters buffer overflow:
84 # Long strings defining a file path makes Fortran or C compiled programs crash if the string
85 # declared is shorter than the input parameter path (string) length.
86 # Generating a temporary folder and working inside this folder (sandbox) fixes this problem.
87 # The problem was found in Galaxy executions, launching Singularity containers (May 2023).
89 # Creating temporary folder
90 self.tmp_folder = fu.create_unique_dir()
91 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log)
93 shutil.copy2(self.io_dict["in"]["input_pdb_path"], self.tmp_folder)
95 # Output temporary file
96 # out_file_prefix = Path(self.stage_io_dict.get("unique_dir", "")).joinpath("imods_evecs")
97 # out_file = Path(self.stage_io_dict.get("unique_dir", "")).joinpath("imods_evecs_ic.evec")
98 out_file_prefix = "imods_evecs" # Needed as imod is appending the _ic.evec extension
99 out_file = "imods_evecs_ic.evec"
101 # Command line
102 # imode_gcc 1ake_backbone.pdb -m 0 -o patata.evec
103 # self.cmd = [self.binary_path,
104 # str(Path(self.stage_io_dict["in"]["input_pdb_path"]).relative_to(Path.cwd())),
105 # "-o", str(out_file_prefix),
106 # "-m", str(self.cg)
107 # ]
109 self.cmd = ['cd', self.tmp_folder, ';',
110 self.binary_path,
111 PurePath(self.io_dict["in"]["input_pdb_path"]).name,
112 '-o', out_file_prefix,
113 '-m', str(self.cg)
114 ]
116 # Run Biobb block
117 self.run_biobb()
119 # Copying generated output file to the final (user-given) file name
120 # shutil.copy2(out_file, self.stage_io_dict["out"]["output_dat_path"])
122 # Copy outputs from temporary folder to output path
123 shutil.copy2(PurePath(self.tmp_folder).joinpath(out_file), PurePath(self.io_dict["out"]["output_dat_path"]))
125 # Copy files to host
126 # self.copy_to_host()
128 # remove temporary folder(s)
129 self.tmp_files.extend([self.tmp_folder])
130 self.remove_tmp_files()
132 self.check_arguments(output_files_created=True, raise_exception=False)
134 return self.return_code
137def imod_imode(input_pdb_path: str, output_dat_path: str,
138 properties: Optional[dict] = None, **kwargs) -> int:
139 """Create :class:`ImodImode <flexdyn.imod_imode.ImodImode>`flexdyn.imod_imode.ImodImode class and
140 execute :meth:`launch() <flexdyn.imod_imode.ImodImode.launch>` method"""
141 return ImodImode(**dict(locals())).launch()
144imod_imode.__doc__ = ImodImode.__doc__
145main = ImodImode.get_main(imod_imode, "Compute the normal modes of a macromolecule using the imode tool from the iMODS package.")
147if __name__ == '__main__':
148 main()