从pdb文件python中提取和绘制原子坐标

时间:2016-12-19 17:54:53

标签: regex python-3.x matplotlib

我试图仅提取alpha碳坐标并以3D表示绘制它们。以下代码的上半部分工作正常,但我似乎无法绘制我的结果。

$films = Film::with(['actors' => function($q) {
                $q->select('name');
            }])
            ->whereMonth('created_at', Carbon::now()->month)
            ->orderBy('created_at', 'desc')
            ->get();

foreach($films as $film) {
    $film_name = $film->name;
    $actors_arr = $film->actors->toArray();
}

运行上述内容的结果看起来像......

import re
import glob
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt


coord = []
pattern = re.compile('ATOM\s{5,}\d+\s{2}CA\s{2,}\w{3}\s\w\s{2,}\d+\s{6}\d+\.\d+\s\d+\.\d+\s{2}\d+\.\d+', flags=re.S)
for file in glob.glob('file_rank_1.pdb'):
with open(file) as fp:
    for result in pattern.findall(fp.read()):
        output = result[-22:]
        coord = " ".join(output.split())
        coord = coord.replace(" ",",")
        c = coord.split(',')
        print(c)
X,Y,Z = (c)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X,Y,Z)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.show()

上面的代码打开了图形界面,但它仍然是空白的。还有一个来自交互式shell的全屏红色文件消息,我在这个问题中试图节省空间。 如何绘制c中的数字?感谢

1 个答案:

答案 0 :(得分:0)

有几点需要指出:

1)在下面的块中,c是一个不浮动的字符串列表。

with open(file) as fp:
    for result in pattern.findall(fp.read()):
        output = result[-22:]
        coord = " ".join(output.split())
        coord = coord.replace(" ",",")
        c = coord.split(',')
        print(c)

您可以使用以下方式更改它们:

[float(i) for i in c]

2)当你设置X,Y,Z = (c)时,c只是循环中的最后一项。因此,您应该在循环中append每个c收集所有坐标。

3)您可能希望使用numpy进行数组操作。

希望以下内容有效:

import re
import numpy as np
import glob
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt


points = []
pattern = re.compile('ATOM\s{5,}\d+\s{2}CA\s{2,}\w{3}\s\w\s{2,}\d+\s{6}\d+\.\d+\s\d+\.\d+\s{2}\d+\.\d+', flags=re.S)
for file in glob.glob('file_rank_1.pdb'):
with open(file) as fp:
    for result in pattern.findall(fp.read()):
        output = result[-22:]
        coord = " ".join(output.split())
        coord = coord.replace(" ",",")
        c = coord.split(',')
        c = [float(i) for i in c] # change them to float
        points.append(c)
        print(c)

X,Y,Z=np.array(points).T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X,Y,Z)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.show()