-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRanProlif_UD_WithMigration.m
340 lines (272 loc) · 10 KB
/
BRanProlif_UD_WithMigration.m
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
% Implementing spatial Moran 2D algorithm: Normal cells=1's
% and PM cells = 0's.
% Input: 21 X 21 matrix with one 0 and rest all 1's
% Generate a randomproliferation matrix for B
% Take the average value of rB to calculate the Probabilities
% rB Distribution: Uniform = [1.4,1.6] for advantageous mutants
% rA = 1.0
% Presence of migration of B cells only: mB = 5.0 (and mA = 0)
% Output: A matrix with either all 1's or all 0's
% Von-Neumann neighbourhood: 4 neighbours only
% The same code can be used for neutral B mutants too by changing the
% distributions
clc; clear all;
RandStream.setDefaultStream(RandStream('mt19937ar','seed',sum(100*clock))); % Random seed
% create a grid
xmax = 21;
ymax = 21;
x = 0:1:xmax;
y = 0:1:ymax;
A_won = 0;
B_won = 0;
lowerlimit = 1.4;
upperlimit = 1.6;
rB = lowerlimit + (upperlimit-lowerlimit).*rand(21,21);
fid = fopen('RanProlifB-D[1.4,1.6].txt', 'wt');
%-------------Randomly choose (i,j) to place PM cell by 0--------------
sets_max = 1;
iter_max = 50000;
for sets=1:sets_max % Number of sets
sets;
for iterations = 1:iter_max
iterations;
fprintf(fid, 'Iterations: %g\n', iterations);
X = ones(xmax,ymax);
M = sum(X);
M1 = sum(M);
M2 = 0;
M1init = xmax*ymax-1;
%i1 = randint(1,1,[1,xmax]);
%j1 = randint(1,1,[1,ymax]);
i1 = randi(xmax,1,1);
j1 = randi(ymax,1,1);
X(i1,j1) = 0;
while ((M1 ~= sum(sum(X))) && (M2 ~= sum(sum(X))))
X;
%i2 = randint(1,1,[1,xmax]);
%j2 = randint(1,1,[1,ymax]);
i2 = randi(xmax,1,1);
j2 = randi(ymax,1,1);
X(i2,j2) = -1;
X_withcelldeath = X;
while (any(any(X==-1))==1) % two times any since it checks both rows and columns
X_minusonepresent = X;
[k l] = ind2sub(size(X),find(X==-1)); % check the position i,j for -1
i2 = k;
j2 = l;
% Interior part (2,2)...(2,4) and (4,2)...(4,4)
if ((i2>1) && (i2<xmax) && (j2>1) && (j2<ymax))
neighbours(1) = X(i2,j2-1); % left side
neighbours(2) = X(i2,j2+1); % right side
neighbours(3) = X(i2-1,j2); % Top
neighbours(4) = X(i2+1,j2) ;% Bottom
end
% Top part (1,2)...(1,4)
if (i2==1)
if ((j2>1) && (j2<ymax))
k = 1;
neighbours(1) = X(i2,j2-1);
neighbours(2) = X(i2,j2+1);
neighbours(3) = 10;
neighbours(4) = X(i2+1,j2);
end
end
% Bottom part (5,2)...(5,4)
if (i2==xmax)
if ((j2>1) && (j2<xmax))
k = 1;
neighbours(1) = X(i2,j2-1);
neighbours(2) = X(i2,j2+1);
neighbours(3) = X(i2-1,j2);
neighbours(4) = 10;
end
end
% Left Side part (2,1)...(4,1)
if (j2==1)
if ((i2>1) && (i2<xmax))
k = 1;
neighbours(1) = 10;
neighbours(2) = X(i2,j2+1);
neighbours(3) = X(i2-1,j2);
neighbours(4) = X(i2+1,j2);
end
end
% Right Side part (2,5)...(4,5)
if (j2==ymax)
if ((i2>1) && (i2<xmax))
k = 1;
neighbours(1) = X(i2,j2-1);
neighbours(2) = 10;
neighbours(3) = X(i2-1,j2);
neighbours(4) = X(i2+1,j2);
end
end
if ((i2==1) && (j2== 1))
k = 1;
neighbours(1) = 10;
neighbours(2) = X(i2,j2+1);
neighbours(3) = 10;
neighbours(4) = X(i2+1,j2);
end
if ((i2==1) && (j2== ymax))
k = 1;
neighbours(1) = X(i2,j2-1);
neighbours(2) = 10;
neighbours(3) = 10;
neighbours(4) = X(i2+1,j2);
end
if ((i2==xmax) && (j2== 1))
k = 1;
neighbours(1) = 10;
neighbours(2) = X(i2,j2+1);
neighbours(3) = X(i2-1,j2);
neighbours(4) = 10 ;
end
if ((i2==xmax) && (j2== ymax))
k = 1;
neighbours(1) = X(i2,j2-1);
neighbours(2) = 10;
neighbours(3) = X(i2-1,j2);
neighbours(4) = 10;
end
neighbours;
% Count the number of normal and pre-malignant cells surrounding
% the empty spot where cell death happened
a = sum(neighbours);
b = floor(a/10);
NCells = a - b*10;
PMCells = 4 - b - NCells;
%---------Calculate the probabilities----------
nA = NCells;
nB = PMCells;
rA = 1.0;
[i2 j2];
%-----Calculate the average rB------
if (length(find(neighbours==0) ~=0 ))
for r = 1:length(neighbours)
if (neighbours(1)==10)
rBtemp(1) = 0;
else
linearindex1 = sub2ind(size(X),i2,j2-1);
rBtemp(1) = rB(linearindex1);
end
if (neighbours(2)==10)
rBtemp(2) = 0;
else
linearindex2 = sub2ind(size(X),i2,j2+1);
rBtemp(2) = rB(linearindex2);
end
if (neighbours(3)==10)
rBtemp(3) = 0;
else
linearindex3 = sub2ind(size(X),i2-1,j2);
rBtemp(3) = rB(linearindex3);
end
if (neighbours(4)==10)
rBtemp(4) = 0;
else
linearindex4 = sub2ind(size(X),i2+1,j2);
rBtemp(4) = rB(linearindex4);
end
end
X;
rB;
rBtemp;
%pause
rBtemp1 = find(neighbours==0);
for t1 = 1:length(rBtemp1)
rBtemp2 = rBtemp(rBtemp1);
temp3 = rBtemp2;
end
temp3;
rBave = mean(temp3);
%pause
else
rBave = 0;
end
%------end of average rB code------
rBave;
mA = 0;
mB = 5.0;
rBave;
Ktilde = nA*(rA+mA) + nB*(rBave+mB);
ProbAdiv = (nA*rA)/Ktilde;
ProbAmig = (nA*mA)/Ktilde;
ProbBdiv = (nB*rBave)/Ktilde;
ProbBmig = (nB*mB)/Ktilde;
Prob = [ProbAdiv ProbBdiv ProbAmig ProbBmig];
% Generate a random number to see which of the above events is
% likely to occur
r = rand;
if (r<=Prob(1))
%display('Event: A divides and places a cell in empty slot')
X(i2,j2) = 1 ;
M1updated = sum(sum(X));
M1init = M1updated;
X_Aafterbirth=X;
%pause
%return;
end
if ((Prob(1)< r) && (r<= Prob(1)+Prob(2)))
%display('Event: B divides and places a cell in empty slot')
[Q1 Q2] = ind2sub([xmax ymax],find(X==-1)); % check the position i,j for -1
Q = [Q1 Q2];
X(Q(1),Q(2)) = 0 ;
X;
Mupdated = sum(X);
M1updated = sum(Mupdated);
M1init = M1updated;
X_Bafterbirth = X;
%pause
%return;
end
if ((Prob(1)+Prob(2)< r) && (r<= Prob(1)+Prob(2)+Prob(3)))
%display('Event: A migrates into the empty spot')
%return;
end
if (Prob(1)+Prob(2)+Prob(3)< r)
%display('Event: B migrates into the empty spot')
Y = find(neighbours==0);
interval = Prob(4)/nB;
a1 = r - (Prob(1)+Prob(2)+Prob(3));
a2 = ceil(a1/interval);
if (Y(a2)==1)
X(i2,j2-1) = -1;
X(i2,j2) = 0;
end
if (Y(a2)==2)
X(i2,j2+1) = -1;
X(i2,j2) = 0;
end
if (Y(a2)==3)
X(i2-1,j2) = -1;
X(i2,j2) = 0;
end
if (Y(a2)==4)
X(i2+1,j2) = -1;
X(i2,j2) = 0;
end
X_aftermig = X;
%pause
end
Mupdated = sum(X);
M1updated = sum(Mupdated);
M1init = M1updated;
end
X;
end
if (sum(sum(X)) == xmax*ymax)
A_won = A_won + 1;
end
if (sum(sum(X)) == 0)
B_won = B_won + 1;
end
end
A(sets) = A_won;
B(sets) = B_won;
end
AverageA = A(sets_max)/(sets_max*iter_max);
AverageB = B(sets_max)/(sets_max*iter_max);
fprintf(fid, 'AverageA: %g\n', AverageA);
fprintf(fid, 'AverageB: %g ', AverageB);
fclose(fid);