Coverage for biobb_amber / cphstats / cphstats_run.py: 74%

94 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-15 15:57 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the Cphstats class and the command line interface.""" 

4 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_amber.cphstats.common import check_input_path, check_output_path 

9 

10 

11class CphstatsRun(BiobbObject): 

12 """ 

13 | biobb_amber CphstatsRun 

14 | Wrapper of the `AmberTools (AMBER MD Package) cphstats tool <https://ambermd.org/AmberTools.php>`_ module. 

15 | Analyzing the results of constant pH MD simulations using cphstats tool from the AMBER MD package. 

16 

17 Args: 

18 input_cpin_path (str): Input constant pH file (AMBER cpin). File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/cphstats/structure.cpin>`_. Accepted formats: cpin (edam:format_2330). 

19 input_cpout_path (str): Output constant pH file (AMBER cpout). File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/cphstats/sander.pH.cpout>`_. Accepted formats: cpout (edam:format_2330), zip (edam:format_3987), gzip (edam:format_3987). 

20 output_dat_path (str): Output file to which the standard calcpka-type statistics are written. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cphstats/cphstats.pH.dat>`_. Accepted formats: dat (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

21 output_population_path (str) (Optional): Output file where protonation state populations are printed for every state of every residue. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cphstats/cphstats.pH.pop.dat>`_. Accepted formats: dat (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

22 output_chunk_path (str) (Optional): Output file where the time series data calculated over chunks of the simulation are printed. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cphstats/cphstats.pH.dat>`_. Accepted formats: dat (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

23 output_cumulative_path (str) (Optional): Output file where the cumulative time series data is printed. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cphstats/cphstats.pH.dat>`_. Accepted formats: dat (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

24 output_conditional_path (str) (Optional): Output file with requested conditional probabilities. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cphstats/cphstats.pH.dat>`_. Accepted formats: dat (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

25 output_chunk_conditional_path (str) (Optional): Output file with a time series of the conditional probabilities over a trajectory split up into chunks. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cphstats/cphstats.pH.dat>`_. Accepted formats: dat (edam:format_2330), out (edam:format_2330), txt (edam:format_2330), o (edam:format_2330). 

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

27 * **timestep** (*float*) - (0.002) Simulation time step -in ps-, used to print data as a function of time. 

28 * **verbose** (*bool*) - (False) Controls how much information is printed to the calcpka-style output file. Options are: False - Just print fraction protonated. True - Print everything calcpka prints. 

29 * **interval** (*int*) - (1000) Interval between which to print out time series data like chunks, cumulative data, and running averages. It is also used as the window of the conditional probability time series. 

30 * **protonated** (*bool*) - (True) Print out protonation fraction instead of deprotonation fraction in time series data. 

31 * **pka** (*bool*) - (False) Print predicted pKas -via Henderson-Hasselbalch equation- in place of fraction -de-protonated. 

32 * **calcpka** (*bool*) - (True) Triggers the calcpka-style output. 

33 * **running_avg_window** (*int*) - (100) Defines a window size -in MD steps- for a moving, running average time series. 

34 * **chunk_window** (*int*) - (100) Computes the time series data over a chunk of the simulation of this specified size -window- time steps. 

35 * **cumulative** (*bool*) - (False) Computes the cumulative average time series data over the course of the trajectory. 

36 * **fix_remd** (*str*) - ("") This option will trigger cphstats to reassemble the titration data into pH-specific ensembles. This is an exclusive mode of the program, no other analyses will be done. 

37 * **conditional** (*str*) - ("") Evaluates conditional probabilities. CONDITIONAL should be a string of the format: <resid>:<state>,<resid>:<state>,... or <resid>:PROT,<resid>:DEPROT,... or <resid>:<state1>;<state2>,<resid>:PROT,... where <resid> is the residue number in the prmtop and <state> is either the state number or -p-rotonated or -d-eprotonated, case-insensitive. 

38 * **binary_path** (*str*) - ("cphstats") Path to the cphstats executable binary. 

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

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

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

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

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

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

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

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

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

48 

49 Examples: 

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

51 

52 from biobb_amber.cphstats.cphstats_run import cphstats_run 

53 prop = { 

54 'timestep' : 0.002 

55 } 

56 cphstats_run(input_cpin_path='/path/to/cpin.cpin', 

57 input_cpout_path='/path/to/cpout.cpout', 

58 output_dat_path='/path/to/cphstats.dat', 

59 properties=prop) 

60 

61 Info: 

62 * wrapped_software: 

63 * name: AMBER cphstats 

64 * version: >=1.5 

65 * license: other 

66 * ontology: 

67 * name: EDAM 

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

69 

70 """ 

71 

72 def __init__(self, input_cpin_path: str, input_cpout_path: str, output_dat_path: str, output_population_path: Optional[str] = None, 

73 output_chunk_path: Optional[str] = None, output_cumulative_path: Optional[str] = None, output_conditional_path: Optional[str] = None, output_chunk_conditional_path: Optional[str] = None, 

74 properties: Optional[dict] = None, **kwargs) -> None: 

75 

76 properties = properties or {} 

77 

78 # Call parent class constructor 

79 super().__init__(properties) 

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

81 

82 # Input/Output files 

83 self.io_dict = { 

84 'in': {'input_cpin_path': input_cpin_path, 

85 'input_cpout_path': input_cpout_path}, 

86 'out': {'output_dat_path': output_dat_path, 

87 'output_population_path': output_population_path, 

88 'output_chunk_path': output_chunk_path, 

89 'output_cumulative_path': output_cumulative_path, 

90 'output_conditional_path': output_conditional_path, 

91 'output_chunk_conditional_path': output_chunk_conditional_path} 

92 } 

93 

94 # Properties specific for BB 

95 self.properties = properties 

96 self.timestep = properties.get('timestep', 0.002) 

97 self.verbose = properties.get('verbose', False) 

98 self.interval = properties.get('interval', 1000) 

99 self.protonated = properties.get('protonated', True) 

100 self.pka = properties.get('pka', False) 

101 self.calcpka = properties.get('calcpka', True) 

102 self.running_avg_window = properties.get('running_avg_window', 100) 

103 self.chunk_window = properties.get('chunk_window', 100) 

104 self.cumulative = properties.get('cumulative', False) 

105 self.fix_remd = properties.get('fix_remd', "") 

106 self.conditional = properties.get('conditional', "") 

107 self.binary_path = properties.get('binary_path', 'cphstats') 

108 

109 # Check the properties 

110 self.check_properties(properties) 

111 self.check_arguments() 

112 

113 def check_data_params(self, out_log, err_log): 

114 """ Checks input/output paths correctness """ 

115 

116 # Check input(s) 

117 self.io_dict["in"]["input_cpin_path"] = check_input_path(self.io_dict["in"]["input_cpin_path"], "input_cpin_path", False, out_log, self.__class__.__name__) 

118 self.io_dict["in"]["input_cpout_path"] = check_input_path(self.io_dict["in"]["input_cpout_path"], "input_cpout_path", False, out_log, self.__class__.__name__) 

119 

120 # Check output(s) 

121 self.io_dict["out"]["output_dat_path"] = check_output_path(self.io_dict["out"]["output_dat_path"], "output_dat_path", False, out_log, self.__class__.__name__) 

122 self.io_dict["out"]["output_population_path"] = check_output_path(self.io_dict["out"]["output_population_path"], "output_population_path", True, out_log, self.__class__.__name__) 

123 self.io_dict["out"]["output_chunk_path"] = check_output_path(self.io_dict["out"]["output_chunk_path"], "output_chunk_path", True, out_log, self.__class__.__name__) 

124 self.io_dict["out"]["output_cumulative_path"] = check_output_path(self.io_dict["out"]["output_cumulative_path"], "output_cumulative_path", True, out_log, self.__class__.__name__) 

125 self.io_dict["out"]["output_chunk_conditional_path"] = check_output_path(self.io_dict["out"]["output_chunk_conditional_path"], "output_chunk_conditional_path", True, out_log, self.__class__.__name__) 

126 self.io_dict["out"]["output_conditional_path"] = check_output_path(self.io_dict["out"]["output_conditional_path"], "output_conditional_path", True, out_log, self.__class__.__name__) 

127 

128 @launchlogger 

129 def launch(self): 

130 """Launches the execution of the CphstatsRun module.""" 

131 

132 # check input/output paths and parameters 

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

134 

135 # Setup Biobb 

136 if self.check_restart(): 

137 return 0 

138 self.stage_files() 

139 

140 # Command line 

141 # cphstats -i 4LYT.equil.cpin 0/4LYT.md1.cpout -o pH0_calcpka.dat --population pH0_populations.dat 

142 self.cmd = [self.binary_path, 

143 '-O', 

144 '-i', self.stage_io_dict['in']['input_cpin_path'], 

145 '-o', self.stage_io_dict['out']['output_dat_path'], 

146 self.stage_io_dict['in']['input_cpout_path'] 

147 ] 

148 

149 if self.io_dict['out']['output_population_path']: 

150 self.cmd.append('--population ') 

151 self.cmd.append(self.stage_io_dict['out']['output_population_path']) 

152 

153 if self.io_dict['out']['output_chunk_path']: 

154 self.cmd.append('--chunk-out ') 

155 self.cmd.append(self.stage_io_dict['out']['output_chunk_path']) 

156 if self.chunk_window: 

157 self.cmd.append('--chunk') 

158 self.cmd.append(str(self.chunk_window)) 

159 

160 if self.io_dict['out']['output_cumulative_path']: 

161 self.cmd.append('--cumulative-out ') 

162 self.cmd.append(self.stage_io_dict['out']['output_cumulative_path']) 

163 

164 if self.io_dict['out']['output_conditional_path']: 

165 self.cmd.append('--conditional-output ') 

166 self.cmd.append(self.stage_io_dict['out']['output_conditional_path']) 

167 

168 if self.io_dict['out']['output_chunk_conditional_path']: 

169 self.cmd.append('--chunk-conditional ') 

170 self.cmd.append(self.stage_io_dict['out']['output_chunk_conditional_path']) 

171 

172 if self.verbose: 

173 self.cmd.append('-v 1') 

174 

175 if self.interval: 

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

177 self.cmd.append(str(self.interval)) 

178 

179 if self.protonated: 

180 self.cmd.append('-p') 

181 else: 

182 self.cmd.append('-d') 

183 

184 if self.pka: 

185 self.cmd.append('-a') 

186 

187 if self.calcpka: 

188 self.cmd.append('--calcpka') 

189 else: 

190 self.cmd.append('--no-calcpka') 

191 

192 if self.running_avg_window: 

193 self.cmd.append('-r') 

194 self.cmd.append(str(self.running_avg_window)) 

195 

196 if self.cumulative: 

197 self.cmd.append('--cumulative') 

198 

199 if self.fix_remd: 

200 self.cmd.append('--fix-remd') 

201 self.cmd.append(str(self.fix_remd)) 

202 

203 if self.conditional: 

204 self.cmd.append('-c') 

205 self.cmd.append(str(self.conditional)) 

206 

207 # Run Biobb block 

208 self.run_biobb() 

209 

210 # Copy files to host 

211 self.copy_to_host() 

212 

213 # Remove temporary file(s) 

214 self.remove_tmp_files() 

215 

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

217 

218 return self.return_code 

219 

220 

221def cphstats_run(input_cpin_path: str, input_cpout_path: str, 

222 output_dat_path: str, 

223 output_population_path: Optional[str] = None, output_chunk_path: Optional[str] = None, 

224 output_conditional_path: Optional[str] = None, output_chunk_conditional_path: Optional[str] = None, 

225 output_cumulative_path: Optional[str] = None, 

226 properties: Optional[dict] = None, **kwargs) -> int: 

227 """Create the :class:`CphstatsRun <cphstats.cphstats_run.CphstatsRun>` class and 

228 execute the :meth:`launch() <cphstats.cphstats_run.CphstatsRun.launch>` method.""" 

229 return CphstatsRun(**dict(locals())).launch() 

230 

231 

232cphstats_run.__doc__ = CphstatsRun.__doc__ 

233main = CphstatsRun.get_main(cphstats_run, "Analyzing the results of constant pH MD simulations using cphstats tool from the AMBER MD package.") 

234 

235if __name__ == '__main__': 

236 main()