Coverage for biobb_amber/parmed/parmed_hmassrepartition.py: 76%
59 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 ParmedHMassRepartition class and the command line interface."""
4import argparse
5from pathlib import PurePath
6from biobb_common.generic.biobb_object import BiobbObject
7from biobb_common.configuration import settings
8from biobb_common.tools import file_utils as fu
9from biobb_common.tools.file_utils import launchlogger
10from biobb_amber.parmed.common import check_input_path, check_output_path
13class ParmedHMassRepartition(BiobbObject):
14 """
15 | biobb_amber ParmedHMassRepartition
16 | Wrapper of the `AmberTools (AMBER MD Package) parmed tool <https://ambermd.org/AmberTools.php>`_ module.
17 | Performs a Hydrogen Mass Repartition from an AMBER topology file using parmed tool from the AmberTools MD package.
19 Args:
20 input_top_path (str): Input AMBER topology file. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/parmed/input.hmass.prmtop>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881).
21 output_top_path (str): Output topology file (AMBER ParmTop). File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/parmed/output.hmass.prmtop>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881).
22 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
23 * **binary_path** (*str*) - ("parmed") Path to the parmed executable binary.
24 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
25 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
26 * **container_path** (*str*) - (None) Container path definition.
27 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition.
28 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
29 * **container_working_dir** (*str*) - (None) Container working directory definition.
30 * **container_user_id** (*str*) - (None) Container user_id definition.
31 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
33 Examples:
34 This is a use example of how to use the building block from Python::
36 from biobb_amber.parmed.parmed_hmassrepartition import parmed_hmassrepartition
37 parmed_hmassrepartition(input_top_path='/path/to/topology.top',
38 output_top_path='/path/to/newTopology.top')
40 Info:
41 * wrapped_software:
42 * name: AmberTools parmed
43 * version: >20.9
44 * license: LGPL 2.1
45 * ontology:
46 * name: EDAM
47 * schema: http://edamontology.org/EDAM.owl
49 """
51 def __init__(self, input_top_path, output_top_path, properties=None, **kwargs) -> None:
53 properties = properties or {}
55 # Call parent class constructor
56 super().__init__(properties)
57 self.locals_var_dict = locals().copy()
59 # Input/Output files
60 self.io_dict = {
61 'in': {'input_top_path': input_top_path},
62 'out': {'output_top_path': output_top_path}
63 }
65 # Properties specific for BB
66 self.properties = properties
67 self.binary_path = properties.get('binary_path', 'parmed')
69 # Check the properties
70 self.check_properties(properties)
71 self.check_arguments()
73 def check_data_params(self, out_log, err_log):
74 """ Checks input/output paths correctness """
76 # Check input(s)
77 self.io_dict["in"]["input_top_path"] = check_input_path(self.io_dict["in"]["input_top_path"], "input_top_path", False, out_log, self.__class__.__name__)
79 # Check output(s)
80 self.io_dict["out"]["output_top_path"] = check_output_path(self.io_dict["out"]["output_top_path"], "output_top_path", False, out_log, self.__class__.__name__)
82 @launchlogger
83 def launch(self):
84 """Launches the execution of the ParmedHMassRepartition module."""
86 # check input/output paths and parameters
87 self.check_data_params(self.out_log, self.err_log)
89 # Setup Biobb
90 if self.check_restart():
91 return 0
92 self.stage_files()
94 # Creating temporary folder & Parmed configuration (instructions) file
95 if self.container_path:
96 instructions_file = str(PurePath(self.stage_io_dict['unique_dir']).joinpath("parmed.in"))
97 instructions_file_path = str(PurePath(self.container_volume_path).joinpath("parmed.in"))
98 self.tmp_folder = None
99 else:
100 self.tmp_folder = fu.create_unique_dir()
101 instructions_file = str(PurePath(self.tmp_folder).joinpath("parmed.in"))
102 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log)
103 instructions_file_path = instructions_file
105 with open(instructions_file, 'w') as parmedin:
106 parmedin.write("hmassrepartition\n")
107 parmedin.write("outparm " + self.stage_io_dict['out']['output_top_path'] + "\n")
109 self.cmd = [self.binary_path,
110 '-p', self.stage_io_dict['in']['input_top_path'],
111 '-i', instructions_file_path,
112 '-O' # Overwrite output files
113 ]
115 # Run Biobb block
116 self.run_biobb()
118 # Copy files to host
119 self.copy_to_host()
121 # remove temporary folder(s)
122 self.tmp_files.extend([
123 self.stage_io_dict.get("unique_dir"),
124 self.tmp_folder
125 ])
126 self.remove_tmp_files()
128 self.check_arguments(output_files_created=True, raise_exception=False)
130 return self.return_code
133def parmed_hmassrepartition(input_top_path: str,
134 output_top_path: str = None,
135 properties: dict = None, **kwargs) -> int:
136 """Create :class:`ParmedHMassRepartition <parmed.parmed_hmassrepartition.ParmedHMassRepartition>`parmed.parmed_hmassrepartition.ParmedHMassRepartition class and
137 execute :meth:`launch() <parmed.parmed_hmassrepartition.ParmedHMassRepartition.launch>` method"""
139 return ParmedHMassRepartition(input_top_path=input_top_path,
140 output_top_path=output_top_path,
141 properties=properties).launch()
144def main():
145 parser = argparse.ArgumentParser(description='Performs a Hydrogen Mass Repartition from an AMBER topology file using parmed tool from the AmberTools MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
146 parser.add_argument('--config', required=False, help='Configuration file')
148 # Specific args
149 required_args = parser.add_argument_group('required arguments')
150 required_args.add_argument('--input_top_path', required=True, help='Input AMBER topology file. Accepted formats: top, parmtop, prmtop.')
151 required_args.add_argument('--output_top_path', required=False, help='Output topology file (AMBER ParmTop). Accepted formats: top, parmtop, prmtop.')
153 args = parser.parse_args()
154 config = args.config if args.config else None
155 properties = settings.ConfReader(config=config).get_prop_dic()
157 # Specific call
158 parmed_hmassrepartition(input_top_path=args.input_top_path,
159 output_top_path=args.output_top_path,
160 properties=properties)
163if __name__ == '__main__':
164 main()