Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_merge.py: 80%
59 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 12:37 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 12:37 +0000
1#!/usr/bin/env python3
3"""Module containing the Pdbmerge class and the command line interface."""
5import argparse
6import os
7import zipfile
8from pathlib import Path
9from typing import Optional
11from biobb_common.configuration import settings
12from biobb_common.generic.biobb_object import BiobbObject
13from biobb_common.tools import file_utils as fu
14from biobb_common.tools.file_utils import launchlogger
17class Pdbmerge(BiobbObject):
18 """
19 | biobb_pdb_tools Pdbmerge
20 | Merges several PDB files into one.
21 | This tool merges several PDB files into one. It can be used to merge several PDB files into one.
23 Args:
24 input_file_path (str): ZIP file of selected protein. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/data/pdb_tools/input_pdb_merge.pdb>`_. Accepted formats: zip (edam:format_3987).
25 output_file_path (str): 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_merge.pdb>`_. Accepted formats: pdb (edam:format_1476).
26 properties (dic):
27 * **binary_path** (*str*) - ("pdb_merge") Path to the pdb_merge executable binary.
28 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
29 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
31 Examples:
32 This is a use example of how to use the building block from Python::
34 from biobb_pdb_tools.pdb_tools.biobb_pdb_merge import biobb_pdb_merge
36 biobb_pdb_merge(input_file_path='/path/to/input1.zip',
37 output_file_path='/path/to/output.pdb')
39 Info:
40 * wrapped_software:
41 * name: pdb_tools
42 * version: >=2.5.0
43 * license: Apache-2.0
44 * ontology:
45 * name: EDAM
46 * schema: http://edamontology.org/EDAM.owl
48 """
50 def __init__(
51 self, input_file_path, output_file_path, properties=None, **kwargs
52 ) -> None:
53 properties = properties or {}
55 super().__init__(properties)
56 self.locals_var_dict = locals().copy()
58 self.io_dict = {
59 "in": {"input_file_path": input_file_path},
60 "out": {"output_file_path": output_file_path},
61 }
63 self.binary_path = properties.get("binary_path", "pdb_merge")
64 self.properties = properties
66 self.check_properties(properties)
67 self.check_arguments()
69 @launchlogger
70 def launch(self) -> int:
71 """Execute the :class:`Pdbmerge <biobb_pdb_tools.pdb_tools.pdb_merge>` object."""
73 if self.check_restart():
74 return 0
75 self.stage_files()
77 input_file_path = self.stage_io_dict["in"]["input_file_path"]
78 folder_path = os.path.dirname(input_file_path)
80 if zipfile.is_zipfile(input_file_path):
81 with zipfile.ZipFile(input_file_path, "r") as zip_ref:
82 zip_ref.extractall(folder_path)
84 pdb_files = [
85 file
86 for file in os.listdir(folder_path)
87 if file.lower().endswith(".pdb")
88 ]
90 input_file_list = [os.path.join(folder_path, file) for file in pdb_files]
92 input_file_list = [Path(i) for i in input_file_list]
93 input_file_list = sorted(input_file_list, key=lambda i: i.stem.upper())
94 input_file_list = [str(i) for i in input_file_list]
96 self.cmd = [
97 self.binary_path,
98 *input_file_list,
99 ">",
100 self.io_dict["out"]["output_file_path"],
101 ]
103 else:
104 fu.log(
105 f"The archive {input_file_path} is not a ZIP!",
106 self.out_log,
107 self.global_log,
108 )
110 fu.log(" ".join(self.cmd), self.out_log, self.global_log)
112 fu.log(
113 "Creating command line with instructions and required arguments",
114 self.out_log,
115 self.global_log,
116 )
118 self.run_biobb()
119 self.copy_to_host()
121 self.tmp_files.extend([self.stage_io_dict.get("unique_dir", "")])
122 self.remove_tmp_files()
123 self.check_arguments(output_files_created=True, raise_exception=False)
125 return self.return_code
128def biobb_pdb_merge(
129 input_file_path: str,
130 output_file_path: str,
131 properties: Optional[dict] = None,
132 **kwargs,
133) -> int:
134 """Create :class:`Pdbmerge <biobb_pdb_tools.pdb_tools.pdb_merge>` class and
135 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_merge.launch>` method."""
136 return Pdbmerge(
137 input_file_path=input_file_path,
138 output_file_path=output_file_path,
139 properties=properties,
140 **kwargs,
141 ).launch()
143biobb_pdb_merge.__doc__ = Pdbmerge.__doc__
146def main():
147 """Command line execution of this building block. Please check the command line documentation."""
148 parser = argparse.ArgumentParser(
149 description="Merges several PDB files into one.",
150 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999),
151 )
152 parser.add_argument("--config", required=True, help="Configuration file")
154 required_args = parser.add_argument_group("required arguments")
155 required_args.add_argument(
156 "--input_file_path",
157 required=True,
158 help="Description for the first input file path. Accepted formats: pdb.",
159 )
160 required_args.add_argument(
161 "--output_file_path",
162 required=True,
163 help="Description for the output file path. Accepted formats: zip.",
164 )
166 args = parser.parse_args()
167 args.config = args.config or "{}"
168 properties = settings.ConfReader(config=args.config).get_prop_dic()
169 biobb_pdb_merge(
170 input_file_path=args.input_file_path,
171 output_file_path=args.output_file_path,
172 properties=properties,
173 )
176if __name__ == "__main__":
177 main()