public static Spectrum GetDeconvolutedSpectrum(Spectrum spec, int minCharge, int maxCharge, Tolerance tolerance, double corrThreshold, int isotopeOffsetTolerance, double filteringWindowSize = 1.1) { var deconvolutedPeaks = Deconvoluter.GetDeconvolutedPeaks(spec, minCharge, maxCharge, isotopeOffsetTolerance, filteringWindowSize, tolerance, corrThreshold); var peakList = new List<Peak>(); var binHash = new HashSet<int>(); foreach (var deconvolutedPeak in deconvolutedPeaks) { var mass = deconvolutedPeak.Mass; var binNum = GetBinNumber(mass); if (!binHash.Add(binNum)) continue; peakList.Add(new Peak(mass, deconvolutedPeak.Intensity)); } var productSpec = spec as ProductSpectrum; if (productSpec != null) { return new ProductSpectrum(peakList, spec.ScanNum) { MsLevel = spec.MsLevel, ActivationMethod = productSpec.ActivationMethod, IsolationWindow = productSpec.IsolationWindow }; } return new Spectrum(peakList, spec.ScanNum); }
public static Spectrum GetFilteredSpectrum(Spectrum spectrum, double windowWidth=100.0, int retentionCount=6) { windowWidth = windowWidth/2; var peaks = spectrum.Peaks; var filteredPeaks = new List<Peak>(); for (var peakIndex = 0; peakIndex < peaks.Count(); peakIndex++) { var rank = 1; var peak = peaks[peakIndex]; var prevIndex = peakIndex - 1; while (prevIndex >= 0) { var prevPeak = peaks[prevIndex]; if ((peak.Mz - prevPeak.Mz) > windowWidth) break; if (prevPeak.Intensity > peak.Intensity) rank++; prevIndex--; } var nextIndex = peakIndex + 1; while (nextIndex < peaks.Length) { var nextPeak = peaks[nextIndex]; if ((nextPeak.Mz - peak.Mz) > windowWidth) break; if (nextPeak.Intensity > peak.Intensity) rank++; nextIndex++; } if (rank <= retentionCount) filteredPeaks.Add(peak); } var filteredSpectrum = new Spectrum(filteredPeaks.ToArray(), spectrum.ScanNum); return filteredSpectrum; }
internal RankedSpectrum(Spectrum spec) { Peaks = spec.Peaks.OrderByDescending(p => p.Intensity) .Select((p, i) => new RankedPeak(p.Mz, p.Intensity, i)) .OrderBy(p => p.Mz) .ToArray(); }
public SpectrumMatch Filter(SpectrumMatch specMatch) { var charge = specMatch.PrecursorCharge; var peaks = specMatch.Spectrum.Peaks; var indexes = new List<int>(); for (int i = 0; i < _offsets.Charge; i++) { var ion = _precursorIonTypes[i].GetIon(specMatch.PeptideComposition); var mz = ion.GetMonoIsotopicMz(); var offsets = _offsets.GetChargeOffsets(i + 1); foreach (var offset in offsets) { var offsetMz = mz + offset; var peakIndex = specMatch.Spectrum.FindPeakIndex(offsetMz, _tolerance); if (peakIndex > 0) indexes.Add(peakIndex); } } indexes = indexes.Distinct().ToList(); var filteredPeaks = peaks.Where((t, i) => !indexes.Contains(i)).ToList(); var spectrum = new Spectrum(filteredPeaks, specMatch.Spectrum.ScanNum); var filteredSpecMatch = new SpectrumMatch(specMatch.Sequence, spectrum, specMatch.ScanNum, charge, specMatch.Decoy); return filteredSpecMatch; }
public static List<DeconvolutedPeak> GetDeconvolutedPeaks( Spectrum spec, int minCharge, int maxCharge, int isotopeOffsetTolerance, double filteringWindowSize, Tolerance tolerance, double corrScoreThreshold) { return GetDeconvolutedPeaks(spec.Peaks, minCharge, maxCharge, isotopeOffsetTolerance, filteringWindowSize, tolerance, corrScoreThreshold); }
public ScoredSpectrum(Spectrum spec, RankScore scorer, int charge, double massWithH2O, Tolerance tolerance) { _rankedSpec = new RankedSpectrum(spec); _scorer = scorer; _charge = charge; _sequenceMass = massWithH2O; _tolerance = tolerance; }
public static DeconvolutedSpectrum GetDeconvolutedSpectrum( Spectrum spec, int minCharge, int maxCharge, int isotopeOffsetTolerance, double filteringWindowSize, Tolerance tolerance, double corrScoreThreshold = 0.7) { var peaks = GetDeconvolutedPeaks(spec.Peaks, minCharge, maxCharge, isotopeOffsetTolerance, filteringWindowSize, tolerance, corrScoreThreshold); return new DeconvolutedSpectrum(spec, peaks.ToArray()); }
/// <summary> /// MaxEnt deconvolution algorithm constructor /// </summary> /// <param name="tolerance">tolerance</param> /// <param name="massBinning">mass binning interface</param> /// <param name="minCharge">maximum charge to be considered</param> /// <param name="maxCharge">minimum charge to be considered</param> /// <param name="minMass">minimum mass to be considered</param> /// <param name="maxMass">maximum mass to be considered</param> public MaxEntDeconvoluter(Spectrum spec, Tolerance tolerance, MzComparerWithBinning massBinning, int minCharge = 1, int maxCharge = 100, double minMass = 10000, double maxMass = 100000) { _minCharge = minCharge; _maxCharge = maxCharge; _minMass = minMass; _maxMass = maxMass; _tolerance = tolerance; _massBinning = massBinning; _spectrum = spec; _deconvolutedSpectrum = null; }
public SpectrumMatch(Sequence sequence, Spectrum spectrum, int scanNum=0, int precursorCharge=1, bool decoy=false) { Peptide = ""; foreach (var aa in sequence) Peptide += aa.Residue; _spectrum = spectrum; ScanNum = scanNum; PrecursorCharge = precursorCharge; Decoy = decoy; Sequence = sequence; if (decoy) CreateDecoy(); }
/// <summary> /// Filter out all peaks in a spectrum that are not explained by certain ion types. /// </summary> /// <param name="sequence">Sequence to calculate ions from.</param> /// <param name="spectrum">Spectrum to filter.</param> /// <param name="ionTypes">Ion types to find peaks for.</param> /// <param name="tolerance"></param> /// <returns>Filtered Peptide Spectrum Match</returns> public static Spectrum FilterIonPeaks(Sequence sequence, Spectrum spectrum, IonType[] ionTypes, Tolerance tolerance) { var filteredPeaks = new List<Peak>(); var specMatch = new SpectrumMatch(sequence, spectrum); foreach (var ionType in ionTypes) { var ions = specMatch.GetCleavageIons(ionType); foreach (var ion in ions) { var peak = spectrum.FindPeak(ion.GetMonoIsotopicMz(), tolerance); if (peak != null) filteredPeaks.Add(peak); } } filteredPeaks.Sort(); return new Spectrum(filteredPeaks, spectrum.ScanNum) {MsLevel = 2}; }
public DeconvolutedSpectrum(Spectrum originalSpec, DeconvolutedPeak[] peaks) : base(originalSpec.ScanNum) { Peaks = new DeconvolutedPeak[peaks.Length]; peaks.CopyTo(Peaks, 0); MsLevel = originalSpec.MsLevel; var ms2Spec = originalSpec as ProductSpectrum; if (ms2Spec == null) { ActivationMethod = ActivationMethod.Unknown; } else { ActivationMethod = ms2Spec.ActivationMethod; } MsLevel = originalSpec.MsLevel; }
protected AbstractFragmentScorer(Spectrum spec, Tolerance tol, int minCharge = 1, int maxCharge = 20, double relativeIsotopeIntensityThreshold = 0.7) { Ms2Spectrum = spec; Tolerance = tol; MinProductCharge = minCharge; MaxProductCharge = maxCharge; var productSpectrum = spec as ProductSpectrum; if (productSpectrum != null) BaseIonTypes = productSpectrum.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD; else { var spectrum = spec as DeconvolutedSpectrum; if (spectrum != null) BaseIonTypes = spectrum.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD; else BaseIonTypes = BaseIonTypesCID; } RelativeIsotopeIntensityThreshold = relativeIsotopeIntensityThreshold; }
public SpectrumMatch(string peptide, DataFileFormat sequenceFormat, Spectrum spectrum, int scanNum=0, int precursorCharge=1, bool decoy=false, string formula="") { Peptide = peptide; _spectrum = spectrum; _lcms = null; ScanNum = scanNum; PrecursorCharge = precursorCharge; Decoy = decoy; var sequenceReader = new SequenceReader(sequenceFormat); Sequence = sequenceReader.GetSequence(peptide); if (decoy) CreateDecoy(); else if (formula.Length > 0) { var composition = Composition.Parse(formula); if (!composition.Equals(Sequence.Composition + Composition.H2O)) { throw new MismatchException(); } } }
private double GetScore(Ion ion, Spectrum spectrum, Tolerance tolerance, double relativeIntenistyThreshold) { double score; switch (_method) { case ScoreMethod.Cosine: score = spectrum.GetConsineScore(ion, tolerance, relativeIntenistyThreshold); break; case ScoreMethod.FitScore: score = spectrum.GetFitScore(ion, tolerance, relativeIntenistyThreshold); break; case ScoreMethod.Pearson: score = spectrum.GetCorrScore(ion, tolerance, relativeIntenistyThreshold); break; default: score = spectrum.GetConsineScore(ion, tolerance, relativeIntenistyThreshold); break; } return score; }
private List<DeconvolutedPeak> CollectMultiplyChargedPeaks(double mass, Spectrum spectrum) { var deconvPeaks = new List<DeconvolutedPeak>(); var minMz = spectrum.Peaks.First().Mz; var maxMz = spectrum.Peaks.Last().Mz; var minCharge = (int)Math.Max(_minCharge, Math.Ceiling(mass / (maxMz - Constants.Proton))); var maxCharge = (int)Math.Min(_maxCharge, Math.Floor(mass / (minMz - Constants.Proton))); for (var charge = minCharge; charge < maxCharge; charge++) { var mz = mass / charge + Constants.Proton; var peak = spectrum.FindPeak(mz, _tolerance); if (peak == null) continue; deconvPeaks.Add(new DeconvolutedPeak(peak, charge)); } return deconvPeaks; }
// 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 RankedPeaks(Spectrum spectrum) : base(spectrum.Peaks, spectrum.ScanNum) { _spectrum = spectrum; Array.Sort(Peaks, new IntensityComparer()); }
public Spectrum GetDeconvolutedSpectrum(Spectrum spec, int minCharge, int maxCharge, Tolerance tolerance, double corrThreshold) { return GetDeconvolutedSpectrum(spec, minCharge, maxCharge, tolerance, corrThreshold, IsotopeOffsetTolerance, FilteringWindowSize); }
/// <summary> /// Handle a single spectrum element and child nodes /// Called by ReadSpectrumList (xml hierarchy) /// </summary> /// <param name="reader">XmlReader that is only valid for the scope of the single spectrum element</param> /// <param name="includePeaks">Whether to read binary data arrays</param> private Spectrum ReadSpectrum(XmlReader reader, bool includePeaks = true) { reader.MoveToContent(); string index = reader.GetAttribute("index"); //Console.WriteLine("Reading spectrum indexed by " + index); // This is correct for Thermo files converted by msConvert, but need to implement for others as well string spectrumId = reader.GetAttribute("id"); // Native ID in mzML_1.1.0; unique identifier in mzML_1.0.0, often same as nativeID string nativeId = spectrumId; if (_version == MzML_Version.mzML1_0_0) { nativeId = reader.GetAttribute("nativeID"); // Native ID in mzML_1.0.0 } int scanNum = -1; // If a random access reader, there is already a scan number stored, based on the order of the index. Use it instead. if (_randomAccess) { scanNum = (int) (_spectrumOffsets.NativeToIdMap[nativeId]); } else { scanNum = (int)(_artificialScanNum++); // Interpret the NativeID (if the format has an interpreter) and use it instead of the artificial number. // TODO: Better handling than the artificial ID for other nativeIDs (ones currently not supported) int num = 0; if (NativeIdConversion.TryGetScanNumberInt(nativeId, out num)) { scanNum = num; } } int defaultArraySize = Convert.ToInt32(reader.GetAttribute("defaultArrayLength")); reader.ReadStartElement("spectrum"); // Throws exception if we are not at the "spectrum" tag. bool is_ms_ms = false; int msLevel = 0; bool centroided = false; double tic = 0; List<Precursor> precursors = new List<Precursor>(); List<ScanData> scans = new List<ScanData>(); List<BinaryDataArray> bdas = new List<BinaryDataArray>(); while (reader.ReadState == ReadState.Interactive) { // Handle exiting out properly at EndElement tags if (reader.NodeType != XmlNodeType.Element) { reader.Read(); continue; } ////////////////////////////////////////////////////////////////////////////////////// /// /// MS1 Spectra: only need Spectrum data: scanNum, MSLevel, ElutionTime, mzArray, IntensityArray /// /// MS2 Spectra: use ProductSpectrum; adds ActivationMethod and IsolationWindow /// ////////////////////////////////////////////////////////////////////////////////////// switch (reader.Name) { case "referenceableParamGroupRef": // Schema requirements: zero to many instances of this element reader.Skip(); break; case "cvParam": // Schema requirements: zero to many instances of this element /* MAY supply a *child* term of MS:1000465 (scan polarity) only once * e.g.: MS:1000129 (negative scan) * e.g.: MS:1000130 (positive scan) * MUST supply a *child* term of MS:1000559 (spectrum type) only once * e.g.: MS:1000322 (charge inversion mass spectrum) * e.g.: MS:1000325 (constant neutral gain spectrum) * e.g.: MS:1000326 (constant neutral loss spectrum) * e.g.: MS:1000328 (e/2 mass spectrum) * e.g.: MS:1000341 (precursor ion spectrum) * e.g.: MS:1000579 (MS1 spectrum) * e.g.: MS:1000580 (MSn spectrum) * e.g.: MS:1000581 (CRM spectrum) * e.g.: MS:1000582 (SIM spectrum) * e.g.: MS:1000583 (SRM spectrum) * e.g.: MS:1000620 (PDA spectrum) * e.g.: MS:1000627 (selected ion current chromatogram) * e.g.: MS:1000789 (enhanced multiply charged spectrum) * e.g.: MS:1000790 (time-delayed fragmentation spectrum) * et al. * MUST supply term MS:1000525 (spectrum representation) or any of its children only once * e.g.: MS:1000127 (centroid spectrum) * e.g.: MS:1000128 (profile spectrum) * MAY supply a *child* term of MS:1000499 (spectrum attribute) one or more times * e.g.: MS:1000285 (total ion current) * e.g.: MS:1000497 (zoom scan) * e.g.: MS:1000504 (base peak m/z) * e.g.: MS:1000505 (base peak intensity) * e.g.: MS:1000511 (ms level) * e.g.: MS:1000527 (highest observed m/z) * e.g.: MS:1000528 (lowest observed m/z) * e.g.: MS:1000618 (highest observed wavelength) * e.g.: MS:1000619 (lowest observed wavelength) * e.g.: MS:1000796 (spectrum title) * et al. */ switch (reader.GetAttribute("accession")) { case "MS:1000127": // name="centroid spectrum" centroided = true; break; case "MS:1000128": // name="profile spectrum" centroided = false; break; case "MS:1000511": // name="ms level" msLevel = Convert.ToInt32(reader.GetAttribute("value")); break; case "MS:1000579": // name="MS1 spectrum" is_ms_ms = false; break; case "MS:1000580": // name="MSn spectrum" is_ms_ms = true; break; case "MS:1000285": // name="total ion current" tic = Convert.ToDouble(reader.GetAttribute("value")); break; } reader.Read(); // Consume the cvParam element (no child nodes) break; case "userParam": // Schema requirements: zero to many instances of this element reader.Skip(); break; case "spectrumDescription": // mzML_1.0.0 compatibility // Schema requirements: one instance of this element ReadSpectrumDescription(reader.ReadSubtree(), ref scans, ref precursors, out centroided); reader.ReadEndElement(); // "spectrumDescription" must have child nodes break; case "scanList": // Schema requirements: zero to one instances of this element scans.AddRange(ReadScanList(reader.ReadSubtree())); reader.ReadEndElement(); // "scanList" must have child nodes break; case "precursorList": // Schema requirements: zero to one instances of this element precursors.AddRange(ReadPrecursorList(reader.ReadSubtree())); reader.ReadEndElement(); // "precursorList" must have child nodes break; case "productList": // Schema requirements: zero to one instances of this element reader.Skip(); break; case "binaryDataArrayList": // Schema requirements: zero to one instances of this element if (includePeaks) { bdas.AddRange(ReadBinaryDataArrayList(reader.ReadSubtree(), defaultArraySize)); reader.ReadEndElement(); // "binaryDataArrayList" must have child nodes } else { reader.Skip(); } break; default: reader.Skip(); break; } } reader.Close(); // Process the spectrum data ScanData scan = new ScanData(); Spectrum spectrum; BinaryDataArray mzs = new BinaryDataArray(); BinaryDataArray intensities = new BinaryDataArray(); foreach (var bda in bdas) { if (bda.ArrayType == ArrayType.m_z_array) { mzs = bda; } else if (bda.ArrayType == ArrayType.intensity_array) { intensities = bda; } } if (!centroided && includePeaks) { // Centroid spectrum // ProteoWizard var centroider = new Centroider(mzs.Data, intensities.Data); double[] centroidedMzs, centroidedIntensities; centroider.GetCentroidedData(out centroidedMzs, out centroidedIntensities); mzs.Data = centroidedMzs; intensities.Data = centroidedIntensities; } if (scans.Count == 1) { scan = scans[0]; } else if (scans.Count > 1) { // TODO: Should do something else to appropriately handle combinations... scan = scans[0]; } if (is_ms_ms) { Precursor precursor = new Precursor(); if (precursors.Count == 1) { precursor = precursors[0]; } else if (precursors.Count > 1) { // TODO: Should do something else to appropriately handle multiple precursors... precursor = precursors[0]; } SelectedIon ion = new SelectedIon(); if (precursor.Ions.Count == 1) { ion = precursor.Ions[0]; } else if (precursor.Ions.Count > 1) { // TODO: Should do something else to appropriately handle multiple selected ions... ion = precursor.Ions[0]; } var pspectrum = new ProductSpectrum(mzs.Data, intensities.Data, scanNum); pspectrum.ActivationMethod = precursor.Activation; // Select mz value to use based on presence of a Thermo-specific user param. // The user param has a slightly higher precision, if that matters. double mz = scan.MonoisotopicMz == 0.0 ? ion.SelectedIonMz : scan.MonoisotopicMz; pspectrum.IsolationWindow = new IsolationWindow(precursor.IsolationWindowTargetMz, precursor.IsolationWindowLowerOffset, precursor.IsolationWindowUpperOffset, mz, ion.Charge); //pspectrum.IsolationWindow.OldCharge = ion.OldCharge; //pspectrum.IsolationWindow.SelectedIonMz = ion.SelectedIonMz; spectrum = pspectrum; } else { spectrum = new Spectrum(mzs.Data, intensities.Data, scanNum); } spectrum.MsLevel = msLevel; spectrum.ElutionTime = scan.StartTime; spectrum.NativeId = nativeId; spectrum.TotalIonCurrent = tic; return spectrum; }
/// <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); }
private void HandleSpectrum( ref SpectrumTrackingInfo trackingInfo, Dictionary<int, List<int>> isolationMzBinToScanNums, Spectrum spec) { trackingInfo.SpecRead += 1; //Console.WriteLine("Reading Scan {0}; {1} peaks", spec.ScanNum, spec.Peaks.Length); ScanNumToMsLevel[spec.ScanNum] = spec.MsLevel; ScanNumElutionTimeMap[spec.ScanNum] = spec.ElutionTime; if (spec.MsLevel == 1) { if (trackingInfo.PrecursorSignalToNoiseRatioThreshold > 0.0) spec.FilterNoise(trackingInfo.PrecursorSignalToNoiseRatioThreshold); //foreach (var peak in spec.Peaks) //{ // _ms1PeakList.Add(new LcMsPeak(peak.Mz, peak.Intensity, spec.ScanNum)); //} _ms1PeakList.AddRange(spec.Peaks.Select(peak => new LcMsPeak(peak.Mz, peak.Intensity, spec.ScanNum))); _scanNumSpecMap.Add(spec.ScanNum, spec); } else if (spec.MsLevel == 2) { var productSpec = spec as ProductSpectrum; if (productSpec != null) { if (trackingInfo.ProductSignalToNoiseRatioThreshold > 0.0) productSpec.FilterNoise(trackingInfo.ProductSignalToNoiseRatioThreshold); var isolationWindow = productSpec.IsolationWindow; var minBinNum = (int)Math.Round(isolationWindow.MinMz * IsolationWindowBinningFactor); var maxBinNum = (int)Math.Round(isolationWindow.MaxMz * IsolationWindowBinningFactor); for (var binNum = minBinNum; binNum <= maxBinNum; binNum++) { List<int> scanNumList; if (!isolationMzBinToScanNums.TryGetValue(binNum, out scanNumList)) { scanNumList = new List<int>(); isolationMzBinToScanNums[binNum] = scanNumList; } scanNumList.Add(productSpec.ScanNum); } _scanNumSpecMap.Add(spec.ScanNum, productSpec); } } if (spec.ScanNum < trackingInfo.MinScanNum) trackingInfo.MinScanNum = spec.ScanNum; if (spec.ScanNum > trackingInfo.MaxScanNum) trackingInfo.MaxScanNum = spec.ScanNum; if (spec.MsLevel < trackingInfo.MinMsLevel) trackingInfo.MinMsLevel = spec.MsLevel; if (spec.MsLevel > trackingInfo.MaxMsLevel) trackingInfo.MaxMsLevel = spec.MsLevel; }
public CompositeScorer(Spectrum ms2Spec, Tolerance tol, int minCharge, int maxCharge, double relativeIsotopeIntensityThreshold = 0.1) : base(ms2Spec, tol, minCharge, maxCharge, relativeIsotopeIntensityThreshold) { ReferencePeakIntensity = GetRefIntensity(ms2Spec.Peaks); }
/// <summary> /// Initializes a new instance of the <see cref="TargetedDeconvolutedSpectrum" /> class. /// </summary> /// <param name="spectrum">Non-deconvoluted spectrum.</param> /// <param name="minCharge">The minimum charge state to consider.</param> /// <param name="maxCharge">The maximum charge state to consider.</param> public TargetedDeconvolutedSpectrum(Spectrum spectrum, int minCharge = 1, int maxCharge = 20) { this.spectrum = spectrum; this.minCharge = minCharge; this.maxCharge = maxCharge; }
/// <summary> /// Get the deconvoluted peaks, selecting the best peak within +/- filteringWindowSize /// </summary> /// <param name="scanNum">Scan number (included in any exceptions that are caught)</param> /// <param name="peaks"></param> /// <param name="minCharge"></param> /// <param name="maxCharge"></param> /// <param name="isotopeOffsetTolerance"></param> /// <param name="filteringWindowSize"></param> /// <param name="tolerance"></param> /// <param name="corrScoreThreshold"></param> /// <returns></returns> public static List <DeconvolutedPeak> GetDeconvolutedPeaks( int scanNum, Peak[] peaks, int minCharge, int maxCharge, int isotopeOffsetTolerance, double filteringWindowSize, Tolerance tolerance, double corrScoreThreshold) { try { var monoIsotopePeakList = new List <DeconvolutedPeak>(); for (var peakIndex = 0; peakIndex < peaks.Length; peakIndex++) { var peak = peaks[peakIndex]; // Check whether peak has the maximum intensity within the window var isBest = true; var prevIndex = peakIndex - 1; while (prevIndex >= 0) { var prevPeak = peaks[prevIndex]; if ((peak.Mz - prevPeak.Mz) > filteringWindowSize) { break; } if (prevPeak.Intensity > peak.Intensity) { isBest = false; break; } prevIndex--; } if (!isBest) { continue; } var nextIndex = peakIndex + 1; while (nextIndex < peaks.Length) { var nextPeak = peaks[nextIndex]; if ((nextPeak.Mz - peak.Mz) > filteringWindowSize) { break; } if (nextPeak.Intensity > peak.Intensity) { isBest = false; break; } nextIndex++; } if (!isBest) { continue; } // peak has the maximum intensity, window = [prevIndex+1,nextIndex-1] var window = new Peak[nextIndex - prevIndex - 1]; Array.Copy(peaks, prevIndex + 1, window, 0, window.Length); var windowSpectrum = new Spectrum(window, 1); var peakMz = peak.Mz; //var bestScore = 0.0; //DeconvolutedPeak bestPeak = null; for (var charge = maxCharge; charge >= minCharge; charge--) { var mass = (peak.Mz * charge) - charge * Constants.Proton; //var isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(mass); //var mostAbundantIsotopeIndex = isotopomerEnvelope.MostAbundantIsotopeIndex; var mostAbundantIsotopeIndex = Averagine.GetIsotopomerEnvelope(mass).MostAbundantIsotopeIndex; for (var isotopeIndex = mostAbundantIsotopeIndex - isotopeOffsetTolerance; isotopeIndex <= mostAbundantIsotopeIndex + isotopeOffsetTolerance; isotopeIndex++) { var monoIsotopeMass = Ion.GetMonoIsotopicMass(peakMz, charge, isotopeIndex); var isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(monoIsotopeMass); var observedPeaks = windowSpectrum.GetAllIsotopePeaks(monoIsotopeMass, charge, isotopomerEnvelope, tolerance, 0.1); if (observedPeaks == null) { continue; } var envelop = isotopomerEnvelope.Envelope; var observedIntensities = new double[observedPeaks.Length]; for (var i = 0; i < observedPeaks.Length; i++) { var observedPeak = observedPeaks[i]; observedIntensities[i] = observedPeak != null ? (float)observedPeak.Intensity : 0.0; } var sim = FitScoreCalculator.GetDistanceAndCorrelation(envelop, observedIntensities); var bcDist = sim.Item1; var corr = sim.Item2; //var score = corr / (bcDist * ((double)Math.Abs(isotopeIndex - mostAbundantIsotopeIndex) / envelop.Length)); if (corr < corrScoreThreshold && bcDist > 0.03) { continue; } // monoIsotopeMass is valid //if (score >= bestScore) //{ // bestScore = score; // bestPeak = new DeconvolutedPeak(monoIsotopeMass, observedIntensities[mostAbundantIsotopeIndex], charge, corr, bcDist, observedPeaks); //} var deconvPeak = new DeconvolutedPeak(monoIsotopeMass, observedIntensities[mostAbundantIsotopeIndex], charge, corr, bcDist, observedPeaks); monoIsotopePeakList.Add(deconvPeak); } } //if (bestPeak != null) //{ // monoIsotopePeakList.Add(bestPeak); //} } monoIsotopePeakList.Sort(); return(monoIsotopePeakList); } catch (Exception ex) { throw new Exception(string.Format("Error getting deconvoluted peaks for scan {0} in GetDeconvolutedPeaks: {1}", scanNum, ex.Message), ex); } }
private Peak GetHighestPeak(Ion ion, Spectrum spectrum, Tolerance tolerance, double relativeIntensityThreshold) { var peaks = spectrum.GetAllIsotopePeaks(ion, tolerance, relativeIntensityThreshold); Peak highestPeak = null; double highestIntensity = 0.0; if (peaks == null) return null; foreach (var peak in peaks) { if (peak != null && peak.Intensity >= highestIntensity) { highestPeak = peak; highestIntensity = peak.Intensity; } } return highestPeak; }
public Peak[] GetAllIsotopePeaks(Spectrum spec, Ion ion, Tolerance tolerance, double relativeIntensityThreshold, out int[] peakIndexList) { var mostAbundantIsotopeIndex = ion.Composition.GetMostAbundantIsotopeZeroBasedIndex(); var isotopomerEnvelope = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities(); peakIndexList = new int[isotopomerEnvelope.Length]; var mostAbundantIsotopeMz = ion.GetIsotopeMz(mostAbundantIsotopeIndex); var mostAbundantIsotopeMatchedPeakIndex = spec.FindPeakIndex(mostAbundantIsotopeMz, tolerance); if (mostAbundantIsotopeMatchedPeakIndex < 0) return null; var observedPeaks = new Peak[isotopomerEnvelope.Length]; observedPeaks[mostAbundantIsotopeIndex] = spec.Peaks[mostAbundantIsotopeMatchedPeakIndex]; peakIndexList[mostAbundantIsotopeIndex] = mostAbundantIsotopeMatchedPeakIndex; // go down var peakIndex = mostAbundantIsotopeMatchedPeakIndex - 1; for (var isotopeIndex = mostAbundantIsotopeIndex - 1; isotopeIndex >= 0; isotopeIndex--) { if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold) break; var isotopeMz = ion.GetIsotopeMz(isotopeIndex); var tolTh = tolerance.GetToleranceAsTh(isotopeMz); var minMz = isotopeMz - tolTh; var maxMz = isotopeMz + tolTh; for (var i = peakIndex; i >= 0; i--) { var peakMz = spec.Peaks[i].Mz; if (peakMz < minMz) { peakIndex = i; break; } if (peakMz <= maxMz) // find match, move to prev isotope { var peak = spec.Peaks[i]; if (observedPeaks[isotopeIndex] == null || peak.Intensity > observedPeaks[isotopeIndex].Intensity) { observedPeaks[isotopeIndex] = peak; peakIndexList[isotopeIndex] = i; } } } } // go up peakIndex = mostAbundantIsotopeMatchedPeakIndex + 1; for (var isotopeIndex = mostAbundantIsotopeIndex + 1; isotopeIndex < isotopomerEnvelope.Length; isotopeIndex++) { if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold) break; var isotopeMz = ion.GetIsotopeMz(isotopeIndex); var tolTh = tolerance.GetToleranceAsTh(isotopeMz); var minMz = isotopeMz - tolTh; var maxMz = isotopeMz + tolTh; for (var i = peakIndex; i < spec.Peaks.Length; i++) { var peakMz = spec.Peaks[i].Mz; if (peakMz > maxMz) { peakIndex = i; break; } if (peakMz >= minMz) // find match, move to prev isotope { var peak = spec.Peaks[i]; if (observedPeaks[isotopeIndex] == null || peak.Intensity > observedPeaks[isotopeIndex].Intensity) { observedPeaks[isotopeIndex] = peak; peakIndexList[isotopeIndex] = i; } } } } return observedPeaks; }
// 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; }
private static Tuple <Peak, int>[] GetAllIsotopePeaks( Spectrum spec, double monoisotopicMass, int charge, IsotopomerEnvelope envelope, Tolerance tolerance, double relativeIntensityThreshold) { var mostAbundantIsotopeIndex = envelope.MostAbundantIsotopeIndex; var isotopomerEnvelope = envelope.Envelope; var mostAbundantIsotopeMz = Ion.GetIsotopeMz(monoisotopicMass, charge, mostAbundantIsotopeIndex); var mostAbundantIsotopePeakIndex = spec.FindPeakIndex(mostAbundantIsotopeMz, tolerance); if (mostAbundantIsotopePeakIndex < 0) { return(null); } var observedPeaks = new Tuple <Peak, int> [isotopomerEnvelope.Length]; observedPeaks[mostAbundantIsotopeIndex] = new Tuple <Peak, int>(spec.Peaks[mostAbundantIsotopePeakIndex], mostAbundantIsotopePeakIndex); // go down var peakIndex = mostAbundantIsotopePeakIndex - 1; for (var isotopeIndex = mostAbundantIsotopeIndex - 1; isotopeIndex >= 0; isotopeIndex--) { if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold) { break; } var isotopeMz = Ion.GetIsotopeMz(monoisotopicMass, charge, isotopeIndex); var tolTh = tolerance.GetToleranceAsMz(isotopeMz); var minMz = isotopeMz - tolTh; var maxMz = isotopeMz + tolTh; for (var i = peakIndex; i >= 0; i--) { var peakMz = spec.Peaks[i].Mz; if (peakMz < minMz) { peakIndex = i; break; } if (peakMz <= maxMz) // find match, move to prev isotope { var peak = spec.Peaks[i]; if (observedPeaks[isotopeIndex] == null || peak.Intensity > observedPeaks[isotopeIndex].Item1.Intensity) { observedPeaks[isotopeIndex] = new Tuple <Peak, int>(peak, peakIndex); } } } } // go up peakIndex = mostAbundantIsotopePeakIndex + 1; for (var isotopeIndex = mostAbundantIsotopeIndex + 1; isotopeIndex < isotopomerEnvelope.Length; isotopeIndex++) { if (isotopomerEnvelope[isotopeIndex] < relativeIntensityThreshold) { break; } var isotopeMz = Ion.GetIsotopeMz(monoisotopicMass, charge, isotopeIndex); var tolTh = tolerance.GetToleranceAsMz(isotopeMz); var minMz = isotopeMz - tolTh; var maxMz = isotopeMz + tolTh; for (var i = peakIndex; i < spec.Peaks.Length; i++) { var peakMz = spec.Peaks[i].Mz; if (peakMz > maxMz) { peakIndex = i; break; } if (peakMz >= minMz) // find match, move to prev isotope { var peak = spec.Peaks[i]; if (observedPeaks[isotopeIndex] == null || peak.Intensity > observedPeaks[isotopeIndex].Item1.Intensity) { observedPeaks[isotopeIndex] = new Tuple <Peak, int>(peak, peakIndex); } } } } return(observedPeaks); }
public CompositeScorer(Spectrum ms2Spec, Tolerance tol, double relativeIsotopeIntensityThreshold = 0.1) : base(ms2Spec, tol, 1, 20, relativeIsotopeIntensityThreshold) { }