-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_tissues.m
273 lines (262 loc) · 11.2 KB
/
simulate_tissues.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
datadir = '' % Edit file path;
%datadir = find_datadir();
const = struct('L',1,'M',2,'C',3,'B',4,'X',5,'EL',6, ...
'LL',1,'LM',2,'LX',3,'MM',4,'MX',5);
%% Quantify boundary sites ------------------------------------------------
% This section makes a bunch of tissues and writes down how many different
% boundary site types there are (classified by total exposed area). I used
% it to calculate multiplicity.
tic
radii = 5:2:19; nrows = 0; data = zeros(length(radii)*2,5);LF=0.5;
for radius = radii
tissue = make_tissue_cube(radius, LF, const);
% where are the CX edges? exclude XC.
CXareas = sum(tissue.edges.areas .* ((tissue.edges.types(:,:,const.C) | tissue.edges.types(:,:,const.X)) ...
& (tissue.is(:,const.B) & tissue.is(:,const.X)')),2);
CXareas = round(CXareas(tissue.is(:,const.C)),6,'significant');
% categorize boundary cells by amount of CX edges
% Quantifying the number of cells with different ECM edge areas
[CXareacounts, CXareavals] = groupcounts(CXareas); nadd = length(CXareacounts);
% tissue stats
nC = sum(tissue.is(:,const.C)); nB = sum(tissue.is(:,const.B));
if nrows + nadd > size(data,1)
data = [data; zeros(nrows+nadd, 5)];
end
data((nrows+1):(nrows+nadd),:) = [repmat([radius, nC, nB],nadd,1), CXareavals, CXareacounts];
nrows = nrows + nadd;
end
dataTable = array2table(data(1:nrows,:),'VariableNames',...
{'r','n','nB','edgeArea','nC'});
writetable(dataTable, fullfile(datadir, 'bcc_areacounts.csv'));
radii = 5:2:19; nrows = 0; data = zeros(length(radii)*2,5);LF=0.5;
for radius = radii
tissue = make_tissue_2Dhex(radius, LF, const);
% where are the CX edges? exclude XC.
CXareas = sum(tissue.edges.areas .* ((tissue.edges.types(:,:,const.C) | tissue.edges.types(:,:,const.X)) ...
& (tissue.is(:,const.B) & tissue.is(:,const.X)')),2);
CXareas = round(CXareas(tissue.is(:,const.C)),6,'significant');
% categorize boundary cells by amount of CX edges
% Quantifying the number of cells with different ECM edge areas
[CXareacounts, CXareavals] = groupcounts(CXareas); nadd = length(CXareacounts);
% tissue stats
nC = sum(tissue.is(:,const.C)); nB = sum(tissue.is(:,const.B));
if nrows + nadd > size(data,1)
data = [data; zeros(nrows+nadd, 5)];
end
data((nrows+1):(nrows+nadd),:) = [repmat([radius, nC, nB],nadd,1), CXareavals, CXareacounts];
nrows = nrows + nadd;
end
dataTable = array2table(data(1:nrows,:),'VariableNames',...
{'r','n','nB','edgeArea','nC'});
writetable(dataTable, fullfile(datadir, 'hex_areacounts.csv'));
%% Random sampling multiplicity ------------------------------------------
% Default: R=9 (134 boundary, 147 core) or R=7 (66 boundary, 33 core) or
% Generate a large number of tissues (1e6) by random placement of LEP and MEP
% Calculate the distribution of edge LEPs
tic
LFs = [0.5, 0.25, 0.75]; % LEP fraction
n_iter = 1e5; % number of tissues to make
radii = [9];
andXS = false;
tissuevarnames = {'r','n','LF','nB','iteration', ...
'nEL','LL_sq','LL_hex','LM_sq','LM_hex','LX_sq','LX_hex',...
'MM_sq','MM_hex','MX_sq','MX_hex'};
% Save: r, n, LF, iteration // areas(x10)
for radius = radii
tissue = make_tissue_cube(radius, LFs(1), const);
for LF = LFs
tissuestats = zeros(n_iter, 5+11);
tissuestats(:, 1:5) = ...
[repmat([radius, tissue.n(const.C), LF,tissue.n(const.B)], n_iter, 1), (1:n_iter)'];
fprintf('Starting to quantify %d random tissues', n_iter);
for iter = 1:n_iter
tissue = reset_tissue(tissue, LF);
stats = quantify_tissue(tissue, andXS);
tissuestats(iter, 6:end) = stats;
if mod(iter, n_iter/100) == 0
fprintf('.');
end
end
fprintf('%s\n', tocstring());
tissueTable = array2table(tissuestats, 'VariableNames', ...
tissuevarnames);
writetable(tissueTable, fullfile(datadir, sprintf('randoms_r%d_LF%g_n%d.csv',radius, LF, n_iter)));
end
end
%% Sample tissues with defined LEP boundary occupancy ------------------------------------------
% Make default tissue (R=9, LF=0.5)
tic
n_iter = 1e2;
radii =[7];
LFs = [0.5];
andXS = false;
tissuevarnames = {'r','n','LF','nB','iteration', ...
'nEL','LL_sq','LL_hex','LM_sq','LM_hex','LX_sq','LX_hex',...
'MM_sq','MM_hex','MX_sq','MX_hex'};
%tELs = [0.1:0.1:0.9];
tELs = [0];
for radius = radii
% make tissue
tissue = make_tissue_cube(radius, LFs(1), const);
c = tissue.const;
for LF = LFs
% Calculate Lb_min and Lb_max
tissue = reset_tissue(tissue, LF);
nC = tissue.n(c.C);
nL = tissue.n(c.L);
nB = tissue.n(c.B);
tEL_min = ceil(max(0,nL-nC+nB)*10/nB)/10;
tEL_max = floor(min(nB, nL)*10/nB)/10;
% get new range of tELs in accessible range
tELs = [tEL_min:0.1:tEL_max];
% intitialize array of #tEL*n_iter rows
tissuestats = zeros(n_iter*length(tELs), 5+11);
tissuestats(:, 1:5) = ...
[repmat([radius, tissue.n(const.C), LF,tissue.n(const.B)], n_iter*length(tELs), 1), (repmat(1:n_iter,1,length(tELs)))'];
fprintf('Quantify tissues with LF %d', LF);
fprintf('%s\n', tocstring());
counter = 0;
for tEL = tELs
% Make tissues for given tEL
fprintf('Quantifying %d random tissues with EL %d', n_iter,tEL);
for iter = 1:n_iter
tissue = reset_tissue(tissue, LF, tEL);
stats = quantify_tissue(tissue, andXS);
tissuestats(counter*n_iter+iter, 6:end) = stats;
if mod(iter, n_iter/10) == 0
fprintf('.');
end
end
counter = counter + 1;
fprintf('%s\n', tocstring());
end % tEL loop
tissueTable = array2table(tissuestats, 'VariableNames', ...
tissuevarnames);
writetable(tissueTable, fullfile(datadir, sprintf('targeted_r%d_LF%g_n%d.csv',radius, LF, n_iter)));
end % LF loop
end % radius loop
%% 2D hexagonal lattice Sample tissues with defined LEP boundary occupancy ------------------------------------------
% Make default tissue (R=5, LF=0.5)
tic
n_iter = 1e3;
radii =[7];
LFs = [0.5];
andXS = false; is2D = true;
tissuevarnames = {'r','n','LF','nB','iteration', ...
'nEL','LL_area','LM_area','LX_area',...
'MM_area','MX_area'};
%tELs = [0.1:0.1:0.9];
tELs = [0:0.25:0.75];
for radius = radii
% make tissue
tissue = make_tissue_2Dhex(radius, LFs(1), const);
c = tissue.const;
for LF = LFs
% Calculate Lb_min and Lb_max
tissue = reset_tissue(tissue, LF);
nC = tissue.n(c.C);
nL = tissue.n(c.L);
nB = tissue.n(c.B);
tEL_min = ceil(max(0,nL-nC+nB)*10/nB)/10;
tEL_max = floor(min(nB, nL)*10/nB)/10;
% get new range of tELs in accessible range
tELs = [tEL_min:0.1:tEL_max];
% intitialize array of #tEL*n_iter rows
tissuestats = zeros(n_iter*length(tELs), 5+6);
tissuestats(:, 1:5) = ...
[repmat([radius, tissue.n(const.C), LF,tissue.n(const.B)], n_iter*length(tELs), 1), (repmat(1:n_iter,1,length(tELs)))'];
fprintf('Quantify tissues with LF %d', LF);
fprintf('%s\n', tocstring());
counter = 0;
for tEL = tELs
% Make tissues for given tEL
fprintf('Quantifying %d random tissues with EL %d', n_iter,tEL);
for iter = 1:n_iter
tissue = reset_tissue(tissue, LF, tEL);
stats = quantify_tissue(tissue, andXS, is2D);
tissuestats(counter*n_iter+iter, 6:end) = stats;
if mod(iter, n_iter/10) == 0
fprintf('.');
end
end
counter = counter + 1;
fprintf('%s\n', tocstring());
end % tEL loop
tissueTable = array2table(tissuestats, 'VariableNames', ...
tissuevarnames);
writetable(tissueTable, fullfile(datadir, sprintf('hex_targeted_r%d_LF%g_n%d.csv',radius, LF, n_iter)));
end % LF loop
end % radius loop
%% Examples of 2D hexagonal lattice tissues with defined LEP boundary occupancy ------------------------------------------
% Make default tissue (R=5, LF=0.5)
tic
n_iter = 10;
radii =[7];
LFs = [0.55];
andXS = false; is2D = true;
tissuevarnames = {'r','n','LF','nB','iteration', ...
'nEL','LL_area','LM_area','LX_area',...
'MM_area','MX_area'};
%tELs = [0.1:0.1:0.9];
tELs = [0:0.1:0.5];
for radius = radii
% make tissue
tissue = make_tissue_2Dhex(radius, LFs(1), const);
c = tissue.const;
for LF = LFs
% Calculate Lb_min and Lb_max
tissue = reset_tissue(tissue, LF);
nC = tissue.n(c.C);
nL = tissue.n(c.L);
nB = tissue.n(c.B);
tEL_min = ceil(max(0,nL-nC+nB)*10/nB)/10;
tEL_max = floor(min(nB, nL)*10/nB)/10;
% get new range of tELs in accessible range
%tELs = [tEL_min:0.1:tEL_max];
tELs = [0,0.1,0.5];
% intitialize array of #tEL*n_iter rows
tissuestats = zeros(n_iter*length(tELs), 5+6);
tissuestats(:, 1:5) = ...
[repmat([radius, tissue.n(const.C), LF,tissue.n(const.B)], n_iter*length(tELs), 1), (repmat(1:n_iter,1,length(tELs)))'];
fprintf('Quantify tissues with LF %d', LF);
fprintf('%s\n', tocstring());
counter = 0;
for tEL = tELs
% Make tissues for given tEL
fprintf('Quantifying %d random tissues with EL %d', n_iter,tEL);
for iter = 1:n_iter
tissue = reset_tissue(tissue, LF, tEL);
stats = quantify_tissue(tissue, andXS, is2D);
tissuestats(counter*n_iter+iter, 6:end) = stats;
if mod(iter, n_iter/10) == 0
fprintf('.');
end
plot_idx2D(tissue);
exportgraphics(gcf, fullfile(datadir,'examples', ...
sprintf('hex_r%d_LF%g_EL%g_n%d.pdf',radius, LF, tEL, iter)), 'ContentType', 'vector');
end
counter = counter + 1;
fprintf('%s\n', tocstring());
end % tEL loop
tissueTable = array2table(tissuestats, 'VariableNames', ...
tissuevarnames);
writetable(tissueTable, fullfile(datadir, sprintf('hex_r%d_LF%g_n%d.csv',radius, LF, n_iter)));
end % LF loop
end % radius loop
%% Make scatter plot with centers as circles
c = tissue.const;
colorL = [255, 185, 15] / 255; colorM = [100, 30, 100] / 255; colorX = [255, 255, 255] / 255;
colors = colorM .* tissue.is(:,c.M) + colorL .* tissue.is(:,c.L) + colorX .* tissue.is(:,c.X);
colors = colors(~tissue.is(:,c.X),:);
figure(1);
clf;
ax = gca; ax.XColor = 'none'; ax.YColor = 'none'; ax.ZColor = 'none';
ax.Clipping = 'off';
set(gcf,'position',[20,20,100,100])
cells = tissue.p(~tissue.is(:,c.X),:);
scatter(cells(:,1), cells(:,2), 60,colors,"filled");
pbaspect([1 1 1]); daspect([1 1 1]);
axis off;
%scatter(tissue.p(:,1), tissue.p(:,2), 60,colors,"filled");
exportgraphics(gcf, fullfile(datadir,'examples', ...
sprintf('test_r%d_LF%g_EL%g_n%d.pdf',radius, LF, tEL, iter)), 'ContentType', 'vector');