Coverage for biobb_gromacs / gromacs / grompp.py: 79%

71 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-05 08:26 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5import shutil 

6from pathlib import Path 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_gromacs.gromacs.common import get_gromacs_version 

11from biobb_gromacs.gromacs.common import create_mdp 

12from biobb_gromacs.gromacs.common import mdp_preset 

13 

14 

15class Grompp(BiobbObject): 

16 """ 

17 | biobb_gromacs Grompp 

18 | Wrapper of the `GROMACS grompp <http://manual.gromacs.org/current/onlinehelp/gmx-grompp.html>`_ module. 

19 | 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>`_. 

20 

21 Args: 

22 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). 

23 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). 

24 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). 

25 input_cpt_path (str) (Optional): Path to the input GROMACS checkpoint file CPT. File type: input. Accepted formats: cpt (edam:format_2333). 

26 input_ndx_path (str) (Optional): Path to the input GROMACS index files NDX. File type: input. Accepted formats: ndx (edam:format_2033). 

27 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). 

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

29 * **mdp** (*dict*) - ({}) MDP options specification. 

30 * **simulation_type** (*str*) - (None) Default options for the mdp file. Each one 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). 

31 * **maxwarn** (*int*) - (0) [0~1000|1] Maximum number of allowed warnings. If simulation_type is index default is 10. 

32 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable. 

33 * **binary_path** (*str*) - ("gmx") Path to the GROMACS executable binary. 

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) Path to the binary executable of your container. 

38 * **container_image** (*str*) - ("gromacs/gromacs:latest") Container Image identifier. 

39 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container. 

40 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container. 

41 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container. 

42 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell. 

43 

44 Examples: 

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

46 

47 from biobb_gromacs.gromacs.grompp import grompp 

48 

49 prop = { 'simulation_type': 'minimization', 

50 'mdp': 

51 { 'emtol':'500', 

52 'nsteps':'5000'}} 

53 grompp(input_gro_path='/path/to/myStructure.gro', 

54 input_top_zip_path='/path/to/myTopology.zip', 

55 output_tpr_path='/path/to/newCompiledBin.tpr', 

56 properties=prop) 

57 

58 Info: 

59 * wrapped_software: 

60 * name: GROMACS Grompp 

61 * version: 2025.2 

62 * license: LGPL 2.1 

63 * ontology: 

64 * name: EDAM 

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

66 """ 

67 

68 def __init__(self, input_gro_path: str, input_top_zip_path: str, output_tpr_path: str, 

69 input_cpt_path: Optional[str] = None, input_ndx_path: Optional[str] = None, input_mdp_path: Optional[str] = None, 

70 properties: Optional[dict] = None, **kwargs) -> None: 

71 properties = properties or {} 

72 

73 # Call parent class constructor 

74 super().__init__(properties) 

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

76 

77 # Input/Output files 

78 self.io_dict = { 

79 "in": {"input_gro_path": input_gro_path, "input_cpt_path": input_cpt_path, 

80 "input_ndx_path": input_ndx_path, "input_mdp_path": input_mdp_path}, 

81 "out": {"output_tpr_path": output_tpr_path} 

82 } 

83 # Should not be copied inside container 

84 self.input_top_zip_path = input_top_zip_path 

85 

86 # Properties specific for BB 

87 self.output_mdp_path = properties.get('output_mdp_path', 'grompp.mdp') 

88 self.output_top_path = properties.get('output_top_path', 'grompp.top') 

89 self.simulation_type = properties.get('simulation_type') 

90 self.maxwarn = str(properties.get('maxwarn', 0)) 

91 if self.simulation_type and self.simulation_type != 'index': 

92 self.maxwarn = str(properties.get('maxwarn', 10)) 

93 self.mdp = {k: str(v) for k, v in properties.get('mdp', dict()).items()} 

94 

95 # Properties common in all GROMACS BB 

96 self.gmx_lib = properties.get('gmx_lib', None) 

97 self.binary_path = properties.get('binary_path', 'gmx') 

98 self.gmx_nobackup = properties.get('gmx_nobackup', True) 

99 self.gmx_nocopyright = properties.get('gmx_nocopyright', True) 

100 if self.gmx_nobackup: 

101 self.binary_path += ' -nobackup' 

102 if self.gmx_nocopyright: 

103 self.binary_path += ' -nocopyright' 

