Coverage for biobb_io/api/structure_info.py: 77%

43 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 StructureInfo class and the command line interface.""" 

4import argparse 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.configuration import settings 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_io.api.common import check_mandatory_property, check_output_path, download_str_info, write_json 

9 

10 

11class StructureInfo(BiobbObject): 

12 """ 

13 | biobb_io StructureInfo 

14 | This class is a wrapper for getting all the available information of a structure from the Protein Data Bank. 

15 | Wrapper for the `MMB PDB mirror <http://mmb.irbbarcelona.org/api/>`_ for getting all the available information of a structure from the Protein Data Bank. 

16 

17 Args: 

18 output_json_path (str): Path to the output JSON file with all the structure information. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/ref_str_info.json>`_. Accepted formats: json (edam:format_3464). 

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

20 * **pdb_code** (*str*) - (None) RSCB PDB structure code. 

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

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

23 

24 Examples: 

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

26 

27 from biobb_io.api.structure_info import structure_info 

28 prop = { 

29 'pdb_code': '2vgb' 

30 } 

31 structure_info(output_json_path='/path/to/newStructure.sdf', 

32 properties=prop) 

33 

34 Info: 

35 * wrapped_software: 

36 * name: Protein Data Bank 

37 * license: Apache-2.0 

38 * ontology: 

39 * name: EDAM 

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

41 

42 """ 

43 

44 def __init__(self, output_json_path, 

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

46 properties = properties or {} 

47 

48 # Call parent class constructor 

49 super().__init__(properties) 

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

51 

52 # Input/Output files 

53 self.io_dict = { 

54 "out": {"output_json_path": output_json_path} 

55 } 

56 

57 # Properties specific for BB 

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

59 self.properties = properties 

60 

61 # Check the properties 

62 self.check_properties(properties) 

63 self.check_arguments() 

64 

65 def check_data_params(self, out_log, err_log): 

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

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

68 

69 @launchlogger 

70 def launch(self) -> int: 

71 """Execute the :class:`StructureInfo <api.structure_info.StructureInfo>` api.structure_info.StructureInfo object.""" 

72 

73 # check input/output paths and parameters 

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

75 

76 # Setup Biobb 

77 if self.check_restart(): 

78 return 0 

79 

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

81 

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

83 url = "http://mmb.irbbarcelona.org/api/pdb/%s.json" 

84 

85 # Downloading PDB file 

86 json_string = download_str_info(self.pdb_code, url, self.out_log, self.global_log) 

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

88 

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

90 

91 return 0 

92 

93 

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

95 """Execute the :class:`StructureInfo <api.structure_info.StructureInfo>` class and 

96 execute the :meth:`launch() <api.structure_info.StructureInfo.launch>` method.""" 

97 

98 return StructureInfo(output_json_path=output_json_path, 

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

100 

101 

102def main(): 

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

104 parser = argparse.ArgumentParser(description="This class is a wrapper for getting all the available information of a structure from the Protein Data Bank.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

106 

107 # Specific args of each building block 

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

109 required_args.add_argument('-o', '--output_json_path', required=True, help="Path to the output JSON file with all the structure information. Accepted formats: json.") 

110 

111 args = parser.parse_args() 

112 config = args.config if args.config else None 

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

114 

115 # Specific call of each building block 

116 structure_info(output_json_path=args.output_json_path, 

117 properties=properties) 

118 

119 

120if __name__ == '__main__': 

121 main()