Debian Codegraph @

pymzml/file_classes/indexedGzip.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 Interface for indexed gzipped files
5
6 @author: Manuel Koesters
7 """
8
9 # Python mzML module - pymzml
10 # Copyright (C) 2010-2019 M. Kösters, C. Fufezan
11 # The MIT License (MIT)
12
13 # Permission is hereby granted, free of charge, to any person obtaining a copy
14 # of this software and associated documentation files (the "Software"), to deal
15 # in the Software without restriction, including without limitation the rights
16 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 # copies of the Software, and to permit persons to whom the Software is
18 # furnished to do so, subject to the following conditions:
19
20 # The above copyright notice and this permission notice shall be included in all
21 # copies or substantial portions of the Software.
22
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 # SOFTWARE.
30
31 import codecs
32 import gzip
33 from xml.etree.ElementTree import XML
34
35 from .. import spec
36 from ..utils.GSGR import GSGR
37
38
39 class IndexedGzip:
40 def __init__(self, path, encoding):
41 """
42 Initialize Wrapper object for indexed gzipped files.
43
44 Arguments:
45 path (str) : path to the file
46 encoding (str) : encoding of the file
47 """
48 self.path = path
49 self.file_handler = codecs.getreader(encoding)(gzip.open(path))
50 self.offset_dict = dict()
51 self._build_index()
52
53 def __del__(self):
54 """Close handlers when deleting object."""
55 self.Reader.close()
56 self.file_handler.close()
57
58 def _build_index(self):
59 """Use the GSGR class to retrieve the index from the file and save it."""
60 self.Reader = GSGR(self.path)
61 self.offset_dict = self.Reader.index
62
63 def read(self, size=-1):
64 """
65 Read binary data from file handler.
66
67 Keyword Arguments:
68 size (int): Number of bytes to read from file, -1 to read to end of file
69
70 Returns:
71 data (str): byte string of len size of input data
72 """
73 return self.file_handler.read(size)
74
75 def __getitem__(self, identifier):
76 """
77 Access the item with id 'identifier' in the file.
78
79 Arguments:
80 identifier (str): native id of the item to access
81
82 Returns:
83 data (str): text associated with the given identifier
84 """
85
86 # TODO more elegant way to add NameSpace (.register_namespace maybe??)
87 ns_prefix = '<mzML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://psi.hupo.org/ms/mzml http://psidev.info/files/ms/mzML/xsd/mzML1.1.0.xsd" id="test_Creinhardtii_QE_pH8" version="1.1.0" xmlns="http://psi.hupo.org/ms/mzml">'
88 ns_suffix = "</mzML>"
89 data = self.Reader.read_block(identifier)
90 element = XML(ns_prefix + data.decode("utf-8") + ns_suffix)
91 if "chromatogram" in element[0].tag:
92 return spec.Chromatogram(list(element)[0], measured_precision=5e-6)
93 else:
94 return spec.Spectrum(list(element)[0], measured_precision=5e-6)
95
96 def close(self):
97 """Close the handlers."""
98 self.Reader.close()
99 self.file_handler.close()
100
101
102 if __name__ == "__main__":
103 print(__doc__)