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

46 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-07 10:41 +0000

1#!/usr/bin/env python 

2 

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

4import argparse 

5import json 

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_output_path, check_mandatory_property, download_binding_site, write_json 

11 

12 

13class ApiBindingSite(BiobbObject): 

14 """ 

15 | biobb_io ApiBindingSite 

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

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

18 

19 Args: 

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

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

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

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

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

25 

26 Examples: 

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

28 

29 from biobb_io.api.api_binding_site import api_binding_site 

30 prop = { 

31 'pdb_code': '4i23' 

32 } 

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

34 properties=prop) 

35 

36 Info: 

37 * wrapped_software: 

38 * name: PDBe REST API 

39 * license: Apache-2.0 

40 * ontology: 

41 * name: EDAM 

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

43 

44 """ 

45 

46 def __init__(self, output_json_path, 

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

48 properties = properties or {} 

49 

50 # Call parent class constructor 

51 super().__init__(properties) 

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

53 

54 # Input/Output files 

55 self.io_dict = { 

56 "out": {"output_json_path": output_json_path} 

57 } 

58 

59 # Properties specific for BB 

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

61 self.properties = properties 

62 

63 # Check the properties 

64 self.check_properties(properties) 

65 self.check_arguments() 

66 

67 def check_data_params(self, out_log, err_log): 

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

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

70 

71 @launchlogger 

72 def launch(self) -> int: 

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

74 

75 # check input/output paths and parameters 

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

77 

78 # Setup Biobb 

79 if self.check_restart(): 

80 return 0 

81 # self.stage_files() 

82 

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

84 

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

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

87 

88 # get JSON object 

89 json_string = download_binding_site(self.pdb_code, url, self.out_log, self.global_log) 

90 

91 # get number of binding sites 

92 fu.log('%d binding sites found' % (len(json.loads(json_string)[self.pdb_code])), self.out_log, self.global_log) 

93 

94 # write JSON file 

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

96 

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

98 

99 return 0 

100 

101 

102def api_binding_site(output_json_path: str, properties: dict = None, **kwargs) -> int: 

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

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

105 

106 return ApiBindingSite(output_json_path=output_json_path, 

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

108 

109 

110def main(): 

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

112 parser = argparse.ArgumentParser(description="This class is a wrapper for the PDBe REST API Binding Sites endpoint", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

114 

115 # Specific args of each building block 

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

117 required_args.add_argument('-o', '--output_json_path', required=True, help="Path to the JSON file with the binding sites for the requested structure. Accepted formats: json.") 

118 

119 args = parser.parse_args() 

120 config = args.config if args.config else None 

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

122 

123 # Specific call of each building block 

124 api_binding_site(output_json_path=args.output_json_path, 

125 properties=properties) 

126 

127 

128if __name__ == '__main__': 

129 main()