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

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

9 

10 

11class Pdb(BiobbObject): 

12 """ 

13 | biobb_io Pdb 

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

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

16 

17 Args: 

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

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

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

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

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

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

30 prop = { 

31 'pdb_code': '2VGB', 

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

33 'api_id': 'pdbe' 

34 } 

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

36 properties=prop) 

37 

38 Info: 

39 * wrapped_software: 

40 * name: Protein Data Bank 

41 * license: Apache-2.0 

42 * ontology: 

43 * name: EDAM 

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

45 

46 """ 

47 

48 def __init__(self, output_pdb_path, 

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

50 properties = properties or {} 

51 

52 # Call parent class constructor 

53 super().__init__(properties) 

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

55 

56 # Input/Output files 

57 self.io_dict = { 

58 "out": {"output_pdb_path": output_pdb_path} 

59 } 

60 

61 # Properties specific for BB 

62 self.api_id = properties.get('api_id', 'pdbe') 

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

64 self.filter = properties.get('filter', ['ATOM', 'MODEL', 'ENDMDL']) 

65 self.properties = properties 

66 

67 # Check the properties 

68 self.check_properties(properties) 

69 self.check_arguments() 

70 

71 def check_data_params(self, out_log, err_log): 

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

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

74 

75 @launchlogger 

76 def launch(self) -> int: 

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

78 

79 # check input/output paths and parameters 

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

81 

82 # Setup Biobb 

83 if self.check_restart(): 

84 return 0 

85 

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

87 

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

89 

90 # Downloading PDB file 

91 pdb_string = download_pdb(self.pdb_code, self.api_id, self.out_log, self.global_log) 

92 write_pdb(pdb_string, self.output_pdb_path, self.filter, self.out_log, self.global_log) 

93 

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

95 

96 return 0 

97 

98 

99def pdb(output_pdb_path: str, properties: dict = None, **kwargs) -> int: 

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

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

102 

103 return Pdb(output_pdb_path=output_pdb_path, 

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

105 

106 

107def main(): 

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

109 parser = argparse.ArgumentParser(description="This class is a wrapper for downloading a PDB structure from the Protein Data Bank.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

111 

112 # Specific args of each building block 

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

114 required_args.add_argument('-o', '--output_pdb_path', required=True, help="Path to the output PDB file. Accepted formats: pdb.") 

115 

116 args = parser.parse_args() 

117 config = args.config if args.config else None 

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

119 

120 # Specific call of each building block 

121 pdb(output_pdb_path=args.output_pdb_path, 

122 properties=properties) 

123 

124 

125if __name__ == '__main__': 

126 main()