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 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 void AddMatch(SpectrumMatch match) { var charge = match.PrecursorCharge; if (!_offsets.ContainsKey(charge)) throw new Exception("Invalid charge."); for (int i = charge; i > 0; i--) { _offsets[i].AddMatches(new List<SpectrumMatch>{match}); } }
public void AddMatch(SpectrumMatch match) { Total++; var ion = _precursorIonTypes[Charge - 1].GetIon(match.PeptideComposition); var monoIsotopicMz = ion.GetMonoIsotopicMz(); var min = GetMinMz(monoIsotopicMz); var max = GetMaxMz(monoIsotopicMz); var peaks = match.Spectrum.Peaks; var offsetMzCollection = (from peak in peaks where peak.Mz >= min && peak.Mz <= max select peak.Mz - monoIsotopicMz).ToList(); AddOffsets(offsetMzCollection); }
public SpectrumMatch(SpectrumMatch match, bool decoy) { Peptide = match.Peptide; _spectrum = match._spectrum; _lcms = match._lcms; ScanNum = match.ScanNum; PrecursorCharge = match.PrecursorCharge; Decoy = decoy; Sequence = new Sequence(match.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 double GetScore(Sequence sequence, int charge, int scan, LcMsRun lcmsRun) { var mass = sequence.Composition.Mass + Composition.H2O.Mass; var spectrum = lcmsRun.GetSpectrum(scan); var ionTypes = _rankScore.GetIonTypes(charge, mass); var filteredSpectrum = SpectrumFilter.FilterIonPeaks(sequence, spectrum, ionTypes, _tolerance); var match = new SpectrumMatch(sequence, filteredSpectrum, charge); var score = 0.0; var rankedPeaks = new RankedPeaks(filteredSpectrum); foreach (var ionType in ionTypes) { var ions = match.GetCleavageIons(ionType); foreach (var ion in ions) { var rank = rankedPeaks.RankIon(ion, _tolerance); score += _rankScore.GetScore(ionType, rank, charge, mass); } } return score; }
public void AddMatch(SpectrumMatch match) { foreach (var ionType in _ionTypes) { var charge = ionType.Charge; var sequence = match.Sequence; var pepSeq = ionType.IsPrefixIon ? sequence.GetRange(0, sequence.Count - 1) : sequence.GetRange(1, sequence.Count - 1); var ions = match.GetCleavageIons(ionType); var nextIonIndex = 1; while (nextIonIndex < ions.Count) { // look for peaks for current ion and next ion _totalPairs++; var currIonIndex = nextIonIndex - 1; var currMz = ions[currIonIndex].GetMonoIsotopicMz(); var currPeak = match.Spectrum.FindPeak(currMz, _tolerance); var nextMz = ions[nextIonIndex].GetMonoIsotopicMz(); var nextPeak = match.Spectrum.FindPeak(nextMz, _tolerance); if (currPeak == null && nextPeak == null) _ionPairFrequency[ionType].AddDatum(IonPairFound.Neither); else if (nextPeak == null) _ionPairFrequency[ionType].AddDatum(IonPairFound.First); else if (currPeak == null) _ionPairFrequency[ionType].AddDatum(IonPairFound.Second); else { // found both peaks, compute mass error _ionPairFrequency[ionType].AddDatum(IonPairFound.Both); var aaIndex = (ionType.IsPrefixIon ? nextIonIndex : currIonIndex); var aaMz = pepSeq[aaIndex].Mass/charge; var massError = Math.Abs(nextPeak.Mz - currPeak.Mz) - aaMz; _massError[ionType].AddDatum(massError); } nextIonIndex++; } } }
/// <summary> /// Add single Peptide-Spectrum match to RankTable. /// </summary> /// <param name="match"></param> public void AddMatch(SpectrumMatch match) { var ranks = new RankedPeaks(match.Spectrum); for (int i = 0; i < ranks.Peaks.Length; i++) { var index = i; if (index >= MaxRanks) index = MaxRanks - 1; _rankTotals[index]++; _rankTotals[MaxRanks]++; } foreach (var ionType in IonTypes) { var ions = match.GetCleavageIons(ionType); foreach (var ion in ions) { var rank = ranks.RankIon(ion, _tolerance); var rankIndex = GetRankIndex(rank); _rankTable[ionType][rankIndex]++; } } }
/// <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 }); }
/// <summary> /// Add Peptide-Spectrum match to ion Probability table. /// </summary> /// <param name="match"></param> public void AddMatch(SpectrumMatch match) { var spectrum = match.Spectrum; var prefixes = match.Prefixes; var suffixes = match.Suffixes; for (int i = 0; i < prefixes.Count; i++) { var ionTypeFound = new Dictionary<IonType, bool>(); foreach (var ionType in _ionTypes) { var it = ionType; if (_combineCharges) it = ReducedChargeIonType(ionType); if (!ionTypeFound.ContainsKey(it)) ionTypeFound.Add(it, false); var cleavagePoints = ionType.BaseIonType.IsPrefix ? prefixes : suffixes; var ion = ionType.GetIon(cleavagePoints[i]); if (spectrum.ContainsIon(ion, _tolerance, _relativeIntensityThreshold)) { ionTypeFound[it] = true; } } foreach (var key in ionTypeFound.Keys) { int found = 0; const int total = 1; if (ionTypeFound[key]) found = 1; AddIonProbability(new Probability<IonType>(key, found, total)); } } }
public SpectrumMatch(SpectrumMatch match, bool decoy) { Peptide = match.Peptide; _spectrum = match._spectrum; _lcms = match._lcms; ScanNum = match.ScanNum; PrecursorCharge = match.PrecursorCharge; Decoy = decoy; Sequence = new Sequence(match.Sequence); if (decoy) CreateDecoy(); }
private void ComputeMatch(SpectrumMatch match, int charge, int massIndex) { var isDecoy = match.Decoy; var massErrors = _massErrors[charge][massIndex]; var rankTable = (isDecoy ? _drankTables[charge][massIndex] : _rankTables[charge][massIndex]); var ionFrequencies = _ionProbabilities[charge][massIndex]; var acMatch = match; if (Config.AcquisitionMethod == AcquisitionMethod.Dia) { // filter out all peaks except ion peaks var ionPeakSpectrum = SpectrumFilter.FilterIonPeaks(match.Sequence, match.Spectrum, Config.IonTypes, Config.Tolerance); acMatch = new SpectrumMatch(acMatch.Sequence, ionPeakSpectrum, acMatch.ScanNum, acMatch.PrecursorCharge, acMatch.Decoy); } else { _precursorOffsets[charge][massIndex].AddMatch(match); // filter precursor peaks var precursorFilter = new PrecursorFilter(_precursorOffsets[charge][massIndex], Config.MassErrorTolerance); acMatch = precursorFilter.Filter(acMatch); } rankTable.AddMatch(acMatch); if (isDecoy) return; massErrors.AddMatch(match); var filteredSpectrum = SpectrumFilter.GetFilteredSpectrum(match.Spectrum, Config.WindowWidth, Config.RetentionCount); var filteredMatch = new SpectrumMatch(match.Sequence, filteredSpectrum, match.PrecursorCharge); ionFrequencies.AddMatch(filteredMatch); }
private void Compute() { if (_computed) return; Initialize(); var total = _dataSet.Count; for (int i = 0; i < total; i++) { var target = _dataSet.Dequeue(); var charge = target.PrecursorCharge; if (!_charges.Contains(charge)) continue; var decoy = new SpectrumMatch(target, true); var mass = target.PrecursorComposition.Mass; var index = _massSorter[charge].GetBinIndex(mass); ComputeMatch(target, charge, index); ComputeMatch(decoy, charge, index); } _computed = true; }
public IList<SpectrumMatch> Read() { var specMatches = new List<SpectrumMatch>(); var file = File.ReadLines(_fileName); var mgfState = MgfState.Label; var sequence = ""; int scanNum = 0, charge = 0; var peaks = new List<Peak>(); var peptideSet = new HashSet<string>(); foreach (var line in file) { switch (mgfState) { case MgfState.Label: if (line == "BEGIN IONS") mgfState = MgfState.Parameter; else throw new FormatException("Invalid MGF file."); break; case MgfState.Parameter: var parameter = line.Split('='); if (parameter.Length < 2) throw new FormatException("Invalid line in MGF file: " + line); if (parameter[0] == "SEQ") sequence = parameter[1]; else if (parameter[0] == "SCANS") scanNum = Convert.ToInt32(parameter[1]); else if (parameter[0] == "CHARGE") { var chargeStr = parameter[1].Substring(0, parameter[1].Length - 1); charge = Convert.ToInt32(chargeStr); mgfState = MgfState.Peak; if (sequence == "" || scanNum == 0 || charge == 0) throw new FormatException("Incomplete spectrum entry."); } break; case MgfState.Peak: if (line == "END IONS") { if (peaks.Count == 0) throw new FormatException("Empty peak list."); mgfState = MgfState.Label; if (peptideSet.Contains(sequence)) { sequence = ""; scanNum = 0; charge = 0; peaks.Clear(); continue; } peptideSet.Add(sequence); var spectrum = new ProductSpectrum(peaks, scanNum) { MsLevel = 2 }; var sequenceReader = new MgfSequenceReader(); var seq = sequenceReader.GetSequence(sequence); var specMatch = new SpectrumMatch(seq, spectrum, scanNum, charge, _decoy); sequence = ""; scanNum = 0; charge = 0; specMatches.Add(specMatch); peaks.Clear(); } else { var parts = line.Split('\t'); if (parts.Length < 2) throw new FormatException("Invalid line in MGF file: " + line); var mz = Convert.ToDouble(parts[0]); var intensity = Convert.ToDouble(parts[1]); peaks.Add(new Peak(mz, intensity)); } break; } } return specMatches; }