// Select the best peak within +/- filteringWindowSize public static List<DeconvolutedPeak> GetDeconvolutedPeaks( Peak[] peaks, int minCharge, int maxCharge, int isotopeOffsetTolerance, double filteringWindowSize, Tolerance tolerance, double corrScoreThreshold) { var monoIsotopePeakList = new List<DeconvolutedPeak>(); for (var peakIndex = 0; peakIndex < peaks.Length; peakIndex++) { var peak = peaks[peakIndex]; // Check whether peak has the maximum intensity within the window var isBest = true; var prevIndex = peakIndex - 1; while (prevIndex >= 0) { var prevPeak = peaks[prevIndex]; if ((peak.Mz - prevPeak.Mz) > filteringWindowSize) break; if (prevPeak.Intensity > peak.Intensity) { isBest = false; break; } prevIndex--; } if (!isBest) continue; var nextIndex = peakIndex + 1; while (nextIndex < peaks.Length) { var nextPeak = peaks[nextIndex]; if ((nextPeak.Mz - peak.Mz) > filteringWindowSize) break; if (nextPeak.Intensity > peak.Intensity) { isBest = false; break; } nextIndex++; } if (!isBest) continue; // peak has the maximum intensity, window = [prevIndex+1,nextIndex-1] var window = new Peak[nextIndex - prevIndex - 1]; Array.Copy(peaks, prevIndex + 1, window, 0, window.Length); var windowSpectrum = new Spectrum(window, 1); var peakMz = peak.Mz; for (var charge = maxCharge; charge >= minCharge; charge--) { var mass = peak.Mz * charge; var mostAbundantIsotopeIndex = Averagine.GetIsotopomerEnvelope(mass).MostAbundantIsotopeIndex; for (var isotopeIndex = mostAbundantIsotopeIndex - isotopeOffsetTolerance; isotopeIndex <= mostAbundantIsotopeIndex + isotopeOffsetTolerance; isotopeIndex++) { var monoIsotopeMass = Ion.GetMonoIsotopicMass(peakMz, charge, isotopeIndex); var isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(monoIsotopeMass); var observedPeaks = windowSpectrum.GetAllIsotopePeaks(monoIsotopeMass, charge, isotopomerEnvelope, tolerance, 0.1); if (observedPeaks == null) continue; var envelop = isotopomerEnvelope.Envolope; var observedIntensities = new double[observedPeaks.Length]; for (var i = 0; i < observedPeaks.Length; i++) { var observedPeak = observedPeaks[i]; observedIntensities[i] = observedPeak != null ? (float)observedPeak.Intensity : 0.0; } var sim = FitScoreCalculator.GetDistanceAndCorrelation(envelop, observedIntensities); var bcDist = sim.Item1; var corr = sim.Item2; if (corr < corrScoreThreshold && bcDist > 0.03) continue; // monoIsotopeMass is valid var deconvPeak = new DeconvolutedPeak(monoIsotopeMass, observedIntensities[mostAbundantIsotopeIndex], charge, corr, bcDist, observedPeaks); monoIsotopePeakList.Add(deconvPeak); } } } monoIsotopePeakList.Sort(); return monoIsotopePeakList; }
// Select the best peak within +/- filteringWindowSize public static List <DeconvolutedPeak> GetDeconvolutedPeaks( Peak[] peaks, int minCharge, int maxCharge, int isotopeOffsetTolerance, double filteringWindowSize, Tolerance tolerance, double corrScoreThreshold) { var monoIsotopePeakList = new List <DeconvolutedPeak>(); for (var peakIndex = 0; peakIndex < peaks.Length; peakIndex++) { var peak = peaks[peakIndex]; // Check whether peak has the maximum intensity within the window var isBest = true; var prevIndex = peakIndex - 1; while (prevIndex >= 0) { var prevPeak = peaks[prevIndex]; if ((peak.Mz - prevPeak.Mz) > filteringWindowSize) { break; } if (prevPeak.Intensity > peak.Intensity) { isBest = false; break; } prevIndex--; } if (!isBest) { continue; } var nextIndex = peakIndex + 1; while (nextIndex < peaks.Length) { var nextPeak = peaks[nextIndex]; if ((nextPeak.Mz - peak.Mz) > filteringWindowSize) { break; } if (nextPeak.Intensity > peak.Intensity) { isBest = false; break; } nextIndex++; } if (!isBest) { continue; } // peak has the maximum intensity, window = [prevIndex+1,nextIndex-1] var window = new Peak[nextIndex - prevIndex - 1]; Array.Copy(peaks, prevIndex + 1, window, 0, window.Length); var windowSpectrum = new Spectrum(window, 1); var peakMz = peak.Mz; for (var charge = maxCharge; charge >= minCharge; charge--) { var mass = peak.Mz * charge; var mostAbundantIsotopeIndex = Averagine.GetIsotopomerEnvelope(mass).MostAbundantIsotopeIndex; for (var isotopeIndex = mostAbundantIsotopeIndex - isotopeOffsetTolerance; isotopeIndex <= mostAbundantIsotopeIndex + isotopeOffsetTolerance; isotopeIndex++) { var monoIsotopeMass = Ion.GetMonoIsotopicMass(peakMz, charge, isotopeIndex); var isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(monoIsotopeMass); var observedPeaks = windowSpectrum.GetAllIsotopePeaks(monoIsotopeMass, charge, isotopomerEnvelope, tolerance, 0.1); if (observedPeaks == null) { continue; } var envelop = isotopomerEnvelope.Envolope; var observedIntensities = new double[observedPeaks.Length]; for (var i = 0; i < observedPeaks.Length; i++) { var observedPeak = observedPeaks[i]; observedIntensities[i] = observedPeak != null ? (float)observedPeak.Intensity : 0.0; } var sim = FitScoreCalculator.GetDistanceAndCorrelation(envelop, observedIntensities); var bcDist = sim.Item1; var corr = sim.Item2; if (corr < corrScoreThreshold && bcDist > 0.03) { continue; } // monoIsotopeMass is valid var deconvPeak = new DeconvolutedPeak(monoIsotopeMass, observedIntensities[mostAbundantIsotopeIndex], charge, corr, bcDist, observedPeaks); monoIsotopePeakList.Add(deconvPeak); } } } monoIsotopePeakList.Sort(); return(monoIsotopePeakList); }
/// <summary> /// Get the deconvoluted peaks, selecting the best peak within +/- filteringWindowSize /// </summary> /// <param name="scanNum">Scan number (included in any exceptions that are caught)</param> /// <param name="peaks"></param> /// <param name="minCharge"></param> /// <param name="maxCharge"></param> /// <param name="isotopeOffsetTolerance"></param> /// <param name="filteringWindowSize"></param> /// <param name="tolerance"></param> /// <param name="corrScoreThreshold"></param> /// <returns></returns> public static List <DeconvolutedPeak> GetDeconvolutedPeaks( int scanNum, Peak[] peaks, int minCharge, int maxCharge, int isotopeOffsetTolerance, double filteringWindowSize, Tolerance tolerance, double corrScoreThreshold) { try { var monoIsotopePeakList = new List <DeconvolutedPeak>(); for (var peakIndex = 0; peakIndex < peaks.Length; peakIndex++) { var peak = peaks[peakIndex]; // Check whether peak has the maximum intensity within the window var isBest = true; var prevIndex = peakIndex - 1; while (prevIndex >= 0) { var prevPeak = peaks[prevIndex]; if ((peak.Mz - prevPeak.Mz) > filteringWindowSize) { break; } if (prevPeak.Intensity > peak.Intensity) { isBest = false; break; } prevIndex--; } if (!isBest) { continue; } var nextIndex = peakIndex + 1; while (nextIndex < peaks.Length) { var nextPeak = peaks[nextIndex]; if ((nextPeak.Mz - peak.Mz) > filteringWindowSize) { break; } if (nextPeak.Intensity > peak.Intensity) { isBest = false; break; } nextIndex++; } if (!isBest) { continue; } // peak has the maximum intensity, window = [prevIndex+1,nextIndex-1] var window = new Peak[nextIndex - prevIndex - 1]; Array.Copy(peaks, prevIndex + 1, window, 0, window.Length); var windowSpectrum = new Spectrum(window, 1); var peakMz = peak.Mz; //var bestScore = 0.0; //DeconvolutedPeak bestPeak = null; for (var charge = maxCharge; charge >= minCharge; charge--) { var mass = (peak.Mz * charge) - charge * Constants.Proton; //var isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(mass); //var mostAbundantIsotopeIndex = isotopomerEnvelope.MostAbundantIsotopeIndex; var mostAbundantIsotopeIndex = Averagine.GetIsotopomerEnvelope(mass).MostAbundantIsotopeIndex; for (var isotopeIndex = mostAbundantIsotopeIndex - isotopeOffsetTolerance; isotopeIndex <= mostAbundantIsotopeIndex + isotopeOffsetTolerance; isotopeIndex++) { var monoIsotopeMass = Ion.GetMonoIsotopicMass(peakMz, charge, isotopeIndex); var isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(monoIsotopeMass); var observedPeaks = windowSpectrum.GetAllIsotopePeaks(monoIsotopeMass, charge, isotopomerEnvelope, tolerance, 0.1); if (observedPeaks == null) { continue; } var envelop = isotopomerEnvelope.Envelope; var observedIntensities = new double[observedPeaks.Length]; for (var i = 0; i < observedPeaks.Length; i++) { var observedPeak = observedPeaks[i]; observedIntensities[i] = observedPeak != null ? (float)observedPeak.Intensity : 0.0; } var sim = FitScoreCalculator.GetDistanceAndCorrelation(envelop, observedIntensities); var bcDist = sim.Item1; var corr = sim.Item2; //var score = corr / (bcDist * ((double)Math.Abs(isotopeIndex - mostAbundantIsotopeIndex) / envelop.Length)); if (corr < corrScoreThreshold && bcDist > 0.03) { continue; } // monoIsotopeMass is valid //if (score >= bestScore) //{ // bestScore = score; // bestPeak = new DeconvolutedPeak(monoIsotopeMass, observedIntensities[mostAbundantIsotopeIndex], charge, corr, bcDist, observedPeaks); //} var deconvPeak = new DeconvolutedPeak(monoIsotopeMass, observedIntensities[mostAbundantIsotopeIndex], charge, corr, bcDist, observedPeaks); monoIsotopePeakList.Add(deconvPeak); } } //if (bestPeak != null) //{ // monoIsotopePeakList.Add(bestPeak); //} } monoIsotopePeakList.Sort(); return(monoIsotopePeakList); } catch (Exception ex) { throw new Exception(string.Format("Error getting deconvoluted peaks for scan {0} in GetDeconvolutedPeaks: {1}", scanNum, ex.Message), ex); } }
private Peak GetHighestPeak(Ion ion, Spectrum spectrum, Tolerance tolerance, double relativeIntensityThreshold) { var peaks = spectrum.GetAllIsotopePeaks(ion, tolerance, relativeIntensityThreshold); Peak highestPeak = null; double highestIntensity = 0.0; if (peaks == null) return null; foreach (var peak in peaks) { if (peak != null && peak.Intensity >= highestIntensity) { highestPeak = peak; highestIntensity = peak.Intensity; } } return highestPeak; }