如何在python中计算质心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18714587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:34:09  来源:igfitidea点击:

How to Calculate Centroid in python

pythonmathnumpyshape

提问by awanit

I'm beginner to python coding. I'm working over structural coordinates. I have pdb structure which have xyz coordinate information (last three col)

我是 python 编码的初学者。我正在研究结构坐标。我有 pdb 结构,其中包含 xyz 坐标信息(最后三个列)

ATOM      1  N   SER A   1      27.130   7.770  34.390    
ATOM      2  1H  SER A   1      27.990   7.760  34.930     
ATOM      3  2H  SER A   1      27.160   6.960  33.790    
ATOM      4  3H  SER A   1      27.170   8.580  33.790    
ATOM      5  CA  SER A   1      25.940   7.780  35.250    
ATOM      6  CB  SER A   1      25.980   9.090  36.020    
ATOM      7  OG  SER A   1      26.740  10.100  35.320    
ATOM      8  HG  SER A   1      26.750  10.940  35.860    
ATOM      9  C   SER A   1      24.640   7.790  34.460    
ATOM     10  O   SER A   1      24.530   8.510  33.500    
ATOM     11  N   CYS A   2      23.590   7.070  34.760    
ATOM     12  H   CYS A   2      23.590   6.550  35.610    
ATOM     13  CA  CYS A   2      22.420   7.010  33.900    
ATOM     14  CB  CYS A   2      21.620   5.760  34.270    
ATOM     15  SG  CYS A   2      22.480   4.210  33.970    
ATOM     16  C   CYS A   2      21.590   8.220  34.040    
ATOM     17  O   CYS A   2      21.370   8.690  35.160   
  • I have 1000 atoms in my structure.
  • I have two queries.
  • 我的结构中有 1000 个原子。
  • 我有两个疑问。

How I can calculate the centroid of the structure from xyz coordinates.
From the centroid I want to draw a sphere of radius 20cm.

我如何从 xyz 坐标计算结构的质心。
从质心我想画一个半径为 20 厘米的球体。

I try this


from __future__ import division
import math as mean
import numpy as nx
from string import*


infile = open('file.pdb', 'r')           #open my file
text1 = infile.read().split('\n')
infile.close()

text = []
for i in text1:
if i != '':
    text.append(i)

for j in text:
x1 = eval(replace(j[30:38], ' ', ''))         #extract x-coordinate
y1 = eval(replace(j[38:46], ' ', ''))         #extract y-coordinate
z1 = eval(replace(j[46:54], ' ', ''))         #extract z-coordinate

idcord = []
idcord.append(x1); idcord.append(y1); idcord.append(z1)

centroid = nx.mean(idcord)
print centroid

it gives the centroid of each atom (xyz) i need a central point how??????

它给出了每个原子的质心(xyz)我需要一个中心点如何??????

回答by lsb123

try this

尝试这个

import numpy as nx
X = nx.rand(10,3)   # generate some number
centroid = nx.mean(X)
print centroid

回答by askewchan

First of all, an easier way to read your file is with numpy's genfromtxtfunction. You don't need to import string, and you don't need to loop through all the lines and append text or count the characters.

首先,读取文件的更简单方法是使用 numpy 的genfromtxt函数。您不需要导入字符串,也不需要遍历所有行并附加文本或计算字符数。

from __future__ import division
import numpy as nx

data = nx.genfromtxt('file.pdb')

Then, the last three columns can be accessed as:

然后,可以通过以下方式访问最后三列:

data[:, -3:]

Where the first :means "all rows", and -3:means from the third-to-last column to the last column.

其中第一个:表示“所有行”,-3:表示从倒数第三列到最后一列。

So, you can average them as such:

因此,您可以将它们平均为:

nx.mean(data[:,-3:], axis=0)

where the axis=0argument tells nx.meanto take the average along the first (0th) axis. It looks like this:

其中axis=0参数告诉nx.mean取平均沿着第一(0th)轴。它看起来像这样:

In : data[:,-3:]
Out: 
array([[ 27.13,   7.77,  34.39],
       [ 27.99,   7.76,  34.93],
       [ 27.16,   6.96,  33.79],
       [ 27.17,   8.58,  33.79],
       [ 25.94,   7.78,  35.25],
       [ 25.98,   9.09,  36.02],
       [ 26.74,  10.1 ,  35.32],
       [ 26.75,  10.94,  35.86],
       [ 24.64,   7.79,  34.46],
       [ 24.53,   8.51,  33.5 ],
       [ 23.59,   7.07,  34.76],
       [ 23.59,   6.55,  35.61],
       [ 22.42,   7.01,  33.9 ],
       [ 21.62,   5.76,  34.27],
       [ 22.48,   4.21,  33.97],
       [ 21.59,   8.22,  34.04],
       [ 21.37,   8.69,  35.16]])

In : np.mean(data[:,-3:], axis=0)
Out: array([ 24.74647059,   7.81117647,  34.64823529])


Some other things:

其他一些事情:

1) remove this line: import math as mean, which imports the entire mathmodule and renames it mean. What you intended was from math import meanwhich imports the meanfunction from the mathmodule. But in your code, you end up using the mathfunction from the numpy(nx) module anyway, so you never used the mathversion.

1) 删除这一行: import math as mean,它导入整个math模块并将其重命名为mean. 你想要的是从模块中from math import mean导入mean函数math。但是在您的代码中,无论如何您最终都会使用( ) 模块中的math函数,因此您从未使用过该版本。numpynxmath

2) your loop is not indented, which means you either pasted incorrectly into StackOverflow, or your loop is incorrectly indented. Possibly, this is what your code actually looks like:

2) 你的循环没有缩进,这意味着你要么错误地粘贴到 StackOverflow 中,要么你的循环缩进不正确。可能,这就是您的代码实际的样子:

for j in text:
    x1 = eval(replace(j[30:38], ' ', ''))         #extract x-coordinate
    y1 = eval(replace(j[38:46], ' ', ''))         #extract y-coordinate
    z1 = eval(replace(j[46:54], ' ', ''))         #extract z-coordinate

    idcord = []
    idcord.append(x1); idcord.append(y1); idcord.append(z1)

    centroid = nx.mean(idcord)
    print centroid

But the problem is that idcordgets set to an empty list everytime the loop goes through, and a new centroid is calculated, for each particle. You don't even need the loop at all if you import the data file all at once as above. In fact, your entire code can be:

但问题是每次循环通过时idcord都会设置为一个空列表,并为每个粒子计算一个新的质心。如果您像上面那样一次性导入数据文件,您甚至根本不需要循环。事实上,你的整个代码可以是:

from __future__ import division
import numpy as nx

data = nx.genfromtxt('file.pdb')
nx.mean(data[:,-3:], axis=0)