Coverage for biobb_analysis/ambertools/cpptraj_rmsf.py: 83%

87 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-06 15:22 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the Cpptraj Rmsf class and the command line interface.""" 

4import argparse 

5from pathlib import PurePath 

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.ambertools.common import get_default_value, check_top_path, check_traj_path, check_out_path, get_binary_path, get_in_parameters, get_negative_mask, copy_instructions_file_to_container, setup_structure, get_mask, get_reference 

11 

12 

13class CpptrajRmsf(BiobbObject): 

14 """ 

15 | biobb_analysis CpptrajRmsf 

16 | Wrapper of the Ambertools Cpptraj module for calculating the Root Mean Square fluctuations (RMSf) of a given cpptraj compatible trajectory. 

17 | Cpptraj (the successor to ptraj) is the main program in Ambertools for processing coordinate trajectories and data files. The parameter names and defaults are the same as the ones in the official `Cpptraj manual <https://amber-md.github.io/cpptraj/CPPTRAJ.xhtml>`_. 

18 

19 Args: 

20 input_top_path (str): Path to the input structure or topology file. File type: input. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/data/ambertools/cpptraj.parm.top>`_. Accepted formats: top (edam:format_3881), pdb (edam:format_1476), prmtop (edam:format_3881), parmtop (edam:format_3881), zip (edam:format_3987). 

21 input_traj_path (str): Path to the input trajectory to be processed. File type: input. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/data/ambertools/cpptraj.traj.dcd>`_. Accepted formats: mdcrd (edam:format_3878), crd (edam:format_3878), cdf (edam:format_3650), netcdf (edam:format_3650), nc (edam:format_3650), restart (edam:format_3886), ncrestart (edam:format_3886), restartnc (edam:format_3886), dcd (edam:format_3878), charmm (edam:format_3887), cor (edam:format_2033), pdb (edam:format_1476), mol2 (edam:format_3816), trr (edam:format_3910), gro (edam:format_2033), binpos (edam:format_3885), xtc (edam:format_3875), cif (edam:format_1477), arc (edam:format_2333), sqm (edam:format_2033), sdf (edam:format_3814), conflib (edam:format_2033). 

22 input_exp_path (str) (Optional): Path to the experimental reference file (required if reference = experimental). File type: input. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/data/ambertools/experimental.1e5t.pdb>`_. Accepted formats: pdb (edam:format_1476). 

23 output_cpptraj_path (str): Path to the output processed analysis. File type: output. `Sample file <https://github.com/bioexcel/biobb_analysis/raw/master/biobb_analysis/test/reference/ambertools/ref_cpptraj.rmsf.first.dat>`_. Accepted formats: dat (edam:format_1637), agr (edam:format_2033), xmgr (edam:format_2033), gnu (edam:format_2033). 

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

25 * **start** (*int*) - (1) [1~100000|1] Starting frame for slicing 

26 * **end** (*int*) - (-1) [-1~100000|1] Ending frame for slicing 

27 * **steps** (*int*) - (1) [1~100000|1] Step for slicing 

28 * **mask** (*str*) - ("all-atoms") Mask definition. Values: c-alpha (All c-alpha atoms; protein only), backbone (Backbone atoms), all-atoms (All system atoms), heavy-atoms (System heavy atoms; not hydrogen), side-chain (All not backbone atoms), solute (All system atoms except solvent atoms), ions (All ion molecules), solvent (All solvent atoms), AnyAmberFromatMask (Amber atom selection syntax like `@*`). 

29 * **reference** (*str*) - ("first") Reference definition. Values: first (Use the first trajectory frame as reference), average (Use the average of all trajectory frames as reference), experimental (Use the experimental structure as reference). 

30 * **binary_path** (*str*) - ("cpptraj") Path to the cpptraj executable binary. 

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

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

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

34 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

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

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

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

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

39 

40 Examples: 

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

42 

