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

43 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2024-06-14 18:20 +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 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

26 

27 Examples: 

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

29 

30 from biobb_io.api.pdb import pdb 

31 prop = { 

32 'pdb_code': '2VGB', 

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

34 'api_id': 'pdbe' 

35 } 

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

37 properties=prop) 

38 

39 Info: 

40 * wrapped_software: 

41 * name: Protein Data Bank 

42 * license: Apache-2.0 

43 * ontology: 

44 * name: EDAM 

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

46 

47 """ 

48 

49 def __init__(self, output_pdb_path, 

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

51 properties = properties or {} 

52 

53 # Call parent class constructor 

54 super().__init__(properties) 

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

56 

57 # Input/Output files 

58 self.io_dict = { 

59 "out": {"output_pdb_path": output_pdb_path} 

60 } 

61 

62 # Properties specific for BB 

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

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

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

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_pdb_path = check_output_path(self.io_dict["out"]["output_pdb_path"], "output_pdb_path", False, out_log, self.__class__.__name__) 

75 

76 @launchlogger 

77 def launch(self) -> int: 

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

79 

80 # check input/output paths and parameters 

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

82 

83 # Setup Biobb 

84 if self.check_restart(): 

85 return 0 

86 

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

88 

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

90 

91 # Downloading PDB file 

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

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

94 

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

96 

97 return 0 

98 

99 

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

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

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

103 

104 return Pdb(output_pdb_path=output_pdb_path, 

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

106 

107 

108def main(): 

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

110 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)) 

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

112 

113 # Specific args of each building block 

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

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

116 

117 args = parser.parse_args() 

118 config = args.config if args.config else None 

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

120 

121 # Specific call of each building block 

122 pdb(output_pdb_path=args.output_pdb_path, 

123 properties=properties) 

124 

125 

126if __name__ == '__main__': 

127 main()