Coverage for biobb_haddock/haddock/rigid_body.py: 68%
75 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 haddock RigidBody 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, unzip_workflow_data
20class RigidBody(BiobbObject):
21 """
22 | biobb_haddock RigidBody
23 | Wrapper class for the Haddock RigidBody module.
24 | The RigidBody module. `Haddock RigidBody module <https://www.bonvinlab.org/haddock3/modules/sampling/haddock.modules.sampling.rigidbody.html>`_ compute rigid body docking between two molecules.
26 Args:
27 input_haddock_wf_data_zip (str): Path to the input zipball containing all the current Haddock workflow data. File type: input. `Sample file <https://github.com/bioexcel/biobb_haddock/raw/master/biobb_haddock/test/data/haddock/haddock_wf_data_topology.zip>`_. Accepted formats: zip (edam:format_3987).
28 docking_output_zip_path (str): Path to the output PDB file collection in zip format. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/reference/haddock/ref_rigidbody.zip>`_. Accepted formats: zip (edam:format_3987).
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 * **global_cfg** (*dict*) - ({"postprocess": False}) `Global configuration options <https://www.bonvinlab.org/haddock3-user-manual/global_parameters.html>`_ specification.
37 * **binary_path** (*str*) - ("haddock") Path to the haddock haddock executable binary.
38 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
39 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
40 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
41 * **container_path** (*str*) - (None) Path to the binary executable of your container.
42 * **container_image** (*str*) - (None) Container Image identifier.
43 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container.
44 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container.
45 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container.
46 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell.
49 Examples:
50 This is a use example of how to use the building block from Python::
52 from biobb_haddock.haddock.rigid_body import rigid_body
53 prop = { 'binary_path': 'haddock' }
54 rigid_body(input_haddock_wf_data_zip='/path/to/myworkflowdata.zip',
55 docking_output_zip_path='/path/to/mydockingstructures.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 input_haddock_wf_data_zip: str,
71 docking_output_zip_path: str,
72 ambig_restraints_table_path: Optional[str] = None,
73 unambig_restraints_table_path: Optional[str] = None,
74 hb_restraints_table_path: Optional[str] = None,
75 output_haddock_wf_data_zip: 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 "ambig_restraints_table_path": ambig_restraints_table_path,
89 "unambig_restraints_table_path": unambig_restraints_table_path,
90 "hb_restraints_table_path": hb_restraints_table_path,
91 "haddock_config_path": haddock_config_path,
92 },
93 "out": {
94 "output_haddock_wf_data_zip": output_haddock_wf_data_zip,
95 "docking_output_zip_path": docking_output_zip_path,
96 },
97 }
98 # Should not be copied inside container
99 self.input_haddock_wf_data_zip = input_haddock_wf_data_zip
101 # Properties specific for BB
102 self.haddock_step_name = "rigidbody"
103 self.output_cfg_path = properties.get("output_cfg_path", "haddock.cfg")
104 # self.cfg = {k: str(v) for k, v in properties.get('cfg', dict()).items()}
105 self.cfg = {k: v for k, v in properties.get("cfg", dict()).items()}
106 self.global_cfg = properties.get("global_cfg", dict(postprocess=False))
108 # Properties specific for BB
109 self.binary_path = properties.get("binary_path", "haddock3")
111 # Check the properties
112 self.check_properties(properties)
114 @launchlogger
115 def launch(self) -> int:
116 """Execute the :class:`RigidBody <biobb_haddock.haddock.rigid_body>` object."""
117 # tmp_files = []
119 # Setup Biobb
120 if self.check_restart():
121 return 0
122 self.stage_files()
124 # Unzip workflow data to workflow_data_out
125 run_dir = unzip_workflow_data(
126 zip_file=self.input_haddock_wf_data_zip, out_log=self.out_log
127 )
129 workflow_dict = {"haddock_step_name": self.haddock_step_name}
130 workflow_dict.update(self.global_cfg)
132 if ambig_path := self.stage_io_dict["in"].get("ambig_restraints_table_path"):
133 self.cfg["ambig_fname"] = ambig_path
135 if unambig_fname := self.stage_io_dict["in"].get("unambig_restraints_table_path"):
136 self.cfg["unambig_fname"] = unambig_fname
138 if hbond_fname := self.stage_io_dict["in"].get("hb_restraints_table_path"):
139 self.cfg["hbond_fname"] = hbond_fname
141 # Create data dir
142 cfg_dir = fu.create_unique_dir()
143 self.output_cfg_path = create_cfg(
144 output_cfg_path=str(Path(cfg_dir).joinpath(self.output_cfg_path)),
145 workflow_dict=workflow_dict,
146 input_cfg_path=self.stage_io_dict["in"].get("haddock_config_path"),
147 cfg_properties_dict=self.cfg,
148 local_log=self.out_log,
149 global_log=self.global_log,
150 )
152 if self.container_path:
153 fu.log("Container execution enabled", self.out_log)
155 shutil.copy2(self.output_cfg_path, self.stage_io_dict.get("unique_dir", ""))
156 self.output_cfg_path = str(
157 Path(self.container_volume_path).joinpath(
158 Path(self.output_cfg_path).name
159 )
160 )
162 shutil.copytree(
163 run_dir,
164 str(
165 Path(self.stage_io_dict.get("unique_dir", "")).joinpath(
166 Path(run_dir).name
167 )
168 ),
169 )
170 run_dir = str(
171 Path(self.stage_io_dict.get("unique_dir", "")).joinpath(
172 Path(run_dir).name
173 )
174 )
176 self.cmd = [self.binary_path, self.output_cfg_path, "--extend-run", run_dir]
178 # Run Biobb block
179 self.run_biobb()
181 # Copy files to host
182 # self.copy_to_host()
184 # Copy output
186 haddock_output_list = [
187 str(path)
188 for path in Path(run_dir).iterdir()
189 if path.is_dir() and str(path).endswith(workflow_dict["haddock_step_name"])
190 ]
191 haddock_output_list.sort(reverse=True)
192 output_file_list = list(
193 Path(haddock_output_list[0]).glob(
194 workflow_dict["haddock_step_name"] + r"*.pdb*"
195 )
196 )
197 fu.zip_list(
198 self.io_dict["out"]["docking_output_zip_path"],
199 output_file_list,
200 self.out_log,
201 )
203 # Create zip output
204 if self.io_dict["out"].get("output_haddock_wf_data_zip"):
205 fu.log(
206 f"Zipping {run_dir} to {str(Path(self.io_dict['out']['output_haddock_wf_data_zip']).with_suffix(''))} ",
207 self.out_log,
208 self.global_log,
209 )
210 shutil.make_archive(
211 str(
212 Path(self.io_dict["out"]["output_haddock_wf_data_zip"]).with_suffix(
213 ""
214 )
215 ),
216 "zip",
217 run_dir,
218 )
220 # Remove temporal files
221 self.tmp_files.extend([
222 run_dir,
223 cfg_dir,
224 self.stage_io_dict.get("unique_dir")])
225 self.remove_tmp_files()
227 return self.return_code
230def rigid_body(
231 input_haddock_wf_data_zip: str,
232 docking_output_zip_path: str,
233 ambig_restraints_table_path: Optional[str] = None,
234 unambig_restraints_table_path: Optional[str] = None,
235 hb_restraints_table_path: Optional[str] = None,
236 output_haddock_wf_data_zip: Optional[str] = None,
237 haddock_config_path: Optional[str] = None,
238 properties: Optional[dict] = None,
239 **kwargs,
240) -> int:
241 """Create :class:`RigidBody <biobb_haddock.haddock.rigid_body>` class and
242 execute the :meth:`launch() <biobb_haddock.haddock.rigid_body.launch>` method."""
244 return RigidBody(
245 input_haddock_wf_data_zip=input_haddock_wf_data_zip,
246 docking_output_zip_path=docking_output_zip_path,
247 ambig_restraints_table_path=ambig_restraints_table_path,
248 unambig_restraints_table_path=unambig_restraints_table_path,
249 hb_restraints_table_path=hb_restraints_table_path,
250 output_haddock_wf_data_zip=output_haddock_wf_data_zip,
251 haddock_config_path=haddock_config_path,
252 properties=properties,
253 **kwargs,
254 ).launch()
257rigid_body.__doc__ = RigidBody.__doc__
260def main():
261 parser = argparse.ArgumentParser(
262 description="Wrapper of the haddock RigidBody module.",
263 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999),
264 )
265 parser.add_argument(
266 "-c",
267 "--config",
268 required=False,
269 help="This file can be a YAML file, JSON file or JSON string",
270 )
272 # Specific args of each building block
273 required_args = parser.add_argument_group("required arguments")
274 required_args.add_argument("--input_haddock_wf_data_zip", required=True)
275 required_args.add_argument("--docking_output_zip_path", required=True)
276 parser.add_argument("--ambig_restraints_table_path", required=False)
277 parser.add_argument("--unambig_restraints_table_path", required=False)
278 parser.add_argument("--hb_restraints_table_path", required=False)
279 parser.add_argument("--output_haddock_wf_data_zip", required=False)
280 parser.add_argument("--haddock_config_path", required=False)
282 args = parser.parse_args()
283 config = args.config if args.config else None
284 properties = settings.ConfReader(config=config).get_prop_dic()
286 # Specific call of each building block
287 rigid_body(
288 input_haddock_wf_data_zip=args.input_haddock_wf_data_zip,
289 docking_output_zip_path=args.docking_output_zip_path,
290 ambig_restraints_table_path=args.restraints_table_path,
291 unambig_restraints_table_path=args.restraints_table_path,
292 hb_restraints_table_path=args.restraints_table_path,
293 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip,
294 haddock_config_path=args.haddock_config_path,
295 properties=properties,
296 )
299if __name__ == "__main__":
300 main()