-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamodel.m
440 lines (390 loc) · 18.7 KB
/
camodel.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
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
function camodel(breakThreshold, tumorChance, deathChance, mutationChance, metastasisChance)
% Close all existing figures
close all;
% Constant parameters
gridSize = 200;
initialTumorSize = 2;
neighborhoodSize = 1;
growhoodSize = 3;
vesselHumanizer = 0.5;
growChance = 1;
minTumorCellCountForMetastasis = 50;
treatment_time = [];
% Initialize grid
grid = ones(gridSize, gridSize);
% Initialize tumor
center = [floor(gridSize / 2), floor(gridSize / 2)];
[x, y] = meshgrid(1:gridSize, 1:gridSize);
circleMask = (x - center(1)).^2 + (y - center(2)).^2 <= (initialTumorSize / 2)^2;
grid(circleMask) = 2;
% Create blood vessel - rectangular shape starting from the edge
vesselWidth = 5;
vesselHeight = 150;
startRow = 1;
startCol = 140;
vesselMask = false(gridSize, gridSize);
vesselMask(startRow:(startRow + vesselHeight - 1), startCol:(startCol + vesselWidth - 1)) = true;
grid(vesselMask) = 5; % Assign a new value (e.g., 5) for blood vessel
% Create a random growth rate map
rng(1234); % For reproducibility
sigma = 1; % Standard deviation of the Gaussian filter
rawMap = imgaussfilt(rand(gridSize, gridSize), sigma); % Smooth the random values
% Normalize the map to the range [0, 1]
minVal = min(rawMap(:));
maxVal = max(rawMap(:));
polarizedMap = ((rawMap - minVal) / (maxVal - minVal)).^2;
% Transform the values based on the specified thresholds
growthRateMap = zeros(size(polarizedMap)); % Initialize the growth rate map
% Apply the thresholds
growthRateMap(polarizedMap > 0.6) = 0.02; % Values above 0.6 set to 1
growthRateMap(polarizedMap > 0.3 & polarizedMap <= 0.6) = 0.02; % Values between 0.3 and 0.6 set to 0.5
growthRateMap(polarizedMap <= 0.3) = 1; % Values below or equal to 0.3 set to 0
% Adjust tumor chance based on the growth rate map
adjustedTumorChance = tumorChance * growthRateMap;
% GUI setup
hFig = figure('Name', 'Cancer Simulation', 'NumberTitle', 'off', 'Position', [100, 100, 1200, 600]);
hAx = axes('Parent', hFig, 'Position', [0.05, 0.1, 0.4, 0.8]);
hText = uicontrol('Style', 'text', 'Parent', hFig, 'Position', [700, 300, 600, 200], 'FontSize', 12, 'HorizontalAlignment', 'left');
% Display initial parameters
paramText = sprintf(['Simulation Parameters:\n', ...
'Break Threshold: %f\n', ...
'Tumor Chance: %f\n', ...
'Death Chance: %f\n', ...
'Mutation Chance: %f\n', ...
'Metastasis Chance: %f\n'], breakThreshold, tumorChance, deathChance, mutationChance, metastasisChance);
set(hText, 'String', paramText);
% Visualization setup
colormap([1 1 1; 0 0 1; 1 0.75 0.7; 0.5 0 0; 1 0.75 0.7; 0.5 0.5 1]); % 2 is tumor, 3 is grown vessel, 5 is original vessel
% Simulation parameters
numIterations = 300;
numberOfBreaks = 0;
normalTumorCount = zeros(1, numIterations);
mutatedTumorCount = zeros(1, numIterations);
netSurvival = zeros(1, numIterations); % Initialize net survival array
maxDistance = abs(center(2) - startCol);
metastasis = 0;
% Initialize density map
densityMap = zeros(gridSize, gridSize);
% Define neighborhood offsets
[dx, dy] = meshgrid(-neighborhoodSize:neighborhoodSize, -neighborhoodSize:neighborhoodSize);
neighborhoodOffsets = [dx(:), dy(:)];
neighborhoodOffsets(all(neighborhoodOffsets == 0, 2), :) = [];
% Define growhood offsets
[gx, gy] = meshgrid(-growhoodSize:growhoodSize, -growhoodSize:growhoodSize);
growhoodOffsets = [gx(:), gy(:)];
growhoodOffsets(all(growhoodOffsets == 0, 2), :) = [];
% Run simulation
for iteration = 1:numIterations
% Visualization
axes(hAx);
image(grid);
set(gca, 'Color', [1 1 1]); % Set background to white
title(['Iteration: ' num2str(iteration)]);
drawnow;
% Update grid
newGrid = grid;
% Tumor growth
growMask = false(size(grid));
for i = 1:gridSize
for j = 1:gridSize
if grid(i, j) == 2 || grid(i, j) == 6
% Update density map
densityMap(i, j) = densityMap(i, j) + 1;
neighbors = bsxfun(@plus, neighborhoodOffsets, [i, j]);
validNeighbors = neighbors(all(neighbors > 0 & neighbors <= gridSize, 2), :);
for n = 1:size(validNeighbors, 1)
ni = validNeighbors(n, 1);
nj = validNeighbors(n, 2);
if grid(ni, nj) == 1
% Check if adjacent to blood vessel
adjacentToBloodVessel = any(grid(sub2ind(size(grid), validNeighbors(:,1), validNeighbors(:,2))) == 3 | grid(sub2ind(size(grid), validNeighbors(:,1), validNeighbors(:,2))) == 5);
if adjacentToBloodVessel
growthChance = adjustedTumorChance(ni, nj) * 1.5; % Increase growth rate near blood vessels
else
growthChance = adjustedTumorChance(ni, nj);
end
if rand <= growthChance
growMask(ni, nj) = true;
end
end
end
end
% Create a break
neighborhood = grid(max(1, i-neighborhoodSize):min(gridSize, i+neighborhoodSize), max(1, j-neighborhoodSize):min(gridSize, j+neighborhoodSize));
emptySpace = sum(neighborhood(:) == 1);
tumorCount = sum(neighborhood(:) == 2) + sum(neighborhood(:) == 6);
if grid(i,j) == 5 && emptySpace ~= 0 && (i < 160 && i > 40)
if numberOfBreaks == 0 && rand <= breakThreshold
newGrid(i,j) = 4;
numberOfBreaks = numberOfBreaks + 1;
elseif rand <= breakThreshold / (10 * numberOfBreaks)
newGrid(i,j) = 4;
numberOfBreaks = numberOfBreaks + 1;
end
end
% Select the closest tumor cell to grow to
tumorDistance = 1e+30;
if grid(i,j) == 4 && tumorCount == 0
for a = 1:gridSize
for b = 1:gridSize
if grid(a,b) == 2 || grid(a,b) == 6
tempDistance = sqrt((a - i)^2 + (b - j)^2);
if tempDistance < tumorDistance
tumorA = a;
tumorB = b;
tumorDistance = tempDistance;
end
end
end
end
end
% Grow towards the closest tumor cell
growDistance = 1e+30;
growC = 0;
growD = 0;
if grid(i,j) == 4 && rand <= growChance && tumorCount == 0
for c = i-1:i+1
for d = j-1:j+1
tempGrowDistance = sqrt((c - tumorA)^2 + (d - tumorB)^2);
if tempGrowDistance < growDistance
growDistance = tempGrowDistance;
growC = c;
growD = d;
end
end
end
if rand <= vesselHumanizer
growC = i + randi([-1, 1]);
growD = j + randi([-1, 1]);
end
bloodVesselWidth = floor(5 * abs(j - center(2)) / maxDistance);
for w = -bloodVesselWidth:bloodVesselWidth
newGrid(i + w, j) = 3;
end
% Make sure it doesn't grow outside the box
if growC < 1
growC = 1;
elseif growC > 200
growC = 200;
end
if growD < 1
growD = 1;
elseif growD > 200
growD = 200;
end
newGrid(growC, growD) = 4;
end
% Stop if touch tumor
if grid(i,j) == 4 && tumorCount ~= 0
newGrid(i, j) = 3;
metastasis = 1;
end
end
end
newGrid(growMask) = 2;
% Mutate
mutateMask = grid == 2 & rand(gridSize, gridSize) <= mutationChance;
newGrid(mutateMask) = 6;
% Drug administration
if ismember(iteration, treatment_time)
deathMask = (grid == 2 & rand(gridSize, gridSize) <= deathChance) | ...
(grid == 6 & rand(gridSize, gridSize) <= deathChance / 2);
newGrid(deathMask) = 1;
end
% Metastasis
if sum(grid(:) == 2) + sum(grid(:) == 6) >= minTumorCellCountForMetastasis && metastasis == 1
numMetastasisSites = randi(3); % Random number of metastasis sites (between 1 and 3)
for n = 1:numMetastasisSites
[metaRow, metaCol] = find(grid == 5 | grid == 3); % Find blood vessel cells
adjacentMask = false(size(grid));
for k = 1:length(metaRow)
neighbors = bsxfun(@plus, neighborhoodOffsets, [metaRow(k), metaCol(k)]);
validNeighbors = neighbors(all(neighbors > 0 & neighbors <= gridSize, 2), :);
for m = 1:size(validNeighbors, 1)
ni = validNeighbors(m, 1);
nj = validNeighbors(m, 2);
if grid(ni, nj) == 1 && rand <= metastasisChance
adjacentMask(ni, nj) = true;
end
end
end
[adjacentRows, adjacentCols] = find(adjacentMask);
if ~isempty(adjacentRows)
chosenIdx = randi(length(adjacentRows));
newGrid(adjacentRows(chosenIdx), adjacentCols(chosenIdx)) = 2;
end
end
end
% Update the grid
grid = newGrid;
% Update tumor counts
normalTumorCount(iteration) = sum(grid(:) == 2);
mutatedTumorCount(iteration) = sum(grid(:) == 6);
% Update net survival percentage
totalTumorCells = normalTumorCount(iteration) + mutatedTumorCount(iteration);
netSurvival(iteration) = calculateSurvival(totalTumorCells);
overviewText = sprintf(['Simulation Parameters:\n', ...
'Break Threshold: %f\n', ...
'Tumor Chance: %f\n', ...
'Death Chance: %f\n', ...
'Mutation Chance: %f\n', ...
'Metastasis Chance: %f\n\n', ...
'Iteration: %d\n', ...
'Normal Tumor Cells: %d\n', ...
'Mutated Tumor Cells: %d\n', ...
'Net Survival: %.2f%%\n'], breakThreshold, tumorChance, deathChance, mutationChance, metastasisChance, iteration, normalTumorCount(iteration), mutatedTumorCount(iteration), netSurvival(iteration));
set(hText, 'String', overviewText);
if iteration == 150
% Plot density heatmap
figure;
set(gcf, 'Color', 'w'); % Set background to white
sigma = 5; % Standard deviation for the Gaussian kernel
smoothedDensityMap = imgaussfilt(densityMap, sigma);
% Normalize the density values to [0, 1]
normalizedDensityMap = smoothedDensityMap;
minVal = min(normalizedDensityMap(:));
maxVal = max(normalizedDensityMap(:));
normalizedDensityMap = (normalizedDensityMap - minVal) / (maxVal - minVal);
% Apply custom colormap
imagesc(normalizedDensityMap);
% Define the custom colormap
customCmap = [
1 1 1; % White for the bottom 2%
0.22 0.21 0.87; % Blue
0.71 0.93 0.53; % Green
0.69 0.27 0.17 % Red
];
% Number of colors
nColors = size(customCmap, 1);
% Interpolate colormap
cmapInterp = interp1(linspace(0, 1, nColors), customCmap, linspace(0, 1, 256));
% Apply the colormap
colormap(cmapInterp);
colorbar;
% Set the color axis limits
caxis([0, 1]);
% Define custom tick marks and labels
colorTicks = [0.02, 0.34, 0.66, 1]; % 2%, 34%, 66%, and 100%
colorbar('Ticks', colorTicks, 'TickLabels', {'2%', '34%', '66%', '100%'});
% Add title and axis labels
xlabel('X', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Y', 'FontSize', 12, 'FontWeight', 'bold');
title('Tumor Cell Density Heatmap for Day 0', 'FontSize', 14, 'FontWeight', 'bold');
pause;
end
if iteration == 240
% Plot density heatmap
figure;
set(gcf, 'Color', 'w'); % Set background to white
sigma = 5; % Standard deviation for the Gaussian kernel
smoothedDensityMap = imgaussfilt(densityMap, sigma);
% Normalize the density values to [0, 1]
normalizedDensityMap = smoothedDensityMap;
minVal = min(normalizedDensityMap(:));
maxVal = max(normalizedDensityMap(:));
normalizedDensityMap = (normalizedDensityMap - minVal) / (maxVal - minVal);
% Apply custom colormap
imagesc(normalizedDensityMap);
% Define the custom colormap
customCmap = [
1 1 1; % White for the bottom 2%
0.22 0.21 0.87; % Blue
0.71 0.93 0.53; % Green
0.69 0.27 0.17 % Red
];
% Number of colors
nColors = size(customCmap, 1);
% Interpolate colormap
cmapInterp = interp1(linspace(0, 1, nColors), customCmap, linspace(0, 1, 256));
% Apply the colormap
colormap(cmapInterp);
colorbar;
% Set the color axis limits
caxis([0, 1]);
% Define custom tick marks and labels
colorTicks = [0.02, 0.34, 0.66, 1]; % 2%, 34%, 66%, and 100%
colorbar('Ticks', colorTicks, 'TickLabels', {'2%', '34%', '66%', '100%'});
% Add title and axis labels
xlabel('X', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Y', 'FontSize', 12, 'FontWeight', 'bold');
title('Tumor Cell Density Heatmap for Day 3', 'FontSize', 14, 'FontWeight', 'bold');
pause;
end
end
% Save tumor counts to CSV
tumorData = table((1:numIterations)', normalTumorCount', mutatedTumorCount', 'VariableNames', {'Iteration', 'NormalTumorCount', 'MutatedTumorCount'});
writetable(tumorData, 'tumor_counts.csv');
% Plot tumor growth over time
figure;
set(gcf, 'Color', 'w'); % Set background to white
hold on; % Hold on to add multiple lines to the same plot
plot(1:numIterations, normalTumorCount, 'b-', 'LineWidth', 2);
plot(1:numIterations, mutatedTumorCount, 'r-', 'LineWidth', 2);
hold off; % Release the hold
legend('Normal Tumor Cells', 'Mutated Tumor Cells');
xlabel('Iteration', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Number of Cells', 'FontSize', 12, 'FontWeight', 'bold');
title('Tumor Growth Over Time', 'FontSize', 14, 'FontWeight', 'bold');
% Plot net survival over time with error range
figure;
set(gcf, 'Color', 'w'); % Set background to white
% Calculate the error range (example: 10% of the tumor cell count)
errorRange = 0.001 * (normalTumorCount + mutatedTumorCount);
% Plot the survival curve
plot(1:numIterations, netSurvival, 'r-', 'LineWidth', 2);
hold on;
% Plot the error range
fill([1:numIterations, fliplr(1:numIterations)], ...
[netSurvival - errorRange, fliplr(netSurvival + errorRange)], ...
'r', 'FaceAlpha', 0.3, 'EdgeColor', 'none');
% Set y-axis limits
ylim([0, 100]);
xlabel('Iteration', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Net Survival Percentage (%)', 'FontSize', 12, 'FontWeight', 'bold');
title('Net Survival Percentage Over Time', 'FontSize', 14, 'FontWeight', 'bold');
hold off;
% Plot final density heatmap
figure;
set(gcf, 'Color', 'w'); % Set background to white
sigma = 5; % Standard deviation for the Gaussian kernel
smoothedDensityMap = imgaussfilt(densityMap, sigma);
% Normalize the density values to [0, 1]
normalizedDensityMap = smoothedDensityMap;
minVal = min(normalizedDensityMap(:));
maxVal = max(normalizedDensityMap(:));
normalizedDensityMap = (normalizedDensityMap - minVal) / (maxVal - minVal);
% Apply custom colormap
imagesc(normalizedDensityMap);
% Define the custom colormap
customCmap = [
1 1 1; % White for the bottom 2%
0.22 0.21 0.87; % Blue
0.71 0.93 0.53; % Green
0.69 0.27 0.17 % Red
];
% Number of colors
nColors = size(customCmap, 1);
% Interpolate colormap
cmapInterp = interp1(linspace(0, 1, nColors), customCmap, linspace(0, 1, 256));
% Apply the colormap
colormap(cmapInterp);
colorbar;
% Set the color axis limits
caxis([0, 1]);
% Define custom tick marks and labels
colorTicks = [0.02, 0.34, 0.66, 1]; % 2%, 34%, 66%, and 100%
colorbar('Ticks', colorTicks, 'TickLabels', {'2%', '34%', '66%', '100%'});
% Add title and axis labels
xlabel('X', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Y', 'FontSize', 12, 'FontWeight', 'bold');
title('Tumor Cell Density Heatmap for Day 5', 'FontSize', 14, 'FontWeight', 'bold');
end
function netSurvival = calculateSurvival(totalTumorCells)
% Parameters for the survival model
initialSurvivalRate = 100; % 100% initial survival rate
maxTumorCells = 20000; % Maximum number of tumor cells where survival rate drops to 0%
% Calculate net survival rate
netSurvival = initialSurvivalRate * (1 - totalTumorCells / maxTumorCells);
% Ensure survival does not go below 0%
netSurvival(netSurvival < 0) = 0;
end