Coverage for biobb_amber/cphstats/cestats_run.py: 64%
111 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 08:28 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 08:28 +0000
1#!/usr/bin/env python3
3"""Module containing the Cestats 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.file_utils import launchlogger
9from biobb_amber.cphstats.common import check_input_path, check_output_path
12class CestatsRun(BiobbObject):
13 """
14 | biobb_amber CestatsRun
15 | Wrapper of the `AmberTools (AMBER MD Package) cestats tool <https://ambermd.org/AmberTools.php>`_ module.
16 | Analyzing the results of constant Redox potential MD simulations using cestats tool from the AMBER MD package.
18 Args:
19 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).
20 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).
21 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).
22 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).
23 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).
24 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).
25 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).
26 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).
27 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
28 * **timestep** (*float*) - (0.002) Simulation time step -in ps-, used to print data as a function of time.
29 * **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.
30 * **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.
31 * **reduced** (*bool*) - (True) Print out reduction fraction instead of oxidation fraction in time series data.
32 * **eos** (*bool*) - (False) Print predicted Eos -via Nernst equation- in place of fraction reduced or oxidized.
33 * **calceo** (*bool*) - (True) Triggers the calceo-style output.
34 * **running_avg_window** (*int*) - (100) Defines a window size -in MD steps- for a moving, running average time series.
35 * **chunk_window** (*int*) - (100) Computes the time series data over a chunk of the simulation of this specified size -window- time steps.
36 * **cumulative** (*bool*) - (False) Computes the cumulative average time series data over the course of the trajectory.
37 * **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.
38 * **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.
39 * **binary_path** (*str*) - ("cestats") Path to the cestats executable binary.
40 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
41 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
42 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
43 * **container_path** (*str*) - (None) Container path definition.
44 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition.
45 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
46 * **container_working_dir** (*str*) - (None) Container working directory definition.
47 * **container_user_id** (*str*) - (None) Container user_id definition.
48 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
50 Examples:
51 This is a use example of how to use the building block from Python::
53 from biobb_amber.cestats.cestats_run import cestats_run
54 prop = {
55 'timestep' : 0.002
56 }
57 cestats_run(input_cein_path='/path/to/cein.cein',
58 input_ceout_path='/path/to/ceout.cpout',
59 output_dat_path='/path/to/cestats.dat',
60 properties=prop)
62 Info:
63 * wrapped_software:
64 * name: AMBER cestats
65 * version: >=1.5
66 * license: other
67 * ontology:
68 * name: EDAM
69 * schema: http://edamontology.org/EDAM.owl
71 """
73 def __init__(self, input_cein_path: str, input_ceout_path: str, output_dat_path: str, output_population_path: Optional[str] = None,
74 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,
75 properties: Optional[dict] = None, **kwargs) -> None:
77 properties = properties or {}
79 # Call parent class constructor
80 super().__init__(properties)
81 self.locals_var_dict = locals().copy()
83 # Input/Output files
84 self.io_dict = {
85 'in': {'input_cein_path': input_cein_path,
86 'input_ceout_path': input_ceout_path},
87 'out': {'output_dat_path': output_dat_path,
88 'output_population_path': output_population_path,
89 'output_chunk_path': output_chunk_path,
90 'output_cumulative_path': output_cumulative_path,
91 'output_conditional_path': output_conditional_path,
92 'output_chunk_conditional_path': output_chunk_conditional_path}
93 }
95 # Properties specific for BB
96 self.properties = properties
97 self.timestep = properties.get('timestep', 0.002)
98 self.verbose = properties.get('verbose', False)
99 self.interval = properties.get('interval', 1000)
100 self.reduced = properties.get('reduced', True)
101 self.eos = properties.get('eos', False)
102 self.calceo = properties.get('calceo', True)
103 self.running_avg_window = properties.get('running_avg_window', 100)
104 self.chunk_window = properties.get('chunk_window', 100)
105 self.cumulative = properties.get('cumulative', False)
106 self.fix_remd = properties.get('fix_remd', "")
107 self.conditional = properties.get('conditional', "")
108 self.binary_path = properties.get('binary_path', 'cestats')
110 # Check the properties
111 self.check_properties(properties)
112 self.check_arguments()
114 def check_data_params(self, out_log, err_log):
115 """ Checks input/output paths correctness """
117 # Check input(s)
118 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__)
119 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__)
121 # Check output(s)
122 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__)
123 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__)
124 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__)
125 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__)
126 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__)
127 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__)
129 @launchlogger
130 def launch(self):
131 """Launches the execution of the CestatsRun module."""
133 # check input/output paths and parameters
134 self.check_data_params(self.out_log, self.err_log)
136 # Setup Biobb
137 if self.check_restart():
138 return 0
139 self.stage_files()
141 # Command line
142 # cphstats -i 4LYT.equil.cpin 0/4LYT.md1.cpout -o pH0_calcpka.dat --population pH0_populations.dat
143 self.cmd = [self.binary_path,
144 '-O',
145 '-i', self.stage_io_dict['in']['input_cein_path'],
146 '-o', self.stage_io_dict['out']['output_dat_path'],
147 self.stage_io_dict['in']['input_ceout_path']
148 ]
150 if self.io_dict['out']['output_population_path']:
151 self.cmd.append('--population ')
152 self.cmd.append(self.stage_io_dict['out']['output_population_path'])
154 if self.io_dict['out']['output_chunk_path']:
155 self.cmd.append('--chunk-out ')
156 self.cmd.append(self.stage_io_dict['out']['output_chunk_path'])
157 if self.chunk_window:
158 self.cmd.append('--chunk')
159 self.cmd.append(str(self.chunk_window))
161 if self.io_dict['out']['output_cumulative_path']:
162 self.cmd.append('--cumulative-out ')
163 self.cmd.append(self.stage_io_dict['out']['output_cumulative_path'])
165 if self.io_dict['out']['output_conditional_path']:
166 self.cmd.append('--conditional-output ')
167 self.cmd.append(self.stage_io_dict['out']['output_conditional_path'])
169 if self.io_dict['out']['output_chunk_conditional_path']:
170 self.cmd.append('--chunk-conditional ')
171 self.cmd.append(self.stage_io_dict['out']['output_chunk_conditional_path'])
173 if self.verbose:
174 self.cmd.append('-v 1')
176 if self.interval:
177 self.cmd.append('-n')
178 self.cmd.append(str(self.interval))
180 if self.reduced:
181 self.cmd.append('-p')
182 else:
183 self.cmd.append('-d')
185 if self.eos:
186 self.cmd.append('-a')
188 if self.calceo:
189 self.cmd.append('--calceo')
190 else:
191 self.cmd.append('--no-calceo')
193 if self.running_avg_window:
194 self.cmd.append('-r')
195 self.cmd.append(str(self.running_avg_window))
197 if self.cumulative:
198 self.cmd.append('--cumulative')
200 if self.fix_remd:
201 self.cmd.append('--fix-remd')
202 self.cmd.append(str(self.fix_remd))
204 if self.conditional:
205 self.cmd.append('-c')
206 self.cmd.append(str(self.conditional))
208 # Run Biobb block
209 self.run_biobb()
211 # Copy files to host
212 self.copy_to_host()
214 # Remove temporary file(s)
215 # self.tmp_files.extend([
216 # self.stage_io_dict.get("unique_dir", "")
217 # ])
218 self.remove_tmp_files()
220 self.check_arguments(output_files_created=True, raise_exception=False)
222 return self.return_code
225def cestats_run(input_cein_path: str, input_ceout_path: str,
226 output_dat_path: str,
227 output_population_path: Optional[str] = None, output_chunk_path: Optional[str] = None,
228 output_conditional_path: Optional[str] = None, output_chunk_conditional_path: Optional[str] = None,
229 output_cumulative_path: Optional[str] = None,
230 properties: Optional[dict] = None, **kwargs) -> int:
231 """Create :class:`CestatsRun <cestats.chpstats_run.CestatsRun>`cestats.chpstats_run.CestatsRun class and
232 execute :meth:`launch() <cestats.chpstats_run.CestatsRun.launch>` method"""
234 return CestatsRun(input_cein_path=input_cein_path,
235 input_ceout_path=input_ceout_path,
236 output_dat_path=output_dat_path,
237 output_population_path=output_population_path,
238 output_chunk_path=output_chunk_path,
239 output_chunk_conditional_path=output_chunk_conditional_path,
240 output_conditional_path=output_conditional_path,
241 output_cumulative_path=output_cumulative_path,
242 properties=properties).launch()
244 cestats_run.__doc__ = CestatsRun.__doc__
247def main():
248 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))
249 parser.add_argument('--config', required=False, help='Configuration file')
251 # Specific args
252 required_args = parser.add_argument_group('required arguments')
253 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.')
254 required_args.add_argument('--input_ceout_path', required=True, help='Output ceout file (AMBER ceout). Accepted formats: ceout.')
255 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.')
256 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.')
257 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.')
258 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.')
259 required_args.add_argument('--output_conditional_path', required=False, help='Output file with requested conditional probabilities. Accepted formats: dat, out, txt, o.')
260 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.')
262 args = parser.parse_args()
263 config = args.config if args.config else None
264 properties = settings.ConfReader(config=config).get_prop_dic()
266 # Specific call
267 cestats_run(input_cein_path=args.input_cein_path,
268 input_ceout_path=args.input_ceout_path,
269 output_dat_path=args.output_dat_path,
270 output_population_path=args.output_population_path,
271 output_chunk_path=args.output_chunk_path,
272 output_cumulative_path=args.output_cumulative_path,
273 output_conditional_path=args.output_conditional_path,
274 output_chunk_conditional_path=args.output_chunk_conditional_path,
275 properties=properties)
278if __name__ == '__main__':
279 main()