private void CalculateBasicFDR(IList <InputFile> csvFiles, bool isBatched = false) { string msg = "Calculating first order FDR threshold"; if (isBatched) { msg += " in batch..."; Log(msg); // Calculate global threshold double threshold = FalseDiscoveryRate <Peptide, double> .CalculateThreshold(_allPeptides, _maximumFalseDiscoveryRate); foreach (InputFile csvFile in csvFiles) { csvFile.ScoreThreshold = threshold; } } else { msg += " separately..."; Log(msg); // Calculate each file separately foreach (InputFile csvFile in csvFiles) { csvFile.ScoreThreshold = FalseDiscoveryRate <Peptide, double> .CalculateThreshold(csvFile.Peptides, _maximumFalseDiscoveryRate); } } foreach (InputFile csvFile in csvFiles) { double threshold = csvFile.ScoreThreshold; List <Peptide> passingPeptides = csvFile.Peptides.Where(peptide => peptide.FdrScoreMetric <= threshold).ToList(); csvFile.FdrFilteredPeptides = passingPeptides; List <PSM> passingPsms = csvFile.PeptideSpectralMatches.Where(psm => psm.FdrScoreMetric <= threshold).ToList(); csvFile.FdrFilteredPSMs = passingPsms; int total = csvFile.FdrFilteredPeptides.Count; int decoys = csvFile.FdrFilteredPeptides.Count(peptide => peptide.IsDecoy); int targets = total - decoys; Log( string.Format( "{0:N0} peptides ({1:N0} decoys FDR = {2:F4}) pass the e-value threshold of {3:G4} for {4}", targets, decoys, 100.0 * decoys / (double)targets, csvFile.ScoreThreshold, csvFile.Name)); } }
private Tuple <double, double> CalculateBestPPMError(IEnumerable <Peptide> inputPeptides, double maximumFalseDisoveryRate = 0.01, int steps = 10, double minimumIncrement = 0.05) { List <Peptide> peptides = inputPeptides.OrderBy(pep => pep.CorrectedPrecursorErrorPPM).ToList(); double[] precursorPPMs = peptides.Select(pep => pep.CorrectedPrecursorErrorPPM).ToArray(); double bestppmError = 0; double max = peptides[peptides.Count - 1].CorrectedPrecursorErrorPPM; double maxPrecursorError = Math.Min(max, _maximumPPMError); double minPrecursorError = 0; double increment = (maxPrecursorError - minPrecursorError) / steps; increment = Math.Max(increment, minimumIncrement); double bestCount = 0; for (double ppmError = minPrecursorError; ppmError <= maxPrecursorError; ppmError += increment) { int index = Array.BinarySearch(precursorPPMs, ppmError); if (index < 0) { index = ~index; } int count = FalseDiscoveryRate <Peptide, double> .Count(peptides.Take(index).ToList(), maximumFalseDisoveryRate); if (count <= bestCount) { continue; } bestCount = count; bestppmError = ppmError; } List <Peptide> filteredPeptides = new List <Peptide>(peptides.Where(pep => pep.CorrectedPrecursorErrorPPM <= bestppmError)); // Calculate the e-value threshold for those filtered peptides double threshold = FalseDiscoveryRate <Peptide, double> .CalculateThreshold(filteredPeptides, maximumFalseDisoveryRate); return(new Tuple <double, double>(bestppmError, threshold)); }