Skip to content

Commit 2b59dd4

Browse files
Molecule.to_rdkit(): support residue number strings that aren't decimal ints
1 parent 3dc07df commit 2b59dd4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

openff/toolkit/_tests/test_toolkits.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,6 +3508,26 @@ def test_to_rdkit_losing_aromaticity_(self):
35083508
for offatom, rdatom in zip(mol.atoms, rdmol.GetAtoms()):
35093509
assert offatom.is_aromatic is rdatom.GetIsAromatic()
35103510

3511+
def test_to_rdkit_str_resnum(self):
3512+
smiles = ("O")
3513+
3514+
mol = Molecule.from_smiles(smiles)
3515+
3516+
# Test an int, a string that can convert to an int, and a non-intable str
3517+
atom_resnums = [9998, "9999", "A000"]
3518+
# RDKit's default residue number is 0
3519+
expected_atom_resnums = [9998, 9999, 0]
3520+
3521+
for atom, resnum in zip(mol.atoms, atom_resnums):
3522+
atom.metadata["residue_number"] = resnum
3523+
atom.metadata["residue_name"] = "HOH"
3524+
3525+
rdmol = mol.to_rdkit()
3526+
3527+
# now make sure the residue number matches for each atom
3528+
for resnum, rdatom in zip(expected_atom_resnums, rdmol.GetAtoms()):
3529+
assert rdatom.GetPDBResidueInfo().GetResidueNumber() == resnum
3530+
35113531
@pytest.mark.slow
35123532
def test_max_substructure_matches_can_handle_large_molecule(self):
35133533
"""Test RDKitToolkitWrapper substructure search handles more than the default of maxMatches = 1000

openff/toolkit/utils/rdkit_wrapper.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,8 +2716,14 @@ def to_rdkit(
27162716
res.SetResidueName(atom.metadata["residue_name"])
27172717

27182718
if "residue_number" in atom.metadata:
2719-
atom_has_any_metadata = True
2720-
res.SetResidueNumber(int(atom.metadata["residue_number"]))
2719+
try:
2720+
residue_number_int = int(atom.metadata["residue_number"])
2721+
except ValueError:
2722+
# Residue number is a string that could not be converted to int
2723+
pass
2724+
else:
2725+
atom_has_any_metadata = True
2726+
res.SetResidueNumber(residue_number_int)
27212727

27222728
if "insertion_code" in atom.metadata:
27232729
atom_has_any_metadata = True

0 commit comments

Comments
 (0)