-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPaCS-MD-dissociation.py
326 lines (288 loc) · 14 KB
/
PaCS-MD-dissociation.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
#!/usr/bin/python
from __future__ import print_function
import sys, math, numpy, os
from subprocess import call
#################################################
# PaCS MD for GROMACS
# Number of data should to be the same with
# number of frames
# DATE updated: Apr 7th 2015
# Apr. 8 2015: change for loop for m to for m in range(1,nbin+1)
# Apr. 24 2015: adding if there is itp file, copy that file to working directory
# May 18 2015: adding restart to code
# June 16 2015: adding cutoff stop for distance in z direction
#################################################
nbin=30
nround=100
rest=-1 #set this < 0 to begin the new PaCSMD simulation
restep=2
nroundadd=100
comdistmax=7.0
groupA="Protein"
groupB="TNG"
grofn="input.gro"
mdfn="mdpacs.mdp"
mdinitfn="md.mdp"
vfn=""
ndxfn="index.ndx"
topolfn="topol.top"
itpfn="*.itp"
comdistfn="comdist"
outfnpf="pacs"
logfn="pacs.log"
ndxfn="index.ndx"
gmxcmd="mpirun -np 1 gmx " #gmx serial calling
gmxcmd2="mpirun -np 1 gmx "#call MPI task in this variable
#use negative number for system choice of MD running conf.
gpu=-1
gpuid="0011"
ntomp=4
wdir=os.getcwd()
outfn=wdir+"/"+outfnpf
#################################################
# Subroutine is below:
#################################################
def GROcheck():
os.system(gmxcmd+" --version | grep 'GROMACS version' > gmxversion ")
f=open("gmxversion","r")
ln=f.readlines()
vers=ln[0].split()[-1].split(".")[0]
return float(vers)
def checkcycle():
n=1
chkfile=True
while chkfile:
print("Currently check cycle "+str(n)+"!")
failcyc=0
for cnt in range(1,nbin+1):
if not(os.path.exists(outfn+"-"+str(n)+"-"+str(cnt)+"/"+outfnpf+"-"+str(n)+"-"+str(cnt)+".tpr")):
chkfile=False
failcyc=cnt
break
if not(os.path.exists(outfn+"-"+str(n)+"-"+str(cnt)+"/"+outfnpf+"-"+str(n)+"-"+str(cnt)+".gro")):
chkfile=False
failcyc=cnt
break
if not(os.path.exists(outfn+"-"+str(n)+"-"+str(cnt)+"/"+outfnpf+"-"+str(n)+"-"+str(cnt)+".xtc")):
chkfile=False
failcyc=cnt
break
if not(os.path.exists(outfn+"-"+str(n)+"-"+str(cnt)+"/"+outfnpf+"-"+str(n)+"-"+str(cnt)+".xvg")):
chkfile=False
failcyc=cnt
break
if not(os.path.exists(outfn+"-"+str(n)+"-"+str(cnt)+"/input.gro")):
chkfile=False
failcyc=cnt
break
if not(os.path.exists(outfn+"-"+str(n)+"-"+str(cnt)+"/"+outfnpf+"-"+str(n)+"-"+str(cnt)+".edr")):
chkfile=False
failcyc=cnt
break
if not(os.path.exists(outfn+"-"+str(n)+"-"+str(cnt)+"/"+outfnpf+"-"+str(n)+"-"+str(cnt)+".log")):
chkfile=False
failcyc=cnt
break
n=n+1
print("Simulation was terminated at CYCLE "+str(n-1)+" and REPLICA "+str(cnt)+". Simulation will restart at this cycle.")
return n-1
#################################################
# Main program is below:
#################################################
#check version of GROMACS
vers=GROcheck()
#write selection file
f=open(wdir+"/sel.dat","w")
f.write('com of group "'+groupA+'" plus com of group "'+groupB+'"')
f.close()
if rest<0:
#save the old log file
if os.path.exists(wdir+"/"+logfn):
i=0
while(os.path.exists(wdir+"/"+logfn+"."+str(i))):
i=i+1
os.system("mv "+wdir+"/"+logfn+" "+wdir+"/"+logfn+"."+str(i))
os.system("echo 'PaCS MD is going to run ' > "+wdir+"/"+logfn)
print("PaCS MD is going to run \n")
os.system("echo 'Number of BIN is "+str(nbin)+" ' >> "+wdir+"/"+logfn)
print("echo 'Number of BIN is "+str(nbin)+"\n")
os.system("echo 'Number of ROUND is "+str(nround)+" ' >> "+wdir+"/"+logfn)
print("echo 'Number of ROUND is "+str(nround)+"\n")
os.system("echo 'Simulation is running from the beginning. ' >> "+wdir+"/"+logfn)
print("Simulation is running from the beginning. \n")
#call gromacs to run for the first cycle
os.system("echo '++++++++++++++++' >> "+wdir+"/"+logfn)
print('++++++++++++++++\n')
os.system("echo 'ROUND 0. ' >> "+wdir+"/"+logfn)
print('ROUND 0. \n')
os.system("echo '1111 Max to Min Ranking 1111' >> "+wdir+"/"+logfn)
print('+ Ranking +\n' )
os.system("echo '+ Bin +++ Step +' >> "+wdir+"/"+logfn)
print('+ Bin +++ Step +\n')
os.system("mkdir "+outfn+"-0-0")
#copy needed file for MD run
os.system("cp -r "+wdir+"/"+mdinitfn+" "+outfn+"-0-0/")
os.system("cp -r "+wdir+"/"+topolfn+" "+outfn+"-0-0/")
if len(itpfn)>0:
os.system("cp -r "+wdir+"/"+itpfn+" "+outfn+"-0-0/")
os.system("cp -r "+wdir+"/"+grofn+" "+outfn+"-0-0/")
if len(vfn)>0:
os.system("cp -r "+wdir+"/"+vfn+" "+outfn+"-0-0/")
if vers<=2016:
os.system(gmxcmd+" grompp -f "+outfn+"-0-0/"+mdinitfn+" -c "+outfn+"-0-0/"+grofn+" -t "+outfn+"-0-0/"+vfn+" -p "+outfn+"-0-0/"+topolfn+" -o "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -maxwarn 10")
else:
os.system(gmxcmd+" grompp -f "+outfn+"-0-0/"+mdinitfn+" -c "+outfn+"-0-0/"+grofn+" -t "+outfn+"-0-0/"+vfn+" -p "+outfn+"-0-0/"+topolfn+" -o "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -r "+grofn+" -maxwarn 10")
else:
if vers<=2016:
os.system(gmxcmd+" grompp -f "+outfn+"-0-0/"+mdinitfn+" -c "+outfn+"-0-0/"+grofn+" -p "+outfn+"-0-0/"+topolfn+" -o "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -maxwarn 10")
else:
os.system(gmxcmd+" grompp -f "+outfn+"-0-0/"+mdinitfn+" -c "+outfn+"-0-0/"+grofn+" -p "+outfn+"-0-0/"+topolfn+" -o "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -r "+grofn+" -maxwarn 10")
print("################################")
if gpu>=0 and ntomp>0:
os.system(gmxcmd2+" mdrun -deffnm "+outfn+"-0-0/"+outfnpf+"-0-0 -v -ntomp "+str(ntomp)+" -gpu_id "+str(gpuid))
else:
os.system(gmxcmd2+" mdrun -deffnm "+outfn+"-0-0/"+outfnpf+"-0-0 -v -ntomp "+str(ntomp))
#apply nopbc for calculating the rmsd
os.system("echo 'System' | "+gmxcmd+" trjconv -s "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -f "+outfn+"-0-0/"+outfnpf+"-0-0.xtc -o "+outfn+"-0-0/"+outfnpf+"-0-0-noPBC.xtc -pbc mol -ur compact")
print("echo 'System' | "+gmxcmd+" trjconv -s "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -f "+outfn+"-0-0/"+outfnpf+"-0-0.xtc -o "+outfn+"-0-0/"+outfnpf+"-0-0-noPBC.xtc -pbc mol -ur compact")
#calculating the rmsd of the first cycle and pick up the 10 best one
os.system(gmxcmd+" distance -f "+outfn+"-0-0/"+outfnpf+"-0-0-noPBC.xtc -s "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -n "+ndxfn+" -oall "+outfn+"-0-0/"+outfnpf+"-0-0.xvg -xvg none -tu ps -sf "+wdir+"/sel.dat")
print(gmxcmd+" distance -f "+outfn+"-0-0/"+outfnpf+"-0-0-noPBC.xtc -s "+outfn+"-0-0/"+outfnpf+"-0-0.tpr -n "+ndxfn+" -oall "+outfn+"-0-0/"+outfnpf+"-0-0.xvg -xvg none -tu ps -sf "+wdir+"/sel.dat")
#reading the rmsd
comdist=numpy.loadtxt(outfn+"-0-0/"+outfnpf+"-0-0.xvg")
comdistcp=numpy.concatenate((comdist,numpy.zeros((len(comdist[:,1]),1))),1)
comdistcp=comdistcp[numpy.lexsort((comdistcp[:,0],comdistcp[:,1]))]
#writing the ranking to log file
for m in range(1,nbin+1):
os.system("echo '"+str(comdistcp[len(comdistcp[:,0])-m,2])+" "+str(comdistcp[len(comdistcp[:,0])-m,0])+" "+str(comdistcp[len(comdistcp[:,0])-m,1])+"' >> "+wdir+"/"+logfn)
print(str(comdistcp[len(comdistcp[:,0])-m,2])+" "+str(comdistcp[len(comdistcp[:,0])-m,0])+" "+str(comdistcp[len(comdistcp[:,0])-m,1])+"\n")
else:
#os.system("echo 'Simulation is going restart at ROUND "+str(nround)+" ' >> "+wdir+"/"+logfn)
print("Simulation is going restart at ROUND "+str(nround)+ "\n" )
#checking the need of restart file or quit the program
if not(os.path.exists(wdir+"/"+logfn)):
print(logfn+" file is not found! Please check the file.")
exit()
f=open(wdir+"/"+logfn,'r')
linesfn=f.readlines()
f.close()
for m in range(0,len(linesfn)-1):
linesfn[m]=linesfn[m].rstrip('\n')
#print(linesfn[1][17:(len(linesfn[1]))]+".")
bn=int(linesfn[1][17:(len(linesfn[1]))])
print("BIN of current PACS MD is ",bn)
#print(linesfn[2][19:(len(linesfn[2]))]+".")
if rest!=0:
rnd=int(linesfn[2][19:(len(linesfn[2]))])
print("ROUND of current PACS MD is ",rnd)
if restep>rnd-1:
print("Restart round is larger than the number of round that we have. Please recheck")
exit()
elif rest==0:
rnd=checkcycle()
restep=rnd-1
print("ROUND of current PACS MD is ",rnd)
# rewrite the PACS MD log:
if os.path.exists(wdir+"/"+logfn):
i=0
while(os.path.exists(wdir+"/"+logfn+"."+str(i))):
i=i+1
os.system("mv "+wdir+"/"+logfn+" "+wdir+"/"+logfn+"."+str(i))
f=open(wdir+"/"+logfn,'w')
wrtstepneed=nbin*(restep+1)+4*(restep+1)+4
while wrtstepneed>len(linesfn):
rnd=rnd-1
restep=rnd
wrtstepneed=nbin*(restep+1)+4*(restep+1)+4
print("Due to the lack of data in "+wdir+"/"+logfn+". Resetting ROUND of current PACS MD is ",rnd)
for m in range(0,wrtstepneed):
if m==2:
f.write(linesfn[2][0:18]+" "+str(int(restep+nroundadd))+"\n")
elif (restep!=0) and (m==wrtstepneed-1-nbin-1):
f.write(linesfn[m]+"\n")
elif m==(wrtstepneed-1):
f.write(linesfn[m]+" ")
else:
f.write(linesfn[m]+"\n")
f.close()
#rebuild the comdist array
cdcptemp=[] #clean buffer array
for m in range(1,nbin+1):
if not(os.path.exists(outfn+"-"+str(restep)+"-"+str(m)+"/"+outfnpf+"-"+str(restep)+"-"+str(m)+".xvg")):
print("Distance file is not found! Please check the file!")
exit()
cdtemp=numpy.loadtxt(outfn+"-"+str(restep)+"-"+str(m)+"/"+outfnpf+"-"+str(restep)+"-"+str(m)+".xvg")
cdcptemp1=[]
cdcptemp1=numpy.concatenate((cdtemp,numpy.ones((len(cdtemp[:,1]),1))*(m)),1)
#cdcptemp1=cdcptemp1[numpy.lexsort((cdcptemp1[:,0],cdcptemp1[:,1]))]
if m>1:
cdcptemp =numpy.concatenate((cdcptemp,cdcptemp1),0)
else:
cdcptemp=cdcptemp1
comdistcp=cdcptemp[numpy.lexsort((cdcptemp[:,0],cdcptemp[:,1]))]
#check for restart flag
if rest<0:
nbloop=1
else:
nbloop=restep+1
nround=restep+nroundadd+1
n=nbloop
while n<nround:
#for n in range(nbloop,nround):
cdcptemp=[] #clean buffer array
os.system("echo '++++++++++++++++' >> "+wdir+"/"+logfn)
print('++++++++++++++++\n')
os.system("echo 'ROUND "+str(n)+". ' >> "+wdir+"/"+logfn)
print("ROUND "+str(n)+"\n" )
os.system("echo '1111 Max to Min Ranking 1111' >> "+wdir+"/"+logfn)
print("++++ Ranking +++")
os.system("echo '+ Bin +++ Step +' >> "+wdir+"/"+logfn)
print("+ Bin +++ Step +")
for m in range(1,nbin+1):
#create directory
os.system("mkdir "+outfn+"-"+str(n)+"-"+str(m))
#dump frame to folder for MD run
os.system("echo 'System' | "+gmxcmd+" trjconv -f "+outfn+"-"+str(n-1)+"-"+str(int(comdistcp[len(comdistcp[:,0])-m,2]))+"/"+outfnpf+"-"+str(n-1)+"-"+str(int(comdistcp[len(comdistcp[:,0])-m,2]))+".xtc -s "+outfn+"-"+str(n-1)+"-"+str(int(comdistcp[len(comdistcp[:,0])-m,2]))+"/"+outfnpf+"-"+str(n-1)+"-"+str(int(comdistcp[len(comdistcp[:,0])-m,2]))+".tpr -o "+outfn+"-"+str(n)+"-"+str(m)+"/input.gro -dump "+str(float(comdistcp[len(comdistcp[:,0])-m,0])) )
#copy needed file for MD run
os.system("cp -r "+wdir+"/"+mdfn+" "+outfn+"-"+str(n)+"-"+str(m)+"/")
os.system("cp -r "+wdir+"/"+topolfn+" "+outfn+"-"+str(n)+"-"+str(m)+"/")
if len(itpfn)>0:
os.system("cp -r "+wdir+"/"+itpfn+" "+outfn+"-"+str(n)+"-"+str(m)+"/")
os.system("cp -r "+wdir+"/index.ndx"+" "+outfn+"-"+str(n)+"-"+str(m)+"/")
#running simulation with random vel (set in mdp file)
if vers <=2016:
os.system(gmxcmd+" grompp -f "+outfn+"-"+str(n)+"-"+str(m)+"/"+mdfn+" -c "+outfn+"-"+str(n)+"-"+str(m)+"/input.gro -o "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".tpr -maxwarn 10")
else:
os.system(gmxcmd+" grompp -f "+outfn+"-"+str(n)+"-"+str(m)+"/"+mdfn+" -c "+outfn+"-"+str(n)+"-"+str(m)+"/input.gro -o "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".tpr -r "+grofn+" -maxwarn 10")
if gpu>=0 and ntomp>0:
os.system(gmxcmd2+" mdrun -deffnm "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+" -v -ntomp "+str(ntomp)+" -gpu_id "+str(gpuid))
else:
os.system(gmxcmd2+" mdrun -deffnm "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+" -v -ntomp "+str(ntomp))
#check the distribution and add to temperary array
#apply nopbc for calculating the CV
os.system("echo 'System' | "+gmxcmd+" trjconv -s "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".tpr -f "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".xtc -o "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+"-noPBC.xtc -pbc mol -ur compact")
print("echo 'System' | "+gmxcmd+" trjconv -s "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".tpr -f "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".xtc -o "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+"-noPBC.xtc -pbc mol -ur compact")
#calculating the CV of the run
os.system(gmxcmd+" distance -f "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+"-noPBC.xtc -s "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".tpr -n "+ndxfn+" -oall "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".xvg -xvg none -tu ps -sf "+wdir+"/sel.dat")
print(gmxcmd+" distance -f "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+"-noPBC.xtc -s "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".tpr -n "+ndxfn+" -oall "+outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".xvg -xvg none -tu ps -sf "+wdir+"/sel.dat")
#reading the CV
cdtemp=numpy.loadtxt(outfn+"-"+str(n)+"-"+str(m)+"/"+outfnpf+"-"+str(n)+"-"+str(m)+".xvg")
cdcptemp1=[]
cdcptemp1=numpy.concatenate((cdtemp,numpy.ones((len(cdtemp[:,1]),1))*(m)),1)
#cdcptemp1=cdcptemp1[numpy.lexsort((cdcptemp1[:,0],cdcptemp1[:,1]))]
if m>1:
cdcptemp =numpy.concatenate((cdcptemp,cdcptemp1),0)
else:
cdcptemp=cdcptemp1
#Preparing for the next PaCS MD step
#Ranking the trajectory
comdistcp=cdcptemp[numpy.lexsort((cdcptemp[:,0],cdcptemp[:,1]))]
#writing the ranking to log file
for l in range(1,nbin+1):
os.system("echo '"+str(comdistcp[len(comdistcp[:,0])-l,2])+" "+str(comdistcp[len(comdistcp[:,0])-l,0])+" "+str(comdistcp[len(comdistcp[:,0])-l,1])+"' >> "+wdir+"/"+logfn)
print(str(comdistcp[len(comdistcp[:,0])-l,2])+" "+str(comdistcp[len(comdistcp[:,0])-l,0])+" "+str(comdistcp[len(comdistcp[:,0])-l,1])+"\n")
if ((comdistmax>0.0) and (comdistcp[len(comdistcp[:,2])-nbin-1,1]<comdistmax) and (n==(nround-1))):
nround=nround+1
if ((comdistmax>0.0) and (comdistcp[len(comdistcp[:,2])-nbin-1,1]>comdistmax) and (n<(nround-1))):
break
n=n+1 #this command is for replacing "for" loop with "while" loop