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

42 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 CanonicalFasta 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_output_path, check_mandatory_property, download_fasta, write_fasta 

9 

10 

11class CanonicalFasta(BiobbObject): 

12 """ 

13 | biobb_io CanonicalFasta 

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

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

16 

17 Args: 

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

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

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

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

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

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

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

25 

26 Examples: 

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

28 

29 from biobb_io.api.canonical_fasta import canonical_fasta 

30 prop = { 

31 'pdb_code': '4i23', 

32 'api_id': 'pdb' 

33 } 

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

35 properties=prop) 

36 

37 Info: 

38 * wrapped_software: 

39 * name: Protein Data Bank 

40 * license: Apache-2.0 

41 * ontology: 

42 * name: EDAM 

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

44 

45 """ 

46 

47 def __init__(self, output_fasta_path, 

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

49 properties = properties or {} 

50 

51 # Call parent class constructor 

52 super().__init__(properties) 

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

54 

55 # Input/Output files 

56 self.io_dict = { 

57 "out": {"output_fasta_path": output_fasta_path} 

58 } 

59 

60 # Properties specific for BB 

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

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

63 self.properties = properties 

64 

65 # Check the properties 

66 self.check_properties(properties) 

67 self.check_arguments() 

68 

69 def check_data_params(self, out_log, err_log): 

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

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

72 

73 @launchlogger 

74 def launch(self) -> int: 

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

76 

77 # check input/output paths and parameters 

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

79 

80 # Setup Biobb 

81 if self.check_restart(): 

82 return 0 

83 

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

85 

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

87 

88 # Downloading PDB file 

89 pdb_string = download_fasta(self.pdb_code, self.api_id, self.out_log, self.global_log) 

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

91 

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

93 

94 return 0 

95 

96 

97def canonical_fasta(output_fasta_path: str, properties: dict = None, **kwargs) -> int: 

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

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

100 

101 return CanonicalFasta(output_fasta_path=output_fasta_path, 

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

103 

104 

105def main(): 

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

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

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

109 

110 # Specific args of each building block 

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

112 required_args.add_argument('-o', '--output_fasta_path', required=True, help="Path to the canonical FASTA file. Accepted formats: fasta.") 

113 

114 args = parser.parse_args() 

115 config = args.config if args.config else None 

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

117 

118 # Specific call of each building block 

119 canonical_fasta(output_fasta_path=args.output_fasta_path, 

120 properties=properties) 

121 

122 

123if __name__ == '__main__': 

124 main()