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

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

4 

5from typing import Optional 

6 

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 

10 

11from biobb_gromacs.gromacs.common import get_gromacs_version 

12 

13 

14class Editconf(BiobbObject): 

15 """ 

16 | biobb_gromacs Editconf 

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

18 | The GROMACS editconf converts generic structure format to .gro, .g96 or .pdb. 

19 

20 Args: 

21 input_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/editconf.gro>`_. Accepted formats: gro (edam:format_2033), pdb (edam:format_1476). 

22 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_editconf.gro>`_. Accepted formats: pdb (edam:format_1476), gro (edam:format_2033). 

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

24 * **distance_to_molecule** (*float*) - (1.0) [0~100|0.1] Distance of the box from the outermost atom in nm. ie 1.0nm = 10 Angstroms. 

25 * **box_vector_lenghts** (*str*) - (None) Array of floats defining the box vector lenghts ie "0.5 0.5 0.5". If this option is used the distance_to_molecule property will be ignored. 

26 * **box_type** (*str*) - ("cubic") Geometrical shape of the solvent box. Values: cubic (rectangular box with all sides equal), triclinic (triclinic box), dodecahedron (rhombic dodecahedron), octahedron (truncated octahedron). 

27 * **center_molecule** (*bool*) - (True) Center molecule in the box. 

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

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

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

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

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

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

34 * **container_image** (*str*) - (None) Container Image identifier. 

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

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

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

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

39 

40 Examples: 

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

42 

43 from biobb_gromacs.gromacs.editconf import editconf 

44 prop = { 'distance_to_molecule': 1.0, 

45 'box_type': 'cubic'} 

46 editconf(input_gro_path='/path/to/structure.gro', 

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

48 properties=prop) 

49 

50 Info: 

51 * wrapped_software: 

52 * name: GROMACS Solvate 

53 * version: 2025.2 

54 * license: LGPL 2.1 

55 * ontology: 

56 * name: EDAM 

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

58 """ 

59 

60 def __init__( 

61 self, 

62 input_gro_path: str, 

63 output_gro_path: str, 

64 properties: Optional[dict] = None, 

65 **kwargs, 

66 ) -> None: 

67 properties = properties or {} 

68 

69 # Call parent class constructor 

70 super().__init__(properties) 

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

72 

73 # Input/Output files 

74 self.io_dict = { 

75 "in": {"input_gro_path": input_gro_path}, 

76 "out": {"output_gro_path": output_gro_path}, 

77 } 

78 

79 # Properties specific for BB 

80 self.distance_to_molecule = properties.get("distance_to_molecule", 1.0) 

81 self.box_vector_lenghts = properties.get("box_vector_lenghts") 

82 self.box_type = properties.get("box_type", "cubic") 

83 self.center_molecule = properties.get("center_molecule", True) 

84 

85 # Properties common in all GROMACS BB 

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

87 self.binary_path: str = 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:`Editconf <gromacs.editconf.Editconf>` object.""" 

104 

105 # Setup Biobb 

106 if self.check_restart(): 

107 return 0 

108 self.stage_files() 

109 

110 # Create command line 

111 self.cmd = [ 

112 self.binary_path, 

113 "editconf", 

114 "-f", 

115 self.stage_io_dict["in"]["input_gro_path"], 

116 "-o", 

117 self.stage_io_dict["out"]["output_gro_path"], 

118 "-bt", 

119 self.box_type, 

120 ] 

121 

122 if self.box_vector_lenghts: 

123 if not isinstance(self.box_vector_lenghts, str): 

124 self.box_vector_lenghts = " ".join(map(str, self.box_vector_lenghts)) 

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

126 self.cmd.append(self.box_vector_lenghts) 

127 else: 

128 self.cmd.append("-d") 

129 self.cmd.append(str(self.distance_to_molecule)) 

130 fu.log( 

131 "Distance of the box to molecule: %6.2f" % self.distance_to_molecule, 

132 self.out_log, 

133 self.global_log, 

134 ) 

135 

136 if self.center_molecule: 

137 self.cmd.append("-c") 

138 fu.log("Centering molecule in the box.", self.out_log, self.global_log) 

139 

140 fu.log("Box type: %s" % self.box_type, self.out_log, self.global_log) 

141 

142 if self.gmx_lib: 

143 self.env_vars_dict = self.gmx_lib 

144 

145 # Run Biobb block 

146 self.run_biobb() 

147 

148 # Copy files to host 

149 self.copy_to_host() 

150 

151 # Remove temporal files 

152 self.remove_tmp_files() 

153 

154 # self.check_arguments(output_files_created=True, raise_exception=False) 

155 return self.return_code 

156 

157 

158def editconf( 

159 input_gro_path: str, 

160 output_gro_path: str, 

161 properties: Optional[dict] = None, 

162 **kwargs, 

163) -> int: 

164 """Create :class:`Editconf <gromacs.editconf.Editconf>` class and 

165 execute the :meth:`launch() <gromacs.editconf.Editconf.launch>` method.""" 

166 return Editconf(**dict(locals())).launch() 

167 

168 

169editconf.__doc__ = Editconf.__doc__ 

170main = Editconf.get_main(editconf, "Wrapper of the GROMACS gmx editconf module.") 

171 

172 

173if __name__ == "__main__": 

174 main()