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

Conversation

bspratt
Copy link
Member

@bspratt bspratt commented Mar 3, 2025

… when the m/z is negative and has greater absolute value than any negative precursor m/z.

Test for the fix was added opportunistically to an existing test involving mixed polarity documents.

Reported by user Paul

…ecursor m/z match in a mixed polarity document when the m/z is negative and has greater absolute value than any negative precursor m/z. (Test for the fix was added opportunistically to an existing test involving mixed polarity documents)

Reported by user Paul
Copy link
Contributor

@nickshulman nickshulman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

You might want to simplify the logic a little. After the binary search, it is either an exact match, or "~i" points to the first item which is greater than the key in which case the closest match is at either "~i-1" or "~i".

Here's what I was thinking this could be:

        public PeptideDocNode FindPeptide(SignedMz precursorMz)
        {
            if (_precursorMzPeptideList.Count == 0)
                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)
            {
                return _precursorMzPeptideList[i].NodePeptide;
            }
            PeptidePrecursorMz closestMatch = null;
            double closestDistance = double.MaxValue;
            foreach (var candidateIndex in new[] { ~i - 1, ~i })
            {
                if (candidateIndex < 0 || candidateIndex >= _precursorMzPeptideList.Count)
                {
                    continue;
                }
                var candidate = _precursorMzPeptideList[candidateIndex];
                if (candidate.PrecursorMz.IsNegative != precursorMz.IsNegative)
                {
                    continue;
                }

                var distance = Math.Abs(candidate.PrecursorMz - precursorMz);
                if (closestMatch == null || distance < closestDistance)
                {
                    closestMatch = candidate;
                    closestDistance = distance;
                }
            }

            // Return only if the match is within allowed tolerance.
            if (closestMatch == null || Math.Abs(closestMatch.PrecursorMz - precursorMz) > _mzMatchTolerance)
            {
                return null;
            }
            return closestMatch.NodePeptide;
        }

@bspratt
Copy link
Member Author

bspratt commented Mar 4, 2025

Another thoughtful review, thanks. I have incorporated your suggestions.

@bspratt bspratt merged commit bc6a7c4 into master Mar 4, 2025
11 checks passed
@bspratt bspratt deleted the Skyline/work/20250303_signedmz_search_fix branch March 4, 2025 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants