Skip to content

Bio.PDB.internal_coords.AtomKey.__init__ fails during class construction of Bio.PDB.internal_coords.IC_Chain #3793

Open

Description

@bzoracler

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.79

and 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 found

Steps 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

# 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

  1. rob-miller commented on Nov 18, 2021

    @rob-miller
    Contributor

    Sorry for the delay, and thank you - that is indeed the correct fix, will implement.

  2. added 2 commits that reference this issue on Nov 18, 2021
    1e47f68
    7d67fe3
  3. rob-miller commented on Dec 2, 2021

    @rob-miller
    Contributor

    Submitted #3816 to fix in main branch now without waiting for #3774.

  4. added a commit that references this issue on Dec 4, 2021
    06c7371
  5. added a commit that references this issue on Jan 25, 2022
    15216f6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions