Coverage for biobb_io/api/mmcif.py: 77%

43 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-07 10:41 +0000

1#!/usr/bin/env python 

2 

3"""Module containing the Mmcif 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, download_mmcif, write_mmcif 

9 

10 

11class Mmcif(BiobbObject): 

12 """ 

13 | biobb_io Mmcif 

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

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

16 

17 Args: 

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

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

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

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

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

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

24 

25 Examples: 

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

27 

28 from biobb_io.api.mmcif import mmcif 

29 prop = { 

30 'pdb_code': '2VGB', 

31 'api_id': 'pdbe' 

32 } 

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

34 properties=prop) 

35 

36 Info: 

37 * wrapped_software: 

38 * name: Protein Data Bank 

39 * license: Apache-2.0 

40 * ontology: 

41 * name: EDAM 

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

43 

44 """ 

45 

46 def __init__(self, output_mmcif_path, 

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

48 properties = properties or {} 

49 

50 # Call parent class constructor 

51 super().__init__(properties) 

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

53 

54 # Input/Output files 

55 self.io_dict = { 

56 "out": {"output_mmcif_path": output_mmcif_path} 

57 } 

58 

59 # Properties specific for BB 

60 self.api_id = properties.get('api_id', 'pdbe') 

61 self.pdb_code = properties.get('pdb_code', None) 

62 self.properties = properties 

63 

64 # Check the properties 

65 self.check_properties(properties) 

66 self.check_arguments() 

67 

68 def check_data_params(self, out_log, err_log): 

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

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

71 

72 @launchlogger 

73 def launch(self) -> int: 

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

75 

76 # check input/output paths and parameters 

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

78 

79 # Setup Biobb 

80 if self.check_restart(): 

81 return 0 

82 

83 check_mandatory_property(self.pdb_code, 'pdb_code', self.out_log, self.__class__.__name__) 

84 

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

86 

87 # Downloading PDB file 

88 mmcif_string = download_mmcif(self.pdb_code, self.api_id, self.out_log, self.global_log) 

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

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

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

99 

100 return Mmcif(output_mmcif_path=output_mmcif_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 MMCIF 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_mmcif_path', required=True, help="Path to the output MMCIF file. Accepted formats: cif, mmcif.") 

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

119 properties=properties) 

120 

121 

122if __name__ == '__main__': 

123 main()