Coverage for biobb_io/api/pdb_cluster_zip.py: 82%

56 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2024-06-14 18:20 +0000

1#!/usr/bin/env python 

2 

3"""PdbClusterZip Module""" 

4import os 

5import argparse 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_io.api.common import check_mandatory_property, check_output_path, get_cluster_pdb_codes, download_pdb, write_pdb 

11 

12 

13class PdbClusterZip(BiobbObject): 

14 """ 

15 | biobb_io PdbClusterZip 

16 | This class is a wrapper for downloading a PDB cluster from the Protein Data Bank. 

17 | 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. 

18 

19 Args: 

20 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). 

21 properties (dic - Python dictionary object containing the tool parameters, not input/output files): 

22 * **pdb_code** (*str*) - (None) RSCB PDB code. 

23 * **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) 

24 * **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). 

25 * **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/>`_). 

26 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

27 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

28 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

29 

30 Examples: 

31 This is a use example of how to use the building block from Python:: 

32 

33 from biobb_io.api.pdb_cluster_zip import pdb_cluster_zip 

34 prop = { 

35 'pdb_code': '2VGB', 

36 'filter': ['ATOM', 'MODEL', 'ENDMDL'], 

37 'cluster': 90, 

38 'api_id': 'pdbe' 

39 } 

40 pdb_cluster_zip(output_pdb_zip_path='/path/to/newStructures.zip', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: Protein Data Bank 

46 * license: Apache-2.0 

47 * ontology: 

48 * name: EDAM 

49 * schema: http://edamontology.org/EDAM.owl 

50 

51 """ 

52 

53 def __init__(self, output_pdb_zip_path, 

54 properties=None, **kwargs) -> None: 

55 properties = properties or {} 

56 

57 # Call parent class constructor 

58 super().__init__(properties) 

59 self.locals_var_dict = locals().copy() 

60 

61 # Input/Output files 

62 self.io_dict = { 

63 "out": {"output_pdb_zip_path": output_pdb_zip_path} 

64 } 

65 

66 # Properties specific for BB 

67 self.api_id = properties.get('api_id', 'pdbe') 

68 self.pdb_code = properties.get('pdb_code', None) 

69 self.filter = properties.get('filter', ['ATOM', 'MODEL', 'ENDMDL']) 

70 self.cluster = properties.get('cluster', 90) 

71 self.properties = properties 

72 

73 # Check the properties 

74 self.check_properties(properties) 

75 self.check_arguments() 

76 

77 def check_data_params(self, out_log, err_log): 

78 """ Checks all the input/output paths and parameters """ 

79 self.output_pdb_zip_path = check_output_path(self.io_dict["out"]["output_pdb_zip_path"], "output_pdb_zip_path", False, out_log, self.__class__.__name__) 

80 

81 @launchlogger 

82 def launch(self) -> int: 

83 """Execute the :class:`PdbClusterZip <api.pdb_cluster_zip.PdbClusterZip>` api.pdb_cluster_zip.PdbClusterZip object.""" 

84 

85 # check input/output paths and parameters 

86 self.check_data_params(self.out_log, self.err_log) 

87 

88 # Setup Biobb 

89 if self.check_restart(): 

90 return 0 

91 

92 check_mandatory_property(self.pdb_code, 'pdb_code', self.out_log, self.__class__.__name__) 

93 

94 self.pdb_code = self.pdb_code.strip().lower() 

95 

96 file_list = [] 

97 # Downloading PDB_files 

98 pdb_code_list = get_cluster_pdb_codes(pdb_code=self.pdb_code, cluster=self.cluster, out_log=self.out_log, global_log=self.global_log) 

99 unique_dir = fu.create_unique_dir() 

100 for pdb_code in pdb_code_list: 

101 pdb_file = os.path.join(unique_dir, pdb_code+".pdb") 

102 pdb_string = download_pdb(pdb_code=pdb_code, api_id=self.api_id, out_log=self.out_log, global_log=self.global_log) 

103 write_pdb(pdb_string, pdb_file, self.filter, self.out_log, self.global_log) 

104 file_list.append(os.path.abspath(pdb_file)) 

105 

106 # Zipping files 

107 fu.log("Zipping the pdb files to: %s" % self.output_pdb_zip_path) 

108 fu.zip_list(self.output_pdb_zip_path, file_list, out_log=self.out_log) 

109 

110 self.tmp_files.extend([ 

111 self.stage_io_dict.get("unique_dir"), 

112 unique_dir 

113 ]) 

114 self.remove_tmp_files() 

115 

116 self.check_arguments(output_files_created=True, raise_exception=False) 

117 

118 return 0 

119 

120 

121def pdb_cluster_zip(output_pdb_zip_path: str, properties: dict = None, **kwargs) -> int: 

122 """Execute the :class:`PdbClusterZip <api.pdb_cluster_zip.PdbClusterZip>` class and 

123 execute the :meth:`launch() <api.pdb_cluster_zip.PdbClusterZip.launch>` method.""" 

124 

125 return PdbClusterZip(output_pdb_zip_path=output_pdb_zip_path, 

126 properties=properties, **kwargs).launch() 

127 

128 

129def main(): 

130 """Command line execution of this building block. Please check the command line documentation.""" 

131 parser = argparse.ArgumentParser(description="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.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

132 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string") 

133 

134 # Specific args of each building block 

135 required_args = parser.add_argument_group('required arguments') 

136 required_args.add_argument('-o', '--output_pdb_zip_path', required=True, help="Path to the ZIP or PDB file containing the output PDB files. Accepted formats: pdb, zip.") 

137 

138 args = parser.parse_args() 

139 config = args.config if args.config else None 

140 properties = settings.ConfReader(config=config).get_prop_dic() 

141 

142 # Specific call of each building block 

143 pdb_cluster_zip(output_pdb_zip_path=args.output_pdb_zip_path, 

144 properties=properties) 

145 

146 

147if __name__ == '__main__': 

148 main()