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

57 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-10 15:33 +0000

1#!/usr/bin/env python 

2 

3"""PdbClusterZip Module""" 

4 

5import argparse 

6import os 

7from typing import Optional 

8 

9from biobb_common.configuration import settings 

10from biobb_common.generic.biobb_object import BiobbObject 

11from biobb_common.tools import file_utils as fu 

12from biobb_common.tools.file_utils import launchlogger 

13 

14from biobb_io.api.common import ( 

15 check_mandatory_property, 

16 check_output_path, 

17 download_pdb, 

18 get_cluster_pdb_codes, 

19 write_pdb, 

20) 

21 

22 

23class PdbClusterZip(BiobbObject): 

24 """ 

25 | biobb_io PdbClusterZip 

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

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

28 

29 Args: 

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

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

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

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

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

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

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

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

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

39 

40 Examples: 

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

42 

43 from biobb_io.api.pdb_cluster_zip import pdb_cluster_zip 

44 prop = { 

45 'pdb_code': '2VGB', 

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

47 'cluster': 90, 

48 'api_id': 'pdbe' 

49 } 

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

51 properties=prop) 

52 

53 Info: 

54 * wrapped_software: 

55 * name: Protein Data Bank 

56 * license: Apache-2.0 

57 * ontology: 

58 * name: EDAM 

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

60 

61 """ 

62 

63 def __init__(self, output_pdb_zip_path, properties=None, **kwargs) -> None: 

64 properties = properties or {} 

65 

66 # Call parent class constructor 

67 super().__init__(properties) 

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

69 

70 # Input/Output files 

71 self.io_dict = {"out": {"output_pdb_zip_path": output_pdb_zip_path}} 

72 

73 # Properties specific for BB 

74 self.api_id = properties.get("api_id", "pdbe") 

75 self.pdb_code = properties.get("pdb_code", None) 

76 self.filter = properties.get("filter", ["ATOM", "MODEL", "ENDMDL"]) 

77 self.cluster = properties.get("cluster", 90) 

78 self.properties = properties 

79 

80 # Check the properties 

81 self.check_properties(properties) 

82 self.check_arguments() 

83 

84 def check_data_params(self, out_log, err_log): 

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

86 self.output_pdb_zip_path = check_output_path( 

87 self.io_dict["out"]["output_pdb_zip_path"], 

88 "output_pdb_zip_path", 

89 False, 

90 out_log, 

91 self.__class__.__name__, 

92 ) 

93 

94 @launchlogger 

95 def launch(self) -> int: 

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

97 

98 # check input/output paths and parameters 

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

100 

101 # Setup Biobb 

102 if self.check_restart(): 

103 return 0 

104 

105 check_mandatory_property( 

106 self.pdb_code, "pdb_code", self.out_log, self.__class__.__name__ 

107 ) 

108 

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

110 

111 file_list = [] 

112 # Downloading PDB_files 

113 pdb_code_list = get_cluster_pdb_codes( 

114 pdb_code=self.pdb_code, 

115 cluster=self.cluster, 

116 out_log=self.out_log, 

117 global_log=self.global_log, 

118 ) 

119 unique_dir = fu.create_unique_dir() 

120 for pdb_code in pdb_code_list: 

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

122 pdb_string = download_pdb( 

123 pdb_code=pdb_code, 

124 api_id=self.api_id, 

125 out_log=self.out_log, 

126 global_log=self.global_log, 

127 ) 

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

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

130 

131 # Zipping files 

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

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

134 

135 self.tmp_files.extend([self.stage_io_dict.get("unique_dir", ""), unique_dir]) 

136 self.remove_tmp_files() 

137 

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

139 

140 return 0 

141 

142 

143def pdb_cluster_zip( 

144 output_pdb_zip_path: str, properties: Optional[dict] = None, **kwargs 

145) -> int: 

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

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

148 

149 return PdbClusterZip( 

150 output_pdb_zip_path=output_pdb_zip_path, properties=properties, **kwargs 

151 ).launch() 

152 

153 

154def main(): 

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

156 parser = argparse.ArgumentParser( 

157 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.", 

158 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999), 

159 ) 

160 parser.add_argument( 

161 "-c", 

162 "--config", 

163 required=False, 

164 help="This file can be a YAML file, JSON file or JSON string", 

165 ) 

166 

167 # Specific args of each building block 

168 required_args = parser.add_argument_group("required arguments") 

169 required_args.add_argument( 

170 "-o", 

171 "--output_pdb_zip_path", 

172 required=True, 

173 help="Path to the ZIP or PDB file containing the output PDB files. Accepted formats: pdb, zip.", 

174 ) 

175 

176 args = parser.parse_args() 

177 config = args.config if args.config else None 

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

179 

180 # Specific call of each building block 

181 pdb_cluster_zip(output_pdb_zip_path=args.output_pdb_zip_path, properties=properties) 

182 

183 

184if __name__ == "__main__": 

185 main()