Question

I am parsing PDB files and I have a list of chain names along with XYZ coordinates in the format (chain,[coordinate]). I have many coordinates, but only 3 different chains. I would like to condense all of the coordinates from the same chain to be in one list so that i get chain = [coordinate], [coordinate],[coordinate] and so on. I looked at the biopython documentation, but I had a hard time understanding exactly how to get the coordinates I wanted, so I decided to extract the coordinates manually. This is the code I have so far:

pdb_file = open('1adq.pdb')
import numpy as np

chainids = []
chainpos= []

for line in pdb_file:
    if line.startswith("ATOM"):
        # get x, y, z coordinates for Cas
        chainid =str((line[20:22].strip()))
        atomid = str((line[16:20].strip()))
        pdbresn= int(line[23:26].strip())
        x = float(line[30:38].strip())
        y = float(line[38:46].strip())
        z = float(line[46:54].strip())
        if line[12:16].strip() == "CA":
            chainpos.append((chainid,[x, y, z]))
            chainids.append(chainid)

allchainids = np.unique(chainids)
print(chainpos)

and some output:

[('A', [1.719, -25.217, 8.694]), ('A', [2.934, -21.997, 7.084]), ('A', [5.35, -19.779,     8.986])

My ideal output would be:

A = ([1.719, -25.217, 8.694]), ([2.934, -21.997, 7.084]),(5.35, -19.779,8.986])...

Thanks!

Here is a section of PDB file:
ATOM      1  N   PRO A 238       1.285 -26.367   7.882  0.00 25.30           N  
ATOM      2  CA  PRO A 238       1.719 -25.217   8.694  0.00 25.30           C  
ATOM      3  C   PRO A 238       2.599 -24.279   7.885  0.00 25.30           C  
ATOM      4  O   PRO A 238       3.573 -24.716   7.275  0.00 25.30           O  
ATOM      5  CB  PRO A 238       2.469 -25.791   9.881  0.00 25.30           C  

A is the chain name there in column 4. I do not know what the chain name is a priori but since I am parsing line by line, I am sticking the chain name with the coordinates in the format I mentioned earlier. Now I would like to pull all of the coordinates with an "A" before them and stick them in one list called "A". I can't just hard code in an "A" because it is not always "A". I also have "L" and "H", but I think I can get them once I get over the hump of understanding..

Was it helpful?

Solution

just make a list of tuple

>>> chainpos.append((chainid,x, y, z))
>>> chainpos 
[('A', 1.719, -25.217, 8.694), ('A', 2.934, -21.997, 7.084)]
>>> import itertools
>>> for id, coor in itertools.groupby(chainpos,lambda x:x[0]):
...     print(id, [c[1:] for c in coor])

OTHER TIPS

Do you want something like:

import numpy as np

chain_dict = {}

for line in open('input'):
    if line.startswith("ATOM"):
        line = line.split()
        # get x, y, z coordinates for Cas
        chainid = line[4]
        atomid = line[2]
        pdbresn= line[5]
        xyz = [line[6],line[7],line[8]]
        if chainid not in chain_dict:
            chain_dict[chainid]=[xyz]
        else:
            chain_dict[chainid].append(xyz)

which, for your example data, gives:

>>> chain_dict
{'A': [['1.285', '-26.367', '7.882'], ['1.719', '-25.217', '8.694'], ['2.599', '-24.279', '7.885'], ['3.573', '-24.716', '7.275'], ['2.469', '-25.791', '9.881']]

and since it's a dictionary, obviously you can do:

>>> chain_dict['A']
[['1.285', '-26.367', '7.882'], ['1.719', '-25.217', '8.694'], ['2.599', '-24.279', '7.885'], ['3.573', '-24.716', '7.275'], ['2.469', '-25.791', '9.881']]

to get just the xyz coords of the chain you're interested in.

You can use a list comprehension:

>>> print chainpos 
[('A', [1.719, -25.217, 8.694]), ('A', [2.934, -21.997, 7.084]), ('A', [5.35, -19.779,     8.986])]
>>> print "A =", [ t[1] for t in chainpos]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top