Description
Setup
I am reporting a problem with Biopython version, Python version, and operating
system as follows:
3.8.10 (default, May 30 2021, 17:33:00)
[GCC 9.3.0]
CPython
Linux-5.13.0-7620-generic-x86_64-with-glibc2.2.5
1.79and with NumPy version 1.21.2.
Expected behaviour
No issues when making an IC_Chain object from a Bio.PDB.Chain.Chain object.
Actual behaviour
Using a Bio.PDB.Chain.Chain object as the constructor argument to IC_Chain fails:
Traceback (most recent call last):
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 1313, in rak
ak = self.akc[atm]
KeyError: <Atom N>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 211, in __init__
self.set_residues(verbose) # no effect if no residues loaded
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 363, in set_residues
if self._add_residue(res, last_res, last_ord_res, verbose):
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 292, in _add_residue
res.internal_coord = IC_Residue(res)
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 1303, in __init__
self._add_atom(atom)
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 1492, in _add_atom
ak = self.rak(atm) # passing Atom here not string
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 1315, in rak
ak = self.akc[atm] = AtomKey(self, atm)
File ".../lib/python3.8/site-packages/Bio/PDB/internal_coords.py", line 3261, in __init__
"".join(filter(None, akl[:2])),
TypeError: sequence item 0: expected str instance, numpy.int64 foundSteps to reproduce
from Bio.PDB import mmtf
from Bio.PDB.internal_coords import IC_Chain
structure = mmtf.MMTFParser.get_structure_from_url("7EJT")
chain = next(structure.get_chains())
ic_chain = IC_Chain(chain)Suggested resolution
The directly offending code is
biopython/Bio/PDB/internal_coords.py
Lines 3227 to 3250 in 8546c96
| # tweak local akl to generate id string | |
| if isinstance(akl[0], int): | |
| akl[0] = str(akl[0]) # numeric residue position to string | |
| # occNdx = AtomKey.fields.occ | |
| # if akl[occNdx] is not None: | |
| # akl[occNdx] = str(akl[occNdx]) # numeric occupancy to string | |
| if self.d2h: | |
| atmNdx = AtomKey.fields.atm | |
| if akl[atmNdx][0] == "D": | |
| akl[atmNdx] = re.sub("D", "H", akl[atmNdx], count=1) | |
| # unused option: | |
| # (self.respos, self.icode, self.resname, self.atm, self.occ, | |
| # self.altloc) = akl | |
| self.id = "_".join( | |
| [ | |
| "".join(filter(None, akl[:2])), | |
| str(akl[2]), # exclude None | |
| "_".join(filter(None, akl[3:])), | |
| ] | |
| ) |
where 3228: if isinstance(akl[0], int): does not account for when akl[0] is a NumPy-type integer,
>>> import numpy
>>> num = numpy.array([0])[0]
>>> isinstance(num, int)
False
>>> type(num)
<class 'numpy.int64'>thereby skipping execution of 3229: akl[0] = str(akl[0]) # numeric residue position to string, and causing 3246: "".join(filter(None, akl[:2])), to fail as it tries to execute str.join on a list containing a non-str item.
The easiest fix, as per https://stackoverflow.com/a/37727662/9407941, is something like changing line 3228 to
3228: if isinstance(akl[0], (int, numpy.integer)):
However, I'm not sure if it is intended that akl[0] is supposed to be a numpy-type integer rather than a builtins.int, so this could be a symptom of another problem.
Activity
rob-miller commentedon Nov 18, 2021
Sorry for the delay, and thank you - that is indeed the correct fix, will implement.
fix biopython#3793 AtomKey init fail with MMTF / np.int
fix biopython#3793 AtomKey init fail with MMTF / np.int
rob-miller commentedon Dec 2, 2021
Submitted #3816 to fix in main branch now without waiting for #3774.
fix biopython#3793 AtomKey init fail with MMTF / np.int
fix biopython#3793 AtomKey init fail with MMTF / np.int