private void addIsotopePeaks(IsotopicProfile foundO16O18Profile, IsotopicProfile profileToAdd, int numIsotopePeaksToAdd) { if (profileToAdd?.Peaklist == null || profileToAdd.Peaklist.Count == 0) { return; } for (var i = 0; i < numIsotopePeaksToAdd; i++) { if (i < profileToAdd.Peaklist.Count) { var peakMz = profileToAdd.Peaklist[i].XValue; var toleranceInMZ = ToleranceInPPM / 1e6 * peakMz; var peaksAlreadyThere = PeakUtilities.GetMSPeaksWithinTolerance(foundO16O18Profile.Peaklist, peakMz, toleranceInMZ); if (peaksAlreadyThere == null || peaksAlreadyThere.Count == 0) { foundO16O18Profile.Peaklist.Add(profileToAdd.Peaklist[i]); } } else // if profileToAdd doesn't have enough peaks { break; } } foundO16O18Profile.Peaklist = foundO16O18Profile.Peaklist.OrderBy(p => p.XValue).ToList(); }
private IsotopicProfile GetTheorIsotopicProfile(IsosResult saturatedFeature, double lowerIntensityCutoff = 0.0001) { var theorTarget = new PeptideTarget { ChargeState = (short)saturatedFeature.IsotopicProfile.ChargeState, MonoIsotopicMass = saturatedFeature.IsotopicProfile.MonoIsotopicMass }; theorTarget.MZ = (theorTarget.MonoIsotopicMass / theorTarget.ChargeState) + Globals.PROTON_MASS; var averagineFormula = _tomIsotopicPatternGenerator.GetClosestAvnFormula(saturatedFeature.IsotopicProfile.MonoIsotopicMass, false); theorTarget.IsotopicProfile = _tomIsotopicPatternGenerator.GetIsotopePattern(averagineFormula, _tomIsotopicPatternGenerator.aafIsos); theorTarget.EmpiricalFormula = averagineFormula; theorTarget.CalculateMassesForIsotopicProfile(saturatedFeature.IsotopicProfile.ChargeState); //NOTE: This is critical to choosing the optimum peak of the observed isotopic profile //A value of 0.001 will leave more peaks in the theor profile. This //can be bad with co-eluting peptides, so that a peak of the interfering peptide //is used to correct the intensity of our target peptide. //A value of 0.01 helps prevent this (by trimming the peaks of the theor profile, //and reducing the peaks to be considered for peak intensity extrapolation of the target peptide. PeakUtilities.TrimIsotopicProfile(theorTarget.IsotopicProfile, lowerIntensityCutoff); return(theorTarget.IsotopicProfile); }
private void lookForMissingPeaksAndInsertZeroIntensityPeaksWhenMissing(IsotopicProfile o16o18Profile, IsotopicProfile theorFeature) { if (o16o18Profile.Peaklist.Count == 0) { return; } var mzDistanceBetweenIsotopes = 1.003 / o16o18Profile.ChargeState; var monoMZ = theorFeature.getMonoPeak().XValue; var indexOfLastPeak = o16o18Profile.Peaklist.Count - 1; var toleranceInDa = 0.1; //this will iterate over the first five expected m/z values of a theoretical profile //and loosely try to the corresponding peak within the observed profile. //If missing, will add one at the expected m/z. This ensures no missing peaks within the O16O18 profile //so that looking up the first peak will always give you the intensity of the O16 peak (even if //it never existed in the real data - in this case the intensity is 0); for (var i = 0; i < 6; i++) { var currentMZ = monoMZ + mzDistanceBetweenIsotopes * i; var peaksWithinTol = PeakUtilities.GetMSPeaksWithinTolerance(o16o18Profile.Peaklist, currentMZ, toleranceInDa); if (peaksWithinTol.Count == 0) // { o16o18Profile.Peaklist.Insert(i, new MSPeak(currentMZ, 0, 0, 0)); } } }
private bool findPeakWithinMSFeatureResults(List <IsosResult> msFeatureList, MSPeakResult peakResult, double scanTolerance) { double toleranceInPPM = 5; var toleranceInMZ = toleranceInPPM / 1e6 * peakResult.MSPeak.XValue; foreach (var msfeature in msFeatureList) { if (msfeature.IsotopicProfile == null || msfeature.IsotopicProfile.Peaklist == null || msfeature.IsotopicProfile.Peaklist.Count == 0) { continue; } //check target peak is within an allowable scan tolerance var targetPeakIsWithinScanTol = Math.Abs(msfeature.ScanSet.PrimaryScanNumber - peakResult.Scan_num) <= scanTolerance; if (!targetPeakIsWithinScanTol) { continue; } var peaksWithinTol = PeakUtilities.GetMSPeaksWithinTolerance(msfeature.IsotopicProfile.Peaklist, peakResult.MSPeak.XValue, toleranceInMZ); if (peaksWithinTol.Count == 0) { continue; } else { return(true); } } return(false); }
private List <Peak> FilterPeaksBasedOnBasePeakList(List <Peak> basePeaklist, List <Peak> inputPeakList) { var trimmedPeakList = new List <Peak>(); foreach (var peak in basePeaklist) { var mzTolerance = _toleranceInPPM * peak.XValue / 1e6; var foundPeaks = PeakUtilities.GetPeaksWithinTolerance(inputPeakList, peak.XValue, mzTolerance); if (foundPeaks.Count == 1) { trimmedPeakList.Add(foundPeaks.First()); } else if (foundPeaks.Count > 1) { trimmedPeakList.Add(foundPeaks.OrderByDescending(p => p.Height).First()); } } //if we can't find any observed peaks, we won't trim anything. Just return the original list if (!trimmedPeakList.Any()) { return(basePeaklist); } return(trimmedPeakList); }
/// <summary> /// Returns a 'peak-to-the-left' of the monoisotopic peak if: 1) it exists and 2) it is above the user provided relative intensity /// </summary> /// <param name="monoPeak"></param> /// <param name="chargeState"></param> /// <param name="peakList"></param> /// <param name="minRelIntensityForFlag"></param> /// <returns></returns> public MSPeak LookforPeakToTheLeftOfMonoPeak(MSPeak monoPeak, int chargeState, List <Peak> peakList, double minRelIntensityForFlag) { double mzTol = monoPeak.Width; var targetMZ = monoPeak.XValue - (1.003 / (double)chargeState); var foundLeftOfMonoPeaks = PeakUtilities.GetPeaksWithinTolerance(peakList, targetMZ, mzTol); //if found a peak to the left, will return that peak. If if (foundLeftOfMonoPeaks.Count == 0) { return(null); } var peakToTheLeft = foundLeftOfMonoPeaks.OrderByDescending(p => p.Height).First() as MSPeak; if (peakToTheLeft == null) { return(null); } if (peakToTheLeft.Height > monoPeak.Height * MinRatioToGiveFlag) { return(peakToTheLeft); } return(null); }
private IsotopicProfile GetUnlabelledIsotopicProfile(TargetBase mt) { var iso = new IsotopicProfile(); try { //empirical formula may contain non-integer values for iso = _isotopicDistCalculator.GetIsotopePattern(mt.EmpiricalFormula); } catch (Exception ex) { throw new Exception("Theoretical feature generator failed. Details: " + ex.Message); } PeakUtilities.TrimIsotopicProfile(iso, LowPeakCutOff); iso.ChargeState = mt.ChargeState; if (iso.ChargeState != 0) { calculateMassesForIsotopicProfile(iso, mt.MonoIsotopicMass, mt.ChargeState); } return(iso); }
public void GetRatioBasedOnTopPeaks(IsotopicProfile iso1, IsotopicProfile iso2, IsotopicProfile theorIso1, IsotopicProfile theorIso2, double backgroundIntensity, int numPeaks, out double ratio, out double ratioContribForIso1, out double ratioContribForIso2) { //Find top n peaks of the theor profile. Reference them by their peak index so that they can be looked up in the experimental. //List<int> sortedTheorIso1Indexes = getIndexValsOfTopPeaks(theorIso1); //List<int> sortedTheorIso2Indexes = getIndexValsOfTopPeaks(theorIso2); IList <Peak> topTheorIso1Peaks = GetTopPeaks(theorIso1, numPeaks); IList <Peak> topTheorIso2Peaks = GetTopPeaks(theorIso2, numPeaks); //get the top n peaks of the experimental profile, based on peaks of the theor profile //adjust intensity (subtract background intensity) var topIso1Peaks = GetTopPeaksBasedOnTheor(iso1, topTheorIso1Peaks, MSToleranceInPPM); var topIso2Peaks = GetTopPeaksBasedOnTheor(iso2, topTheorIso2Peaks, MSToleranceInPPM); //Since the number of top experimental iso peaks may be less than the number of top theor iso peaks, //we have to filter and ensure that they have the same peak numbers, so that the correction factor // (applied below) is properly applied. HOWEVER, this may induce some differences between runs var filteredTopTheorIso1Peaks = GetTopPeaksBasedOnTheor(theorIso1, topIso1Peaks, MSToleranceInPPM); var filteredTopTheorIso2Peaks = GetTopPeaksBasedOnTheor(theorIso2, topIso2Peaks, MSToleranceInPPM); var summedTopIso1PeakIntensities = PeakUtilities.GetSumOfIntensities(topIso1Peaks, backgroundIntensity); var summedTopIso2PeakIntensities = PeakUtilities.GetSumOfIntensities(topIso2Peaks, backgroundIntensity); //Console.WriteLine(backgroundIntensity); //need to find out the contribution of the top n peaks to the overall isotopic envelope. Base this on the theor profile double summedTheorIso1Intensities = filteredTopTheorIso1Peaks.Sum(p => (p.Height)); double summedTheorIso2Intensities = filteredTopTheorIso2Peaks.Sum(p => (p.Height)); var fractionTheor1 = summedTheorIso1Intensities / theorIso1.Peaklist.Sum(p => p.Height); var fractionTheor2 = summedTheorIso2Intensities / theorIso2.Peaklist.Sum(p => p.Height); //use the above ratio to correct the intensities based on how much the top n peaks contribute to the profile. var correctedIso1SummedIntensity = summedTopIso1PeakIntensities / fractionTheor1; var correctedIso2SummedIntensity = summedTopIso2PeakIntensities / fractionTheor2; var summedAllIsos1PeakIntensities = iso1.Peaklist.Sum(p => (p.Height - backgroundIntensity)); var summedAllIsos2PeakIntensities = iso2.Peaklist.Sum(p => (p.Height - backgroundIntensity)); switch (RatioType) { case RatioType.ISO1_OVER_ISO2: ratio = correctedIso1SummedIntensity / correctedIso2SummedIntensity; break; case RatioType.ISO2_OVER_ISO1: ratio = correctedIso2SummedIntensity / correctedIso1SummedIntensity; break; default: ratio = correctedIso1SummedIntensity / correctedIso2SummedIntensity; break; } ratioContribForIso1 = summedTopIso1PeakIntensities / summedAllIsos1PeakIntensities * 1 / fractionTheor1; //we expect a value of '1' ratioContribForIso2 = summedTopIso2PeakIntensities / summedAllIsos2PeakIntensities * 1 / fractionTheor2; }
public override void GenerateTheorFeature(TargetBase mt) { Check.Require(mt != null, "FeatureGenerator failed. Target must not be null."); var iso = IsotopicDistCalculator.GetAveraginePattern(mt.MZ); PeakUtilities.TrimIsotopicProfile(iso, LowPeakCutOff); iso.ChargeState = mt.ChargeState; mt.IsotopicProfile = iso; }
public void FindPeaksWithinTolerance_highMZRangeTest() { List <IPeak> peakList = TestUtilities.GeneratePeakList(new ScanSet(6005)); List <IPeak> filteredList = PeakUtilities.GetPeaksWithinTolerance(peakList, 1500, 100); Assert.AreEqual(809, peakList.Count); TestUtilities.DisplayPeaks(filteredList); Assert.AreEqual(115, filteredList.Count); }
private void GetIsotopicProfilePeaks(List <Peak> peakList, int chargeState, double monoMass, ref IsotopicProfile inputProfile) { double toleranceInPPM = 20; var tff = new BasicTFF(toleranceInPPM); var theorProfile = new IsotopicProfile(); theorProfile.MonoIsotopicMass = monoMass; theorProfile.ChargeState = chargeState; theorProfile.MonoPeakMZ = monoMass / chargeState + Globals.PROTON_MASS; //a hack to guess how many peaks to include in the theor isotopic profile int numPeaksToIncludeInProfile = (int)Math.Round(Math.Max(3, 3 + (monoMass - 1000) / 1000)); double monoPeakMZ = monoMass / chargeState + Globals.PROTON_MASS; for (int i = 0; i < numPeaksToIncludeInProfile; i++) { var peak = new MSPeak(); peak.XValue = monoPeakMZ + i * Globals.MASS_DIFF_BETWEEN_ISOTOPICPEAKS / chargeState; if (i == 0) { peak.Height = 1; } else { peak.Height = 0; } theorProfile.Peaklist.Add(peak); } var foundIso = tff.FindMSFeature(peakList, theorProfile); if (foundIso == null) { var monoPeak = PeakUtilities.GetPeaksWithinTolerance(peakList, monoPeakMZ, toleranceInPPM).OrderByDescending(p => p.Height).FirstOrDefault(); if (monoPeak != null) { inputProfile.Peaklist.Add((MSPeak)monoPeak); } } else { inputProfile.Peaklist = new List <MSPeak>(foundIso.Peaklist); } }
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); }
public void getN15IsotopicProfileTest1() { var isIt = isotopicDistributionCalculator.IsSetToLabeled; var utils = new PeptideUtils(); //string formula = utils.GetEmpiricalFormulaForPeptideSequence("SAMPLERPAMPLERSAMPLERPAMPLERSAMPLERPAMPLERSAMPLERPAMPLER"); var formula = utils.GetEmpiricalFormulaForPeptideSequence("SAMPLERPAMPLERSAMPLERPAMPLER"); var numNitrogens = utils.GetNumAtomsForElement("N", formula); var iso1 = isotopicDistributionCalculator.GetIsotopePattern(formula); PeakUtilities.TrimIsotopicProfile(iso1, 0.001); TestUtilities.DisplayIsotopicProfileData(iso1); isotopicDistributionCalculator.SetLabeling("N", 14, 0.02, 15, 0.98); var iso2 = isotopicDistributionCalculator.GetIsotopePattern(formula); //PeakUtilities.TrimIsotopicProfile(iso2, 0.001); isotopicDistributionCalculator.ResetToUnlabeled(); var iso3 = isotopicDistributionCalculator.GetIsotopePattern(formula); PeakUtilities.TrimIsotopicProfile(iso3, 0.001); Console.WriteLine(); TestUtilities.DisplayIsotopicProfileData(iso2); for (var i = 0; i < iso1.Peaklist.Count; i++) { Assert.AreEqual((decimal)Math.Round(iso1.Peaklist[i].Height, 4), (decimal)Math.Round(iso3.Peaklist[i].Height, 4)); } Console.WriteLine(); TestUtilities.DisplayIsotopicProfileData(iso3); Console.WriteLine("Num nitrogens= " + numNitrogens); }
/// <summary> /// Calculates mass error based on the theoretical most intense peak. /// </summary> /// <returns> This returns the mass error between a theoretical and observed peak. Nota bene the is MASS, not m/z /// If no peak is detected, we return the mass error 999999. This should be interpreted as a null value.</returns> protected double TheorMostIntensePeakMassError(IsotopicProfile theoreticalIso, IsotopicProfile observedIso, int chargeState) { var theoreticalMostIntensePeak = theoreticalIso.getMostIntensePeak(); //find peak in obs data var mzTolerance = WorkflowParameters.MSToleranceInPPM * theoreticalMostIntensePeak.XValue / 1e6; var foundPeaks = PeakUtilities.GetPeaksWithinTolerance(new List <Peak>(observedIso.Peaklist), theoreticalMostIntensePeak.XValue, mzTolerance); if (foundPeaks.Count == 0) { return(999999); } var obsXValue = foundPeaks.OrderByDescending(p => p.Height).First().XValue; //order the peaks and take the first (most intense) one. return((theoreticalMostIntensePeak.XValue * chargeState) - (obsXValue * chargeState)); }
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 IsotopicProfile GetN15IsotopicProfile(TargetBase mt, double lowpeakCutoff) { Check.Require(mt != null, "Mass tag not defined"); Check.Require(mt.IsotopicProfile != null, "Mass tag's theor isotopic profile not defined"); Check.Require(mt.ChargeState != 0, "Can't have a charge state of '0'"); var numNitrogens = mt.GetAtomCountForElement("N"); var labeledTheorProfile = _TomIsotopicPatternGenerator.GetIsotopePattern(mt.EmpiricalFormula, _TomIsotopicPatternGenerator.aafN15Isos); addMZInfoToTheorProfile(mt.IsotopicProfile, labeledTheorProfile, numNitrogens, mt.ChargeState); PeakUtilities.TrimIsotopicProfile(labeledTheorProfile, lowpeakCutoff); labeledTheorProfile.ChargeState = mt.ChargeState; return(labeledTheorProfile); }
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); }
public IsotopicProfile GetN15IsotopicProfile2(TargetBase mt, double lowPeakCutoff) { Check.Require(mt != null, "Mass tag not defined"); Check.Require(mt.IsotopicProfile != null, "Mass tag's theor isotopic profile not defined"); Check.Require(mt.ChargeState != 0, "Can't have a charge state of '0'"); var nitrogenCount = mt.GetAtomCountForElement("N"); _isotopicDistributionCalculator.SetLabeling("N", N14ISOTOPE_NUMBER, this.N14LabelingAmount, N15ISOTOPE_NUMBER, this.N15LabelingAmount); var labeledTheorProfile = _isotopicDistributionCalculator.GetIsotopePattern(mt.EmpiricalFormula); addMZInfoToTheorProfile(mt.IsotopicProfile, labeledTheorProfile, nitrogenCount, mt.ChargeState); _isotopicDistributionCalculator.ResetToUnlabeled(); PeakUtilities.TrimIsotopicProfile(labeledTheorProfile, lowPeakCutoff); labeledTheorProfile.ChargeState = mt.ChargeState; return(labeledTheorProfile); }
private IsosResult getMSFeatureForCurrentSourcePeak(MSPeakResult peakResult, Run run) { if (run.ResultCollection.IsosResultBin == null || run.ResultCollection.IsosResultBin.Count == 0) { return(null); } var isosResultPossiblyContainingSourcePeak = new Dictionary <IsosResult, double>(); //store possible isosResult, along with it's difference with the peakResult for (var i = 0; i < run.ResultCollection.IsosResultBin.Count; i++) { var msfeature = run.ResultCollection.IsosResultBin[i]; double toleranceInMZ = peakResult.MSPeak.Width / 2; var peaksWithinTolerance = PeakUtilities.GetMSPeaksWithinTolerance(msfeature.IsotopicProfile.Peaklist, peakResult.MSPeak.XValue, toleranceInMZ); if (peaksWithinTolerance == null || peaksWithinTolerance.Count == 0) { } else { var diff = Math.Abs(peaksWithinTolerance[0].XValue - peakResult.MSPeak.XValue); isosResultPossiblyContainingSourcePeak.Add(msfeature, diff); } } if (isosResultPossiblyContainingSourcePeak.Count == 0) { return(null); } else if (isosResultPossiblyContainingSourcePeak.Count == 1) { return(isosResultPossiblyContainingSourcePeak.First().Key); } else { return(isosResultPossiblyContainingSourcePeak.Keys.OrderByDescending(p => p.IntensityAggregate).First()); } }
private List <Peak> ApplySmartTrimming(List <Peak> theorPeakList, List <Peak> massSpectrumPeakList) { var trimmedPeakList = new List <Peak>(); foreach (var peak in theorPeakList) { var mzTolerance = _toleranceInPPM * peak.XValue / 1e6; var foundPeaks = PeakUtilities.GetPeaksWithinTolerance(massSpectrumPeakList, peak.XValue, mzTolerance); if (foundPeaks.Any()) { trimmedPeakList.Add(peak); } } //if we can't find any observed peaks, we won't trim anything. Just return the original list if (!trimmedPeakList.Any()) { return(theorPeakList); } return(trimmedPeakList); }
public void GenerateSyntheticMSBasedOnPeakDataTest1() { var fileOutput_xyvalsBefore = FileRefs.OutputFolderPath + "MENDData_scan311_before.txt"; var fileOutput_xyvalsAfter = FileRefs.OutputFolderPath + "MENDData_scan311_after.txt"; Run run = new YAFMSRun(m_testFile); var scan = new ScanSet(311); run.CurrentScanSet = scan; var peakWidthForAllPeaks = 0.001; var synMSGen = new DeconTools.Backend.ProcessingTasks.MSGenerators.SyntheticMSGeneratorFromPeakData(); var msgen = MSGeneratorFactory.CreateMSGenerator(run.MSFileType); msgen.Execute(run.ResultCollection); Assert.AreEqual(15226, run.XYData.Xvalues.Length); TestUtilities.WriteToFile(run.XYData, fileOutput_xyvalsBefore); run.PeakList = PeakUtilities.CreatePeakDataFromXYData(run.XYData, peakWidthForAllPeaks); Assert.AreEqual(15226, run.PeakList.Count); synMSGen.Execute(run.ResultCollection); Assert.AreNotEqual(15226, run.XYData.Xvalues.Length); Console.WriteLine(); Console.WriteLine(run.XYData.Xvalues.Length); TestUtilities.WriteToFile(run.XYData, fileOutput_xyvalsAfter); }
private IsotopicProfile GetUnlabeledIsotopicProfile(TargetBase mt) { IsotopicProfile iso; try { iso = _tomIsotopicPatternGenerator.GetIsotopePattern(mt.EmpiricalFormula, _tomIsotopicPatternGenerator.aafIsos); } catch (Exception ex) { throw new Exception("Theoretical feature generator failed. Details: " + ex.Message); } PeakUtilities.TrimIsotopicProfile(iso, LowPeakCutOff); iso.ChargeState = mt.ChargeState; if (iso.ChargeState != 0) { calculateMassesForIsotopicProfile(iso, mt); } return(iso); }
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); }
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; }
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 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 void getIsotopicProfileTest1() { var utils = new PeptideUtils(); var formula = utils.GetEmpiricalFormulaForPeptideSequence("SAMPLERPAMPLERSAMPLERPAMPLERSAMPLERPAMPLERSAMPLERPAMPLER"); var iso1 = isotopicDistributionCalculator.GetIsotopePattern(formula); PeakUtilities.TrimIsotopicProfile(iso1, 0.001); var tomGen = new TomIsotopicPattern(); var iso2 = tomGen.GetIsotopePattern(formula); PeakUtilities.TrimIsotopicProfile(iso2, 0.001); for (var i = 0; i < iso1.Peaklist.Count; i++) { var roundedI1 = (decimal)Math.Round(iso1.Peaklist[i].Height, 2); var roundedI2 = (decimal)Math.Round(iso2.Peaklist[i].Height, 2); Assert.AreEqual(roundedI1, roundedI2); } TestUtilities.DisplayIsotopicProfileData(iso1); }
public IsotopicProfile GetDHIsotopicProfile2(TargetBase mt, double lowpeakCutoff, double fractionLabeling, double molarMixingofH) { Check.Require(mt != null, "Mass tag not defined"); Check.Require(mt.IsotopicProfile != null, "Mass tag's theor isotopic profile not defined"); Check.Require(mt.ChargeState != 0, "Can't have a charge state of '0'"); //int numNitrogens = mt.GetAtomCountForElement("N"); var numDeuterium = 0; //_isotopicDistributionCalculator.SetLabeling("H", H_ISOTOPE_NUMBER, this.HLabelingAmount, D_ISOTOPE_NUMBER, this.DLabelingAmount); var hydrogenTheoreticalProfile = _isotopicDistributionCalculator.GetIsotopePattern(mt.EmpiricalFormula); var deuteriumTheoreticalProfile = _isotopicDistributionCalculator.GetIsotopePattern(mt.EmpiricalFormula); HLabelingAmount = molarMixingofH; DLabelingAmount = 1 - molarMixingofH; //convert to floats var labelingAmountFraction = Convert.ToSingle(fractionLabeling); var HLabelingAmountMix = Convert.ToSingle(HLabelingAmount); var DLabelingAmountMix = Convert.ToSingle(DLabelingAmount); //initialization float maxHeightForNormalization = 0; if (hydrogenTheoreticalProfile.Peaklist.Count > 0) { maxHeightForNormalization = hydrogenTheoreticalProfile.Peaklist[0].Height * HLabelingAmountMix; } //add deuterated peaks as an offset index for (var i = 0; i < hydrogenTheoreticalProfile.Peaklist.Count; i++) { var peakH = hydrogenTheoreticalProfile.Peaklist[i]; MSPeak peakD; if (i == 0) //initial peak where there is no D contribution { peakD = new MSPeak(0); } else { peakD = deuteriumTheoreticalProfile.Peaklist[i - 1]; } var contributionH = peakH.Height * HLabelingAmountMix; var contributionD = (1 - labelingAmountFraction) * peakD.Height * DLabelingAmountMix + labelingAmountFraction * peakD.Height * DLabelingAmountMix; peakH.Height = contributionH + contributionD; //peakH.Height = peakH.Height + (1-Convert.ToSingle(fractionLabeling)) * peakD.Height +Convert.ToSingle(fractionLabeling) * peakD.Height; //find true hightes peak in combined distribusion if (peakH.Height > maxHeightForNormalization) { maxHeightForNormalization = peakH.Height; } } //rename for clarity var labeledTheoreticalProfile = hydrogenTheoreticalProfile; //normalize to 1 foreach (var peak in labeledTheoreticalProfile.Peaklist) { peak.Height /= maxHeightForNormalization; } //should be good up to here addMZInfoToTheorProfile(mt.IsotopicProfile, labeledTheoreticalProfile, numDeuterium, mt.ChargeState);//Keep this as the H mass? //_isotopicDistributionCalculator.ResetToUnlabeled(); PeakUtilities.TrimIsotopicProfile(labeledTheoreticalProfile, lowpeakCutoff); labeledTheoreticalProfile.ChargeState = mt.ChargeState; return(labeledTheoreticalProfile); }
public double GetFit( List <Peak> theorPeakList, List <Peak> observedPeakList, double minIntensityForScore, double toleranceInPPM, int numPeaksToTheLeftForScoring, out int ionCountUsed) { Utilities.IqLogger.IqLogger.LogTrace("Min Intensity For Scoring: " + minIntensityForScore); Utilities.IqLogger.IqLogger.LogTrace("PPM Tolerance: " + toleranceInPPM); ionCountUsed = 0; var theorIntensitiesUsedInCalc = new List <double>(); var observedIntensitiesUsedInCalc = new List <double>(); //first gather all the intensities from theor and obs peaks var maxTheorIntensity = double.MinValue; foreach (var peak in theorPeakList) { if (peak.Height > maxTheorIntensity) { maxTheorIntensity = peak.Height; } } for (var index = 0; index < theorPeakList.Count; index++) { var peak = theorPeakList[index]; var overrideMinIntensityCutoff = index < numPeaksToTheLeftForScoring; if (peak.Height > minIntensityForScore || overrideMinIntensityCutoff) { theorIntensitiesUsedInCalc.Add(peak.Height); Utilities.IqLogger.IqLogger.LogTrace("Theoretical Peak Selected! Peak Height: "+ peak.Height + " Peak X-Value: " + peak.XValue); //find peak in obs data var mzTolerance = toleranceInPPM * peak.XValue / 1e6; var foundPeaks = PeakUtilities.GetPeaksWithinTolerance(observedPeakList, peak.XValue, mzTolerance); double obsIntensity; if (foundPeaks.Count == 0) { Utilities.IqLogger.IqLogger.LogTrace("No Observed Peaks Found Within Tolerance"); obsIntensity = 0; } else if (foundPeaks.Count == 1) { obsIntensity = foundPeaks.First().Height; Utilities.IqLogger.IqLogger.LogTrace("Observed Peak Selected! Peak Height: "+ foundPeaks[0].Height + " Peak X-Value " + foundPeaks[0].XValue); } else { obsIntensity = foundPeaks.OrderByDescending(p => p.Height).First().Height; Utilities.IqLogger.IqLogger.LogTrace("Observed Peak Selected! Peak Height: "+ foundPeaks[0].Height + " Peak X-Value " + foundPeaks[0].XValue); } observedIntensitiesUsedInCalc.Add(obsIntensity); } else { Utilities.IqLogger.IqLogger.LogTrace("Theoretical Peak Not Selected! Peak Height: "+ peak.Height + " Peak X-Value: " + peak.XValue); } } //the minIntensityForScore is too high and no theor peaks qualified. This is bad. But we don't //want to throw errors here if (theorIntensitiesUsedInCalc.Count == 0) { Utilities.IqLogger.IqLogger.LogTrace("No peaks meet minIntensityForScore."); return(1.0); } var maxObs = observedIntensitiesUsedInCalc.Max(); if (Math.Abs(maxObs) < float.Epsilon) { maxObs = double.PositiveInfinity; } Utilities.IqLogger.IqLogger.LogTrace("Max Observed Intensity: " + maxObs); var normalizedObs = observedIntensitiesUsedInCalc.Select(p => p / maxObs).ToList(); var maxTheor = theorIntensitiesUsedInCalc.Max(); var normalizedTheor = theorIntensitiesUsedInCalc.Select(p => p / maxTheor).ToList(); Utilities.IqLogger.IqLogger.LogTrace("Max Theoretical Intensity: " + maxTheor); //foreach (var val in normalizedObs) //{ // Console.WriteLine(val); //} //Console.WriteLine(); //foreach (var val in normalizedTheor) //{ // Console.WriteLine(val); //} double sumSquareOfDiffs = 0; double sumSquareOfTheor = 0; for (var i = 0; i < normalizedTheor.Count; i++) { var diff = normalizedObs[i] - normalizedTheor[i]; sumSquareOfDiffs += (diff * diff); sumSquareOfTheor += (normalizedTheor[i] * normalizedTheor[i]); Utilities.IqLogger.IqLogger.LogTrace("Normalized Observed: " + normalizedObs[i]); Utilities.IqLogger.IqLogger.LogTrace("Normalized Theoretical: " + normalizedTheor[i]); Utilities.IqLogger.IqLogger.LogTrace("Iterator: " + i + " Sum of Squares Differences: " + sumSquareOfDiffs + " Sum of Squares Theoretical: " + sumSquareOfTheor); } ionCountUsed = normalizedTheor.Count; var fitScore = sumSquareOfDiffs / sumSquareOfTheor; if (double.IsNaN(fitScore) || fitScore > 1) { fitScore = 1; } else { // Future possibility (considered in January 2014): // Normalize the fit score by the number of theoretical ions // fitScore /= ionCountUsed; } Utilities.IqLogger.IqLogger.LogTrace("Fit Score: " + fitScore); return(fitScore); }
public virtual IsotopicProfile FindMSFeature(List <Peak> peakList, IsotopicProfile theorFeature) { Check.Require(theorFeature != null, "Theoretical feature hasn't been defined."); if (theorFeature == null) { return(null); } Check.Require(theorFeature.Peaklist != null && theorFeature.Peaklist.Count > 0, "Theoretical feature hasn't been defined."); var outFeature = new IsotopicProfile { ChargeState = theorFeature.ChargeState }; var indexOfMaxTheorPeak = theorFeature.GetIndexOfMostIntensePeak(); var toleranceInMZ = theorFeature.getMonoPeak().XValue *ToleranceInPPM / 1e6; var foundMatchingMaxPeak = false; double massDefect = 0; // this is the m/z diff between the max peak of theor feature and the max peak of the experimental feature var failedResult = false; for (var i = indexOfMaxTheorPeak; i >= 0; i--) { //find experimental peak(s) within range var peaksWithinTol = PeakUtilities.GetPeaksWithinTolerance(peakList, theorFeature.Peaklist[i].XValue, toleranceInMZ); if (i == indexOfMaxTheorPeak) { foundMatchingMaxPeak = peaksWithinTol.Count > 0; } if (!foundMatchingMaxPeak) // can't even find the observed peak that matches the most intense theor peak. { failedResult = true; break; } if (peaksWithinTol.Count == 0) { if (NeedMonoIsotopicPeak) { //here, we are looking to the left of most intense theor peak. If we have the prerequisite of finding the monoIsotopic peak and fail here, we'll return a failed result failedResult = true; } break; // stop looking to the left of the most intense peak. } if (peaksWithinTol.Count == 1) { if (outFeature.Peaklist.Count == 0) { outFeature.Peaklist.Add((MSPeak)peaksWithinTol[0]); } else { outFeature.Peaklist.Insert(0, (MSPeak)peaksWithinTol[0]); } } else // when we have several peaks within tolerance, we'll need to decide what to do { MSPeak bestPeak; if (i == indexOfMaxTheorPeak) //when matching to most intense peak, we will use the most intense peak { bestPeak = (MSPeak)findMostIntensePeak(peaksWithinTol); } else { bestPeak = (MSPeak)findClosestToXValue(peaksWithinTol, theorFeature.Peaklist[i].XValue - massDefect); } if (outFeature.Peaklist.Count == 0) { outFeature.Peaklist.Add(bestPeak); } else { outFeature.Peaklist.Insert(0, bestPeak); } } if (i == indexOfMaxTheorPeak) //when matching to most intense peak, we will get the mass defect using the most intense peak { massDefect = theorFeature.Peaklist[i].XValue - outFeature.Peaklist[0].XValue; } } //------------------------- look right ------------------------------------------- for (var i = indexOfMaxTheorPeak + 1; i < theorFeature.Peaklist.Count; i++) //start one peak to the right of the max intense theor peak { var peaksWithinTol = PeakUtilities.GetPeaksWithinTolerance(peakList, theorFeature.Peaklist[i].XValue, toleranceInMZ); if (peaksWithinTol.Count == 0) { if (i == indexOfMaxTheorPeak + 1) // first peak to the right of the max peak. We need this one or we declare it to be a failure (= null) { failedResult = true; } break; // finished. Exit loop. } if (peaksWithinTol.Count == 1) { outFeature.Peaklist.Add((MSPeak)peaksWithinTol[0]); //here, we tack peaks onto the profile } else //two or more peaks are within tolerance. Need to get the best one, which is based on the distance from the { outFeature.Peaklist.Add((MSPeak)findClosestToXValue(peaksWithinTol, theorFeature.Peaklist[i].XValue - massDefect)); } } //for higher mass peptides, we will return the profile if there is 2 or more peaks, regardless if none are found to the right of the most abundant if (indexOfMaxTheorPeak > 0 && outFeature.Peaklist.Count > 1) { failedResult = false; } if (failedResult) { return(null); // return a null Isotopic profile, indicating a failed result } addMassInfoToIsotopicProfile(theorFeature, outFeature); return(outFeature); }