|
| 1 | +#nullable enable |
| 2 | +using System; |
| 3 | +using Omics.Fragmentation; |
| 4 | +using System.Collections.Generic; |
| 5 | +using Omics; |
| 6 | +using System.Linq; |
| 7 | +using Omics.Modifications; |
| 8 | + |
| 9 | +namespace EngineLayer; |
| 10 | + |
| 11 | +public interface IGptmdFilter |
| 12 | +{ |
| 13 | + public static string GetFilterTypeName(IGptmdFilter filter) => filter.GetType().Name; |
| 14 | + |
| 15 | + bool Passes( |
| 16 | + IBioPolymerWithSetMods candidatePeptide, |
| 17 | + SpectralMatch psm, |
| 18 | + double newScore, |
| 19 | + double originalScore, |
| 20 | + List<MatchedFragmentIon> matchedIons, |
| 21 | + int peptideOneBasedModSite, |
| 22 | + int peptideLength, |
| 23 | + Modification modAttemptingToAdd); |
| 24 | +} |
| 25 | + |
| 26 | +/// <summary> |
| 27 | +/// Requires that the new score is greater than the original score. |
| 28 | +/// </summary> |
| 29 | +public sealed class ImprovedScoreFilter : IGptmdFilter |
| 30 | +{ |
| 31 | + public bool Passes( |
| 32 | + IBioPolymerWithSetMods candidatePeptide, |
| 33 | + SpectralMatch psm, |
| 34 | + double newScore, |
| 35 | + double originalScore, |
| 36 | + List<MatchedFragmentIon> matchedIons, |
| 37 | + int peptideOneBasedModSite, |
| 38 | + int peptideLength, |
| 39 | + Modification modAttemptingToAdd) |
| 40 | + { |
| 41 | + return newScore > originalScore; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// <summary> |
| 46 | +/// Requires the mod site to be covered by at least one N-terminal and one C-terminal ion. |
| 47 | +/// That is, ions from both directions must include the mod, even if not flanking it. |
| 48 | +/// </summary> |
| 49 | +public sealed class DualDirectionalIonCoverageFilter : IGptmdFilter |
| 50 | +{ |
| 51 | + public bool Passes( |
| 52 | + IBioPolymerWithSetMods candidatePeptide, |
| 53 | + SpectralMatch psm, |
| 54 | + double newScore, |
| 55 | + double originalScore, |
| 56 | + List<MatchedFragmentIon> matchedIons, |
| 57 | + int peptideOneBasedModSite, |
| 58 | + int peptideLength, |
| 59 | + Modification modAttemptingToAdd) |
| 60 | + { |
| 61 | + if (matchedIons == null || matchedIons.Count == 0) |
| 62 | + return false; |
| 63 | + |
| 64 | + int site = peptideOneBasedModSite; |
| 65 | + |
| 66 | + bool coveredFromNTerm = matchedIons.Any(m => |
| 67 | + m.NeutralTheoreticalProduct.ProductType == ProductType.M || |
| 68 | + (m.NeutralTheoreticalProduct.Terminus is FragmentationTerminus.N or FragmentationTerminus.FivePrime && |
| 69 | + m.NeutralTheoreticalProduct.ResiduePosition >= site) |
| 70 | + ); |
| 71 | + |
| 72 | + bool coveredFromCTerm = matchedIons.Any(m => |
| 73 | + m.NeutralTheoreticalProduct.ProductType == ProductType.M || |
| 74 | + (m.NeutralTheoreticalProduct.Terminus is FragmentationTerminus.C or FragmentationTerminus.ThreePrime && |
| 75 | + m.NeutralTheoreticalProduct.ResiduePosition < site) |
| 76 | + ); |
| 77 | + |
| 78 | + if (modAttemptingToAdd.LocationRestriction.Contains("terminal", StringComparison.InvariantCultureIgnoreCase)) |
| 79 | + return coveredFromCTerm || coveredFromNTerm; |
| 80 | + |
| 81 | + return coveredFromNTerm && coveredFromCTerm; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// <summary> |
| 86 | +/// Requires the mod site to be covered by at least one N-terminal or one C-terminal ion. |
| 87 | +/// That is, ions from one direction must include the mod, even if not flanking it. |
| 88 | +/// </summary> |
| 89 | +public sealed class UniDirectionalIonCoverageFilter : IGptmdFilter |
| 90 | +{ |
| 91 | + public bool Passes( |
| 92 | + IBioPolymerWithSetMods candidatePeptide, |
| 93 | + SpectralMatch psm, |
| 94 | + double newScore, |
| 95 | + double originalScore, |
| 96 | + List<MatchedFragmentIon> matchedIons, |
| 97 | + int peptideOneBasedModSite, |
| 98 | + int peptideLength, |
| 99 | + Modification modAttemptingToAdd) |
| 100 | + { |
| 101 | + if (matchedIons == null || matchedIons.Count == 0) |
| 102 | + return false; |
| 103 | + |
| 104 | + int site = peptideOneBasedModSite; |
| 105 | + |
| 106 | + bool coveredFromNTerm = matchedIons.Any(m => |
| 107 | + m.NeutralTheoreticalProduct.ProductType == ProductType.M || |
| 108 | + (m.NeutralTheoreticalProduct.Terminus is FragmentationTerminus.N or FragmentationTerminus.FivePrime && |
| 109 | + m.NeutralTheoreticalProduct.ResiduePosition >= site) |
| 110 | + ); |
| 111 | + |
| 112 | + bool coveredFromCTerm = matchedIons.Any(m => |
| 113 | + m.NeutralTheoreticalProduct.ProductType == ProductType.M || |
| 114 | + (m.NeutralTheoreticalProduct.Terminus is FragmentationTerminus.C or FragmentationTerminus.ThreePrime && |
| 115 | + m.NeutralTheoreticalProduct.ResiduePosition < site) |
| 116 | + ); |
| 117 | + |
| 118 | + return coveredFromNTerm || coveredFromCTerm; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// <summary> |
| 123 | +/// Requires flanking ions — a fragment from *before* and one from *after* the mod site, |
| 124 | +/// regardless of fragmentation direction. |
| 125 | +/// </summary> |
| 126 | +public sealed class FlankingIonCoverageFilter : IGptmdFilter |
| 127 | +{ |
| 128 | + public bool Passes( |
| 129 | + IBioPolymerWithSetMods candidatePeptide, |
| 130 | + SpectralMatch psm, |
| 131 | + double newScore, |
| 132 | + double originalScore, |
| 133 | + List<MatchedFragmentIon> matchedIons, |
| 134 | + int peptideOneBasedModSite, |
| 135 | + int peptideLength, |
| 136 | + Modification modAttemptingToAdd) |
| 137 | + { |
| 138 | + if (matchedIons == null || matchedIons.Count == 0) |
| 139 | + return false; |
| 140 | + |
| 141 | + int site = peptideOneBasedModSite; |
| 142 | + |
| 143 | + bool leftFlank = matchedIons.Any(m => |
| 144 | + m.NeutralTheoreticalProduct.ResiduePosition == site - 1); |
| 145 | + |
| 146 | + bool rightFlank = matchedIons.Any(m => |
| 147 | + m.NeutralTheoreticalProduct.ResiduePosition == site); |
| 148 | + |
| 149 | + if (modAttemptingToAdd.LocationRestriction.Contains("terminal", StringComparison.InvariantCultureIgnoreCase)) |
| 150 | + return leftFlank || rightFlank; |
| 151 | + |
| 152 | + return leftFlank && rightFlank; |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments