-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPH_Protein_Modeling.py
More file actions
393 lines (288 loc) · 9.24 KB
/
PH_Protein_Modeling.py
File metadata and controls
393 lines (288 loc) · 9.24 KB
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
386
387
388
389
390
391
392
393
import csv
import math
import statistics
import numpy
import random
from random import randint
import matplotlib.pyplot as plt
##############
###Functions
##############
def get_new_pos (prev_pos, step): #Given a position on the lattice and a Manhattan step, calculate the new lattice position
if step[0] == 0:
if step[1] == 1:
##calc new position
new_pos = [prev_pos[0] , prev_pos[1]+1]
return new_pos
elif step[1] == 0:
##calc new position
new_pos = [prev_pos[0]+1 , prev_pos[1]]
return new_pos
else:
##Something has gone wrong
print ("Manhattan step not correct 1")
elif step[0] == 1:
if step[1] == 1:
##calc new position
new_pos = [prev_pos[0] , prev_pos[1]-1]
return new_pos
elif step[1] == 0:
##calc new position
new_pos = [prev_pos[0]-1 , prev_pos[1]]
return new_pos
else:
##Something has gone wrong
print ("Manhattan step not correct 2")
else:
##Something has gone wrong
print ("Manhattan step not correct 3")
def legal (position, d): #Determine if a given lattice position is currently occupied
#position is not open in the lattice
if position in d.values():
return False
else:
return True
def folding (iter_start, i, pd, o, iter_end): #folds the sequence
sto_is = iter_start
iter = iter_start
sto_pd = pd.copy()
p = pd
op = [[0,0],[0,1],[1,1],[1,0]]
count = 0
while iter < iter_end: ##iterate through all the positions that we want folded
if iter == iter_start: ##This is for if we pass a limited number of options to start with
options = o[:]
else:
options = op[:]
check = True
while (check):
#Randomly choose Manhattan Step
if len(options) == 1:
ri = 0
elif len(options) == 0:
print("Something has gone wrong with options")
else:
ri = numpy.random.randint(0, len(options)-1)
randstep = options[ri]
#Fold the amino acid
fold = get_new_pos(p[iter-1], randstep)
#is the fold legal?
if legal(fold, p):
#place the amino acid in the lattice
p[iter] = fold
#Successful fold
check = False
else:
#remove move from options
del options[ri]
#check if we are out of options
if len(options) == 0:
#Check if folding is impossible
if (iter == sto_is):
return False
#Start all over
count += 1
if count == 20:
return False
p = sto_pd
iter = sto_is - 1
check = False
iter += 1
return p
def calc_energy(i, pd, steps):
energy=0
for x in range(0, len(i)):
if i[x] == "H":
current = pd[x]
neighbors = []
for y in range (0,len(steps)):
check_step = steps[y]
neighbors.append(get_new_pos(current, check_step))
for z in range (0,len(neighbors)):
#determine if neighbors are occupied
if neighbors[z] in pd.values():
#get key that corresponds to the neighbors position
n_key = list(pd.keys())[list(pd.values()).index(neighbors[z])]
#check if neighbor is "H"
if i[n_key] == "H":
#check if neighbor is topological
if n_key-1 != x and n_key+1 != x:
#update energy calc
energy += -1
return(energy)
def reconfig (i, pd, steps): #Outputs new configuration
pop_step = steps[:]
#eliminate the step used in the previous config, so that we are guaranteed to propose new change
for s in range(0,len(steps)):
if pd[len(i)-1] == get_new_pos(pd[(len(i)-1)], pop_step[s]) :
del pop_step[s]
npd = folding(len(i)-1, i, pd, pop_step, len(pd))
return npd
def point_mut (i, pd): ##create a point mutation and outputs the resulting configuration
count_ri = 0
step = [[0,0],[0,1],[1,1],[1,0]]
##Choose random index to change##
randchange = randint(2,len(i)-2)
count_ri += 1
new_c = []
##Get old configuration up to where the change will be made##
for y in range(0,len(i)):
if y < randchange:
new_c.append(i[y])
##Get new configuration##
new_posdict = reconfig(new_c, pd, step)
#If the fold was impossible
while new_posdict == False:
if count_ri == (len(i)-2):
return False
#Re-do
randchange = randint(2,len(i)-2)
count_ri +=1
new_c = []
for y in range(0,len(i)):
if y < randchange:
new_c.append(i[y])
new_posdict = reconfig(new_c, pd, step)
return new_posdict
def simulate(iter_end, i, pd, c, T): #Runs a number of simulations and outputs the energies in a list
sample_energy = []
old_pd = pd
old_config_energy = c
for x in range(0,iter_end):
new_pd = point_mut(i, old_pd)
step = [[0,0],[0,1],[1,1],[1,0]]
##Calc Energy##
new_config_energy = calc_energy(i, new_pd, step)
##Calc Metropolis Alg Values##
n_nce = -1 * new_config_energy #Neg new energy
n_oce = -1 * old_config_energy #Neg old energy
met_value = (math.exp(n_nce/T) / math.exp(n_oce/T))
##Metropolis accept/reject##
if new_config_energy < old_config_energy:
#ACCEPT
sample_energy.append(new_config_energy)
##Set old_pd to new_pd for the next iteration
old_pd = new_pd
old_config_energy = new_config_energy
elif random.uniform(0,1) < met_value:
#ACCEPT
sample_energy.append(new_config_energy)
##Set old_pd to new_pd for the next iteration
old_pd = new_pd
old_config_energy = new_config_energy
else:
##REJECT
sample_energy.append(old_config_energy)
return sample_energy
########
##MAIN##
########
#############################
###Read in the file to a list
#############################
file = open("hw1.data.txt", "rU")
input = []
for line in file:
for c in line:
input.append(c)
#testable sim input
#input = ["H","H","P","P","H","H","P","P"]
###################################################
###Part 1 - Build the lattice model and calc energy
###################################################
start_dict = {0 : [0,0]} #Holds lattice positions
man_steps = [[0,0],[0,1],[1,1],[1,0]]
##Fold the next amino acid in the input sequence##
position_dict = folding(1, input, start_dict, man_steps, len(input))
##See output for debugging
#for i in position_dict:
#print (i, position_dict[i])
##Calc Energy for model##
config_energy = calc_energy(input, position_dict, man_steps)
print(config_energy)
#############################################
###Part 2 - 1000 simulations of the model
#############################################
s = simulate(1000, input, position_dict, config_energy, 100)
plt.hist(s, align='mid', bins=range(min(s), max(s) + 2, 1))
plt.title("Histogram of Energy: 1000 Configurations")
plt.xlabel("Energy")
plt.ylabel("Count")
plt.show()
#########################
###Part 3 - Optimization
#########################
#Initialize Population
population = [] #A list of configurations (dictionaries)
best_energy = 0 #Inititalize best energy variable
best_config = {0 : [0,0]}
for x in range(0,100): #Population the population and get the value of the best energy
pop_f = folding(1, input, start_dict, man_steps, len(input))
if pop_f == False:
x = x-1
break
else:
population.append(pop_f)
ce = calc_energy(input, population[x], man_steps)
if ce <= best_energy:
best_energy = ce
best_config = population[x]
#Optimization
t = 70
current_pop = population
next_gen = []
check = True
while (check):
n=0
y = randint(0,len(current_pop)-1)
#Make a pointwise mutation of the next two configurations
pm_config_m = current_pop[y]
pm_config_f = point_mut(input, current_pop[y])
if(pm_config_f == False):
y = randint(0,len(current_pop)-1)
pm_config_m = current_pop[y]
pm_config_f = point_mut(input, current_pop[y])
while (n < len(current_pop)): #The second generation should make an equal or less number of individuals in the initial population
Produce child via crossover
cross_loc = randint(1, len(input)-2)
child = {0 : [0,0]}
for z in range(1,len(input)):
if z <= cross_loc:
child[z] = pm_config_m[z]
else:
if legal(pm_config_f[z], child):
child[z] = pm_config_f[z]
else: #Crossover is not legal
break
if len(child) == len(pm_config_m): ##We sucessfully made a legal child
child_e = calc_energy(input, child, man_steps)
m_e = calc_energy(input, pm_config_m, man_steps)
f_e = calc_energy(input, pm_config_f, man_steps)
p_av = statistics.mean([m_e, f_e])
if (child_e <= p_av):
next_gen.append(child)
else:
met_rand = random.uniform(0,1)
if (met_rand < (-(p_av + child_e))/t):
next_gen.append(child)
if (child_e <= best_energy):
best_energy = child_e
best_config = child
n += 1
t = t/math.log(n)
if len(current_pop) < 2 or t == 0:
check = False
break
#Update the generations and prepare to make another
current_pop = next_gen
next_gen = []
if len(current_pop) < 2 or t == 0:
check = False
##Write best config to a file
with open("dict.csv", "w") as csv_file:
writer = csv.writer(csv_file)
for key, value in best_config.items():
writer.writerow([key, value])
print ("BEST ENERGY")
print (best_energy)
file.close()