Skip to content

fix(structure): reject atom-to-self bonds in BondList and chem_comp_bond writer#884

Open
haoyu-haoyu wants to merge 1 commit intobiotite-dev:mainfrom
haoyu-haoyu:fix/bondlist-no-self-bond
Open

fix(structure): reject atom-to-self bonds in BondList and chem_comp_bond writer#884
haoyu-haoyu wants to merge 1 commit intobiotite-dev:mainfrom
haoyu-haoyu:fix/bondlist-no-self-bond

Conversation

@haoyu-haoyu
Copy link
Copy Markdown

Fixes #812.

A `BondList` should encode chemistry, so a bond whose two atom indices refer to the same atom is meaningless. Today both the constructor and `add_bond()` accept self-bonds silently.

Changes

  • `BondList.add_bond` raises `ValueError` when the resolved positive indices are equal, including the case where one index is given positive and the other negative (e.g. `4, -1` with `atom_count=5` — both resolve to atom `4`).
  • `BondList.init` performs the same check on the bulk-add path after the per-row sort, so building a `BondList` from a numpy array that contains a self-bond row raises instead of silently storing it.
  • The companion concern from Bond can be created of an atom to itself #812 — that `pdbx.set_structure()` could emit a self-bond into `chem_comp_bond` when two distinct atoms in an `AtomArray` share the same `(res_name, atom_name)` annotations — is also addressed: `_set_intra_residue_bonds()` now filters bond rows where the two `atom_name` annotations are equal before deduplication, and returns `None` if no rows remain.

Tests

  • `test_invalid_creation` extended with two self-bond cases (positive index pair, mixed positive/negative).
  • New `test_add_self_bond_rejected` covers `add_bond()` for both index forms and verifies the list is unchanged after each rejected add.

…ond writer

Fixes biotite-dev#812.

A `BondList` should encode chemistry, so a bond whose two atom indices
refer to the same atom is meaningless and should be rejected at the
construction boundary rather than being stored.

- `BondList.add_bond` raises `ValueError` when the resolved positive
  indices are equal, including the case where one index is given
  positive and the other negative (e.g. `4, -1` with
  `atom_count=5` both resolve to atom `4`).
- `BondList.__init__` performs the same check on the bulk-add path
  after the per-row sort, so building a `BondList` from a numpy array
  that contains a self-bond row now raises rather than silently
  storing it.

The companion concern from biotite-dev#812 — that `pdbx.set_structure()` could
emit a self-bond into `chem_comp_bond` when two distinct atoms in an
`AtomArray` share the same `(res_name, atom_name)` annotations — is
also addressed: `_set_intra_residue_bonds` now filters bond rows where
the two `atom_name` annotations are equal before deduplication, and
returns `None` if no rows remain.

Tests cover both `BondList` construction and `add_bond`, including the
mixed positive/negative index case.

Assisted-by: Claude Code
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.

Bond can be created of an atom to itself

1 participant