Make PDB.Chain.__getitem__ more lenient for indexing with int-like values (#5018)

Includes residue indexing test case for `np.int64` condition
This commit is contained in:
Kâmuran İmran
2025-09-29 18:50:29 +10:00
committed by GitHub
parent 20ad9bea6d
commit e85f1958ee
2 changed files with 32 additions and 0 deletions

View File

@ -94,6 +94,12 @@ class Chain(Entity["Model", "Residue"]):
- id - int, residue resseq
"""
try:
id = int(id)
except ValueError:
pass
except TypeError:
pass
if isinstance(id, int):
id = (" ", id, " ")
return id

View File

@ -520,6 +520,32 @@ class CopyTests(unittest.TestCase):
self.assertIsNot(e.get_list()[0], ee.get_list()[0])
class IndexingTests(unittest.TestCase):
"""Tests for indexing SMCRA objects."""
def setUp(self):
parser = PDBParser(PERMISSIVE=True, QUIET=True)
self.structure = parser.get_structure("a", "PDB/1LCD.pdb")
def test_res_indexing(self):
chain = self.structure[0]["A"] # Get first chain in the first model
all_residues = list(chain.get_residues())
res_positions = [
res.id[1] for res in all_residues if res.id[0] == " "
] # get only standard residues, e.g. (' ', 1, ' ') vs ('W', 56, ' ')
for res in res_positions:
self.assertIn(res, chain) # check if residue is in chain
self.assertIsInstance(res, int) # check if residue is an int
self.assertIn(
np.int64(res), chain
) # check if residue is recognised in chain as an `np.int64`
with self.assertRaises(KeyError):
chain[oct(int(res))] # the conversion should not work for octal strings
class CenterOfMassTests(unittest.TestCase):
"""Tests calculating centers of mass/geometry."""