4.1. Writing and Reading igzip files¶
One key feature of pymzML is the ability to write and read indexed gzip files.
The utils module contains a script to easily convert plain or gzipped mzML files into indexed gzip files.
However, the GSGW() and GSGR() class can be used to index any type of data.
After these 2 classes have been introduced, a small example of how to use them for other data than mzML can be found.
4.1.1. Generalized Seekable index Gzip Writer¶
Writer class for indexed gzipped files
-
class
pymzml.utils.GSGW.GSGW(file=None, max_idx=10000, max_idx_len=8, max_offset_len=8, output_path='./test.dat', comp_str=-1)[source]¶ Generalized Gzip writer class with random access to indexed offsets.
Keyword Arguments: - file (string) – Filename for the resulting file
- max_idx (int) – max number of indices which can be saved in this file
- max_idx_len (int) – maximal length of the index in bytes, must be between 1 and 255
- max_offset_len (int) – maximal length of the offset in bytes
- output_path (str) – path to the output file
-
add_data(data, identifier)[source]¶ Create a new gzip member with compressed ‘data’ indexed with ‘index’
Parameters: - data (str) – uncompressed data to write to file
- index (str or int) – unique index for the data
-
encoding¶ Returns the encoding used for this file
-
file_out¶ Output filehandler.
-
write_index()[source]¶ Only call after all the calls of
add_data()!Seek back to the beginning of the file and write the index into the allocated comment bytes (see _write_gen_header(Index=True)).
4.1.2. Generalized Seekable index Gzip Reader¶
Reader class for indexed gzipped files
-
class
pymzml.utils.GSGR.GSGR(file=None)[source]¶ Generalized Gzip reader class wich enables random access in files written with the
GSGWclass.Keyword Arguments: file (str) – path to file to read -
read(size=-1)[source]¶ Read the content of the in File in binary mode
Keyword Arguments: size (int, optional) – number of bytes to read, -1 for everything Returns: data – parsed bytes from input file Return type: bytes
-
read_block(index)[source]¶ Faster version of read_block(_old). It does not use the gzip module, which always reads the gzip header commment block byte by byte, which slows reading down by 1 fold.
Parameters: index (int or str) – identifier associated with a specific block Returns: data – indexed text block as string Return type: str
-
4.1.3. Practical Example: Moby Dick¶
4.1.3.1. Creating the compressed file¶
To utilze GSGW() for other data, one simply needs to parse the data blockwise, so every piece of data, which should be accessible by indexing is written in one go. The index used can be either an integer or a string.
The code example below parses each chapter of moby dick and indexes it with its corresponding chapter number.
Example:
def index_by_chapter(txt):
"""
Iterate the text file while collecting the data for each chapter and compressing it
Args:
txt(str): Moby Dick text as string
"""
chapter_start = regex_patterns.MOBY_DICK_CHAPTER_PATTERN
general_seekable_gzip_writer = GSGW(
file = 'Moby_Dick_indexed.gz',
max_idx = 50,
max_idx_len = 2,
output_path = './Moby_Dick_indexed.gz'
)
with general_seekable_gzip_writer as index_writer:
current_chapter = ''
for line in txt:
if re.match(chapter_start, line):
match = re.match(chapter_start, line)
current_chapter_number = int(match.group(1)) - 1
print(
'indexing chapter {0} '.format(
current_chapter_number
),
end = '\r'
)
index_writer.add_data(current_chapter, current_chapter_number)
current_chapter = ''
else:
current_chapter += line
current_chapter_number += 1
index_writer.add_data(current_chapter, current_chapter_number)
print(
'index chapter {0} '.format(
current_chapter_number
),
end='\r'
)
index_writer.write_index()
print('Indexed {0} chapters'.format(current_chapter_number))
4.1.3.2. Accessing the data¶
In order to access the chapter in the compressed file, one simply needs to initialize the GSGR() with the path to the created file and can access the chapters conveniently by the python bracket notation ([]).
from GSGR import GSGR
import sys
import time
my_Reader = GSGR('./Moby_Dick_indexed.gz')
if __name__ == '__main__':
if len(sys.argv) != 2:
print(__doc__)
else:
chap_num = int(sys.argv[1])
print(
'''
Reading indexed gzip and retrieving chapter {0}
'''.format(chap_num)
)
s = time.time()
print(
'''
Chapter {0} Took {1:.5f} seconds to retrieve chapter
'''.format(
my_Reader.read_block(chap_num),
time.time() - s
)
)