-
Notifications
You must be signed in to change notification settings - Fork 2
/
islet_segmentation_pipeline.m
167 lines (136 loc) · 6.54 KB
/
islet_segmentation_pipeline.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
set(0,'DefaultFigureWindowStyle','docked')
addpath 'islet_functions'
addpath 'islet_functions\export_fig'
% inpath = 'D:\SickKids\YuvalDor\mouse islet z-stacks\images\';
inpath = '\\carbon.research.sickkids.ca\rkafri\YuvalDor\mouse islet z-stacks\images\';
folders = { ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Control\0003\' ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Control\0005\' ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Control\0004\' ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Torin_wash\0000\' ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Torin_wash\0002\' ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Torin\0001\' ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Torin\0000\' ...
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Torin\0002\' ...
% ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\ctl_1-0000\ctl_1-0000\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_wash_1-0000\Torin_wash_1-0000\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_1-0006\Torin_1-0006\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\ctl_1-0001\ctl_1-0001\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\ctl_1-0002\ctl_1-0002\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\ctl_1-0004\ctl_1-0004\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\ctl_1-0005\ctl_1-0005\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_1-0000\Torin_1-0000\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_1-0003\Torin_1-0003\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_1-0004\Torin_1-0004\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_1-0005\Torin_1-0005\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_wash_1-0001\Torin_wash_1-0001\' ...
'2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_wash_1-0002\Torin_wash_1-0002\' ... % great
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_wash_1-0003\Torin_wash_1-0003\' ...
% '2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_wash_1-0005\Torin_wash_1-0005\' ... % great
};
%% REJECTED ISLETS
% REJECT 1) BAD Z-RESOLVING:
%'2nd image set\Whole_mount_cy5pdx_cy3E-cad_DAPI_140217\Torin_wash_1-0004\Torin_wash_1-0004\' ...
% REJECT 2)
% '3rd image set\Whole_mount_Islets_with_Torin_Cy5pdx_cy3E-cad_DAPI_080317\Torin_wash\0001\' ...
% Reason for rejection: Calculating the distance to center of islet is not supported for
% two islets, consider cropping the image into two seperate islet stacks.
ResultsTable = table(); % initialize empty table
for f=1:length(folders)
folder = folders(f)
%% LOAD IMAGES
[DAPI, ECad, PDX1] = LoadImages([inpath char(folder)]);
%% AUTO-CROP CYTO
[cyto, nuc, pdx1] = AutoCrop(ECad, DAPI, PDX1);
%% NORMALIZE BRIGHTNESS IN Z-DIMENSION
cyto_norm = NormalizeZ(cyto, 90);
nuc_norm = NormalizeZ(nuc, 90);
pdx1_norm = NormalizeZ(pdx1, 90);
% figure('name','ECad','NumberTitle', 'off'); imshow3Dfull(cyto_norm,[]);
% SMOOTH
cyto_smooth = SmoothIslet(cyto_norm, folder);
%% SEEDS
% Seeding algorithm #1: Find seeds in 3D using imhmin and imregionalmin on cytoplasm
seeds = Seeding1_cyto_imhmin_3D(cyto_norm, cyto_smooth);
% Alternative seeding algorithms go here:
% seeds = Seeding2_YOUR_NEW_ALGORITHM(cyto_norm, cyto_smooth);
%% SEGMENTATION
% Segmentation algorithm #2: Simple seed based watershed 3D
labelled_cyto = Segmentation2_watershed_3D(cyto_norm, cyto_smooth, seeds);
% Alternative seeding algorithms go here:
% Segmentation algorithm #1: enhance boundries with imerode then markerless 2D watershed
% labelled_cyto = Segmentation1_imerode_markerless_watershed_2D(cyto_norm);
%% MEASUREMENTS
newResults = NewMeasurements(cyto_norm, nuc_norm, pdx1_norm, labelled_cyto, folder);
% STORE RESULTS
ResultsTable = [ResultsTable; newResults];
end
save('ResultsTable.mat', 'ResultsTable');
save('AllVars.mat');
load('ResultsTable.mat');
%load('ResultsTable2D.mat'); ResultsTable2D = ResultsTable;
%load('ResultsTable3D.mat'); ResultsTable3D = ResultsTable;
%%
%% FILTERING BAD DATA SECTION
%%
% % Filter by solidity
% solidity_thresholds = [0.75 0.75 0.75 0.75 0.85 0.75 0.85 0.75];
% subsetTable = table();
% for img_id=img_ids
% newSubset = ResultsTable(find(strcmp(ResultsTable.Image,{['Image ', int2str(img_id)]})),:);
% subset_ids=newSubset.Solidity>solidity_thresholds(img_id);
% newSubset=newSubset(subset_ids,:);
% subsetTable = [subsetTable; newSubset];
% end
% % Filter very big objects
% max_cell_size = 10000;
% subsetTable=subsetTable(subsetTable.CellSize<10000,:);
% % Filter by reporter
% insulin_thresholds = [12 4.6 40 14 33 31 9 9];
% insulinTable = table();
% for img_id=img_ids
% subset_ids=subsetTable.ReporterIntensity>insulin_thresholds(img_id);
% newSubset=subsetTable(subset_ids,:);
% newSubset = newSubset(find(strcmp(newSubset.Image,{['Image ', int2str(img_id)]})),:);
% insulinTable = [insulinTable; newSubset];
% end
% % Filter cells touching Z-bottom
%%
%% GRAPHICS SECTION (plots and images)
%%
subsetTable = ResultsTable;
% Anova table and boxplot
figure
[p,t,stats] = anova1(subsetTable.CellSize,subsetTable.Stack);
set(gca,'FontSize',19)
% Bar chart (per stack)
Means = grpstats(subsetTable.CellSize,subsetTable.Stack,'mean');
Stds = grpstats(subsetTable.CellSize,subsetTable.Stack,'std');
Lngth = grpstats(subsetTable.CellSize,subsetTable.Stack,'numel');
figure
bar(Means)
hold on
errorbar(Means,Stds./sqrt(Lngth),'.r')
ylabel('Cell Area (pixel count)', 'FontSize', 21);
for i=1:length(Means)
text(i+0.06,Means(i)+30,int2str(Means(i)),'FontSize',20);
end
set(gca,'FontSize',19,'XTickLabel',unique(subsetTable.Stack),'stable')
% Bar chart (experimental classes)
Means = grpstats(subsetTable.CellSize,subsetTable.Experiment,'mean');
Stds = grpstats(subsetTable.CellSize,subsetTable.Experiment,'std');
Lngth = grpstats(subsetTable.CellSize,subsetTable.Experiment,'numel');
figure
bar(Means)
hold on
errorbar(Means,Stds./sqrt(Lngth),'.r')
ylabel('Cell Area (pixel count)', 'FontSize', 21);
for i=1:length(Means)
text(i+0.06,Means(i)+30,int2str(Means(i)),'FontSize',20);
end
set(gca,'FontSize',19,'XTickLabel',unique(subsetTable.Experiment),'stable')
%% SAVE GIFS TO DISK
Gif1_RGB_on_Cyto(subsetTable, inpath);
%% PLOTTING 2D vs 3D
%PlotBarChart2Dvs3D(ResultsTable2D, ResultsTable3D);