Coverage for biobb_analysis / ambertools / cpptraj_rgyr.py: 94%

63 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-12 10:53 +0000

1#!/usr/bin/env python3 

2 

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

4 

5from typing import Optional 

6from pathlib import PurePath 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools.file_utils import launchlogger 

9from 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, setup_structure 

10 

11 

12class CpptrajRgyr(BiobbObject): 

13 """ 

14 | biobb_analysis CpptrajRgyr 

15 | Wrapper of the Ambertools Cpptraj module for computing the radius of gyration (Rgyr) from a given cpptraj compatible trajectory. 

16 | 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://raw.githubusercontent.com/Amber-MD/cpptraj/master/doc/CpptrajManual.pdf>`_. 

17 

18 Args: 

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

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

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

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

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

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

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

26 * **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 `@*`). 

27 * **binary_path** (*str*) - ("cpptraj") Path to the cpptraj 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*) - ('afandiadib/ambertools:serial') 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.ambertools.cpptraj_rgyr import cpptraj_rgyr 

42 prop = { 

43 'start': 1, 

44 'end': -1, 

45 'steps': 1, 

46 'mask': 'c-alpha' 

47 } 

48 cpptraj_rgyr(input_top_path='/path/to/myTopology.top', 

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

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

51 properties=prop) 

52 

53 Info: 

54 * wrapped_software: 

55 * name: Ambertools Cpptraj 

56 * version: >=22.5 

57 * license: GNU 

58 * ontology: 

59 * name: EDAM 

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

61 

62 """ 

63 

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

65 properties=None, **kwargs) -> None: 

66 properties = properties or {} 

67 

68 # Call parent class constructor 

69 super().__init__(properties) 

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

71 

72 # Input/Output files 

73 self.io_dict = { 

74 "in": {"input_top_path": input_top_path, "input_traj_path": input_traj_path}, 

75 "out": {"output_cpptraj_path": output_cpptraj_path} 

76 } 

77 

78 # Properties specific for BB 

79 self.instructions_file = get_default_value('instructions_file') 

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

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

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

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

84 self.properties = properties 

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

86 

87 # Check the properties 

88 self.check_init(properties) 

89 

90 def check_data_params(self, out_log, err_log): 

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

92 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__) 

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

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

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

96 

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

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

99 instructions_list = [] 

100 # different path if container execution or not 

101 if self.container_path: 

102 self.instructions_file = str(PurePath(self.stage_io_dict['unique_dir']).joinpath("cpptraj.in")) 

103 self.instructions_file_path = str(PurePath(self.container_volume_path).joinpath("cpptraj.in")) 

104 else: 

105 self.instructions_file = self.create_tmp_file(self.instructions_file) 

106 self.instructions_file_path = self.instructions_file 

107 

108 # parm 

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

110 

111 # trajin 

112 in_params = get_in_parameters(self.in_parameters, out_log) 

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

114 

115 # Set up 

116 instructions_list += setup_structure(self) 

117 

118 # mask 

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

120 if mask: 

121 strip_mask = get_negative_mask(mask, out_log) 

122 instructions_list.append('strip ' + strip_mask) 

123 

124 # output 

125 instructions_list.append('radgyr time 1 out ' + container_io_dict["out"]["output_cpptraj_path"]) 

126 

127 # create .in file 

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

129 for line in instructions_list: 

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

131 

132 return self.instructions_file_path 

133 

134 @launchlogger 

135 def launch(self) -> int: 

136 """Execute the :class:`CpptrajRgyr <ambertools.cpptraj_rgyr.CpptrajRgyr>` object.""" 

137 

138 # check input/output paths and parameters 

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

140 

141 # Setup Biobb 

142 if self.check_restart(): 

143 return 0 

144 self.stage_files() 

145 

146 # create instructions file 

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

148 

149 # create cmd and launch execution 

150 self.cmd = [self.binary_path, '-i', self.instructions_file_path] 

151 

152 # Run Biobb block 

153 self.run_biobb() 

154 

155 # Copy files to host 

156 self.copy_to_host() 

157 

158 # remove temporary folder(s) 

159 self.remove_tmp_files() 

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

161 

162 return self.return_code 

163 

164 

165def cpptraj_rgyr(input_top_path: str, input_traj_path: str, output_cpptraj_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

166 """Create the :class:`CpptrajRgyr <ambertools.cpptraj_rgyr.CpptrajRgyr>` class and 

167 execute the :meth:`launch() <ambertools.cpptraj_rgyr.CpptrajRgyr.launch>` method.""" 

168 return CpptrajRgyr(**dict(locals())).launch() 

169 

170 

171cpptraj_rgyr.__doc__ = CpptrajRgyr.__doc__ 

172main = CpptrajRgyr.get_main(cpptraj_rgyr, "Computes the radius of gyration (Rgyr) from a given cpptraj compatible trajectory.") 

173 

174if __name__ == '__main__': 

175 main()