Coverage for biobb_gromacs/gromacs/gmxselect.py: 80%

65 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 Select 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 

10 

11 

12class Gmxselect(BiobbObject): 

13 """ 

14 | biobb_gromacs Gmxselect 

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

16 | The GROMACS select module writes out basic data about dynamic selections. It can be used for some simple analyses, or the output can be combined with output from other programs and/or external analysis programs to calculate more complex things. 

17 

18 Args: 

19 input_structure_path (str): Path to the input GRO/PDB/TPR file. File type: input. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/data/gromacs/make_ndx.tpr>`_. Accepted formats: pdb (edam:format_1476), gro (edam:format_2033), tpr (edam:format_2333). 

20 output_ndx_path (str): Path to the output index NDX file. File type: output. `Sample file <https://github.com/bioexcel/biobb_gromacs/raw/master/biobb_gromacs/test/reference/gromacs/ref_select.ndx>`_. Accepted formats: ndx (edam:format_2033). 

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

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

23 * **selection** (*str*) - ("a CA C N O") Heavy atoms. Atom selection string. 

24 * **append** (*bool*) - (False) Append the content of the input_ndx_path to the output_ndx_path. 

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

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

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

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

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

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

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

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

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

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

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

36 

37 Examples: 

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

39 

40 from biobb_gromacs.gromacs.gmxselect import gmxselect 

41 prop = { 'selection': '"Mynewgroup" group "Protein-H" and not same residue as within 0.4 of resname ARG' } 

42 gmxselect(input_structure_path='/path/to/myStructure.gro', 

43 output_ndx_path='/path/to/newIndex.ndx', 

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: GROMACS Gmxselect 

49 * version: 2025.2 

50 * license: LGPL 2.1 

51 * ontology: 

52 * name: EDAM 

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

54 """ 

55 

56 def __init__(self, input_structure_path: str, output_ndx_path: str, input_ndx_path: Optional[str] = None, 

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

58 properties = properties or {} 

59 

60 # Call parent class constructor 

61 super().__init__(properties) 

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

63 

64 # Input/Output files 

65 self.io_dict = { 

66 "in": {"input_structure_path": input_structure_path, "input_ndx_path": input_ndx_path}, 

67 "out": {"output_ndx_path": output_ndx_path} 

68 } 

69 

70 # Properties specific for BB 

71 self.selection = properties.get('selection', "a CA C N O") 

72 self.append = properties.get('append', False) 

73 

74 # Properties common in all GROMACS BB 

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

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

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

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

79 if self.gmx_nobackup: 

80 self.binary_path += ' -nobackup' 

81 if self.gmx_nocopyright: 

82 self.binary_path += ' -nocopyright' 

83 if not self.container_path: 

84 self.gmx_version = get_gromacs_version(self.binary_path) 

85 

86 # Check the properties 

87 self.check_properties(properties) 

88 self.check_arguments() 

89 

90 @launchlogger 

91 def launch(self) -> int: 

92 """Execute the :class:`Gmxselect <gromacs.gmxselect.Gmxselect>` object.""" 

93 

94 # Setup Biobb 

95 if self.check_restart(): 

96 return 0 

97 self.stage_files() 

98 

99 if self.container_path: 

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

101 else: 

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

103 

104 selection_file_path = Path(str(self.stage_io_dict.get("unique_dir", ""))).joinpath("gmxselect_selection.dat") 

105 with open(selection_file_path, "w") as selection_file: 

106 selection_file.write(f"{self.selection}\n") 

107 

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

109 self.binary_path, 'select', 

110 '-s', PurePath(self.stage_io_dict["in"]["input_structure_path"]).name, 

111 '-on', PurePath(self.stage_io_dict["out"]["output_ndx_path"]).name 

112 ] 

113 

114 if self.stage_io_dict["in"].get("input_ndx_path") and Path( 

115 self.stage_io_dict["in"].get("input_ndx_path")).exists(): 

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

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

118 

119 self.cmd.append('-sf') 

120 self.cmd.append(PurePath(selection_file_path).name) 

121 

122 if self.gmx_lib: 

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

124 

125 # Run Biobb block 

126 self.run_biobb() 

127 

128 # Copy files to host 

129 self.copy_to_host() 

130 

131 if self.io_dict["in"].get("input_ndx_path"): 

132 if self.append: 

133 fu.log(f"Appending {self.io_dict['in'].get('input_ndx_path')} to {self.io_dict['out']['output_ndx_path']}", self.out_log, self.global_log) 

134 with open(self.io_dict["out"]["output_ndx_path"], 'a') as out_ndx_file: 

135 out_ndx_file.write('\n') 

136 with open(self.io_dict["in"].get("input_ndx_path", '')) as in_ndx_file: 

137 for line in in_ndx_file: 

138 out_ndx_file.write(line) 

139 

140 # Remove temporal files 

141 self.tmp_files.append(str(selection_file_path)) 

142 self.remove_tmp_files() 

143 

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

145 return self.return_code 

146 

147 

148def gmxselect(input_structure_path: str, output_ndx_path: str, 

149 input_ndx_path: Optional[str] = None, properties: Optional[dict] = None, 

150 **kwargs) -> int: 

151 """Create :class:`Gmxselect <gromacs.gmxselect.Gmxselect>` class and 

152 execute the :meth:`launch() <gromacs.gmxselect.Gmxselect.launch>` method.""" 

153 return Gmxselect(**dict(locals())).launch() 

154 

155 

156gmxselect.__doc__ = Gmxselect.__doc__ 

157main = Gmxselect.get_main(gmxselect, "Wrapper for the GROMACS select module.") 

158 

159 

160if __name__ == '__main__': 

161 main()