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

44 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 Pdb 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_pdb, 

16 write_pdb, 

17) 

18 

19 

20class Pdb(BiobbObject): 

21 """ 

22 | biobb_io Pdb 

23 | This class is a wrapper for downloading a PDB structure from the Protein Data Bank. 

24 | 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 single PDB structure. 

25 

26 Args: 

27 output_pdb_path (str): Path to the output PDB file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/output_pdb.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

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

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

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

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.pdb import pdb 

40 prop = { 

41 'pdb_code': '2VGB', 

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

43 'api_id': 'pdbe' 

44 } 

45 pdb(output_pdb_path='/path/to/newStructure.pdb', 

46 properties=prop) 

47 

48 Info: 

49 * wrapped_software: 

50 * name: Protein Data Bank 

51 * license: Apache-2.0 

52 * ontology: 

53 * name: EDAM 

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

55 

56 """ 

57 

58 def __init__(self, output_pdb_path, properties=None, **kwargs) -> None: 

59 properties = properties or {} 

60 

61 # Call parent class constructor 

62 super().__init__(properties) 

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

64 

65 # Input/Output files 

66 self.io_dict = {"out": {"output_pdb_path": output_pdb_path}} 

67 

68 # Properties specific for BB 

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

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

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

72 self.properties = properties 

73 

74 # Check the properties 

75 self.check_properties(properties) 

76 self.check_arguments() 

77 

78 def check_data_params(self, out_log, err_log): 

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

80 self.output_pdb_path = check_output_path( 

81 self.io_dict["out"]["output_pdb_path"], 

82 "output_pdb_path", 

83 False, 

84 out_log, 

85 self.__class__.__name__, 

86 ) 

87 

88 @launchlogger 

89 def launch(self) -> int: 

90 """Execute the :class:`Pdb <api.pdb.Pdb>` api.pdb.Pdb object.""" 

91 

92 # check input/output paths and parameters 

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

94 

95 # Setup Biobb 

96 if self.check_restart(): 

97 return 0 

98 

99 check_mandatory_property( 

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

101 ) 

102 

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

104 

105 # Downloading PDB file 

106 pdb_string = download_pdb( 

107 self.pdb_code, self.api_id, self.out_log, self.global_log 

108 ) 

109 write_pdb( 

110 pdb_string, self.output_pdb_path, self.filter, self.out_log, self.global_log 

111 ) 

112 

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

114 

115 return 0 

116 

117 

118def pdb(output_pdb_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

119 """Execute the :class:`Pdb <api.pdb.Pdb>` class and 

120 execute the :meth:`launch() <api.pdb.Pdb.launch>` method.""" 

121 

122 return Pdb( 

123 output_pdb_path=output_pdb_path, properties=properties, **kwargs 

124 ).launch() 

125 

126 

127def main(): 

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

129 parser = argparse.ArgumentParser( 

130 description="This class is a wrapper for downloading a PDB structure from the Protein Data Bank.", 

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

132 ) 

133 parser.add_argument( 

134 "-c", 

135 "--config", 

136 required=False, 

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

138 ) 

139 

140 # Specific args of each building block 

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

142 required_args.add_argument( 

143 "-o", 

144 "--output_pdb_path", 

145 required=True, 

146 help="Path to the output PDB file. Accepted formats: pdb.", 

147 ) 

148 

149 args = parser.parse_args() 

150 config = args.config if args.config else None 

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

152 

153 # Specific call of each building block 

154 pdb(output_pdb_path=args.output_pdb_path, properties=properties) 

155 

156 

157if __name__ == "__main__": 

158 main()