dexterdev
dexterdev

Reputation: 537

Can any one help me understand and solve this error?

I would like to plot a distribution of alpha-cabon to nitrogen bond distances of ubiquitin protein. So I downloaded the 1UBQ.pdb from RCSB website. Now using biopython, I am trying to find the distances between all alpha-cabon(CA) to nitrogen(N) bonds.

I have tried doing a code like below:

import sys
#from Bio.PDB import *
from Bio.PDB.PDBParser import PDBParser 
from numpy import loadtxt

pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb'
sys.stdout = open('file_ubq', 'w')
parser=PDBParser(PERMISSIVE=1)

#file=open('1UBQ.pdb','r')
#header_dict=parse_pdb_header(file)
#file.close()
i=1
structure = parser.get_structure('1UBQ',pdb1)
for model in structure:
    for chain in model:
        for residue in chain:
            for atom in residue:
                model = structure[0] 
                chain = model['A'] 
                residue_1 = chain[i] 
                atom_1 = residue_1['N'] 
                atom_2 = residue_1['CA'] 
                distance = atom_1-atom_2 
                #print atom.get_vector(),atom.name,distance
                print distance
                i=i+1

#lines = loadtxt("file_ubq")

And I got the output in the file 'file_ubq' like below:

1.47369
1.4966
1.47547
1.51187
1.44885
1.50423
1.47052
1.48006
1.50265
.
.
.
.
1.48417
1.47194
1.45661
1.47023

But alongside this, I got error too like as below:

    return self.child_dict[id]
KeyError: (' ', 77, ' ')

----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ... 
EXECUTING:
/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py 
----------------------------------------------
Traceback (most recent call last):
  File "/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py", line 38, in <module>
    residue_1 = chain[i] 
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Chain.py", line 67, in __getitem__
    return Entity.__getitem__(self, id)
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Entity.py", line 38, in __getitem__
    return self.child_dict[id]
KeyError: (' ', 77, ' ')

----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ... 

EXECUTING:
/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py 
----------------------------------------------
Traceback (most recent call last):
  File "/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py", line 38, in <module>
    residue_1 = chain[i] 
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Chain.py", line 67, in __getitem__
    return Entity.__getitem__(self, id)
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Entity.py", line 38, in __getitem__
    return self.child_dict[id]
KeyError: (' ', 77, ' ')

----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ... 
^CEXECUTING:
/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py 
----------------------------------------------
Traceback (most recent call last):
  File "/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py", line 38, in <module>
    residue_1 = chain[i] 
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Chain.py", line 67, in __getitem__
    return Entity.__getitem__(self, id)
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Entity.py", line 38, in __getitem__
    return self.child_dict[id]
KeyError: (' ', 77, ' ')

----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ... 
EXECUTING:
/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py 
----------------------------------------------
Traceback (most recent call last):
  File "/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py", line 38, in <module>
    residue_1 = chain[i] 
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Chain.py", line 67, in __getitem__
    return Entity.__getitem__(self, id)
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Entity.py", line 38, in __getitem__
    return self.child_dict[id]
KeyError: (' ', 77, ' ')

----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ... 

EXECUTING:
/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py 
----------------------------------------------
Traceback (most recent call last):
  File "/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py", line 38, in <module>
    residue_1 = chain[i] 
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Chain.py", line 67, in __getitem__
    return Entity.__getitem__(self, id)
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Entity.py", line 38, in __getitem__
    return self.child_dict[id]
KeyError: (' ', 77, ' ')

----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ... 

EXECUTING:
/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py 
----------------------------------------------
Traceback (most recent call last):
  File "/home/devanandt/Documents/PYTHON/ubq/src/ubq_dist_pdf.py", line 38, in <module>
    residue_1 = chain[i] 
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Chain.py", line 67, in __getitem__
    return Entity.__getitem__(self, id)
  File "/usr/lib/pymodules/python2.7/Bio/PDB/Entity.py", line 38, in __getitem__
    return self.child_dict[id]
