API MS Data Containers

Objects in emzed which can hold mass spectrometry-related data and their member functions.

Class Spectrum

class emzed.core.data_types.ms_types.Spectrum(peaks, rt, msLevel, polarity, precursors=None, meta=None, scan_number=None)[source]

MS Spectrum Type

__init__(peaks, rt, msLevel, polarity, precursors=None, meta=None, scan_number=None)[source]

Initialize instance

  • peaks
    n x 2 matrix first column: m/z values second column: intensities
  • rt
    float retention time in seconds
  • msLevel
    int MSn level.
  • polarity
    string of length 1 values: 0, + or -
  • precursors
    list of (float, float, int) for (m/z, intensity, charge) precursor m/z values if msLevel > 1
static compute_alignments(spectra, mz_tolerance)[source]

takes a list of spectra and groups peaks given mz_tolerance. it returns a list of lists. every inner list specifies the alignment of one input spectrum to its follower in the list. One assignment is a list of tuples, where the first entry is a peak index from the first list, the second entry is the index of a peak from the second spectrum.

For example:

if you run this method with a list or tuple of three spectra (s0, s1, s2) the return values will be [align_0_to_1, align_1_to_2]

an alignment is a list [(i0, j0), (i1, j1), ...]

so that s0.peaks[i0, :] is assigned to s1.peaks[j0, :] and so on.

cosine_distance(other, mz_tolerance, top_n=10, min_matches=10, consider_precursor_shift=False)[source]

computes the cosine distance of self and other.

top_n is the number of most intense peaks which should be used for alignment. min_matches: if there are less than min_matches matches the cosine distance will be 0.0

weight_for_precursor_shift: for ms2 spectra the parameter weight_for_precursor_shift stirs advanced matching. in this mode the mz values of the peaks of other are shifted by the precursor difference of self and other and an additional cosine distance is computed. the final result is a weighted sum of “cosine_ms_level_0” and “cosine_ms_level_1”, where the weight 0.0 only considers the match on ms1 level, a value of 1.0 only considers match on ms2 level and 0.5 computes an average of the cosine distances on both levels.

classmethod fromMSSpectrum(clz, mspec)[source]

creates Spectrum from pyopenms.MSSpectrum

intensityInRange(mzmin, mzmax)[source]

summed up intensities in given m/z range


maximal intensity in spectrum


maximal m/z value in spectrum


minimal m/z value in spectrum


returns pair min(mz), max(mz) for mz values in current spec. may return None, None if spec is empty !

peaksInRange(mzmin=None, mzmax=None)[source]

peaks in given m/z range as n x 2 matrix

first column: m/z values second column: intenisities


converts to pyopenms.MSSpectrum

Class PeakMap

class emzed.core.data_types.ms_types.PeakMap(spectra, meta=None)[source]

This is the container object for spectra of type Spectrum. Peakmaps can be loaded from .mzML, .mxXML or .mzData files, using loadPeakMap()

A PeakMap is a list of Spectrum objects attached with meta data about its source.

__init__(spectra, meta=None)[source]

spectra : iterable (list, tuple, ...) of objects of type Spectrum

meta : dictionary of meta values


returns all rt values in peakmap

chromatogram(mzmin, mzmax, rtmin=None, rtmax=None, msLevel=None)[source]

extracts chromatogram in given rt- and mz-window. returns a tuple (rts, intensities) where rts is a list of rt values (in seconds, as always) and intensities is a list of same length containing the summed up peaks for each rt value.


removes empty spectra

extract(rtmin=None, rtmax=None, mzmin=None, mzmax=None, imin=None, imax=None, mslevelmin=None, mslevelmax=None)[source]

returns restricted Peakmap with given limits. Parameters with None value are not considered.


pm.extract(rtmax = 12.5 * 60)
pm.extract(rtmin = 12*60, rtmax = 12.5 * 60)
pm.extract(rtmax = 12.5 * 60, mzmin = 100, mzmax = 200)
pm.extract(rtmin = 12.5 * 60, mzmax = 200)
pm.extract(mzmax = 200)

builds new peakmap where condition(s) is True for spectra s

filterIntensity(msLevel=None, minInt=None, maxInt=None)[source]

creates new peakmap matching the given conditions. Using a single requirement as:

classmethod fromMSExperiment(clz, mse)[source]

creates Spectrum from pyopenms.MSExperiment


returns list of ms levels in current peak map

levelNSpecs(minN, maxN=None)[source]

returns list of spectra in given msLevel range

levelNSpecsInRange(n, rtmin, rtmax)[source]

returns lists level one spectra in peakmap


returns rt values of all level one spectra in peakmap

msNPeaks(n, rtmin=None, rtmax=None)[source]

return ms level n peaks in given range


returns mz-range (mzmin, mzmax) of current peakmap

representingMzPeak(mzmin, mzmax, rtmin, rtmax)[source]

returns a weighted mean m/z value in given range. high intensities contribute with weight ln(I+1) to final m/z value


returns rt-range (rtmin, tax) of current peakmap

sample_peaks(rtmin, rtmax, mzmin, mzmax, n_bins, ms_level)[source]

creates binned spectrum from given range. returns matrix with two columns, the first column corresponds to the mz bins and the second to the max intensity per bin. is used for plotting spectra on different zoom levels


shifts all rt values by delta

specsInRange(rtmin, rtmax)[source]

returns list of spectra with rt values in range rtmin...rtmax

splitLevelN(msLevel, significant_digits_precursor=2)[source]

splits peakmap to list of tuples. the first entry of a tuple is the precursor mass, the second one the corresponding peak map of spectra of level msLevel


only supported for peakmap proxies to save space if possible


converts peakmap to pyopenms.MSExperiment