Exemple #1
0
        /// <summary>
        /// Find the index of the peak that matches the m/z and tolerance
        /// </summary>
        /// <param name="mz"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        public int FindPeakIndex(double mz, Tolerance tolerance)
        {
            var tolTh = tolerance.GetToleranceAsMz(mz);
            var minMz = mz - tolTh;
            var maxMz = mz + tolTh;

            return(FindPeakIndex(minMz, maxMz));
        }
Exemple #2
0
        /// <summary>
        /// Filter noise peaks out using an intensity histogram
        /// </summary>
        public void FilterNoiseByIntensityHistogram()
        {
            var filteredPeaks = new List <Peak>();
            var intensities   = new double[Peaks.Length];
            var tolerance     = new Tolerance(10000);

            var st = 0;
            var ed = 0;

            foreach (var peak in Peaks)
            {
                var mzWindowWidth = tolerance.GetToleranceAsMz(peak.Mz);
                var intensity     = peak.Intensity;

                var mzStart = peak.Mz - mzWindowWidth;
                var mzEnd   = peak.Mz + mzWindowWidth;

                while (st < Peaks.Length)
                {
                    if (st < Peaks.Length - 1 && Peaks[st].Mz < mzStart)
                    {
                        st++;
                    }
                    else
                    {
                        break;
                    }
                }
                while (ed < Peaks.Length)
                {
                    if (ed < Peaks.Length - 1 && Peaks[ed].Mz < mzEnd)
                    {
                        ed++;
                    }
                    else
                    {
                        break;
                    }
                }

                var abundantIntensityBucket = GetMostAbundantIntensity(st, ed);
                if (abundantIntensityBucket.LowerBound < intensity && intensity < abundantIntensityBucket.UpperBound)
                {
                    continue;
                }

                filteredPeaks.Add(peak);
            }
            filteredPeaks.Sort();
            Peaks = filteredPeaks.ToArray();
        }
Exemple #3
0
        private static Tuple <Peak, int>[] GetAllIsotopePeaks(
            Spectrum spec,
            double monoisotopicMass,
            int charge,
            IsotopomerEnvelope envelope,
            Tolerance tolerance,
            double relativeIntensityThreshold)
        {
            var mostAbundantIsotopeIndex     = envelope.MostAbundantIsotopeIndex;
            var isotopomerEnvelope           = envelope.Envelope;
            var mostAbundantIsotopeMz        = Ion.GetIsotopeMz(monoisotopicMass, charge, mostAbundantIsotopeIndex);
            var mostAbundantIsotopePeakIndex = spec.FindPeakIndex(mostAbundantIsotopeMz, tolerance);

            if (mostAbundantIsotopePeakIndex < 0)
            {
                return(null);
            }

            var observedPeaks = new Tuple <Peak, int> [isotopomerEnvelope.Length];

            observedPeaks[mostAbundantIsotopeIndex] = new Tuple <Peak, int>(spec.Peaks[mostAbundantIsotopePeakIndex], mostAbundantIsotopePeakIndex);

            // go down
            var peakIndex = mostAbundantIsotopePeakIndex - 1;

            for (var isotopeIndex = mostAbundantIsotopeIndex - 1; isotopeIndex >= 0; isotopeIndex--)
            {
                if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold)
                {
                    break;
                }
                var isotopeMz = Ion.GetIsotopeMz(monoisotopicMass, charge, isotopeIndex);
                var tolTh     = tolerance.GetToleranceAsMz(isotopeMz);
                var minMz     = isotopeMz - tolTh;
                var maxMz     = isotopeMz + tolTh;
                for (var i = peakIndex; i >= 0; i--)
                {
                    var peakMz = spec.Peaks[i].Mz;
                    if (peakMz < minMz)
                    {
                        peakIndex = i;
                        break;
                    }
                    if (peakMz <= maxMz)    // find match, move to prev isotope
                    {
                        var peak = spec.Peaks[i];
                        if (observedPeaks[isotopeIndex] == null ||
                            peak.Intensity > observedPeaks[isotopeIndex].Item1.Intensity)
                        {
                            observedPeaks[isotopeIndex] = new Tuple <Peak, int>(peak, peakIndex);
                        }
                    }
                }
            }

            // go up
            peakIndex = mostAbundantIsotopePeakIndex + 1;
            for (var isotopeIndex = mostAbundantIsotopeIndex + 1; isotopeIndex < isotopomerEnvelope.Length; isotopeIndex++)
            {
                if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold)
                {
                    break;
                }
                var isotopeMz = Ion.GetIsotopeMz(monoisotopicMass, charge, isotopeIndex);
                var tolTh     = tolerance.GetToleranceAsMz(isotopeMz);
                var minMz     = isotopeMz - tolTh;
                var maxMz     = isotopeMz + tolTh;
                for (var i = peakIndex; i < spec.Peaks.Length; i++)
                {
                    var peakMz = spec.Peaks[i].Mz;
                    if (peakMz > maxMz)
                    {
                        peakIndex = i;
                        break;
                    }
                    if (peakMz >= minMz)    // find match, move to prev isotope
                    {
                        var peak = spec.Peaks[i];
                        if (observedPeaks[isotopeIndex] == null ||
                            peak.Intensity > observedPeaks[isotopeIndex].Item1.Intensity)
                        {
                            observedPeaks[isotopeIndex] = new Tuple <Peak, int>(peak, peakIndex);
                        }
                    }
                }
            }

            return(observedPeaks);
        }
