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

34 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-04 08:31 +0000

1#!/usr/bin/env python 

2 

3"""Module containing the CanonicalFasta class and the command line interface.""" 

4 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8 

9from biobb_io.api.common import ( 

10 check_mandatory_property, 

11 check_output_path, 

12 download_fasta, 

13 write_fasta, 

14) 

15 

16 

17class CanonicalFasta(BiobbObject): 

18 """ 

19 | biobb_io CanonicalFasta 

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

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

22 

23 Args: 

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

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

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

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

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

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

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

31 

32 Examples: 

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

34 

35 from biobb_io.api.canonical_fasta import canonical_fasta 

36 prop = { 

37 'pdb_code': '4i23', 

38 'api_id': 'pdb' 

39 } 

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

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: Protein Data Bank 

46 * license: Apache-2.0 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

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

54 properties = properties or {} 

55 

56 # Call parent class constructor 

57 super().__init__(properties) 

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

59 

60 # Input/Output files 

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

62 

63 # Properties specific for BB 

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

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

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_fasta_path = check_output_path( 

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

76 "output_fasta_path", 

77 False, 

78 out_log, 

79 self.__class__.__name__, 

80 ) 

81 

82 @launchlogger 

83 def launch(self) -> int: 

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

85 

86 # check input/output paths and parameters 

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

88 

89 # Setup Biobb 

90 if self.check_restart(): 

91 return 0 

92 

93 check_mandatory_property( 

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

95 ) 

96 

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

98 

99 # Downloading PDB file 

100 pdb_string = download_fasta( 

101 self.pdb_code, self.api_id, self.out_log, self.global_log 

102 ) 

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

104 

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

106 

107 return 0 

108 

109 

110def canonical_fasta( 

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

112) -> int: 

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

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

115 return CanonicalFasta(**dict(locals())).launch() 

116 

117 

118canonical_fasta.__doc__ = CanonicalFasta.__doc__ 

119main = CanonicalFasta.get_main(canonical_fasta, "This class is a wrapper for downloading a FASTA structure from the Protein Data Bank.") 

120 

121if __name__ == "__main__": 

122 main()