Skip to content

Commit

Permalink
Updated FilteredPsms to only iterate through PSMs once (#2430)
Browse files Browse the repository at this point in the history
* Updated FilterePsms to only iterate through PSMs once

* Updated filtering
  • Loading branch information
Alexander-Sol authored Oct 28, 2024
1 parent c2d5a41 commit 32658b1
Showing 1 changed file with 51 additions and 37 deletions.
88 changes: 51 additions & 37 deletions MetaMorpheus/TaskLayer/FilteredPsms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,43 +112,14 @@ public static FilteredPsms Filter(IEnumerable<SpectralMatch> psms,
}
}

if (!includeHighQValuePsms)
{
filteredPsms = filterType.Equals(FilterType.QValue)
? psms.Where(p => p.GetFdrInfo(filterAtPeptideLevel) != null
&& p.GetFdrInfo(filterAtPeptideLevel).QValue <= filterThreshold
&& p.GetFdrInfo(filterAtPeptideLevel).QValueNotch <= filterThreshold).ToList()
: psms.Where(p => p.GetFdrInfo(filterAtPeptideLevel) != null && p.GetFdrInfo(filterAtPeptideLevel).PEP_QValue <= filterThreshold).ToList();
}
else
{
filteredPsms = psms.ToList();
}

if (!includeDecoys)
{
filteredPsms.RemoveAll(p => p.IsDecoy);
}
if (!includeContaminants)
{
filteredPsms.RemoveAll(p => p.IsContaminant);
}
if (!includeAmbiguous)
{
filteredPsms.RemoveAll(p => p.BaseSequence.IsNullOrEmpty());
}
if (!includeAmbiguousMods)
{
filteredPsms.RemoveAll(p => p.FullSequence.IsNullOrEmpty());
}
if (filterAtPeptideLevel)
{
//Choose the top scoring PSM for each peptide
filteredPsms = filteredPsms
.OrderByDescending(p => p)
.GroupBy(b => b.FullSequence)
.Select(b => b.FirstOrDefault()).ToList();
}
filteredPsms = psms.Where(psm =>
(includeDecoys || !psm.IsDecoy)
&& (includeContaminants || !psm.IsContaminant)
&& (includeAmbiguous || !psm.BaseSequence.IsNullOrEmpty())
&& (includeAmbiguousMods || !psm.FullSequence.IsNullOrEmpty()))
.FilterByQValue(includeHighQValuePsms, filterThreshold, filterAtPeptideLevel, filterType)
.CollapseToPeptides(filterAtPeptideLevel)
.ToList();

return new FilteredPsms(filteredPsms, filterType, filterThreshold, filteringNotPerformed, filterAtPeptideLevel);
}
Expand All @@ -163,4 +134,47 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
return FilteredPsmsList.GetEnumerator();
}
}

public static class FilteredPsmsExtensions
{
public static IEnumerable<SpectralMatch> CollapseToPeptides(this IEnumerable<SpectralMatch> psms, bool filterAtPeptideLevel)
{
if(!filterAtPeptideLevel)
{
return psms;
}
else
{
return psms
.OrderByDescending(p => p)
.GroupBy(b => b.FullSequence)
.Select(b => b.FirstOrDefault());
}
}

public static IEnumerable<SpectralMatch> FilterByQValue(this IEnumerable<SpectralMatch> psms, bool includeHighQValuePsms, double qValueThreshold, bool filterAtPeptideLevel, FilterType filterType)
{
foreach (var psm in psms)
{
if (includeHighQValuePsms)
{
yield return psm;
}
else if (filterType == FilterType.PepQValue)
{
if (psm.GetFdrInfo(filterAtPeptideLevel).PEP_QValue <= qValueThreshold)
{
yield return psm;
}
}
else
{
if (psm.GetFdrInfo(filterAtPeptideLevel).QValue <= qValueThreshold && psm.GetFdrInfo(filterAtPeptideLevel).QValueNotch <= qValueThreshold)
{
yield return psm;
}
}
}
}
}
}

0 comments on commit 32658b1

Please sign in to comment.