Coverage for biobb_chemistry/babelm/babel_minimize.py: 35%
34 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-02 09:19 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-02 09:19 +0000
1#!/usr/bin/env python3
3"""Module containing the BabelMinimize class and the command line interface."""
4from typing import Optional
5from pathlib import PurePath
6from biobb_common.generic.biobb_object import BiobbObject, launcher
7from biobb_common.tools import file_utils as fu
8from biobb_chemistry.babelm.common import MINIMIZE_OPTIONS, is_valid_minimize_value
11class BabelMinimize(BiobbObject):
12 """
13 | biobb_chemistry BabelMinimize
14 | This class is a wrapper of the Open Babel tool.
15 | Energetically minimizes small molecules. Open Babel is a chemical toolbox designed to speak the many languages of chemical data. It's an open, collaborative project allowing anyone to search, convert, analyze, or store data from molecular modeling, chemistry, solid-state materials, biochemistry, or related areas. `Visit the official page <http://openbabel.org/wiki/Main_Page>`_.
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/babelm/babel.minimize.pdb>`_. Accepted formats: pdb (edam:format_1476), mol2 (edam:format_3816).
19 output_path (str): Path to the output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/babelm/ref_babel.minimize.pdb>`_. Accepted formats: pdb (edam:format_1476), mol2 (edam:format_3816).
20 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
21 * **criteria** (*float*) - (None) Convergence criteria. If not set, obminimize uses its own default.
22 * **method** (*str*) - (None) Method. If not set, obminimize uses its own default. Values: cg (conjugate gradients algorithm), sd (steepest descent algorithm).
23 * **force_field** (*str*) - (None) Force field. If not set, obminimize uses its own default. Values: GAFF (General Amber Force Field), Ghemical (Ghemical force field), MMFF94 (MMFF94 force field), MMFF94s (MMFF94s force field), UFF (Universal Force Field).
24 * **hydrogens** (*bool*) - (None) Add hydrogen atoms. If not set, obminimize uses its own default.
25 * **steps** (*int*) - (None) [0~5000|1] Maximum number of steps. If not set, obminimize uses its own default.
26 * **cutoff** (*bool*) - (None) Use cut-off. If not set, obminimize uses its own default.
27 * **rvdw** (*float*) - (None) [0~50|1.0] VDW cut-off distance. If not set, obminimize uses its own default.
28 * **rele** (*float*) - (None) [0~50|1.0] Electrostatic cut-off distance. If not set, obminimize uses its own default.
29 * **frequency** (*int*) - (None) [0~50|1] Frequency to update the non-bonded pairs. If not set, obminimize uses its own default.
30 * **binary_path** (*str*) - ("obminimize") Path to the obminimize executable binary.
31 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
32 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
33 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
34 * **container_path** (*str*) - (None) Container path definition.
35 * **container_image** (*str*) - ('informaticsmatters/obabel:latest') Container image definition.
36 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition.
37 * **container_user_id** (*str*) - (None) Container user_id definition.
38 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container.
40 Examples:
41 This is a use example of how to use the building block from Python::
43 from biobb_chemistry.babelm.babel_minimize import babel_minimize
44 prop = {
45 'criteria': 1e-6,
46 'method': 'cg',
47 'force_field': 'GAFF'
48 }
49 babel_minimize(input_path='/path/to/myStructure.mol2',
50 output_path='/path/to/newStructure.mol2',
51 properties=prop)
53 Info:
54 * wrapped_software:
55 * name: Open Babel
56 * version: 2.4.1
57 * license: GNU
58 * ontology:
59 * name: EDAM
60 * schema: http://edamontology.org/EDAM.owl
62 """
64 def __init__(self, input_path: str, output_path: str,
65 properties: Optional[dict] = None, **kwargs) -> None:
66 super().__init__(**locals())
68 def prepare(self) -> None:
69 """Leaves out the options with a value that is not valid: obminimize uses its own default"""
70 for name, (flag, kind) in MINIMIZE_OPTIONS.items():
71 value = getattr(self, name)
72 if value is not None and str(value) != "" and not is_valid_minimize_value(value, kind):
73 fu.log("Value %s of the %s property is not valid, obminimize default used" % (value, name), self.out_log)
74 setattr(self, name, None)
76 def create_cmd(self, container_io_dict, out_log, err_log):
77 """Creates the command line instruction using the properties file settings"""
78 instructions_list = [self.binary_path]
80 for name, (flag, kind) in MINIMIZE_OPTIONS.items():
81 value = getattr(self, name)
82 if value is None or str(value) == "" or (kind is bool and str(value) == "False"):
83 continue
84 instructions_list.append(flag.format(value))
86 iextension = PurePath(container_io_dict["in"]["input_path"]).suffix
87 oextension = PurePath(container_io_dict["out"]["output_path"]).suffix
89 instructions_list.append('-i' + iextension[1:] + ' ' + container_io_dict["in"]["input_path"])
91 instructions_list.append('-o' + oextension[1:])
93 instructions_list.append('>')
95 instructions_list.append(container_io_dict["out"]["output_path"])
97 return instructions_list
99 def run(self) -> None:
100 """Execute the :class:`BabelMinimize <babelm.babel_minimize.BabelMinimize>` babelm.babel_minimize.BabelMinimize object."""
102 # create command line instruction
103 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log)
105 # Run Biobb block
106 self.run_biobb()
109babel_minimize = launcher(BabelMinimize)
112if __name__ == '__main__':
113 BabelMinimize.main()