Skip to content

[FIX] EmpiricalFormula: order the elements by atomic number, not by address - #10605

Merged
timosachsenberg merged 1 commit into
developfrom
claude/proteomics-lfq-alignment-ixcaue
Oct 17, 2026
Merged

timosachsenberg merged 1 commit into
developfrom
claude/proteomics-lfq-alignment-ixcaue

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

TOPP_ProSE_12_pin_rescore_idXML_out failed on Windows in many PRs: the percolator_score of two identical -rescore runs differed by about 1%. 055f6a0 worked around it by comparing only hit counts. This PR fixes the cause and restores the full comparison.

Cause

EmpiricalFormula keeps its elements in a std::map<const Element*, SignedSize>, i.e. in the order of the addresses of the Element objects. getMonoWeight(), getAverageWeight(), getLightestIsotopeWeight(), the isotope distributions and operator< follow that order.

ElementDB allocates each element on its own, so the order depends on the heap:

  • On Linux the elements lie in memory in order of atomic number.
  • On Windows the heap is randomized, and the order changes from run to run.

A different order of the additions changes the last bits of the masses, e.g. CalcMass 768.9445477617209 vs …211. In ProSE Test 12, Percolator trains on 896 PSMs with a single decoy, so one cross-validation fold has none. That ill-conditioned training turned last-bit differences in the features CalcMass, dm, absdm, fragment_mz_error_median_ppm and chance_match_surprise into ~1% differences in percolator_score. Thread races are ruled out, because the test runs with threads 1.

Fix

  • EmpiricalFormula::MapType_ gets a comparator: atomic number, then length and text of the symbol. An element comes before its isotopes (C, (12)C, (13)C), and the isotopes come in order of mass number. Only distinct elements with the same atomic number and symbol fall back to their address.
  • Atomic number and symbol are kept when ElementDB updates an element in place, so the map order stays valid.
  • operator< compared the keys by address (std::map::operator< uses < on the pointers); it now uses the same order.
  • The new order is the order the elements have in memory on Linux: all 367 elements of ElementDB match except two exotic pairs, (155)Gd/(81)Br and In/(106)Cd. So results on Linux do not change.
  • ProSE Test 12 with -rescore again diffs the full idXML of the two runs. The test part of 055f6a0 is reverted; the block is identical to the one before it.

How this was validated

There was no full build in this environment. The changed EmpiricalFormula.cpp was compiled against the 2026-10-14 nightly libraries, whose EmpiricalFormula is identical to develop, and took precedence over libOpenMS's copy.

  • Class test: EmpiricalFormula_test passes. Its new checks fail with the old code: the iteration order with isotopes, and Element objects whose address order is opposite to their atomic numbers (checked for iteration, weight and operator<).
  • Windows behaviour reproduced on Linux: a malloc shim shifts every allocation by a random offset and frees memory in random order.
    • Unchanged nightly ProSE: 8 -rescore runs gave 6 different idXMLs.
    • With the fix: 24 runs (12 heap layouts × the two Test 12 commands) gave the same idXML. It is byte-identical to the nightly output without the shim.
  • No change on Linux: I ran 174 test commands of formula-heavy tools with and without the fix (FeatureFinderMultiplex, AccurateMassSearch, MetaboliteAdductDecharger, SiriusExport, MassCalculator, FeatureFinderMetabo, IsobaricAnalyzer, NucleicAcidSearchEngine, RNAMassCalculator, MetaboliteSpectralMatcher, MRMAssay, OpenSwathDecoyGenerator, Digestor, TheoreticalSpectrumGenerator, SimpleSearchEngine, ProSE 1–12). Every test gave the same pass/fail result. The 100 output files are identical except for file paths and timings.
  • The full ctest suite, the other platforms and pyOpenMS are left to CI. The pyOpenMS bindings do not use the map type.

Not changed: ResidueDB::getResidues() returns a std::set<const Residue*>, which is also ordered by address. It is used by MassDecompositionAlgorithm, Tagger and OpenNuXL, and is not in the ProSE path.

Checklist

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes (EmpiricalFormula_test and the TOPP tests above pass against the nightly libraries; full suite not run, see above)
  • Updated or added python bindings for changed or new classes (Tick if no updates were necessary.)

Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Made empirical formula element ordering consistent by atomic number, with each element before its isotopes. This prevents run-to-run variation in theoretical masses and ProSE Percolator scores on Windows; Linux results are unchanged.
  • Tests
    • Updated formula ordering and ProSE output checks to verify consistent results.

…ddress

EmpiricalFormula kept its elements in a std::map<const Element*, SignedSize>,
i.e. in the order of the addresses of the Element objects, and summed weights
and isotope distributions in that order. ElementDB allocates every element on
its own, so the order depends on the heap: on Linux it follows the atomic
numbers, on Windows (randomized heap) it changes from run to run. Theoretical
masses then differed in the last bits between two identical runs; Percolator
rescoring in ProSE (Test 12, one decoy among 896 PSMs) turned that into
percolator_score differences of about 1% and failed
TOPP_ProSE_12_pin_rescore_idXML_out on Windows.

