Coverage for biobb_chemistry / acpype / acpype_convert_amber_to_gmx.py: 93%
54 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-15 17:59 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-15 17:59 +0000
1#!/usr/bin/env python3
3"""Module containing the AcpypeConvertAMBERtoGMX class and the command line interface."""
4from biobb_common.generic.biobb_object import BiobbObject
5from biobb_common.tools.file_utils import launchlogger
6from biobb_chemistry.acpype.common import get_binary_path, check_output_path, get_basename, create_unique_name, get_default_value, process_output_gmx
7from typing import Optional
10class AcpypeConvertAMBERtoGMX(BiobbObject):
11 """
12 | biobb_chemistry AcpypeConvertAMBERtoGMX
13 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for the conversion of AMBER topologies to GROMACS.
14 | Acpype is a tool based in Python to use Antechamber to generate topologies for chemical compounds and to interface with others python applications like CCPN or ARIA. `Visit the official page <https://github.com/alanwilter/acpype>`_.
16 Args:
17 input_crd_path (str): Path to the input coordinates file (AMBER crd). File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_chemistry/master/biobb_chemistry/test/data/acpype/acpype.coords.inpcrd>`_. Accepted formats: inpcrd (edam:format_3878).
18 input_top_path (str): Path to the input topology file (AMBER ParmTop). File type: input. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/data/acpype/acpype.top.prmtop>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881).
19 output_path_gro (str): Path to the GRO output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.amber2gmx.gro>`_. Accepted formats: gro (edam:format_2033).
20 output_path_top (str): Path to the TOP output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.amber2gmx.top>`_. Accepted formats: top (edam:format_3880).
21 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
22 * **basename** (*str*) - ("BBB") A basename for the project (folder and output files).
23 * **binary_path** (*str*) - ("acpype") Path to the acpype 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*) - ('acpype/acpype:2022.7.21') 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_chemistry.acpype.acpype_convert_amber_to_gmx import acpype_convert_amber_to_gmx
37 prop = {
38 'basename': 'BBB',
39 }
40 acpype_convert_amber_to_gmx(input_crd_path='/path/to/myStructure.inpcrd',
41 input_top_path='/path/to/myStructure.prmtop',
42 output_path_gro='/path/to/newGRO.gro',
43 output_path_top='/path/to/newTOP.top',
44 properties=prop)
46 Info:
47 * wrapped_software:
48 * name: Acpype
49 * version: 2019.10.05.12.26
50 * license: GNU
51 * ontology:
52 * name: EDAM
53 * schema: http://edamontology.org/EDAM.owl
55 """
57 def __init__(self, input_crd_path, input_top_path, output_path_gro, output_path_top,
58 properties=None, **kwargs) -> None:
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_crd_path": input_crd_path, "input_top_path": input_top_path},
68 "out": {"output_path_gro": output_path_gro, "output_path_top": output_path_top}
69 }
71 # Properties specific for BB
72 self.basename = properties.get('basename', 'BBB')
73 self.binary_path = get_binary_path(properties, 'binary_path')
74 self.properties = properties
76 # Check the properties
77 self.check_properties(properties)
78 self.check_arguments()
80 def check_data_params(self, out_log, err_log):
81 """ Checks all the input/output paths and parameters """
82 # NOTE: missing check input paths
83 self.io_dict["out"]["output_path_gro"] = check_output_path(self.io_dict["out"]["output_path_gro"], 'gro', out_log, self.__class__.__name__)
84 self.io_dict["out"]["output_path_top"] = check_output_path(self.io_dict["out"]["output_path_top"], 'top', out_log, self.__class__.__name__)
85 self.output_files = {
86 'gro': self.io_dict["out"]["output_path_gro"],
87 'top': self.io_dict["out"]["output_path_top"],
88 }
90 def create_cmd(self, container_io_dict, out_log, err_log):
91 """Creates the command line instruction using the properties file settings"""
92 instructions_list = []
94 # generating output path
95 if self.container_path:
96 out_pth = self.container_volume_path + '/' + get_basename(self.basename, out_log) + '.' + self.unique_name
97 else:
98 out_pth = get_basename(self.basename, out_log) + '.' + self.unique_name
100 # executable path
101 instructions_list.append(self.binary_path)
103 # generating inputs
104 crdpath = '-x ' + container_io_dict["in"]["input_crd_path"]
105 instructions_list.append(crdpath)
106 prmtopath = '-p ' + container_io_dict["in"]["input_top_path"]
107 instructions_list.append(prmtopath)
109 # generating output
110 basename = '-b ' + out_pth
111 instructions_list.append(basename)
113 return instructions_list
115 @launchlogger
116 def launch(self) -> int:
117 """Execute the :class:`AcpypeConvertAMBERtoGMX <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX>` acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX object."""
119 # check input/output paths and parameters
120 self.check_data_params(self.out_log, self.err_log)
122 # Setup Biobb
123 if self.check_restart():
124 return 0
125 self.stage_files()
127 # create unique name for temporary folder (created by acpype)
128 self.unique_name = create_unique_name(6)
130 # create command line instruction
131 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log)
133 # Run Biobb block
134 self.run_biobb()
136 # Copy files to host
137 self.copy_to_host()
139 # move files to output_path and removes temporary folder
140 if self.container_path:
141 process_output_gmx(self.unique_name,
142 # self.stage_io_dict['unique_dir'],
143 self.remove_tmp,
144 self.basename,
145 get_default_value(self.__class__.__name__),
146 self.output_files, self.out_log)
147 else:
148 self.tmp_files.extend([self.basename + "." + self.unique_name + ".acpype"])
149 process_output_gmx(self.unique_name,
150 self.basename + "." + self.unique_name + ".amb2gmx",
151 self.remove_tmp,
152 self.basename,
153 get_default_value(self.__class__.__name__),
154 self.output_files, self.out_log)
156 self.check_arguments(output_files_created=True, raise_exception=False)
158 return self.return_code
161def acpype_convert_amber_to_gmx(input_crd_path: str, input_top_path: str, output_path_gro: str, output_path_top: str, properties: Optional[dict] = None, **kwargs) -> int:
162 """Create the :class:`AcpypeConvertAMBERtoGMX <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX>` class and
163 execute the :meth:`launch() <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX.launch>` method."""
164 return AcpypeConvertAMBERtoGMX(**dict(locals())).launch()
167acpype_convert_amber_to_gmx.__doc__ = AcpypeConvertAMBERtoGMX.__doc__
168main = AcpypeConvertAMBERtoGMX.get_main(acpype_convert_amber_to_gmx, "Small molecule parameterization for GROMACS MD package.")
170if __name__ == '__main__':
171 main()