How to find a centre in the structure. python code

1.9k views Asked by At

I'm beginner to python coding.

I'm working over structural coordinates.

I have pdb structure of 1000 atoms which have xyz coordinate information.

My structure can have any shape.

I am struggling to find the center point inside the structure. From the central point I want to draw a sphere of radius 20cm.

                                 X        Y      Z
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 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)

cenpoint = nx.mean(idcord)   # center point
print cenpoint
2

There are 2 answers

0
asafpr On

You would probably want to create a list of values for each axis and then get the mean of each such list.
Let's start with reading the file, the way to read a file is as follows:

xval = []
yval = []
zval = []
with open('file.pdb') as fin:
    for line in fin:
        if line.startswith('ATOM'):
            line_vals = line.strip().split()
            xval.append(line_vals[6])
            yval.append(line_vals[7])
            zval.append(line_vals[8])

Then go on and compute the mean of each list using math.mean

0
Sven Marnach On

There are two steps here. One is to parse the input file, which can be easily done using numpy.gefromtxt():

a = numpy.genfromtxt("file.pdb", skip_header=1, usecols=[6, 7, 8])

The second step is to average the array along it's first dimension. Use the axis=0 argument to the mean() method:

>>> a.mean(axis=0)
array([ 24.74647059,   7.81117647,  34.64823529])