KeyError: (' ', 77, ' ')

----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ... 

Can any one help me understand and solve this error?

Upvotes: 0

Views: 1971

Answers (3)

Heena Shaikh
Heena Shaikh

Reputation: 1

import sys
#from Bio.PDB import *
from Bio.PDB.PDBParser import PDBParser 
from numpy import loadtxt

pdb1 ='1UBQ.pdb'
sys.stdout = open('file_ubq', 'w')
parser=PDBParser(PERMISSIVE=1)

#file=open('1UBQ.pdb','r')
#header_dict=parse_pdb_header(file)
#file.close()
i=1
structure = parser.get_structure('1UBQ',pdb1)
for model in structure:
    for chain in model:
        for residue in chain:
            i = 1
            for atom in residue:
                model = structure[0] 
                chain = model['A'] 
                residue_1 = chain[i] 
                atom_1 = residue_1['N'] 
                atom_2 = residue_1['CA'] 
                distance = atom_1-atom_2 
                #print atom.get_vector(),atom.name,distance
                print distance
                i=i+1

#lines = loadtxt("file_ubq")

This might help you 

Upvotes: -1

joaor
joaor

Reputation: 41

There are several issues with your code.

  1. You are overloading sys.stdout with a file handle so you can write to the file. You can simply do:
f_handle = open('random.pdb', 'w')
print >>f_handle, "This will be written to the file, followed by a newline character"
  1. Your access to the Structure object and respective children (Chain, Residue, etc) could be done better, read a bit the FAQ to get to know better the library. You probably reach the limit of the chain (76 residues) and try to look for the 77th residue since you are using a counter to access the residues. Biopython has iterators implemented in the Structure, Chain, etc objects so you can just say 'for x in object' and go over its children. To print distances between N and Ca atoms in each chain you do:
parser = PDBParser()
structure = parser.get_structure('randomPDB', 'randomPDB.pdb')
for model in structure:
    for chain in model:
        for residue in chain:
            if residue.id[1] == ' ': # filters heteroatoms
                resid = residue.id[0]
                n_ca_dist = residue['N'] - residue['CA']
                print >>f_handle, "Distance N-Ca bond of residue {0}: {1}".format(resid, n_ca_dist)

Upvotes: 3

matsjoyce
matsjoyce

Reputation: 5844

OK, this code fixes the error:

import sys
#from Bio.PDB import *
from Bio.PDB.PDBParser import PDBParser 
from numpy import loadtxt

pdb1 ='1UBQ.pdb'
sys.stdout = open('file_ubq', 'w')
parser=PDBParser(PERMISSIVE=1)

#file=open('1UBQ.pdb','r')
#header_dict=parse_pdb_header(file)
#file.close()
i=1
structure = parser.get_structure('1UBQ',pdb1)
for model in structure:
    for chain in model:
        for residue in chain:
            i = 1
            for atom in residue:
                model = structure[0] 
                chain = model['A'] 
                residue_1 = chain[i] 
                atom_1 = residue_1['N'] 
                atom_2 = residue_1['CA'] 
                distance = atom_1-atom_2 
                #print atom.get_vector(),atom.name,distance
                print distance
                i=i+1

#lines = loadtxt("file_ubq")

However, it seems to contain lots of duplicate results, and may not cover the full chain. This code should not contain duplicates:

import sys
from Bio.PDB.PDBParser import PDBParser 
from numpy import loadtxt

pdb1 ='1UBQ.pdb'
sys.stdout = open('file_ubq', 'w')
parser=PDBParser(PERMISSIVE=1)

structure = parser.get_structure('1UBQ',pdb1)
for model in structure:
    for chain in model:
        for residue in chain:
            try:
                atom_1 = residue['N'] 
                atom_2 = residue['CA'] 
                distance = atom_1-atom_2 
                print distance
            except:
                pass

It also produces just 76 results, which is the length of the chain. Tell me if there is anything wrong, as I haven't used biopython before.

Upvotes: 1

Related Questions