public double ComputeFDR(PeptideSpectrumMatches matches, out long nbTarget, out long nbDecoy) { nbTarget = 0; nbDecoy = 0; foreach (PeptideSpectrumMatch psm in matches) { if (KeepPSM(psm)) { if (psm.Target) { nbTarget++; } else { nbDecoy++; } } } if (nbTarget == 0) { return(1); } else { return((double)nbDecoy / (double)nbTarget); } }
public Precursor() { this.psms_AllPossibilities = new PeptideSpectrumMatches(); // GraphML_List<PeptideSpectrumMatch>(); this.psms = new PeptideSpectrumMatches(); // GraphML_List<PeptideSpectrumMatch>(); Isotopes = new GraphML_List <Precursor>(); OtherCharges = new GraphML_List <Precursor>(); }
public Precursor(Track track, int charge, Sample entry, double massShift = 0, GraphML_List <Precursor> isotopes = null)//double mz, double intensity, int charge = -1, double rt = 0, double massShift = 0, List<Precursor> isotopes = null) { INDEX = COMPTEUR++; this.Track = track; this.Charge = charge; this.sample = entry; this.psms_AllPossibilities = new PeptideSpectrumMatches(); // GraphML_List<PeptideSpectrumMatch>(); this.psms = new PeptideSpectrumMatches(); // GraphML_List<PeptideSpectrumMatch>(); // this.Mz = mz; // this.Intensity = intensity; // this.Charge = charge; this.Mass = Numerics.MassFromMZ(track.MZ, charge); // this.Rt = rt; MassShift = massShift; if (isotopes == null) { Isotopes = new GraphML_List <Precursor>(); } else { Isotopes = isotopes; } OtherCharges = new GraphML_List <Precursor>(); }
/// <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 Query(DBOptions dbOptions, Sample entry, ProductSpectrum spectrum, Precursor precursor, int spectraIndex = -1) { this.options = dbOptions; this.sample = entry; this.psms = new PeptideSpectrumMatches(); this.spectrum = spectrum; this.precursor = precursor; this.spectrumIndex = spectraIndex; if (spectrum != null && precursor != null) { this.TrapDistance = Math.Abs(spectrum.PrecursorMZ - precursor.Track.MZ); } }
/// <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); }
/// <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); } }
}//*/ /// <summary> /// Latest version of the search routine. Associates spectrum to digested peptide sequences, /// aligns precursors and fragments, clusters common precursors accross samples, create the list of detected /// peptide and protein sequences. /// TODO Align maps together (retention time) /// </summary> /// <param name="queries"></param> /// <returns></returns> public Result SearchVersionAugust2013(Queries queries, bool optimize) { Result result = new Result(); result.queries = queries; result.dbOptions = dbOptions; result.samples = Project; DBSearcher dbSearcher = new DBSearcher(dbOptions); Digestion ps = new Digestion(dbOptions); if (dbOptions.NoEnzymeSearch) { result.SetPrecursors(dbSearcher.Search(queries, ps.DigestProteomeOnTheFlyNoEnzyme(AllProteins, queries))); } else { result.SetPrecursors(dbSearcher.Search(queries, ps.DigestProteomeOnTheFly(AllProteins, false, queries))); } dbOptions.ConSole.WriteLine(result.precursors.Count + " precursors matched !"); PeptideSpectrumMatches allPSMs = new PeptideSpectrumMatches(); foreach (Precursor precursor in result.precursors) { foreach (PeptideSpectrumMatch psm in precursor.psms_AllPossibilities) { allPSMs.Add(psm); } } //Add all psm possibilities to psms list foreach (PeptideSpectrumMatch psm in allPSMs) { psm.Query.precursor.psms.Add(psm); } long nbTargets = result.SetPrecursors(result.precursors); dbOptions.ConSole.WriteLine("Targets before Optimizing Score Ratios : " + nbTargets + " [" + result.matchedPrecursors.Count + "]"); if (optimize) { //allPSMs.OptimizePSMScoreRatios(dbOptions, dbOptions.PSMFalseDiscoveryRate, result); //result.matchedPrecursors.OptimizePSMScoreRatios(dbOptions, dbOptions.PSMFalseDiscoveryRate, result); nbTargets = result.SetPrecursors(result.precursors); dbOptions.ConSole.WriteLine("Targets : " + nbTargets + " [" + result.matchedPrecursors.Count + "]"); //*/ //TODO Improve alignment results /* * Align.AlignPrecursorsByDiff(result, allPSMs); * nbTargets = result.SetPrecursors(result.precursors); * dbOptions.ConSole.WriteLine("Targets after precursor alignment : " + nbTargets + " [" + result.matchedPrecursors.Count + "]"); * * Align.AlignProductsByDiff(result, allPSMs); * nbTargets = result.SetPrecursors(result.precursors); * dbOptions.ConSole.WriteLine("Targets after fragment alignment : " + nbTargets + " [" + result.matchedPrecursors.Count + "]"); * //*/ /* * dbOptions.precursorMassTolerance.Value = Align.CropPrecursors(result, allPSMs); * nbTargets = result.SetPrecursors(result.precursors); * dbOptions.ConSole.WriteLine("Targets after croping precursors : " + nbTargets + " [" + result.matchedPrecursors.Count + "]"); * * dbOptions.productMassTolerance.Value = Align.CropProducts(result, allPSMs); * nbTargets = result.SetPrecursors(result.precursors); * dbOptions.ConSole.WriteLine("Targets after croping fragments : " + nbTargets + " [" + result.matchedPrecursors.Count + "]"); * //*/ } //*/ allPSMs = null; long bestTargets = nbTargets; MSSearcher msSearcher = new MSSearcher(dbOptions, Project); msSearcher.CumulPsm(result.matchedPrecursors);//TODO Check if its still needed //Step 1 : Cluster psms together based on precursor feature //TODO Implement ProteoProfile Scoring based clustering //Group in clusters result.clusters = msSearcher.Search(result.matchedPrecursors, true); //Todo Align retention times //Todo redo clusterization, based on retention time aligned maps //Step 2 : Regroup based on peptide sequence (Morpheus code) PeptideSearcher pepSearcher = new PeptideSearcher(dbOptions); result.peptides = pepSearcher.Search(result.clusters, result.matchedPrecursors, true); result.peptideSequences = pepSearcher.Search(result.clusters, result.matchedPrecursors, false); //Step 3 : Regroup based on protein sequences (Morpheus code) ProteinSearcher protSearcher = new ProteinSearcher(dbOptions); result.proteins = protSearcher.SearchLatest(result.peptideSequences);//, dbSearcher.DicOfProteins); //long lastNbTarget = nbTargets; //do //{ // lastNbTarget = nbTargets; //UpdatePsmScores(result.proteins); // nbTargets = result.SetPrecursors(result.precursors); //} while (lastNbTarget < nbTargets);//*/ result.peptides = pepSearcher.Search(result.clusters, result.matchedPrecursors, true); result.peptideSequences = pepSearcher.Search(result.clusters, result.matchedPrecursors, false); if (optimize) { dbOptions.ConSole.WriteLine("Targets before second Optimization of Score Ratios : " + nbTargets + " [" + result.matchedPrecursors.Count + "]"); //result.matchedPrecursors.OptimizePSMScoreRatios(dbOptions, dbOptions.PSMFalseDiscoveryRate, result); nbTargets = result.SetPrecursors(result.precursors); dbOptions.ConSole.WriteLine("Targets after ReOptimizing PSM Score Ratios : " + nbTargets + " [" + result.matchedPrecursors.Count + "]");//*/ } //Step 5 : Compute the new number of Targets nbTargets = result.SetPrecursors(result.precursors); if (nbTargets < bestTargets) { dbOptions.ConSole.WriteLine("FAILED to improve PSMs while adding protein and peptide information"); } dbOptions.ConSole.WriteLine(result.matchedPrecursors.Count + " precursors remaining after ProPheus Search!"); return(result); }//*/
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 Query() { this.psms = new PeptideSpectrumMatches(); }