|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using Chemistry; |
| 4 | +using MassSpectrometry; |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace Test |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Unit tests for the resolved "most abundant mass" precursor-selection support (Strategy B). |
| 11 | + /// |
| 12 | + /// Terminology pinned by these tests: |
| 13 | + /// • most-abundant mass = the neutral mass of the single most intense (tallest) isotopic peak, |
| 14 | + /// proton-corrected (<see cref="IsotopicEnvelope.MostAbundantObservedNeutralMass"/>); |
| 15 | + /// • most-abundant offset = GetDiffToMonoisotopic(GetMostIntenseMassIndex(mono)) on the averagine |
| 16 | + /// model — the gap from the monoisotopic mass to that tallest isotopologue. |
| 17 | + /// The intensity-weighted average (centroid) mass and the isotopically-unresolved path are a |
| 18 | + /// separate change and are tested with that work, not here. All tests build synthetic envelopes |
| 19 | + /// from the Averagine model — no deconvolution is run. |
| 20 | + /// </summary> |
| 21 | + [TestFixture] |
| 22 | + public sealed class TestMostAbundantMass |
| 23 | + { |
| 24 | + private static readonly AverageResidue Model = new Averagine(); |
| 25 | + |
| 26 | + // Most-abundant offset for a monoisotopic mass = the averagine diff-to-monoisotopic of the |
| 27 | + // nearest mass bin (the mass-keyed composition consumers use in place of a dedicated method). |
| 28 | + private static double MostAbundantOffset(double monoMass) => Model.GetDiffToMonoisotopic(Model.GetMostIntenseMassIndex(monoMass)); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Builds a perfect synthetic envelope: peaks at exact theoretical m/z with |
| 32 | + /// Averagine-proportional intensities. (Same construction as TestDeconvolutionScorerUnit.) |
| 33 | + /// </summary> |
| 34 | + private static List<(double mz, double intensity)> BuildPerfectPeaks(double monoMass, int charge, double baseIntens = 1e6) |
| 35 | + { |
| 36 | + int avgIdx = Model.GetMostIntenseMassIndex(monoMass); |
| 37 | + double[] rawMasses = Model.GetAllTheoreticalMasses(avgIdx); |
| 38 | + double[] rawIntens = Model.GetAllTheoreticalIntensities(avgIdx); |
| 39 | + |
| 40 | + var sorted = rawMasses.Zip(rawIntens).OrderBy(p => p.First).ToArray(); |
| 41 | + |
| 42 | + double isotopeStep = Constants.C13MinusC12 / charge; |
| 43 | + double monoMz = monoMass.ToMz(charge); |
| 44 | + var peaks = new List<(double mz, double intensity)>(); |
| 45 | + for (int n = 0; n < sorted.Length; n++) |
| 46 | + { |
| 47 | + double intensity = baseIntens * sorted[n].Second; |
| 48 | + if (intensity < baseIntens * 0.001) continue; |
| 49 | + peaks.Add((monoMz + n * isotopeStep, intensity)); |
| 50 | + } |
| 51 | + return peaks; |
| 52 | + } |
| 53 | + |
| 54 | + private static IsotopicEnvelope BuildPerfectEnvelope(double monoMass, int charge, double baseIntens = 1e6) |
| 55 | + { |
| 56 | + var peaks = BuildPerfectPeaks(monoMass, charge, baseIntens); |
| 57 | + return new IsotopicEnvelope(0, peaks, monoMass, charge, peaks.Sum(p => p.intensity), 0.999); |
| 58 | + } |
| 59 | + |
| 60 | + // ── AverageResidue most-abundant offset ───────────────────────────────── |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void MostAbundantOffset_IsNonNegativeAndNonDecreasingWithMass() |
| 64 | + { |
| 65 | + double[] masses = { 500, 2000, 5000, 10000, 20000, 40000 }; |
| 66 | + double prev = double.NegativeInfinity; |
| 67 | + foreach (double m in masses) |
| 68 | + { |
| 69 | + double offset = MostAbundantOffset(m); |
| 70 | + Assert.That(offset, Is.GreaterThanOrEqualTo(-1e-6)); // ~0 at tiny mass (mono IS most abundant) |
| 71 | + Assert.That(offset, Is.GreaterThanOrEqualTo(prev - 1e-6), $"offset decreased at mass {m}"); |
| 72 | + prev = offset; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void MostAbundantOffset_IsNearZeroForSmallMass() |
| 78 | + { |
| 79 | + // A small peptide's monoisotopic peak is (nearly) the most abundant. |
| 80 | + Assert.That(MostAbundantOffset(500), Is.LessThan(0.5)); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void MostAbundantOffset_GrowsRoughlyOneNeutronPer1600Da() |
| 85 | + { |
| 86 | + // ~1 13C neutron (~1.00235 Da) per ~1.6 kDa. Assert the offset at 16 kDa is in a |
| 87 | + // physically plausible band (≈ 9–11 Da) rather than an exact value. |
| 88 | + double offset = MostAbundantOffset(16000); |
| 89 | + Assert.That(offset, Is.GreaterThan(8.0).And.LessThan(12.0)); |
| 90 | + } |
| 91 | + |
| 92 | + // ── IsotopicEnvelope most-abundant observed mass ──────────────────────── |
| 93 | + |
| 94 | + [Test] |
| 95 | + public void MostAbundantObservedNeutralMass_IsProtonCorrectedNeutralMass() |
| 96 | + { |
| 97 | + const int charge = 10; |
| 98 | + var env = BuildPerfectEnvelope(15000, charge); |
| 99 | + |
| 100 | + // The proton-corrected neutral mass equals the most intense peak's m/z .ToMass(charge)... |
| 101 | + double mostIntenseMz = env.Peaks.MaxBy(p => p.intensity).mz; |
| 102 | + Assert.That(env.MostAbundantObservedNeutralMass, Is.EqualTo(mostIntenseMz.ToMass(charge)).Within(1e-6)); |
| 103 | + |
| 104 | + // ...and it differs from the un-proton-corrected mz*|z| field by exactly z proton masses. |
| 105 | + Assert.That(env.MostAbundantObservedIsotopicMass - env.MostAbundantObservedNeutralMass, |
| 106 | + Is.EqualTo(charge * Constants.ProtonMass).Within(1e-6)); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void MostAbundantObservedNeutralMass_MatchesMonoPlusAveragineOffset() |
| 111 | + { |
| 112 | + // Ties the two features together: the observed most-abundant neutral mass of a perfect |
| 113 | + // envelope ≈ candidate monoisotopic + averagine most-abundant offset. |
| 114 | + const double mono = 12000; |
| 115 | + const int charge = 12; |
| 116 | + var env = BuildPerfectEnvelope(mono, charge); |
| 117 | + |
| 118 | + double predicted = mono + MostAbundantOffset(mono); |
| 119 | + Assert.That(env.MostAbundantObservedNeutralMass, Is.EqualTo(predicted).Within(0.15)); |
| 120 | + } |
| 121 | + |
| 122 | + [Test] |
| 123 | + public void DeconvolutionConstructor_ComputesMostAbundantObservedNeutralMass() |
| 124 | + { |
| 125 | + // The 5-arg mzLib-deconvolution constructor must compute the most-abundant observed mass. |
| 126 | + const double mono = 12000; |
| 127 | + const int charge = 12; |
| 128 | + var peaks = BuildPerfectPeaks(mono, charge); |
| 129 | + var env = new IsotopicEnvelope(peaks, mono, charge, peaks.Sum(p => p.intensity), 0.5); |
| 130 | + |
| 131 | + double mostIntenseMz = peaks.MaxBy(p => p.intensity).mz; |
| 132 | + Assert.That(env.MostAbundantObservedNeutralMass, Is.EqualTo(mostIntenseMz.ToMass(charge)).Within(1e-6)); |
| 133 | + } |
| 134 | + |
| 135 | + [Test] |
| 136 | + public void FileReadEnvelope_HasNoMostAbundantPeak_ReturnsSentinel() |
| 137 | + { |
| 138 | + // The file-read constructor carries a neutral mass but no observed isotopic envelope, so the |
| 139 | + // most-abundant observed mass is undefined: both the m/z×|charge| form and the proton-corrected |
| 140 | + // form report the -1 sentinel rather than a synthetic value. |
| 141 | + const double mono = 8000; |
| 142 | + const int charge = 8; |
| 143 | + var env = new IsotopicEnvelope(mono, 1e6, charge); |
| 144 | + |
| 145 | + Assert.That(env.MostAbundantObservedIsotopicMass, Is.EqualTo(-1)); |
| 146 | + Assert.That(env.MostAbundantObservedNeutralMass, Is.EqualTo(-1)); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments