Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4: Update _establish_structure_mappping to not fail when serial number 0 is present #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions arpeggio/core/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,18 +1987,28 @@ def _establish_structure_mappping(self):
# FIRST MAP PDB SERIAL NUMBERS TO BIOPYTHON ATOMS FOR SPEED LATER
# THIS AVOIDS LOOPING THROUGH `s_atoms` MANY TIMES
serial_to_bio = {x.serial_number: x for x in self.s_atoms}
for ob_atom in ob.OBMolAtomIter(self.ob_mol):
serial = ob_atom.GetId()

# `Id` IS A UNIQUE AND STABLE ID IN OPENBABEL
# CAN RECOVER THE ATOM WITH `mol.GetAtomById(id)`
serial_to_ob = {x.GetId(): x for x in ob.OBMolAtomIter(self.ob_mol)}

if 0 in serial_to_ob and 0 not in serial_to_bio:
offset = 1
logging.debug('OB atom serial numbers start with 0, but there are no BioPython atoms with serial number 0. Adding 1 to all OB serials for mapping purposes.')
else:
offset = 0

for serial, ob_atom in serial_to_ob.items():
# MATCH TO THE BIOPYTHON ATOM BY SERIAL NUMBER
try:
biopython_atom = serial_to_bio[serial]

biopython_atom = serial_to_bio[serial + offset]
except KeyError:
raise OBBioMatchError(serial)
raise OBBioMatchError("Failed to match OB atom to a BioPython atom: {},{} : {}({})".format(serial, serial + offset, ob_atom.GetType()[:1], ob_atom.GetType()))

# Sanity check that elements match, for debugging
if ob_atom.GetType()[:1] != biopython_atom.element[:1]:
raise OBBioMatchError("Failed to correctly match atoms with different elements: {},{} : {}({}) != {}({}, {})".format(serial, serial + offset, ob_atom.GetType()[:1], ob_atom.GetType(), biopython_atom.element[:1], biopython_atom.element, biopython_atom.name))

# `Id` IS A UNIQUE AND STABLE ID IN OPENBABEL
# CAN RECOVER THE ATOM WITH `mol.GetAtomById(id)`
self.ob_to_bio[ob_atom.GetId()] = biopython_atom
self.bio_to_ob[biopython_atom] = ob_atom.GetId()

Expand Down