Coverage for biobb_amber / parmed / parmed_hmassrepartition.py: 90%
49 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 ParmedHMassRepartition class and the command line interface."""
4from typing import Optional
5from pathlib import PurePath
6from biobb_common.generic.biobb_object import BiobbObject
7from biobb_common.tools import file_utils as fu
8from biobb_common.tools.file_utils import launchlogger
9from biobb_amber.parmed.common import check_input_path, check_output_path
12class ParmedHMassRepartition(BiobbObject):
13 """
14 | biobb_amber ParmedHMassRepartition
15 | Wrapper of the `AmberTools (AMBER MD Package) parmed tool <https://ambermd.org/AmberTools.php>`_ module.
16 | Performs a Hydrogen Mass Repartition from an AMBER topology file using parmed tool from the AmberTools MD package.
18 Args:
19 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).
20 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).
21 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
22 * **binary_path** (*str*) - ("parmed") Path to the parmed executable binary.
23 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
24 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
25 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
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 tmp_folder = None
99 else:
100 tmp_folder = fu.create_unique_dir()
101 instructions_file = str(PurePath(tmp_folder).joinpath("parmed.in"))
102 fu.log('Creating %s temporary folder' % 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([str(tmp_folder)])
123 self.remove_tmp_files()
125 self.check_arguments(output_files_created=True, raise_exception=False)
127 return self.return_code
130def parmed_hmassrepartition(input_top_path: str,
131 output_top_path: Optional[str] = None,
132 properties: Optional[dict] = None, **kwargs) -> int:
133 """Create :class:`ParmedHMassRepartition <parmed.parmed_hmassrepartition.ParmedHMassRepartition>`parmed.parmed_hmassrepartition.ParmedHMassRepartition class and
134 execute :meth:`launch() <parmed.parmed_hmassrepartition.ParmedHMassRepartition.launch>` method"""
135 return ParmedHMassRepartition(**dict(locals())).launch()
138parmed_hmassrepartition.__doc__ = ParmedHMassRepartition.__doc__
139main = ParmedHMassRepartition.get_main(parmed_hmassrepartition, 'Performs a Hydrogen Mass Repartition from an AMBER topology file using parmed tool from the AmberTools MD package.')
141if __name__ == '__main__':
142 main()