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

87 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-25 09:23 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6import shutil 

7from pathlib import Path 

8from biobb_common.generic.biobb_object import BiobbObject 

9from biobb_common.configuration import settings 

10from biobb_common.tools import file_utils as fu 

11from biobb_common.tools.file_utils import launchlogger 

12from biobb_gromacs.gromacs.common import get_gromacs_version 

13from biobb_gromacs.gromacs.common import create_mdp 

14from biobb_gromacs.gromacs.common import mdp_preset 

15 

16 

17class Grompp(BiobbObject): 

18 """ 

19 | biobb_gromacs Grompp 

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

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

22 

23 Args: 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

45 

46 Examples: 

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

48 

49 from biobb_gromacs.gromacs.grompp import grompp 

50 

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

52 'mdp': 

53 { 'emtol':'500', 

54 'nsteps':'5000'}} 

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

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

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

58 properties=prop) 

59 

60 Info: 

61 * wrapped_software: 

62 * name: GROMACS Grompp 

63 * version: 2024.5 

64 * license: LGPL 2.1 

65 * ontology: 

66 * name: EDAM 

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

68 """ 

69 

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

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

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

73 properties = properties or {} 

74 

75 # Call parent class constructor 

76 super().__init__(properties) 

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

78 

79 # Input/Output files 

80 self.io_dict = { 

81 "in": {"input_gro_path": input_gro_path, "input_cpt_path": input_cpt_path, 

82 "input_ndx_path": input_ndx_path, "input_mdp_path": input_mdp_path}, 

83 "out": {"output_tpr_path": output_tpr_path} 

84 } 

85 # Should not be copied inside container 

86 self.input_top_zip_path = input_top_zip_path 

87 

88 # Properties specific for BB 

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

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

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

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

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

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

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

96 

97 # Properties common in all GROMACS BB 

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

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

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

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

102 if self.gmx_nobackup: 

103 self.binary_path += ' -nobackup' 

104 if self.gmx_nocopyright: 

105 self.binary_path += ' -nocopyright' 

106 if not self.container_path: 

107 self.gmx_version = get_gromacs_version(self.binary_path) 

108 

109 # Check the properties 

110 self.check_properties(properties) 

111 self.check_arguments() 

112 

113 @launchlogger 

114 def launch(self) -> int: 

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

116 

117 # Setup Biobb 

118 if self.check_restart(): 

119 return 0 

120 self.stage_files() 

121 

122 # Unzip topology to topology_out 

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

124 

125 # Create MDP file 

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

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

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

129 mdp_properties_dict=self.mdp) 

130 

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

132 if self.container_path: 

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

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

135 

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

137 '-f', self.output_mdp_path, 

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

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

140 '-p', top_file, 

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

142 '-po', 'mdout.mdp', 

143 '-maxwarn', self.maxwarn] 

144 

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

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

147 if self.container_path: 

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

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

150 else: 

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

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

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

154 if self.container_path: 

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

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

157 else: 

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

159 

160 if self.gmx_lib: 

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

162 

163 # Run Biobb block 

164 self.run_biobb() 

165 

166 # Copy files to host 

167 self.copy_to_host() 

168 

169 # Remove temporal files 

170 self.tmp_files.extend([ 

171 # self.stage_io_dict.get("unique_dir", ''), 

172 'mdout.mdp' 

173 ]) 

174 self.remove_tmp_files() 

175 

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

177 return self.return_code 

178 

179 

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

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

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

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

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

185 

186 return Grompp(input_gro_path=input_gro_path, input_top_zip_path=input_top_zip_path, 

187 output_tpr_path=output_tpr_path, input_cpt_path=input_cpt_path, 

188 input_ndx_path=input_ndx_path, input_mdp_path=input_mdp_path, 

189 properties=properties, **kwargs).launch() 

190 

191 

192grompp.__doc__ = Grompp.__doc__ 

193 

194 

195def main(): 

196 """Command line execution of this building block. Please check the command line documentation.""" 

197 parser = argparse.ArgumentParser(description="Wrapper for the GROMACS grompp module.", 

198 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

199 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string") 

200 

201 # Specific args of each building block 

202 required_args = parser.add_argument_group('required arguments') 

203 required_args.add_argument('--input_gro_path', required=True) 

204 required_args.add_argument('--input_top_zip_path', required=True) 

205 required_args.add_argument('--output_tpr_path', required=True) 

206 parser.add_argument('--input_cpt_path', required=False) 

207 parser.add_argument('--input_ndx_path', required=False) 

208 parser.add_argument('--input_mdp_path', required=False) 

209 

210 args = parser.parse_args() 

211 config = args.config if args.config else None 

212 properties = settings.ConfReader(config=config).get_prop_dic() 

213 

214 # Specific call of each building block 

215 grompp(input_gro_path=args.input_gro_path, input_top_zip_path=args.input_top_zip_path, 

216 output_tpr_path=args.output_tpr_path, input_cpt_path=args.input_cpt_path, 

217 input_ndx_path=args.input_ndx_path, input_mdp_path=args.input_mdp_path, 

218 properties=properties) 

219 

220 

221if __name__ == '__main__': 

222 main()