1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
.. pymzml_utils:
Utils
=====
.. General
.. ----------
.. These are the different APIS for supported file formats
.. Currently: mzML, mzML.gz, mzML.igzip
GSGW
----
.. autoclass:: pymzml.utils.GSGW.GSGW
:members:
:private-members:
:exclude-members: __del__, __enter__, __exit__, close
GSGR
----
.. autoclass:: pymzml.utils.GSGR.GSGR
:members:
:private-members:
:exclude-members: __enter__, __exit__, _read_until_zero, close
.. Creating a custom Filehandler
.. ------------------------------
.. Introduction
.. +++++++++++++
.. It is also possible to create an own API for different forms
.. of mzML files. For this, a new class needs to be written, which
.. implements a `read` and a `__getitem__` function.
.. Implementation of the API Class
.. ++++++++++++++++++++++++++++++++
Example::
.. class SQLiteDatabase(object):
.. """
.. Example implementation of a database Connector,
.. which can be used to make run accept paths to
.. sqlite db files.
.. We initialize with a path to a database and implement
.. a custom __getitem__ function to retrieve the spectra
.. """
.. def __init__(self, path):
.. """
.. """
.. connection = sqlite3.connect(path)
.. self.cursor = connection.cursor()
.. def __getitem__(self, key):
.. """
.. Execute a SQL request, process the data and return a spectrum object.
.. Args:
.. key (str or int): unique identifier for the given spectrum in the
.. database
.. """
.. self.cursor.execute('SELECT * FROM spectra WHERE id=?', key)
.. ID, element = self.cursor.fetchone()
.. element = et.XML(element)
.. if 'spectrum' in element.tag:
.. spectrum = spec.Spectrum(element)
.. elif 'chromatogram' in element.tag:
.. spectrum = spec.Chromatogram(element)
.. return spectrum
.. def get_spectrum_count(self):
.. self.cursor.execute("SELECT COUNT(*) from spectra")
.. num = self.cursor.fetchone()[0]
.. return num
.. def read(self, size=-1):
.. # implement read so it starts reading in first ID,
.. # if end reached switches to next id and so on ...
.. return '<spectrum index="0" id="controllerType=0 controllerNumber=1 scan=1" defaultArrayLength="917"></spectrum>\n'
.. Enabling the new API Class in File Interface
.. +++++++++++++++++++++++++++++++++++++++++++++
.. In order to make the run class accept the new file class, one need to edit
.. the :py:func:`_open` function in file_interface.py
Example::
.. def _open(self, path):
.. if path.endswith('.gz'):
.. if self._indexed_gzip(path):
.. self.file_handler = indexedGzip.IndexedGzip(path, self.encoding)
.. else:
.. self.file_handler = standardGzip.StandardGzip(path, self.encoding)
.. # Insert a new condition to enable your new fileclass
.. elif path.endswith('.db'):
.. self.file_handler = utils.SQLiteConnector.SQLiteDatabase(path, self.encoding)
.. else:
.. self.file_handler = standardMzml.StandardMzml(path, self.encoding)
.. return self.file_handler
.. Moby Dick as indexed Gzip
.. ---------------------------
.. Example of how to use the GSGW and GSGR class to create and access indexed Gzip files
.. .. code-block:: bash
.. python3 index_moby_dick.py
.. python3 read_moby_dick.py 10
.. Scripts
.. --------
.. pymzML ships with some usefull scripts, which are described in the following
.. Create indexed gziped conveniently
.. +++++++++++++++++++++++++++++++++++
.. blub
.. Find optimal read size
.. +++++++++++++++++++++++
.. Hard drives have different sector sizes, ....
.. utils -- A collection of useful functions
.. ++++++++++++++++++++++++++++++++++++++++++
.. blub
|