public double CompareSpectra(MSSpectra spectraX, MSSpectra spectraY) { var xIntensities = this.ExpandVector(spectraY.Peaks, spectraX.Peaks).Select(xpeak => xpeak.X).ToArray(); var yIntensities = this.ExpandVector(spectraX.Peaks, spectraY.Peaks).Select(ypeak => ypeak.Y).ToArray(); return(FitScoreCalculator.GetPearsonCorrelation(xIntensities, yIntensities, yIntensities.Length)); }
public double GetRankSumTestPvalue(Ms1Peak[] peaks, int envelopeSize) { if (PeakRanking == null) { return(1.0d); } // calculate ranksum test score var ranksum = 0; var nRankSum = 0; for (var i = 0; i < envelopeSize; i++) { if (peaks[i] == null || !peaks[i].Active) { continue; } var localIndex = peaks[i].IndexInSpectrum - PeakStartIndex; if (localIndex >= PeakCount || localIndex < 0) { continue; } ranksum += PeakRanking[localIndex]; nRankSum++; } var pvalue = FitScoreCalculator.GetRankSumPvalue(PeakCount, nRankSum, ranksum); return(pvalue); }
/// <summary> /// Get the fit between a theoretical and actual isotopic profile using DeconTools fit score. /// </summary> /// <param name="theoretical">The theoretical isotopic profile.</param> /// <param name="observed">The actual observed isotopic profile.</param> /// <returns>The DeconTools fit score.</returns> /// <remarks>0 is the best score, 1 is the worst score.</remarks> protected override double GetFitScore(double[] theoretical, double[] observed) { if (theoretical == null || observed == null || theoretical.Length != observed.Length) { return(1.0); } return(FitScoreCalculator.GetDeconToolsFit(theoretical, observed)); }
/// <summary> /// Get the fit between a theoretical and actual isotopic profile using Pearson correlation. /// </summary> /// <param name="theoretical">The theoretical isotopic profile.</param> /// <param name="observed">The actual observed isotopic profile.</param> /// <returns>The Pearson correlation score.</returns> /// <remarks>1 is the best score, 0 is the worst score.</remarks> protected override double GetFitScore(double[] theoretical, double[] observed) { if (theoretical == null || observed == null || theoretical.Length != observed.Length) { return(0.0); } return(FitScoreCalculator.GetPearsonCorrelation(theoretical, observed)); }
public void TestSumIsoProfilesAcrossDifferentCharges() { var methodName = MethodBase.GetCurrentMethod().Name; Utils.ShowStarting(methodName); if (!File.Exists(TestRawFilePath)) { Assert.Ignore(@"Skipping test " + methodName + @" since file not found: " + TestRawFilePath); } var run = PbfLcMsRun.GetLcMsRun(TestRawFilePath) as PbfLcMsRun; //var spec = run.GetSpectrum(46452); // 635.37 var spec = run.GetSummedMs1Spectrum(46437, 46466); var tolerance = new Tolerance(10); const string protSequence = "AIPQSVEGQSIPSLAPMLERTTPAVVSVAVSGTHVSKQRVPDVFRYFFGPNAPQEQVQERPFRGLGSGVIIDADKGYIVTNNHVIDGADDIQVGLHDGREVKAKLIGTDSESDIALLQIEAKNLVAIKTSDSDELRVGDFAVAIGNPFGLGQTVTSGIVSALGRSGLGIEMLENFIQTDAAINSGNSGGALVNLKGELIGINTAIVAPNGGNVGIGFAIPANMVKNLIAQIAEHGEVRRGVLGIAGRDLDSQLAQGFGLDTQHGGFVNEVSAGSAAEKAGIKAGDIIVSVDGRAIKSFQELRAKVATMGAGAKVELGLIRDGDKKTVNVTLGEANQTTEKAAGAVHPMLQGASLENASKGVEITDVAQGSPAAMSGLQKGDLIVGINRTAVKDLKSLKELLKDQEGAVALKIVRGKSMLYLVLR"; //const string annotation = "_." + protSequence + "._"; var seqGraph = SequenceGraph.CreateGraph(new AminoAcidSet(), AminoAcid.ProteinNTerm, protSequence, AminoAcid.ProteinCTerm); if (seqGraph == null) { return; } seqGraph.SetSink(0); var neutral = seqGraph.GetSinkSequenceCompositionWithH2O(); var theoProfile = neutral.GetIsotopomerEnvelopeRelativeIntensities(); var expProfile = new double[theoProfile.Length]; for (var charge = 22; charge <= 45; charge++) { var ion = new Ion(neutral, charge); var isotopePeaks = spec.GetAllIsotopePeaks(ion, tolerance, 0.1); if (isotopePeaks == null) { continue; } Assert.True(isotopePeaks.Length == theoProfile.Length); for (var i = 0; i < isotopePeaks.Length; i++) { if (isotopePeaks[i] != null) { expProfile[i] += isotopePeaks[i].Intensity; } } } for (var i = 0; i < theoProfile.Length; i++) { Console.WriteLine("{0}\t{1}\t{2}", neutral.GetIsotopeMass(i), theoProfile[i], expProfile[i] / expProfile.Max()); } Console.WriteLine("Corr: " + FitScoreCalculator.GetPearsonCorrelation(theoProfile, expProfile)); }
private double GetHyperGeometricScore() { var nPossiblePeaks = Comparer.GetBinNumber(_ms2Spec.Peaks.Last().Mz) - Comparer.GetBinNumber(_ms2Spec.Peaks.First().Mz) + 1; var nObservedPeaks = _ms2Spec.Peaks.Length; var pvalue = FitScoreCalculator.GetHyperGeometricPvalue(nPossiblePeaks, nObservedPeaks, _nTheoreticalIonPeaks, _nObservedIonPeaks); if (pvalue > 0) { return(-Math.Log(pvalue, 2)); } return(50); }
public void OutputStatistics(ProductSpectrum spectrum, Sequence sequence) { var baseIonTypes = spectrum.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCid : BaseIonTypesEtd; var cleavages = sequence.GetInternalCleavages().ToArray(); var tolerance = new Tolerance(10); var maxIntensity = spectrum.Peaks.Max(p => p.Intensity); foreach (var c in cleavages) { foreach (var baseIonType in baseIonTypes) { var fragmentComposition = baseIonType.IsPrefix ? c.PrefixComposition + baseIonType.OffsetComposition : c.SuffixComposition + baseIonType.OffsetComposition; for (int charge = MinCharge; charge <= MaxCharge; charge++) { var ion = new Ion(fragmentComposition, charge); var observedPeaks = spectrum.GetAllIsotopePeaks(ion, tolerance, RelativeIsotopeIntensityThreshold); if (observedPeaks == null) { continue; } var mostAbundantIsotopeIndex = ion.Composition.GetMostAbundantIsotopeZeroBasedIndex(); // representative peak intensity var ionPeakIntensity = observedPeaks[mostAbundantIsotopeIndex].Intensity; // calc. correlation var isotopomerEnvelope = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities(); 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 corrCoeff = FitScoreCalculator.GetPearsonCorrelation(isotopomerEnvelope, observedIntensities); // mz error var mostAbundantIsotopeMz = ion.GetIsotopeMz(mostAbundantIsotopeIndex); var errorPpm = ((observedPeaks[mostAbundantIsotopeIndex].Mz - mostAbundantIsotopeMz) / mostAbundantIsotopeMz) * 1e6; } } } }
/// <summary> /// Computes the fit score between the ion and corresponding peaks in the spectrum /// </summary> /// <param name="ion">ion</param> /// <param name="tolerance">tolerance</param> /// <param name="relativeIntensityThreshold">relative intensity threshold of the theoretical isotope profile</param> /// <returns>fit score</returns> public double GetFitScore(Ion ion, Tolerance tolerance, double relativeIntensityThreshold = 0.1) { var isotopomerEnvelope = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities(); var observedPeaks = GetAllIsotopePeaks(ion, tolerance, relativeIntensityThreshold); if (observedPeaks == null) { return(1); } var theoIntensities = new double[observedPeaks.Length]; Array.Copy(isotopomerEnvelope, theoIntensities, theoIntensities.Length); var maxObservedIntensity = observedPeaks.Select(p => p != null ? p.Intensity : 0).Max(); var normalizedObs = observedPeaks.Select(p => p != null ? p.Intensity / maxObservedIntensity : 0).ToArray(); return(FitScoreCalculator.GetFitOfNormalizedVectors(isotopomerEnvelope, normalizedObs)); }
/// <summary> /// Get the Bhattacharyya distance and the Pearson correlation for the provided ion and peaks /// </summary> /// <param name="ion"></param> /// <param name="observedPeaks"></param> /// <returns></returns> public static Tuple <double, double> GetDistCorr(Ion ion, Peak[] observedPeaks) { var isotopomerEnvelope = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities(); var envelope = isotopomerEnvelope; var observedIntensities = new double[envelope.Length]; for (var i = 0; i < isotopomerEnvelope.Length; i++) { if (observedPeaks[i] != null) { observedIntensities[i] = observedPeaks[i].Intensity; } } return(FitScoreCalculator.GetDistanceAndCorrelation(envelope, observedIntensities)); //var bcDist = FitScoreCalculator.GetBhattacharyyaDistance(envelope, observedIntensities); //var corr = FitScoreCalculator.GetPearsonCorrelation(envelope, observedIntensities); //return new Tuple<double, double>(bcDist, corr); }
/// <summary> /// Computes the Pearson correlation between the ion and corresponding peaks in the spectrum /// </summary> /// <param name="ion">ion</param> /// <param name="tolerance">tolerance</param> /// <param name="relativeIntensityThreshold">relative intensity threshold of the theoretical isotope profile</param> /// <returns>Pearson correlation</returns> public double GetCorrScore(Ion ion, Tolerance tolerance, double relativeIntensityThreshold = 0.1) { var observedPeaks = GetAllIsotopePeaks(ion, tolerance, relativeIntensityThreshold); if (observedPeaks == null) { return(0); } var isotopomerEnvelope = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities(); 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; } return(FitScoreCalculator.GetPearsonCorrelation(isotopomerEnvelope, observedIntensities)); }
/// <summary> /// Gets the pearson correlation and cosine score of an observed isotopic distribution compared to a theoretical ion. /// </summary> /// <param name="ion">Theoretical ion.</param> /// <param name="observedPeaks">The observed isotopic distribution.</param> /// <returns>A tuple where the first item is pearson correlation and the second item is cosine.</returns> private Tuple <double, double> GetCorrCos(Ion ion, Peak[] observedPeaks) { var isotopomerEnvelope = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities(); var envelope = isotopomerEnvelope; var observedIntensities = new double[envelope.Length]; for (var i = 0; i < isotopomerEnvelope.Length; i++) { if (observedPeaks[i] != null) { observedIntensities[i] = observedPeaks[i].Intensity; } } double pearsonCorrelation = FitScoreCalculator.GetPearsonCorrelation(envelope, observedIntensities); double cosine = FitScoreCalculator.GetCosine(envelope, observedIntensities); return(new Tuple <double, double>(pearsonCorrelation, cosine)); }
/// <summary> /// Get the Pearson correlation of 2 XICs /// </summary> /// <param name="other"></param> /// <returns></returns> public double GetCorrelation(Xic other) { if (Count == 0 || other == null || other.Count == 0) { return(0); } var count1 = Count; var count2 = other.Count; var index1 = 0; var index2 = 0; var intList1 = new List <double>(); var intList2 = new List <double>(); while (index1 < count1 && index2 < count2) { var comp = this[index1].ScanNum - other[index2].ScanNum; if (comp < 0) { ++index1; } else if (comp > 0) { ++index2; } else { intList1.Add(this[index1].Intensity); intList2.Add(other[index2].Intensity); ++index1; ++index2; } } var correlation = FitScoreCalculator.GetPearsonCorrelation(intList1.ToArray(), intList2.ToArray()); return(correlation); }
private double GetRankSumScore() { var rankSum = 0d; var nMatchedIons = _prefixIonPeakIndex.Count + _suffixIonPeakIndex.Count; var nObservedPeaks = _ms2Spec.Peaks.Length; foreach (var peakIndex in _prefixIonPeakIndex) { rankSum += _peakRanking[peakIndex]; } foreach (var peakIndex in _suffixIonPeakIndex) { rankSum += _peakRanking[peakIndex]; } var pvalue = FitScoreCalculator.GetRankSumPvalue(nObservedPeaks, nMatchedIons, rankSum); if (pvalue > 0) { return(-Math.Log(pvalue, 2)); } return(50); }
/// <summary> /// Get the Cosine score of 2 XICs /// </summary> /// <param name="other"></param> /// <returns></returns> public double GetCosine(Xic other) { if (Count == 0 || other == null || other.Count == 0) { return(0); } //var minScanNum = Math.Min(this[0].ScanNum, other[0].ScanNum); //var maxScanNum = Math.Max(this[Count-1].ScanNum, other[other.Count-1].ScanNum); var minScanNum = this[0].ScanNum; var maxScanNum = this[Count - 1].ScanNum; var intArr1 = new double[maxScanNum - minScanNum + 1]; foreach (var p in this) { intArr1[p.ScanNum - minScanNum] = p.Intensity; } var intArr2 = new double[maxScanNum - minScanNum + 1]; foreach (var p in other) { var index = p.ScanNum - minScanNum; if (index < 0 || index >= intArr2.Length) { continue; } intArr2[p.ScanNum - minScanNum] = p.Intensity; } var correlation = FitScoreCalculator.GetCosine(intArr1, intArr2); return(correlation); }
public void UpdateScore(List <Ms1Spectrum> ms1Spectra, bool pValueCheck = true) { var nRows = MaxCharge - MinCharge + 1; var ms1ScanNumToIndex = _run.GetMs1ScanNumToIndex(); var minCol = ms1ScanNumToIndex[MinScanNum]; var maxCol = ms1ScanNumToIndex[MaxScanNum]; var nCols = maxCol - minCol + 1; var mostAbuIdx = TheoreticalEnvelope.IndexOrderByRanking[0]; ClearScore(); var bestChargeDist = new double[] { 10.0d, 10.0d }; // sum envelopes at each charge var summedIntensity = new double[TheoreticalEnvelope.Size]; var xicLen = nCols + 18; var xicStartIdx = 9; /* * if (nCols < 13) * { * xicLen = 13; * xicStartIdx = (int) Math.Floor((xicLen - nCols)*0.5); * }*/ var xic2 = new double[2][]; xic2[0] = new double[xicLen]; xic2[1] = new double[xicLen]; var chargeXic = new double[nRows][]; var tempBestBcDist = 10.0d; var repEnvelopeBcDist = 10.0d; ObservedIsotopeEnvelope repEnvelope = null; var repEnvelopeBcDist2 = 10.0d; ObservedIsotopeEnvelope repEnvelope2 = null; var tempBestDistanceScoreAcrossCharge = new double[2] { 10, 10 }; var tempBestIntensityScoreAcrossCharge = new double[2]; var tempBestCorrelationScoreAcrossCharge = new double[2]; for (var i = 0; i < nRows; i++) { var charge = i + MinCharge; var mostAbuMz = TheoreticalEnvelope.GetIsotopeMz(charge, mostAbuIdx); Array.Clear(summedIntensity, 0, summedIntensity.Length); chargeXic[i] = new double[xicLen]; var chargeIdx = (charge % 2 == 0) ? EvenCharge : OddCharge; var summedMostAbuIsotopeIntensity = 0d; var summedReferenceIntensity = 0d; for (var j = 0; j < nCols; j++) { var envelope = Envelopes[i][j]; var col = minCol + j; var localWin = ms1Spectra[col].GetLocalMzWindow(mostAbuMz); if (envelope == null) { continue; } envelope.Peaks.SumEnvelopeTo(summedIntensity); var mostAbuPeak = envelope.Peaks[mostAbuIdx]; if (mostAbuPeak != null && mostAbuPeak.Active) { summedMostAbuIsotopeIntensity += mostAbuPeak.Intensity; summedReferenceIntensity += localWin.HighestIntensity; } AbundanceDistributionAcrossCharge[chargeIdx] += envelope.Abundance; var newBcDist = TheoreticalEnvelope.GetBhattacharyyaDistance(envelope.Peaks); var newCorr = TheoreticalEnvelope.GetPearsonCorrelation(envelope.Peaks); var goodEnvelope = (newBcDist <0.07 || newCorr> 0.7); if (goodEnvelope) { xic2[chargeIdx][xicStartIdx + j] += envelope.Abundance; chargeXic[i][xicStartIdx + j] = envelope.Abundance; } var levelOneEnvelope = true; var levelTwoEnvelope = true; if (pValueCheck) { var poissonPvalue = localWin.GetPoissonTestPvalue(envelope.Peaks, TheoreticalEnvelope.Size); var rankSumPvalue = localWin.GetRankSumTestPvalue(envelope.Peaks, TheoreticalEnvelope.Size); levelOneEnvelope = (rankSumPvalue < 0.01 && poissonPvalue < 0.01); //levelTwoEnvelope = (rankSumPvalue < 0.05 || poissonPvalue < 0.05); } if (levelOneEnvelope) { if (newBcDist < BestDistanceScoreAcrossCharge[chargeIdx]) { BestDistanceScoreAcrossCharge[chargeIdx] = newBcDist; if (localWin.MedianIntensity > 0) { BestIntensityScoreAcrossCharge[chargeIdx] = envelope.HighestIntensity / localWin.HighestIntensity; } else { BestIntensityScoreAcrossCharge[chargeIdx] = 1.0d; } } BestCorrelationScoreAcrossCharge[chargeIdx] = Math.Max(BestCorrelationScoreAcrossCharge[chargeIdx], newCorr); if (newBcDist < repEnvelopeBcDist) { repEnvelopeBcDist = newBcDist; repEnvelope = envelope; } // in the initial scoring, classify major and minor envelopes if (!_initScore && goodEnvelope) { envelope.GoodEnough = true; } } if (levelTwoEnvelope) { if (newBcDist < tempBestDistanceScoreAcrossCharge[chargeIdx]) { tempBestDistanceScoreAcrossCharge[chargeIdx] = newBcDist; if (localWin.MedianIntensity > 0) { tempBestIntensityScoreAcrossCharge[chargeIdx] = envelope.HighestIntensity / localWin.HighestIntensity; } else { tempBestIntensityScoreAcrossCharge[chargeIdx] = 1.0d; } } tempBestCorrelationScoreAcrossCharge[chargeIdx] = Math.Max(tempBestCorrelationScoreAcrossCharge[chargeIdx], newCorr); if (newBcDist < repEnvelopeBcDist2) { repEnvelopeBcDist2 = newBcDist; repEnvelope2 = envelope; } } } var bcDist = TheoreticalEnvelope.GetBhattacharyyaDistance(summedIntensity); EnvelopeDistanceScoreAcrossCharge[chargeIdx] = Math.Min(bcDist, EnvelopeDistanceScoreAcrossCharge[chargeIdx]); EnvelopeCorrelationScoreAcrossCharge[chargeIdx] = Math.Max(TheoreticalEnvelope.GetPearsonCorrelation(summedIntensity), EnvelopeCorrelationScoreAcrossCharge[chargeIdx]); if (BestCharge[chargeIdx] < 1 || bcDist < bestChargeDist[chargeIdx]) { BestCharge[chargeIdx] = charge; bestChargeDist[chargeIdx] = bcDist; if (summedReferenceIntensity > 0) { EnvelopeIntensityScoreAcrossCharge[chargeIdx] = summedMostAbuIsotopeIntensity / summedReferenceIntensity; } //if (summedMedianIntensity > 0) EnvelopeIntensityScoreAcrossCharge[chargeIdx] = Math.Min(1.0, 0.1*(summedMostAbuIsotopeIntensity / summedMedianIntensity)); } if (bcDist < tempBestBcDist) { tempBestBcDist = bcDist; Array.Copy(summedIntensity, RepresentativeSummedEnvelop, RepresentativeSummedEnvelop.Length); } } // when good envellope is observed at only either even or odd charge... if (BestCorrelationScoreAcrossCharge[0] > 0.7 && BestCorrelationScoreAcrossCharge[1] < 0.5) { const int i = 1; BestCorrelationScoreAcrossCharge[i] = tempBestCorrelationScoreAcrossCharge[i]; BestIntensityScoreAcrossCharge[i] = tempBestIntensityScoreAcrossCharge[i]; BestDistanceScoreAcrossCharge[i] = tempBestDistanceScoreAcrossCharge[i]; } if (BestCorrelationScoreAcrossCharge[1] > 0.7 && BestCorrelationScoreAcrossCharge[0] < 0.5) { const int i = 0; BestCorrelationScoreAcrossCharge[i] = tempBestCorrelationScoreAcrossCharge[i]; BestIntensityScoreAcrossCharge[i] = tempBestIntensityScoreAcrossCharge[i]; BestDistanceScoreAcrossCharge[i] = tempBestDistanceScoreAcrossCharge[i]; } // normalize abudnace across charges var s = AbundanceDistributionAcrossCharge[0] + AbundanceDistributionAcrossCharge[1]; if (s > 0) { for (var chargeIdx = 0; chargeIdx < 2; chargeIdx++) { AbundanceDistributionAcrossCharge[chargeIdx] = AbundanceDistributionAcrossCharge[chargeIdx] / s; } } if (nCols > 1) { var evenChargeIdx = BestCharge[EvenCharge] - MinCharge; var oddChargeIdx = BestCharge[OddCharge] - MinCharge; XicCorrelationBetweenBestCharges[0] = FitScoreCalculator.GetPearsonCorrelation(Smoother.Smooth(chargeXic[evenChargeIdx]), Smoother.Smooth(chargeXic[oddChargeIdx])); XicCorrelationBetweenBestCharges[1] = FitScoreCalculator.GetPearsonCorrelation(Smoother.Smooth(xic2[EvenCharge]), Smoother.Smooth(xic2[OddCharge])); } if (repEnvelope == null && repEnvelope2 != null) { repEnvelope = repEnvelope2; } if (repEnvelope != null) { // set representative charge, mz and scanNum RepresentativeCharge = repEnvelope.Charge; RepresentativeMz = repEnvelope.RepresentativePeak.Mz; RepresentativeScanNum = repEnvelope.ScanNum; } _initScore = true; }
public void TestFitScoreComputationTime() { var methodName = MethodBase.GetCurrentMethod().Name; Utils.ShowStarting(methodName); const int numTrials = 1000000; const int numIsotopes = 20; var random = new Random(); var theoretical = new double[numTrials][]; var observed = new double[numTrials][]; for (var i = 0; i < numTrials; i++) { theoretical[i] = new double[numIsotopes]; observed[i] = new double[numIsotopes]; for (var j = 0; j < numIsotopes; j++) { theoretical[i][j] = random.NextDouble(); observed[i][j] = random.NextDouble(); } } Console.WriteLine("Calculating fit scores {0} times", numTrials * 2); var sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (var trial = 0; trial < numTrials; trial++) { //FitScoreCalculator.GetDeconToolsFit(theoretical[trial], observed[trial]); //FitScoreCalculator.GetFitOfNormalizedVectors(theoretical[trial], observed[trial]); //FitScoreCalculator.GetCosine(theoretical[trial], observed[trial]); //FitScoreCalculator.GetFitNormalizedByTheoMaxIsotope(theoretical[trial], observed[trial]); FitScoreCalculator.GetPearsonCorrelation(theoretical[trial], observed[trial]); } var series1 = new double[7]; series1[0] = 0.2; series1[1] = 0.5; series1[2] = 0.8; series1[3] = 0.7; series1[4] = 0.6; series1[5] = 0.3; series1[6] = 0.05; var series2 = new double[series1.Length]; double smallestFit = 1; var threshold = (int)(1000000 / 10.0); for (var iteration = 0; iteration < 1000000; iteration++) { for (var i = 0; i < series1.Length; i++) { series2[i] = series1[i] + random.NextDouble() / 6 - (1 / 12.0); } var result = FitScoreCalculator.GetPearsonCorrelation(series1, series2); if (iteration % threshold == 0) { Console.WriteLine(@"Fit=" + result); } if (result < smallestFit) { smallestFit = result; } } Console.WriteLine(@"SmallestFit=" + smallestFit); Assert.IsTrue(smallestFit > 0.94); sw.Stop(); Console.WriteLine(@"Elapsed Time: {0:f4} sec", sw.Elapsed.TotalSeconds); }
// 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); }
public IsotopeEnvelopeStatisticalInfo PreformStatisticalSignificanceTest(ObservedIsotopeEnvelope envelope) { int peakStartIndex; Tuple <double, double> mzBoundary; //var refPeak = envelope.Peaks[envelope.RefIsotopeInternalIndex]; var mostAbuMz = 0d; var mostAbutPeakInternalIndex = envelope.TheoreticalEnvelope.IndexOrderByRanking[0]; if (envelope.Peaks[mostAbutPeakInternalIndex] != null) { mostAbuMz = envelope.Peaks[mostAbutPeakInternalIndex].Mz; } else { mostAbuMz = envelope.TheoreticalEnvelope.GetIsotopeMz(envelope.Charge, mostAbutPeakInternalIndex); } var rankings = GetLocalRankings(mostAbuMz, out peakStartIndex, out mzBoundary); // smallest delta_mz = 0.01 (th) ? var ret = new IsotopeEnvelopeStatisticalInfo { LocalMzStart = mzBoundary.Item1, LocalMzEnd = mzBoundary.Item2, NumberOfLocalPeaks = rankings.Length, NumberOfPossiblePeaks = (int)Math.Ceiling(100 * (mzBoundary.Item2 - mzBoundary.Item1)), NumberOfIsotopePeaks = envelope.Size, }; // calculate ranksum test score var ranksum = 0; var nRankSum = 0; for (var i = 0; i < envelope.Size; i++) { if (envelope.Peaks[i] == null || !envelope.Peaks[i].Active) { continue; } ret.NumberOfMatchedIsotopePeaks++; //if (isotopeList[i].Ratio > RelativeIntesnityThresholdForRankSum) //{ var localIndex = envelope.Peaks[i].IndexInSpectrum - peakStartIndex; if (localIndex >= rankings.Length || localIndex < 0) { continue; } ranksum += rankings[localIndex]; nRankSum++; //} } var pvalue = FitScoreCalculator.GetRankSumPvalue(ret.NumberOfLocalPeaks, nRankSum, ranksum); ret.RankSumScore = (pvalue > 0) ? -Math.Log(pvalue, 2) : 50; // calculate poisson test score var n = ret.NumberOfPossiblePeaks; var k = ret.NumberOfIsotopePeaks; // # of theretical isotope ions of the mass within the local window var n1 = ret.NumberOfLocalPeaks; // # of detected ions within the local window var k1 = ret.NumberOfMatchedIsotopePeaks; // # of matched ions generating isotope envelope profile var lambda = ((double)n1 / (double)n) * k; pvalue = 1 - Poisson.CDF(lambda, k1); ret.PoissonScore = (pvalue > 0) ? -Math.Log(pvalue, 2) : 50; return(ret); }
/// <summary> /// Get the deconvoluted peaks that correspond to the provided peak list /// </summary> /// <param name="peaks"></param> /// <param name="minCharge"></param> /// <param name="maxCharge"></param> /// <param name="isotopeOffsetTolerance"></param> /// <param name="tolerance"></param> /// <param name="corrScoreThreshold"></param> /// <returns></returns> public static List <DeconvolutedPeak> GetDeconvolutedPeaks_new( Peak[] peaks, int minCharge, int maxCharge, int isotopeOffsetTolerance, Tolerance tolerance, double corrScoreThreshold) { var spectrum = new Spectrum(peaks, 0); var monoIsotopePeakList = new List <DeconvolutedPeak>(); var sortedPeaks = peaks.OrderByDescending(peak => peak.Intensity).ToArray(); var peakUsed = new bool[peaks.Length]; foreach (var peak in sortedPeaks) { var peakIndex = Array.BinarySearch(peaks, peak); if (peakUsed[peakIndex]) { continue; } var bestScore = 0.0; DeconvolutedPeak bestPeak = null; Tuple <Peak, int>[] bestObservedPeaks = null; for (var charge = minCharge; charge <= maxCharge; charge++) { var mass = peak.Mz * charge - (charge * Constants.Proton); if (mass > MaxMass) { continue; } var isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(mass); var mostAbundantIsotopeIndex = isotopomerEnvelope.MostAbundantIsotopeIndex; var offsetTolerance = isotopeOffsetTolerance; if (isotopeOffsetTolerance < 0) { offsetTolerance = isotopomerEnvelope.Envelope.Length; } for (var isotopeIndex = mostAbundantIsotopeIndex - offsetTolerance; isotopeIndex <= mostAbundantIsotopeIndex + offsetTolerance; isotopeIndex++) { var monoIsotopeMass = Ion.GetMonoIsotopicMass(peak.Mz, charge, isotopeIndex); var observedPeaks = GetAllIsotopePeaks(spectrum, monoIsotopeMass, charge, isotopomerEnvelope, tolerance, 0.1); if (observedPeaks == null) { continue; } var envelop = isotopomerEnvelope.Envelope; var observedIntensities = new double[observedPeaks.Length]; var observedPeakCount = 0; for (var i = 0; i < observedPeaks.Length; i++) { var observedPeak = observedPeaks[i]; if (observedPeak != null && peakUsed[observedPeak.Item2]) { observedPeak = null; observedPeaks[i] = null; } observedPeakCount += observedPeak != null ? 1 : 0; observedIntensities[i] = observedPeak != null ? (float)observedPeak.Item1.Intensity : 0.0; } var sim = FitScoreCalculator.GetDistanceAndCorrelation(envelop, observedIntensities); var bcDist = sim.Item1; var corr = sim.Item2; var foundPeakRatio = observedPeakCount / ((double)envelop.Length); var interferenceScore = 10.0; var filteredObserved = observedPeaks.Where(p => p != null).ToArray(); if (filteredObserved.Length >= 2) { var allPeaks = spectrum.Peaks.Where(p => p.Mz >= filteredObserved[0].Item1.Mz && p.Mz <= filteredObserved[filteredObserved.Length - 1].Item1.Mz).ToArray(); interferenceScore = CalculateInterferenceScore(allPeaks, filteredObserved); } bcDist = Math.Max(bcDist, double.Epsilon); if (corr < corrScoreThreshold && bcDist > 0.1) { continue; } var score = (foundPeakRatio * corr) / (bcDist * (Math.Abs(mostAbundantIsotopeIndex - isotopeIndex) + 1) * interferenceScore); //if (corr < corrScoreThreshold) continue; // monoIsotopeMass is valid if (score >= bestScore) { bestScore = score; bestPeak = new DeconvolutedPeak(monoIsotopeMass, observedIntensities[mostAbundantIsotopeIndex], charge, corr, bcDist, observedPeaks.Where(p => p != null).Select(p => p.Item1).ToArray()); bestObservedPeaks = observedPeaks; } } } if (bestPeak != null) { monoIsotopePeakList.Add(bestPeak); foreach (var p in bestObservedPeaks) { if (p != null) { bestPeak.ObservedPeakIndices.Add(p.Item2); peakUsed[p.Item2] = true; } } } } 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); } }