Coverage for biobb_amber/cphstats/cphstats_run.py: 65%
111 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 08:11 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 08:11 +0000
1#!/usr/bin/env python3
3"""Module containing the Cphstats 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
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.
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 * **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.
48 Examples:
49 This is a use example of how to use the building block from Python::
51 from biobb_amber.cphstats.cphstats_run import cphstats_run
52 prop = {
53 'timestep' : 0.002
54 }
55 cphstats_run(input_cpin_path='/path/to/cpin.cpin',
56 input_cpout_path='/path/to/cpout.cpout',
57 output_dat_path='/path/to/cphstats.dat',
58 properties=prop)
60 Info:
61 * wrapped_software:
62 * name: AMBER cphstats
63 * version: >=1.5
64 * license: other
65 * ontology:
66 * name: EDAM
67 * schema: http://edamontology.org/EDAM.owl
69 """
70 def __init__(self, input_cpin_path: str, input_cpout_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:
74 properties = properties or {}
76 # Call parent class constructor
77 super().__init__(properties)
78 self.locals_var_dict = locals().copy()
80 # Input/Output files
81 self.io_dict = {
82 'in': {'input_cpin_path': input_cpin_path,
83 'input_cpout_path': input_cpout_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 }
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.protonated = properties.get('protonated', True)
98 self.pka = properties.get('pka', False)
99 self.calcpka = properties.get('calcpka', 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', 'cphstats')
107 # Check the properties
108 self.check_properties(properties)
109 self.check_arguments()
111 def check_data_params(self, out_log, err_log):
112 """ Checks input/output paths correctness """
114 # Check input(s)
115 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__)
116 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__)
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__)
126 @launchlogger
127 def launch(self):
128 """Launches the execution of the CphstatsRun module."""
130 # check input/output paths and parameters
131 self.check_data_params(self.out_log, self.err_log)
133 # Setup Biobb
134 if self.check_restart():
135 return 0
136 self.stage_files()
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_cpin_path'],
143 '-o', self.stage_io_dict['out']['output_dat_path'],
144 self.stage_io_dict['in']['input_cpout_path']
145 ]
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'])
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))
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'])
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'])
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'])
170 if self.verbose:
171 self.cmd.append('-v 1')
173 if self.interval:
174 self.cmd.append('-n')
175 self.cmd.append(str(self.interval))
177 if self.protonated:
178 self.cmd.append('-p')
179 else:
180 self.cmd.append('-d')
182 if self.pka:
183 self.cmd.append('-a')
185 if self.calcpka:
186 self.cmd.append('--calcpka')
187 else:
188 self.cmd.append('--no-calcpka')
190 if self.running_avg_window:
191 self.cmd.append('-r')
192 self.cmd.append(str(self.running_avg_window))
194 if self.cumulative:
195 self.cmd.append('--cumulative')
197 if self.fix_remd:
198 self.cmd.append('--fix-remd')
199 self.cmd.append(str(self.fix_remd))
201 if self.conditional:
202 self.cmd.append('-c')
203 self.cmd.append(str(self.conditional))
205 # Run Biobb block
206 self.run_biobb()
208 # Copy files to host
209 self.copy_to_host()
211 # Remove temporary file(s)
212 self.tmp_files.extend([
213 self.stage_io_dict.get("unique_dir")
214 ])
215 self.remove_tmp_files()
217 self.check_arguments(output_files_created=True, raise_exception=False)
219 return self.return_code
222def cphstats_run(input_cpin_path: str, input_cpout_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:`CphstatsRun <cphstats.chpstats_run.CphstatsRun>`cphstats.chpstats_run.CphstatsRun class and
229 execute :meth:`launch() <cphstats.chpstats_run.CphstatsRun.launch>` method"""
231 return CphstatsRun(input_cpin_path=input_cpin_path,
232 input_cpout_path=input_cpout_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()
242def main():
243 parser = argparse.ArgumentParser(description='Analyzing the results of constant pH MD simulations using cphstats 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')
246 # Specific args
247 required_args = parser.add_argument_group('required arguments')
248 required_args.add_argument('--input_cpin_path', required=True, help='Input constant pH file (AMBER cpin). Accepted formats: cpin.')
249 required_args.add_argument('--input_cpout_path', required=True, help='Output constant pH file (AMBER cpout). Accepted formats: cpout.')
250 required_args.add_argument('--output_dat_path', required=True, help='Output file to which the standard calcpka-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.')
257 args = parser.parse_args()
258 config = args.config if args.config else None
259 properties = settings.ConfReader(config=config).get_prop_dic()
261 # Specific call
262 cphstats_run(input_cpin_path=args.input_cpin_path,
263 input_cpout_path=args.input_cpout_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)
273if __name__ == '__main__':
274 main()