Coverage for biobb_amber/parmed/parmed_hmassrepartition.py: 75%
60 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 ParmedHMassRepartition class and the command line interface."""
4import argparse
5from typing import Optional
6from pathlib import PurePath
7from biobb_common.generic.biobb_object import BiobbObject
8from biobb_common.configuration import settings
9from biobb_common.tools import file_utils as fu
10from biobb_common.tools.file_utils import launchlogger
11from biobb_amber.parmed.common import check_input_path, check_output_path
14class ParmedHMassRepartition(BiobbObject):
15 """
16 | biobb_amber ParmedHMassRepartition
17 | Wrapper of the `AmberTools (AMBER MD Package) parmed tool <https://ambermd.org/AmberTools.php>`_ module.
18 | Performs a Hydrogen Mass Repartition from an AMBER topology file using parmed tool from the AmberTools MD package.
20 Args:
21 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).
22 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).
23 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
24 * **binary_path** (*str*) - ("parmed") Path to the parmed executable binary.
25 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
26 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
27 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
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.parmed.parmed_hmassrepartition import parmed_hmassrepartition
39 parmed_hmassrepartition(input_top_path='/path/to/topology.top',
40 output_top_path='/path/to/newTopology.top')
42 Info:
43 * wrapped_software:
44 * name: AmberTools parmed
45 * version: >20.9
46 * license: LGPL 2.1
47 * ontology:
48 * name: EDAM
49 * schema: http://edamontology.org/EDAM.owl
51 """
53 def __init__(self, input_top_path, output_top_path, properties=None, **kwargs) -> None:
55 properties = properties or {}
57 # Call parent class constructor
58 super().__init__(properties)
59 self.locals_var_dict = locals().copy()
61 # Input/Output files
62 self.io_dict = {
63 'in': {'input_top_path': input_top_path},
64 'out': {'output_top_path': output_top_path}
65 }
67 # Properties specific for BB
68 self.properties = properties
69 self.binary_path = properties.get('binary_path', 'parmed')
71 # Check the properties
72 self.check_properties(properties)
73 self.check_arguments()
75 def check_data_params(self, out_log, err_log):
76 """ Checks input/output paths correctness """
78 # Check input(s)
79 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__)
81 # Check output(s)
82 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__)
84 @launchlogger
85 def launch(self):
86 """Launches the execution of the ParmedHMassRepartition module."""
88 # check input/output paths and parameters
89 self.check_data_params(self.out_log, self.err_log)
91 # Setup Biobb
92 if self.check_restart():
93 return 0
94 self.stage_files()
96 # Creating temporary folder & Parmed configuration (instructions) file
97 if self.container_path:
98 instructions_file = str(PurePath(self.stage_io_dict['unique_dir']).joinpath("parmed.in"))
99 instructions_file_path = str(PurePath(self.container_volume_path).joinpath("parmed.in"))
100 self.tmp_folder = None
101 else:
102 self.tmp_folder = fu.create_unique_dir()
103 instructions_file = str(PurePath(self.tmp_folder).joinpath("parmed.in"))
104 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log)
105 instructions_file_path = instructions_file
107 with open(instructions_file, 'w') as parmedin:
108 parmedin.write("hmassrepartition\n")
109 parmedin.write("outparm " + self.stage_io_dict['out']['output_top_path'] + "\n")
111 self.cmd = [self.binary_path,
112 '-p', self.stage_io_dict['in']['input_top_path'],
113 '-i', instructions_file_path,
114 '-O' # Overwrite output files
115 ]
117 # Run Biobb block
118 self.run_biobb()
120 # Copy files to host
121 self.copy_to_host()
123 # remove temporary folder(s)
124 self.tmp_files.extend([
125 # self.stage_io_dict.get("unique_dir", ""),
126 str(self.tmp_folder)
127 ])
128 self.remove_tmp_files()
130 self.check_arguments(output_files_created=True, raise_exception=False)
132 return self.return_code
135def parmed_hmassrepartition(input_top_path: str,
136 output_top_path: Optional[str] = None,
137 properties: Optional[dict] = None, **kwargs) -> int:
138 """Create :class:`ParmedHMassRepartition <parmed.parmed_hmassrepartition.ParmedHMassRepartition>`parmed.parmed_hmassrepartition.ParmedHMassRepartition class and
139 execute :meth:`launch() <parmed.parmed_hmassrepartition.ParmedHMassRepartition.launch>` method"""
141 return ParmedHMassRepartition(input_top_path=input_top_path,
142 output_top_path=output_top_path,
143 properties=properties).launch()
145 parmed_hmassrepartition.__doc__ = ParmedHMassRepartition.__doc__
148def main():
149 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))
150 parser.add_argument('--config', required=False, help='Configuration file')
152 # Specific args
153 required_args = parser.add_argument_group('required arguments')
154 required_args.add_argument('--input_top_path', required=True, help='Input AMBER topology file. Accepted formats: top, parmtop, prmtop.')
155 required_args.add_argument('--output_top_path', required=False, help='Output topology file (AMBER ParmTop). Accepted formats: top, parmtop, prmtop.')
157 args = parser.parse_args()
158 config = args.config if args.config else None
159 properties = settings.ConfReader(config=config).get_prop_dic()
161 # Specific call
162 parmed_hmassrepartition(input_top_path=args.input_top_path,
163 output_top_path=args.output_top_path,
164 properties=properties)
167if __name__ == '__main__':
168 main()