Coverage for biobb_io/api/alphafold.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 AlphaFold 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 check_uniprot_code, 

13 download_af, 

14 write_pdb, 

15) 

16 

17 

18class AlphaFold(BiobbObject): 

19 """ 

20 | biobb_io AlphaFold 

21 | This class is a wrapper for downloading a PDB structure from the AlphaFold Protein Structure Database. 

22 | Wrapper for the `AlphaFold Protein Structure Database <https://alphafold.ebi.ac.uk/>`_ for downloading a single PDB structure from its corresponding Uniprot code. 

23 

24 Args: 

25 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_alphafold.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

27 * **uniprot_code** (*str*) - (None) Uniprot code. 

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

36 prop = { 

37 'uniprot_code': 'P00489' 

38 } 

39 alphafold(output_pdb_path='/path/to/newStructure.pdb', 

40 properties=prop) 

41 

42 Info: 

43 * wrapped_software: 

44 * name: AlphaFold Protein Structure Database 

45 * license: Apache-2.0 

46 * ontology: 

47 * name: EDAM 

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

49 

50 """ 

51 

52 def __init__(self, output_pdb_path, properties=None, **kwargs) -> None: 

53 properties = properties or {} 

54 

55 # Call parent class constructor 

56 super().__init__(properties) 

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

58 

59 # Input/Output files 

60 self.io_dict = {"out": {"output_pdb_path": output_pdb_path}} 

61 

62 # Properties specific for BB 

63 self.uniprot_code = properties.get("uniprot_code", None) 

64 self.properties = properties 

65 

66 # Check the properties 

67 self.check_properties(properties) 

68 self.check_arguments() 

69 

70 def check_data_params(self, out_log, err_log): 

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

72 self.output_pdb_path = check_output_path( 

73 self.io_dict["out"]["output_pdb_path"], 

74 "output_pdb_path", 

75 False, 

76 out_log, 

77 self.__class__.__name__, 

78 ) 

79 

80 @launchlogger 

81 def launch(self) -> int: 

82 """Execute the :class:`AlphaFold <api.alphafold.AlphaFold>` api.alphafold.AlphaFold object.""" 

83 

84 # check input/output paths and parameters 

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

86 

87 # Setup Biobb 

88 if self.check_restart(): 

89 return 0 

90 

91 check_mandatory_property( 

92 self.uniprot_code, "uniprot_code", self.out_log, self.__class__.__name__ 

93 ) 

94 

95 self.uniprot_code = self.uniprot_code.strip().upper() 

96 

97 check_uniprot_code(self.uniprot_code, self.out_log, self.__class__.__name__) 

98 

99 # Downloading PDB file 

100 pdb_string = download_af( 

101 self.uniprot_code, self.out_log, self.global_log, self.__class__.__name__ 

102 ) 

103 write_pdb(pdb_string, self.output_pdb_path, None, 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 alphafold(output_pdb_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

111 """Execute the :class:`AlphaFold <api.alphafold.AlphaFold>` class and 

112 execute the :meth:`launch() <api.alphafold.AlphaFold.launch>` method.""" 

113 return AlphaFold(**dict(locals())).launch() 

114 

115 

116alphafold.__doc__ = AlphaFold.__doc__ 

117main = AlphaFold.get_main(alphafold, "This class is a wrapper for downloading a PDB structure from the Protein Data Bank.") 

118 

119if __name__ == "__main__": 

120 main()