Coverage for biobb_chemistry / acpype / acpype_params_gmx_opls.py: 93%
59 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 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
10class AcpypeParamsGMXOPLS(BiobbObject):
11 """
12 | biobb_chemistry AcpypeParamsGMXOPLS
13 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for generation of topologies for OPLS/AA.
14 | 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>`_.
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_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).
19 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).
20 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
21 * **basename** (*str*) - ("BBB") A basename for the project (folder and output files).
22 * **charge** (*int*) - (0) [-20~20|1] Net molecular charge, for gas default is 0. If None the charge is guessed by acpype.
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 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
27 * **container_path** (*str*) - (None) Container path definition.
28 * **container_image** (*str*) - ('acpype/acpype:2022.7.21') Container image definition.
29 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
30 * **container_working_dir** (*str*) - (None) Container working directory definition.
31 * **container_user_id** (*str*) - (None) Container user_id definition.
32 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
34 Examples:
35 This is a use example of how to use the building block from Python::
37 from biobb_chemistry.acpype.acpype_params_gmx_opls import acpype_params_gmx_opls
38 prop = {
39 'basename': 'BBB',
40 'charge': 0
41 }
42 acpype_params_gmx_opls(input_path='/path/to/myStructure.mol2',
43 output_path_itp='/path/to/newITP.itp',
44 output_path_top='/path/to/newTOP.top',
45 properties=prop)
47 Info:
48 * wrapped_software:
49 * name: Acpype
50 * version: 2019.10.05.12.26
51 * license: GNU
52 * ontology:
53 * name: EDAM
54 * schema: http://edamontology.org/EDAM.owl
56 """
58 def __init__(self, input_path, output_path_itp, output_path_top,
59 properties=None, **kwargs) -> None:
60 properties = properties or {}
62 # Call parent class constructor
63 super().__init__(properties)
64 self.locals_var_dict = locals().copy()
66 # Input/Output files
67 self.io_dict = {
68 "in": {"input_path": input_path},
69 "out": {"output_path_itp": output_path_itp, "output_path_top": output_path_top}
70 }
72 # Properties specific for BB
73 self.basename = properties.get('basename', 'BBB')
74 self.charge = properties.get('charge', '')
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 self.io_dict["in"]["input_path"] = check_input_path(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__)
85 self.io_dict["out"]["output_path_itp"] = check_output_path(self.io_dict["out"]["output_path_itp"], 'itp', 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 'itp': self.io_dict["out"]["output_path_itp"],
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 input
106 ipath = '-i ' + container_io_dict["in"]["input_path"]
107 instructions_list.append(ipath)
109 # generating output
110 basename = '-b ' + out_pth
111 instructions_list.append(basename)
113 # adding charge if not none
114 charge = get_charge(self.charge, out_log)
115 if charge:
116 charge = '-n ' + charge
117 instructions_list.append(charge)
119 return instructions_list
121 @launchlogger
122 def launch(self) -> int:
123 """Execute the :class:`AcpypeParamsGMXOPLS <acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS>` acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS object."""
125 # check input/output paths and parameters
126 self.check_data_params(self.out_log, self.err_log)
128 # Setup Biobb
129 if self.check_restart():
130 return 0
131 self.stage_files()
133 # create unique name for temporary folder (created by acpype)
134 self.unique_name = create_unique_name(6)
136 # create command line instruction
137 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log)
139 # Run Biobb block
140 self.run_biobb()
142 # Copy files to host
143 self.copy_to_host()
145 # move files to output_path and removes temporary folder
146 if self.container_path:
147 process_output_gmx(self.unique_name,
148 # self.stage_io_dict['unique_dir'],
149 self.remove_tmp,
150 self.basename,
151 get_default_value(self.__class__.__name__),
152 self.output_files, self.out_log)
153 else:
154 self.tmp_files.append(self.basename + "." + self.unique_name + ".acpype")
155 process_output_gmx(self.unique_name,
156 self.basename + "." + self.unique_name + ".acpype",
157 self.remove_tmp,
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()