Coverage for biobb_analysis/gromacs/gmx_rgyr.py: 94%

49 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-08 10:47 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the GMX Rgyr class and the command line interface.""" 

4 

5from typing import Optional 

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_analysis.gromacs.common import get_binary_path, check_input_path, check_traj_path, check_index_path, get_selection_index_file, check_out_xvg_path, get_xvg, get_selection 

10 

11 

12class GMXRgyr(BiobbObject): 

13 """ 

14 | biobb_analysis GMXRgyr 

15 | Wrapper of the GROMACS gyrate module for computing the radius of gyration (Rgyr) of a molecule about the x-, y- and z-axes, as a function of time, from a given GROMACS compatible trajectory. 

16 | `GROMACS gyrate <http://manual.gromacs.org/documentation/2018/onlinehelp/gmx-gyrate.html>`_ computes the radius of gyration of a molecule and the radii of gyration about the x-, y- and z-axes, as a function of time. The atoms are explicitly mass weighted. 

17 

18 Args: 

19 input_structure_path (str): Path to the input structure file. File type: input. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/data/gromacs/topology.tpr>`_. Accepted formats: tpr (edam:format_2333), gro (edam:format_2033), g96 (edam:format_2033), pdb (edam:format_1476), brk (edam:format_2033), ent (edam:format_1476). 

20 input_traj_path (str): Path to the GROMACS trajectory file. File type: input. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/data/gromacs/trajectory.trr>`_. Accepted formats: xtc (edam:format_3875), trr (edam:format_3910), cpt (edam:format_2333), gro (edam:format_2033), g96 (edam:format_2033), pdb (edam:format_1476), tng (edam:format_3876). 

21 input_index_path (str) (Optional): Path to the GROMACS index file. File type: input. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/data/gromacs/index.ndx>`_. Accepted formats: ndx (edam:format_2033). 

22 output_xvg_path (str): Path to the XVG output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/reference/gromacs/ref_rgyr.xvg>`_. Accepted formats: xvg (edam:format_2030). 

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

24 * **xvg** (*str*) - ("none") XVG plot formatting. Values: xmgrace, xmgr, none. 

25 * **selection** (*str*) - ("System") Group where the rgyr will be performed. If **input_index_path** provided, check the file for the accepted values. Values: System (all atoms in the system), Protein (all protein atoms), Protein-H (protein atoms excluding hydrogens), C-alpha (C-alpha atoms), Backbone (protein backbone atoms: N; C-alpha and C), MainChain (protein main chain atoms: N; C-alpha; C and O; including oxygens in C-terminus), MainChain+Cb (protein main chain atoms including C-beta), MainChain+H (protein main chain atoms including backbone amide hydrogens and hydrogens on the N-terminus), SideChain (protein side chain atoms: that is all atoms except N; C-alpha; C; O; backbone amide hydrogens and oxygens in C-terminus and hydrogens on the N-terminus), SideChain-H (protein side chain atoms excluding all hydrogens), Prot-Masses (protein atoms excluding dummy masses), non-Protein (all non-protein atoms), Water (water molecules), SOL (water molecules), non-Water (anything not covered by the Water group), Ion (any name matching an Ion entry in residuetypes.dat), NA (all NA atoms), CL (all CL atoms), Water_and_ions (combination of the Water and Ions groups), DNA (all DNA atoms), RNA (all RNA atoms), Protein_DNA (all Protein-DNA complex atoms), Protein_RNA (all Protein-RNA complex atoms), Protein_DNA_RNA (all Protein-DNA-RNA complex atoms), DNA_RNA (all DNA-RNA complex atoms). 

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

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) Container path definition. 

31 * **container_image** (*str*) - ('gromacs/gromacs:2022.2') Container image definition. 

32 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

33 * **container_working_dir** (*str*) - (None) Container working directory definition. 

34 * **container_user_id** (*str*) - (None) Container user_id definition. 

35 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

36 

37 Examples: 

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

39 

40 from biobb_analysis.gromacs.gmx_rgyr import gmx_rgyr 

41 prop = { 

42 'xvg': 'xmgr', 

43 'selection': 'Water_and_ions' 

44 } 

45 gmx_rgyr(input_structure_path='/path/to/myStructure.tpr', 

46 input_traj_path='/path/to/myTrajectory.trr', 

47 output_xvg_path='/path/to/newXVG.xvg', 

48 input_index_path='/path/to/myIndex.ndx', 

49 properties=prop) 

50 

51 Info: 

52 * wrapped_software: 