43 from biobb_analysis.ambertools.cpptraj_rmsf import cpptraj_rmsf 

44 prop = { 

45 'start': 1, 

46 'end': -1, 

47 'steps': 1, 

48 'mask': 'c-alpha', 

49 'reference': 'first' 

50 } 

51 cpptraj_rmsf(input_top_path='/path/to/myTopology.top', 

52 input_traj_path='/path/to/myTrajectory.dcd', 

53 output_cpptraj_path='/path/to/newAnalysis.dat', 

54 input_exp_path= '/path/to/myExpStructure.pdb', 

55 properties=prop) 

56 

57 Info: 

58 * wrapped_software: 

59 * name: Ambertools Cpptraj 

60 * version: >=20.0 

61 * license: GNU 

62 * ontology: 

63 * name: EDAM 

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

65 

66 """ 

67 

68 def __init__(self, input_top_path, input_traj_path, output_cpptraj_path, 

69 input_exp_path=None, properties=None, **kwargs) -> None: 

70 properties = properties or {} 

71 

72 # Call parent class constructor 

73 super().__init__(properties) 

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

75 

76 # Input/Output files 

77 self.io_dict = { 

78 "in": {"input_top_path": input_top_path, "input_traj_path": input_traj_path, "input_exp_path": input_exp_path}, 

79 "out": {"output_cpptraj_path": output_cpptraj_path} 

80 } 

81 

82 # Properties specific for BB 

83 self.instructions_file = get_default_value('instructions_file') 

84 self.start = properties.get('start', 1) 

85 self.end = properties.get('end', -1) 

86 self.steps = properties.get('steps', 1) 

87 self.mask = properties.get('mask', 'all-atoms') 

88 self.reference = properties.get('reference', 'first') 

89 self.properties = properties 

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

91 

92 # Check the properties 

93 self.check_properties(properties) 

94 self.check_arguments() 

95 

96 def check_data_params(self, out_log, err_log): 

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

98 self.io_dict["in"]["input_top_path"], self.input_top_path_orig = check_top_path(self.io_dict["in"]["input_top_path"], out_log, self.__class__.__name__) 

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

100 self.io_dict["out"]["output_cpptraj_path"] = check_out_path(self.io_dict["out"]["output_cpptraj_path"], out_log, self.__class__.__name__) 

101 self.in_parameters = {'start': self.start, 'end': self.end, 'step': self.steps, 'mask': self.mask, 'reference': self.reference} 

102 

103 def create_instructions_file(self, container_io_dict, out_log, err_log): 

104 """Creates an input file using the properties file settings""" 

105 instructions_list = [] 

106 # different path if container execution or not 

107 if self.container_path: 

108 self.instructions_file = str(PurePath(self.container_volume_path).joinpath(self.instructions_file)) 

109 else: 

110 self.instructions_file = str(PurePath(fu.create_unique_dir()).joinpath(self.instructions_file)) 

111 fu.create_name(prefix=self.prefix, step=self.step, name=self.instructions_file) 

112 

113 # parm 

114 instructions_list.append('parm ' + container_io_dict["in"]["input_top_path"]) 

115 

116 # trajin 

117 in_params = get_in_parameters(self.in_parameters, out_log) 

118 instructions_list.append('trajin ' + container_io_dict["in"]["input_traj_path"] + ' ' + in_params) 

119 

120 # Set up 

121 instructions_list += setup_structure(self) 

122 

123 # mask 

124 mask = self.in_parameters.get('mask', '') 

125 ref_mask = '' 

126 if mask: 

127 strip_mask = get_negative_mask(mask, out_log) 

128 ref_mask = get_mask(mask, out_log) 

129 instructions_list.append('strip ' + strip_mask) 

130 

131 # reference 

132 reference = self.in_parameters.get('reference', '') 

133 inp_exp_pth = None 

134 if "input_exp_path" in container_io_dict["in"]: 

135 inp_exp_pth = container_io_dict["in"]["input_exp_path"] 

136 instructions_list += get_reference(reference, container_io_dict["out"]["output_cpptraj_path"], inp_exp_pth, ref_mask, False, self.__class__.__name__, out_log) 

137 instructions_list.append('atomicfluct out ' + container_io_dict["out"]["output_cpptraj_path"] + ' byres') 

138 

139 # create .in file 

140 with open(self.instructions_file, 'w') as mdp: 

141 for line in instructions_list: 

142 mdp.write(line.strip() + '\n') 

143 

144 return self.instructions_file 

145 

146 @launchlogger 

147 def launch(self) -> int: 

148 """Execute the :class:`CpptrajRmsf <ambertools.cpptraj_rmsf.CpptrajRmsf>` ambertools.cpptraj_rmsf.CpptrajRmsf object.""" 

149 

150 # check input/output paths and parameters 

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

152 

153 # Setup Biobb 

154 if self.check_restart(): 

155 return 0 

156 self.stage_files() 

157 

158 # create instructions file 

159 self.create_instructions_file(self.stage_io_dict, self.out_log, self.err_log) 

160 

161 # if container execution, copy intructions file to container 

162 if self.container_path: 

163 copy_instructions_file_to_container(self.instructions_file, self.stage_io_dict['unique_dir']) 

164 

165 # create cmd and launch execution 

166 self.cmd = [self.binary_path, '-i', self.instructions_file] 

167 

168 # Run Biobb block 

169 self.run_biobb() 

170 

171 # Copy files to host 

172 self.copy_to_host() 

173 

174 # remove temporary folder(s) 

175 self.tmp_files.extend([ 

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

177 PurePath(self.instructions_file).parent 

178 ]) 

179 self.remove_tmp_files() 

180 

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

182 

183 return self.return_code 

184 

185 

186def cpptraj_rmsf(input_top_path: str, input_traj_path: str, output_cpptraj_path: str, input_exp_path: str = None, properties: dict = None, **kwargs) -> int: 

187 """Execute the :class:`CpptrajRmsf <ambertools.cpptraj_rmsf.CpptrajRmsf>` class and 

