Coverage for biobb_chemistry / acpype / acpype_params_gmx_opls.py: 92%
61 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-13 15:13 +0000
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-13 15:13 +0000
1#!/usr/bin/env python3
3"""Module containing the AcpypeParamsGMXOPLS 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
8import os
11class AcpypeParamsGMXOPLS(BiobbObject):
12 """
13 | biobb_chemistry AcpypeParamsGMXOPLS
14 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for generation of topologies for OPLS/AA.
15 | Generation of topologies for OPLS/AA. 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>`_.
17 Args:
18 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).
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.opls.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.opls.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_opls import acpype_params_gmx_opls
39 prop = {
40 'basename': 'BBB',
41 'charge': 0
42 }
43 acpype_params_gmx_opls(input_path='/path/to/myStructure.mol2',
44 output_path_itp='/path/to/newITP.itp',
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_path, output_path_itp, 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_path": input_path},
70 "out": {"output_path_itp": output_path_itp, "output_path_top": output_path_top}
71 }
73 # Properties specific for BB
74 self.basename = properties.get('basename', 'BBB')
75 self.charge = properties.get('charge', '')
76 self.binary_path = get_binary_path(properties, 'binary_path')
77 self.properties = properties
79 # Check the properties
80 self.check_properties(properties)
81 self.check_arguments()
83 def check_data_params(self, out_log, err_log):
84 """ Checks all the input/output paths and parameters """
85 self.io_dict["in"]["input_path"] = check_input_path(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__)
86 self.io_dict["out"]["output_path_itp"] = check_output_path(self.io_dict["out"]["output_path_itp"], 'itp', out_log, self.__class__.__name__)
87 self.io_dict["out"]["output_path_top"] = check_output_path(self.io_dict["out"]["output_path_top"], 'top', out_log, self.__class__.__name__)
88 self.output_files = {
89 'itp': self.io_dict["out"]["output_path_itp"],
90 'top': self.io_dict["out"]["output_path_top"],
91 }
93 def create_cmd(self, container_io_dict, out_log, err_log):
94 """Creates the command line instruction using the properties file settings"""
95 instructions_list = []
97 # generating output path
98 if self.container_path:
99 self.container_working_dir = self.container_volume_path
100 out_pth = get_basename(self.basename, out_log) + '.' + self.unique_name
101 else:
102 out_pth = get_basename(self.basename, out_log) + '.' + self.unique_name
104 # executable path
105 instructions_list.append(self.binary_path)
107 # generating input
108 ipath = '-i ' + container_io_dict["in"]["input_path"]
109 instructions_list.append(ipath)
111 # generating output
112 basename = '-b ' + out_pth
113 instructions_list.append(basename)
115 # adding charge if not none
116 charge = get_charge(self.charge, out_log)
117 if charge:
118 charge = '-n ' + charge
119 instructions_list.append(charge)
121 return instructions_list
123 @launchlogger
124 def launch(self) -> int:
125 """Execute the :class:`AcpypeParamsGMXOPLS <acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS>` acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS object."""
127 # check input/output paths and parameters
128 self.check_data_params(self.out_log, self.err_log)
130 # Setup Biobb
131 if self.check_restart():
132 return 0
133 self.stage_files()
135 # create unique name for temporary folder (created by acpype)
136 self.unique_name = create_unique_name(6)
138 # create command line instruction
139 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log)
141 # Run Biobb block
142 self.run_biobb()
144 # Copy files to host
145 self.copy_to_host()
147 # move files to output_path and removes temporary folder
148 if self.container_path:
149 process_output_gmx(self.unique_name,
150 os.path.join(self.stage_io_dict['unique_dir'], self.basename + "." + self.unique_name + ".amb2gmx"),
151 self.basename,
152 get_default_value(self.__class__.__name__),
153 self.output_files, self.out_log)
154 else:
155 self.tmp_files.append(self.basename + "." + self.unique_name + ".acpype")
156 process_output_gmx(self.unique_name,
157 self.basename + "." + self.unique_name + ".acpype",
158 self.basename,
159 get_default_value(self.__class__.__name__),
160 self.output_files, self.out_log)
162 self.remove_tmp_files()
163 self.check_arguments(output_files_created=True, raise_exception=False)
165 return self.return_code
168def acpype_params_gmx_opls(input_path: str, output_path_itp: str, output_path_top: str, properties: Optional[dict] = None, **kwargs) -> int:
169 """Create the :class:`AcpypeParamsGMXOPLS <acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS>` class and
170 execute the :meth:`launch() <acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS.launch>` method."""
171 return AcpypeParamsGMXOPLS(**dict(locals())).launch()
174acpype_params_gmx_opls.__doc__ = AcpypeParamsGMXOPLS.__doc__
175main = AcpypeParamsGMXOPLS.get_main(acpype_params_gmx_opls, "Small molecule parameterization for OPLS/AA MD package.")
177if __name__ == '__main__':
178 main()