-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFES.py
212 lines (149 loc) · 6.77 KB
/
FES.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Juanrong Zhang
"""
#set the name of system
system_name="open_amber"
#set the number of cycle
num_cycle=30
#the list that save the number of seeds in each cycle
lis=[]
import numpy as np
import mdtraj as md
import simtk.openmm.app as omma
import simtk.openmm as omm
import simtk.unit as unit
import os
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture
from scipy.spatial import ConvexHull
#build the system and it can be used by initial short simulation and seed simulation
def build_system(prmtop):
system = prmtop.createSystem(nonbondedMethod = omma.PME,
nonbondedCutoff = 1*unit.nanometer,
constraints = omma.HBonds)
integrator = omm.LangevinIntegrator(310*unit.kelvin, 1/unit.picoseconds,
0.002*unit.picoseconds)
platform = omm.Platform.getPlatformByName('CUDA')
#properties = {'DeviceIndex':'2'}
simulation = omma.Simulation(prmtop.topology, system, integrator, platform)
#simulation = omma.Simulation(prmtop.topology, system, integrator)
return simulation
#run short eq simulation
def run_short_eq(simulation, inpcrd, steps):
simulation.context.setPositions(inpcrd.positions)
if inpcrd.boxVectors is not None:
simulation.context.setPeriodicBoxVectors(*inpcrd.boxVectors)
simulation.minimizeEnergy()
simulation.reporters.append(omma.DCDReporter('eq.dcd', 5000))
simulation.reporters.append(omma.StateDataReporter('eq_log.txt', 5000,
step=True,
potentialEnergy=True,
temperature=True))
simulation.step(steps)
#run seed simulation by reading the coordinates of seed and initialize velocity based on temperature
def run_seed_simulation(simulation, inpcrd, steps, seed_index, coming_cycle):
simulation.context.setPositions(inpcrd.positions)
simulation.context.setVelocitiesToTemperature(310)
if inpcrd.boxVectors is not None:
simulation.context.setPeriodicBoxVectors(*inpcrd.boxVectors)
simulation.reporters.append(omma.DCDReporter('./'+str(coming_cycle)+'/'+
str(seed_index)+'.dcd',5000))
simulation.reporters.append(omma.StateDataReporter('./'+str(coming_cycle)+'/'
+str(seed_index)+'.txt',
5000, step=True,
potentialEnergy=True,
temperature=True))
simulation.step(steps)
def get_proj_and_rst(coming_cycle,lis):
os.system('mkdir '+str(coming_cycle))
#read coordinates, perform PCA and get projection
coord=[]
ensemble = md.load_pdb(system_name+'.pdb')
topology = ensemble.topology
ref=md.load_pdb(system_name+'.pdb',atom_indices=topology.select('name CA'))
if coming_cycle==1:
dcd_bb=md.load('./eq.dcd',top=system_name+'.pdb',
atom_indices=topology.select('name CA'))
dcd=md.load('./eq.dcd',top=system_name+'.pdb')
else:
filennames=['./eq.dcd']+['./'+str(index_cycle+1)+'/'+str(index_seed+1)+'.dcd'
for index_cycle in range(len(lis))
for index_seed in range(lis[index_cycle])
]
dcd_bb=md.load(filennames, top=system_name+'.pdb', atom_indices=topology.select('name CA'))
dcd=md.load(filennames, top=system_name'.pdb')
dcd_bb=dcd_bb.superpose(reference=ref)
coord.extend(dcd_bb.xyz)
coord=np.array(coord)
res=coord
d_1,d_2,d_3=res.shape
res=res.reshape(d_1,d_2*d_3)
mean=np.mean(res,axis=0)
res_new=res-mean
pca=PCA(n_components=3)
pca.fit(res_new)
print(pca.explained_variance_ratio_)
proj=np.dot(res_new,pca.components_.T)
np.savetxt('./'+str(coming_cycle)+'/proj.txt',proj,fmt="%.2f")
#perform GMM
probability_cutoff=0.1
BIC_cutoff=0.3
n_components=np.arange(1,21)
models=[GaussianMixture(n, covariance_type='full',random_state=0).fit(proj) for n in n_components]
bic=[m.bic(proj) for m in models]
print(bic)
#determine the number of components of GMM using BIC_cutoff 0.3
slope=(bic-min(bic))/(max(bic)-min(bic))<BIC_cutoff
model_index=np.where(slope==True)[0][0]
components=model_index+1
print(components)
#seperate points using probability 0.1 and then use convex hull at each set
gmm2=models[model_index]
prob=gmm2.fit(proj).predict_proba(proj).round(3)
index=[]
hull_index=[]
index_not_hull=[]
for i in range(components):
index.append(np.argwhere((prob[:,i]> probability_cutoff )==True)[:,0])
hull=ConvexHull(proj[index[i]])
hull_index_Xmoon=index[i][hull.vertices]
hull_index.append(hull_index_Xmoon)
index_not_hull.append(set(index[i]).difference(set(hull_index[i])))
#get the unique index of seeds after eliminating the intersection points
vertix_index=[]
for i in range(components):
hull=ConvexHull(proj[index[i]])
hull_index_res=index[i][hull.vertices]
for j in hull_index_res:
for k in range(components):
mark=True
if i==k:
continue
else:
if j in index_not_hull[k]:
mark=False
break
if mark==True:
vertix_index.append(j)
vertix_index=np.unique(vertix_index)
np.savetxt('./'+str(coming_cycle)+'/vertix_index.txt',vertix_index,fmt='%d')
dcd_save=dcd[vertix_index]
dcd_save.save_amberrst7('./'+str(coming_cycle)+'/rst')
return vertix_index
#read prmtop and inpcrd file of system
prmtop = omma.AmberPrmtopFile(system_name+'./prmtop')
initial_inpcrd = omma.AmberInpcrdFile(system_name+'.inpcrd')
simulation = build_system(prmtop)
#run 10 ns eq simulation
run_short_eq(simulation, initial_inpcrd, 5000000)
for i in range(num_cycle):
coming_cycle=i+1
vertix_index=get_proj_and_rst(coming_cycle,lis)
lis.append(len(vertix_index))
for j in range(len(vertix_index)):
simulation_seed=build_system(prmtop)
seed_inpcrd=omma.AmberInpcrdFile('./'+str(coming_cycle)+'/rst.'+
("{0:0"+str(len(str(len(vertix_index))))+"d}").format(j+1))
run_seed_simulation(simulation_seed,seed_inpcrd,50000,j+1,coming_cycle)