Skip to content

Commit

Permalink
Update interactions.py
Browse files Browse the repository at this point in the history
Added bug fix as noted PDBeurope#4
  • Loading branch information
sonnybrilliant authored Sep 22, 2022
1 parent 0d1d14a commit 2c7b461
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions arpeggio/core/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,7 @@ def _extend_atom_properties(self):

atom.is_metal = atom.element.upper() in config.METALS
atom.is_halogen = atom.element.upper() in config.HALOGENS


def _establish_structure_mappping(self):
"""Maps biopython atoms to openbabel ones and vice-versa.
Expand All @@ -1987,23 +1988,34 @@ 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()

logging.debug('Mapped OB to BioPython atoms and vice-versa.')



def _read_in_biopython(self, path):
"""Reads in molecule using Biopython
Expand Down

0 comments on commit 2c7b461

Please sign in to comment.