-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
451 lines (351 loc) · 14.4 KB
/
main.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""
EC552 HW 1: Genetic Circuit Design Program
Drew Gross and Marlee Feltham
"""
import sys
import json
import math
import copy
# circuit: NOT and NOR gate
# pLuxStar----- NOT P3_PhlF ________
# pTet ________|---NOR A1_AmtR--output
# not input: pluxstar
# not gate: P3_PhlF
# nor input a: not output
# nor input b: ptet
# nor gate: A1_AmtR
# ======================================================================================
# ====================================== FILE R/W ======================================
# ======================================================================================
def read_file(fname):
# open and read .input and .UCF JSON files
with open('input/' + fname, 'r') as file:
content = file.read()
data = json.loads(content)
return data
def write_output(fname, data):
# open and write to .output JSON file
with open('output/' + fname, 'w') as file:
json.dump(data, file)
file.close()
# ======================================================================================
# ======================================= PARSER =======================================
# ======================================================================================
def parse_UCF(data, gate_not, gate_nor):
ucf = {}
name = []
ymax = []
ymin = []
n = []
k = []
for i in range(len(data)):
if data[i]["collection"] == "models":
if data[i]['name'] == gate_not or data[i]['name'] == gate_nor:
name.append(data[i]['name'])
for j in range(len(data[i]['parameters'])):
if data[i]['parameters'][j]['name'] == 'ymax':
ymax.append(data[i]['parameters'][j]['value'])
elif data[i]['parameters'][j]['name'] == 'ymin':
ymin.append(data[i]['parameters'][j]['value'])
elif data[i]['parameters'][j]['name'] == 'n':
n.append(data[i]['parameters'][j]['value'])
elif data[i]['parameters'][j]['name'] == 'K':
k.append(data[i]['parameters'][j]['value'])
ucf['name'] = name
ucf['ymin'] = ymin
ucf['ymax'] = ymax
ucf['n'] = n
ucf['K'] = k
# print('ucf parameters: ', ucf)
return ucf
def parse_input(data, not_prom, nor_prom):
inputs = {}
name = []
ymax = []
ymin = []
n = []
k = []
for i in range(len(data)):
if data[i]['collection'] == 'models':
if data[i]['name'] == not_prom or data[i]['name'] == nor_prom:
name.append(data[i]['name'])
for j in range(len(data[i]['parameters'])):
if data[i]['parameters'][j]['name'] == 'ymax':
ymax.append(data[i]['parameters'][j]['value'])
elif data[i]['parameters'][j]['name'] == 'ymin':
ymin.append(data[i]['parameters'][j]['value'])
elif data[i]['parameters'][j]['name'] == 'alpha':
n.append(data[i]['parameters'][j]['value'])
elif data[i]['parameters'][j]['name'] == 'beta':
k.append(data[i]['parameters'][j]['value'])
inputs['name'] = name
inputs['ymin'] = ymin
inputs['ymax'] = ymax
inputs['n'] = n
inputs['K'] = k
print('input parameters: ', inputs)
return inputs
# ======================================================================================
# ===================================== OPERATIONS =====================================
# ======================================================================================
def find_idx(ucf, gate):
for i in range(len(ucf['name'])):
if ucf['name'][i] == gate:
return i
def stretch(ucf, gate, x):
new_ucf = copy.deepcopy(ucf)
i = find_idx(ucf, gate)
new_ucf['ymax'][i] = ucf['ymax'][i]*x
new_ucf['ymin'][i] = ucf['ymin'][i]/x
# print('stretch: ', new_ucf)
return new_ucf
def promoter(ucf, pick, gate, x):
new_ucf = copy.deepcopy(ucf)
i = find_idx(ucf, gate)
if pick == 0:
# weaker promoter
new_ucf['ymax'][i] = ucf['ymax'][i]/x
new_ucf['ymin'][i] = ucf['ymin'][i]/x
elif pick == 1:
# stronger promoter
new_ucf['ymax'][i] = ucf['ymax'][i]*x
new_ucf['ymin'][i] = ucf['ymin'][i]*x
# print('promoter: ', new_ucf)
return new_ucf
def slope(ucf, pick, gate, x):
new_ucf = copy.deepcopy(ucf)
i = find_idx(ucf, gate)
if pick == 0:
# decrease slope
new_ucf['n'][i] = ucf['n'][i]/x
elif pick == 1:
# increase slope
new_ucf['n'][i] = ucf['n'][i]*x
# print('slope: ', new_ucf)
return new_ucf
def rbs(ucf, pick, gate, x):
new_ucf = copy.deepcopy(ucf)
i = find_idx(ucf, gate)
if pick == 0:
# weaker rbs
new_ucf['K'][i] = ucf['K'][i]*x
elif pick == 1:
# stronger rbs
new_ucf['K'][i] = ucf['K'][i]/x
# print('rbs: ', new_ucf)
return new_ucf
# ======================================================================================
# =================================== SCORE CIRCUIT ====================================
# ======================================================================================
def nor_gate(ucf, inputs, not_output):
x = [ucf['ymin'][0]+not_output[0],
ucf['ymin'][0]+not_output[1],
ucf['ymax'][0]+not_output[0],
ucf['ymax'][0]+not_output[1]]
ttable = [0]*4
ttable[0] = float(ucf['ymin'][0] + (ucf['ymax'][0]-ucf['ymin']
[0]) / (1+(x[0]/ucf['K'][0]) ** ucf['n'][0]))
ttable[1] = float(ucf['ymin'][0] + (ucf['ymax'][0]-ucf['ymin']
[0]) / (1+(x[1]/ucf['K'][0]) ** ucf['n'][0]))
ttable[2] = float(ucf['ymin'][0] + (ucf['ymax'][0]-ucf['ymin']
[0]) / (1+(x[2]/ucf['K'][0]) ** ucf['n'][0]))
ttable[3] = float(ucf['ymin'][0] + (ucf['ymax'][0]-ucf['ymin']
[0]) / (1+(x[3]/ucf['K'][0]) ** ucf['n'][0]))
on_min = ttable[1]
off_max = ttable[3]
score = math.log10(on_min/off_max)
return ttable, score
def not_gate(ucf, inputs):
ttable = [0]*2
ttable[0] = float(ucf['ymin'][1] + (ucf['ymax'][1]-ucf['ymin'][1]) /
(1+(inputs['ymin'][1]/ucf['K'][1]) ** ucf['n'][1]))
ttable[1] = float(ucf['ymin'][1] + (ucf['ymax'][1]-ucf['ymin'][1]) /
(1+(inputs['ymax'][1]/ucf['K'][1]) ** ucf['n'][1]))
# print(ttable)
return ttable
def score_circuit(ucf, inputs):
not_output = not_gate(ucf, inputs)
[ttable, score] = nor_gate(ucf, inputs, not_output)
return score
# ======================================================================================
# ===================================== NEW VALUES =====================================
# ======================================================================================
def merge(ucf, param):
cpy = copy.deepcopy(ucf[0])
cpy2 = copy.deepcopy(ucf[1])
for i in range(len(param)):
cpy[param[i]][1] = cpy2[param[i]][1]
# print('cpy param:', cpy[param[i]])
return cpy
def y_decision(ucf, inputs, gate_nor, gate_not):
# performs a combination of stretch and promoter operations
# returns the best (lowest) score and combination of operations that modify
# ymax and ymin
gates = [gate_nor, gate_not]
original_score = score_circuit(ucf, inputs)
x = .85
scores = [original_score]
ucfs = []
for i in range(len(gates)):
ucfs.append(stretch(ucf, gates[i], x))
scores.append(score_circuit(ucfs[i], inputs))
ucfs.append(merge(ucfs, ['ymax', 'ymin']))
scores.append(score_circuit(ucfs[i+1], inputs))
ucfs.append(ucf)
idx = scores.index(min(scores)) # best ucfs index
# print('ucfs: ', ucfs)
prom_ucf = []
scores = [original_score]
for i in range(len(gates)):
prom_ucf.append(promoter(ucfs[idx], i, gates[i], x))
scores.append(score_circuit(prom_ucf[i], inputs))
prom_ucf.append(merge(prom_ucf, ['ymax', 'ymin']))
scores.append(score_circuit(prom_ucf[i+1], inputs))
ucfs = [ucf]
for i in range(len(prom_ucf)):
ucfs.append(prom_ucf[i])
idx = scores.index(min(scores)) # best inputs index
return ucfs[idx], scores[idx]
def n_decision(ucf, inputs, gate_nor, gate_not):
# performs slope operation
# returns the best (lowest) score and combination of operations that modify n
gates = [gate_nor, gate_not]
original_score = score_circuit(ucf, inputs)
x = .85
scores = [original_score]
slope_ucf = []
for i in range(len(gates)):
slope_ucf.append(slope(ucf, i, gates[i], x))
scores.append(score_circuit(slope_ucf[i], inputs))
slope_ucf.append(merge(slope_ucf, 'n'))
scores.append(score_circuit(slope_ucf[i+1], inputs))
ins = [ucf]
for i in range(len(slope_ucf)):
ins.append(slope_ucf[i])
# print('n ins: ', ins)
idx = scores.index(min(scores))
return ins[idx], scores[idx]
def k_decision(ucf, inputs, gate_nor, gate_not):
# performs RBS operation
# returns the best (lowest) score and combination of operations that modify K
gates = [gate_nor, gate_not]
original_score = score_circuit(ucf, inputs)
x = .85
scores = [original_score]
rbs_ucf = []
for i in range(len(gates)):
rbs_ucf.append(slope(ucf, i, gates[i], x))
scores.append(score_circuit(rbs_ucf[i], inputs))
rbs_ucf.append(merge(rbs_ucf, 'K'))
scores.append(score_circuit(rbs_ucf[i+1], inputs))
ins = [ucf]
for i in range(len(rbs_ucf)):
ins.append(rbs_ucf[i])
# print('n ins: ', ins)
idx = scores.index(min(scores))
return ins[idx], scores[idx]
def best_score(ucf, inputs, gate_nor, gate_not):
y_ucf, y_score = y_decision(ucf, inputs, gate_nor, gate_not)
n_ucf, n_score = n_decision(ucf, inputs, gate_nor, gate_not)
k_ucf, k_score = k_decision(ucf, inputs, gate_nor, gate_not)
scores = [y_score, n_score, k_score]
# print('scores: ', scores)
ucfs = [ucf, y_ucf, n_ucf, k_ucf]
cpy = copy.deepcopy(y_ucf)
cpy['n'] = n_ucf['n']
ucfs.append(cpy)
cpy = copy.deepcopy(y_ucf)
cpy['K'] = k_ucf['K']
ucfs.append(cpy)
cpy['n'] = n_ucf['n']
ucfs.append(cpy)
cpy = copy.deepcopy(k_ucf)
cpy['n'] = n_ucf['n']
ucfs.append(cpy)
# [inputs, y, n, k, y+n, y+k, y+k+n, n+k]
for i in range(len(ucfs)-4):
scores.append(score_circuit(ucfs[i+4], inputs))
idx = scores.index(min(scores))
return scores[idx]
def x_in():
x = input("Define x value (0 < x <= 1.05): \n")
if float(x) <= 0 or float(x) > 1.05:
sys.exit("Invalid x value.\n")
return x
# ======================================================================================
# ========================================MAIN==========================================
# ======================================================================================
def main():
chassis_name = 'Eco1C1G1T1'
in_ucf = f'{chassis_name}.UCF.json'
input_sensor_file = f'{chassis_name}.input.json'
output_device_file = f'{chassis_name}.output.json'
in_param = read_file(input_sensor_file)
UCF_param = read_file(in_ucf)
print('====== MODEL CHOICE ================================ \n')
print('Please refer to .json files & use underscores for this section. Omit anything but the molecule name.\n')
gate_choice = str(input("Assignment or Custom?: \n"))
#print(gate_choice)
if gate_choice in ["Assignment", "assignment"]:
gate_not = 'A1_AmtR_model'
gate_nor = 'P3_PhlF_model'
nor_prom = 'TetR_sensor_model'
not_prom = 'LuxR_sensor_model'
elif gate_choice in ["Custom", "custom"]:
gate_not = input("Enter NOT gate model: \n") + '_model'
gate_nor = input("Enter NOR gate model: \n") + '_model'
not_prom = input("Enter NOT sensor model: \n") + '_sensor_model'
nor_prom = input("Enter NOR sensor model: \n") + '_sensor_model'
elif gate_choice in ["Assignment","assignment","Custom","custom"]:
sys.exit("Invalid entry.\n")
print('\n')
ucf = parse_UCF(UCF_param, gate_not, gate_nor)
inputs = parse_input(in_param, not_prom, nor_prom)
print('====== INPUT SIGNALS ================================ \n')
operation = input("Choose up to 4 operations to perform on the NOT gate input from the following list:\n(a) Stretch\n(b) Increase slope\n(c) Decrease slope\n(d) Stronger promoter\n(e) Weaker promoter\n(f) Stronger RBS\n(g) Weaker RBS\n(x) done\n")
operation = [i for i in operation]
if len(operation) > 4:
sys.exit("Invalid entry. Too many operations.\n")
is_op = 1
for i in range(len(operation)):
if operation[i] == 'a':
new_ucf = stretch(ucf, gate_nor, float(x_in()))
elif operation[i] == 'b':
new_ucf = slope(ucf, 1, gate_nor, float(x_in()))
elif operation[i] == 'c':
new_ucf = slope(ucf, 0, gate_nor, float(x_in()))
elif operation[i] == 'd':
new_ucf = promoter(ucf, 1, gate_nor, float(x_in()))
elif operation[i] == 'e':
new_ucf = promoter(ucf, 0, gate_nor, float(x_in()))
elif operation[i] == 'f':
new_ucf = rbs(ucf, 1, gate_nor, float(x_in()))
elif operation[i] == 'g':
new_ucf = rbs(ucf, 0, gate_nor, float(x_in()))
elif operation[i] == 'x':
if i==0:
is_op = 0
break
if gate_choice in ["Assignment","assignment"]:
print('\n')
print('====== Assignment ===================================')
print('pTetR 0011')
print('pLuxStar 0101')
print('P3_PhlF pLuxStar')
print('A1_AmtR pLuxStar pTetR')
print('Output A1_AmtR')
print('\n')
if is_op == 0:
print('====== Default Score (No Operations) ================')
print(score_circuit(ucf, inputs))
print('\n')
print('====== Optimized Score ==============================')
print(best_score(ucf, inputs, gate_nor, gate_not))
write_output(output_device_file,[ucf,inputs])
elif is_op == 1:
print('====== Score with Operations ========================')
print(score_circuit(new_ucf, inputs))
write_output(output_device_file,[new_ucf,inputs])
print('\nYour parameter data is available in ',output_device_file,'. Thanks!\n')
if __name__ == "__main__":
main()