File: example_scripts.rst

package info (click to toggle)
python-pymzml 2.5.2%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,792 kB
  • sloc: python: 6,495; pascal: 341; makefile: 233; sh: 30
file content (251 lines) | stat: -rwxr-xr-x 5,344 bytes parent folder | download | duplicates (3)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
.. _example_scripts:

Example Scripts
================

Parsing a mzML file (new syntax)
--------------------------------

.. autofunction:: simple_parser.main

.. include:: code_inc/simple_parser.inc


Parsing a mzML file (old syntax)
--------------------------------

.. autofunction:: simple_parser_v2.main

.. include:: code_inc/simple_parser_v2.inc


Query the obo files
-------------------

.. autofunction:: queryOBO.main

.. include:: code_inc/queryOBO.inc


Plotting a chromatogram
-----------------------

.. autofunction:: plot_chromatogram.main

.. include:: code_inc/plot_chromatogram.inc


Plotting a spectrum
-------------------

.. autofunction:: plot_spectrum.main

.. include:: code_inc/plot_spectrum.inc


Plotting a spectrum with annotation
-----------------------------------

.. autofunction:: plot_spectrum_with_annotation.main

.. include:: code_inc/plot_spectrum_with_annotation.inc



Extracting highest peaks
------------------------

.. autofunction:: highest_peaks.main

.. include:: code_inc/highest_peaks.inc


Compare spectra
---------------

.. autofunction:: compare_spectra.main

.. include:: code_inc/compare_spectra.inc


Find m/z values
---------------

.. autofunction:: has_peak.main

.. include:: code_inc/has_peak.inc


Extract ion chromatogram
------------------------

.. autofunction:: extract_ion_chromatogram.main

.. include:: code_inc/extract_ion_chromatogram.inc


Find abundant precursors
------------------------

.. autofunction:: get_precursors.main

.. include:: code_inc/get_precursors.inc


Access polarity of spectra
--------------------------

.. autofunction:: polarity.main

.. include:: code_inc/polarity.inc


Check old to new function name mapping
--------------------------------------

.. autofunction:: deprecation_check.main

.. include:: code_inc/deprecation_check.inc


Convert mzML(.gz) to mzML.gz (igzip)
------------------------------------

.. autofunction:: gzip_mzml.main

.. include:: code_inc/gzip_mzml.inc


Multi threading conversion of mzML(.gz) to mzML.gz (igzip)
----------------------------------------------------------

.. autofunction:: multi_threading_file_compression.main

.. include:: code_inc/multi_threading_file_compression.inc


Acces run infos
---------------

.. autofunction:: access_run_info.main

.. include:: code_inc/access_run_info.inc



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 Conncetor,
		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