104 if not self.container_path: 

105 self.gmx_version = get_gromacs_version(self.binary_path) 

106 

107 # Check the properties 

108 self.check_properties(properties) 

109 self.check_arguments() 

110 

111 @launchlogger 

112 def launch(self) -> int: 

113 """Execute the :class:`Grompp <gromacs.grompp.Grompp>` object.""" 

114 

115 # Setup Biobb 

116 if self.check_restart(): 

117 return 0 

118 self.stage_files() 

119 

120 # Unzip topology to topology_out 

121 top_file = fu.unzip_top(zip_file=self.input_top_zip_path, out_log=self.out_log, unique_dir=self.stage_io_dict.get("unique_dir", "")) 

122 

123 # Create MDP file 

124 self.output_mdp_path = create_mdp(output_mdp_path=str(Path(self.stage_io_dict.get("unique_dir", "")).joinpath(self.output_mdp_path)), 

125 input_mdp_path=self.io_dict["in"]["input_mdp_path"], 

126 preset_dict=mdp_preset(str(self.simulation_type)), 

127 mdp_properties_dict=self.mdp) 

128 

129 # Copy extra files to container: MDP file and topology folder 

130 if self.container_path: 

131 self.output_mdp_path = str(Path(self.container_volume_path).joinpath(Path(self.output_mdp_path).name)) 

132 top_file = str(Path(self.container_volume_path).joinpath(Path(top_file).name)) 

133 

134 self.cmd = [self.binary_path, 'grompp', 

135 '-f', self.output_mdp_path, 

136 '-c', self.stage_io_dict["in"]["input_gro_path"], 

137 '-r', self.stage_io_dict["in"]["input_gro_path"], 

138 '-p', top_file, 

139 '-o', self.stage_io_dict["out"]["output_tpr_path"], 

140 '-po', self.create_tmp_file('mdout.mdp'), 

141 '-maxwarn', self.maxwarn] 

142 

143 if self.stage_io_dict["in"].get("input_cpt_path") and Path(self.stage_io_dict["in"]["input_cpt_path"]).exists(): 

144 self.cmd.append('-t') 

145 if self.container_path: 

146 shutil.copy2(self.stage_io_dict["in"]["input_cpt_path"], self.stage_io_dict.get("unique_dir", "")) 

147 self.cmd.append(str(Path(self.container_volume_path).joinpath(Path(self.stage_io_dict["in"]["input_cpt_path"]).name))) 

148 else: 

149 self.cmd.append(self.stage_io_dict["in"]["input_cpt_path"]) 

150 if self.stage_io_dict["in"].get("input_ndx_path") and Path(self.stage_io_dict["in"]["input_ndx_path"]).exists(): 

151 self.cmd.append('-n') 

152 if self.container_path: 

153 shutil.copy2(self.stage_io_dict["in"]["input_ndx_path"], self.stage_io_dict.get("unique_dir", "")) 

154 self.cmd.append(str(Path(self.container_volume_path).joinpath(Path(self.stage_io_dict["in"]["input_ndx_path"]).name))) 

155 else: 

156 self.cmd.append(self.stage_io_dict["in"]["input_ndx_path"]) 

157 

158 if self.gmx_lib: 

159 self.env_vars_dict['GMXLIB'] = self.gmx_lib 

160 

161 # Run Biobb block 

162 self.run_biobb() 

163 

164 # Copy files to host 

165 self.copy_to_host() 

166 

167 # Remove temporal files 

168 self.remove_tmp_files() 

169 

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

171 return self.return_code 

172 

173 

174def grompp(input_gro_path: str, input_top_zip_path: str, output_tpr_path: str, 

175 input_cpt_path: Optional[str] = None, input_ndx_path: Optional[str] = None, input_mdp_path: Optional[str] = None, 

176 properties: Optional[dict] = None, **kwargs) -> int: 

177 """Create :class:`Grompp <gromacs.grompp.Grompp>` class and 

178 execute the :meth:`launch() <gromacs.grompp.Grompp.launch>` method.""" 

179 return Grompp(**dict(locals())).launch() 

180 

181 

182grompp.__doc__ = Grompp.__doc__ 

183main = Grompp.get_main(grompp, "Wrapper for the GROMACS grompp module.") 

184 

185 

186if __name__ == '__main__': 

187 main()