分离(X,Y)列表

时间:2013-07-20 00:03:42

标签: python list zip biopython

我正在解析PDB文件,我有一个链名列表以及格式(链,[坐标])的XYZ坐标。我有很多坐标,但只有3个不同的链。我想将同一链中的所有坐标压缩到一个列表中,这样我得到链= [坐标],[坐标],[坐标]等等。我查看了biopython文档,但我很难理解如何获得我想要的坐标,所以我决定手动提取坐标。这是我到目前为止的代码:

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)

和一些输出:

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

我理想的输出是:

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

谢谢!

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是第4列中的链名称。我不知道链名称是先验的,但由于我逐行解析,因此我使用前面提到的格式的坐标来保持链名称。现在我想在它们之前用“A”拉出所有坐标并将它们粘贴在一个名为“A”的列表中。我不能只是硬编码“A”,因为它并不总是“A”。我也有“L”和“H”,但我想,一旦我克服了理解,我就可以得到它们。

3 个答案:

答案 0 :(得分:1)

你想要这样的东西:

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)

,对于您的示例数据,它给出:

>>> 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']]

因为它是字典,显然你可以这样做:

>>> 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']]

获得您感兴趣的链条的xyz坐标。

答案 1 :(得分:1)

只列出元组

>>> 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])

答案 2 :(得分:0)

您可以使用列表理解:

>>> 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]