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

16 download_af, 

17 write_pdb, 

18) 

19 

20 

21class AlphaFold(BiobbObject): 

22 """ 

23 | biobb_io AlphaFold 

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

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

26 

27 Args: 

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

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

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

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

39 prop = { 

40 'uniprot_code': 'P00489' 

41 } 

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

43 properties=prop) 

44 

45 Info: 

46 * wrapped_software: 

47 * name: AlphaFold Protein Structure Database 

48 * license: Apache-2.0 

49 * ontology: 

50 * name: EDAM 

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

52 

53 """ 

54 

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

56 properties = properties or {} 

57 

58 # Call parent class constructor 

59 super().__init__(properties) 

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

61 

62 # Input/Output files 

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

64 

65 # Properties specific for BB 

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

67 self.properties = properties 

68 

69 # Check the properties 

70 self.check_properties(properties) 

71 self.check_arguments() 

72 

73 def check_data_params(self, out_log, err_log): 

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

75 self.output_pdb_path = check_output_path( 

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

77 "output_pdb_path", 

78 False, 

79 out_log, 

80 self.__class__.__name__, 

81 ) 

82 

83 @launchlogger 

84 def launch(self) -> int: 

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

86 

87 # check input/output paths and parameters 

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

89 

90 # Setup Biobb 

91 if self.check_restart(): 

92 return 0 

93 

94 check_mandatory_property( 

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

96 ) 

97 

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

99 

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

101 

102 # Downloading PDB file 

103 pdb_string = download_af( 

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

105 ) 

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

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

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

116 

117 return AlphaFold( 

118 output_pdb_path=output_pdb_path, properties=properties, **kwargs 

119 ).launch() 

120 

121 

122def main(): 

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

124 parser = argparse.ArgumentParser( 

125 description="This class is a wrapper for downloading a PDB structure from the Protein Data Bank.", 

126 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999), 

127 ) 

128 parser.add_argument( 

129 "-c", 

130 "--config", 

131 required=False, 

132 help="This file can be a YAML file, JSON file or JSON string", 

133 ) 

134 

135 # Specific args of each building block 

136 required_args = parser.add_argument_group("required arguments") 

137 required_args.add_argument( 

138 "-o", 

139 "--output_pdb_path", 

140 required=True, 

141 help="Path to the output PDB file. Accepted formats: pdb.", 

142 ) 

143 

144 args = parser.parse_args() 

145 config = args.config if args.config else None 

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

147 

148 # Specific call of each building block 

149 alphafold(output_pdb_path=args.output_pdb_path, properties=properties) 

150 

151 

152if __name__ == "__main__": 

153 main()