#!usr/bin/env python3
# -*- coding: latin-1 -*-
"""
The spectrum class offers a python object for mass spectrometry data.
The spectrum object holds the basic information on the spectrum and offers
methods to interrogate properties of the spectrum.
Data, i.e. mass over charge (m/z) and intensity decoding is performed on demand
and can be accessed via their properties, e.g. :py:attr:`~pymzml.spec.Spectrum.peaks`.
The Spectrum class is used in the :py:class:`~pymzml.run.Reader` class.
There each spectrum is accessible as a spectrum object.
Theoretical spectra can also be created using the setter functions.
For example, m/z values, intensities, and peaks can be set by the
corresponding properties: :py:attr:`spec.Spectrum.mz`,
:py:attr:`spec.Spectrum.i`, :py:attr:`pymzml.spec.Spectrum.peaks`.
Similiar to the spectrum class, the chromatogram class allows interrogation with profile data
(time, intensity) in an total ion chromatogram etc.
"""
#
# pymzml
#
# Copyright (C) 2016 M. Kösters, C. Fufezan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
from base64 import b64decode as b64dec
from struct import unpack
try:
import numpy as np
except:
global np
np = None
from collections import defaultdict as ddict
import math
import re
import pymzml.obo
from operator import itemgetter as itemgetter
import sys
import zlib
import xml.etree.ElementTree as ElementTree
import pymzml.regex_patterns as regex_patterns
try:
# print('MSNumpress decoding activated')
import PyMSNumpress as PyNump
except:
# print('cant find PyMSNumpress, falling back to slower python code')
pass
PROTON = 1.00727646677
# use this as a base class for spectrum and chromatogram
# class PeakDict(dict):
# def __init__(self, *args, **kwargs):
# # self.update(*args, **kwargs)
# super(PeakDict, self).__setitem__('raw', None)
# super(PeakDict, self).__setitem__('centroided', None)
# super(PeakDict, self).__setitem__('reprofiled', None)
# def __getitem__(self, key):
# """
# """
# if key not in ['raw', 'centroided', 'reprofiled']:
# raise KeyError('Can only access raw, cetroided and reprofiled')
# else:
# if super(PeakDict, self).__getitem__(key) is None:
# if key is 'raw':
# super(PeakDict, self).__setitem__(key, self._decode('peaks'))
# elif key is 'centroided':
# if super(PeakDict, self).__getitem__('raw') is None:
# super(PeakDict, self).__setitem__('raw', self._decode('peaks'))
# super(PeakDict, self).__setitem__(key, self._centroid_peaks('peaks'))
# elif key is 'reprofiled':
# if super(PeakDict, self).__getitem__('raw') is None:
# super(PeakDict, self).__setitem__('raw', self._decode('peaks'))
# super(PeakDict, self).__setitem__(key, self._reprofile_peaks('peaks'))
# return super(PeakDict, self).__getitem__(key)
# def __setitem__(self, key, val):
# """
# """
# if key not in ['raw', 'centroided', 'reprofiled']:
# raise Exception('Can only set raw, centroided or reprofiled')
# elif not isinstance(val, (list, np.ndarray, type(None))):
# raise Exception('Value has to be either list or numpy array')
# else:
# print('set {} == {}'.format(key, val))
# super(PeakDict, self).__setitem__(key, val)
# def _centroid_peaks(self, peaks):
# """
# """
# return ['centroided']
# def _reprofile_peaks(self, peaks):
# """
# """
# return ['reprofiled']
# def _decode(self, peaks):
# """
# """
# return ['decode']
[docs]class MS_Spectrum(object):
def __init__(self):
pass
def _read_accessions(self):
"""
Set all required variables for this spectrum.
"""
self.accessions = {}
for element in self.element.getiterator():
accession = element.get('accession')
name = element.get('name')
if accession is not None:
self.accessions[name] = accession
if 'profile spectrum' in self.accessions.keys():
self._profile = True
[docs] def get_element_by_name(self, name, obo_version=None):
"""
Get element from the original tree by it's unit name.
Arguments:
name (str): unit name of the mzml element.
Keyword Arguments:
obo_version (str, optional): obo version number.
"""
iterator = self.element.getiterator()
return_ele = None
for ele in iterator:
if ele.get('name', default=None) == name:
return_ele = ele
break
return return_ele
[docs] def get_element_by_path(self, hooks):
"""
Find elements in spectrum by its path.
Arguments:
hooks (list): list of parent elements for the target element.
Returns:
elements (list): list of XML objects
found in the path
Example:
To access cvParam in scanWindow tag:
>>> spec.get_element_by_path(['scanList', 'scan', 'scanWindowList',
... 'scanWindow', 'cvParam'])
"""
return_ele = None
if len(hooks) > 0:
path_array = ['.']
for hook in hooks:
path_array.append('{ns}{hook}'.format(ns=self.ns, hook=hook))
path = '/'.join(path_array)
return_ele = self.element.findall(path)
return return_ele
# one return per function is your friend :)
def _register(self, decoded_tuple):
d_type, array = decoded_tuple
if d_type == 'mz':
self._mz = array
elif d_type == 'i':
self._i = array
elif d_type == 'time':
self._time = array
else:
raise Exception('Unknown data Type ({0})'.format(d_type))
@property
def precursors(self):
"""
Find the precursor ID of this spectrum.
Returns:
precursor(list): list of precursor ids for this spectrum.
"""
if self._precursors is None:
precursors = self.element.findall(
'./{ns}precursorList/{ns}precursor'.format(ns=self.ns)
)
self._precursors = []
for prec in precursors:
spec_ref = prec.get('spectrumRef')
self._precursors.append(
regex_patterns.SPECTRUM_ID_PATTERN.search(
spec_ref
).group(0)
)
return self._precursors
def _get_encoding_parameters(self, array_type):
"""
Find the correct parameter required for decoding and return them as
a tuple.
Arguments:
array_type (str): data type of the array, e.g. m/z, time or
itensity
Returns:
data (str) : encoded data
comp (str) : compression method
fType (str) : float precision
d_array_length (str) : length of the data arrray
"""
numpress_encoding = False
array_type_accession = self.calling_instance.OT[array_type]['id']
b_data_string ="./{ns}binaryDataArrayList/{ns}binaryDataArray/{ns}cvParam[@accession='{Acc}']/..".format(
ns=self.ns, Acc=array_type_accession
)
float_type_string = "./{ns}cvParam[@accession='{Acc}']"
b_data_array = self.element.find(b_data_string)
comp = []
for cvParam in b_data_array.iterfind("./{ns}cvParam".format(ns=self.ns)):
if 'compression' in cvParam.get('name'):
if 'numpress' in cvParam.get('name').lower():
numpress_encoding = True
comp.append(cvParam.get('name'))
d_array_length = self.element.get('defaultArrayLength')
if not numpress_encoding:
try:
# 32-bit float
f_type = b_data_array.find(
float_type_string.format(
ns=self.ns,
Acc=self.calling_instance.OT['32-bit float']['id']
)
).get('name')
except:
# 64-bit Float
f_type = b_data_array.find(
float_type_string.format(
ns=self.ns,
Acc=self.calling_instance.OT['64-bit float']['id']
)
).get('name')
else:
# compression is numpress, dont need floattype here
f_type = None
data = b_data_array.find(
"./{ns}binary".format(
ns=self.ns
)
).text.encode("utf-8")
return (data, d_array_length, f_type, comp)
@property
def measured_precision(self):
"""
Sets the measured and internal precision
Returns:
value (float): measured Precision (e.g. 5e-6)
"""
return self._measured_precision
@measured_precision.setter
def measured_precision(self, value):
self._measured_precision = value
self.internal_precision = int(round(50000.0 / (value * 1e6)))
return
def _decode_to_numpy(self, data, d_array_length, float_type, comp):
"""
Decodes the base 64 encoded and packed strings from the data as numpy
arrays
Returns:
data (np.ndarray): Returns the unpacked data as a tuple. Returns an
empty list if there is no raw data or raises an
exception if data could not be decoded.
d_array_length just for compatibility
"""
out_data = b64dec(data)
if 'zlib' in comp or \
'zlib compression' in comp:
out_data = zlib.decompress(out_data)
if 'ms-np-linear' in comp or\
'ms-np-pic' in comp or\
'ms-np-slof' in comp or\
'MS-Numpress linear prediction compression' in comp or\
'MS-Numpress short logged float compression' in comp:
print('Data is Numpress encoded')
out_data = self._decodeNumpress_to_array(out_data, comp)
# else:
# print(
# 'New data compression ({0}) detected, cant decompress'.format(
# comp
# )
# )
# sys.exit(1)
if float_type == '32-bit float':
# one character code may be sufficient too (f)
f_type = np.float32
out_data = np.fromstring(out_data, f_type)
elif float_type == '64-bit float':
# one character code may be sufficient too (d)
f_type = np.float64
out_data = np.fromstring(out_data, f_type)
return out_data
def _decode_to_tuple(self, data, float_type, d_array_length, comp):
"""
Decodes the base 64 encoded and packed strings from the data.
Returns:
data (tuple): Returns the unpacked data as a tuple. Returns an
empty list if
there is no raw data or raises an exception if data
could not be
decoded.
"""
dec_data = b64dec(data)
if 'zlib' in comp:
data = zlib.decompress(dec_data)
if set(['ms-np-linear', 'ms-np-pic', 'ms-np-slof']) & set(comp):
print('bla')
self._decodeNumpress(data, comp)
# else:
# print(
# 'New data compression ({0}) detected, cant decompress'.format(
# comp
# )
# )
# sys.exit(1)
print('ld', len(data), 'ft', float_type, 'dal', d_array_length, comp)
if float_type == '32-bit float':
f_type = 'f'
elif float_type == '64-bit float':
f_type = 'd'
fmt = "{endian}{array_length}{float_type}".format(
endian="<",
array_length=d_array_length,
float_type=f_type
)
print(data)
print(fmt)
return unpack(fmt, dec_data)
def _decodeNumpress_to_array(self, data, compression):
"""
Decodes numpress encoded data.
Arguments:
data (str) : Input string, numpress compressed
compression (str) : Decompression algorithm to be used
(valid are 'ms-np-linear', 'ms-np-pic', 'ms-np-slof')
Returns:
array (list): Returns the unpacked data as an array of floats.
TODO:
implement numpress decoding and encoding without pyopenms
"""
result = []
comp_ms_tags = [self.calling_instance.OT[comp]['id'] for comp in compression]
data = np.frombuffer(data, dtype=np.uint8)
print()
print(dir(pymzml.MSDecoder))
if 'MS:1002312' in comp_ms_tags:
result = pymzml.MSDecoder.decode_linear(data)
elif 'MS:1002313' in comp_ms_tags:
PyNump.decodePic(data, result)
result = pymzml.MSDecoder.decode_pic(data)
elif 'MS:1002314' in comp_ms_tags:
PyNump.decodeSlof(data, result)
result = pymzml.MSDecoder.decode_slof(data)
return result
def _median(self, data):
"""
Compute median.
Arguments:
data (list): list of numeric values
Returns:
median (float): median of the input data
"""
'NEEDS Numpy version'
if np:
median = np.median(data)
else:
if len(data) == 0:
return None
data.sort()
l = len(data)
if not l % 2:
median = (
data[int(math.floor(float(l) / float(2)))] \
+ data[int(math.ceil(float(l) / float(2)))]
) / float(2.0)
else:
median = data[int(l / 2)]
return median
[docs] def to_string(self, encoding='latin-1', method='xml'):
"""
Return string representation of the xml element the
spectrum was initialized with.
Keyword Arguments:
encoding (str) : text encoding of the returned string.\n
Default is latin-1.
method (str) : text format of the returned string.\n
Default is xml, alternatives are html and text.
Returns:
element (str) : xml string representation of the spectrum.
"""
# Tree = ElementTree.ElementTree(self.element)
return ElementTree.tostring(
self.element, encoding=encoding, method=method
)
[docs]class Spectrum(MS_Spectrum):
"""
Arguments:
element (xml.etree.ElementTree.Element): spectrum as xml Element
Keyword Arguments:
measured_precision (float): in ppm, i.e. 5e-6 equals to 5 ppm.
param (dict): parameter mapping for this spectrum.
"""
def __init__(self, element = None, measured_precision = 5e-6):
__slots__ = [
"_measured_precision",
"element",
"noise_level_estimate",
"_time",
"_i",
"_t_mass_set",
"_peaks",
"_t_mz_set",
"_centroided_peaks",
"_reprofiled_peaks",
"_deconvoluted_peaks",
"_profile",
"_extreme_values",
"_centroided_peaks_sorted_by_i",
"_transformed_mz_with_error",
"_transformed_mass_with_error",
"_precursors",
"_ID",
"internal_precision"
"_transformed_peaks"
"calling_instance"
]
self.measured_precision = measured_precision
self.element = element
self.noise_level_estimate = {}
self.calling_instance = None
self._peak_dict = {
'raw' : None,
'centroided' : None,
'reprofiled' : None
}
self._i = None
self._mz = None
self._TIC = None
self._t_mass_set = None
self._t_mz_set = None
self._centroided_peaks = None
self._reprofiled_peaks = None
self._profile = None
self._extreme_values = None
self._centroided_peaks_sorted_by_i = None
self._transformed_mz_with_error = None
self._transformed_mass_with_error = None
self._transformed_peaks = None
self._precursors = None
self._ID = None
self._ms_level = None
if self.element:
self.ns = re.match(
'\{.*\}', element.tag
).group(0) if re.match('\{.*\}', element.tag) else ''
if np is None:
self._decode = self._decode_to_tuple
self._array = list
else:
self._decode = self._decode_to_numpy
self._array = np.array
def __add__(self, other_spec):
"""
Adds two pymzml spectra together.
Arguments:
other_spec (Spectrum): spectrum to add to the current spectrum
Returns:
self (Spectrum): reference to the edited spectrum
Example:
>>> import pymzml
>>> s = pymzml.spec.Spectrum( measuredPrescision = 20e-6 )
>>> file_to_read = "../mzML_example_files/xy.mzML.gz"
>>> run = pymzml.run.Reader(
... file_to_read ,
... MS1_Precision = 5e-6 ,
... MSn_Precision = 20e-6
... )
>>> for spec in run:
... s += spec
"""
assert isinstance(other_spec, Spectrum)
if self._peak_dict['reprofiled'] is None:
self.set_peaks(self._reprofile_Peaks(), 'reprofiled')
for mz, i in other_spec.peaks('reprofiled'):
self._peak_dict['reprofiled'][mz] += i
# self.set_peaks(None, 'reprofiled')
# self.set_peaks(None, 'reprofiled')
return self
def __sub__(self, other_spec):
"""
Substracts two pymzml spectra.
Arguments:
otherSpec (spec.Spectrum): spectrum to substract from the current
spectrum
Returns:
self (spec.Spectrum): returns self after otherSpec was substracted
"""
assert isinstance(other_spec, Spectrum)
if self._peak_dict['reprofiled'] is None:
self.set_peaks(self._reprofile_Peaks(), 'reprofiled')
for mz, i in other_spec.peaks('reprofiled'):
self._peak_dict['reprofiled'][mz] -= i
self.set_peaks(None, 'centroided')
self.set_peaks(None, 'raw')
return self
def __mul__(self, value):
"""
Multiplies each intensity with a float, i.e. scales the spectrum.
Arguments:
value (int, float): value to multiply the intensities with
Returns:
self (spec.Spectrum): returns self after intensities were scaled
by value
"""
assert isinstance(value, (int, float))
if self._peak_dict['raw'] is not None:
if np is None:
self.set_peaks(
[(mz, i * float(value)) for mz, i in self.peaks],
'raw'
)
else:
self.set_peaks(
np.column_stack(
(self.peaks('raw')[:, 0], self.peaks('raw')[:, 1] * value)
),
'raw'
)
if self._peak_dict['centroided'] is not None:
if np is None:
self.set_peaks(
[(mz, i * float(value)) for mz, i in self.centroided_peaks],
'centroided'
)
else:
self.set_peaks(
np.column_stack(
(
self.centroided_peaks[:, 0],
self.centroided_peaks[:, 1] * value
)
),
'centroided'
)
if self._peak_dict['reprofiled'] is not None:
if np is None:
for mz in self._peak_dict['reprofiled'].keys():
self._peak_dict['reprofiled'][mz] *= float(value)
else:
# TODO more efficient version with numpy
pass
return self
def __truediv__(self, value):
"""
Divides each intensity by a float, i.e. scales the spectrum.
Arguments:
value (int, float): value to divide the intensities by
Returns:
self (spec.Spectrum): returns self after intensities were scaled
by value.
"""
assert isinstance(value, (int, float)), ''
if self._peak_dict['raw'] is not None:
if np is None:
self.set_peaks([(mz, i / float(value)) for mz, i in self.peaks], 'raw')
else:
print(type(self.peaks('raw')))
print(self.peaks('raw')[:5])
self.set_peaks(np.column_stack(
(self.peaks('raw')[:, 0], self.peaks('raw')[:, 1] / float(value))
), 'raw'
)
if self._peak_dict['centroided'] is not None:
if np is None:
self.set_peaks(
[
(mz, i / float(value)) for mz, i in self.centroided_peaks
],
'centroided'
)
else:
self.set_peaks(
np.column_stack(
(
self.centroided_peaks[:, 0],
self.centroided_peaks[:, 1] / float(value)
)
),
'centroided'
)
if self._peak_dict['reprofiled'] is not None:
if np is None:
for mz in self.peak_dict['reprofiled'].keys():
self.peak_dict['reprofiled'][mz] /= float(value)
else:
pass
return self
def __div__(self, value):
"""
Integer division is the same as __truediv__ for this class
"""
return self.__truediv__(value)
def __repr__(self):
"""
"""
return '<__main__.Spectrum object with native ID {0} at {1}>'.format(
self.ID, hex(id(self))
)
def __str__(self):
"""
"""
return '<__main__.Spectrum object with native ID {0} at {1}>'.format(
self.ID, hex(id(self))
)
def __getitem__(self, accession):
"""
"""
#TODO implement cache???
if accession == 'id':
return_val = self.ID
else:
if not accession.startswith('MS:'):
accession = self.calling_instance.OT[accession]['id']
search_string = './/*[@accession="{0}"]'.format(accession)
elements = []
for x in self.element.iterfind(search_string):
val = x.attrib.get('value')
try:
val = float(val)
except:
pass
elements.append(val)
if len(elements) == 0:
return_val = None
elif len(elements) == 1:
return_val = elements[ 0 ]
else:
return_val = elements
return return_val
# Properties, setter and getter
@property
def measured_precision(self):
"""
Sets the measured and internal precision
Returns:
value (float): measured Precision (e.g. 5e-6)
"""
return self._measured_precision
@measured_precision.setter
def measured_precision(self, value):
self._measured_precision = value
self.internal_precision = int(round(50000.0 / (value * 1e6)))
return
@property
def t_mz_set(self):
"""
Create set out of transformed m/z values
(including all values in the defined imprecision).
Returns:
t_mz_set (set): set of transformed m/z values
"""
if self._t_mz_set is None:
self._t_mz_set = set()
for mz, i in self.peaks('centroided'):
self._t_mz_set |= set(
range(
int(round(
(mz - (mz * self.measured_precision)) * self.internal_precision
)),
int(round(
(mz + (mz * self.measured_precision)) * self.internal_precision)) + 1)
)
return self._t_mz_set
@property
def transformed_mz_with_error(self):
"""
Returns transformed m/z value with error
Returns:
tmz values (dict): Transformed m/z values in dictionary\n
{\n
m/z_with_error : [(m/z,intensity), ...], ...\n
}\n
"""
if self._transformed_mz_with_error is None:
self._transformed_mz_with_error = ddict(list)
for mz, i in self.peaks('centroided'):
for t_mz_with_error in range(
int(round((mz - (mz * self.measured_precision)) * self.internal_precision)),
int(round((mz + (mz * self.measured_precision)) * self.internal_precision)) + 1):
self._transformed_mz_with_error[t_mz_with_error].append((mz, i))
return self._transformed_mz_with_error
@property
def transformed_peaks(self):
"""
m/z value is multiplied by the internal precision.
Returns:
Transformed peaks (list): Returns a list of peaks (tuples of mz and
intensity).
Float m/z values are adjusted by the internal precision
to integers.
"""
if self._transformed_peaks is None:
self._transformed_peaks = [
(self.transform_mz(mz), i) for mz, i in self.peaks('centroided')
]
return self._transformed_peaks
@property
def TIC(self):
"""
Property to access the total ion current for this spectrum.
Returns:
TIC (float): Total Ion Current of the spectrum.
"""
if self._TIC is None:
self._TIC = float(
self.element.find(
"./{ns}cvParam[@accession='MS:1000285']".format(
ns=self.ns
)
).get('value')
)# put hardcoded MS tags in minimum.py???
return self._TIC
@property
def ID(self):
"""
Access the native id (last number in the id attribute) of the spectrum.
Returns:
ID (str): native ID of the spectrum
"""
if self._ID is None:
self._ID = regex_patterns.SPECTRUM_ID_PATTERN.search(
self.element.get('id')
).group(0)
try:
self._ID = int(self._ID)
except:
pass
return self._ID
@property
def ms_level(self):
"""
Property to access the ms level.
Returns:
ms_level (int):
"""
if self._ms_level is None:
self._ms_level = self.element.find(
"./{ns}cvParam[@accession='MS:1000511']".format(
ns=self.ns
)
).get('value') # put hardcoded MS tags in minimum.py???
return int(self._ms_level)
@property
def mz(self):
"""
Returns the list of m/z values. If the m/z values are encoded, the
function :func:`~spec.MS_Spectrum._decode` is used to decode the encoded data.
The mz property can also be set, e.g. for theoretical data.
However, it is recommended to use the peaks property to set mz and
intensity tuples at same time.
Returns:
mz (list): list of m/z values of spectrum.
"""
if self._mz is None:
params = self._get_encoding_parameters('m/z array')
self._mz = self._decode(*params)
return self._mz
@mz.setter
def mz(self, mz_list):
''''''
if np:
mz_list = np.array(mz_list, dtype=np.float64)
mz_list.sort()
self._mz = mz_list
else:
assert isinstance(mz_list, list)
self._mz = sorted(mz_list)
@property
def i(self):
"""
Returns the list of the intensity values.
If the intensity values are encoded, the function :func:`~spec.MS_Spectrum._decode`
is used to decode the encoded data.\n
The i property can also be setted, e.g. for theoretical data. However,
it is recommended to use the peaks property to set mz and intesity
tuples at same time.
Returns
i (list): list of intensity values from the analysed spectrum
"""
if self._i is None:
params = self._get_encoding_parameters('intensity array')
self._i = self._decode(*params)
return self._i
@i.setter
def i(self, intensity_list):
if np:
intensity_list = np.array(intensity_list)
else:
assert isinstance(intensity_list, list)
self._i = intensity_list
[docs] def peaks(self, peak_type):
"""
As Property or function
"""
if self._peak_dict[peak_type] is None:
if self._peak_dict['raw'] is None:
self._peak_dict['raw'] = []
mz_params = self._get_encoding_parameters('m/z array')
i_params = self._get_encoding_parameters('intensity array')
mz = self._decode(*mz_params)
i = self._decode(*i_params)
# self._peak_dict['raw'] = np.ndarray(len(mz), dtype=tuple)
for pos, mz_val in enumerate(mz):
self._peak_dict['raw'].append((mz_val, i[pos]))
# self._peak_dict['raw'][pos] = [mz, 1]
if peak_type is 'raw':
pass
elif peak_type is 'centroided':
self._peak_dict['centroided'] = self._centroid_peaks()
elif peak_type is 'reprofiled':
self._peak_dict['reprofiled'] = self._reprofile_Peaks()
else:
raise KeyError
peaks = self._array(self._peak_dict[peak_type])
if peak_type is 'reprofiled':
peaks = list(self._peak_dict[peak_type].items())
peaks.sort(key=itemgetter(0))
return peaks
[docs] def set_peaks(self, peaks, peak_type):
"""
"""
peak_type = peak_type.lower()
if peak_type == 'raw':
self._peak_dict['raw'] = peaks
self._mz = [mz for mz, i in self.peaks('raw')]
self._i = [i for mz, i in self.peaks('raw')]
elif peak_type == 'centroided':
self._peak_dict['centroided'] = peaks
self._mz = [mz for mz, i in self.peaks('raw')]
self._i = [i for mz, i in self.peaks('raw')]
elif peak_type == 'reprofiled':
try:
self._peak_dict['reprofiled'] = dict(peaks)
except TypeError:
self._peak_dict['reprofiled'] = None
else:
raise Exception(
'Peak type is not suppported\n'
'Choose either "raw", "centroided" or "reprofiled"'
)
def _centroid_peaks(self):
"""
Perform a Gauss fit to centroid the peaks for the property
centroided_peaks.
Returns:
centroided_peaks (list): list of centroided m/z, i tuples
"""
if self['MS:1000128']: # check if spec is a profile spec
tmp = []
if self._peak_dict['reprofiled'] is not None:
i_array = [i for mz, i in self.peaks('reprofiled')]
mz_array = [mz for mz, i in self.peaks('reprofiled')]
else:
i_array = self.i
mz_array = self.mz
for pos, i in enumerate(i_array[:-1]):
if pos <= 1:
continue
if 0 < i_array[pos - 1] < i > i_array[pos + 1] > 0:
x1 = float(mz_array[pos - 1])
y1 = float(i_array[pos - 1])
x2 = float(mz_array[pos])
y2 = float(i_array[pos])
x3 = float(mz_array[pos + 1])
y3 = float(i_array[pos + 1])
if x2 - x1 > (x3 - x2) * 10 or (x2 - x1) * 10 < x3 - x2:
continue
if y3 == y1:
before = 3
after = 4
while y1 == y3 and after < 10: # we dont want to go too far
if pos - before < 0:
lower_pos = 0
else:
lower_pos = pos - before
if pos + after >= len(mz_array):
upper_pos = len(mz_array) - 1
else:
upper_pos = pos + after
x1 = mz_array[lower_pos]
y1 = i_array[lower_pos]
x3 = mz_array[upper_pos]
y3 = i_array[upper_pos]
if before % 2 == 0:
after += 1
else:
before += 1
try:
double_log = math.log(y2 / y1) / math.log(y3 / y1)
mue = (double_log * (x1 * x1 - x3 * x3) - x1 * x1 + x2\
* x2) / (2 * (x2 - x1) - 2 * double_log * \
(x3 - x1))
c_squarred = (x2 * x2 - x1 * x1 - 2 * x2 * mue \
+ 2 * x1 * mue) / (2 * math.log(y1 / y2)
)
A = y1 * math.exp((x1 - mue) * (x1 - mue) \
/ (2 * c_squarred))
except:
continue
tmp.append((mue, A))
return tmp
else:
return self.peaks('raw')
def _reprofile_Peaks(self):
"""
Performs reprofiling for property reprofiled_peaks.
Returns:
reprofiled_peaks (list): list of reprofiled m/z, i tuples
"""
tmp = ddict(int)
for mz, i in self.peaks('centroided'):
# Let the measured precision be 2 sigma of the signal width
# When using normal distribution
# FWHM = 2 sqt(2 * ln(2)) sigma = 2.3548 sigma
s = mz * self.measured_precision * 2 # in before 2
s2 = s * s
floor = mz - 5.0 * s # Gauss curve +- 3 sigma
ceil = mz + 5.0 * s
ip = self.internal_precision / 4
# more spacing, i.e. less points describing the gauss curve
# -> faster adding
for _ in range(int(round(floor * ip)), int(round(ceil * ip)) + 1):
if _ % int(5) == 0:
a = float(_) / float(ip)
y = i * math.exp(-1 * ((mz - a) * (mz - a)) / (2 * s2))
tmp[a] += y
self.reprofiled = True
return tmp
def _register(self, decoded_tuple):
d_type, array = decoded_tuple
if d_type == 'mz':
self._mz = array
elif d_type == 'i':
self._i = array
elif d_type == 'time':
self._time = array
else:
raise Exception('Unknown data Type ({0})'.format(d_type))
def _mz_2_mass(self, mz, charge):
"""
Calculate the uncharged mass for a given mz value
Arguments:
mz (float) : m/z value
charge(int) : charge
Returns:
mass (float): Returns mass of a given m/z value
"""
return ((mz - PROTON) * charge)
# Public functions
[docs] def reduce(self, mz_range=(None, None)):
"""
Remove all mz values outside the given range.
Arguments:
mz_range (tuple): tuple of min, max values
Returns:
peaks (list): list of mz, i tuples in the given range.
"""
assert isinstance(mz_range, tuple), \
"require tuple of (min,max) mz range to reduce spectrum"
if mz_range != (None, None):
tmp_peaks = []
for mz, i in self.peaks('raw'):
if mz < mz_range[0]:
continue
elif mz > mz_range[1]:
break
else:
tmp_peaks.append((mz, i))
self.set_peaks(tmp_peaks, 'raw')
return self.peaks('raw')
[docs] def remove_noise(self, mode='median', noise_level=None):
"""
Function to remove noise from peaks, centroided peaks and reprofiled
peaks.
Keyword arguments:
mode (str): define mode for removing noise. Default = "median"
(other modes: "mean", "mad")
noise_level (float): noise Threshold
Returns:
reprofiled peaks (list): Returns a list with tuples of
m/z-intensity pairs above the noise threshold
"""
# Thanks to JD Hogan for pointing it out!
callPeaks = self.peaks('raw')
callcentPeaks = self.peaks('centroided')
if noise_level is None:
noise_level = self.estimated_noise_level(mode=mode)
if self._peak_dict['centroided'] is not None:
self._peak_dict['centroided'] = [
(mz, i) for mz, i in self.peaks('centroided') if i >= noise_level
]
if self._peak_dict['raw'] is not None:
self._peak_dict['raw'] = [(mz, i) for mz, i in self.peaks('raw') if i >= noise_level]
self._peak_dict['reprofiled'] = None
return self
[docs] def estimated_noise_level(self, mode='median'):
"""
Calculates noise threshold for function remove_noise.
Keyword Arguments:
mode (str): define mode for removing noise. Default = "median"
(other modes: "mean", "mad")
Returns:
noiseLevel (float): estimate noise threshold.
"""
if self.peaks('centroided') == []: # or is None?
return 0
self.noise_level_estimate = {}
if mode not in self.noise_level_estimate.keys():
if mode == 'median':
self.noise_level_estimate['median'] = self._median(
[ i for mz, i in self.peaks('centroided')]
)
elif mode == 'mad':
median = self.estimated_noise_level(mode='median')
self.noise_level_estimate['mad'] = self._median(
sorted(
[abs(i - median) for mz, i in self.peaks('centroided')])
)
elif mode == 'mean':
self.noise_level_estimate['mean'] = sum(
[i for mz, i in self.peaks('centroided')]
) / float(len(self.peaks('centroided')))
else:
print(
'dont understand noise level estimation method call', mode
)
return self.noise_level_estimate[mode]
[docs] def highest_peaks(self, n):
"""
Function to retrieve the n-highest centroided peaks of the spectrum.
Arguments:
n (int): number of highst peaks to return.
Returns:
centroided peaks (list): list mz, i tupls with n-highest
centroided peaks
Example:
>>> run = pymzml.run.Reader(
... "../tests/data/example.mzML.gz",
... MS1_Precision = 5e-6,
... MSn_Precision = 20e-6
... )
>>> for spectrum in run:
... if spectrum.ms_level == 2:
... if spectrum["id"] == 1770:
... for mz,i in spectrum.highest_peaks(5):
... print(mz,i)
"""
if self._centroided_peaks_sorted_by_i is None:
self._centroided_peaks_sorted_by_i = sorted(
self.peaks('centroided'), key=itemgetter(1)
)
return self._centroided_peaks_sorted_by_i[-n:]
[docs] def ppm2abs(self, value, ppmValue, direction=1, factor=1):
"""
Returns the value plus (or minus, dependent on direction) the
imprecession for this value.
Arguments:
value (float) : m/z value
ppmValue (int) : ppm value
Keyword Arguments:
direction (int) : plus or minus the considered m/z value.
The argument *direction* should be 1 or -1
factor (int) : multiplication factor for the imprecision.
The argument *factor* should be bigger than 0
Returns:
imprecision (float): imprecision for the given value
"""
result = value + (value * (ppmValue * factor)) * direction
return result
[docs] def extreme_values(self, key):
"""
Find extreme values, minimal and maximum mz and intensity
Arguments:
key (str) : m/z : "mz" or intensity : "i"
Returns:
extrema (tuple) : tuple of minimal and maximum m/z or intensity
"""
if key not in ['mz', 'i']:
print("Dont understand extreme request ")
if self._extreme_values is None:
self._extreme_values = {}
try:
if key == 'mz':
self._extreme_values['mz'] = (
min([mz for mz, i in self.peaks('raw')]),
max([mz for mz, i in self.peaks('raw')])
)
else:
self._extreme_values['i'] = (
min([i for mz, i in self.peaks('raw')]),
max([i for mz, i in self.peaks('raw')])
)
except ValueError:
# emtpy spectrum
self._extreme_values[key] = ()
return self._extreme_values[key]
[docs] def has_peak(self, mz2find):
"""
Checks if a Spectrum has a certain peak.
Needs a certain mz value as input and returns a list of peaks if a peak
is found in the spectrum, otherwise ``[]`` is returned.
Every peak is a tuple of m/z and intensity.
Arguments:
mz2find (float): mz value which should be found
Returns:
peaks (list): list of mz, i tuples
Example:
>>> import pymzml
>>> example_file = '../tests/data/example.mzML'
/// )
>>> run = pymzml.run.Reader(
... example_file,
... MS1_Precision = 5e-6,
... MSn_Precision = 20e-6
... )
>>> for spectrum in run:
... if spectrum.ms_level == 2:
... peak_to_find = spectrum.has_peak(1016.5404)
... print(peak_to_find)
[(1016.5404, 19141.735187697403)]
"""
value = self.transform_mz(mz2find)
return self.transformed_mz_with_error[value]
[docs] def has_overlapping_peak(self, mz):
"""
Checks if a spectrum has more than one peak for a given m/z value
and within the measured precision
Arguments:
mz (float): m/z value which should be checked
Returns:
Boolean (bool): Returns ``True`` if a nearby peak is detected,
otherwise ``False``
"""
for minus_or_plus in [-1, 1]:
target = self.ppm2abs(
mz, self.measured_precision, minus_or_plus, 1
)
temp = self.has_peak(self.ppm2abs(mz, self.measured_precision))
if temp and len(temp) > 1:
return True
return False
def deprecation_warning(self, function_name):
deprecation_lookup = {
'similarityTo' : 'similarity_to',
'hasPeak' : 'has_peak',
}
print(
'''
Function: "{0}" deprecated since version 1.2.0, please use new function: "{1}"
'''.format(
function_name,
deprecation_lookup.get(
function_name,
'not_defined_yet'
)
)
)
def similarityTo(self, spec2, round_precision=0):
self.deprecation_warning( sys._getframe().f_code.co_name )
return self.similarity_to( spec2, round_precision = round_precision)
def hasPeak(self, mz):
self.deprecation_warning( sys._getframe().f_code.co_name )
return self.has_peak( mz )
[docs] def similarity_to(self, spec2, round_precision=0):
"""
Compares two spectra and returns cosine
Arguments:
spec2 (Spectrum): another pymzml spectrum that is compared to the
current spectrum.
Keyword Arguments:
round_precision (int): precision mzs are rounded to, ie round( mz,
round_precision )
Returns:
cosine (float): value between 0 and 1, i.e. the cosine between the
two spectra.
Note:
Spectra data is transformed into an n-dimensional vector,
whereas mz values are binned in bins of 10 m/z and the intensities
are added up. Then the cosine is calculated between those two
vectors. The more similar the specs are, the closer the value is
to 1.
"""
assert isinstance(spec2, Spectrum), \
"Spectrum2 is not a pymzML spectrum"
vector1 = ddict(int)
vector2 = ddict(int)
mzs = set()
for mz, i in self.peaks('raw'):
vector1[round(mz, round_precision)] += i
mzs.add(round(mz, round_precision))
for mz, i in spec2.peaks('raw'):
vector2[round(mz, round_precision)] += i
mzs.add(round(mz, round_precision))
# print(sorted(vector1.keys()), sorted(vector2.keys()))
z = 0
n_v1 = 0
n_v2 = 0
for mz in mzs:
int1 = vector1[mz]
int2 = vector2[mz]
z += int1 * int2
n_v1 += int1 * int1
n_v2 += int2 * int2
try:
cosine = z / (math.sqrt(n_v1) * math.sqrt(n_v2))
except:
cosine = 0.0
return cosine
[docs]class Chromatogram(MS_Spectrum):
"""
"""
def __init__(self, element, measured_precision=5e-6, param=None):
"""
Arguments:
element (xml.etree.ElementTree.Element): spectrum as xml Element
Keyword Arguments:
measured_precision (float): in ppm, i.e. 5e-6 equals to 5 ppm.
param (dict): parameter mapping for this spectrum
"""
__slots__ = [
"_measured_precision",
"element",
"noise_level_estimate",
"_time",
"_i",
"_t_mass_set",
"_peaks",
"_t_mz_set",
"_centroided_peaks",
"_reprofiled_peaks",
"_deconvoluted_peaks",
"_profile",
"_extreme_values",
"_centroided_peaks_sorted_by_i",
"_transformed_mz_with_error",
"_transformed_mass_with_error",
"_precursors",
"_ID",
"internal_precision"
]
self._measured_precision = measured_precision
self.element = element
self.noise_level_estimate = {}
# Property variables
self._time = None
self._ms_level = None
self._i = None
self._t_mass_set = None
self._peaks = None
self._t_mz_set = None
self._centroided_peaks = None
self._reprofiled_peaks = None
self._deconvoluted_peaks = None
self._profile = None
self._extreme_values = None
self._centroided_peaks_sorted_by_i = None
self._transformed_mz_with_error = None
self._transformed_mass_with_error = None
self._precursors = None
self._ID = None
if self.element:
# self._read_accessions()
self.ns = re.match(
'\{.*\}', element.tag
).group(0) if re.match('\{.*\}', element.tag) else ''
# self._ns_paths = {
# 'mz' : "{ns}binaryDataArrayList/" \
# "{ns}binaryDataArray/" \
# "{ns}cvParam[@accession='{Acc}']/..".format(
# ns=self.ns,
# Acc=self.accessions['time array']
# ),
# 'i' : "{ns}binaryDataArrayList/" \
# "{ns}binaryDataArray/" \
# "{ns}cvParam[@accession='{Acc}']/..".format(
# ns=self.ns,
# Acc=self.accessions['intensity array']
# ),
# 'time' : "{ns}binaryDataArrayList/" \
# "{ns}binaryDataArray/" \
# "{ns}cvParam[@accession='{Acc}']/..".format(
# ns=self.ns,
# Acc=self.accessions['time array']
# ),
# 'float_type' : "./{ns}cvParam[@accession='{Acc}']"
# }
# TODO make everything numpy compatible
if np:
self._decode = self._decode_to_numpy
# assign function to create numpy array to list???
self._array = np.array
else:
self._array = list
self._decode = self._decode_to_tuple
def __repr__(self):
"""
"""
return '<__main__.Chromatogram object with native ID {0} at {1}>'.format(self.ID ,hex(id(self)))
def __str__(self):
"""
"""
return '<__main__.Chromatogram object with native ID {0} at {1}>'.format(self.ID ,hex(id(self)))
@property
def ID(self):
if self._ID is None:
self._ID = self.element.get('id')
return self._ID
@property
def mz(self):
''''''
print('Chromatogram has no property mz.\nReturn retention time instead')
return self.time
@property
def time(self):
"""
Returns the list of time values. If the time values are encoded, the
function _decode() is used to decode the encoded data.\n
The time property can also be setted, e.g. for theoretical data.
However, it is recommended to use the profile property to set time and
intesity tuples at same time.
Returns:
time (list): list of time values from the analysed chromatogram
"""
if self._time is None:
params = self._get_encoding_parameters(
'time array'
)
self._time = self._decode(*params)
return self._time
@property
def i(self):
if self._i is None:
params = self._get_encoding_parameters(
'intensity array'
)
self._i = self._decode(*params)
return self._i
@property
def profile(self):
"""
Returns the list of peaks of the chromatogram as tuples (time, intensity).
Returns:
peaks (list): list of time, i tuples
Example:
>>> import pymzml
>>> run = pymzml.run.Reader(spectra.mzMl.gz, MS1_Precision = 5e-6, MSn_Precision = 20e-6)
>>> for entry in run:
... if isinstance(entry, Chromatogram):
... for t, i in entry.peaks:
... print(t, i)
Note:
The peaks property can also be setted, e.g. for theoretical data.
It requires a list of time/intensity tuples.
"""
if self._profile is None:
if self._time is None and self._i is None:
self._profile = []
for pos, t in enumerate(self.time):
self._profile.append([t, self.i[pos]])
# much faster than zip ... list(zip(self.mz, self.i))
elif self._time is not None and self._i is not None:
self._profile = []
for pos, t in enumerate(self.time):
self._profile.append([t, self.i[pos]])
elif self._profile is None:
self._profile = []
return self._array(self._profile)
@profile.setter
def profile(self, tuple_list):
if len(tuple_list) == 0:
return
# self._mz, self._i = map(list, zip(*tuple_list))
# same here .. zip is soooooo slow :)
self._time = []
self._i = []
for time, i in tuple_list:
self._time.append( time )
self._i.append( i )
self._peaks = tuple_list
self._reprofiledPeaks = None
self._centroidedPeaks = None
return self
@property
def peaks(self):
"""
Returns the list of peaks of the spectrum as tuples (time, intensity).
Returns:
peaks (list): list of time, i tuples
Example:
>>> import pymzml
>>> run = pymzml.run.Reader(
... spectra.mzMl.gz,
... MS1_Precision = 5e-6,
... MSn_Precision = 20e-6
... )
>>> for entry in run:
... if isinstance(entry, pymzml.spec.Chromatogram):
... for t, i in entry.peaks:
... print(t, i)
Note:
The peaks property can also be setted, e.g. for theoretical data.
It requires a list of time/intensity tuples.
"""
# if self._profile is None:
# if self._time is None and self._i is None:
# self._profile = []
# data_mz, comp_mz, d_array_length_mz, f_type_mz= \
# self._get_encoding_parameters('time')
# data_i, comp_i, d_array_length_i, f_type_i= \
# self._get_encoding_parameters('i')
# # param_array_I = (data_i, f_type_i, 'i')
# param_array_I = (
# data_i, comp_i, d_array_length_i, f_type_i, 'i'
# )
# param_array_mz = (
# data_mz, comp_mz, d_array_length_mz, f_type_mz, 'time'
# )
# pymzml.Decoder.pool_decode(
# [param_array_mz, param_array_I],
# self._register
# )
# # use numpy to join lists!
# for pos, t in enumerate(self.time):
# self._profile.append([t, self.i[pos]])
# elif self._time and self._i:
# self._profile = []
# for pos, t in enumerate(self.time):
# self._profile.append([t, self.i[pos]])
# else:
# self._profile = []
# return self._array(self._profile)
return self.profile