Coverage for biobb_chemistry / acpype / acpype_params_gmx.py: 93%
60 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 12:49 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 12:49 +0000
1#!/usr/bin/env python3
3"""Module containing the AcpypeParamsGMX class and the command line interface."""
4from typing import Optional
5from biobb_common.generic.biobb_object import BiobbObject
6from biobb_common.tools.file_utils import launchlogger
7from biobb_chemistry.acpype.common import get_binary_path, check_input_path, check_output_path, get_basename, get_charge, create_unique_name, get_default_value, process_output_gmx
10class AcpypeParamsGMX(BiobbObject):
11 """
12 | biobb_chemistry AcpypeParamsGMX
13 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for generation of topologies for GROMACS.
14 | Generation of topologies for GROMACS. 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_path (str): Path to the input file. File type: input. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/data/acpype/acpype.params.mol2>`_. Accepted formats: pdb (edam:format_1476), mdl (edam:format_3815), mol2 (edam:format_3816).
18 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.gmx.gro>`_. Accepted formats: gro (edam:format_2033).
19 output_path_itp (str): Path to the ITP output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.gmx.itp>`_. Accepted formats: itp (edam:format_3883).
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.gmx.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 * **charge** (*int*) - (0) [-20~20|1] Net molecular charge, for gas default is 0. If None the charge is guessed by acpype.
24 * **binary_path** (*str*) - ("acpype") Path to the acpype 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*) - ('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_params_gmx import acpype_params_gmx
39 prop = {
40 'basename': 'BBB',
41 'charge': 0
42 }
43 acpype_params_gmx(input_path='/path/to/myStructure.mol2',
44 output_path_gro='/path/to/newGRO.gro',
45 output_path_itp='/path/to/newITP.itp',
46 output_path_top='/path/to/newTOP.top',
47 properties=prop)
49 Info:
50 * wrapped_software:
51 * name: Acpype
52 * version: 2019.10.05.12.26
53 * license: GNU
54 * ontology:
55 * name: EDAM
56 * schema: http://edamontology.org/EDAM.owl
58 """
60 def __init__(self, input_path, output_path_gro, output_path_itp, output_path_top,
61 properties=None, **kwargs) -> None:
62 properties = properties or {}
64 # Call parent class constructor
65 super().__init__(properties)
66 self.locals_var_dict = locals().copy()
68 # Input/Output files
69 self.io_dict = {
70 "in": {"input_path": input_path},
71 "out": {"output_path_gro": output_path_gro, "output_path_itp": output_path_itp, "output_path_top": output_path_top}
72 }
74 # Properties specific for BB
75 self.basename = properties.get('basename', 'BBB')
76 self.charge = properties.get('charge', '')
77 self.binary_path = get_binary_path(properties, 'binary_path')
78 self.properties = properties
80 # Check the properties
81 self.check_properties(properties)
82 self.check_arguments()
84 def check_data_params(self, out_log, err_log):
85 """ Checks all the input/output paths and parameters """
86 self.io_dict["in"]["input_path"] = check_input_path(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__)
87 self.io_dict["out"]["output_path_gro"] = check_output_path(self.io_dict["out"]["output_path_gro"], 'gro', out_log, self.__class__.__name__)
88 self.io_dict["out"]["output_path_itp"] = check_output_path(self.io_dict["out"]["output_path_itp"], 'itp', out_log, self.__class__.__name__)
89 self.io_dict["out"]["output_path_top"] = check_output_path(self.io_dict["out"]["output_path_top"], 'top', out_log, self.__class__.__name__)
90 self.output_files = {
91 'gro': self.io_dict["out"]["output_path_gro"],
92 'itp': self.io_dict["out"]["output_path_itp"],
93 'top': self.io_dict["out"]["output_path_top"],
94 }
96 def create_cmd(self, container_io_dict, out_log, err_log):
97 """Creates the command line instruction using the properties file settings"""
98 instructions_list = []
100 # generating output path
101 if self.container_path:
102 out_pth = self.container_volume_path + '/' + get_basename(self.basename, out_log) + '.' + self.unique_name
103 else:
104 out_pth = get_basename(self.basename, out_log) + '.' + self.unique_name
106 # executable path
107 instructions_list.append(self.binary_path)
109 # generating input
110 ipath = '-i ' + container_io_dict["in"]["input_path"]
111 instructions_list.append(ipath)
113 # generating output
114 basename = '-b ' + out_pth
115 instructions_list.append(basename)
117 # adding charge if not None
118 charge = get_charge(self.charge, out_log)
119 if charge:
120 charge = '-n ' + charge
121 instructions_list.append(charge)
123 return instructions_list
125 @launchlogger
126 def launch(self) -> int:
127 """Execute the :class:`AcpypeParamsGMX <acpype.acpype_params_gmx.AcpypeParamsGMX>` acpype.acpype_params_gmx.AcpypeParamsGMX object."""
129 # check input/output paths and parameters
130 self.check_data_params(self.out_log, self.err_log)
132 # Setup Biobb
133 if self.check_restart():
134 return 0
135 self.stage_files()
137 # create unique name for temporary folder (created by acpype)
138 self.unique_name = create_unique_name(6)
140 # create command line instruction
141 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log)
143 # Run Biobb block
144 self.run_biobb()
146 # Copy files to host
147 self.copy_to_host()
149 # move files to output_path and removes temporary folder
150 if self.container_path:
151 process_output_gmx(self.unique_name,
152 # self.stage_io_dict['unique_dir'],
153 self.remove_tmp,
154 self.basename,
155 get_default_value(self.__class__.__name__),
156 self.output_files, self.out_log)
157 else:
158 self.tmp_files.append(self.basename + "." + self.unique_name + ".acpype")
159 process_output_gmx(self.unique_name,
160 self.basename + "." + self.unique_name + ".acpype",
161 self.remove_tmp,
162 self.basename,
163 get_default_value(self.__class__.__name__),
164 self.output_files, self.out_log)
166 self.remove_tmp_files()
167 self.check_arguments(output_files_created=True, raise_exception=False)
169 return self.return_code
172def acpype_params_gmx(input_path: str, output_path_gro: str, output_path_itp: str, output_path_top: str, properties: Optional[dict] = None, **kwargs) -> int:
173 """Create the :class:`AcpypeParamsGMX <acpype.acpype_params_gmx.AcpypeParamsGMX>` class and
174 execute the :meth:`launch() <acpype.acpype_params_gmx.AcpypeParamsGMX.launch>` method."""
175 return AcpypeParamsGMX(**dict(locals())).launch()
178acpype_params_gmx.__doc__ = AcpypeParamsGMX.__doc__
179main = AcpypeParamsGMX.get_main(acpype_params_gmx, "Small molecule parameterization for GROMACS MD package.")
181if __name__ == '__main__':
182 main()