-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculatCentre_oop.py
executable file
·385 lines (302 loc) · 11.3 KB
/
calculatCentre_oop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
############################################################################
##This is used to find the High density box centre in water distribution.
##We used the water coordidates in the first step generated by 'readDCDFile'
##We used grid method to find the High water occupied boxes and uses optimize
##method to find the high density water place with an r<=1.5 anstrom.
##
##Xianqiang Sun
##TheoChem&Bio
##KTH
##2012-05-14
###########################################################################
import numpy
import sys
sys.path.append('/home/x/xiansu/pfs/program/numpy/lib/python2.6/site-packages')
from Numeric import *
from datetime import datetime
waters=open('distance.txt','r')
##the file were read through the output file of 'read DCD file'
waterCentre=open('waterCentre.txt','w')
##waterCentre2=open('waterCentre2.txt','w')
def formatWaterInfor(self):
waterInfor=[]
for line in self:
line=line.split()
if line[0]=='TIP3':
line[1]=line[1][:-1]
line[-1]=line[-1][1:-2]
elif line[0]=='frame':
frameNo=line[1]
if line[0]=='TIP3':
line.append(str(frameNo))
waterInfor.append(line)
return waterInfor
##formatWaterInfor is used to read the waters information from the input
##file and translate it into list format. The frame No. information were
##also added to the list. the format is [[waterInfor]....]
def getWaterCoord(self,firstFrame,frequency):
waterCoord=[]
for waterAtom in self:
frameNo=waterAtom[-1]
## print frameNo
if int(frameNo)>=firstFrame:
if (int(frameNo)/frequency)-(int(frameNo)/float(frequency))==0:
atomCoord=waterAtom[3:-2]
atomCoordFloat=[]
for coord in atomCoord:
coord=float(coord)
atomCoordFloat.append(coord)
waterCoord.append(atomCoordFloat)
return waterCoord
##getWaterCoord use the output of 'formatWaterInfor' as the input. It will
##read the atom Coordiates and extract them ad the float point numbers. Moreover,
##the firstFrame number and frequency can be seted to save computational time. Each
##coordidates can be save as one element in one list, the format is:[[x1,y1,z2]
##,[x2,y2,z2]...]
def getMinAndMax(self):
minAndMax=[]
xmin=self[0][0]
ymin=self[0][1]
zmin=self[0][2]
xmax=self[0][0]
ymax=self[0][1]
zmax=self[0][2]
for coord in self:
if coord[0]>=xmax:
xmax=coord[0]
if coord[0]<=xmin:
xmin=coord[0]
if coord[1]>=ymax:
ymax=coord[1]
if coord[1]<=ymin:
ymin=coord[1]
if coord[2]>=zmax:
zmax=coord[2]
if coord[2]<=zmin:
zmin=coord[2]
minAndMax.append(xmin)
minAndMax.append(ymin)
minAndMax.append(zmin)
minAndMax.append(xmax)
minAndMax.append(ymax)
minAndMax.append(zmax)
return minAndMax
##getMaxAndMin read a set of coordidates with the format of [[x1,y1,z2]
##,[x2,y2,z2]...](the value should be folat numbers), then get the xmin
##ymin zmin and xmax ymax zmax, the output wer saved as a list with the
##format of[xmin,ymin,zmin,xmax,ymax,zmax]
def divideGrid(self):
boxSize=[]
xmin=self[0]
ymin=self[1]
zmin=self[2]
xmax=self[3]
ymax=self[4]
zmax=self[5]
xboxsize=int((xmax-xmin)/1.6)+1
yboxsize=int((ymax-ymin)/1.6)+1
zboxsize=int((zmax-zmin)/1.6)+1
boxSize.append(xboxsize)
boxSize.append(yboxsize)
boxSize.append(zboxsize)
print xboxsize
return boxSize
##divideGrid read the format of [xmin,ymin,zmin,xmax,ymax,zmax] and devide the
##total box into grid with the wideth of 1.6 anstrom, we set 1.6 as the width
##because the oxygen in water has a distance of 2.8. 1.6*1.7<2.8. Therefore, I
##it. The output is [xboxsize,yboxsize,zboxsize](integar)
def generateGrid(self,boxMin):
xgridSize=self[0]
ygridSize=self[1]
zgridSize=self[2]
xmin=boxMin[0]
ymin=boxMin[1]
zmin=boxMin[2]
totalGrid=[]
for x in range(xgridSize):
minboxx=[]
minboxx.append(xmin)
xmin=xmin+1.6
minboxx.append(xmin)
for y in range(ygridSize):
minboxy=[]
minboxy.append(ymin)
ymin=ymin+1.6
minboxy.append(ymin)
for z in range(zgridSize):
minboxz=[]
minbox=[]
minboxz.append(zmin)
zmin=zmin+1.6
minboxz.append(zmin)
minbox.append(minboxx[0])
minbox.append(minboxy[0])
minbox.append(minboxz[0])
minbox.append(minboxx[1])
minbox.append(minboxy[1])
minbox.append(minboxz[1])
totalGrid.append(minbox)
zmin=boxMin[2]
ymin=boxMin[1]
return totalGrid
##generateGrid is use to generate grid from coordidates space, the input includes:
##1: self [xgridsize,ygridsize,zgridsize],2: boxMin [xmin,ymin,zmin......]
##the out put is the totalGrid for all the coordidates space. the format is:
##[[xmin,ymin,zmin,xmax,ymax,zmax].....[xmin,ymin,zmin,xmax,ymax,zmax]]
def getWaterDistGrid(self,totalGrid):
waterDistGrid=[]
for No in range(len(totalGrid)):
waterDistGrid.append([])
for coord in self:
gridNo=0
for grid in totalGrid:
if grid[3]>coord[0]>=grid[0] and grid[4]>coord[1]>=grid[1] and grid[5]>coord[2]>=grid[2]:
waterDistGrid[gridNo].append(coord)
## print coord,'is in ',grid
## print waterDistGrid[gridNo]
gridNo=gridNo+1
return waterDistGrid
##getWaterDistGrid needs two inputs: 1,self: the coordidats saved as [[x,y,z]....]
##and 2. the total Grid input with the format of [[xmin,ymin,zmin,xmax,ymax,zmax]
##.....[xmin,ymin,zmin,xmax,ymax,zmax]]. Thr output of the function is the the
##distribution of each coordidates according to the grid file. The format of the out
##out looks like[[[x1,y1,z1],[x2,y2,z2]]...].
def countWaterDistGrid(self):
waterCoDist=[]
for eachDis in self:
## print len(eachDis)
waterCoDist.append(len(eachDis))
return waterCoDist
##countWaterNoDistGrid count the waters in each grid. It read the output of getWaterDisGrid.
##the output looks like[No.watersInGrid1,No.WatersInGrid2,....]
def getHighDensityGrid(self,value):
highGrid=[]
for No in range(len(self)):
if self[No]>=value:
## print No
highGrid.append(No)
return highGrid
##getHighDensityGrid read the water count distribution file with format of [No.watersInGrid1,No.
##WatersInGrid2,....] and determine which grid is filled with more waters in the grid. The value
##can be used as the threshold for the determination. the output is a serie of Number represent
##the grid in totalGrid.
def extractHighDensity(self,grids):
highDensity=[]
for number in self:
highDensity.append(grids[number])
return highDensity
##extracHighDensity read the output from getHighDensityGrid which inludes a series of grid numbers.
##The according elment in 'grids' with this number will be extraced to 'HighDensity'. This can be
##used to extract the high density grid from total grid, and extrat according waterCoDist in the grid.
##the output is extracted grid and waterCoDist.
def getCentre(self):
centreAll=[]
for coordSet in self:
coordNo=0
xsum=0
ysum=0
zsum=0
for coord in coordSet:
coordNo+=1
xsum=xsum+coord[0]
ysum=ysum+coord[1]
zsum=zsum+coord[2]
centre=[]
centre.append(xsum/coordNo)
centre.append(ysum/coordNo)
centre.append(zsum/coordNo)
centreAll.append(centre)
return centreAll
##getCentre use a set of coordidate sets to find the centre of these sets. The input can be [[[x,y,z],[x,y,z]..]...]
##the output of this function is centre for each coordidate set [[xcen,ycen,zcen]...]
def determineMerge(self,threShould):
centre2=[]
for i in self:
centre2.append(i)
merge=[]
deleted=[]
for i in range(len(self)):
eachMerge=[]
eachMerge.append(i)
a=numpy.array(self[i])
if i not in deleted:
deleted.append(i)
for j in range(len(centre2)):
if j not in deleted:
b=numpy.array(centre2[j])
dist = numpy.linalg.norm(a-b)
if dist <=threShould:
print self[i],'and',self[j],'has smaller distance, and merge it!!',dist
eachMerge.append(j)
deleted.append(j)
if len(eachMerge)>=2:
merge.append(eachMerge)
return merge
##determingMerge is used to calculate the distance of each high densiy centre to determine which centre should be merged
##together. The input is the highly water occupied centre. the output is the pairs which should be merged.
def recaulcateCentre(self,highWaterCoor,originalCentre):
deleted=[]
newCentre=[]
for merge in self:
mergeCoor=[]
for i in merge:
mergeCoor=mergeCoor+highWaterCoor[i]
deleted.append(i)
xsum=0
ysum=0
zsum=0
coordNo=0
for coord in mergeCoor:
coordNo+=1
xsum=xsum+coord[0]
ysum=ysum+coord[1]
zsum=zsum+coord[2]
centre=[]
centre.append(xsum/coordNo)
centre.append(ysum/coordNo)
centre.append(zsum/coordNo)
newCentre.append(centre)
for i in range(len(originalCentre)):
if i not in deleted:
newCentre.append(originalCentre[i])
return newCentre
def writeCentre(self,outPutFileName):
for i in self:
outPutFileName.write(str(i[0])+' '+str(i[1])+' '+str(i[2])+'\n')
outPutFileName.close()
##This function read a set of coorditates and write it to file. The input is:[[x1,y1,z2]
##,[x2,y2,z2]...],outPutFileName is the output file name.
waterInfor=formatWaterInfor(waters)
print len(waterInfor)
waterCoords=getWaterCoord(waterInfor,0,1)
print len(waterCoords)
minAndMax=getMinAndMax(waterCoords)
print minAndMax
gridSize=divideGrid(minAndMax)
print gridSize
totalGrid=generateGrid(gridSize,minAndMax)
print len(totalGrid)
waterGridDist=getWaterDistGrid(waterCoords,totalGrid)
print len(waterGridDist)
##number=0
##for i in waterGridDist:
## if len(i)>15:
## print len(i)
## number=number+len(i)
##print 'there were', number,'were clustered'
##print waterGridDist
waterCountDist=countWaterDistGrid(waterGridDist)
highDensity=getHighDensityGrid(waterCountDist,1500)
print len(highDensity)
highDensityGrid=extractHighDensity(highDensity,totalGrid)
print highDensityGrid
highDensityWaterGridDist=extractHighDensity(highDensity,waterGridDist)
print len(highDensityWaterGridDist)
highDensityCentre=getCentre(highDensityWaterGridDist)
print highDensityCentre
shortDistance=determineMerge(highDensityCentre,2.4)
print shortDistance
newCentre=recaulcateCentre(shortDistance,highDensityWaterGridDist,highDensityCentre)
print len(newCentre)
print newCentre
writeCentre(newCentre,waterCentre)