53 * name: GROMACS gyrate 

54 * version: >=2024.5 

55 * license: LGPL 2.1 

56 * ontology: 

57 * name: EDAM 

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

59 

60 """ 

61 

62 def __init__(self, input_structure_path, input_traj_path, output_xvg_path, 

63 input_index_path=None, properties=None, **kwargs) -> None: 

64 properties = properties or {} 

65 

66 # Call parent class constructor 

67 super().__init__(properties) 

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

69 

70 # Input/Output files 

71 self.io_dict = { 

72 "in": {"input_structure_path": input_structure_path, "input_traj_path": input_traj_path, "input_index_path": input_index_path}, 

73 "out": {"output_xvg_path": output_xvg_path} 

74 } 

75 

76 # Properties specific for BB 

77 self.xvg = properties.get('xvg', "none") 

78 self.selection = properties.get('selection', "System") 

79 self.properties = properties 

80 

81 # Properties common in all GROMACS BB 

82 self.binary_path = get_binary_path(properties, 'binary_path') 

83 

84 # Check the properties 

85 self.check_init(properties) 

86 

87 def check_data_params(self, out_log, err_log): 

88 """ Checks all the input/output paths and parameters """ 

89 self.io_dict["in"]["input_structure_path"] = check_input_path(self.io_dict["in"]["input_structure_path"], out_log, self.__class__.__name__) 

90 self.io_dict["in"]["input_traj_path"] = check_traj_path(self.io_dict["in"]["input_traj_path"], out_log, self.__class__.__name__) 

91 self.io_dict["in"]["input_index_path"] = check_index_path(self.io_dict["in"]["input_index_path"], out_log, self.__class__.__name__) 

92 self.io_dict["out"]["output_xvg_path"] = check_out_xvg_path(self.io_dict["out"]["output_xvg_path"], out_log, self.__class__.__name__) 

93 self.xvg = get_xvg(self.properties, out_log, self.__class__.__name__) 

94 if not self.io_dict["in"]["input_index_path"]: 

95 self.selection = get_selection(self.properties, out_log, self.__class__.__name__) 

96 else: 

97 self.selection = get_selection_index_file(self.properties, self.io_dict["in"]["input_index_path"], 'selection', out_log, self.__class__.__name__) 

98 

99 @launchlogger 

100 def launch(self) -> int: 

101 """Execute the :class:`GMXRgyr <gromacs.gmx_rgyr.GMXRgyr>` object.""" 

102 

103 # check input/output paths and parameters 

104 self.check_data_params(self.out_log, self.err_log) 

105 

106 # Setup Biobb 

107 if self.check_restart(): 

108 return 0 

109 

110 # standard input 

111 self.io_dict['in']['stdin_file_path'] = fu.create_stdin_file(f'{self.selection}') 

112 self.stage_files() 

113 

114 self.cmd = [self.binary_path, 'gyrate', 

115 '-s', self.stage_io_dict["in"]["input_structure_path"], 

116 '-f', self.stage_io_dict["in"]["input_traj_path"], 

117 '-o', self.stage_io_dict["out"]["output_xvg_path"], 

118 '-xvg', self.xvg] 

119 

120 if self.stage_io_dict["in"].get("input_index_path"): 

121 self.cmd.extend(['-n', self.stage_io_dict["in"]["input_index_path"]]) 

122 

123 # Add stdin input file 

124 self.cmd.append('<') 

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

126 

127 # Run Biobb block 

128 self.run_biobb() 

129 

130 # Copy files to host 

131 self.copy_to_host() 

132 

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

134 self.remove_tmp_files() 

135 

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

137 

138 return self.return_code 

139 

140 

141def gmx_rgyr(input_structure_path: str, input_traj_path: str, output_xvg_path: str, input_index_path: Optional[str] = None, properties: Optional[dict] = None, **kwargs) -> int: 

142 """Execute the :class:`GMXRgyr <gromacs.gmx_rgyr.GMXRgyr>` class and 

143 execute the :meth:`launch() <gromacs.gmx_rgyr.GMXRgyr.launch>` method.""" 

144 return GMXRgyr(**dict(locals())).launch() 

145 

146 

147gmx_rgyr.__doc__ = GMXRgyr.__doc__ 

148main = GMXRgyr.get_main(gmx_rgyr, "Computes the radius of gyration (Rgyr) of a molecule about the x-, y- and z-axes, as a function of time, from a given GROMACS compatible trajectory.") 

149 

150if __name__ == '__main__': 

151 main()