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

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

4from typing import Optional 

5from pathlib import Path 

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 # Create command line 

102 self.cmd = [self.binary_path, 'make_ndx', 

103 '-f', self.stage_io_dict["in"]["input_structure_path"], 

104 '-o', self.stage_io_dict["out"]["output_ndx_path"]] 

105 

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

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

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

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

110 

111 # Add stdin input file 

112 self.cmd.append('<') 

113 self.cmd.append(self.stage_io_dict["in"]["stdin_file_path"]) 

114 

115 if self.gmx_lib: 

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

117 

118 # create_cmd_line and execute_command 

119 self.run_biobb() 

120 

121 # Retrieve results 

122 self.copy_to_host() 

123 

124 # Remove temporal files 

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

126 self.remove_tmp_files() 

127 

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

129 return self.return_code 

130 

131 

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

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

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

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

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

137 

138 

139make_ndx.__doc__ = MakeNdx.__doc__ 

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

141 

142 

143if __name__ == '__main__': 

144 main()