Coverage for biobb_analysis/gromacs/gmx_rms.py: 77%

62 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-14 14:38 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from 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 

11 

12 

13class GMXRms(BiobbObject): 

14 """ 

15 | biobb_analysis GMXRms 

16 | Wrapper of the GROMACS rms module for performing a Root Mean Square deviation (RMSd) analysis from a given GROMACS compatible trajectory. 

17 | `GROMACS rms <http://manual.gromacs.org/current/onlinehelp/gmx-rms.html>`_ compares two structures by computing the root mean square deviation (RMSD), the size-independent rho similarity parameter (rho) or the scaled rho (rhosc). 

18 

19 Args: 

20 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). 

21 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). 

22 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). 

23 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_rms.xvg>`_. Accepted formats: xvg (edam:format_2030). 

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

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

26 * **selection** (*str*) - ("System") Group where the rms 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). 

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

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

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

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

31 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

37 

38 Examples: 

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

40 

41 from biobb_analysis.gromacs.gmx_rms import gmx_rms 

42 prop = { 

43 'xvg': 'xmgr', 

44 'selection': 'Water_and_ions' 

45 } 

46 gmx_rms(input_structure_path='/path/to/myStructure.tpr', 

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

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

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

50 properties=prop) 

51 

52 Info: 

53 * wrapped_software: 

54 * name: GROMACS rms 

55 * version: >=2019.1 

56 * license: LGPL 2.1 

57 * ontology: 

58 * name: EDAM 

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

60 

61 """ 

62 

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

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

65 properties = properties or {} 

66 

67 # Call parent class constructor 

68 super().__init__(properties) 

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

70 

71 # Input/Output files 

72 self.io_dict = { 

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

74 "out": {"output_xvg_path": output_xvg_path} 

75 } 

76 

77 # Properties specific for BB 

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

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

80 self.properties = properties 

81 

82 # Properties common in all GROMACS BB 

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

84 

85 # Check the properties 

86 self.check_properties(properties) 

87 self.check_arguments() 

88 

89 def check_data_params(self, out_log, err_log): 

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

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

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

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

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

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

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

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

98 else: 

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

100 

101 @launchlogger 

102 def launch(self) -> int: 

103 """Execute the :class:`GMXRms <gromacs.gmx_rms.GMXRms>` gromacs.gmx_rms.GMXRms object.""" 

104 

105 # check input/output paths and parameters 

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

107 

108 # Setup Biobb 

109 if self.check_restart(): 

110 return 0 

111 

112 # standard input 

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

114 self.stage_files() 

115 

116 self.cmd = [self.binary_path, 'rms', 

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

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

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

120 '-xvg', self.xvg] 

121 

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

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

124 

125 # Add stdin input file 

126 self.cmd.append('<') 

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

128 

129 # Run Biobb block 

130 self.run_biobb() 

131 

132 # Copy files to host 

133 self.copy_to_host() 

134 

135 self.tmp_files.extend([ 

136 self.stage_io_dict.get("unique_dir", ""), 

137 self.io_dict['in'].get("stdin_file_path", "") 

138 ]) 

139 self.remove_tmp_files() 

140 

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

142 

143 return self.return_code 

144 

145 

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

147 """Execute the :class:`GMXRms <gromacs.gmx_rms.GMXRms>` class and 

148 execute the :meth:`launch() <gromacs.gmx_rms.GMXRms.launch>` method.""" 

149 

150 return GMXRms(input_structure_path=input_structure_path, 

151 input_traj_path=input_traj_path, 

152 output_xvg_path=output_xvg_path, 

153 input_index_path=input_index_path, 

154 properties=properties, **kwargs).launch() 

155 

156 

157def main(): 

158 """Command line execution of this building block. Please check the command line documentation.""" 

159 parser = argparse.ArgumentParser(description="Performs a Root Mean Square deviation (RMSd) analysis from a given GROMACS compatible trajectory.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

160 parser.add_argument('--config', required=False, help='Configuration file') 

161 

162 # Specific args of each building block 

163 required_args = parser.add_argument_group('required arguments') 

164 required_args.add_argument('--input_structure_path', required=True, help='Path to the input structure file. Accepted formats: tpr, gro, g96, pdb, brk, ent.') 

165 required_args.add_argument('--input_traj_path', required=True, help='Path to the GROMACS trajectory file. Accepted formats: xtc, trr, cpt, gro, g96, pdb, tng.') 

166 parser.add_argument('--input_index_path', required=False, help="Path to the GROMACS index file. Accepted formats: ndx.") 

167 required_args.add_argument('--output_xvg_path', required=True, help='Path to the XVG output file. Accepted formats: xvg.') 

168 

169 args = parser.parse_args() 

170 args.config = args.config or "{}" 

171 properties = settings.ConfReader(config=args.config).get_prop_dic() 

172 

173 # Specific call of each building block 

174 gmx_rms(input_structure_path=args.input_structure_path, 

175 input_traj_path=args.input_traj_path, 

176 output_xvg_path=args.output_xvg_path, 

177 input_index_path=args.input_index_path, 

178 properties=properties) 

179 

180 

181if __name__ == '__main__': 

182 main()