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

43 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 CanonicalFasta 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_fasta, 

16 write_fasta, 

17) 

18 

19 

20class CanonicalFasta(BiobbObject): 

21 """ 

22 | biobb_io CanonicalFasta 

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

24 | Wrapper for the `Protein Data Bank <https://www.rcsb.org/>`_ and the `MMB PDB mirror <http://mmb.irbbarcelona.org/api/>`_ for downloading a single FASTA structure. 

25 

26 Args: 

27 output_fasta_path (str): Path to the canonical FASTA file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/canonical_fasta.fasta>`_. Accepted formats: fasta (edam:format_1929). 

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

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

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

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

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

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

34 

35 Examples: 

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

37 

38 from biobb_io.api.canonical_fasta import canonical_fasta 

39 prop = { 

40 'pdb_code': '4i23', 

41 'api_id': 'pdb' 

42 } 

43 canonical_fasta(output_fasta_path='/path/to/newFasta.fasta', 

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: Protein Data Bank 

49 * license: Apache-2.0 

50 * ontology: 

51 * name: EDAM 

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

53 

54 """ 

55 

56 def __init__(self, output_fasta_path, properties=None, **kwargs) -> None: 

57 properties = properties or {} 

58 

59 # Call parent class constructor 

60 super().__init__(properties) 

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

62 

63 # Input/Output files 

64 self.io_dict = {"out": {"output_fasta_path": output_fasta_path}} 

65 

66 # Properties specific for BB 

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

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

69 self.properties = properties 

70 

71 # Check the properties 

72 self.check_properties(properties) 

73 self.check_arguments() 

74 

75 def check_data_params(self, out_log, err_log): 

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

77 self.output_fasta_path = check_output_path( 

78 self.io_dict["out"]["output_fasta_path"], 

79 "output_fasta_path", 

80 False, 

81 out_log, 

82 self.__class__.__name__, 

83 ) 

84 

85 @launchlogger 

86 def launch(self) -> int: 

87 """Execute the :class:`CanonicalFasta <api.canonical_fasta.CanonicalFasta>` api.canonical_fasta.CanonicalFasta object.""" 

88 

89 # check input/output paths and parameters 

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

91 

92 # Setup Biobb 

93 if self.check_restart(): 

94 return 0 

95 

96 check_mandatory_property( 

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

98 ) 

99 

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

101 

102 # Downloading PDB file 

103 pdb_string = download_fasta( 

104 self.pdb_code, self.api_id, self.out_log, self.global_log 

105 ) 

106 write_fasta(pdb_string, self.output_fasta_path, self.out_log, self.global_log) 

107 

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

109 

110 return 0 

111 

112 

113def canonical_fasta( 

114 output_fasta_path: str, properties: Optional[dict] = None, **kwargs 

115) -> int: 

116 """Execute the :class:`CanonicalFasta <api.canonical_fasta.CanonicalFasta>` class and 

117 execute the :meth:`launch() <api.canonical_fasta.CanonicalFasta.launch>` method.""" 

118 

119 return CanonicalFasta( 

120 output_fasta_path=output_fasta_path, properties=properties, **kwargs 

121 ).launch() 

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 downloading a FASTA 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_fasta_path", 

142 required=True, 

143 help="Path to the canonical FASTA file. Accepted formats: fasta.", 

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 canonical_fasta(output_fasta_path=args.output_fasta_path, properties=properties) 

152 

153 

154if __name__ == "__main__": 

155 main()