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

9 

10 

11class AlphaFold(BiobbObject): 

12 """ 

13 | biobb_io AlphaFold 

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

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

16 

17 Args: 

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

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

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

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

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

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

24 

25 Examples: 

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

27 

28 from biobb_io.api.alphafold import alphafold 

29 prop = { 

30 'uniprot_code': 'P00489' 

31 } 

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

33 properties=prop) 

34 

35 Info: 

36 * wrapped_software: 

37 * name: AlphaFold Protein Structure Database 

38 * license: Apache-2.0 

39 * ontology: 

40 * name: EDAM 

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

42 

43 """ 

44 

45 def __init__(self, output_pdb_path, 

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

47 properties = properties or {} 

48 

49 # Call parent class constructor 

50 super().__init__(properties) 

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

52 

53 # Input/Output files 

54 self.io_dict = { 

55 "out": {"output_pdb_path": output_pdb_path} 

56 } 

57 

58 # Properties specific for BB 

59 self.uniprot_code = properties.get('uniprot_code', None) 

60 self.properties = properties 

61 

62 # Check the properties 

63 self.check_properties(properties) 

64 self.check_arguments() 

65 

66 def check_data_params(self, out_log, err_log): 

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

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

69 

70 @launchlogger 

71 def launch(self) -> int: 

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

73 

74 # check input/output paths and parameters 

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

76 

77 # Setup Biobb 

78 if self.check_restart(): 

79 return 0 

80 

81 check_mandatory_property(self.uniprot_code, 'uniprot_code', self.out_log, self.__class__.__name__) 

82 

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

84 

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

86 

87 # Downloading PDB file 

88 pdb_string = download_af(self.uniprot_code, self.out_log, self.global_log, self.__class__.__name__) 

89 write_pdb(pdb_string, self.output_pdb_path, None, self.out_log, self.global_log) 

90 

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

92 

93 return 0 

94 

95 

96def alphafold(output_pdb_path: str, properties: dict = None, **kwargs) -> int: 

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

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

99 

100 return AlphaFold(output_pdb_path=output_pdb_path, 

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

102 

103 

104def main(): 

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

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

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

108 

109 # Specific args of each building block 

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

111 required_args.add_argument('-o', '--output_pdb_path', required=True, help="Path to the output PDB file. Accepted formats: pdb.") 

112 

113 args = parser.parse_args() 

114 config = args.config if args.config else None 

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

116 

117 # Specific call of each building block 

118 alphafold(output_pdb_path=args.output_pdb_path, 

119 properties=properties) 

120 

121 

122if __name__ == '__main__': 

123 main()