Coverage for biobb_amber/cphstats/cestats_run.py: 65%

111 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 08:11 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.configuration import settings 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_amber.cphstats.common import check_input_path, check_output_path 

9 

10 

11class CestatsRun(BiobbObject): 

12 """ 

13 | biobb_amber CestatsRun 

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

15 | Analyzing the results of constant Redox potential MD simulations using cestats tool from the AMBER MD package. 

16 

17 Args: 

18 input_cein_path (str): Input cein or cpein file (from pmemd or sander) with titrating residue information. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/cphstats/structure.cein>`_. Accepted formats: cein (edam:format_2330), cpein (edam:format_2330). 

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

20 output_dat_path (str): Output file to which the standard calceo-type statistics are written. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cphstats/cestats.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/cestats.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/cestats.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/cestats.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/cestats.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/cestats.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 calceo-style output file. Options are: False - Just print fraction protonated. True - Print everything calceo 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 * **reduced** (*bool*) - (True) Print out reduction fraction instead of oxidation fraction in time series data. 

31 * **eos** (*bool*) - (False) Print predicted Eos -via Nernst equation- in place of fraction reduced or oxidized. 

32 * **calceo** (*bool*) - (True) Triggers the calceo-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 cestats 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*) - ("cestats") Path to the cestats 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 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

47 

48 Examples: 

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

50 

51 from biobb_amber.cestats.cestats_run import cestats_run 

52 prop = { 

53 'timestep' : 0.002 

54 } 

55 cestats_run(input_cein_path='/path/to/cein.cein', 

56 input_ceout_path='/path/to/ceout.cpout', 

57 output_dat_path='/path/to/cestats.dat', 

58 properties=prop) 

59 

60 Info: 

61 * wrapped_software: 

62 * name: AMBER cestats 

63 * version: >=1.5 

64 * license: other 

65 * ontology: 

66 * name: EDAM 

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

68 

69 """ 

70 def __init__(self, input_cein_path: str, input_ceout_path: str, output_dat_path: str, output_population_path: str = None, 

71 output_chunk_path: str = None, output_cumulative_path: str = None, output_conditional_path: str = None, output_chunk_conditional_path: str = None, 

72 properties: dict = None, **kwargs) -> None: 

73 

74 properties = properties or {} 

75 

76 # Call parent class constructor 

77 super().__init__(properties) 

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

79 

80 # Input/Output files 

81 self.io_dict = { 

82 'in': {'input_cein_path': input_cein_path, 

83 'input_ceout_path': input_ceout_path}, 

84 'out': {'output_dat_path': output_dat_path, 

85 'output_population_path': output_population_path, 

86 'output_chunk_path': output_chunk_path, 

87 'output_cumulative_path': output_cumulative_path, 

88 'output_conditional_path': output_conditional_path, 

89 'output_chunk_conditional_path': output_chunk_conditional_path} 

90 } 

91 

92 # Properties specific for BB 

93 self.properties = properties 

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

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

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

97 self.reduced = properties.get('reduced', True) 

98 self.eos = properties.get('eos', False) 

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

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

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

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

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

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

105 self.binary_path = properties.get('binary_path', 'cestats') 

106 

107 # Check the properties 

108 self.check_properties(properties) 

109 self.check_arguments() 

110 

111 def check_data_params(self, out_log, err_log): 

112 """ Checks input/output paths correctness """ 

113 

114 # Check input(s) 

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

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

117 

118 # Check output(s) 

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

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

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

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

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

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

125 

126 @launchlogger 

127 def launch(self): 

128 """Launches the execution of the CestatsRun module.""" 

129 

130 # check input/output paths and parameters 

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

132 

133 # Setup Biobb 

134 if self.check_restart(): 

135 return 0 

136 self.stage_files() 

137 

138 # Command line 

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

140 self.cmd = [self.binary_path, 

141 '-O', 

142 '-i', self.stage_io_dict['in']['input_cein_path'], 

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

144 self.stage_io_dict['in']['input_ceout_path'] 

145 ] 

146 

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

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

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

150 

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

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

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

154 if self.chunk_window: 

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

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

157 

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

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

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

161 

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

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

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

165 

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

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

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

169 

170 if self.verbose: 

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

