Coverage for biobb_haddock/haddock/haddock3_run.py: 67%
64 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-07 08:48 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-07 08:48 +0000
1#!/usr/bin/env python3
3"""Module containing the haddock3 run class and the command line interface."""
5# import os
6# import json
7import argparse
8import shutil
9from pathlib import Path
10from typing import Optional
12from biobb_common.configuration import settings
13from biobb_common.generic.biobb_object import BiobbObject
14from biobb_common.tools import file_utils as fu
15from biobb_common.tools.file_utils import launchlogger
17from biobb_haddock.haddock.common import create_cfg
20class Haddock3Run(BiobbObject):
21 """
22 | biobb_haddock Haddock3Run
23 | Wrapper class for the Haddock3 run module.
24 | The Haddock3 run module launches the HADDOCK3 execution for docking.
26 Args:
27 mol1_input_pdb_path (str): Path to the input PDB file. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2aP_1F3G.pdb>`_. Accepted formats: pdb (edam:format_1476).
28 mol2_input_pdb_path (str): Path to the input PDB file. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/hpr_ensemble.pdb>`_. Accepted formats: pdb (edam:format_1476).
29 ambig_restraints_table_path (str) (Optional): Path to the input TBL file containing a list of ambiguous restraints for docking. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2a-hpr_air.tbl>`_. Accepted formats: tbl (edam:format_2330).
30 unambig_restraints_table_path (str) (Optional): Path to the input TBL file containing a list of unambiguous restraints for docking. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2a-hpr_air.tbl>`_. Accepted formats: tbl (edam:format_2330).
31 hb_restraints_table_path (str) (Optional): Path to the input TBL file containing a list of hydrogen bond restraints for docking. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2a-hpr_air.tbl>`_. Accepted formats: tbl (edam:format_2330).
32 output_haddock_wf_data_zip (str) (Optional): Path to the output zipball containing all the current Haddock workflow data. File type: output. `Sample file <https://github.com/bioexcel/biobb_haddock/raw/master/biobb_haddock/test/data/haddock/haddock_wf_data_emref.zip>`_. Accepted formats: zip (edam:format_3987).
33 haddock_config_path (str) (Optional): Haddock configuration CFG file path. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/run.cfg>`_. Accepted formats: cfg (edam:format_1476).
34 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
35 * **cfg** (*dict*) - ({}) Haddock configuration options specification.
36 * **binary_path** (*str*) - ("haddock") Path to the haddock haddock executable binary.
37 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
38 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
39 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
40 * **container_path** (*str*) - (None) Path to the binary executable of your container.
41 * **container_image** (*str*) - (None) Container Image identifier.
42 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container.
43 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container.
44 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container.
45 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell.
48 Examples:
49 This is a use example of how to use the building block from Python::
51 from biobb_haddock.haddock.haddock3_run import haddock3_run
52 haddock3_run(mol1_input_pdb_path='/path/to/myStructure1.pdb',
53 mol2_input_pdb_path='/path/to/myStructure2.pdb,
54 haddock_config_path='/path/to/myHaddockConfig.cfg',
55 output_haddock_wf_data_zip='/path/to/haddock_output.zip',
56 properties=prop)
58 Info:
59 * wrapped_software:
60 * name: Haddock3
61 * version: 2025.5
62 * license: Apache-2.0
63 * ontology:
64 * name: EDAM
65 * schema: http://edamontology.org/EDAM.owl
66 """
68 def __init__(
69 self,
70 mol1_input_pdb_path: str,
71 mol2_input_pdb_path: str,
72 output_haddock_wf_data_zip: str,
73 ambig_restraints_table_path: Optional[str] = None,
74 unambig_restraints_table_path: Optional[str] = None,
75 hb_restraints_table_path: Optional[str] = None,
76 haddock_config_path: Optional[str] = None,
77 properties: Optional[dict] = None,
78 **kwargs,
79 ) -> None:
80 properties = properties or {}
82 # Call parent class constructor
83 super().__init__(properties)
85 # Input/Output files
86 self.io_dict = {
87 "in": {
88 "mol1_input_pdb_path": mol1_input_pdb_path,
89 "mol2_input_pdb_path": mol2_input_pdb_path,
90 "ambig_restraints_table_path": ambig_restraints_table_path,
91 "unambig_restraints_table_path": unambig_restraints_table_path,
92 "hb_restraints_table_path": hb_restraints_table_path,
93 "haddock_config_path": haddock_config_path,
95 },
96 "out": {
97 "output_haddock_wf_data_zip": output_haddock_wf_data_zip,
98 },
99 }
101 # Properties specific for BB
102 self.output_cfg_path = properties.get("output_cfg_path", "haddock.cfg")
103 self.cfg = {k: str(v)
104 for k, v in properties.get("cfg", dict()).items()}
106 # Properties specific for BB
107 self.binary_path = properties.get("binary_path", "haddock3")
109 # Check the properties
110 self.check_properties(properties)
112 @launchlogger
113 def launch(self) -> int:
114 """Execute the :class:`Haddock3Run <biobb_haddock.haddock.haddock3_run>` object."""
115 # tmp_files = []
117 # Setup Biobb
118 if self.check_restart():
119 return 0
120 self.stage_files()
122 workflow_dict = {
123 "run_dir": fu.create_unique_dir(self.stage_io_dict["unique_dir"]),
124 "molecules": [self.stage_io_dict["in"]["mol1_input_pdb_path"], self.stage_io_dict["in"]["mol2_input_pdb_path"]],
125 }
127 if ambig_restraints_table_path := self.stage_io_dict["in"].get("ambig_restraints_table_path"):
128 workflow_dict["ambig_restraints_table_path"] = ambig_restraints_table_path
129 if unambig_restraints_table_path := self.stage_io_dict["in"].get("unambig_restraints_table_path"):
130 workflow_dict["unambig_restraints_table_path"] = unambig_restraints_table_path
131 if hb_restraints_table_path := self.stage_io_dict["in"].get("hb_restraints_table_path"):
132 workflow_dict["hb_restraints_table_path"] = hb_restraints_table_path
134 # Create data dir
135 cfg_dir = fu.create_unique_dir(self.stage_io_dict["unique_dir"])
136 self.output_cfg_path = create_cfg(
137 output_cfg_path=str(Path(cfg_dir).joinpath(self.output_cfg_path)),
138 workflow_dict=workflow_dict,
139 input_cfg_path=self.stage_io_dict["in"].get("haddock_config_path"),
140 cfg_properties_dict=self.cfg,
141 )
143 if self.container_path:
144 fu.log("Container execution enabled", self.out_log)
146 shutil.copy2(self.output_cfg_path,
147 self.stage_io_dict.get("unique_dir", ""))
148 self.output_cfg_path = str(
149 Path(self.container_volume_path).joinpath(
150 Path(self.output_cfg_path).name
151 )
152 )
154 self.cmd = [self.binary_path, self.output_cfg_path]
156 # Run Biobb block
157 self.run_biobb()
159 # Copy files to host
160 # self.copy_to_host()
162 # Create zip output
163 if self.io_dict["out"].get("output_haddock_wf_data_zip"):
164 fu.log(
165 f"Zipping {workflow_dict['run_dir']} to {str(Path(self.io_dict['out']['output_haddock_wf_data_zip']).with_suffix(''))} ",
166 self.out_log,
167 self.global_log,
168 )
169 shutil.make_archive(
170 str(
171 Path(self.io_dict["out"]["output_haddock_wf_data_zip"]).with_suffix(
172 ""
173 )
174 ),
175 "zip",
176 str(workflow_dict["run_dir"]),
177 )
179 # Remove temporal files
180 self.tmp_files.extend([cfg_dir, self.stage_io_dict.get("unique_dir")])
181 self.remove_tmp_files()
183 return self.return_code
186def haddock3_run(
187 mol1_input_pdb_path: str,
188 mol2_input_pdb_path: str,
189 output_haddock_wf_data_zip: str,
190 ambig_restraints_table_path: Optional[str] = None,
191 unambig_restraints_table_path: Optional[str] = None,
192 hb_restraints_table_path: Optional[str] = None,
193 haddock_config_path: Optional[str] = None,
194 properties: Optional[dict] = None,
195 **kwargs,
196) -> int:
197 """Create :class:`Haddock3Run <biobb_haddock.haddock.haddock3_run>` class and
198 execute the :meth:`launch() <biobb_haddock.haddock.haddock3_run.launch>` method."""
200 return Haddock3Run(
201 mol1_input_pdb_path=mol1_input_pdb_path,
202 mol2_input_pdb_path=mol2_input_pdb_path,
203 output_haddock_wf_data_zip=output_haddock_wf_data_zip,
204 ambig_restraints_table_path=ambig_restraints_table_path,
205 unambig_restraints_table_path=unambig_restraints_table_path,
206 hb_restraints_table_path=hb_restraints_table_path,
207 haddock_config_path=haddock_config_path,
208 properties=properties,
209 **kwargs,
210 ).launch()
213haddock3_run.__doc__ = Haddock3Run.__doc__
216def main():
217 parser = argparse.ArgumentParser(
218 description="Wrapper of the haddock3 HADDOCK3 module.",
219 formatter_class=lambda prog: argparse.RawTextHelpFormatter(
220 prog, width=99999),
221 )
222 parser.add_argument(
223 "-c",
224 "--config",
225 required=False,
226 help="This file can be a YAML file, JSON file or JSON string",
227 )
229 # Specific args of each building block
230 required_args = parser.add_argument_group("required arguments")
231 required_args.add_argument("--mol1_input_pdb_path", required=True)
232 required_args.add_argument("--mol2_input_pdb_path", required=True)
233 required_args.add_argument("--output_haddock_wf_data_zip", required=True)
234 parser.add_argument("--ambig_restraints_table_path", required=False)
235 parser.add_argument("--unambig_restraints_table_path", required=False)
236 parser.add_argument("--hb_restraints_table_path", required=False)
237 parser.add_argument("--haddock_config_path", required=False)
239 args = parser.parse_args()
240 config = args.config if args.config else None
241 properties = settings.ConfReader(config=config).get_prop_dic()
243 # Specific call of each building block
244 haddock3_run(
245 mol1_input_pdb_path=args.mol1_input_pdb_path,
246 mol2_input_pdb_path=args.mol2_input_pdb_path,
247 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip,
248 ambig_restraints_table_path=args.ambig_restraints_table_path,
249 unambig_restraints_table_path=args.unambig_restraints_table_path,
250 hb_restraints_table_path=args.hb_restraints_table_path,
251 haddock_config_path=args.haddock_config_path,
252 properties=properties,
253 )
256if __name__ == "__main__":
257 main()