Coverage for biobb_io/api/pdb_cluster_zip.py: 31%
48 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-04 08:31 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-04 08:31 +0000
1#!/usr/bin/env python
3"""PdbClusterZip Module"""
5import os
6from typing import Optional
7from biobb_common.generic.biobb_object import BiobbObject
8from biobb_common.tools import file_utils as fu
9from biobb_common.tools.file_utils import launchlogger
11from biobb_io.api.common import (
12 check_mandatory_property,
13 check_output_path,
14 download_pdb,
15 get_cluster_pdb_codes,
16 write_pdb,
17)
20class PdbClusterZip(BiobbObject):
21 """
22 | biobb_io PdbClusterZip
23 | This class is a wrapper for downloading a PDB cluster from the Protein Data Bank.
24 | Wrapper for the `Protein Data Bank in Europe <https://www.ebi.ac.uk/pdbe/>`_, the `Protein Data Bank <https://www.rcsb.org/>`_ and the `MMB PDB mirror <http://mmb.irbbarcelona.org/api/>`_ for downloading a PDB cluster.
26 Args:
27 output_pdb_zip_path (str): Path to the ZIP file containing the output PDB files. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_pdb_cluster.zip>`_. Accepted formats: zip (edam:format_3987).
28 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
29 * **pdb_code** (*str*) - (None) RSCB PDB code.
30 * **filter** (*str*) - (["ATOM", "MODEL", "ENDMDL"]) Array of groups to be kept. If value is None or False no filter will be applied. All the possible values are defined in the official PDB specification (http://www.wwpdb.org/documentation/file-format-content/format33/v3.3.html)
31 * **cluster** (*int*) - (90) Sequence Similarity Cutoff. Values: 50 (structures having less than 50% sequence identity to each other), 70 (structures having less than 70% sequence identity to each other), 90 (structures having less than 90% sequence identity to each other), 95 (structures having less than 95% sequence identity to each other).
32 * **api_id** (*str*) - ("pdbe") Identifier of the PDB REST API from which the PDB structure will be downloaded. Values: pdbe (`PDB in Europe REST API <https://www.ebi.ac.uk/pdbe/pdbe-rest-api>`_), pdb (`RCSB PDB REST API <https://data.rcsb.org/>`_), mmb (`MMB PDB mirror API <http://mmb.irbbarcelona.org/api/>`_).
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.
37 Examples:
38 This is a use example of how to use the building block from Python::
40 from biobb_io.api.pdb_cluster_zip import pdb_cluster_zip
41 prop = {
42 'pdb_code': '2VGB',
43 'filter': ['ATOM', 'MODEL', 'ENDMDL'],
44 'cluster': 90,
45 'api_id': 'pdbe'
46 }
47 pdb_cluster_zip(output_pdb_zip_path='/path/to/newStructures.zip',
48 properties=prop)
50 Info:
51 * wrapped_software:
52 * name: Protein Data Bank
53 * license: Apache-2.0
54 * ontology:
55 * name: EDAM
56 * schema: http://edamontology.org/EDAM.owl
58 """
60 def __init__(self, output_pdb_zip_path, properties=None, **kwargs) -> None:
61 properties = properties or {}
63 # Call parent class constructor
64 super().__init__(properties)
65 self.locals_var_dict = locals().copy()
67 # Input/Output files
68 self.io_dict = {"out": {"output_pdb_zip_path": output_pdb_zip_path}}
70 # Properties specific for BB
71 self.api_id = properties.get("api_id", "pdbe")
72 self.pdb_code = properties.get("pdb_code", None)
73 self.filter = properties.get("filter", ["ATOM", "MODEL", "ENDMDL"])
74 self.cluster = properties.get("cluster", 90)
75 self.properties = properties
77 # Check the properties
78 self.check_properties(properties)
79 self.check_arguments()
81 def check_data_params(self, out_log, err_log):
82 """Checks all the input/output paths and parameters"""
83 self.output_pdb_zip_path = check_output_path(
84 self.io_dict["out"]["output_pdb_zip_path"],
85 "output_pdb_zip_path",
86 False,
87 out_log,
88 self.__class__.__name__,
89 )
91 @launchlogger
92 def launch(self) -> int:
93 """Execute the :class:`PdbClusterZip <api.pdb_cluster_zip.PdbClusterZip>` api.pdb_cluster_zip.PdbClusterZip object."""
95 # check input/output paths and parameters
96 self.check_data_params(self.out_log, self.err_log)
98 # Setup Biobb
99 if self.check_restart():
100 return 0
102 check_mandatory_property(
103 self.pdb_code, "pdb_code", self.out_log, self.__class__.__name__
104 )
106 self.pdb_code = self.pdb_code.strip().lower()
108 file_list = []
109 # Downloading PDB_files
110 pdb_code_list = get_cluster_pdb_codes(
111 pdb_code=self.pdb_code,
112 cluster=self.cluster,
113 out_log=self.out_log,
114 global_log=self.global_log,
115 )
116 unique_dir = fu.create_unique_dir()
117 for pdb_code in pdb_code_list:
118 pdb_file = os.path.join(unique_dir, pdb_code + ".pdb")
119 pdb_string = download_pdb(
120 pdb_code=pdb_code,
121 api_id=self.api_id,
122 out_log=self.out_log,
123 global_log=self.global_log,
124 )
125 write_pdb(pdb_string, pdb_file, self.filter, self.out_log, self.global_log)
126 file_list.append(os.path.abspath(pdb_file))
128 # Zipping files
129 fu.log("Zipping the pdb files to: %s" % self.output_pdb_zip_path)
130 fu.zip_list(self.output_pdb_zip_path, file_list, out_log=self.out_log)
132 self.tmp_files.extend([unique_dir])
133 self.remove_tmp_files()
135 self.check_arguments(output_files_created=True, raise_exception=False)
137 return 0
140def pdb_cluster_zip(
141 output_pdb_zip_path: str, properties: Optional[dict] = None, **kwargs
142) -> int:
143 """Execute the :class:`PdbClusterZip <api.pdb_cluster_zip.PdbClusterZip>` class and
144 execute the :meth:`launch() <api.pdb_cluster_zip.PdbClusterZip.launch>` method."""
145 return PdbClusterZip(**dict(locals())).launch()
148pdb_cluster_zip.__doc__ = PdbClusterZip.__doc__
149main = PdbClusterZip.get_main(pdb_cluster_zip, "Wrapper for the Protein Data Bank in Europe (https://www.ebi.ac.uk/pdbe/), the Protein Data Bank (https://www.rcsb.org/) and the MMB PDB mirror (http://mmb.irbbarcelona.org/api/) for downloading a PDB cluster.")
151if __name__ == "__main__":
152 main()