Coverage for biobb_amber / cphstats / cestats_run.py: 74%
94 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 10:27 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 10:27 +0000
1#!/usr/bin/env python3
3"""Module containing the Cestats class and the command line interface."""
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
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.
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 * **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.
49 Examples:
50 This is a use example of how to use the building block from Python::
52 from biobb_amber.cestats.cestats_run import cestats_run
53 prop = {
54 'timestep' : 0.002
55 }
56 cestats_run(input_cein_path='/path/to/cein.cein',
57 input_ceout_path='/path/to/ceout.cpout',
58 output_dat_path='/path/to/cestats.dat',
59 properties=prop)
61 Info:
62 * wrapped_software:
63 * name: AMBER cestats
64 * version: >=1.5
65 * license: other
66 * ontology:
67 * name: EDAM
68 * schema: http://edamontology.org/EDAM.owl
70 """
72 def __init__(self, input_cein_path: str, input_ceout_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:
76 properties = properties or {}
78 # Call parent class constructor
79 super().__init__(properties)
80 self.locals_var_dict = locals().copy()
82 # Input/Output files
83 self.io_dict = {
84 'in': {'input_cein_path': input_cein_path,
85 'input_ceout_path': input_ceout_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 }
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.reduced = properties.get('reduced', True)
100 self.eos = properties.get('eos', False)
101 self.calceo = properties.get('calceo', 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', 'cestats')
109 # Check the properties
110 self.check_properties(properties)
111 self.check_arguments()
113 def check_data_params(self, out_log, err_log):
114 """ Checks input/output paths correctness """
116 # Check input(s)
117 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__)
118 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__)
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__)
128 @launchlogger
129 def launch(self):
130 """Launches the execution of the CestatsRun module."""
132 # check input/output paths and parameters
133 self.check_data_params(self.out_log, self.err_log)
135 # Setup Biobb
136 if self.check_restart():
137 return 0
138 self.stage_files()
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_cein_path'],
145 '-o', self.stage_io_dict['out']['output_dat_path'],
146 self.stage_io_dict['in']['input_ceout_path']
147 ]
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'])
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))
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'])
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'])
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'])
172 if self.verbose:
173 self.cmd.append('-v 1')
175 if self.interval:
176 self.cmd.append('-n')
177 self.cmd.append(str(self.interval))
179 if self.reduced:
180 self.cmd.append('-p')
181 else:
182 self.cmd.append('-d')
184 if self.eos:
185 self.cmd.append('-a')
187 if self.calceo:
188 self.cmd.append('--calceo')
189 else:
190 self.cmd.append('--no-calceo')
192 if self.running_avg_window:
193 self.cmd.append('-r')
194 self.cmd.append(str(self.running_avg_window))
196 if self.cumulative:
197 self.cmd.append('--cumulative')
199 if self.fix_remd:
200 self.cmd.append('--fix-remd')
201 self.cmd.append(str(self.fix_remd))
203 if self.conditional:
204 self.cmd.append('-c')
205 self.cmd.append(str(self.conditional))
207 # Run Biobb block
208 self.run_biobb()
210 # Copy files to host
211 self.copy_to_host()
213 # Remove temporary file(s)
214 self.remove_tmp_files()
216 self.check_arguments(output_files_created=True, raise_exception=False)
218 return self.return_code
221def cestats_run(input_cein_path: str, input_ceout_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:`CestatsRun <cphstats.cestats_run.CestatsRun>` class and
228 execute the :meth:`launch() <cphstats.cestats_run.CestatsRun.launch>` method."""
229 return CestatsRun(**dict(locals())).launch()
232cestats_run.__doc__ = CestatsRun.__doc__
233main = CestatsRun.get_main(cestats_run, "Analyzing the results of constant Redox potential MD simulations using cestats tool from the AMBER MD package.")
235if __name__ == '__main__':
236 main()