#!/usr/bin/env python # -*- coding: latin-1 -*- """ Reader class for indexed gzipped files """ # Python mzML module - pymzml # Copyright (C) 2010-2019 M. Kösters, C. Fufezan # The MIT License (MIT) # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import struct import zlib from collections import OrderedDict class GSGR(object): """ Generalized Gzip reader class which enables random access in files written with the :class:`~pymzml.utils.GSGW.GSGW` class. Keyword Arguments: file (str): path to file to read """ def __init__(self, file=None): self.file_in = open(file, "rb") self.filename = file self.magic_bytes = b"\x1f\x8b" self.indexed = True if not self._check_magic_bytes(): raise Exception("not a gzip file (wrong magic bytes)") self.random_access = False # initial state, until index is read self._read_basic_header() if self.flg & 0 != 0: # FTEXT flag self.ascii_file = True if self.flg & 2 != 0: # FHCRC flag crc16 = self.file_in.read(2) if self.flg & 4 != 0: # FEXTRA flag # TODO: maybe never tested xlen = struct.unpack("