Coverage for biobb_io/api/api_binding_site.py: 78%

46 statements  

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

1#!/usr/bin/env python 

2 

3"""Module containing the ApiBindingSite class and the command line interface.""" 

4 

5import argparse 

6import json 

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_binding_site, 

18 write_json, 

19) 

20 

21 

22class ApiBindingSite(BiobbObject): 

23 """ 

24 | biobb_io ApiBindingSite 

25 | This class is a wrapper for the `PDBe REST API <https://www.ebi.ac.uk/pdbe/api/doc/#pdb_apidiv_call_16_calltitle>`_ Binding Sites endpoint. 

26 | This call provides details on binding sites in the entry as per STRUCT_SITE records in PDB files, such as ligand, residues in the site, description of the site, etc. 

27 

28 Args: 

29 output_json_path (str): Path to the JSON file with the binding sites for the requested structure. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_binding_site.json>`_. Accepted formats: json (edam:format_3464). 

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

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

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

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

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

35 

36 Examples: 

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

38 

39 from biobb_io.api.api_binding_site import api_binding_site 

40 prop = { 

41 'pdb_code': '4i23' 

42 } 

43 api_binding_site(output_json_path='/path/to/newBindingSites.json', 

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: PDBe REST API 

49 * license: Apache-2.0 

50 * ontology: 

51 * name: EDAM 

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

53 

54 """ 

55 

56 def __init__(self, output_json_path, properties=None, **kwargs) -> None: 

57 properties = properties or {} 

58 

59 # Call parent class constructor 

60 super().__init__(properties) 

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

62 

63 # Input/Output files 

64 self.io_dict = {"out": {"output_json_path": output_json_path}} 

65 

66 # Properties specific for BB 

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

68 self.properties = properties 

69 

70 # Check the properties 

71 self.check_properties(properties) 

72 self.check_arguments() 

73 

74 def check_data_params(self, out_log, err_log): 

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

76 self.output_json_path = check_output_path( 

77 self.io_dict["out"]["output_json_path"], 

78 "output_json_path", 

79 False, 

80 out_log, 

81 self.__class__.__name__, 

82 ) 

83 

84 @launchlogger 

85 def launch(self) -> int: 

86 """Execute the :class:`ApiBindingSite <api.api_binding_site.ApiBindingSite>` api.api_binding_site.ApiBindingSite object.""" 

87 

88 # check input/output paths and parameters 

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

90 

91 # Setup Biobb 

92 if self.check_restart(): 

93 return 0 

94 # self.stage_files() 

95 

96 check_mandatory_property( 

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

98 ) 

99 

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

101 url = "https://www.ebi.ac.uk/pdbe/api/pdb/entry/binding_sites/%s" 

102 

103 # get JSON object 

104 json_string = download_binding_site( 

105 self.pdb_code, url, self.out_log, self.global_log 

106 ) 

107 

108 # get number of binding sites 

109 fu.log( 

110 "%d binding sites found" % (len(json.loads(json_string)[self.pdb_code])), 

111 self.out_log, 

112 self.global_log, 

113 ) 

114 

115 # write JSON file 

116 write_json(json_string, self.output_json_path, self.out_log, self.global_log) 

117 

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

119 

120 return 0 

121 

122 

123def api_binding_site( 

124 output_json_path: str, properties: Optional[dict] = None, **kwargs 

125) -> int: 

126 """Execute the :class:`ApiBindingSite <api.api_binding_site.ApiBindingSite>` class and 

127 execute the :meth:`launch() <api.api_binding_site.ApiBindingSite.launch>` method.""" 

128 

129 return ApiBindingSite( 

130 output_json_path=output_json_path, properties=properties, **kwargs 

131 ).launch() 

132 

133 

134def main(): 

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

136 parser = argparse.ArgumentParser( 

137 description="This class is a wrapper for the PDBe REST API Binding Sites endpoint", 

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

139 ) 

140 parser.add_argument( 

141 "-c", 

142 "--config", 

143 required=False, 

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

145 ) 

146 

147 # Specific args of each building block 

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

149 required_args.add_argument( 

150 "-o", 

151 "--output_json_path", 

152 required=True, 

153 help="Path to the JSON file with the binding sites for the requested structure. Accepted formats: json.", 

154 ) 

155 

156 args = parser.parse_args() 

157 config = args.config if args.config else None 

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

159 

160 # Specific call of each building block 

161 api_binding_site(output_json_path=args.output_json_path, properties=properties) 

162 

163 

164if __name__ == "__main__": 

165 main()