The map now orders the elements by atomic number, then by the length and the
text of the symbol: an element before its isotopes ("C", "(12)C", "(13)C"),
the isotopes by mass number. This is the order the elements have in memory on
Linux, so Linux results do not change. operator< compared the map keys by
address as well and now uses the same order.

ProSE Test 12 with -rescore again diffs the idXML of the two runs (reverts the
hit-count comparison of 055f6a0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014w9WfnJVb61NA9tyTwNbZ6
@coderabbitai

coderabbitai Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b86520bf-b104-4b96-9d8f-1bbf5b6fc912
📥 Commits

Reviewing files that changed from the base of the PR and between 89cbde6 and 8386f8c.

📒 Files selected for processing: 5
  • CHANGELOG
  • src/openms/include/OpenMS/CHEMISTRY/EmpiricalFormula.h
  • src/openms/source/CHEMISTRY/EmpiricalFormula.cpp
  • src/tests/class_tests/openms/source/EmpiricalFormula_test.cpp
  • src/tests/topp/CMakeLists.txt

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Walkthrough

EmpiricalFormula now orders elements by atomic number and places each element before its isotopes. The changelog reports run-to-run variation on Windows and unchanged Linux results. ProSE tests now compare generated idXML with the selected reference in both rescore modes.

Changes: EmpiricalFormula ordering

Layer / File(s) Summary
Define and test element ordering
src/openms/include/OpenMS/CHEMISTRY/EmpiricalFormula.h, src/openms/source/CHEMISTRY/EmpiricalFormula.cpp, src/tests/class_tests/openms/source/EmpiricalFormula_test.cpp, CHANGELOG
The map comparator orders elements by atomic number, symbol length, and symbol text. Formula comparison and iteration tests cover element and isotope ordering, including separately allocated elements. The changelog describes the reported platform results.
Compare ProSE idXML output
src/tests/topp/CMakeLists.txt
Both rescore modes compare generated idXML with the selected reference.

Suggested reviewers: cbielow, poshul

Priority: ➖ Normal

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 8386f

No actionable issue remains in the reviewed changes; the PR is ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. (2 skipped: 2… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: ordering EmpiricalFormula elements by atomic number instead of memory address.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. (2 skipped: 2 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

I’m a rabbit with a formula to sort,
By atomic number, I hop to the court.
Elements first, then isotopes in line,
My carrots compare in order just fine.
I thump for tests that keep results in time.

Comment @coderabbitai help to get the list of available commands.

@timosachsenberg
timosachsenberg merged commit d67b262 into develop Oct 17, 2026
33 checks passed
@timosachsenberg
timosachsenberg deleted the claude/proteomics-lfq-alignment-ixcaue branch October 17, 2026 12:45
timosachsenberg pushed a commit that referenced this pull request Oct 19, 2026
…#10301, #10611, #10606, #10608, #10609, #10617, #10610, #10578 ProSE Percolator rescoring, #10628)

Only CHANGELOG conflicted; both entries are kept. Develop's changes to FalseDiscoveryRate,
IDScoreGetterSetter, ConsensusMapMergerAlgorithm, FileInfo and ProSE work on the established
classes, which this branch keeps as they are.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B51nRFoHvZuVXD7cT8XZav
t0mdavid-m pushed a commit to t0mdavid-m/OpenMS that referenced this pull request Nov 9, 2026
…ddress (OpenMS#10605)

EmpiricalFormula kept its elements in a std::map<const Element*, SignedSize>,
i.e. in the order of the addresses of the Element objects, and summed weights
and isotope distributions in that order. ElementDB allocates every element on
its own, so the order depends on the heap: on Linux it follows the atomic
numbers, on Windows (randomized heap) it changes from run to run. Theoretical
masses then differed in the last bits between two identical runs; Percolator
rescoring in ProSE (Test 12, one decoy among 896 PSMs) turned that into
percolator_score differences of about 1% and failed
TOPP_ProSE_12_pin_rescore_idXML_out on Windows.

The map now orders the elements by atomic number, then by the length and the
text of the symbol: an element before its isotopes ("C", "(12)C", "(13)C"),
the isotopes by mass number. This is the order the elements have in memory on
Linux, so Linux results do not change. operator< compared the map keys by
address as well and now uses the same order.

ProSE Test 12 with -rescore again diffs the idXML of the two runs (reverts the
hit-count comparison of 055f6a0).

Claude-Session: https://claude.ai/code/session_014w9WfnJVb61NA9tyTwNbZ6

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit d67b262)
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.

1 participant