Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_mkensemble.py: 93%
46 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-04 08:26 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-04 08:26 +0000
1#!/usr/bin/env python3
3"""Module containing the Mkensemble class and the command line interface."""
5import os
6import zipfile
7from pathlib import Path
8from typing import Optional
9from biobb_common.generic.biobb_object import BiobbObject
10from biobb_common.tools import file_utils as fu
11from biobb_common.tools.file_utils import launchlogger
14class Mkensemble(BiobbObject):
15 """
16 | biobb_pdb_tools Mkensemble
17 | Merges several PDB files into one multi-model (ensemble) file.
18 | This tool merges several PDB files into one multi-model (ensemble) file. It can be used to merge several PDB files into one multi-model (ensemble) file.
20 Args:
21 input_file_path (str): Input ZIP file of selected proteins. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/data/pdb_tools/input_pdb_mkensemble.pdb>`_. Accepted formats: zip (edam:format_3987).
22 output_file_path (str): Multi-model (ensemble) PDB file with input PDBs merged. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/reference/pdb_tools/ref_pdb_mkensemble.pdb>`_. Accepted formats: pdb (edam:format_3987).
23 properties (dic):
24 * **binary_path** (*str*) - ("pdb_mkensemble") Path to the pdb_mkensemble executable binary.
25 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
26 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
28 Examples:
29 This is a use example of how to use the building block from Python::
31 from biobb_pdb_tools.pdb_tools.biobb_pdb_mkensemble import biobb_pdb_mkensemble
33 biobb_pdb_mkensemble(input_file_path='/path/to/input1.zip',
34 output_file_path='/path/to/output.pdb')
36 Info:
37 * wrapped_software:
38 * name: pdb_tools
39 * version: >=2.5.0
40 * license: Apache-2.0
41 * ontology:
42 * name: EDAM
43 * schema: http://edamontology.org/EDAM.owl
45 """
47 def __init__(
48 self, input_file_path, output_file_path, properties=None, **kwargs
49 ) -> None:
50 properties = properties or {}
52 super().__init__(properties)
53 self.locals_var_dict = locals().copy()
54 self.io_dict = {
55 "in": {"input_file_path": input_file_path},
56 "out": {"output_file_path": output_file_path},
57 }
59 self.binary_path = properties.get("binary_path", "pdb_mkensemble")
60 self.properties = properties
61 self.check_init(properties)
63 @launchlogger
64 def launch(self) -> int:
65 """Execute the :class:`Mkensemble <biobb_pdb_tools.pdb_tools.pdb_mkensemble>` object."""
67 if self.check_restart():
68 return 0
69 self.stage_files()
71 input_file_path = self.stage_io_dict["in"]["input_file_path"]
72 folder_path = os.path.dirname(input_file_path)
74 if zipfile.is_zipfile(input_file_path):
75 with zipfile.ZipFile(input_file_path, "r") as zip_ref:
76 zip_ref.extractall(folder_path)
78 pdb_files = [
79 file
80 for file in os.listdir(folder_path)
81 if file.lower().endswith(".pdb")
82 ]
84 input_file_list = [os.path.join(
85 folder_path, file) for file in pdb_files]
87 input_file_list = [Path(i) for i in input_file_list]
88 input_file_list = sorted(
89 input_file_list, key=lambda i: i.stem.upper())
90 input_file_list = [str(i) for i in input_file_list]
92 self.cmd = [
93 self.binary_path,
94 *input_file_list,
95 ">",
96 self.io_dict["out"]["output_file_path"],
97 ]
99 else:
100 fu.log(
101 f"The archive {input_file_path} is not a ZIP!",
102 self.out_log,
103 self.global_log,
104 )
106 fu.log(" ".join(self.cmd), self.out_log, self.global_log)
108 fu.log(
109 "Creating command line with instructions and required arguments",
110 self.out_log,
111 self.global_log,
112 )
114 self.run_biobb()
115 self.copy_to_host()
116 self.remove_tmp_files()
117 self.check_arguments(output_files_created=True, raise_exception=False)
119 return self.return_code
122def biobb_pdb_mkensemble(
123 input_file_path: str,
124 output_file_path: str,
125 properties: Optional[dict] = None,
126 **kwargs,
127) -> int:
128 """Create :class:`Mkensemble <biobb_pdb_tools.pdb_tools.pdb_mkensemble>` class and
129 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_mkensemble.launch>` method."""
130 return Mkensemble(**dict(locals())).launch()
133biobb_pdb_mkensemble.__doc__ = Mkensemble.__doc__
134main = Mkensemble.get_main(biobb_pdb_mkensemble, "Merges several PDB files into one multi-model (ensemble) file.")
136if __name__ == "__main__":
137 main()