⬅ biobb_pdb_tools/pdb_tools/biobb_pdb_merge.py source

1 #!/usr/bin/env python3
2  
3 """Module containing the Pdbmerge class and the command line interface."""
4  
5 import argparse
6 import os
7 import zipfile
8 from pathlib import Path
9 from typing import Optional
10  
11 from biobb_common.configuration import settings
12 from biobb_common.generic.biobb_object import BiobbObject
13 from biobb_common.tools import file_utils as fu
14 from biobb_common.tools.file_utils import launchlogger
15  
16  
17 class 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.
22  
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.
30  
31 Examples:
32 This is a use example of how to use the building block from Python::
33  
34 from biobb_pdb_tools.pdb_tools.biobb_pdb_merge import biobb_pdb_merge
35  
36 biobb_pdb_merge(input_file_path='/path/to/input1.zip',
37 output_file_path='/path/to/output.pdb')
38  
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
47  
48 """
49  
50 def __init__(
51 self, input_file_path, output_file_path, properties=None, **kwargs
52 ) -> None:
53 properties = properties or {}
54  
55 super().__init__(properties)
56 self.locals_var_dict = locals().copy()
57  
58 self.io_dict = {
59 "in": {"input_file_path": input_file_path},
60 "out": {"output_file_path": output_file_path},
61 }
62  
63 self.binary_path = properties.get("binary_path", "pdb_merge")
64 self.properties = properties
65  
66 self.check_properties(properties)
67 self.check_arguments()
68  
69 @launchlogger
70 def launch(self) -> int:
71 """Execute the :class:`Pdbmerge <biobb_pdb_tools.pdb_tools.pdb_merge>` object."""
72  
73 if self.check_restart():
74 return 0
75 self.stage_files()
76  
77 input_file_path = self.stage_io_dict["in"]["input_file_path"]
78 folder_path = os.path.dirname(input_file_path)
79  
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)
83  
84 pdb_files = [
85 file
86 for file in os.listdir(folder_path)
87 if file.lower().endswith(".pdb")
88 ]
89  
90 input_file_list = [os.path.join(folder_path, file) for file in pdb_files]
91  
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]
95  
96 self.cmd = [
97 self.binary_path,
98 *input_file_list,
99 ">",
100 self.io_dict["out"]["output_file_path"],
101 ]
102  
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 )
109  
110 fu.log(" ".join(self.cmd), self.out_log, self.global_log)
111  
112 fu.log(
113 "Creating command line with instructions and required arguments",
114 self.out_log,
115 self.global_log,
116 )
117  
118 self.run_biobb()
119 self.copy_to_host()
120  
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)
124  
125 return self.return_code
126  
127  
128 def 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()
142  
  • E305 Expected 2 blank lines after class or function definition, found 1
143 biobb_pdb_merge.__doc__ = Pdbmerge.__doc__
144  
145  
146 def 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")
153  
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 )
165  
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 )
174  
175  
176 if __name__ == "__main__":
177 main()