| 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
| import Tkinter
import random
import math
import time
import rules #That's "TedsWorkspace2"
winwidth = 1000
winheight = 700
randinterp = False
num_morphogens = 1
class morphogen:
def __init__(self, diffcoeff, prodrate,decayrate, initconc, mr):
self.diffcoeff = diffcoeff
self.prodrate = prodrate
self.decayrate = decayrate
self.conc = initconc
self.newc = initconc
self.rule = mr
class cell:
def __init__(self, idnum, type, xcor, ycor):
self.idnum = idnum
self.type = type # 0 = nonwing, 1 = scalecell, 2+ = vein
self.xcor = xcor
self.ycor = ycor
self.mrph = []
self.cnxn = [] # list of idnums of neighbors
self.colr = 0
def connect_to(self, clist): # accepts list of cells focal should be connected to and creates one-way connections
for i in range(len(clist)):
self.cnxn.append(clist[i])
def update_concs(self):
area= 1.0/6.0
aa = 1.0
bb = 1.0
cc = 1.0
for m in range(len(self.mrph)):
if self.mrph[m].conc != -1:
self.mrph[m].newc -= self.mrph[m].conc * self.mrph[m].decayrate
self.mrph[m].newc += self.mrph[m].prodrate
self.mrph[m].newc += self.mrph[m].rule.evaluate(self)
for n in range(len(self.cnxn)):
if self.mrph[m].conc > self.cnxn[n].mrph[m].conc:
self.mrph[m].newc -= (self.mrph[m].conc - self.cnxn[n].mrph[m].conc) * area * self.mrph[m].diffcoeff / self.length_to(self.cnxn[n])
self.cnxn[n].mrph[m].newc += (self.mrph[m].conc - self.cnxn[n].mrph[m].conc) * area * self.mrph[m].diffcoeff / self.length_to(self.cnxn[n])
# if m == 0: # because we only want these done once -- the hard-coded 0 and 1 are just for reproducing turing result sloppily
# # later should try to generalize code and make code more elegant
# self.mrph[0].newc += aa * self.mrph[0].conc*self.mrph[0].conc / len(self.cnxn)
# self.mrph[0].newc -= bb * self.mrph[1].conc / len(self.cnxn)
# self.mrph[0].newc -= 12.0 / len(self.cnxn)
# self.mrph[0].newc += self.mrph[0].prodrate / len(self.cnxn)
# self.mrph[0].newc -= self.mrph[0].conc * self.mrph[0].decayrate / len(self.cnxn)
# self.mrph[1].newc += self.mrph[0].conc / len(self.cnxn)
# self.mrph[1].newc -= self.mrph[1].conc * self.mrph[0].conc / len(self.cnxn)
# self.mrph[1].newc += 16.0 / len(self.cnxn)
# self.mrph[1].newc -= self.mrph[1].conc * self.mrph[1].decayrate / len(self.cnxn)
if self.mrph[m].newc < 0.0:
self.mrph[m].newc = 0.0
#if self.type > 1:
#self.mrph[m].newc = 100.0
def length_to(self, other):
x0 = float(self.xcor)
y0 = float(self.ycor)
x1 = float(other.xcor)
y1 = float(other.ycor)
return math.sqrt((x0 - x1)*(x0 - x1) + (y0 - y1) * (y0 - y1))
def define_morphogen(mrphnum, morphogenlist, celllist,morphname,diffcoeff,prodrate,decayrate,initconc):
mr = rules.random_proteinrule(mrphnum)
morphogenlist.append([morphname,diffcoeff,prodrate,decayrate, mr])
for i in range(len(celllist)):
celllist[i].mrph.append(morphogen(diffcoeff,prodrate,decayrate,initconc, mr))
return morphogenlist
def colorToType(r,g,b):
c = (r,g,b)
if c == (0,0,255):
tmp = 2
elif c == (255,0,0):
tmp = 3
elif c == (255,0,107):
tmp = 4
elif c == (255,0,255):
tmp = 5
elif c in ((115,0,107),(90,0,90),(82,0,99)):
tmp = 6
elif c == (49,0,99):
tmp = 7
elif c in ((90,0,57), (123,0,49), (66,0,57), (90,0,41)):
tmp = 8
elif c == (247,0,49):
tmp = 9
elif c == (49, 0, 255):
tmp = 10
elif c in ((255,33,0), (255,74,0)):
tmp = 11
elif c in ((247,90,0)): # also (255,255,0), but only in solitary pixels
tmp = 12
elif c == (0, 99, 255):
tmp = 13
elif c == (0, 255, 0):
tmp = 14
elif c == (0, 255, 255):
tmp = 15
elif c == (0, 247, 99):
tmp = 16
elif c == (0,99,0):
tmp = 17
elif c == (255,255,0):
tmp = 18
elif c == (99,0,0):
tmp = 19
elif c == (255,255,255):
tmp = 0
else:
tmp = 1
return tmp
def make_honeycomb(width, height):
print "Creating wing."
img = Tkinter.PhotoImage(file="/Users/twong/Desktop/VeinWing.gif")
imgwidth = img.width()
imgheight = img.height()
tmpM = [] # a 1-dim list of cells
tmpT = [] # a 2-dim list for looking up cells by xy location and finding tmpM address corresponding to cell with that location
i = 0
for col in range(width):
tmp = []
for row in range(height):
irow = int(row * (float(imgheight) / float(height)))
if row % 2 == 0:
icol = int(col * (float(imgwidth) / float(width)))
r, g, b = map(int, img.get(icol, irow).split(" "))
ttype = colorToType(r,g,b)
if ttype == 18:
tr, tg, tb = map(int, img.get(icol, irow-1).split(" "))
if colorToType(tr,tg,tb) == 12:
ttype = 12
tmpM.append(cell(i,ttype,col,row))
else:
icol = int((col+.5) * (float(width) / float(imgwidth)))
r, g, b = map(int, img.get(icol, irow).split(" "))
ttype = colorToType(r,g,b)
if ttype == 18:
tr, tg, tb = map(int, img.get(icol, irow-1).split(" "))
if colorToType(tr,tg,tb) == 12:
ttype = 12
tmpM.append(cell(i,ttype,col+.5,row))
tmp.append(i)
i += 1
if i % 5000 == 0:
print " ", i, "cells created."
tmpT.append(tmp)
print len(tmpM), "cells created."
print "Creating neighbor connections."
cnxnnum = 0
for col in range(len(tmpT)):
for row in range(len(tmpT[0])):
if tmpM[tmpT[col][row]].type > 0:
if row % 2 == 0 and row < len(tmpT[0]) - 1: # even, not bottom
if tmpM[tmpT[col][row+1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col][row+1]]]) # connect to straight down, is to right
cnxnnum += 1
if col > 0: # connect down to left
if tmpM[tmpT[col-1][row+1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col-1][row+1]]])
cnxnnum += 1
if row % 2 == 0 and row > 0: # even, not top
if col < len(tmpT) - 1:
if tmpM[tmpT[col][row-1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col][row-1]]]) # connect to straight up, is to right
cnxnnum += 1
if col > 0: # connect up to left
if tmpM[tmpT[col-1][row-1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col-1][row-1]]])
cnxnnum += 1
if row % 2 == 1 and row < len(tmpT[0]) - 1: # odd, not bottom
if tmpM[tmpT[col][row+1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col][row+1]]]) # connect to straight down, is to left
cnxnnum += 1
if col < len(tmpT) - 1: # connect down to rt
if tmpM[tmpT[col+1][row+1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col+1][row+1]]])
cnxnnum += 1
if row % 2 == 1 and row > 0: # odd, not top
if tmpM[tmpT[col][row-1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col][row-1]]]) # connect to straight up, is to left
cnxnnum += 1
if col < len(tmpT) - 1: # connect up to left
if tmpM[tmpT[col+1][row-1]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col+1][row-1]]])
cnxnnum += 1
if col > 0:
if tmpM[tmpT[col-1][row]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col-1][row]]])
cnxnnum += 1
if col< len(tmpT) - 1:
if tmpM[tmpT[col+1][row]].type > 0:
tmpM[tmpT[col][row]].connect_to([tmpM[tmpT[col+1][row]]])
cnxnnum += 1
if cnxnnum % 5000 == 0 and cnxnnum > 0:
print " ", cnxnnum,"neighbor-pairs connected."
print " ", cnxnnum,"neighbor-pairs connected."
print "Deleting non-wing cells."
deletednum = 0
deletelist = []
k = 0
for l in range(len(tmpM) / 5000):
deletelist.append(tmpM[(l * 5000):((l+1)*5000)])
deletelist.append(tmpM[(len(tmpM) / 5000)*5000:len(tmpM)])
tmpM = []
for l in range(len(deletelist)):
m = len(deletelist[l]) - 1
while m >= 0:
if deletelist[l][m].type == 0:
del(deletelist[l][m])
deletednum += 1
if deletednum % 2500 == 0:
print " Deleted", deletednum, "cells."
m -= 1
for l in range(len(deletelist)):
tmpM += deletelist[l]
# j = len(tmpM) - 1
# while j >= 0:
# if tmpM[j].type == 0:
# del(tmpM[j])
# deletednum += 1
# j -= 1
# if deletednum % 2500 == 0:
# print " Deleted", deletednum, "cells."
print "Wing created. Final cell number", str(len(tmpM))+"."
return tmpM
def connect(pairlist, isDirectional): #accept list of two-element lists and connects from first elem to second for each
for i in len(pairlist): # if not isDirectional, also connects from second back to first
pairlist[i][0].connect([pairlist[i][1]])
if not(isDirectional):
pairlist[i][1].connect([pairlist[i][0]])
def distance(a,b):
x0 = float(a.xcor)
y0 = float(a.ycor)
x1 = float(b.xcor)
y1 = float(b.ycor)
return math.sqrt((x0-x1)*(x0-x1)+ (y0-y1)*(y0-y1))
def coin(prob):
if random.random() < prob:
return 1
else:
return 0
random.seed()
win = Tkinter.Tk()
can = Tkinter.Canvas(win,height=winheight,width=winwidth,background="white")
can.pack()
rfile = open("rulefile", "a")
wing = make_honeycomb(400,400)
for rnum in range(2000):
graphicslist = []
if rnum > 0:
for i in range(len(wing)):
for j in range(len(wing[i].mrph)):
wing[i].mrph[j].conc = 0.0
morph = []
define_morphogen(num_morphogens, morph,wing,"activator",0.1,0.4,0.5,0.0) # (morphogenlist, celllist,morphname,diffcoeff,prodrate,decayrate,initconc)
# define_morphogen(num_morphogens, morph,wing,"inhibitor",0.5,0.2,0.5,0.0)
if rnum == 0:
#tg = can.create_text(150+5*wing[len(wing)-1].xcor+10,50, anchor = "w", text="t = 1", fill = "black")
tg = can.create_text(50,500, anchor = "w", text="t = 1", fill = "black")
if randinterp:
iru = rules.random_interprule(num_morphogens)
irstring = iru.rule_string()
pru = rules.random_proteinrule(num_morphogens)
prstring = ""
for i in range(len(morph)):
prstring += str(i) + ": " + morph[i][4].rule_string()
if i != len(morph)-1:
prstring += "\n"
if rnum == 0:
if randinterp:
irs = Tkinter.StringVar()
prs = Tkinter.StringVar()
if randinterp:
irtext = Tkinter.Label(win, textvariable = irs, wraplength = winwidth, font = ("Helvetica", 24))
prtext = Tkinter.Label(win, textvariable = prs, anchor = "w", wraplength = winwidth, font = ("Helvetica", 24))
if randinterp:
irs.set(irstring)
prs.set(prstring)
if randinterp:
irtext.pack()
prtext.pack()
else:
if randinterp:
irs.set(irstring)
prs.set(prstring)
if rnum == 0:
if randinterp:
rb = Tkinter.Button(win, text = "record", width = 12, pady = 2, command = rules.write_curr_rstring(rfile, irstring + "\n"+prstring))
else:
rb = Tkinter.Button(win, text = "record", width = 12, pady = 2, command = rules.write_curr_rstring(rfile, prstring))
rb.pack()
for t in range(10):
can.itemconfig(tg, text = ("t = "+str(t+1)))
can.update()
for i in range(len(wing)):
for j in range(len(wing[i].mrph)):
wing[i].mrph[j].newc = wing[i].mrph[j].conc
for i in range(len(wing)):
wing[i].update_concs()
for i in range(len(wing)):
if wing[i].type > 1:
# Here is where morphogen sources are sources
wing[i].mrph[0].newc = 4.0
wmin = 1e60
wmax = -1
for i in range(len(wing)):
if wing[i].type == 1:
wmin = min(wmin, wing[i].mrph[0].conc)
wmax = max(wmax, wing[i].mrph[0].conc)
for j in range(len(wing[i].mrph)):
wing[i].mrph[j].conc = wing[i].mrph[j].newc
if abs(wing[i].mrph[j].conc) > 1e50:
wing[i].mrph[j].conc = -1
for i in range(len(wing)):
if randinterp:
if iru.evaluate(wing[i]):
wing[i].colr = "#0000ff"
else:
wing[i].colr = "#000000"
else:
if wing[i].mrph[0].conc == -1:
wing[i].colr = "#ff0000"
elif wmax - wmin > .01:
wing[i].colr = "#%02x%02x%02x" % (0, 0, (min(255,max(0,int(wmin+wing[i].mrph[0].conc*256.0/wmax-wmin)))))
else:
wing[i].colr = "#%02x%02x%02x" % (0, 0, (min(255,max(0,int(wmin+wing[i].mrph[0].conc*256.0/.01)))))
d=1
s = 3
if t == 0:
for i in range(len(wing)):
graphicslist.append(can.create_rectangle(s+d*wing[i].xcor-1, s+d*wing[i].ycor-1, s+d*wing[i].xcor+0, s+d*wing[i].ycor+1,width=0,fill=wing[i].colr))
else:
for i in range(len(wing)):
can.itemconfig(graphicslist[i], fill = wing[i].colr)
can.itemconfig(graphicslist[len(graphicslist) - 1], fill = "red")
for i in range(len(graphicslist)):
can.delete(graphicslist[i])
i = len(wing) - 1
while i >= 0:
del wing[i]
i -= 1
# time.sleep(0.5)
rfile.close()
can.mainloop() |