| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Interface for mzML files |
| 5 |
|
| 6 |
@author: Manuel Koesters |
| 7 |
""" |
| 8 |
from io import BytesIO |
| 9 |
from pymzml.file_classes import indexedGzip, standardGzip, standardMzml, bytesMzml |
| 10 |
from pymzml.utils import GSGR |
| 11 |
|
| 12 |
|
| 13 |
class FileInterface(object): |
| 14 |
"""Interface to different mzML formats.""" |
| 15 |
|
| 16 |
def __init__( |
| 17 |
self, path, encoding, build_index_from_scratch=False, index_regex=None |
| 18 |
): |
| 19 |
""" |
| 20 |
Initialize a object interface to mzML files. |
| 21 |
|
| 22 |
Arguments: |
| 23 |
path (str) : path to the mzML file |
| 24 |
encoding (str) : encoding of the file |
| 25 |
|
| 26 |
""" |
| 27 |
self.build_index_from_scratch = build_index_from_scratch |
| 28 |
self.encoding = encoding |
| 29 |
self.index_regex = index_regex |
| 30 |
self.file_handler = self._open(path) |
| 31 |
self.offset_dict = self.file_handler.offset_dict |
| 32 |
|
| 33 |
def close(self): |
| 34 |
"""Close the internal file handler.""" |
| 35 |
self.file_handler.close() |
| 36 |
|
| 37 |
def _open(self, path_or_file): |
| 38 |
""" |
| 39 |
Open a file like object resp. a wrapper for a file like object. |
| 40 |
|
| 41 |
Arguments: |
| 42 |
path (str): path to the mzml file |
| 43 |
|
| 44 |
Returns: |
| 45 |
file_handler: instance of |
| 46 |
:py:class:`~pymzml.file_classes.standardGzip.StandardGzip`, |
| 47 |
:py:class:`~pymzml.file_classes.indexedGzip.IndexedGzip` or |
| 48 |
:py:class:`~pymzml.file_classes.standardMzml.StandardMzml`, |
| 49 |
based on the file ending of 'path' |
| 50 |
""" |
| 51 |
if isinstance(path_or_file, BytesIO): |
| 52 |
return bytesMzml.BytesMzml( |
| 53 |
path_or_file, self.encoding, self.build_index_from_scratch |
| 54 |
) |
| 55 |
if path_or_file.endswith(".gz"): |
| 56 |
if self._indexed_gzip(path_or_file): |
| 57 |
return indexedGzip.IndexedGzip(path_or_file, self.encoding) |
| 58 |
else: |
| 59 |
return standardGzip.StandardGzip(path_or_file, self.encoding) |
| 60 |
return standardMzml.StandardMzml( |
| 61 |
path_or_file, |
| 62 |
self.encoding, |
| 63 |
self.build_index_from_scratch, |
| 64 |
index_regex=self.index_regex, |
| 65 |
) |
| 66 |
|
| 67 |
def _indexed_gzip(self, path): |
| 68 |
""" |
| 69 |
Check if the given file is an indexed gzip file or not. |
| 70 |
|
| 71 |
Arguments: |
| 72 |
path (str): path to the file |
| 73 |
|
| 74 |
Returns: |
| 75 |
bool : `True` if path is a gzip file with index, else `False` |
| 76 |
""" |
| 77 |
indexed = False |
| 78 |
indexed = GSGR.GSGR(path).indexed |
| 79 |
return indexed |
| 80 |
|
| 81 |
def read(self, size=-1): |
| 82 |
""" |
| 83 |
Read binary data from file handler. |
| 84 |
|
| 85 |
Keyword Arguments: |
| 86 |
size (int): Number of bytes to read from file, -1 to |
| 87 |
read to end of file |
| 88 |
|
| 89 |
Returns: |
| 90 |
data (str): byte string with defined size of the input data |
| 91 |
""" |
| 92 |
return self.file_handler.read(size) |
| 93 |
|
| 94 |
def __getitem__(self, identifier): |
| 95 |
""" |
| 96 |
Access the item with id 'identifier' in the file. |
| 97 |
|
| 98 |
Arguments: |
| 99 |
identifier (str): native id of the item to access |
| 100 |
|
| 101 |
Returns: |
| 102 |
data (str): text associated with the given identifier |
| 103 |
""" |
| 104 |
|
| 105 |
|
| 106 |
return self.file_handler[identifier] |
| 107 |
|
| 108 |
|
| 109 |
if __name__ == "__main__": |
| 110 |
print(__doc__) |