/// <summary> /// Aligns fragments observed Mz values /// </summary> /// <param name="result"></param> /// <param name="allPSMs"></param> public static void AlignProductsByDiff(Result result, PeptideSpectrumMatches allPSMs) { List <double> observedDiff = new List <double>(); List <double> observedMz = new List <double>(); //Precursors and Tracks foreach (Precursor precursor in result.matchedPrecursors)//.ComputeAtFDR(result.dbOptions.maximumFalseDiscoveryRate, false)) { PeptideSpectrumMatch psm = precursor.OptimizedBestPsm(); if (psm.Target) { foreach (ProductMatch fragment in psm.AllProductMatches) { observedMz.Add(fragment.obsMz); observedDiff.Add(fragment.mass_diff); } } } PolynominalRegression pr = new PolynominalRegression(observedMz, observedDiff, 2); foreach (PeptideSpectrumMatch psm in allPSMs) { foreach (ProductMatch match in psm.AllProductMatches) { match.obsMz += pr.Calculate(match.obsMz); match.mass_diff = match.theoMz - match.obsMz; } psm.Initialize(result.dbOptions, psm.AllProductMatches); foreach (MsMsPeak peak in psm.Query.spectrum.Peaks) { peak.MZ += pr.Calculate(peak.MZ); } } }
public double OptimizedBestPsmScore(Peptide peptide = null, bool checkMods = false) { PeptideSpectrumMatch psm = OptimizedBestPsm(peptide, checkMods); if (psm != null) { return(psm.ProbabilityScore()); } else { return(0); } }
public static int DescendingOptimizedScoreComparison(PeptideSpectrumMatch left, PeptideSpectrumMatch right) { int comparison = -(left.ProbabilityScore().CompareTo(right.ProbabilityScore())); if (comparison != 0) { return(comparison); } else { return(left.Target.CompareTo(right.Target)); } }
public bool KeepPSM(PeptideSpectrumMatch psm) { return(psm.MatchingIntensity >= minIntensityScore && psm.MatchingIntensityFraction >= minIntensityFractionScore && psm.ProductScore >= minProductScore && psm.PrecursorScore >= minPrecursorScore && psm.MatchingProductsFraction >= minMatchingProductFractionScore && psm.MatchingProducts >= minMatchingProductScore && psm.ProteinScore >= minProteinScore && psm.PeptideScore >= minPeptideScore && psm.FragmentScore >= minFragmentScore && psm.ProbabilityScore() >= minProbabilityScore); }
public void Merge(PeptideSpectrumMatch psm, DBOptions options) { //TODO Replace merging with Max flow approach //Augment list A List <ProductMatch> newList = new List <ProductMatch>(); foreach (ProductMatch matchA in this.AllProductMatches) { bool isNew = true; foreach (ProductMatch matchB in psm.AllProductMatches) { if (matchA.theoMz == matchB.theoMz && matchA.charge == matchB.charge) { if (matchA.mass_diff > matchB.mass_diff) { matchA.mass_diff = matchB.mass_diff; matchA.obsMz = matchB.obsMz; } matchA.obsIntensity += matchB.obsIntensity; newList.Add(matchA); isNew = false; } } if (isNew) { newList.Add(matchA); } } //Add items unique to list B foreach (ProductMatch matchB in psm.AllProductMatches) { bool isNew = true; foreach (ProductMatch matchNew in newList) { if (matchNew.theoMz == matchB.theoMz && matchNew.charge == matchB.charge) { isNew = false; } } if (isNew) { newList.Add(matchB); } } highestFragmentIntensity += psm.highestFragmentIntensity; Initialize(options, newList); }//*/
public IEnumerable <PeptideSpectrumMatch> OptimizedBestPsms() { if (psms.Count > 0) { PeptideSpectrumMatch bestpsm = OptimizedBestPsm(); double score = bestpsm.ProbabilityScore(); for (int i = 0; i < psms.Count; i++) { if (psms[i].ProbabilityScore() >= score) { yield return(psms[i]); } } } }
//public ConcurrentDictionary<string, List<Protein>> DicOfProteins = new ConcurrentDictionary<string, List<Protein>>(); private void ComputePSMs(Query query, Peptide modified_peptide) { PeptideSpectrumMatch psm = new PeptideSpectrumMatch(query, modified_peptide, options); if (psm.MatchingProducts > options.fragments.Count) { //DicOfProteins.GetOrAdd(psm.Peptide.BaseSequence, new List<Protein>()).Add(psm.Peptide.Parent); // if (!DicOfProteins.ContainsKey(psm.Peptide.BaseSequence)) // DicOfProteins.AddOrUpdate(psm.Peptide.BaseSequence, new List<Protein>()); // DicOfProteins[psm.Peptide.BaseSequence].Add(psm.Peptide.Parent); //List<PeptideSpectrumMatch> psmsOfQuery = query.psms; if (query.psms.Count < options.NbPSMToKeep) { query.psms.Add(psm); } else if (query.psms[options.NbPSMToKeep - 1].ProbabilityScore() < psm.ProbabilityScore()) { for (int i = 0; i < query.psms.Count; i++) { if (query.psms[i].ProbabilityScore() <= psm.ProbabilityScore()) { query.psms.Insert(i, psm); break; } } if (query.psms.Count > options.NbPSMToKeep) { query.psms.RemoveAt(options.NbPSMToKeep - 1); } }//*/ //TODO check optimal number of PSM to store /* * //TODO Reactivate this to save on memory space * if (psmsOfPrecursor.Count == 0 || psmsOfPrecursor[0].MaxQuantScore() == psm.MaxQuantScore()) * psmsOfPrecursor.Add(psm); * else * if (psm.MaxQuantScore() > psmsOfPrecursor[0].MaxQuantScore()) * { * psmsOfPrecursor.Clear(); * psmsOfPrecursor.Add(psm); * } * //*/ } }
/// <summary> /// Crop observed precursors values outside the variance or Standard Deviation (whichever is bigger) /// </summary> /// <param name="result"></param> /// <param name="allPSMs"></param> /// <returns>Returns the newly computed Precursor tolerance</returns> public static double CropPrecursors(Result result, PeptideSpectrumMatches allPSMs) { List <double> errorPrecursor = new List <double>(result.precursors.Count); foreach (Precursor precursor in result.matchedPrecursors) { PeptideSpectrumMatch psm = precursor.OptimizedBestPsm(); if (psm.Target) { errorPrecursor.Add(psm.PrecursorMzError); } } double variance = Numerics.Variance(errorPrecursor); double stdev = Numerics.StandardDeviation(errorPrecursor); result.dbOptions.ConSole.WriteLine("Computed Precursor Variance = " + variance + " STDev = " + stdev); if (variance < stdev) { variance = stdev; } //variance = result.dbOptions.precursorMassTolerance.Value * ((2 * variance) / result.dbOptions.precursorMassTolerance.Value); int nbRemovedPSM = 0; foreach (Precursor precursor in result.precursors) { for (int i = 0; i < precursor.psms.Count;) { if (Math.Abs(precursor.psms[i].PrecursorMzError) > variance) { //allPSMs[i].Query.precursor.psms_AllPossibilities.Remove(allPSMs[i]); precursor.psms.RemoveAt(i); //allPSMs.RemoveAt(i); nbRemovedPSM++; } else { i++; } } } result.dbOptions.ConSole.WriteLine("Removed " + nbRemovedPSM + " [" + allPSMs.Count + " remaining] Peptide Spectrum matches outside the variance [" + variance + "]"); return(variance); }
public static void Export(string filename, List <Precursor> precursors) { vsCSVWriter writer = new vsCSVWriter(filename); writer.AddLine("Index.Mz,Rt,Precursor Mz,Charge,Most Intense Charge,Precursor Mass,Peptide Mass,Sequence,Modified Sequence,Precursor Score,Product Score,Intensity Score,Final Score,Precursor Mass Error,Decoy?,Protein Score"); foreach (Precursor precursor in precursors) { string line = precursor.INDEX + "," + precursor.Track.RT + "," + precursor.Track.MZ + "," + precursor.Charge + "," + precursor.GetMostIntenseCharge() + "," + precursor.Mass + ","; PeptideSpectrumMatch match = precursor.OptimizedBestPsm(); if (match != null) { line += match.Peptide.MonoisotopicMass + "," + match.Peptide.BaseSequence + "," + match.Peptide.Sequence + "," + match.PrecursorScore + "," + match.ProductScore + "," + match.IntensityScore + "," + precursor.ProbabilityScore(match.Peptide) + "," + match.PrecursorMzError + "," + match.Decoy + "," + match.ProteinScore; } writer.AddLine(line); } writer.WriteToFile(); }
public static void Export(string filename, IEnumerable <Query> queries) { vsCSVWriter writer = new vsCSVWriter(filename); writer.AddLine("Spectrum Precursor Mz,Rt,Charge,BaseSequence,Sequence,Precursor Score,Product Score,Intensity Score,Final Score,Precursor Mass Error,Decoy?,Protein Score"); foreach (Query query in queries) { string line = query.spectrum.PrecursorMZ + "," + query.precursor.Track.RT + "," + query.precursor.Charge + ","; PeptideSpectrumMatch match = query.precursor.OptimizedBestPsm(); if (match != null) { line += match.Peptide.BaseSequence + "," + match.Peptide.Sequence + "," + match.PrecursorScore + "," + match.ProductScore + "," + match.IntensityScore + "," + query.ScoreFct(match.Peptide) + "," + match.PrecursorMzError + "," + match.Decoy + "," + match.ProteinScore; } writer.AddLine(line); } writer.WriteToFile(); }
/* * public List<PeptideSpectrumMatch> GetPSMs(double fdr, double decoyOverTargetRatio = 1, int nbMaxPsm = 1) * { * return FDR.PSMs(clusters, fdr, decoyOverTargetRatio, nbMaxPsm); * }//*/ /* * public List<PeptideMatch> GetPeptides(double fdr) * { * if (peptides != null) * return FDR.Peptides(peptides, fdr); * else * return null; * } * * public List<ProteinGroupMatch> GetProteins(double fdr) * { * if (proteins != null) * return FDR.Proteins(proteins, fdr); * else * return null; * }//*/ public static int DescendingProteinScoreComparison(Precursor left, Precursor right) { PeptideSpectrumMatch psmLeft = left.OptimizedBestPsm(); if (psmLeft != null) { PeptideSpectrumMatch psmRight = right.OptimizedBestPsm(); if (psmRight != null) { return(-(psmLeft.ProteinScore.CompareTo(psmRight.ProteinScore))); } else { return(-1); } } else { return(1); } }
/// <summary> /// Aligns precursors observed Mz values /// </summary> /// <param name="result"></param> /// <param name="allPSMs"></param> public static void AlignPrecursorsByDiff(Result result, PeptideSpectrumMatches allPSMs) { List <double> observedDiff = new List <double>(); List <double> observedMz = new List <double>(); //Precursors and Tracks foreach (Precursor precursor in result.matchedPrecursors)//.ComputeAtFDR(result.dbOptions.maximumFalseDiscoveryRate, false)) { PeptideSpectrumMatch psm = precursor.OptimizedBestPsm(); if (psm.Target) { observedMz.Add(precursor.Track.MZ); observedDiff.Add(Numerics.MZFromMass(psm.Peptide.MonoisotopicMass, precursor.Charge) - precursor.Track.MZ); } } PolynominalRegression pr = new PolynominalRegression(observedMz, observedDiff, 2); foreach (Query query in result.queries) { query.precursor.Track.MZ += pr.Calculate(query.precursor.Track.MZ); query.precursor.Mass = Numerics.MassFromMZ(query.precursor.Track.MZ, query.precursor.Charge); foreach (Precursor precursor in query.precursor.Isotopes) { precursor.Track.MZ += pr.Calculate(precursor.Track.MZ); precursor.Mass = Numerics.MassFromMZ(precursor.Track.MZ, precursor.Charge); } foreach (Precursor precursor in query.precursor.OtherCharges) { precursor.Track.MZ += pr.Calculate(precursor.Track.MZ); precursor.Mass = Numerics.MassFromMZ(precursor.Track.MZ, precursor.Charge); } query.spectrum.PrecursorMZ += pr.Calculate(query.spectrum.PrecursorMZ); query.spectrum.PrecursorMass = Numerics.MassFromMZ(query.spectrum.PrecursorMZ, query.spectrum.PrecursorCharge); } foreach (PeptideSpectrumMatch psm in allPSMs) { psm.UpdatePrecursor(result.dbOptions); } }
public static double CropProductsBKP(Result result, PeptideSpectrumMatches allPSMs) { List <double> errorProduct = new List <double>(result.precursors.Count); foreach (Precursor precursor in result.matchedPrecursors) { PeptideSpectrumMatch psm = precursor.OptimizedBestPsm(); if (psm.Target) { foreach (ProductMatch match in psm.AllProductMatches) { errorProduct.Add(match.mass_diff); } } } double variance = Numerics.Variance(errorProduct); double stdev = Numerics.StandardDeviation(errorProduct); result.dbOptions.ConSole.WriteLine("Computed Product Variance = " + variance + " STDev = " + stdev); if (variance < stdev) { variance = stdev; } //variance = result.dbOptions.productMassTolerance.Value * ((2 * variance) / result.dbOptions.productMassTolerance.Value); int nbRemovedProduct = 0; foreach (PeptideSpectrumMatch psm in allPSMs) { for (int i = 0; i < psm.AllProductMatches.Count;) { if (Math.Abs(psm.AllProductMatches[i].mass_diff) > variance) { psm.AllProductMatches.RemoveAt(i); nbRemovedProduct++; } else { i++; } } psm.MatchingProducts = psm.AllProductMatches.Count; } int nbRemovedPSM = 0; foreach (Precursor precursor in result.precursors) { for (int i = 0; i < precursor.psms.Count;) { if (precursor.psms[i].MatchingProducts < 2) { precursor.psms.RemoveAt(i); } else { precursor.psms[i].Initialize(result.dbOptions, precursor.psms[i].AllProductMatches); i++; } } } result.dbOptions.ConSole.WriteLine("Removed " + nbRemovedProduct + " [" + nbRemovedPSM + " removed PSMs] Fragment matches outside the variance [" + variance + "]"); return(variance); }
public void ExportFragments(PeptideSpectrumMatch psm) { vsCSVWriter writer = new vsCSVWriter(dbOptions.OutputFolder + psm.Peptide.Sequence + "_" + vsCSV.GetFileName_NoExtension(psm.Query.sample.sSDF) + "_" + psm.Query.precursor.Track.RT + ".csv"); List <FragmentClass> fragments = new List <FragmentClass>(); foreach (FragmentClass fragment in dbOptions.fragments) //foreach (string fragment in FragmentDictionary.Fragments.Keys) { bool found = false; foreach (ProductMatch match in dbOptions.fragments.ComputeFragments(psm.Peptide, psm.Query.precursor.Charge, dbOptions)) { if (fragment == match.Fragment) { found = true; break; } } if (found) { fragments.Add(fragment); } } string title = "Theoretical Fragments"; for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { title += "," + fragment.Name + " ^" + charge; } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { title += "," + fragment.Name + " ^" + charge; } } writer.AddLine(title); for (int i = 1; i <= psm.Peptide.Length; i++) { string line = i.ToString(); for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { bool found = false; foreach (ProductMatch match in dbOptions.fragments.ComputeFragments(psm.Peptide, psm.Query.precursor.Charge, dbOptions)) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.theoMz; found = true; break; } } if (!found) { line += ","; } } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { bool found = false; foreach (ProductMatch match in dbOptions.fragments.ComputeFragments(psm.Peptide, psm.Query.precursor.Charge, dbOptions)) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.theoMz; found = true; break; } } if (!found) { line += ","; } } } writer.AddLine(line); } title = "Observed Fragments Intensities"; for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { title += "," + fragment.Name + " ^" + charge; } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { title += "," + fragment.Name + " ^" + charge; } } writer.AddLine(title); for (int i = 1; i <= psm.Peptide.Length; i++) { string line = i.ToString(); for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { bool found = false; foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.obsIntensity; found = true; break; } } if (!found) { line += ","; } } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { bool found = false; foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.obsIntensity; found = true; break; } } if (!found) { line += ","; } } } writer.AddLine(line); } title = "Observed Fragments Mz"; for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { title += "," + fragment.Name + " ^" + charge; } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { title += "," + fragment.Name + " ^" + charge; } } writer.AddLine(title); for (int i = 1; i <= psm.Peptide.Length; i++) { string line = i.ToString(); for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { bool found = false; foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.obsMz; found = true; break; } } if (!found) { line += ","; } } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { bool found = false; foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.obsMz; found = true; break; } } if (!found) { line += ","; } } } writer.AddLine(line); } title = "Error on Fragments"; for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { title += "," + fragment.Name + " ^" + charge; } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { title += "," + fragment.Name + " ^" + charge; } } writer.AddLine(title); for (int i = 1; i <= psm.Peptide.Length; i++) { string line = i.ToString(); for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in dbOptions.fragments) { bool found = false; foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.mass_diff; found = true; break; } } if (!found) { line += ","; } } } for (int charge = 1; charge <= psm.Query.precursor.Charge; charge++) { foreach (FragmentClass fragment in fragments) { bool found = false; foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment && match.fragmentPos == i && match.charge == charge) { line += "," + match.mass_diff; found = true; break; } } if (!found) { line += ","; } } } writer.AddLine(line); } writer.WriteToFile(); }
public void WriteFragmentation(bool target) { vsCSVWriter writer = new vsCSVWriter(dbOptions.OutputFolder + "FragmentStats_" + (target ? "Targets" : "Decoy") + ".csv"); writer.AddLine(" === Fragmentation of " + (target ? "Targets" : "Decoys") + " ==="); foreach (FragmentClass fragment in dbOptions.fragments) { double cumulIntensity = 0; int nbFrag = 0; Dictionary <int, int> positions = new Dictionary <int, int>(); foreach (Precursor precursor in matchedPrecursors) { PeptideSpectrumMatch psm = precursor.OptimizedBestPsm(); if (psm.Target == target) { foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment) { nbFrag++; if (!positions.ContainsKey(match.fragmentPos)) { positions.Add(match.fragmentPos, 1); } else { positions[match.fragmentPos]++; } cumulIntensity += match.obsIntensity; } } } } string strPos = ""; if (positions.Count > 0) { foreach (int key in positions.Keys) { strPos += "|" + key + ":" + positions[key]; } } else { strPos += ","; } writer.AddLine(" " + fragment.Name + ", Number of fragments = , " + nbFrag + ", Intensity = ," + cumulIntensity + ", fragment matched [" + strPos.Substring(1) + "]"); } foreach (FragmentClass fragment in dbOptions.fragments) //foreach (string fragment in FragmentDictionary.Fragments.Keys) { double cumulIntensity = 0; int nbFrag = 0; foreach (Precursor precursor in matchedPrecursors) { PeptideSpectrumMatch psm = precursor.OptimizedBestPsm(); if (psm.Target == target) { foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment) { nbFrag++; cumulIntensity += match.obsIntensity; } } } } writer.AddLine(" " + fragment + ", Number of fragments = ," + nbFrag + ", Intensity = ," + cumulIntensity); } foreach (FragmentClass fragment in dbOptions.fragments) //foreach (string fragment in FragmentDictionary.AAFragments.Keys) { double cumulIntensity = 0; int nbFrag = 0; foreach (Precursor precursor in matchedPrecursors) { PeptideSpectrumMatch psm = precursor.OptimizedBestPsm(); if (psm.Target == target) { foreach (ProductMatch match in psm.AllProductMatches) { if (fragment == match.Fragment) { nbFrag++; cumulIntensity += match.obsIntensity; } } } } writer.AddLine(" " + fragment + ", Number of fragments = ," + nbFrag + ", Intensity = ," + cumulIntensity); } writer.WriteToFile(); }
public static int AscendingSpectrumNumberComparison(PeptideSpectrumMatch left, PeptideSpectrumMatch right) { return(left.Query.spectrum.ScanNumber.CompareTo(right.Query.spectrum.ScanNumber)); }
public static int DescendingProteinScoreComparison(PeptideSpectrumMatch left, PeptideSpectrumMatch right) { return(-left.ProteinScore.CompareTo(right.ProteinScore)); }