Coverage for biobb_io/api/mmcif.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 Mmcif 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 download_mmcif, 

16 write_mmcif, 

17) 

18 

19 

20class Mmcif(BiobbObject): 

21 """ 

22 | biobb_io Mmcif 

23 | This class is a wrapper for downloading a MMCIF structure from the Protein Data Bank. 

24 | Wrapper for the `Protein Data Bank in Europe <https://www.ebi.ac.uk/pdbe/>`_, the `Protein Data Bank <https://www.rcsb.org/>`_ and the `MMB PDB mirror <http://mmb.irbbarcelona.org/api/>`_ for downloading a single MMCIF structure. 

25 

26 Args: 

27 output_mmcif_path (str): Path to the output MMCIF file. File type: output. `Sample file <https://github.com/bioexcel/biobb_io/raw/master/biobb_io/test/reference/api/ref_output.mmcif>`_. Accepted formats: cif (edam:format_1477), mmcif (edam:format_1477). 

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

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

30 * **api_id** (*str*) - ("pdbe") Identifier of the PDB REST API from which the MMCIF 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/>`_). 

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

39 prop = { 

40 'pdb_code': '2VGB', 

41 'api_id': 'pdbe' 

42 } 

43 mmcif(output_mmcif_path='/path/to/newStructure.mmcif', 

44 properties=prop) 

45 

46 Info: 

47 * wrapped_software: 

48 * name: Protein Data Bank 

49 * license: Apache-2.0 

50 * ontology: 

51 * name: EDAM 

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

53 

54 """ 

55 

56 def __init__(self, output_mmcif_path, properties=None, **kwargs) -> None: 

57 properties = properties or {} 

58 

59 # Call parent class constructor 

60 super().__init__(properties) 

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

62 

63 # Input/Output files 

64 self.io_dict = {"out": {"output_mmcif_path": output_mmcif_path}} 

65 

66 # Properties specific for BB 

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

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

69 self.properties = properties 

70 

71 # Check the properties 

72 self.check_properties(properties) 

73 self.check_arguments() 

74 

75 def check_data_params(self, out_log, err_log): 

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

77 self.output_mmcif_path = check_output_path( 

78 self.io_dict["out"]["output_mmcif_path"], 

79 "output_mmcif_path", 

80 False, 

81 out_log, 

82 self.__class__.__name__, 

83 ) 

84 

85 @launchlogger 

86 def launch(self) -> int: 

87 """Execute the :class:`Mmcif <api.mmcif.Mmcif>` api.mmcif.Mmcif object.""" 

88 

89 # check input/output paths and parameters 

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

91 

92 # Setup Biobb 

93 if self.check_restart(): 

94 return 0 

95 

96 check_mandatory_property( 

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

98 ) 

99 

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

101 

102 # Downloading PDB file 

103 mmcif_string = download_mmcif( 

104 self.pdb_code, self.api_id, self.out_log, self.global_log 

105 ) 

106 write_mmcif(mmcif_string, self.output_mmcif_path, 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 mmcif(output_mmcif_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

114 """Execute the :class:`Mmcif <api.mmcif.Mmcif>` class and 

115 execute the :meth:`launch() <api.mmcif.Mmcif.launch>` method.""" 

116 

117 return Mmcif( 

118 output_mmcif_path=output_mmcif_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 MMCIF 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_mmcif_path", 

140 required=True, 

141 help="Path to the output MMCIF file. Accepted formats: cif, mmcif.", 

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 mmcif(output_mmcif_path=args.output_mmcif_path, properties=properties) 

150 

151 

152if __name__ == "__main__": 

153 main()