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

64 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-28 06:50 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5from pathlib import Path, PurePath 

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.common import get_gromacs_version 

10from biobb_gromacs.gromacs.common import create_mdp 

11from biobb_gromacs.gromacs.common import mdp_preset 

12 

13 

14class Grompp(BiobbObject): 

15 """ 

16 | biobb_gromacs Grompp 

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

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

19 

20 Args: 

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

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

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

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

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

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

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

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

29 * **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). 

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

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

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

33 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

34 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

35 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

36 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

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

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

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

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

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

42 

43 Examples: 

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

45 

46 from biobb_gromacs.gromacs.grompp import grompp 

47 

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

49 'mdp': 

50 { 'emtol':'500', 

51 'nsteps':'5000'}} 

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

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

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

55 properties=prop) 

56 

57 Info: 

58 * wrapped_software: 

59 * name: GROMACS Grompp 

60 * version: 2025.2 

61 * license: LGPL 2.1 

62 * ontology: 

63 * name: EDAM 

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

65 """ 

66 

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

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

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

70 properties = properties or {} 

71 

72 # Call parent class constructor 

73 super().__init__(properties) 

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

75 

76 # Input/Output files 

77 self.io_dict = { 

78 "in": {"input_gro_path": input_gro_path, "input_cpt_path": input_cpt_path, 

79 "input_ndx_path": input_ndx_path, "input_mdp_path": input_mdp_path}, 

80 "out": {"output_tpr_path": output_tpr_path} 

81 } 

82 # Should not be copied inside container 

83 self.input_top_zip_path = input_top_zip_path 

84 

85 # Properties specific for BB 

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

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

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

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

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

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

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

93 

94 # Properties common in all GROMACS BB 

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

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

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

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

99 if self.gmx_nobackup: 

100 self.binary_path += ' -nobackup' 

101 if self.gmx_nocopyright: 

102 self.binary_path += ' -nocopyright' 

103 if not self.container_path: 

104 self.gmx_version = get_gromacs_version(self.binary_path) 

105 

106 # Check the properties 

107 self.check_properties(properties) 

108 self.check_arguments() 

109 

110 @launchlogger 

111 def launch(self) -> int: 

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

113 

114 # Setup Biobb 

115 if self.check_restart(): 

116 return 0 

117 self.stage_files() 

118 

119 # Unzip topology to topology_out 

120 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", "")) 

121 

122 # Create MDP file 

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

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

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

126 mdp_properties_dict=self.mdp) 

127 

128 if self.container_path: 

129 working_dir = self.container_volume_path if self.container_volume_path else "/data" 

130 else: 

131 working_dir = self.stage_io_dict.get('unique_dir', '') 

132 

133 self.cmd = ["cd", working_dir, ";", 

134 self.binary_path, 'grompp', 

135 '-f', PurePath(self.output_mdp_path).name, 

136 '-c', PurePath(self.stage_io_dict["in"]["input_gro_path"]).name, 

137 '-r', PurePath(self.stage_io_dict["in"]["input_gro_path"]).name, 

138 '-p', PurePath(top_file).name, 

139 '-o', PurePath(self.stage_io_dict["out"]["output_tpr_path"]).name, 

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

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 self.cmd.append(PurePath(self.stage_io_dict["in"]["input_cpt_path"]).name) 

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

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

148 self.cmd.append(PurePath(self.stage_io_dict["in"]["input_ndx_path"]).name) 

149 

150 if self.gmx_lib: 

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

152 

153 # Run Biobb block 

154 self.run_biobb() 

155 

156 # Copy files to host 

157 self.copy_to_host() 

158 

159 # Remove temporal files 

160 self.remove_tmp_files() 

161 

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

163 return self.return_code 

164 

165 

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

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

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

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

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

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

172 

173 

174grompp.__doc__ = Grompp.__doc__ 

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

176 

177 

178if __name__ == '__main__': 

179 main()