# How to Calculate Centroid in python

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

``````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.

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

``````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
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??????

Asked by: Walter354 | Posted: 30-11-2021

First of all, an easier way to read your file is with numpy's `genfromtxt` function. 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.

``````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.

So, you can average them as such:

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

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

``````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 `math` module and renames it `mean`. What you intended was `from math import mean` which imports the `mean` function from the `math` module. But in your code, you end up using the `math` function from the `numpy` (`nx`) module anyway, so you never used the `math` version.

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:

``````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 `idcord` gets set to an empty list every time 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:

``````from __future__ import division
import numpy as nx

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

Answered by: Emily235 | Posted: 01-01-2022

try this

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

Answered by: Emily905 | Posted: 01-01-2022

# Similar questions

python - What's the best way to calculate a 3D (or n-D) centroid?

As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I'm doing it in a way that seems simple but naive -- by taking the average of each set of points, as in: centroid = average(x), average(y), average(z) where x, y and z are arrays of floating-point numbers. I seem to recall that there is a way to get...

python - What's the best way to calculate a 3D (or n-D) centroid?

As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I'm doing it in a way that seems simple but naive -- by taking the average of each set of points, as in: centroid = average(x), average(y), average(z) where x, y and z are arrays of floating-point numbers. I seem to recall that there is a way to get...

python - What's the best way to calculate a 3D (or n-D) centroid?

As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I'm doing it in a way that seems simple but naive -- by taking the average of each set of points, as in: centroid = average(x), average(y), average(z) where x, y and z are arrays of floating-point numbers. I seem to recall that there is a way to get...

How Python calculate number?

python - Calculate score in a pyramid score system

I am trying to calculate gamescores for a bunch over users and I haven't really got it yet. It is a pyramid game where you can invite people, and the people you invite is placed beneth you in the relations tree. So if i invite X and X invites Y i get kickback from both of them. Let's say 10%^steps... So from X i get 10% of his score and 1% from Y, and X get 10% from Y. So to calculate this i was thi...

How to calculate a mod b in Python?

Is there a modulo function in the Python math library? Isn't 15 % 4, 3? But 15 mod 4 is 1, right?

To calculate the sum of numbers in a list by Python

My data 466.67 465.56 464.44 463.33 462.22 461.11 460.00 458.89 ... I run in Python sum(/tmp/1,0) I get an error. How can you calculate the sum of the values by Python?

python - How to calculate a date back from another date with a given number of work days

I need to calculate date (year, month, day) which is (for example) 18 working days back from another date. It would be enough to eliminate just weekends. Example: I've got a date 2009-08-21 and a number of 18 workdays as a parameter, and correct answer should be 2009-07-27. thanks for any help

python - How to calculate the scrape URL for a torrent

I've read the Bit-torrent specification and done a number of searches, trying to find out how I can get the seeds/peers/downloaded data from a torrent tracker (using Python). I can calculate the info hash from a Torrent no problem, which matches up with the info hash given by various working torrent applications. However, when I try to get the information from the tracker I either timeout (the tracker is working) o...

datetime - How to use Python to calculate time

I want to write python script that acts as a time calculator. For example: Suppose the time is now 13:05:00 I want to add 1 hour, 23 minutes, and 10 seconds to it. and I want to print the answer out. How do I do this in Python? What if date is also involved?

c# - Calculate percent at runtime

I have this problem where I have to "audit" a percent of my transtactions. If percent is 100 I have to audit them all, if is 0 I have to skip them all and if 50% I have to review the half etc. The problem ( or the opportunity ) is that I have to perform the check at runtime. What I tried was: audit = 100/percent So if percent is 50 audit = 100 /...

python - Calculate time between time-1 to time-2?

enter time-1 // eg 01:12 enter time-2 // eg 18:59 calculate: time-1 to time-2 / 12 // i.e time between 01:12 to 18:59 divided by 12 How can it be done in Python. I'm a beginner so I really have no clue where to start. Edited to add: I don't want a timer. Both time-1 and time-2 are entered by the user manually. Thanks in advance for your help.