Equality Between Residues Of Different Structure Instances Of The Same Pdb File
1
1
Entering edit mode
10.5 years ago
comacke ▴ 10

I'm storing a set of residues (from Biopython) in a Python set. What's odd is that when I read the same pdb file into two different structure objects, the corresponding residues are not equal. I checked the output from the __hash__ function for the residue class and that is also producing different output, which causes problems for me when trying to store them in sets or dictionaries.

from Bio.PDB import *

parser = PDBParser(QUIET = True)

structA = parser.get_structure('1AOPA', 'mypath/1AOPA.pdb') 
modelA = structA[0]
resListA = Selection.unfold_entities(modelA, 'R')
resA1 = resListA[0]

structB = parser.get_structure('1AOPA', 'mypath/1AOPA.pdb') 
modelB = structB[0]
resListB = Selection.unfold_entities(modelB, 'R')
resB1 = resListB[0] 

print resA1 
print resB1 # looks exactly the same as resA1 
print resA1 == resB1 # false

I'd like to store certain residues in a set and then in various functions, read the pdb files (from a list passed to the function) into structures one by one and search for the residues in the set each time a structure is created. I'd like to avoid creating lists of structures because there will be thousands of structures. Does anyone know of a work-around for storing residues in sets or dictionaries?

Edit:

So I found this related post on Stack Overflow: http://stackoverflow.com/questions/10802123/implementing-equivalence-in-biopythons-pdb-module

I'd still like to hear if anyone has different thoughts or suggestions on this.

biopython pdb structural • 2.3k views
ADD COMMENT
1
Entering edit mode
10.4 years ago
Peter 6.0k

You could try comparing resA1.get_full_id() and resB1.get_full_id() instead?

Python objects (and their hash) default to comparison as instances - two instances of the same class with identical data are not equal. That is what you are seeing here. If you are interested in modifying this part of Biopython by defining __hash__ and __eq__ comparison methods, please join the development mailing list to talk about it: http://lists.open-bio.org/mailman/listinfo/biopython-dev

ADD COMMENT

Login before adding your answer.

Traffic: 2743 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6