Coverage for biobb_gromacs / gromacs / grompp_mdrun.py: 97%
60 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 08:26 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-05 08:26 +0000
1#!/usr/bin/env python3
3"""Module containing the GromppMDrun class and the command line interface."""
4from typing import Optional
5from pathlib import Path
6from biobb_common.generic.biobb_object import BiobbObject
7from biobb_common.tools import file_utils as fu
8from biobb_common.tools.file_utils import launchlogger
9from biobb_gromacs.gromacs.grompp import grompp
10from biobb_gromacs.gromacs.mdrun import mdrun
13class GromppMdrun(BiobbObject):
14 """
15 | biobb_gromacs GromppMdrun
16 | Wrapper of the `GROMACS grompp <http://manual.gromacs.org/current/onlinehelp/gmx-grompp.html>`_ module and the `GROMACS mdrun <http://manual.gromacs.org/current/onlinehelp/gmx-mdrun.html>`_ module.
17 | Grompp The GROMACS preprocessor module needs to be fed with the input system and the dynamics parameters to create a portable binary run input file TPR. The simulation parameters can be specified by two methods: 1.The predefined mdp settings defined at simulation_type property or 2.A custom mdp file defined at the input_mdp_path argument. These two methods are mutually exclusive. In both cases can be further modified by adding parameters to the mdp section in the yaml configuration file. The simulation parameter names and default values can be consulted in the `official MDP specification <http://manual.gromacs.org/current/user-guide/mdp-options.html>`_. MDRun is the main computational chemistry engine within GROMACS. It performs Molecular Dynamics simulations, but it can also perform Stochastic Dynamics, Energy Minimization, test particle insertion or (re)calculation of energies.
19 Args:
20 input_gro_path (str): Path to the input GROMACS structure GRO file. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/grompp.gro>`_. Accepted formats: gro (edam:format_2033).
21 input_top_zip_path (str): Path to the input GROMACS topology TOP and ITP files in zip format. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/grompp.zip>`_. Accepted formats: zip (edam:format_3987).
22 output_trr_path (str): Path to the GROMACS uncompressed raw trajectory file TRR. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_mdrun.trr>`_. Accepted formats: trr (edam:format_3910).
23 output_gro_path (str): Path to the output GROMACS structure GRO file. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_mdrun.gro>`_. Accepted formats: gro (edam:format_2033).
24 output_edr_path (str): Path to the output GROMACS portable energy file EDR. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_mdrun.edr>`_. Accepted formats: edr (edam:format_2330).
25 output_log_path (str): Path to the output GROMACS trajectory log file LOG. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_gmx_mdrun.log>`_. Accepted formats: log (edam:format_2330).
26 input_cpt_path (str) (Optional): Path to the input GROMACS checkpoint file CPT. File type: input. Accepted formats: cpt (edam:format_2333).
27 input_ndx_path (str) (Optional): Path to the input GROMACS index files NDX. File type: input. Accepted formats: ndx (edam:format_2033).
28 input_mdp_path (str) (Optional): Path to the input GROMACS `MDP file <http://manual.gromacs.org/current/user-guide/mdp-options.html>`_. File type: input. Accepted formats: mdp (edam:format_2330).
29 output_xtc_path (str) (Optional): Path to the GROMACS compressed trajectory file XTC. File type: output. Accepted formats: xtc (edam:format_3875).
30 output_cpt_path (str) (Optional): Path to the output GROMACS checkpoint file CPT. File type: output. Accepted formats: cpt (edam:format_2333).
31 output_dhdl_path (str) (Optional): Path to the output dhdl.xvg file only used when free energy calculation is turned on. File type: output. Accepted formats: xvg (edam:format_2033).
32 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
33 * **mdp** (*dict*) - ({}) MDP options specification.
34 * **simulation_type** (*str*) - ("minimization") Default options for the mdp file. Each creates a different mdp file. Values: `minimization <https://biobb-gromacs.readthedocs.io/en/latest/_static/mdp/minimization.mdp>`_ (Energy minimization using steepest descent algorithm is used), `nvt <https://biobb-gromacs.readthedocs.io/en/latest/_static/mdp/nvt.mdp>`_ (substance N Volume V and Temperature T are conserved), `npt <https://biobb-gromacs.readthedocs.io/en/latest/_static/mdp/npt.mdp>`_ (substance N pressure P and Temperature T are conserved), `free <https://biobb-gromacs.readthedocs.io/en/latest/_static/mdp/free.mdp>`_ (No design constraints applied; Free MD), `ions <https://biobb-gromacs.readthedocs.io/en/latest/_static/mdp/minimization.mdp>`_ (Synonym of minimization), index (Creates an empty mdp file).
35 * **maxwarn** (*int*) - (10) [0~1000|1] Maximum number of allowed warnings.
36 * **mpi_bin** (*str*) - (None) Path to the MPI runner. Usually "mpirun" or "srun".
37 * **mpi_np** (*str*) - (None) Number of MPI processes. Usually an integer bigger than 1.
38 * **mpi_hostlist** (*str*) - (None) Path to the MPI hostlist file.
39 * **checkpoint_time** (*int*) - (15) [0~1000|1] Checkpoint writing interval in minutes. Only enabled if an output_cpt_path is provided.
40 * **num_threads** (*int*) - (0) [0-1000|1] Let GROMACS guess. The number of threads that are going to be used.
41 * **num_threads_mpi** (*int*) - (0) [0-1000|1] Let GROMACS guess. The number of GROMACS MPI threads that are going to be used.
42 * **num_threads_omp** (*int*) - (0) [0-1000|1] Let GROMACS guess. The number of GROMACS OPENMP threads that are going to be used.
43 * **num_threads_omp_pme** (*int*) - (0) [0-1000|1] Let GROMACS guess. The number of GROMACS OPENMP_PME threads that are going to be used.
44 * **use_gpu** (*bool*) - (False) Use settings appropriate for GPU. Adds: -nb gpu -pme gpu
45 * **gpu_id** (*str*) - (None) list of unique GPU device IDs available to use.
46 * **gpu_tasks** (*str*) - (None) list of GPU device IDs, mapping each PP task on each node to a device.
47 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable.
48 * **binary_path** (*str*) - ("gmx") Path to the GROMACS executable binary.
49 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
50 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
51 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
52 * **container_path** (*str*) - (None) Path to the binary executable of your container.
53 * **container_image** (*str*) - ("gromacs/gromacs:latest") Container Image identifier.
54 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container.
55 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container.
56 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container.
57 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell.
59 Examples:
60 This is a use example of how to use the building block from Python::
62 from biobb_gromacs.gromacs.grompp_mdrun import grompp_mdrun
63 prop = { 'num_threads': 0,
64 'binary_path': 'gmx',
65 'mdp':
66 { 'simulation_type': 'minimization',
67 'emtol':'500',
68 'nsteps':'5000'}
69 }
70 grompp_mdrun(input_gro_path='/path/to/myStructure.gro',
71 input_top_zip_path='/path/to/myTopology.zip',
72 output_trr_path='/path/to/newTrajectory.trr',
73 output_gro_path='/path/to/newStructure.gro',
74 output_edr_path='/path/to/newEnergy.edr',
75 output_log_path='/path/to/newSimulationLog.log',
76 properties=prop)
78 Info:
79 * wrapped_software:
80 * name: GROMACS Grompp & MDRun
81 * version: 2025.2
82 * license: LGPL 2.1
83 * ontology:
84 * name: EDAM
85 * schema: http://edamontology.org/EDAM.owl
86 """
88 def __init__(self, input_gro_path: str, input_top_zip_path: str, output_trr_path: str,
89 output_gro_path: str, output_edr_path: str, output_log_path: str,
90 input_cpt_path: Optional[str] = None, input_ndx_path: Optional[str] = None, input_mdp_path: Optional[str] = None,
91 output_xtc_path: Optional[str] = None, output_cpt_path: Optional[str] = None, output_dhdl_path: Optional[str] = None,
92 output_tpr_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> None:
93 # Properties management
94 properties = properties or {}
96 # Call parent class constructor
97 super().__init__(properties)
98 self.locals_var_dict = locals().copy()
100 grompp_properties_keys = ['mdp', 'maxwarn', 'simulation_type']
101 mdrun_properties_keys = ['mpi_bin', 'mpi_np', 'mpi_hostlist', 'checkpoint_time', 'num_threads', 'num_threads_mpi', 'num_threads_omp', 'num_threads_omp_pme', 'use_gpu', 'gpu_id', 'gpu_tasks', 'dev']
102 self.properties_grompp = {}
103 self.properties_mdrun = {}
104 if properties:
105 self.global_log = properties.get('global_log', None)
106 self.properties_grompp = properties.copy()
107 for key in mdrun_properties_keys:
108 self.properties_grompp.pop(key, None)
109 self.properties_mdrun = properties.copy()
110 for key in grompp_properties_keys:
111 self.properties_mdrun.pop(key, None)
113 # Grompp arguments
114 self.input_gro_path = input_gro_path
115 self.input_top_zip_path = input_top_zip_path
116 self.output_tpr_path = output_tpr_path
117 if not self.output_tpr_path:
118 self.output_tpr_path = str(Path(fu.create_unique_dir()).joinpath('internal.tpr'))
119 self.input_cpt_path = input_cpt_path
120 self.input_ndx_path = input_ndx_path
121 self.input_mdp_path = input_mdp_path
123 # MDRun arguments
124 self.input_tpr_path = self.output_tpr_path
125 self.output_trr_path = output_trr_path
126 self.output_gro_path = output_gro_path
127 self.output_edr_path = output_edr_path
128 self.output_log_path = output_log_path
129 self.output_xtc_path = output_xtc_path
130 self.output_cpt_path = output_cpt_path
131 self.output_dhdl_path = output_dhdl_path
133 @launchlogger
134 def launch(self) -> int:
135 """Execute the :class:`GromppMdrun <gromacs.grompp_mdrun.GromppMdrun>` object."""
137 fu.log('Calling Grompp class', self.out_log, self.global_log)
138 grompp_return_code = grompp(input_gro_path=self.input_gro_path, input_top_zip_path=self.input_top_zip_path,
139 output_tpr_path=self.output_tpr_path, input_cpt_path=self.input_cpt_path, # type: ignore
140 input_ndx_path=self.input_ndx_path, input_mdp_path=self.input_mdp_path,
141 properties=self.properties_grompp)
142 fu.log(f'Grompp return code: {grompp_return_code}', self.out_log, self.global_log)
144 if not grompp_return_code:
145 fu.log('Grompp return code is correct. Calling MDRun class', self.out_log, self.global_log)
146 mdrun_return_code = mdrun(input_tpr_path=self.output_tpr_path, output_trr_path=self.output_trr_path, # type: ignore
147 output_gro_path=self.output_gro_path, output_edr_path=self.output_edr_path,
148 output_log_path=self.output_log_path, output_xtc_path=self.output_xtc_path,
149 output_cpt_path=self.output_cpt_path, output_dhdl_path=self.output_dhdl_path,
150 properties=self.properties_mdrun)
151 fu.log(f'MDRun return code: {mdrun_return_code}', self.out_log, self.global_log)
152 else:
153 return 1
155 # Remove temporal files
156 self.tmp_files.append(Path(str(self.output_tpr_path)).parent)
157 self.remove_tmp_files()
159 self.check_arguments(output_files_created=True, raise_exception=False)
160 return mdrun_return_code
163def grompp_mdrun(input_gro_path: str, input_top_zip_path: str, output_trr_path: str,
164 output_gro_path: str, output_edr_path: str, output_log_path: str,
165 input_cpt_path: Optional[str] = None, input_ndx_path: Optional[str] = None, input_mdp_path: Optional[str] = None,
166 output_xtc_path: Optional[str] = None, output_cpt_path: Optional[str] = None, output_dhdl_path: Optional[str] = None,
167 output_tpr_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> int:
168 return GromppMdrun(**dict(locals())).launch()
171grompp_mdrun.__doc__ = GromppMdrun.__doc__
172main = GromppMdrun.get_main(grompp_mdrun, "Wrapper for the GROMACS grompp_mdrun module.")
175if __name__ == '__main__':
176 main()