mirror of
https://github.com/biopython/biopython.git
synced 2025-10-20 13:43:47 +08:00
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:
@ -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
|
||||
|
@ -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."""
|
||||
|
||||
|
Reference in New Issue
Block a user