Exemple #4
0
        /// <summary>
        /// Filter noise peaks out using a local window
        /// </summary>
        /// <param name="signalToNoiseRatio"></param>
        /// <param name="windowPpm"></param>
        public void FilterNoiseByLocalWindow(double signalToNoiseRatio = 1.4826, int windowPpm = 10000)
        {
            var filteredPeaks = new List <Peak>();
            var tolerance     = new Tolerance(windowPpm);
            var st            = 0;
            var ed            = 0;

            var prevSt = 0;
            var prevEd = 0;

            var intensityValues = new SortedSet <double>();

            foreach (var peak in Peaks)
            {
                var mzWindowWidth = tolerance.GetToleranceAsMz(peak.Mz);
                var mzStart       = peak.Mz - mzWindowWidth;
                var mzEnd         = peak.Mz + mzWindowWidth;

                while (st < Peaks.Length)
                {
                    if (st < Peaks.Length - 1 && Peaks[st].Mz < mzStart)
                    {
                        st++;
                    }
                    else
                    {
                        break;
                    }
                }
                while (ed < Peaks.Length)
                {
                    if (ed < Peaks.Length - 1 && Peaks[ed].Mz < mzEnd)
                    {
                        ed++;
                    }
                    else
                    {
                        break;
                    }
                }

                if (ed - st + 1 < 2)
                {
                    filteredPeaks.Add(peak);
                    continue;
                }

                if (intensityValues.Count < 1)
                {
                    for (var i = st; i <= ed; i++)
                    {
                        intensityValues.Add(Peaks[i].Intensity);
                    }
                }
                else
                {
                    if (prevEd >= st)
                    {
                        for (var i = prevSt; i < ed; i++)
                        {
                            intensityValues.Remove(Peaks[i].Intensity);
                        }
                        for (var i = prevEd + 1; i <= ed; i++)
                        {
                            intensityValues.Add(Peaks[i].Intensity);
                        }
                    }
                    else
                    {
                        for (var i = prevSt; i <= prevEd; i++)
                        {
                            intensityValues.Remove(Peaks[i].Intensity);
                        }
                    }
                }

                var intensityMedian = intensityValues.Median();
                if (peak.Intensity > intensityMedian * signalToNoiseRatio)
                {
                    filteredPeaks.Add(peak);
                }

                prevSt = st;
                prevEd = ed;
            }
            filteredPeaks.Sort();
            Peaks = filteredPeaks.ToArray();
        }
