Coverage for biobb_haddock/haddock/flex_ref.py: 69%
74 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 FlexRef class and the command line interface."""
5import argparse
6import shutil
7from pathlib import Path
8from typing import Optional
10from biobb_common.configuration import settings
11from biobb_common.generic.biobb_object import BiobbObject
12from biobb_common.tools import file_utils as fu
13from biobb_common.tools.file_utils import launchlogger
15from biobb_haddock.haddock.common import create_cfg, unzip_workflow_data
18class FlexRef(BiobbObject):
19 """
20 | biobb_haddock FlexRef
21 | Wrapper class for the Haddock FlexRef module.
22 | The FlexRef module.`Haddock FlexRef module <https://www.bonvinlab.org/haddock3/modules/refinement/haddock.modules.refinement.flexref.html>`_ computes a flexible refinement over selected structures.
24 Args:
25 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).
26 refinement_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).
27 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).
28 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).
29 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).
30 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).
31 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).
32 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
33 * **cfg** (*dict*) - ({}) Haddock configuration options specification.
34 * **global_cfg** (*dict*) - ({"postprocess": False}) `Global configuration options <https://www.bonvinlab.org/haddock3-user-manual/global_parameters.html>`_ specification.
35 * **binary_path** (*str*) - ("haddock") Path to the haddock haddock executable binary.
36 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
37 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
38 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
39 * **container_path** (*str*) - (None) Path to the binary executable of your container.
40 * **container_image** (*str*) - (None) Container Image identifier.
41 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container.
42 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container.
43 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container.
44 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell.
47 Examples:
48 This is a use example of how to use the building block from Python::
50 from biobb_haddock.haddock.flex_ref import flex_ref
51 prop = { 'binary_path': 'haddock' }
52 flex_ref(input_haddock_wf_data_zip='/path/to/myworkflowdata.zip',
53 refinement_output_zip_path='/path/to/mydockingstructures.zip',
54 properties=prop)
56 Info:
57 * wrapped_software:
58 * name: Haddock3
59 * version: 2025.5
60 * license: Apache-2.0
61 * ontology:
62 * name: EDAM
63 * schema: http://edamontology.org/EDAM.owl
64 """
66 def __init__(
67 self,
68 input_haddock_wf_data_zip: str,
69 refinement_output_zip_path: str,
70 ambig_restraints_table_path: Optional[str] = None,
71 unambig_restraints_table_path: Optional[str] = None,
72 hb_restraints_table_path: Optional[str] = None,
73 output_haddock_wf_data_zip: Optional[str] = None,
74 haddock_config_path: Optional[str] = None,
75 properties: Optional[dict] = None,
76 **kwargs,
77 ) -> None:
78 properties = properties or {}
80 # Call parent class constructor
81 super().__init__(properties)
83 # Input/Output files
84 self.io_dict = {
85 "in": {
86 "ambig_restraints_table_path": ambig_restraints_table_path,
87 "unambig_restraints_table_path": unambig_restraints_table_path,
88 "hb_restraints_table_path": hb_restraints_table_path,
89 "haddock_config_path": haddock_config_path,
90 },
91 "out": {
92 "output_haddock_wf_data_zip": output_haddock_wf_data_zip,
93 "refinement_output_zip_path": refinement_output_zip_path,
94 },
95 }
96 # Should not be copied inside container
97 self.input_haddock_wf_data_zip = input_haddock_wf_data_zip
99 # Properties specific for BB
100 self.haddock_step_name = "flexref"
101 self.output_cfg_path = properties.get("output_cfg_path", "haddock.cfg")
102 self.cfg = {k: v for k, v in properties.get("cfg", dict()).items()}
103 self.global_cfg = properties.get("global_cfg", dict(postprocess=False))
105 # Properties specific for BB
106 self.binary_path = properties.get("binary_path", "haddock3")
108 # Check the properties
109 self.check_properties(properties)
111 @launchlogger
112 def launch(self) -> int:
113 """Execute the :class:`FlexRef <biobb_haddock.haddock.flex_ref>` object."""
114 # tmp_files = []
116 # Setup Biobb
117 if self.check_restart():
118 return 0
119 self.stage_files()
121 # Unzip workflow data to workflow_data_out
122 run_dir = unzip_workflow_data(
123 zip_file=self.input_haddock_wf_data_zip, out_log=self.out_log
124 )
126 workflow_dict = {"haddock_step_name": self.haddock_step_name}
127 workflow_dict.update(self.global_cfg)
129 if ambig_path := self.stage_io_dict["in"].get("ambig_restraints_table_path"):
130 self.cfg["ambig_fname"] = ambig_path
132 if unambig_fname := self.stage_io_dict["in"].get("unambig_restraints_table_path"):
133 self.cfg["unambig_fname"] = unambig_fname
135 if hbond_fname := self.stage_io_dict["in"].get("hb_restraints_table_path"):
136 self.cfg["hbond_fname"] = hbond_fname
138 # Create data dir
139 cfg_dir = fu.create_unique_dir()
140 self.output_cfg_path = create_cfg(
141 output_cfg_path=str(Path(cfg_dir).joinpath(self.output_cfg_path)),
142 workflow_dict=workflow_dict,
143 input_cfg_path=self.stage_io_dict["in"].get("haddock_config_path"),
144 cfg_properties_dict=self.cfg,
145 local_log=self.out_log,
146 global_log=self.global_log,
147 )
149 if self.container_path:
150 fu.log("Container execution enabled", self.out_log)
152 shutil.copy2(self.output_cfg_path, self.stage_io_dict.get("unique_dir", ""))
153 self.output_cfg_path = str(
154 Path(self.container_volume_path).joinpath(
155 Path(self.output_cfg_path).name
156 )
157 )
159 shutil.copytree(
160 run_dir,
161 str(
162 Path(self.stage_io_dict.get("unique_dir", "")).joinpath(
163 Path(run_dir).name
164 )
165 ),
166 )
167 run_dir = str(
168 Path(self.stage_io_dict.get("unique_dir", "")).joinpath(
169 Path(run_dir).name
170 )
171 )
173 self.cmd = [self.binary_path, self.output_cfg_path, "--extend-run", run_dir]
175 # Run Biobb block
176 self.run_biobb()
178 # Copy files to host
179 # self.copy_to_host()
181 # Copy output
183 haddock_output_list = [
184 str(path)
185 for path in Path(run_dir).iterdir()
186 if path.is_dir() and str(path).endswith(workflow_dict["haddock_step_name"])
187 ]
188 haddock_output_list.sort(reverse=True)
189 output_file_list = list(
190 Path(haddock_output_list[0]).glob(
191 workflow_dict["haddock_step_name"] + r"*.pdb*"
192 )
193 )
194 fu.zip_list(
195 self.io_dict["out"]["refinement_output_zip_path"],
196 output_file_list,
197 self.out_log,
198 )
200 # Create zip output
201 if self.io_dict["out"].get("output_haddock_wf_data_zip"):
202 fu.log(
203 f"Zipping {run_dir} to {str(Path(self.io_dict['out']['output_haddock_wf_data_zip']).with_suffix(''))} ",
204 self.out_log,
205 self.global_log,
206 )
207 shutil.make_archive(
208 str(
209 Path(self.io_dict["out"]["output_haddock_wf_data_zip"]).with_suffix(
210 ""
211 )
212 ),
213 "zip",
214 run_dir,
215 )
217 # Remove temporal files
218 self.tmp_files.extend([run_dir,
219 cfg_dir,
220 self.stage_io_dict.get("unique_dir")
221 ])
222 self.remove_tmp_files()
224 return self.return_code
227def flex_ref(
228 input_haddock_wf_data_zip: str,
229 refinement_output_zip_path: str,
230 ambig_restraints_table_path: Optional[str] = None,
231 unambig_restraints_table_path: Optional[str] = None,
232 hb_restraints_table_path: Optional[str] = None,
233 output_haddock_wf_data_zip: Optional[str] = None,
234 haddock_config_path: Optional[str] = None,
235 properties: Optional[dict] = None,
236 **kwargs,
237) -> int:
238 """Create :class:`FlexRef <biobb_haddock.haddock.flex_ref>` class and
239 execute the :meth:`launch() <biobb_haddock.haddock.flex_ref.launch>` method."""
241 return FlexRef(
242 input_haddock_wf_data_zip=input_haddock_wf_data_zip,
243 refinement_output_zip_path=refinement_output_zip_path,
244 ambig_restraints_table_path=ambig_restraints_table_path,
245 unambig_restraints_table_path=unambig_restraints_table_path,
246 hb_restraints_table_path=hb_restraints_table_path,
247 output_haddock_wf_data_zip=output_haddock_wf_data_zip,
248 haddock_config_path=haddock_config_path,
249 properties=properties,
250 **kwargs,
251 ).launch()
254def main():
255 parser = argparse.ArgumentParser(
256 description="Wrapper of the haddock FlexRef module.",
257 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999),
258 )
259 parser.add_argument(
260 "-c",
261 "--config",
262 required=False,
263 help="This file can be a YAML file, JSON file or JSON string",
264 )
266 # Specific args of each building block
267 required_args = parser.add_argument_group("required arguments")
268 required_args.add_argument("--input_haddock_wf_data_zip", required=True)
269 required_args.add_argument("--refinement_output_zip_path", required=True)
270 parser.add_argument("--ambig_restraints_table_path", required=False)
271 parser.add_argument("--unambig_restraints_table_path", required=False)
272 parser.add_argument("--hb_restraints_table_path", required=False)
273 parser.add_argument("--output_haddock_wf_data_zip", required=False)
274 parser.add_argument("--haddock_config_path", required=False)
276 args = parser.parse_args()
277 config = args.config if args.config else None
278 properties = settings.ConfReader(config=config).get_prop_dic()
280 # Specific call of each building block
281 flex_ref(
282 input_haddock_wf_data_zip=args.input_haddock_wf_data_zip,
283 refinement_output_zip_path=args.refinement_output_zip_path,
284 ambig_restraints_table_path=args.ambig_restraints_table_path,
285 unambig_restraints_table_path=args.unambig_restraints_table_path,
286 hb_restraints_table_path=args.hb_restraints_table_path,
287 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip,
288 haddock_config_path=args.haddock_config_path,
289 properties=properties,
290 )
293if __name__ == "__main__":
294 main()