public void TestIsotopePeakAlignment(string rawFilePath, string idFilePath) { var idFileReader = IdFileReaderFactory.CreateReader(idFilePath); var lcms = PbfLcMsRun.GetLcMsRun(rawFilePath); var ids = idFileReader.Read(); var idList = ids.ToList(); var rawFileName = Path.GetFileNameWithoutExtension(rawFilePath); foreach (var id in idList) { id.LcMs = lcms; id.RawFileName = rawFileName; } var prsms = idList.Where(prsm => prsm.Sequence.Count > 0); const double relIntThres = 0.1; var tolerance = new Tolerance(10, ToleranceUnit.Ppm); var toleranceValue = tolerance.GetValue(); const int maxCharge = 15; var ionTypeFactory = new IonTypeFactory(maxCharge); var ionTypes = ionTypeFactory.GetAllKnownIonTypes().ToArray(); var psmsValidated = 0; var ionsValidated = 0; var validationErrors = new List <string>(); foreach (var prsm in prsms) { foreach (var ionType in ionTypes) { var composition = prsm.Sequence.Aggregate(Composition.Zero, (current, aa) => current + aa.Composition); var ion = ionType.GetIon(composition); var observedPeaks = prsm.Ms2Spectrum.GetAllIsotopePeaks(ion, tolerance, relIntThres); if (observedPeaks == null) { continue; } var errors = IonUtils.GetIsotopePpmError(observedPeaks, ion, relIntThres); foreach (var error in errors) { if (error == null) { continue; } if (error > toleranceValue) { validationErrors.Add(string.Format("In scan {0}, PSM {1} has error {2:F1} for ion at {3} m/z", prsm.Scan, prsm.SequenceText, error, ion.GetIsotopeMz(0))); } ionsValidated++; } } psmsValidated++; } Console.WriteLine("Validated {0:N0} ions for {1:N0} PSMs", ionsValidated, psmsValidated); if (validationErrors.Count <= 0) { return; } var validationMsg = string.Format("{0} ions had errors greater than {1} ppm", validationErrors.Count, tolerance); Console.WriteLine(validationMsg); foreach (var item in validationErrors.Take(10)) { Console.WriteLine(item); } Assert.Fail(validationMsg); }
/// <summary> /// Get the peaks for the ion. /// </summary> /// <param name="spectrum">The spectrum to get the peaks from</param> /// <param name="o">Object required for cache</param> /// <returns>The peaks for the ion.</returns> private IList <PeakDataPoint> GetPeakDataPoints(Tuple <Spectrum, bool> spectrum, object o) { var tolerance = this.IsFragmentIon ? IcParameters.Instance.ProductIonTolerancePpm : IcParameters.Instance.PrecursorTolerancePpm; var noPeaks = new List <PeakDataPoint> { new PeakDataPoint(double.NaN, double.NaN, double.NaN, double.NaN, this.Label) { TheoMonoisotopicMass = this.Ion.Composition.Mass, IonType = this.IonType, Index = this.Index } }; var peakDataPoints = new List <PeakDataPoint>(); IonType ionType = null; if (this.IsFragmentIon) { ionType = this.IonType; } var deconvoluted = spectrum.Item2; Ion ion; if (deconvoluted) { if (this.IonType.Charge > 1) { return(peakDataPoints); // Deconvoluted spectrum means decharged (only charge 1 ions shown) } if (!this.IsFragmentIon) { ion = new Ion(this.Composition, 1); } else { var ionTypeFactory = IcParameters.Instance.DeconvolutedIonTypeFactory; var ionTypeName = this.IonType.Name.Insert(1, @"'"); ion = ionTypeFactory.GetIonType(ionTypeName).GetIon(this.Composition); } } else { ion = this.Ion; } var labeledIonPeaks = IonUtils.GetIonPeaks(ion, spectrum.Item1, tolerance, deconvoluted); if (labeledIonPeaks.Item1 == null) { return(noPeaks); } var peaks = labeledIonPeaks.Item1; var correlation = labeledIonPeaks.Item2; if (correlation < IcParameters.Instance.IonCorrelationThreshold) { return(noPeaks); } var errors = IonUtils.GetIsotopePpmError(peaks, ion, 0.1, deconvoluted); peakDataPoints = new List <PeakDataPoint> { Capacity = errors.Length }; for (int i = 0; i < errors.Length; i++) { if (errors[i] != null) { peakDataPoints.Add(new PeakDataPoint(peaks[i].Mz, peaks[i].Intensity, errors[i].Value, correlation, this.Label) { MonoisotopicMass = (peaks[i].Mz * this.Ion.Charge) - InformedProteomics.Backend.Data.Biology.Constants.Proton * this.Ion.Charge, TheoMonoisotopicMass = this.Ion.Composition.Mass, Index = this.Index, IonType = ionType, }); } } peakDataPoints = peakDataPoints.OrderByDescending(x => x.Y).ToList(); return(peakDataPoints); }