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

44 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-20 06:47 +0000

1#!/usr/bin/env python 

2 

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

4 

5import argparse 

6from typing import Optional 

7 

8from biobb_common.configuration import settings 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools.file_utils import launchlogger 

11 

12from biobb_io.api.common import ( 

13 check_mandatory_property, 

14 check_output_path, 

15 download_str_info, 

16 write_json, 

17) 

18 

19 

20class StructureInfo(BiobbObject): 

21 """ 

22 | biobb_io StructureInfo 

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

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

25 

26 Args: 

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

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

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

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

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

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

33 

34 Examples: 

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

36 

37 from biobb_io.api.structure_info import structure_info 

38 prop = { 

39 'pdb_code': '2vgb' 

40 } 

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

42 properties=prop) 

43 

44 Info: 

45 * wrapped_software: 

46 * name: Protein Data Bank 

47 * license: Apache-2.0 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__(self, output_json_path, 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 = {"out": {"output_json_path": output_json_path}} 

63 

64 # Properties specific for BB 

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

66 self.properties = properties 

67 

68 # Check the properties 

69 self.check_properties(properties) 

70 self.check_arguments() 

71 

72 def check_data_params(self, out_log, err_log): 

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

74 self.output_json_path = check_output_path( 

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

76 "output_json_path", 

77 False, 

78 out_log, 

79 self.__class__.__name__, 

80 ) 

81 

82 @launchlogger 

83 def launch(self) -> int: 

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

85 

86 # check input/output paths and parameters 

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

88 

89 # Setup Biobb 

90 if self.check_restart(): 

91 return 0 

92 

93 check_mandatory_property( 

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

95 ) 

96 

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

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

99 

100 # Downloading PDB file 

101 json_string = download_str_info( 

102 self.pdb_code, url, self.out_log, self.global_log 

103 ) 

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

105 

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

107 

108 return 0 

109 

110 

111def structure_info( 

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

113) -> int: 

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

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

116 

117 return StructureInfo( 

118 output_json_path=output_json_path, properties=properties, **kwargs 

119 ).launch() 

120 

121 structure_info.__doc__ = StructureInfo.__doc__ 

122 

123 

124def main(): 

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

126 parser = argparse.ArgumentParser( 

127 description="This class is a wrapper for getting all the available information of a structure from the Protein Data Bank.", 

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

129 ) 

130 parser.add_argument( 

131 "-c", 

132 "--config", 

133 required=False, 

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

135 ) 

136 

137 # Specific args of each building block 

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

139 required_args.add_argument( 

140 "-o", 

141 "--output_json_path", 

142 required=True, 

143 help="Path to the output JSON file with all the structure information. Accepted formats: json.", 

144 ) 

145 

146 args = parser.parse_args() 

147 config = args.config if args.config else None 

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

149 

150 # Specific call of each building block 

151 structure_info(output_json_path=args.output_json_path, properties=properties) 

152 

153 

154if __name__ == "__main__": 

155 main()