Coverage for biobb_gromacs/gromacs/solvate.py: 73%

74 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 Editconf class and the command line interface.""" 

4import shutil 

5import argparse 

6from typing import Optional 

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 

13 

14 

15class Solvate(BiobbObject): 

16 """ 

17 | biobb_gromacs Solvate 

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

19 | The GROMACS solvate module, generates a box of solvent around the selected structure. 

20 

21 Args: 

22 input_solute_gro_path (str): Path to the input GRO file. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/solvate.gro>`_. Accepted formats: gro (edam:format_2033), pdb (edam:format_1476). 

23 output_gro_path (str): Path to the output GRO file. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_solvate.gro>`_. Accepted formats: gro (edam:format_2033), pdb (edam:format_1476). 

24 input_top_zip_path (str): Path the input TOP topology in zip format. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/solvate.zip>`_. Accepted formats: zip (edam:format_3987). 

25 output_top_zip_path (str): Path the output topology in zip format. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_solvate.zip>`_. Accepted formats: zip (edam:format_3987). 

26 input_solvent_gro_path (str) (Optional): (spc216.gro) Path to the GRO file containing the structure of the solvent. File type: input. Accepted formats: gro (edam:format_2033). 

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

28 * **shell** (*float*) - (0.0) [0~100|0.1] Thickness in nanometers of optional water layer around solute. 

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

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

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

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

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

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

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

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

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

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

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

40 

41 Examples: 

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

43 

44 from biobb_gromacs.gromacs.solvate import Solvate 

45 prop = { 'shell': 1.0 } 

46 solvate(input_solute_gro_path='/path/to/myStructure.gro', 

47 output_gro_path='/path/to/newStructure.gro', 

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

49 output_top_zip_path='/path/to/newTopology.zip', 

50 properties=prop) 

51 

52 

53 Info: 

54 * wrapped_software: 

55 * name: GROMACS Solvate 

56 * version: 2024.5 

57 * license: LGPL 2.1 

58 * ontology: 

59 * name: EDAM 

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

61 """ 

62 

63 def __init__(self, input_solute_gro_path: str, output_gro_path: str, input_top_zip_path: str, 

64 output_top_zip_path: str, input_solvent_gro_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> None: 

65 properties = properties or {} 

66 

67 # Call parent class constructor 

68 super().__init__(properties) 

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

70 

71 # Input/Output files 

72 self.io_dict = { 

73 "in": {"input_solute_gro_path": input_solute_gro_path, "input_solvent_gro_path": input_solvent_gro_path}, 

74 "out": {"output_gro_path": output_gro_path, "output_top_zip_path": output_top_zip_path} 

75 } 

76 

77 # Should not be copied inside container 

78 self.input_top_zip_path = input_top_zip_path 

79 

80 # Properties specific for BB 

81 self.shell = properties.get('shell') 

82 if not self.io_dict["in"].get('input_solvent_gro_path'): 

83 self.io_dict["in"]['input_solvent_gro_path'] = 'spc216.gro' 

84 

85 # Properties common in all GROMACS BB 

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

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

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

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

90 if self.gmx_nobackup: 

91 self.binary_path += ' -nobackup' 

92 if self.gmx_nocopyright: 

93 self.binary_path += ' -nocopyright' 

94 if not self.container_path: 

95 self.gmx_version = get_gromacs_version(self.binary_path) 

96 

97 # Check the properties 

98 self.check_properties(properties) 

99 self.check_arguments() 

100 

101 @launchlogger 

102 def launch(self) -> int: 

103 """Execute the :class:`Solvate <gromacs.solvate.Solvate>` object.""" 

104 

105 # Setup Biobb 

106 if self.check_restart(): 

107 return 0 

108 self.stage_files() 

109 

110 # Unzip topology to topology_out 

111 top_file = fu.unzip_top(zip_file=self.input_top_zip_path, out_log=self.out_log) 

112 top_dir = str(Path(top_file).parent) 

113 

114 if self.container_path: 

115 shutil.copytree(top_dir, str(Path(self.stage_io_dict.get("unique_dir", "")).joinpath(Path(top_dir).name))) 

116 top_file = str(Path(self.container_volume_path).joinpath(Path(top_dir).name, Path(top_file).name)) 

117 

118 self.cmd = [self.binary_path, 'solvate', 

119 '-cp', self.stage_io_dict["in"]["input_solute_gro_path"], 

120 '-cs', self.stage_io_dict["in"]["input_solvent_gro_path"], 

121 '-o', self.stage_io_dict["out"]["output_gro_path"], 

122 '-p', top_file] 

123 

124 if self.shell: 

125 self.cmd.append("-shell") 

126 self.cmd.append(str(self.shell)) 

127 

128 if self.gmx_lib: 

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

130 

131 # Run Biobb block 

132 self.run_biobb() 

133 

134 # Copy files to host 

135 self.copy_to_host() 

136 

137 if self.container_path: 

138 top_file = str(Path(self.stage_io_dict.get("unique_dir", "")).joinpath(Path(top_dir).name, Path(top_file).name)) 

139 

140 # zip topology 

141 fu.log('Compressing topology to: %s' % self.stage_io_dict["out"]["output_top_zip_path"], self.out_log, 

142 self.global_log) 

143 fu.zip_top(zip_file=self.io_dict["out"]["output_top_zip_path"], top_file=top_file, out_log=self.out_log, remove_original_files=self.remove_tmp) 

144 

145 # Remove temporal files 

146 # self.tmp_files.extend([self.stage_io_dict.get("unique_dir", ""), top_dir]) 

147 self.remove_tmp_files() 

148 

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

150 return self.return_code 

151 

152 

153def solvate(input_solute_gro_path: str, output_gro_path: str, input_top_zip_path: str, 

154 output_top_zip_path: str, input_solvent_gro_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> int: 

155 """Create :class:`Solvate <gromacs.solvate.Solvate>` class and 

156 execute the :meth:`launch() <gromacs.solvate.Solvate.launch>` method.""" 

157 

158 return Solvate(input_solute_gro_path=input_solute_gro_path, output_gro_path=output_gro_path, 

159 input_top_zip_path=input_top_zip_path, output_top_zip_path=output_top_zip_path, 

160 input_solvent_gro_path=input_solvent_gro_path, properties=properties, **kwargs).launch() 

161 

162 

163solvate.__doc__ = Solvate.__doc__ 

164 

165 

166def main(): 

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

168 parser = argparse.ArgumentParser(description="Wrapper for the GROMACS solvate module.", 

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

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

171 

172 # Specific args of each building block 

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

174 required_args.add_argument('--input_solute_gro_path', required=True) 

175 required_args.add_argument('--output_gro_path', required=True) 

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

177 required_args.add_argument('--output_top_zip_path', required=True) 

178 parser.add_argument('--input_solvent_gro_path', required=False) 

179 

180 args = parser.parse_args() 

181 config = args.config if args.config else None 

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

183 

184 # Specific call of each building block 

185 solvate(input_solute_gro_path=args.input_solute_gro_path, output_gro_path=args.output_gro_path, 

186 input_top_zip_path=args.input_top_zip_path, output_top_zip_path=args.output_top_zip_path, 

187 input_solvent_gro_path=args.input_solvent_gro_path, properties=properties) 

188 

189 

190if __name__ == '__main__': 

191 main()