188 execute the :meth:`launch() <ambertools.cpptraj_rmsf.CpptrajRmsf.launch>` method.""" 

189 

190 return CpptrajRmsf(input_top_path=input_top_path, 

191 input_traj_path=input_traj_path, 

192 output_cpptraj_path=output_cpptraj_path, 

193 input_exp_path=input_exp_path, 

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

195 

196 

197def main(): 

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

199 parser = argparse.ArgumentParser(description="Calculates the Root Mean Square fluctuations (RMSf) of a given cpptraj compatible trajectory.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

201 

202 # Specific args of each building block 

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

204 required_args.add_argument('--input_top_path', required=True, help='Path to the input structure or topology file. Accepted formats: top, pdb, prmtop, parmtop, zip.') 

205 required_args.add_argument('--input_traj_path', required=True, help='Path to the input trajectory to be processed. Accepted formats: crd, cdf, netcdf, restart, ncrestart, restartnc, dcd, charmm, cor, pdb, mol2, trr, gro, binpos, xtc, cif, arc, sqm, sdf, conflib.') 

206 parser.add_argument('--input_exp_path', required=False, help='Path to the experimental reference file (required if reference = experimental).') 

207 required_args.add_argument('--output_cpptraj_path', required=True, help='Path to the output processed analysis.') 

208 

209 args = parser.parse_args() 

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

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

212 

213 # Specific call of each building block 

214 cpptraj_rmsf(input_top_path=args.input_top_path, 

215 input_traj_path=args.input_traj_path, 

216 output_cpptraj_path=args.output_cpptraj_path, 

217 input_exp_path=args.input_exp_path, 

218 properties=properties) 

219 

220 

221if __name__ == '__main__': 

222 main()