Skip to content

Commit 35e4334

Browse files
authored
Fix bond guessing with mixed-case atomtypes (#5343)
Guessing with atom types that were mixed case ('Au' vs 'AU') errored. Atomtypes are now converted to uppercase during comparison with the vdwradii table which is all uppercase.
1 parent d150bfe commit 35e4334

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

package/CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The rules for this file:
2222
* 2.11.0
2323

2424
Fixes
25+
* Fix mixed-case atom types in guess_bonds (Issue #5342, PR #5343)
2526
* Fixes msd for non-linear frames, when non_linear is not explicitly
2627
provided (Issue #5100, PR #5254)
2728
* Fixes TypeError with np.int64 indexing in GSD Reader (Issue #5224)

package/MDAnalysis/guesser/default_guesser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ def guess_bonds(self, atoms=None, coords=None):
433433
else:
434434
atomtypes = self.guess_types(atom_types=atoms.names)
435435

436+
# vdwradii keys are uppercase, so normalize atomtypes to match
437+
atomtypes = np.char.upper(np.asarray(atomtypes, dtype=str))
438+
436439
# check that all types have a defined vdw
437440
if not all(val in vdwradii for val in set(atomtypes)):
438441
raise ValueError(

testsuite/MDAnalysisTests/guesser/test_default_guesser.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ def test_guess_bonds_peptide():
280280
assert_equal(np.sort(u.bonds.indices, axis=0), np.sort(bonds, axis=0))
281281

282282

283+
def test_guess_bonds_mixed_case_types():
284+
"""Test that the guesser can handle mixed-case types"""
285+
n_atoms = 2
286+
# Actual positions don't matter for this test
287+
positions = np.array([[0.0, 0.0, 0.0], [2.5, 0.0, 0.0]])
288+
# Use mixed-case types
289+
topology = Topology(n_atoms, attrs=[Atomtypes(["Au", "h"])])
290+
u = mda.Universe(topology)
291+
guesser = DefaultGuesser(None)
292+
# This should not raise an error due to the mixed-case types
293+
bonds = guesser.guess_bonds(u.atoms, positions)
294+
295+
283296
@pytest.mark.parametrize(
284297
"smi",
285298
[

0 commit comments

Comments
 (0)