Coverage for biobb_haddock/haddock/sele_top_clusts.py: 72%
65 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 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 SeleTopClusts(BiobbObject):
19 """
20 | biobb_haddock SeleTopClusts
21 | Wrapper class for the Haddock SeleTopClusts module.
22 | The SeleTopClusts module. `Haddock SeleTopClusts module <https://www.bonvinlab.org/haddock3/modules/analysis/haddock.modules.analysis.seletopclusts.html>`_ selects the top clusters of a docking.
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_rigid.zip>`_. Accepted formats: zip (edam:format_3987).
26 output_selection_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_seletop.zip>`_. Accepted formats: zip (edam:format_3987).
27 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).
28 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).
29 properties (dict - Python dictionary object containing the tool parameters, not input/output files):
30 * **cfg** (*dict*) - ({}) Haddock configuration options specification.
31 * **global_cfg** (*dict*) - ({"postprocess": False}) `Global configuration options <https://www.bonvinlab.org/haddock3-user-manual/global_parameters.html>`_ specification.
32 * **binary_path** (*str*) - ("haddock") Path to the haddock haddock executable binary.
33 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
34 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
35 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory.
36 * **container_path** (*str*) - (None) Path to the binary executable of your container.
37 * **container_image** (*str*) - (None) Container Image identifier.
38 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container.
39 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container.
40 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container.
41 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell.
44 Examples:
45 This is a use example of how to use the building block from Python::
47 from biobb_haddock.haddock.sele_top_clusts import sele_top_clusts
48 prop = { 'binary_path': 'haddock' }
49 sele_top_clusts(input_haddock_wf_data_zip='/path/to/myworkflowdata.zip',
50 output_evaluation_zip='/path/to/myevalfiles.zip',
51 properties=prop)
53 Info:
54 * wrapped_software:
55 * name: Haddock3
56 * version: 2025.5
57 * license: Apache-2.0
58 * ontology:
59 * name: EDAM
60 * schema: http://edamontology.org/EDAM.owl
61 """
63 def __init__(
64 self,
65 input_haddock_wf_data_zip: str,
66 output_selection_zip_path: str,
67 reference_pdb_path: Optional[str] = None,
68 output_haddock_wf_data_zip: Optional[str] = None,
69 haddock_config_path: Optional[str] = None,
70 properties: Optional[dict] = None,
71 **kwargs,
72 ) -> None:
73 properties = properties or {}
75 # Call parent class constructor
76 super().__init__(properties)
78 # Input/Output files
79 self.io_dict = {
80 "in": {"haddock_config_path": haddock_config_path},
81 "out": {
82 "output_haddock_wf_data_zip": output_haddock_wf_data_zip,
83 "output_selection_zip_path": output_selection_zip_path,
84 },
85 }
86 # Should not be copied inside container
87 self.input_haddock_wf_data_zip = input_haddock_wf_data_zip
89 # Properties specific for BB
90 self.haddock_step_name = "seletopclusts"
91 self.output_cfg_path = properties.get("output_cfg_path", "haddock.cfg")
92 self.cfg = {k: v for k, v in properties.get("cfg", dict()).items()}
93 self.global_cfg = properties.get("global_cfg", dict(postprocess=False))
95 # Properties specific for BB
96 self.binary_path = properties.get("binary_path", "haddock3")
98 # Check the properties
99 self.check_properties(properties)
101 @launchlogger
102 def launch(self) -> int:
103 """Execute the :class:`SeleTopClusts <biobb_haddock.haddock.sele_top_clusts>` object."""
104 # tmp_files = []
106 # Setup Biobb
107 if self.check_restart():
108 return 0
109 self.stage_files()
111 # Unzip workflow data to workflow_data_out
112 run_dir = unzip_workflow_data(
113 zip_file=self.input_haddock_wf_data_zip, out_log=self.out_log
114 )
116 workflow_dict = {"haddock_step_name": self.haddock_step_name}
117 workflow_dict.update(self.global_cfg)
119 # Create data dir
120 cfg_dir = fu.create_unique_dir()
121 self.output_cfg_path = create_cfg(
122 output_cfg_path=str(Path(cfg_dir).joinpath(self.output_cfg_path)),
123 workflow_dict=workflow_dict,
124 input_cfg_path=self.stage_io_dict["in"].get("haddock_config_path"),
125 cfg_properties_dict=self.cfg,
126 )
128 if self.container_path:
129 fu.log("Container execution enabled", self.out_log)
131 shutil.copy2(self.output_cfg_path, self.stage_io_dict.get("unique_dir", ""))
132 self.output_cfg_path = str(
133 Path(self.container_volume_path).joinpath(
134 Path(self.output_cfg_path).name
135 )
136 )
138 shutil.copytree(
139 run_dir,
140 str(
141 Path(self.stage_io_dict.get("unique_dir", "")).joinpath(
142 Path(run_dir).name
143 )
144 ),
145 )
146 run_dir = str(
147 Path(self.stage_io_dict.get("unique_dir", "")).joinpath(
148 Path(run_dir).name
149 )
150 )
152 self.cmd = [self.binary_path, self.output_cfg_path, "--extend-run", run_dir]
154 # Run Biobb block
155 self.run_biobb()
157 # Copy files to host
158 # self.copy_to_host()
160 # Copy output
161 haddock_output_list = [
162 str(path)
163 for path in Path(run_dir).iterdir()
164 if path.is_dir() and str(path).endswith(workflow_dict["haddock_step_name"])
165 ]
166 haddock_output_list.sort(reverse=True)
167 output_file_list = [
168 str(path)
169 for path in Path(haddock_output_list[0]).iterdir()
170 if path.is_file() and str(path.name) not in ["io.json", "params.cfg"]
171 ]
172 fu.zip_list(
173 self.io_dict["out"]["output_selection_zip_path"],
174 output_file_list,
175 self.out_log,
176 )
178 # Create zip output
179 if self.io_dict["out"].get("output_haddock_wf_data_zip"):
180 fu.log(
181 f"Zipping {run_dir} to {str(Path(self.io_dict['out']['output_haddock_wf_data_zip']).with_suffix(''))} ",
182 self.out_log,
183 self.global_log,
184 )
185 shutil.make_archive(
186 str(
187 Path(self.io_dict["out"]["output_haddock_wf_data_zip"]).with_suffix(
188 ""
189 )
190 ),
191 "zip",
192 run_dir,
193 )
195 # Remove temporal files
196 self.tmp_files.extend([run_dir,
197 cfg_dir,
198 self.stage_io_dict.get("unique_dir")
199 ])
200 self.remove_tmp_files()
202 return self.return_code
205def sele_top_clusts(
206 input_haddock_wf_data_zip: str,
207 output_selection_zip_path: str,
208 output_haddock_wf_data_zip: Optional[str] = None,
209 haddock_config_path: Optional[str] = None,
210 properties: Optional[dict] = None,
211 **kwargs,
212) -> int:
213 """Create :class:`SeleTopClusts <biobb_haddock.haddock.sele_top_clusts>` class and
214 execute the :meth:`launch() <biobb_haddock.haddock.sele_top_clusts.launch>` method."""
216 return SeleTopClusts(
217 input_haddock_wf_data_zip=input_haddock_wf_data_zip,
218 output_selection_zip_path=output_selection_zip_path,
219 output_haddock_wf_data_zip=output_haddock_wf_data_zip,
220 haddock_config_path=haddock_config_path,
221 properties=properties,
222 **kwargs,
223 ).launch()
226def main():
227 parser = argparse.ArgumentParser(
228 description="Wrapper of the haddock SeleTopClusts module.",
229 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999),
230 )
231 parser.add_argument(
232 "-c",
233 "--config",
234 required=False,
235 help="This file can be a YAML file, JSON file or JSON string",
236 )
238 # Specific args of each building block
239 required_args = parser.add_argument_group("required arguments")
240 required_args.add_argument("--input_haddock_wf_data_zip", required=True)
241 required_args.add_argument("--output_selection_zip_path", required=True)
242 parser.add_argument("--output_haddock_wf_data_zip", required=False)
243 parser.add_argument("--haddock_config_path", required=False)
245 args = parser.parse_args()
246 config = args.config if args.config else None
247 properties = settings.ConfReader(config=config).get_prop_dic()
249 # Specific call of each building block
250 sele_top_clusts(
251 input_haddock_wf_data_zip=args.input_haddock_wf_data_zip,
252 output_selection_zip_path=args.output_selection_zip_path,
253 reference_pdb_path=args.reference_pdb_path,
254 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip,
255 haddock_config_path=args.haddock_config_path,
256 properties=properties,
257 )
260if __name__ == "__main__":
261 main()