Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for a "polarity mismatch" exception when searching for nearest precursor m/z match in a mixed polarity document... #3383

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions pwiz_tools/Skyline/Model/Results/PeptideFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
using System;
using System.Collections.Generic;
using System.Collections;
using pwiz.Common.Chemistry;

namespace pwiz.Skyline.Model.Results
Expand Down Expand Up @@ -59,6 +58,7 @@ public PeptideDocNode FindPeptide(SignedMz precursorMz)
return null;

// Find closest precursor Mz match.
// Watch out for negative values, which sort before positive values.
var lookup = new PeptidePrecursorMz(null, precursorMz);
int i = _precursorMzPeptideList.BinarySearch(lookup, PeptidePrecursorMz.COMPARER);
if (i < 0)
Expand All @@ -67,11 +67,33 @@ public PeptideDocNode FindPeptide(SignedMz precursorMz)
if (i >= _precursorMzPeptideList.Count)
i = _precursorMzPeptideList.Count - 1;
else if (i > 0 &&
precursorMz - _precursorMzPeptideList[i - 1].PrecursorMz <
_precursorMzPeptideList[i].PrecursorMz - precursorMz)
_precursorMzPeptideList[i - 1].PrecursorMz.IsNegative == precursorMz.IsNegative &&
_precursorMzPeptideList[i].PrecursorMz.IsNegative == precursorMz.IsNegative &&
precursorMz - _precursorMzPeptideList[i - 1].PrecursorMz <
_precursorMzPeptideList[i].PrecursorMz - precursorMz)
i--;
}
var closestMatch = _precursorMzPeptideList[i];
if (closestMatch.PrecursorMz.IsNegative != precursorMz.IsNegative)
{
// Just past the negative range, or just below the positive range
if (precursorMz.IsNegative)
{
if (!_precursorMzPeptideList[0].PrecursorMz.IsNegative)
{
return null; // There aren't any negative values in the list
}
closestMatch = _precursorMzPeptideList[i - 1]; // End of negative range
}
else
{
if (i+1 >= _precursorMzPeptideList.Count)
{
return null; // There aren't any positive values in the list
}
closestMatch = _precursorMzPeptideList[i + 1]; // Start of positive range
}
}

// Return color seed only if the match is within allowed tolerance.
return Math.Abs(closestMatch.PrecursorMz - precursorMz) > _mzMatchTolerance
Expand All @@ -97,10 +119,15 @@ public class MzComparer : IComparer<PeptidePrecursorMz>
public int Compare(PeptidePrecursorMz p1, PeptidePrecursorMz p2)
{
// ReSharper disable PossibleNullReferenceException
return Comparer.Default.Compare(p1.PrecursorMz, p2.PrecursorMz);
return p1.PrecursorMz.CompareTo(p2.PrecursorMz);
// ReSharper restore PossibleNullReferenceException
}
}

public override string ToString()
{
return $@"{PrecursorMz.RawValue} {NodePeptide}"; // For debug convenience, not user-facing
}
}
}
}
12 changes: 12 additions & 0 deletions pwiz_tools/Skyline/TestFunctional/ExportIsolationListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using pwiz.Common.Chemistry;
using pwiz.Skyline.Alerts;
using pwiz.Skyline.FileUI;
using pwiz.Skyline.Model;
using pwiz.Skyline.Model.DocSettings;
using pwiz.Skyline.Model.DocSettings.Extensions;
using pwiz.Skyline.Model.Results;
using pwiz.Skyline.Properties;
using pwiz.Skyline.Util.Extensions;
using pwiz.SkylineTestUtil;
Expand Down Expand Up @@ -151,6 +153,16 @@ protected override void DoTest()
var ceFirst = AsSmallMoleculesNegative ? 20.3 : 20.4;
var ceLast = AsSmallMoleculesNegative ? 19.1 : 19.2;

// Test an issue found in the PeptideFinder class with mixed polarity docs
if (SkylineWindow.Document.IsMixedPolarity())
{
var beyondMaxNegMz = (from precursor in SkylineWindow.Document.MoleculeTransitionGroups
where precursor.PrecursorMz.IsNegative
select precursor.PrecursorMz.RawValue).Min()-100.0;
var finder = new PeptideFinder(SkylineWindow.Document);
AssertEx.IsNull(finder.FindPeptide(new SignedMz(beyondMaxNegMz))); // This will throw a "polarity mismatch" exception if the issue is not fixed
}

// Export Agilent unscheduled DDA list.
ExportIsolationList(
"AgilentUnscheduledDda.csv",
Expand Down