Skip to content

fix: decide MI metal-bond disconnection per fragment - #309

Merged
nnuk merged 7 commits into
devfrom
fix/305-mi-per-fragment
Nov 7, 2026
Merged

nnuk merged 7 commits into
devfrom
fix/305-mi-per-fragment

Conversation

@fbaensch-beilstein

@fbaensch-beilstein fbaensch-beilstein commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked PR. This branch is based on #306 (fix/mi-charge-separated-group12). Merge #306 first, then retarget this PR to dev and merge it. Because the base is not the default branch, GitHub will not close #305 automatically if this is merged before being retargeted; close #305 by hand in that case.

Fixes #305

Problem

MolecularInorganicsPreprocessing perceived ring systems with MarkRingSystemsInp(at, num_at, 0). That walks only the fragment containing atom 0, so chelate-ring protection never applied to metals in later fragments, and the result depended on atom order. Example from #305: the Ti chelate of test_MI_Issue244_TitaniumComplex_1 is kept, but with a water inserted as atom 1 it is disconnected.

Change

  1. Ring perception in every fragment. New MolecularInorganicsMarkFragments() labels each fragment and calls MarkRingSystemsInp once per fragment, shifting ring-system IDs so they stay unique across fragments. MI clears the ring fields again before returning.
  2. Decision per fragment. Since cf3d805 the decision tree used one keep_all_metal_bonds flag for the whole input: any reason to keep a metal bond (non-single or stereo bond, valence, bond table, link to another metal, chelate ring) kept every metal bond in the input, including those in unrelated fragments. Fixing (1) alone would have made a ring in one fragment protect all others. The flag is now per fragment, and the "all metals terminal" exception is checked per fragment. Each fragment now gives the same result as when it is the only one in the input. Before cf3d805 the decision was made per metal.

Results

Input #306 This PR
Ti chelate, water as atom 1 chelate cut chelate kept
NaOMe, then Ti chelate all cut NaOMe split, chelate kept
Ti chelate, then NaOMe all kept NaOMe split, chelate kept
PdCl₂ + NaOMe NaOMe kept NaOMe split, PdCl₂ kept

Regression reference

inchi.sdf reference regenerated; 8 records change, 0 in mcule.sdf. All are multi-fragment test collections where a salt fragment was kept connected only because another fragment in the same record had to be kept:

Record Fragments Now split
salt02 5 NaCl (next to four Fe compounds)
CaPlus 3 Ca⁺ acetate (next to an Sn catecholate)
Example-Feature4 9 Na–O salt (next to Co hydrides)
test3.002 12 three Na carboxylates/phenolates (next to Sn chelates)
_Tech_Man_Table04a 32 Na, Ca, Ba salts and NaCl (next to Fe/Mo compounds)
HillSort 13 AgF (next to Ag₂X clusters)
_Elements_C 106 CH₃K, CH₃Rb, CH₃Cs, CH₃Fr
_Elements_C2 54 CH₃Fr

The complexes in these records stay connected as before. Checked: NaCl, AgF, CH₃Cs and CH₃Fr alone give the same InChI on #306 and here.

Tests

Seven new unit tests in test_molecularInorganics.cpp: TiChelate_water_first, NaOMe_then_TiChelate, TiChelate_then_NaOMe, PdCl2_NaOMe (all fail on #306); OctylTHF_TiOMe4, which guards the ring-ID offset (fails when the offset is removed); TerminalAg2F_TiOMe4, which covers the per-fragment "all metals terminal" exception (fails with an input-wide flag); and ChargedPassSplitsFragments, which checks that fragments are found after the charge-separated pass (fails when they are found before it). Unit, executable, meta, multithreading, regression and invariance tests pass locally.

#306 follow-up merged in

The #306 branch, including its follow-up commits (a CuCl charged/neutral test, and charged non-Group-1/2 metal bonds that the bond table cuts, e.g. Ag(+)-F(-), now rejected with an error), is merged into this branch (merge commit 3b4e116, no rebase). Full test battery rerun after the merge: unit, executable, meta, multithreading, regression (0 diffs) and invariance pass.

MolecularInorganicsPreprocessing perceived ring systems with
MarkRingSystemsInp(at, num_at, 0), which walks only the fragment that
contains atom 0, so the chelate-ring protection never applied to metals
in later fragments and the result depended on atom order (#305).

Ring systems are now perceived in every fragment, with ring-system IDs
kept unique across fragments. The decision tree is evaluated per
fragment: a reason to keep metal bonds in one fragment no longer keeps
the metal bonds of unrelated fragments in the same input, and the
"all metals terminal" exception is checked per fragment. Each fragment
now gives the same result as when it is the only one in the input.
MI also clears the ring-system fields it set before returning.

Fixes #305
…ecisions

Eight multi-fragment records change (CaPlus, Example-Feature4, HillSort,
_Elements_C, _Elements_C2, _Tech_Man_Table04a, salt02, test3.002): salt
fragments that were kept connected only because another fragment in the
same record had to be kept are now split as they are on their own.
@gblanke02

Copy link
Copy Markdown
Contributor

Good analysis and implementation

@gblanke02 gblanke02 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The described modifications should be implemented

… into fix/305-mi-per-fragment

# Conflicts:
#	INCHI-1-TEST/tests/test_unit/test_molecularInorganics.cpp
Comment thread INCHI-1-SRC/INCHI_BASE/src/strutil.c Resolved
MarkRingSystemsInp() allocates work arrays for all atoms on every call,
and it was called once per fragment, including fragments without a metal
and inputs without any metal. Only fragments with a metal are perceived
now; the chelate check compares a metal's ring system with atoms of its
own fragment only, and atoms of skipped fragments keep nRingSystem 0,
which never matches a perceived ID. Output is unchanged (0 regression
diffs). A new test with a Li on the octyl-THF fragment guards the
ring-ID offset, since the metal-free octyl-THF is no longer perceived.

Refs #305
@nnuk
nnuk changed the base branch from fix/mi-charge-separated-group12 to dev November 7, 2026 07:33
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Unit Test Coverage Report

Coverage Regression Summary

Metric PR (%) Base (%) Difference (pp)
Lines 34.3 34.3 +0.00
Branches 22.3 22.3 +0.00

Find details on the base coverage at https://iupac-inchi.github.io/InChI/coverage/index.html

Find details on this PR's coverage by downloading coverage-reports-42351888397 and opening html/index.html

@nnuk
nnuk merged commit 6035c27 into dev Nov 7, 2026
3 checks passed
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.

MI: chelate-ring protection only applies to the component containing atom 0

3 participants