Coverage for biobb_amber/pdb4amber/pdb4amber_run.py: 76%
58 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 Pdb4amber 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 import file_utils as fu
8from biobb_common.tools.file_utils import launchlogger
9from biobb_amber.pdb4amber.common import check_input_path, check_output_path
12class Pdb4amberRun(BiobbObject):
13 """
14 | biobb_amber.pdb4amber.pdb4amber_run Pdb4amberRun
15 | Wrapper of the `AmberTools (AMBER MD Package) pdb4amber tool <https://ambermd.org/AmberTools.php>`_ module.
16 | Analyse PDB files and clean them for further usage, especially with the LEaP programs of Amber, using pdb4amber tool from the AmberTools MD package.
18 Args:
19 input_pdb_path (str): Input 3D structure PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/pdb4amber/1aki_fixed.pdb>`_. Accepted formats: pdb (edam:format_1476).
20 output_pdb_path (str): Output 3D structure PDB file. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/pdb4amber/structure.pdb4amber.pdb>`_. Accepted formats: pdb (edam:format_1476).
21 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
22 * **remove_hydrogens** (*bool*) - (False) Remove hydrogen atoms from the PDB file.
23 * **remove_waters** (*bool*) - (False) Remove water molecules from the PDB file.
24 * **constant_pH** (*bool*) - (False) Rename ionizable residues e.g. GLU,ASP,HIS for constant pH simulation.
25 * **binary_path** (*str*) - ("pdb4amber") Path to the pdb4amber executable binary.
26 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
27 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
28 * **container_path** (*str*) - (None) Container path definition.
29 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition.
30 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
31 * **container_working_dir** (*str*) - (None) Container working directory definition.
32 * **container_user_id** (*str*) - (None) Container user_id definition.
33 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
35 Examples:
36 This is a use example of how to use the building block from Python::
38 from biobb_amber.pdb4amber.pdb4amber_run import pdb4amber_run
39 prop = {
40 'remove_tmp': True
41 }
42 pdb4amber_run(input_pdb_path='/path/to/structure.pdb',
43 output_pdb_path='/path/to/newStructure.pdb',
44 properties=prop)
46 Info:
47 * wrapped_software:
48 * name: AmberTools pdb4amber
49 * version: >20.9
50 * license: LGPL 2.1
51 * ontology:
52 * name: EDAM
53 * schema: http://edamontology.org/EDAM.owl
55 """
56 def __init__(self, input_pdb_path: str, output_pdb_path: str,
57 properties: dict, **kwargs):
59 properties = properties or {}
61 # Call parent class constructor
62 super().__init__(properties)
63 self.locals_var_dict = locals().copy()
65 # Input/Output files
66 self.io_dict = {
67 'in': {'input_pdb_path': input_pdb_path},
68 'out': {'output_pdb_path': output_pdb_path}
69 }
71 # Properties specific for BB
72 self.properties = properties
73 self.remove_hydrogens = properties.get('remove_hydrogens', False)
74 self.remove_waters = properties.get('remove_waters', False)
75 self.constant_pH = properties.get('constant_pH', False)
76 self.binary_path = properties.get('binary_path', 'pdb4amber')
78 # Check the properties
79 self.check_properties(properties)
80 self.check_arguments()
82 def check_data_params(self, out_log, err_log):
83 """ Checks input/output paths correctness """
85 # Check input(s)
86 self.io_dict["in"]["input_pdb_path"] = check_input_path(self.io_dict["in"]["input_pdb_path"], "input_pdb_path", False, out_log, self.__class__.__name__)
88 # Check output(s)
89 self.io_dict["out"]["output_pdb_path"] = check_output_path(self.io_dict["out"]["output_pdb_path"], "output_pdb_path", False, out_log, self.__class__.__name__)
91 @launchlogger
92 def launch(self):
93 """Launches the execution of the Pdb4amberRun module."""
95 # check input/output paths and parameters
96 self.check_data_params(self.out_log, self.err_log)
98 # Setup Biobb
99 if self.check_restart():
100 return 0
101 self.stage_files()
103 # Creating temporary folder
104 self.tmp_folder = fu.create_unique_dir()
105 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log)
107 # Command line
108 # sander -O -i mdin/min.mdin -p $1.cpH.prmtop -c ph$i/$1.inpcrd -r ph$i/$1.min.rst7 -o ph$i/$1.min.o
109 self.cmd = [self.binary_path,
110 '-i', self.stage_io_dict['in']['input_pdb_path'],
111 '-o', self.stage_io_dict['out']['output_pdb_path']
112 ]
114 if self.remove_hydrogens:
115 self.cmd.append("-y ")
116 if self.remove_waters:
117 self.cmd.append("-d ")
118 if self.constant_pH:
119 self.cmd.append("--constantph ")
121 # Run Biobb block
122 self.run_biobb()
124 # Copy files to host
125 self.copy_to_host()
127 # remove temporary folder(s)
128 self.tmp_files.extend([
129 self.stage_io_dict.get("unique_dir"),
130 self.tmp_folder
131 ])
132 self.remove_tmp_files()
134 self.check_arguments(output_files_created=True, raise_exception=False)
136 return self.return_code
139def pdb4amber_run(input_pdb_path: str, output_pdb_path: str,
140 properties: dict = None, **kwargs) -> int:
141 """Create :class:`Pdb4amberRun <pdb4amber.pdb4amber_run.Pdb4amberRun>`pdb4amber.pdb4amber_run.Pdb4amberRun class and
142 execute :meth:`launch() <pdb4amber.pdb4amber_run.Pdb4amberRun.launch>` method"""
144 return Pdb4amberRun(input_pdb_path=input_pdb_path,
145 output_pdb_path=output_pdb_path,
146 properties=properties).launch()
149def main():
150 parser = argparse.ArgumentParser(description='Analyse PDB files and clean them for further usage, especially with the LEaP programs of Amber, using pdb4amber tool from the AmberTools MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
151 parser.add_argument('--config', required=False, help='Configuration file')
153 # Specific args
154 required_args = parser.add_argument_group('required arguments')
155 required_args.add_argument('--input_pdb_path', required=True, help='Input 3D structure PDB file. Accepted formats: pdb.')
156 required_args.add_argument('--output_pdb_path', required=True, help='Output 3D structure PDB file. Accepted formats: pdb.')
158 args = parser.parse_args()
159 config = args.config if args.config else None
160 properties = settings.ConfReader(config=config).get_prop_dic()
162 # Specific call
163 pdb4amber_run(input_pdb_path=args.input_pdb_path,
164 output_pdb_path=args.output_pdb_path,
165 properties=properties)
168if __name__ == '__main__':
169 main()