I am currently using the BioPython Bio.PDB
module to parse and read protein structure files (PDB files). I have run into recursion issues when trying to create a deep copy of the structure
object returned by the PDBParser().get_structure(...)
method.
I was under the impression that the copy.deepcopy
function was designed to deal with recursion? Strangely, this is only an issue using Python 3. Running equivalent code in Python 2.7 works fine (uses urllib2
instead of urllib
).
Is this a bug in copy.deepcopy
? Or an issue with Biopython
?
A minimal example (Python 3.6, Biopython 1.68) is:
import urllib.request
from io import StringIO
from copy import deepcopy
import Bio.PDB
pdb_name = '1zro'
#Download file
pdb_url = "http://www.rcsb.org/pdb/files/" + pdb_name + ".pdb"
pdb_file = urllib.request.urlopen(pdb_url)
#Create string stream for Bio.PDB to read:
pdb_input_stream = StringIO(pdb_file.read().decode('utf-8'))
#Make Bio.PDB structure object
parser = Bio.PDB.PDBParser()
structure = parser.get_structure(pdb_name, pdb_input_stream)
#Attempt to perform a deepcopy on Bio.PDB Structure:
structure_copy = deepcopy(structure)
Full traceback:
File "<stdin>", line 2, in <module>
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 281, in _reconstruct
if hasattr(y, '__setstate__'):
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/site-packages/Bio/PDB/Entity.py", line 207, in __getattr__
if not hasattr(self, 'selected_child'):
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/site-packages/Bio/PDB/Entity.py", line 207, in __getattr__
if not hasattr(self, 'selected_child'):
File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/site-packages/Bio/PDB/Entity.py", line 207, in __getattr__
if not hasattr(self, 'selected_child'):
[Previous line repeated 318 more times]
RecursionError: maximum recursion depth exceeded