-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimizeCentre_oop.py
executable file
·289 lines (218 loc) · 9.32 KB
/
optimizeCentre_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
############################################################################
##This is used to find the waters around the high density centre. This script
##use two outputs, which were generated by reasDCD File.py and calculatCentre
##_oop.py. We used several steps to optimize the founded coordidates centre
##and the detected centre to make them consistent with each other.
##The outputfile is a set of water coordidates and according centre index.
##Moreover, the waterInformation arround the centre were write to an outputFile.
##
##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')
centreFile=open('waterCentre.txt','r')
optimizedCentreOutput=open('optimizedCentre.txt','w')
waterInforByCentreOutput=open('waterInforByCentre.txt','w')
waterInforByCentreOutput.write('resname atomid resnumber X Y Z segname frameNo centreNo'+ '\n')
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 getCentreCoord(self):
centreCoord=[]
for centre in self:
centre=centre.split()
centreFloat=[]
for coord in centre:
coord =float(coord)
centreFloat.append(coord)
centreCoord.append(centreFloat)
return centreCoord
##getCentreCoord read the output file from the 'calculatCentre_oop.py'. Read the
##coorditates and save them as list with the float format. The output format is
##the format is:[[x1,y1,z2],[x2,y2,z2]...]
def compareCoord(self,waterCoord,threshould):
waterByCentre=[]
for centre in range(len(self)):
a=numpy.array(self[centre])
waterByEachCentre=[]
for i in range(len(waterCoord)):
b=numpy.array(waterCoord[i])
dist = numpy.linalg.norm(a-b)
if dist<=threshould:
waterByEachCentre.append(waterCoord[i])
waterByCentre.append(waterByEachCentre)
return waterByCentre
##compareCoord is used to compare two sets of coordidates with certain threshould.
##the output is a set of coorditates set with ranked by each coordidate in self.
##the format of output is [[[x1,y1,z1]...],[...]].
def compareOneCoorWithSet(self,waterCoord,threshould):
a=numpy.array(self)
waterByEachCentre=[]
for i in range(len(waterCoord)):
b=numpy.array(waterCoord[i])
dist = numpy.linalg.norm(a-b)
if dist<=threshould:
waterByEachCentre.append(waterCoord[i])
return waterByEachCentre
##compareCoord is used to compare one coordidate with a set of coordidates with certain threshould.
##the output is a set of coorditates. The format of output is [[x1,y1,z1]...].
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 getOneCentre(self):
xsum=0
ysum=0
zsum=0
coordNo=0
for coord in self:
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)
return centre
##getCentre use a set of coordidates to find the centre of these coordidates. The input can be [[x,y,z],[x,y,z]..]
##the output of this function is centre for these coordidates [xcen,ycen,zcen]
def optimizeCentre(self,oldCentre,waterCoord,threshould):
for i in range(len(self)):
a=numpy.array(self[i])
b=numpy.array(oldCentre[i])
dist=numpy.linalg.norm(a-b)
if dist>=0.1:
oldCentre[i]=self[i]
for number in range(100):
if dist>=0.01:
print dist
print 'optimizing centre ',oldCentre[i],' No ', i
## oldCentre[i]=self[i]
c=numpy.array(oldCentre[i])
optimizedWater=compareOneCoorWithSet(oldCentre[i],waterCoord,threshould)
optimizedCentre=getOneCentre(optimizedWater)
oldCentre[i]=optimizedCentre
d=numpy.array(optimizedCentre)
dist=numpy.linalg.norm(c-d)
else:
print 'the distance is', dist
break
return oldCentre
##optimize caompare the data in two list of coordidats(newcentre generated by getCentre, oldcenter by
##compareCentre, and waterfoord for all the water coordidated. the threshould were used in 'compraeOneCoorWithSet'
##One should mention that this function use two other function. therefore, the ranking of this function should be
##take care). At last we obtained several coordidates with optimized centre. Nevermind about the name oldCentre
##at last return. I have updated it.
def getWaterInforFromCentre(self,waterCoord,waterInfor,threshould):
waterByCentre=[]
waterInforByCentre=[]
for centre in range(len(self)):
a=numpy.array(self[centre])
waterByEachCentre=[]
waterInforByEachCentre=[]
for i in range(len(waterCoord)):
b=numpy.array(waterCoord[i])
dist = numpy.linalg.norm(a-b)
if dist<=threshould:
waterByEachCentre.append(waterCoord[i])
waterInfor[i].append(centre)
waterInforByEachCentre.append(waterInfor[i])
waterInforByCentre.append(waterInforByEachCentre)
waterByCentre.append(waterByEachCentre)
return waterInforByCentre
##getWaterInforFromCentre is used to compare each coordidates in self and waterCoord with certain threshould. Then
##water information will be obtianed with waterInfor. The waterCoord should always matched with waterinfor.
##.in with certain threshould.
##the output is a set of water Information set according to self.
##the format of output is [[[x1,y1,z1]...],[...]].
def writeListSets(self,outputFileName):
for i in self:
## eachWater=''
for j in i:
eachWater=''
for item in j:
eachWater=eachWater+str(item)+' '
print eachWater
eachWater=eachWater+'\n'
outputFileName.write(eachWater)
outputFileName.close()
##writeListSets use a set of water information list and write it to file. The input of the file looks like
##[[[waterInfor],...]...].
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,1,1)
print len(waterCoords)
waterCentre=getCentreCoord(centreFile)
print len(waterCentre)
waterAroundCentre=compareCoord(waterCentre,waterCoords,1.2)
print len(waterAroundCentre)
newWaterCentre=getCentre(waterAroundCentre)
print len(newWaterCentre)
optimizedCentre=optimizeCentre(newWaterCentre,waterCentre,waterCoords,1.2)
print len(optimizedCentre)
waterInforFromCentre=getWaterInforFromCentre(optimizedCentre,waterCoords,waterInfor,1.2)
print len(waterInforFromCentre)
writeListSets(waterInforFromCentre,waterInforByCentreOutput)
writeCentre(optimizedCentre,optimizedCentreOutput)