Coverage for biobb_chemistry/acpype/acpype_convert_amber_to_gmx.py: 76%
67 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 09:28 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 09:28 +0000
1#!/usr/bin/env python3
3"""Module containing the AcpypeConvertAMBERtoGMX 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.file_utils import launchlogger
8from biobb_chemistry.acpype.common import get_binary_path, check_output_path, get_basename, create_unique_name, get_default_value, process_output_gmx
9from typing import Optional
12class AcpypeConvertAMBERtoGMX(BiobbObject):
13 """
14 | biobb_chemistry AcpypeConvertAMBERtoGMX
15 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for the conversion of AMBER topologies to GROMACS.
16 | 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>`_.
18 Args:
19 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).
20 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).
21 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).
22 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).
23 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
24 * **basename** (*str*) - ("BBB") A basename for the project (folder and output files).
25 * **binary_path** (*str*) - ("acpype") Path to the acpype 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*) - ('acpype/acpype:2022.7.21') 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_chemistry.acpype.acpype_convert_amber_to_gmx import acpype_convert_amber_to_gmx
39 prop = {
40 'basename': 'BBB',
41 }
42 acpype_convert_amber_to_gmx(input_crd_path='/path/to/myStructure.inpcrd',
43 input_top_path='/path/to/myStructure.prmtop',
44 output_path_gro='/path/to/newGRO.gro',
45 output_path_top='/path/to/newTOP.top',
46 properties=prop)
48 Info:
49 * wrapped_software:
50 * name: Acpype
51 * version: 2019.10.05.12.26
52 * license: GNU
53 * ontology:
54 * name: EDAM
55 * schema: http://edamontology.org/EDAM.owl
57 """
59 def __init__(self, input_crd_path, input_top_path, output_path_gro, output_path_top,
60 properties=None, **kwargs) -> None:
61 properties = properties or {}
63 # Call parent class constructor
64 super().__init__(properties)
65 self.locals_var_dict = locals().copy()
67 # Input/Output files
68 self.io_dict = {
69 "in": {"input_crd_path": input_crd_path, "input_top_path": input_top_path},
70 "out": {"output_path_gro": output_path_gro, "output_path_top": output_path_top}
71 }
73 # Properties specific for BB
74 self.basename = properties.get('basename', 'BBB')
75 self.binary_path = get_binary_path(properties, 'binary_path')
76 self.properties = properties
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 all the input/output paths and parameters """
84 # NOTE: missing check input paths
85 self.io_dict["out"]["output_path_gro"] = check_output_path(self.io_dict["out"]["output_path_gro"], 'gro', out_log, self.__class__.__name__)
86 self.io_dict["out"]["output_path_top"] = check_output_path(self.io_dict["out"]["output_path_top"], 'top', out_log, self.__class__.__name__)
87 self.output_files = {
88 'gro': self.io_dict["out"]["output_path_gro"],
89 'top': self.io_dict["out"]["output_path_top"],
90 }
92 def create_cmd(self, container_io_dict, out_log, err_log):
93 """Creates the command line instruction using the properties file settings"""
94 instructions_list = []
96 # generating output path
97 if self.container_path:
98 out_pth = self.container_volume_path + '/' + get_basename(self.basename, out_log) + '.' + self.unique_name
99 else:
100 out_pth = get_basename(self.basename, out_log) + '.' + self.unique_name
102 # executable path
103 instructions_list.append(self.binary_path)
105 # generating inputs
106 crdpath = '-x ' + container_io_dict["in"]["input_crd_path"]
107 instructions_list.append(crdpath)
108 prmtopath = '-p ' + container_io_dict["in"]["input_top_path"]
109 instructions_list.append(prmtopath)
111 # generating output
112 basename = '-b ' + out_pth
113 instructions_list.append(basename)
115 return instructions_list
117 @launchlogger
118 def launch(self) -> int:
119 """Execute the :class:`AcpypeConvertAMBERtoGMX <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX>` acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX object."""
121 # check input/output paths and parameters
122 self.check_data_params(self.out_log, self.err_log)
124 # Setup Biobb
125 if self.check_restart():
126 return 0
127 self.stage_files()
129 # create unique name for temporary folder (created by acpype)
130 self.unique_name = create_unique_name(6)
132 # create command line instruction
133 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log)
135 # Run Biobb block
136 self.run_biobb()
138 # Copy files to host
139 self.copy_to_host()
141 # move files to output_path and removes temporary folder
142 if self.container_path:
143 process_output_gmx(self.unique_name,
144 # self.stage_io_dict['unique_dir'],
145 self.remove_tmp,
146 self.basename,
147 get_default_value(self.__class__.__name__),
148 self.output_files, self.out_log)
149 else:
150 self.tmp_files.extend([self.basename + "." + self.unique_name + ".acpype"])
151 process_output_gmx(self.unique_name,
152 self.basename + "." + self.unique_name + ".amb2gmx",
153 self.remove_tmp,
154 self.basename,
155 get_default_value(self.__class__.__name__),
156 self.output_files, self.out_log)
158 self.check_arguments(output_files_created=True, raise_exception=False)
160 return self.return_code
163def 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:
164 """Execute the :class:`AcpypeConvertAMBERtoGMX <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX>` class and
165 execute the :meth:`launch() <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX.launch>` method."""
167 return AcpypeConvertAMBERtoGMX(input_crd_path=input_crd_path,
168 input_top_path=input_top_path,
169 output_path_gro=output_path_gro,
170 output_path_top=output_path_top,
171 properties=properties, **kwargs).launch()
173 acpype_convert_amber_to_gmx.__doc__ = AcpypeConvertAMBERtoGMX.__doc__
176def main():
177 """Command line execution of this building block. Please check the command line documentation."""
178 parser = argparse.ArgumentParser(description="Small molecule parameterization for GROMACS MD package.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
179 parser.add_argument('--config', required=False, help='Configuration file')
181 # Specific args of each building block
182 required_args = parser.add_argument_group('required arguments')
183 required_args.add_argument('--input_crd_path', required=True, help='Path to the input coordinates file (AMBER crd). Accepted formats: inpcrd.')
184 required_args.add_argument('--input_top_path', required=True, help='Path to the input topology file (AMBER ParmTop). Accepted formats: top, parmtop, prmtop.')
185 required_args.add_argument('--output_path_gro', required=True, help='Path to the GRO output file. Accepted formats: gro.')
186 required_args.add_argument('--output_path_top', required=True, help='Path to the TOP output file. Accepted formats: top.')
188 args = parser.parse_args()
189 args.config = args.config or "{}"
190 properties = settings.ConfReader(config=args.config).get_prop_dic()
192 # Specific call of each building block
193 acpype_convert_amber_to_gmx(input_crd_path=args.input_crd_path,
194 input_top_path=args.input_top_path,
195 output_path_gro=args.output_path_gro,
196 output_path_top=args.output_path_top,
197 properties=properties)
200if __name__ == '__main__':
201 main()