Exemple #5
0
        /// <summary>
        /// Finds all isotope peaks corresponding to theoretical profiles with relative intensity higher than the threshold
        /// </summary>
        /// <param name="ion">ion</param>
        /// <param name="tolerance">tolerance</param>
        /// <param name="relativeIntensityThreshold">relative intensity threshold of the theoretical isotope profile</param>
        /// <returns>array of observed isotope peaks in the spectrum. null if no peak found.</returns>
        public Peak[] GetAllIsotopePeaks(Ion ion, Tolerance tolerance, double relativeIntensityThreshold = 0.1)
        {
            var mostAbundantIsotopeIndex            = ion.Composition.GetMostAbundantIsotopeZeroBasedIndex();
            var isotopomerEnvelope                  = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities();
            var mostAbundantIsotopeMz               = ion.GetIsotopeMz(mostAbundantIsotopeIndex);
            var mostAbundantIsotopeMatchedPeakIndex = FindPeakIndex(mostAbundantIsotopeMz, tolerance);

            if (mostAbundantIsotopeMatchedPeakIndex < 0)
            {
                return(null);
            }

            var observedPeaks = new Peak[isotopomerEnvelope.Length];

            observedPeaks[mostAbundantIsotopeIndex] = Peaks[mostAbundantIsotopeMatchedPeakIndex];

            // go down
            var peakIndex = mostAbundantIsotopeMatchedPeakIndex - 1;

            for (var isotopeIndex = mostAbundantIsotopeIndex - 1; isotopeIndex >= 0; isotopeIndex--)
            {
                if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold)
                {
                    break;
                }
                var isotopeMz = ion.GetIsotopeMz(isotopeIndex);
                var tolTh     = tolerance.GetToleranceAsMz(isotopeMz);
                var minMz     = isotopeMz - tolTh;
                var maxMz     = isotopeMz + tolTh;
                for (var i = peakIndex; i >= 0; i--)
                {
                    var peakMz = Peaks[i].Mz;
                    if (peakMz < minMz)
                    {
                        peakIndex = i;
                        break;
                    }
                    if (peakMz <= maxMz)    // find match, move to prev isotope
                    {
                        var peak = Peaks[i];
                        if (observedPeaks[isotopeIndex] == null ||
                            peak.Intensity > observedPeaks[isotopeIndex].Intensity)
                        {
                            observedPeaks[isotopeIndex] = peak;
                        }
                    }
                }
            }

            // go up
            peakIndex = mostAbundantIsotopeMatchedPeakIndex + 1;
            for (var isotopeIndex = mostAbundantIsotopeIndex + 1; isotopeIndex < isotopomerEnvelope.Length; isotopeIndex++)
            {
                if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold)
                {
                    break;
                }
                var isotopeMz = ion.GetIsotopeMz(isotopeIndex);
                var tolTh     = tolerance.GetToleranceAsMz(isotopeMz);
                var minMz     = isotopeMz - tolTh;
                var maxMz     = isotopeMz + tolTh;
                for (var i = peakIndex; i < Peaks.Length; i++)
                {
                    var peakMz = Peaks[i].Mz;
                    if (peakMz > maxMz)
                    {
                        peakIndex = i;
                        break;
                    }
                    if (peakMz >= minMz)    // find match, move to prev isotope
                    {
                        var peak = Peaks[i];
                        if (observedPeaks[isotopeIndex] == null ||
                            peak.Intensity > observedPeaks[isotopeIndex].Intensity)
                        {
                            observedPeaks[isotopeIndex] = peak;
                        }
                    }
                }
            }

            return(observedPeaks);
        }
Exemple #6
0
        /// <summary>
        /// Checks whether this spectrum contains all isotope peaks whose relative intensity is equal or larter than the threshold
        /// </summary>
        /// <param name="ion">ion</param>
        /// <param name="tolerance">tolerance</param>
        /// <param name="relativeIntensityThreshold">relative intensity threshold of the theoretical isotope profile</param>
        /// <returns>true if spectrum contains all ions; false otherwise.</returns>
        public bool ContainsIon(Ion ion, Tolerance tolerance, double relativeIntensityThreshold = 0.1)
        {
            var baseIsotopeIndex     = ion.Composition.GetMostAbundantIsotopeZeroBasedIndex();
            var isotopomerEnvelope   = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities();
            var baseIsotopMz         = ion.GetIsotopeMz(baseIsotopeIndex);
            var baseIsotopePeakIndex = FindPeakIndex(baseIsotopMz, tolerance);

            if (baseIsotopePeakIndex < 0)
            {
                return(false);
            }

            // go down
            var peakIndex = baseIsotopePeakIndex;

            for (var isotopeIndex = baseIsotopeIndex - 1; isotopeIndex >= 0; isotopeIndex--)
            {
                if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold)
                {
                    break;
                }
                var isotopeMz = ion.GetIsotopeMz(isotopeIndex);
                var tolTh     = tolerance.GetToleranceAsMz(isotopeMz);
                var minMz     = isotopeMz - tolTh;
                var maxMz     = isotopeMz + tolTh;
                for (var i = peakIndex - 1; i >= 0; i--)
                {
                    var peakMz = Peaks[i].Mz;
                    if (peakMz < minMz)
                    {
                        return(false);
                    }
                    if (peakMz <= maxMz)    // find match, move to prev isotope
                    {
                        peakIndex = i;
                        break;
                    }
                }
            }

            // go up
            peakIndex = baseIsotopePeakIndex;
            for (var isotopeIndex = baseIsotopeIndex + 1; isotopeIndex < isotopomerEnvelope.Length; isotopeIndex++)
            {
                if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold)
                {
                    break;
                }
                var isotopeMz = ion.GetIsotopeMz(isotopeIndex);
                var tolTh     = tolerance.GetToleranceAsMz(isotopeMz);
                var minMz     = isotopeMz - tolTh;
                var maxMz     = isotopeMz + tolTh;
                for (var i = peakIndex + 1; i < Peaks.Length; i++)
                {
                    var peakMz = Peaks[i].Mz;
                    if (peakMz > maxMz)
                    {
                        return(false);
                    }
                    if (peakMz >= minMz)    // find match, move to prev isotope
                    {
                        peakIndex = i;
                        break;
                    }
                }
            }

            return(true);
        }