Skip to content

Commit

Permalink
Small performance optimization in spectrum.extreme_values() function,…
Browse files Browse the repository at this point in the history
… added example script for this function
  • Loading branch information
JB-MS committed Nov 15, 2018
1 parent 1894fb9 commit 24949dc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
51 changes: 51 additions & 0 deletions example_scripts/extreme_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python

import pymzml
from collections import defaultdict as ddict
import os


def main():
"""
Testscript to fetch the extreme m/z values from each spectrum
of an example file.
Usage:
./extreme_values.py
Parses the file '../tests/data/example.mzML' and extracts the smallest and
largest m/z from each each spectrum.
"""
example_file = os.path.join(
os.path.dirname(__file__),
os.pardir,
'tests',
'data',
'example.mzML'
)
run = pymzml.run.Reader(
example_file
)
extreme_mz_values = {}

number_of_mz_values = 2

for spectrum in run:
# print( spectrum.ID )
if spectrum.ms_level == 1:
extreme_mz_values[ spectrum.ID ] = spectrum.extreme_values('mz')

for spectrum_id, extreme_mz_tuple in extreme_mz_values.items():
assert len(extreme_mz_tuple) == number_of_mz_values
print(
'Spectrum {0}; lowest m/z: {1} highest m/z: {2}'.format(
spectrum_id,
*extreme_mz_tuple
)
)


if __name__ == '__main__':
main()
30 changes: 21 additions & 9 deletions pymzml/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,10 @@ def estimated_noise_level(self, mode='median'):
) / float(len(self.peaks('centroided')))
else:
print(
'dont understand noise level estimation method call', mode
'Do not understand noise level estimation method call with \
given mode: {0}'.format(
mode
)
)
return_value = self.noise_level_estimate[mode]
return return_value
Expand Down Expand Up @@ -1381,20 +1384,29 @@ def extreme_values(self, key):
extrema (tuple) : tuple of minimal and maximum m/z or intensity
"""
if key not in ['mz', 'i']:
print("Dont understand extreme request ")
available_extreme_values = ['mz', 'i']
if key not in available_extreme_values:
print(
"Do not understand extreme request: '{0}'; available values are: {1}".format(
key,
available_extreme_values
)
)
exit()
if self._extreme_values is None:
self._extreme_values = {}
try:
if key == 'mz':
all_mz_values = [mz for mz, i in self.peaks('raw')]
self._extreme_values['mz'] = (
min([mz for mz, i in self.peaks('raw')]),
max([mz for mz, i in self.peaks('raw')])
min(all_mz_values),
max(all_mz_values)
)
else:
self._extreme_values['i'] = (
min([i for mz, i in self.peaks('raw')]),
max([i for mz, i in self.peaks('raw')])
all_i_values = [i for mz, i in self.peaks('raw')]
self._extreme_values['i'] = (
min(all_i_values),
max(all_i_values)
)
except ValueError:
# emtpy spectrum
Expand Down Expand Up @@ -1483,7 +1495,7 @@ def similarity_to(self, spec2, round_precision=0):
to 1.
"""
assert isinstance(spec2, Spectrum), \
"Spectrum2 is not a pymzML spectrum"
"Spectrum 2 is not a pymzML spectrum"

vector1 = ddict(int)
vector2 = ddict(int)
Expand Down

0 comments on commit 24949dc

Please sign in to comment.