Coverage for biobb_gromacs/gromacs/convert_tpr.py: 0%
61 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-10-21 09:54 +0000
« prev ^ index » next coverage.py v7.10.4, created at 2025-10-21 09:54 +0000
1#!/usr/bin/env python3
3"""Module containing the Convert_tpr 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_gromacs.gromacs.common import get_gromacs_version
12class ConvertTpr(BiobbObject):
13 """
14 | biobb_gromacs ConvertTpr
15 | Wrapper of the `GROMACS convert-tpr <https://manual.gromacs.org/current/onlinehelp/gmx-convert-tpr.html>`_ module.
16 | The GROMACS convert-tpr module can edit run input files (.tpr)
18 Args:
19 input_tpr_path (str): Path to the input portable binary run file TPR. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_grompp.tpr>`_. Accepted formats: tpr (edam:format_2333).
20 output_tpr_path (str): Path to the output portable binary run file TPR. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_grompp.tpr>`_. Accepted formats: tpr (edam:format_2333).
21 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
22 * **extend** (*int*) - (0) Extend the runtime by this amount (ps).
23 * **until** (*int*) - (0) Extend the runtime until this ending time (ps).
24 * **nsteps** (*int*) - (0) Change the number of steps remaining to be made.
25 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable.
26 * **binary_path** (*str*) - ("gmx") Path to the GROMACS executable binary.
27 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
28 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
29 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
30 * **container_path** (*str*) - (None) Path to the binary executable of your container.
31 * **container_image** (*str*) - ("gromacs/gromacs:latest") Container Image identifier.
32 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container.
33 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container.
34 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container.
35 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell.
37 Examples:
38 This is a use example of how to use the building block from Python::
40 from biobb_gromacs.gromacs.convert_tpr import convert_tpr
42 prop = { 'extend': 100000}
43 convert_tpr(input_tpr_path='/path/to/myStructure.tpr',
44 output_tpr_path='/path/to/newCompiledBin.tpr',
45 properties=prop)
47 Info:
48 * wrapped_software:
49 * name: GROMACS Convert-tpr
50 * version: 2025.2
51 * license: LGPL 2.1
52 * ontology:
53 * name: EDAM
54 * schema: http://edamontology.org/EDAM.owl
55 """
57 def __init__(self, input_tpr_path: str, output_tpr_path: str,
58 properties: Optional[dict] = None, **kwargs) -> None:
59 properties = properties or {}
61 # Call parent class constructor
62 super().__init__(properties)
63 self.locals_var_dict = locals().copy()
65 # Input/Output files
66 self.io_dict = {
67 "in": {"input_tpr_path": input_tpr_path},
68 "out": {"output_tpr_path": output_tpr_path}
69 }
71 # Properties specific for BB
72 self.extend = properties.get('extend')
73 self.until = properties.get('until')
74 self.nsteps = properties.get('nsteps')
76 # Properties common in all GROMACS BB
77 self.gmx_lib = properties.get('gmx_lib', None)
78 self.binary_path = properties.get('binary_path', 'gmx')
79 self.gmx_nobackup = properties.get('gmx_nobackup', True)
80 self.gmx_nocopyright = properties.get('gmx_nocopyright', True)
81 if self.gmx_nobackup:
82 self.binary_path += ' -nobackup'
83 if self.gmx_nocopyright:
84 self.binary_path += ' -nocopyright'
85 if not self.container_path:
86 self.gmx_version = get_gromacs_version(self.binary_path)
88 # Check the properties
89 self.check_properties(properties)
90 self.check_arguments()
92 @launchlogger
93 def launch(self) -> int:
94 """Execute the :class:`ConvertTpr <gromacs.convert_tpr.ConvertTpr>` object."""
96 # Setup Biobb
97 if self.check_restart():
98 return 0
99 self.stage_files()
101 self.cmd = [self.binary_path, 'convert-tpr',
102 '-s', self.stage_io_dict["in"]["input_tpr_path"],
103 '-o', self.stage_io_dict["out"]["output_tpr_path"]
104 ]
106 if self.extend:
107 self.cmd.extend(['-extend', str(self.extend)])
108 if self.until:
109 self.cmd.extend(['-until', str(self.until)])
110 if self.nsteps:
111 self.cmd.extend(['-nsteps', str(self.nsteps)])
113 if self.gmx_lib:
114 self.env_vars_dict['GMXLIB'] = self.gmx_lib
116 # Run Biobb block
117 self.run_biobb()
119 # Copy files to host
120 self.copy_to_host()
122 self.remove_tmp_files()
124 self.check_arguments(output_files_created=True, raise_exception=False)
125 return self.return_code
128def convert_tpr(input_tpr_path: str, output_tpr_path: str, properties: Optional[dict] = None, **kwargs) -> int:
129 """Create :class:`ConvertTpr <gromacs.convert_tpr.ConvertTpr>` class and
130 execute the :meth:`launch() <gromacs.convert_tpr.ConvertTpr.launch>` method."""
132 return ConvertTpr(input_tpr_path=input_tpr_path, output_tpr_path=output_tpr_path,
133 properties=properties, **kwargs).launch()
136convert_tpr.__doc__ = ConvertTpr.__doc__
139def main():
140 """Command line execution of this building block. Please check the command line documentation."""
141 parser = argparse.ArgumentParser(description="Wrapper for the GROMACS convert-tpr module.",
142 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
143 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string")
145 # Specific args of each building block
146 required_args = parser.add_argument_group('required arguments')
147 required_args.add_argument('--input_tpr_path', required=True)
148 required_args.add_argument('--output_tpr_path', required=True)
150 args = parser.parse_args()
151 config = args.config if args.config else None
152 properties = settings.ConfReader(config=config).get_prop_dic()
154 # Specific call of each building block
155 convert_tpr(input_tpr_path=args.input_tpr_path, output_tpr_path=args.output_tpr_path,
156 properties=properties)
158if __name__ == '__main__':
159 main()