public double GetMZOfObservedPeakClosestToTargetVal(double targetMZ) { if (IsotopicProfile?.Peaklist == null) { return(0); } var indexOfTargetPeak = PeakUtilities.getIndexOfClosestValue(IsotopicProfile.Peaklist, targetMZ, 0, IsotopicProfile.Peaklist.Count - 1, 0.1); if (indexOfTargetPeak != -1) { return(IsotopicProfile.Peaklist[indexOfTargetPeak].XValue); } return(0); }
private void combineIsotopicProfiles(IsotopicProfile baseIso, IsotopicProfile addedIso, double toleranceInPPM = 50) { var toleranceInMZ = toleranceInPPM * baseIso.MonoPeakMZ / 1e6; foreach (var msPeak in addedIso.Peaklist) { var indexOfPeakInBaseIos = PeakUtilities.getIndexOfClosestValue(baseIso.Peaklist, msPeak.XValue, 0, baseIso.Peaklist.Count - 1, toleranceInMZ); if (indexOfPeakInBaseIos == -1) { baseIso.Peaklist.Add(msPeak); } } baseIso.Peaklist = baseIso.Peaklist.OrderBy(p => p.XValue).ToList(); }
public double CalculateFitScore(IsotopicProfile theorProfile, IsotopicProfile observedProfile, XYData massSpecXYData, int numberOfPeaksToLeftForPenalty = 0, double massErrorPPMBetweenPeaks = 15) { if (observedProfile == null || observedProfile.Peaklist == null || observedProfile.Peaklist.Count == 0) { return(1.0); // this is the worst possible fit score. ( 0.000 is the best possible fit score); Maybe we want to return a '-1' to indicate a failure... } var indexOfMostAbundantTheorPeak = theorProfile.GetIndexOfMostIntensePeak(); var indexOfCorrespondingObservedPeak = PeakUtilities.getIndexOfClosestValue(observedProfile.Peaklist, theorProfile.getMostIntensePeak().XValue, 0, observedProfile.Peaklist.Count - 1, 0.1); if (indexOfCorrespondingObservedPeak < 0) // most abundant peak isn't present in the actual theoretical profile... problem! { return(1.0); } var mzOffset = observedProfile.Peaklist[indexOfCorrespondingObservedPeak].XValue - theorProfile.Peaklist[indexOfMostAbundantTheorPeak].XValue; var observedPeakList = observedProfile.Peaklist.Cast <Peak>().ToList(); var theorPeakList = theorProfile.Peaklist.Cast <Peak>().ToList(); foreach (var peak in theorPeakList)//May want to avoid this offset if the masses have been aligned using LCMS Warp { peak.XValue += mzOffset; } var minCuttoffTheorPeakIntensityFraction = 0.1f; var peakFitter = new PeakLeastSquaresFitter(); int ionCountUsed; var fitval = peakFitter.GetFit( theorPeakList, observedPeakList, minCuttoffTheorPeakIntensityFraction, massErrorPPMBetweenPeaks, numberOfPeaksToLeftForPenalty, out ionCountUsed); if (double.IsNaN(fitval) || fitval > 1) { fitval = 1; } return(fitval); }
private void addMassInfoToIsotopicProfile(IsotopicProfile theorFeature, IsotopicProfile outFeature) { var indexOfMonoPeak = PeakUtilities.getIndexOfClosestValue(outFeature.Peaklist, theorFeature.MonoPeakMZ, 0, outFeature.Peaklist.Count - 1, 0.1); outFeature.MonoIsotopicPeakIndex = indexOfMonoPeak; double monopeakMZ = 0; double monoIsotopicMass = 0; var monoPeakFoundInObservedIso = (outFeature.MonoIsotopicPeakIndex != -1); if (monoPeakFoundInObservedIso) { var monoPeak = outFeature.Peaklist[outFeature.MonoIsotopicPeakIndex]; monopeakMZ = monoPeak.XValue; monoIsotopicMass = (monoPeak.XValue - Globals.PROTON_MASS) * outFeature.ChargeState; } else { var indexOfMostAbundantTheorPeak = theorFeature.GetIndexOfMostIntensePeak(); var indexOfCorrespondingObservedPeak = PeakUtilities.getIndexOfClosestValue(outFeature.Peaklist, theorFeature.Peaklist[indexOfMostAbundantTheorPeak].XValue, 0, outFeature.Peaklist.Count - 1, 0.1); if (indexOfCorrespondingObservedPeak != -1) { //double mzOffset = outFeature.Peaklist[indexOfCorrespondingObservedPeak].XValue - theorFeature.Peaklist[indexOfMostAbundantTheorPeak].XValue; var locationOfMonoPeakRelativeToTheorMaxPeak = theorFeature.MonoIsotopicPeakIndex - indexOfMostAbundantTheorPeak; var mzOfObservedMostAbundantPeak = outFeature.Peaklist[indexOfCorrespondingObservedPeak].XValue; monopeakMZ = mzOfObservedMostAbundantPeak + (locationOfMonoPeakRelativeToTheorMaxPeak * Globals.MASS_DIFF_BETWEEN_ISOTOPICPEAKS / outFeature.ChargeState); monoIsotopicMass = (monopeakMZ - Globals.PROTON_MASS) * outFeature.ChargeState; } } outFeature.MonoPeakMZ = monopeakMZ; outFeature.MonoIsotopicMass = monoIsotopicMass; }
private double getFitValue(XYData rawXYData, IsotopicProfile theorIso, IsotopicProfile isoN15) { var indexOfMostAbundantTheorPeak = theorIso.GetIndexOfMostIntensePeak(); var indexOfCorrespondingObservedPeak = PeakUtilities.getIndexOfClosestValue(isoN15.Peaklist, theorIso.getMostIntensePeak().XValue, 0, isoN15.Peaklist.Count - 1, 0.1); var mzOffset = isoN15.Peaklist[indexOfCorrespondingObservedPeak].XValue - theorIso.Peaklist[indexOfMostAbundantTheorPeak].XValue; var fwhm = isoN15.GetFWHM(); var theorXYData = theorIso.GetTheoreticalIsotopicProfileXYData(isoN15.GetFWHM()); theorXYData.OffSetXValues(mzOffset); //May want to avoid this offset if the masses have been aligned using LCMS Warp areafitter = new AreaFitter(); var fitval = areafitter.GetFit(theorXYData, rawXYData, 0.1); if (fitval == double.NaN || fitval > 1) { fitval = 1; } return(fitval); }
public override void Execute(ResultCollection resultList) { //don't do anything if there aren't any MSFeatures if (resultList.IsosResultBin == null || resultList.IsosResultBin.Count == 0) { return; } //don't do anything if there aren't any peaks if (resultList.Run.PeakList == null || resultList.Run.PeakList.Count == 0) { return; } foreach (var msfeature in resultList.IsosResultBin) { foreach (var peak in msfeature.IsotopicProfile.Peaklist) { var targetMZ = peak.XValue; toleranceInPPM = 0.1d; var toleranceInMZ = toleranceInPPM * targetMZ / 1e6; //binary search to find peak var indexOfPeak = PeakUtilities.getIndexOfClosestValue(resultList.Run.PeakList, targetMZ, 0, resultList.Run.PeakList.Count - 1, toleranceInMZ); if (indexOfPeak != -1) { var foundpeak = resultList.Run.PeakList[indexOfPeak]; if (foundpeak is MSPeak) { ((MSPeak)foundpeak).MSFeatureID = peak.MSFeatureID; } } } } }
public double CalculateFitScore(IsotopicProfile theorProfile, IsotopicProfile observedProfile, XYData massSpecXYData) { if (observedProfile == null || observedProfile.Peaklist == null || observedProfile.Peaklist.Count == 0) { return(1.0); // this is the worst possible fit score. ( 0.000 is the best possible fit score); Maybe we want to return a '-1' to indicate a failure... } var indexOfMostAbundantTheorPeak = theorProfile.GetIndexOfMostIntensePeak(); var indexOfCorrespondingObservedPeak = PeakUtilities.getIndexOfClosestValue(observedProfile.Peaklist, theorProfile.getMostIntensePeak().XValue, 0, observedProfile.Peaklist.Count - 1, 0.1); if (indexOfCorrespondingObservedPeak < 0) // most abundant peak isn't present in the actual theoretical profile... problem! { return(1.0); } var mzOffset = observedProfile.Peaklist[indexOfCorrespondingObservedPeak].XValue - theorProfile.Peaklist[indexOfMostAbundantTheorPeak].XValue; var theorXYData = theorProfile.GetTheoreticalIsotopicProfileXYData(observedProfile.GetFWHM()); //theorXYData.Display(); theorXYData.OffSetXValues(mzOffset); //May want to avoid this offset if the masses have been aligned using LCMS Warp //theorXYData.Display(); var areafitter = new AreaFitter(); int ionCountUsed; var fitval = areafitter.GetFit(theorXYData, massSpecXYData, 0.1, out ionCountUsed); if (double.IsNaN(fitval) || fitval > 1) { fitval = 1; } return(fitval); }
public IsotopicProfileComponent FindBestLabeledProfile(TargetBase target, List <Peak> massSpectrumPeakList, XYData massSpectrumXYData = null) { IsotopicProfile bestIso = null; var bestFitScore = 1.0; double bestLabelAmount = -1; Check.Require(target != null, "Target is null. You need a valid target in order to quantify it."); Check.Require(target.IsotopicProfile != null, "Target's theoretical isotopic profile is null. You need to create it first."); //create theor profiles to iterate over and use in fitting _theorLabeledProfiles.Clear(); FitScoreData.Clear(); for (var labelAmount = MinLabelAmount; labelAmount < MaxLabelAmount; labelAmount = labelAmount + StepAmountForIterator) { var theorIso = _isoCreator.CreateIsotopicProfileFromEmpiricalFormula(target.EmpiricalFormula, _elementLabeled, _lightIsotope, _heavyIsotope, labelAmount, target.ChargeState); var indexOfMostAbundantTheorPeak = theorIso.GetIndexOfMostIntensePeak(); var indexOfCorrespondingObservedPeak = PeakUtilities.getIndexOfClosestValue(massSpectrumPeakList, theorIso.getMostIntensePeak().XValue, 0, massSpectrumPeakList.Count - 1, 0.1); double fitScore; var obsPeakListForFitter = new List <Peak>(); if (indexOfCorrespondingObservedPeak < 0) // most abundant peak isn't present in the actual theoretical profile... problem! { fitScore = 1; } else { //double mzOffset = massSpectrumPeakList[indexOfCorrespondingObservedPeak].XValue - theorIso.Peaklist[indexOfMostAbundantTheorPeak].XValue; var minIntensityForFitting = 0.02; var theorPeakListForFitter = new List <Peak>(theorIso.Peaklist).Where(p => p.Height > minIntensityForFitting).ToList(); obsPeakListForFitter = FilterPeaksBasedOnBasePeakList(theorPeakListForFitter, massSpectrumPeakList); AddLeftZeroPads(obsPeakListForFitter, NumLeftZeroPads, theorIso.ChargeState); AddRightZeroPads(obsPeakListForFitter, NumRightZeroPads, theorIso.ChargeState); if (IsTheoreticalTrimmedDownToObserved) { theorPeakListForFitter = FilterPeaksBasedOnBasePeakList(obsPeakListForFitter, theorPeakListForFitter); } //foreach (var peak in obsPeakListForFitter) //{ // Console.WriteLine(peak.XValue + "\t" + peak.Height); //} //foreach (var peak in theorPeakListForFitter) //{ // Console.WriteLine(peak.XValue + "\t" + peak.Height); //} const int numPeaksToTheLeftForScoring = 0; fitScore = _leastSquaresFitter.GetFit(theorPeakListForFitter, obsPeakListForFitter, 0, 30, numPeaksToTheLeftForScoring, out var ionCountUsed); //if (double.IsNaN(fitScore) || fitScore > 1) fitScore = 1; } if (fitScore < bestFitScore) { bestFitScore = fitScore; if (massSpectrumXYData == null) { bestIso = new IsotopicProfile { Peaklist = new List <MSPeak>() }; foreach (var peak in obsPeakListForFitter) { bestIso.Peaklist.Add(new MSPeak(peak.XValue, peak.Height, peak.Width, 0)); } bestIso.ChargeState = theorIso.ChargeState; } else { bestIso = _iterativeTff.IterativelyFindMSFeature(massSpectrumXYData, theorIso); } if (bestIso != null) { bestIso.Score = fitScore; } bestLabelAmount = labelAmount; } FitScoreData.Add((decimal)labelAmount, fitScore); _theorLabeledProfiles.Add(theorIso); } var isoComponent = new IsotopicProfileComponent(bestIso, 0, bestLabelAmount); return(isoComponent); }
public override void Execute(ResultCollection resultList) { Check.Require(resultList.CurrentTargetedResult is SipperLcmsTargetedResult, "Sipper Quantifier only works on Sipper-type result objects"); Check.Require(resultList.Run.CurrentMassTag != null, this.Name + " failed; CurrentMassTag is empty"); Check.Require(resultList.Run.CurrentMassTag.IsotopicProfile != null, this.Name + " failed; Theor isotopic profile is empty. Run a TheorFeatureGenerator"); ResetQuantifierData(); var result = (SipperLcmsTargetedResult)resultList.CurrentTargetedResult; result.AreaUnderDifferenceCurve = -9999; result.AreaUnderRatioCurve = -9999; RatioVals.Xvalues = new double[] { 1, 2, 3, 4, 5, 6, 7 }; RatioVals.Yvalues = new double[] { 0, 0, 0, 0, 0, 0, 0 }; ChromatogramRSquaredVals.Clear(); FitScoreData.Clear(); Check.Require(result != null, "No MassTagResult has been generated for CurrentMassTag"); if (result.IsotopicProfile == null || result.IsotopicProfile.Peaklist == null || result.IsotopicProfile.Peaklist.Count < 2) { return; } var theorUnlabelledIso = resultList.Run.CurrentMassTag.IsotopicProfile.CloneIsotopicProfile(); IsotopicProfileUtilities.NormalizeIsotopicProfile(theorUnlabelledIso); //PeakUtilities.TrimIsotopicProfile(unlabeledIso, 0.001); var indexOfCorrespondingObservedPeak = PeakUtilities.getIndexOfClosestValue(result.IsotopicProfile.Peaklist, theorUnlabelledIso.getMostIntensePeak().XValue, 0, result.IsotopicProfile.Peaklist.Count - 1, 0.1); NormalizedIso = result.IsotopicProfile.CloneIsotopicProfile(); if (indexOfCorrespondingObservedPeak >= 0) { IsotopicProfileUtilities.NormalizeIsotopicProfileToSpecificPeak(NormalizedIso, indexOfCorrespondingObservedPeak); } else { IsotopicProfileUtilities.NormalizeIsotopicProfile(NormalizedIso); } //insert zero intensity peaks into observed // return; if (result.Flags.Count > 0) { var flagstring = ""; foreach (var resultFlag in result.Flags) { flagstring += resultFlag.Description + ";"; } result.ErrorDescription = flagstring.TrimEnd(';'); result.FailedResult = true; if (flagstring.Contains("PeakToTheLeft")) { result.FailureType = Globals.TargetedResultFailureType.DeisotopingProblemDetected; } else { result.FailureType = Globals.TargetedResultFailureType.QuantifierFailure; } } var resultPassesMinimalCriteria = (result.Score < MaximumFitScoreForFurtherProcessing && result.Flags.Count == 0); if (resultPassesMinimalCriteria) { //------------------------------------- Get chromatogramCorrelation data ------------------------------- if (IsChromatogramCorrelationPerformed) { GetChromatogramCorrelationData(result); } NormalizedAdjustedIso = NormalizedIso.CloneIsotopicProfile(); //this is experimental!! it attempts to correct the problems caused by Orbitraps on isotopic profile intensities //UpdateIsoIntensitiesUsingChromCorrData(result.ChromCorrelationData, NormalizedAdjustedIso); //----------------- create ratio data ------------------------------------------------------- var ratioData = NormalizedAdjustedIso.CloneIsotopicProfile(); for (var i = 0; i < NormalizedIso.Peaklist.Count; i++) { if (i < theorUnlabelledIso.Peaklist.Count && theorUnlabelledIso.Peaklist[i].Height > MinimumRelativeIntensityForRatioCalc) { ratioData.Peaklist[i].Height = (NormalizedIso.Peaklist[i].Height / theorUnlabelledIso.Peaklist[i].Height - 1); } else { ratioData.Peaklist[i].Height = 0; } } //trim off zeros from ratio data for (var i = ratioData.Peaklist.Count - 1; i >= 0; i--) { if (ratioData.Peaklist[i].Height == 0) { ratioData.Peaklist.RemoveAt(i); } else { break; } } var xvals = ratioData.Peaklist.Select((p, i) => new { peak = p, index = i }).Select(n => (double)n.index).ToList(); var yvals = ratioData.Peaklist.Select(p => (double)p.Height).ToList(); result.AreaUnderRatioCurve = yvals.Sum(); RatioVals.Xvalues = xvals.ToArray(); RatioVals.Yvalues = yvals.ToArray(); //NOTE: Sept 23, 2013 - the 'R is no longer used. result.RSquaredValForRatioCurve = GetLinearRegressionData(result, xvals, yvals); //------------- subtract unlabelled profile from normalized profile ---------------------- var subtractedIsoData = NormalizedAdjustedIso.CloneIsotopicProfile(); for (var i = 0; i < subtractedIsoData.Peaklist.Count; i++) { float intensityTheorPeak = 0; if (i < theorUnlabelledIso.Peaklist.Count) { intensityTheorPeak = theorUnlabelledIso.Peaklist[i].Height; } var subtractedIntensity = subtractedIsoData.Peaklist[i].Height - intensityTheorPeak; if (subtractedIntensity < 0) { subtractedIntensity = 0; } subtractedIsoData.Peaklist[i].Height = subtractedIntensity; } result.AreaUnderDifferenceCurve = subtractedIsoData.Peaklist.Select(p => p.Height).Sum(); //----------- get data for the subtracted, labeled isotopic profile------------------------ var peaksForLabeledIsoQuant = new List <Peak>(subtractedIsoData.Peaklist.Where(p => p.Height > 0)); // var isoFromPartialLabelingQuantifier = _partialLabelingQuantifier.FindBestLabeledProfile(result.Target, peaksForLabeledIsoQuant); //FitScoreData = _partialLabelingQuantifier.FitScoreData; //result.FitScoreLabeledProfile = isoFromPartialLabelingQuantifier.IsotopicProfile == null ? 1.00d : isoFromPartialLabelingQuantifier.IsotopicProfile.Score; //result.PercentCarbonsLabelled = isoFromPartialLabelingQuantifier.PercentLabeling; //int numCarbons = result.Target.GetAtomCountForElement("C"); //result.NumCarbonsLabelled = result.PercentCarbonsLabelled * numCarbons / 100; //StringBuilder sb = new StringBuilder(); //sb.Append(result.Target.ID + "\t" + result.Target.MZ.ToString("0.0000") + "\t" + result.Target.ChargeState + // "-----------------------------\n"); //int counter = 0; //foreach (var fsData in _partialLabelingQuantifier.FitScoreData) //{ // sb.Append(fsData.Key + "\t" + fsData.Value.ToString("0.000") + "\n"); // counter++; //} //Console.WriteLine(sb.ToString()); //-------------- make calculations using inputs from chrom correlation data ------------------- HighQualitySubtractedProfile = GetIsoDataPassingChromCorrelation(result.ChromCorrelationData, subtractedIsoData); var contiguousnessScore = GetContiguousnessScore(HighQualitySubtractedProfile); result.ContiguousnessScore = contiguousnessScore; //GORD ------------- note this section is a duplicate of the above.... choose one or the other ------------------------ //Feb 26, 2013... I processed the results and these look really good. ROC curve nice peaksForLabeledIsoQuant = new List <Peak>(HighQualitySubtractedProfile.Peaklist.Where(p => p.Height > 0)); //calculate labeled fit score var isoFromPartialLabelingQuantifier = _partialLabelingQuantifier.FindBestLabeledProfile(result.Target, peaksForLabeledIsoQuant); FitScoreData = _partialLabelingQuantifier.FitScoreData; result.FitScoreLabeledProfile = isoFromPartialLabelingQuantifier.IsotopicProfile == null ? 1.00d : isoFromPartialLabelingQuantifier.IsotopicProfile.Score; result.PercentCarbonsLabelled = isoFromPartialLabelingQuantifier.PercentLabeling; var numCarbons = result.Target.GetAtomCountForElement("C"); result.NumCarbonsLabelled = result.PercentCarbonsLabelled * numCarbons / 100; //end of section -------------------------------------------------------------------------- //-------------- calculate Label Distribution ------------------------------------------------ // see Chik et al: http://pubs.acs.org/doi/abs/10.1021/ac050988l ; Also see Sipper paper. double[] numLabelVals; double[] labelDistributionVals; var theorIntensityVals = theorUnlabelledIso.Peaklist.Select(p => (double)p.Height).ToList(); if (isoFromPartialLabelingQuantifier.IsotopicProfile != null) { var normalizedCorrectedIntensityVals = NormalizedAdjustedIso.Peaklist.Select(p => (double)p.Height).ToList(); var numRightPads = 3; _labelingDistributionCalculator.CalculateLabelingDistribution(theorIntensityVals, normalizedCorrectedIntensityVals, LabeldistCalcIntensityThreshold, LabeldistCalcIntensityThreshold, out numLabelVals, out labelDistributionVals, true, true, 0, numRightPads, 0, 0); //negative distribution values are zeroed out. And, then the remaining values are adjusted such that they add up to 1. result.LabelDistributionVals = AdjustLabelDistributionVals(labelDistributionVals); double distFractionUnlabelled, distFractionLabelled, distAverageLabelsIncorporated; _labelingDistributionCalculator.OutputLabelingInfo(result.LabelDistributionVals, out distFractionUnlabelled, out distFractionLabelled, out distAverageLabelsIncorporated); result.PercentPeptideLabelled = distFractionLabelled * 100; } else { result.PercentCarbonsLabelled = 0; } var highQualityRatioProfileData = GetIsoDataPassingChromCorrelation(result.ChromCorrelationData, ratioData); if (highQualityRatioProfileData.Peaklist != null && highQualityRatioProfileData.Peaklist.Count > 0) { result.AreaUnderDifferenceCurve = HighQualitySubtractedProfile.Peaklist.Select(p => p.Height).Sum(); } else { result.AreaUnderDifferenceCurve = 0; } if (HighQualitySubtractedProfile.Peaklist != null && HighQualitySubtractedProfile.Peaklist.Count > 0) { result.AreaUnderRatioCurveRevised = highQualityRatioProfileData.Peaklist.Select(p => p.Height).Sum(); } else { result.AreaUnderRatioCurveRevised = 0; } result.NumHighQualityProfilePeaks = highQualityRatioProfileData.Peaklist != null ? highQualityRatioProfileData.Peaklist.Count : 0; } else { result.FailedResult = true; if (result.FailureType == Globals.TargetedResultFailureType.None) { result.FailureType = Globals.TargetedResultFailureType.MinimalCriteriaNotMet; } } }