-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bifurcation.py
361 lines (326 loc) · 14.4 KB
/
Bifurcation.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
#------------------------------------------------------------------------------
# The Bifurcation class contains the bifurcation rules for cell migration
# Lowell Taylor Edgar
# University of Edinburgh
# 2020
from LTETools import *
from Input import *
#------------------------------------------------------------------------------
# Handle a bifurcation point while migrating
def handle_bifurcation(migrating_cell, parent_vess, bif_node, branch1, branch2):
# Determine if flow within branches are incoming or outgoing of the bifurcation
if (branch1.n1 == bif_node):
if (branch1.Q > 0.):
branch1_status = "incoming"
else:
branch1_status = "outgoing"
if (branch1.n0 == bif_node):
if (branch1.Q > 0.):
branch1_status = "outgoing"
else:
branch1_status = "incoming"
if (branch2.n1 == bif_node):
if (branch2.Q > 0.):
branch2_status = "incoming"
else:
branch2_status = "outgoing"
if (branch2.n0 == bif_node):
if (branch2.Q > 0.):
branch2_status = "outgoing"
else:
branch2_status = "incoming"
if (migrating_cell.mig_direction == 'against'):
# If flow is only incoming in one branch, always choose that branch (ie, against flow)
if (branch1_status == "incoming" and branch2_status == "outgoing"):
migrating_cell.vessID = branch1.ID
branch1.cells.append(migrating_cell)
return
if (branch2_status == "incoming" and branch1_status == "outgoing"):
migrating_cell.vessID = branch2.ID
branch2.cells.append(migrating_cell)
return
if (migrating_cell.mig_direction == 'with'):
# If flow is only outgoing in one branch, always choose that branch (ie, with flow)
if (branch1_status == "incoming" and branch2_status == "outgoing"):
migrating_cell.vessID = branch2.ID
branch2.cells.append(migrating_cell)
return
if (branch2_status == "incoming" and branch1_status == "outgoing"):
migrating_cell.vessID = branch1.ID
branch1.cells.append(migrating_cell)
return
# If a flow-converging bifurcation, enact bifurcation rule
if (branch1_status == "incoming" and branch2_status == "incoming"):
bifurcation_rule(migrating_cell, parent_vess, bif_node, branch1, branch2)
return
# If a flow-diverging bifurcation, enact bifurcation rule
if (branch1_status == "outgoing" and branch2_status == "outgoing"):
bifurcation_rule(migrating_cell, parent_vess, bif_node, branch1, branch2)
return
print('handle_bifurcation made it this far without returning!')
migrating_cell.migrate = 0
return
#------------------------------------------------------------------------------
# Determine cell behavior using the specified bifurcation rule
def bifurcation_rule(migrating_cell, parent_vess, bif_node, branch1, branch2):
# If bifurcation is at n0 node of parent vessel
if (bif_node == parent_vess.n0):
angle1 = findangle2D(-parent_vess.unit, branch1.unit)
angle2 = findangle2D(-parent_vess.unit, branch2.unit)
if (angle1 < 0):
left_branch = branch1
right_branch = branch2
else:
left_branch = branch2
right_branch = branch1
if (migrating_cell.zeta <= 0.5):
migrating_cell.vessID = right_branch.ID
right_branch.cells.append(migrating_cell)
return
if (migrating_cell.zeta > 0.5):
migrating_cell.vessID = left_branch.ID
left_branch.cells.append(migrating_cell)
return
# If bifurcation is at n0 node of parent vessel
if (bif_node == parent_vess.n1):
angle1 = findangle2D(parent_vess.unit, branch1.unit)
angle2 = findangle2D(parent_vess.unit, branch2.unit)
if (angle1 < 0):
left_branch = branch1
right_branch = branch2
else:
left_branch = branch2
right_branch = branch1
if (migrating_cell.zeta <= 0.5):
migrating_cell.vessID = left_branch.ID
left_branch.cells.append(migrating_cell)
return
if (migrating_cell.zeta > 0.5):
migrating_cell.vessID = right_branch.ID
right_branch.cells.append(migrating_cell)
return
#
#
# # Bifurcation Rule 1 - Simple geometric
# if (i_bifurcation_rule == 1):
# if (parent_vess.n1 == bif_node):
# if (migrating_cell.zeta <= 0.5):
# migrating_cell.vessID = left_branch.ID
# left_branch.cells.append(migrating_cell)
# return
#
# if (migrating_cell.zeta > 0.5):
# migrating_cell.vessID = right_branch.ID
# right_branch.cells.append(migrating_cell)
# return
# else:
#
#
#
#
#
#
#
#
#
# # Find the angle between parent vessel and both branches
# angle1 = findangle2D(parent_vess.unit, branch1.unit)
# angle2 = findangle2D(parent_vess.unit, branch2.unit)
#
#
# # Set the left branch to be the branch with the greatest angle
## if (angle1 > angle2):
## left_branch = branch1
## right_branch = branch2
## else:
## left_branch = branch2
## right_branch = branch1
#
# if (angle1 < 0):
# left_branch = branch1
# right_branch = branch2
# else:
# left_branch = branch2
# right_branch = branch1
#
# # Bifurcation Rule 1 - Simple geometric
# if (i_bifurcation_rule == 1):
# if (parent_vess.n1 == bif_node):
# if (migrating_cell.zeta <= 0.5):
# migrating_cell.vessID = left_branch.ID
# left_branch.cells.append(migrating_cell)
# return
#
# if (migrating_cell.zeta > 0.5):
# migrating_cell.vessID = right_branch.ID
# right_branch.cells.append(migrating_cell)
# return
# else:
# if (migrating_cell.zeta <= 0.5):
# migrating_cell.vessID = right_branch.ID
# right_branch.cells.append(migrating_cell)
# return
#
# if (migrating_cell.zeta > 0.5):
# migrating_cell.vessID = left_branch.ID
# left_branch.cells.append(migrating_cell)
# return
#
#
# # Bifurcation Rule 2 - Geometric with diameter control
# if (i_bifurcation_rule == 2):
# if ((left_branch.num_cells + right_branch.num_cells) == 0.):
# print('bifurcation_rule zero cells in both branches')
# migrating_cell.migrate = 0
# return
#
# zeta_left = left_branch.num_cells/(left_branch.num_cells + right_branch.num_cells)
# zeta_right = right_branch.num_cells/(left_branch.num_cells + right_branch.num_cells)
#
# if (parent_vess.n1 == bif_node):
# left_pt = migrating_cell.zeta
# right_pt = migrating_cell.zeta
#
# if ((left_pt >= 0.25) and (left_pt <= 0.25 + zeta_left/2.)) or ((left_pt - 1. < 0.25) and (left_pt - 1. > 0.25 - zeta_left/2.)):
# migrating_cell.vessID = left_branch.ID
# left_branch.cells.append(migrating_cell)
# return
#
# if ((left_pt >= 0.25) and (left_pt <= 0.25 + zeta_left/2.)) or ((left_pt < 0.25) and (left_pt > 0.25 - zeta_left/2.)):
# migrating_cell.vessID = left_branch.ID
# left_branch.cells.append(migrating_cell)
# return
#
# if ((right_pt >= 0.75) and (right_pt < 0.75 + zeta_right/2.)) or ((right_pt < 0.75) and (right_pt >= 0.75 - zeta_right/2.)):
# migrating_cell.vessID = right_branch.ID
# right_branch.cells.append(migrating_cell)
# return
#
# if ((right_pt + 1. >= 0.75) and (right_pt + 1. < 0.75 + zeta_right/2.)) or ((right_pt < 0.75) and (right_pt >= 0.75 - zeta_right/2.)):
# migrating_cell.vessID = right_branch.ID
# right_branch.cells.append(migrating_cell)
# return
# else:
# right_pt = migrating_cell.zeta
# left_pt = migrating_cell.zeta
#
# if ((right_pt >= 0.25) and (right_pt <= 0.25 + zeta_right/2.)) or ((right_pt < 0.25) and (right_pt > 0.25 - zeta_right/2.)):
# migrating_cell.vessID = right_branch.ID
# right_branch.cells.append(migrating_cell)
# return
#
# if ((right_pt >= 0.25) and (right_pt <= 0.25 + zeta_right/2.)) or ((right_pt - 1. < 0.25) and (right_pt - 1. > 0.25 - zeta_right/2.)):
# migrating_cell.vessID = right_branch.ID
# right_branch.cells.append(migrating_cell)
# return
#
# if ((left_pt + 1. >= 0.75) and (left_pt + 1. < 0.75 + zeta_left/2.)) or ((left_pt < 0.75) and (left_pt >= 0.75 - zeta_left/2.)):
# migrating_cell.vessID = left_branch.ID
# left_branch.cells.append(migrating_cell)
# return
#
# if ((left_pt >= 0.75) and (left_pt < 0.75 + zeta_left/2.)) or ((left_pt < 0.75) and (left_pt >= 0.75 - zeta_left/2.)):
# migrating_cell.vessID = left_branch.ID
# left_branch.cells.append(migrating_cell)
# return
# If we somehow didn't trigger a return
print('bifrucation_rule made it this far without returning!')
return
##------------------------------------------------------------------------------
## The simplest bifurcation rule, turn left or right based just on what side you're on
#def enhanced_geometric(migrating_cell, parent_vess, bif_node, branch1, branch2):
#
# # Find the angle between parent vessel and both branches
# angle1 = findangle2D(parent_vess.unit, branch1.unit)
# angle2 = findangle2D(parent_vess.unit, branch2.unit)
#
# # Set the left branch to be the branch with the greatest angle
# if (angle1 > angle2):
# left_branch = branch1
# right_branch = branch2
# else:
# left_branch = branch2
# right_branch = branch1
#
# zeta_left = left_branch.num_cells/(left_branch.num_cells + right_branch.num_cells)
# zeta_right = right_branch.num_cells/(left_branch.num_cells + right_branch.num_cells)
#
# # If exiting parent vessel from +1 terminus
# if (parent_vess.n1 == bif_node):
# cell_pt = migrating_cell.zeta
#
# if ((cell_pt >= 0.25) and (cell_pt <= 0.25 + zeta_left/2.)) or ((cell_pt - 1. < 0.25) and (cell_pt - 1. > 0.25 - zeta_left/2.)):
# assign_branch(left_branch, migrating_cell)
# return
#
# if ((cell_pt >= 0.25) and (cell_pt <= 0.25 + zeta_left/2.)) or ((cell_pt < 0.25) and (cell_pt > 0.25 - zeta_left/2.)):
# assign_branch(left_branch, migrating_cell)
# return
#
# if ((cell_pt >= 0.75) and (cell_pt < 0.75 + zeta_right/2.)) or ((cell_pt < 0.75) and (cell_pt >= 0.75 - zeta_right/2.)):
# assign_branch(right_branch, migrating_cell)
# return
#
# if ((cell_pt + 1. >= 0.75) and (cell_pt + 1. < 0.75 + zeta_right/2.)) or ((cell_pt < 0.75) and (cell_pt >= 0.75 - zeta_right/2.)):
# assign_branch(right_branch, migrating_cell)
# return
# # If exiting from 0 terminus
# else:
# cell_pt = migrating_cell.zeta
#
# if ((cell_pt >= 0.25) and (cell_pt <= 0.25 + zeta_right/2.)) or ((cell_pt < 0.25) and (cell_pt > 0.25 - zeta_right/2.)):
# assign_branch(right_branch, migrating_cell)
# return
#
# if ((cell_pt >= 0.25) and (cell_pt <= 0.25 + zeta_right/2.)) or ((cell_pt - 1. < 0.25) and (cell_pt - 1. > 0.25 - zeta_right/2.)):
# assign_branch(right_branch, migrating_cell)
# return
#
# if ((cell_pt + 1. >= 0.75) and (cell_pt + 1. < 0.75 + zeta_left/2.)) or ((cell_pt < 0.75) and (cell_pt >= 0.75 - zeta_left/2.)):
# assign_branch(left_branch, migrating_cell)
# return
#
# if ((cell_pt >= 0.75) and (cell_pt < 0.75 + zeta_left/2.)) or ((cell_pt < 0.75) and (cell_pt >= 0.75 - zeta_left/2.)):
# assign_branch(left_branch, migrating_cell)
# return
#
#
##------------------------------------------------------------------------------
## The simplest bifurcation rule, turn left or right based just on what side you're on
#def basic_geometric(migrating_cell, parent_vess, bif_node, branch1, branch2):
#
# # Find the angle between parent vessel and both branches
# angle1 = findangle2D(parent_vess.unit, branch1.unit)
# angle2 = findangle2D(parent_vess.unit, branch2.unit)
#
# # Set the left branch to be the branch with the greatest angle
# if (angle1 > angle2):
# left_branch = branch1
# right_branch = branch2
# else:
# left_branch = branch2
# right_branch = branch1
#
# # If exiting parent vessel from +1 terminus
# if (parent_vess.n1 == bif_node):
# if (migrating_cell.zeta <= 0.5):
# assign_branch(left_branch, migrating_cell)
# return
#
# if (migrating_cell.zeta > 0.5):
# assign_branch(right_branch, migrating_cell)
# return
# # If exiting from 0 terminus
# else:
# if (migrating_cell.zeta <= 0.5):
# assign_branch(right_branch, migrating_cell)
# return
#
# if (migrating_cell.zeta > 0.5):
# assign_branch(left_branch, migrating_cell)
# return
#------------------------------------------------------------------------------
# Assign a cell to a chosen target branch
def assign_branch(target_branch, migrating_cell):
migrating_cell.vessID = target_branch.ID
target_branch.cells.append(migrating_cell)