Coverage for biobb_gromacs/gromacs/make_ndx.py: 89%

56 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 MakeNdx 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 MakeNdx(BiobbObject): 

13 """ 

14 | biobb_gromacs MakeNdx 

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

16 | The GROMACS make_ndx module, generates an index file using the atoms of the selection. 

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: gro (edam:format_2033), pdb (edam:format_1476), 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_make_ndx.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 * **gmx_lib** (*str*) - (None) Path set GROMACS GMXLIB environment variable. 

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

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

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

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

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

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

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

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

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

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

35 

36 Examples: 

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

38 

39 from biobb_gromacs.gromacs.make_ndx import make_ndx 

40 prop = { 'selection': 'a CA C N O' } 

41 make_ndx(input_structure_path='/path/to/myStructure.gro', 

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

43 properties=prop) 

44 

45 Info: 

46 * wrapped_software: 

47 * name: GROMACS MakeNdx 

48 * version: 2025.2 

49 * license: LGPL 2.1 

50 * ontology: 

51 * name: EDAM 

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

53 """ 

54 

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

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

57 properties = properties or {} 

58 

59 # Call parent class constructor 

60 super().__init__(properties) 

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

62 

63 # Input/Output files 

64 self.io_dict = { 

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

66 "out": {"output_ndx_path": output_ndx_path} 

67 } 

68 

69 # Properties specific for BB 

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

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

72 

73 # Properties common in all GROMACS BB 

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

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

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

77 if self.gmx_nobackup: 

78 self.binary_path += ' -nobackup' 

79 if self.gmx_nocopyright: 

80 self.binary_path += ' -nocopyright' 

81 if not self.container_path: 

82 self.gmx_version = get_gromacs_version(self.binary_path) 

83 

84 # Check the properties 

85 self.check_properties(properties) 

86 self.check_arguments() 

87 

88 @launchlogger 

89 def launch(self) -> int: 

90 """Execute the :class:`MakeNdx <gromacs.make_ndx.MakeNdx>` object.""" 

91 

92 # Setup Biobb 

93 if self.check_restart(): 

94 return 0 

95 

96 self.selection = str(self.selection) 

97 self.selection = self.selection.replace("\\n", "\n") 

98 self.io_dict['in']['stdin_file_path'] = fu.create_stdin_file(f'{self.selection}\nq\n') 

99 self.stage_files() 

100 

101 if self.container_path: 

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

103 else: 

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

105 

106 # Create command line 

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

108 self.binary_path, 'make_ndx', 

109 '-f', PurePath(self.stage_io_dict["in"]["input_structure_path"]).name, 

110 '-o', PurePath(self.stage_io_dict["out"]["output_ndx_path"]).name] 

111 

112 if self.stage_io_dict["in"].get("input_ndx_path")\ 

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

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

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

116 

117 # Add stdin input file 

118 self.cmd.append('<') 

119 self.cmd.append(PurePath(self.stage_io_dict["in"]["stdin_file_path"]).name) 

120 

121 if self.gmx_lib: 

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

123 

124 # create_cmd_line and execute_command 

125 self.run_biobb() 

126 

127 # Retrieve results 

128 self.copy_to_host() 

129 

130 # Remove temporal files 

131 self.tmp_files.append(self.io_dict['in'].get("stdin_file_path", '')) 

132 self.remove_tmp_files() 

133 

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

135 return self.return_code 

136 

137 

138def make_ndx(input_structure_path: str, output_ndx_path: str, 

139 input_ndx_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> int: 

140 """Create :class:`MakeNdx <gromacs.make_ndx.MakeNdx>` class and 

141 execute the :meth:`launch() <gromacs.make_ndx.MakeNdx.launch>` method.""" 

142 return MakeNdx(**dict(locals())).launch() 

143 

144 

145make_ndx.__doc__ = MakeNdx.__doc__ 

146main = MakeNdx.get_main(make_ndx, "Wrapper for the GROMACS make_ndx module.") 

147 

148 

149if __name__ == '__main__': 

150 main()