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

13 write_mmcif, 

14) 

15 

16 

17class Mmcif(BiobbObject): 

18 """ 

19 | biobb_io Mmcif 

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

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

22 

23 Args: 

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

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

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

36 prop = { 

37 'pdb_code': '2VGB', 

38 'api_id': 'pdbe' 

39 } 

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

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_mmcif_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_mmcif_path": output_mmcif_path}} 

62 

63 # Properties specific for BB 

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

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

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

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

76 "output_mmcif_path", 

77 False, 

78 out_log, 

79 self.__class__.__name__, 

80 ) 

81 

82 @launchlogger 

83 def launch(self) -> int: 

84 """Execute the :class:`Mmcif <api.mmcif.Mmcif>` api.mmcif.Mmcif 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 mmcif_string = download_mmcif( 

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

102 ) 

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

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

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

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

114 

115 

116mmcif.__doc__ = Mmcif.__doc__ 

117main = Mmcif.get_main(mmcif, "This class is a wrapper for downloading a MMCIF structure from the Protein Data Bank.") 

118 

119if __name__ == "__main__": 

120 main()