Debian Codegraph @

pymzml/file_classes/standardGzip.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 Interface for gzipped mzML files.
5 """
6
7 # Python mzML module - pymzml
8 # Copyright (C) 2010-2019 M. Kösters, C. Fufezan
9 # The MIT License (MIT)
10
11 # Permission is hereby granted, free of charge, to any person obtaining a copy
12 # of this software and associated documentation files (the "Software"), to deal
13 # in the Software without restriction, including without limitation the rights
14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 # copies of the Software, and to permit persons to whom the Software is
16 # furnished to do so, subject to the following conditions:
17
18 # The above copyright notice and this permission notice shall be included in all
19 # copies or substantial portions of the Software.
20
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 # SOFTWARE.
28
29 import codecs
30 import gzip
31 from xml.etree.ElementTree import iterparse
32
33 from .. import regex_patterns
34 from .. import spec
35
36
37 class StandardGzip(object):
38 def __init__(self, path, encoding):
39 """
40 Initalize Wrapper object for gzipped mzML files.
41
42 Arguments:
43 path (str) : path to the file
44 encoding (str) : encoding of the file
45 """
46 self.path = path
47 self.file_handler = codecs.getreader(encoding)(gzip.open(path))
48 self.offset_dict = self._build_index()
49 return
50
51 def close(self):
52 self.file_handler.close()
53
54 def _build_index(self):
55 """
56 Cant build index for standard gzip files
57 """
58 # raise Exception('Cant build index for gzip files')
59 pass
60
61 def read(self, size=-1):
62 """
63 Read binary data from file handler.
64
65 Keyword Arguments:
66 size (int): Number of bytes to read from file, -1 to read to end of file
67
68 Returns:
69 data (str): byte string of len size of input data
70 """
71 return self.file_handler.read(size)
72
73 def __getitem__(self, identifier):
74 """
75 Access the item with id 'identifier' in the file by iterating the xml-tree.
76
77 Arguments:
78 identifier (str): native id of the item to access
79
80 Returns:
81 data (str): text associated with the given identifier
82 """
83 old_pos = self.file_handler.tell()
84 self.file_handler.seek(0, 0)
85 mzml_iter = iter(iterparse(self.file_handler, events=["end"]))
86 while True:
87 event, element = next(mzml_iter)
88 if event == "end":
89 if element.tag.endswith("}spectrum"):
90 if (
91 int(
92 regex_patterns.SPECTRUM_ID_PATTERN.search(
93 element.get("id")
94 ).group(1)
95 )
96 == identifier
97 ):
98 self.file_handler.seek(old_pos, 0)
99 return spec.Spectrum(element, measured_precision=5e-6)
100 elif element.tag.endswith("}chromatogram"):
101 if element.get("id") == identifier:
102 self.file_handler.seek(old_pos, 0)
103 return spec.Chromatogram(element, measured_precision=5e-6)
104
105
106 if __name__ == "__main__":
107 print(__doc__)