172 

173 if self.interval: 

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

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

176 

177 if self.reduced: 

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

179 else: 

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

181 

182 if self.eos: 

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

184 

185 if self.calceo: 

186 self.cmd.append('--calceo') 

187 else: 

188 self.cmd.append('--no-calceo') 

189 

190 if self.running_avg_window: 

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

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

193 

194 if self.cumulative: 

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

196 

197 if self.fix_remd: 

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

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

200 

201 if self.conditional: 

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

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

204 

205 # Run Biobb block 

206 self.run_biobb() 

207 

208 # Copy files to host 

209 self.copy_to_host() 

210 

211 # Remove temporary file(s) 

212 self.tmp_files.extend([ 

213 self.stage_io_dict.get("unique_dir") 

214 ]) 

215 self.remove_tmp_files() 

216 

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

218 

219 return self.return_code 

220 

221 

222def cestats_run(input_cein_path: str, input_ceout_path: str, 

223 output_dat_path: str, 

224 output_population_path: str = None, output_chunk_path: str = None, 

225 output_conditional_path: str = None, output_chunk_conditional_path: str = None, 

226 output_cumulative_path: str = None, 

227 properties: dict = None, **kwargs) -> int: 

228 """Create :class:`CestatsRun <cestats.chpstats_run.CestatsRun>`cestats.chpstats_run.CestatsRun class and 

229 execute :meth:`launch() <cestats.chpstats_run.CestatsRun.launch>` method""" 

230 

231 return CestatsRun(input_cein_path=input_cein_path, 

232 input_ceout_path=input_ceout_path, 

233 output_dat_path=output_dat_path, 

234 output_population_path=output_population_path, 

235 output_chunk_path=output_chunk_path, 

236 output_chunk_conditional_path=output_chunk_conditional_path, 

237 output_conditional_path=output_conditional_path, 

238 output_cumulative_path=output_cumulative_path, 

239 properties=properties).launch() 

240 

241 

242def main(): 

243 parser = argparse.ArgumentParser(description='Analyzing the results of constant Redox potential MD simulations using cestats tool from the AMBER MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

245 

246 # Specific args 

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

248 required_args.add_argument('--input_cein_path', required=True, help='Input cein or cpein file (from pmemd or sander) with titrating residue information. Accepted formats: cein, cpein.') 

249 required_args.add_argument('--input_ceout_path', required=True, help='Output ceout file (AMBER ceout). Accepted formats: ceout.') 

250 required_args.add_argument('--output_dat_path', required=True, help='Output file to which the standard calceo-type statistics are written. Accepted formats: dat, out, txt, o.') 

251 required_args.add_argument('--output_population_path', required=False, help='Output file where protonation state populations are printed for every state of every residue. Accepted formats: dat, out, txt, o.') 

252 required_args.add_argument('--output_chunk_path', required=False, help='Output file where the time series data calculated over chunks of the simulation are printed. Accepted formats: dat, out, txt, o.') 

253 required_args.add_argument('--output_cumulative_path', required=False, help='Output file where the cumulative time series data is printed. Accepted formats: dat, out, txt, o.') 

254 required_args.add_argument('--output_conditional_path', required=False, help='Output file with requested conditional probabilities. Accepted formats: dat, out, txt, o.') 

255 required_args.add_argument('--output_chunk_conditional_path', required=False, help='Output file with a time series of the conditional probabilities over a trajectory split up into chunks. Accepted formats: dat, out, txt, o.') 

256 

257 args = parser.parse_args() 

258 config = args.config if args.config else None 

259 properties = settings.ConfReader(config=config).get_prop_dic() 

260 

261 # Specific call 

262 cestats_run(input_cein_path=args.input_cein_path, 

263 input_ceout_path=args.input_ceout_path, 

264 output_dat_path=args.output_dat_path, 

265 output_population_path=args.output_population_path, 

266 output_chunk_path=args.output_chunk_path, 

267 output_cumulative_path=args.output_cumulative_path, 

268 output_conditional_path=args.output_conditional_path, 

269 output_chunk_conditional_path=args.output_chunk_conditional_path, 

270 properties=properties) 

271 

272 

273if __name__ == '__main__': 

274 main()