This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
forked from zelanolab/primaryolfactorycortexparcellation
/
Plot_Overlap_Manual_Parc.m
299 lines (240 loc) · 9.34 KB
/
Plot_Overlap_Manual_Parc.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
% overlap between manual ROIs and automatic parcellations
%% parameter setup for the permutation test
% Figure 2B
parfile = '~/Documents/RestfMRI/SourceData/Figure_2_sourcedata2/Figure_2_Parcellation_2mm.nii.gz';
roi = '~/Documents/RestfMRI/SourceData/Figure_2_sourcedata2/Figure_2_MNI152_ROI_ManualSegmentation_2mm.nii.gz';
roi_label = {'Aon', 'Tub', 'PirF', 'PirT'};
roi_label = fliplr( roi_label);
% 1-LR 2-L 3-R
hemi = 2:3;
% 'first' | 'last'
ref_label = 'last';
% 'normal' | 'reverse'
ydir = 'normal';
name_prefix = 'Figure2C';
% Figure 3, K=3-6
roi = '~/Documents/RestfMRI/SourceData/Figure_3_source_data/Figure_3_MNI152_ROI_ManualSegmentation_2mm_LRMerged.nii.gz';
parfile = '~/Documents/RestfMRI/SourceData/Figure_3_source_data/Figure_3_K6_MNI152_ROI_2mm.nii.gz';
roi_label = {'LAon', 'RAon', 'LTub', 'TRub', 'LPirF', 'RPirF', 'LPirT', 'RPirT'};
hemi = 1;
% 'first' | 'last'
ref_label = 'first';
% 'normal' | 'reverse'
ydir = 'reverse';
name_prefix = 'Figure3';
% Supplementary Figure 1B
roi = '~/Documents/RestfMRI/SourceData/Supplementary_Figure_1_sourcedata/Suppl_Figure1_Manual_Segmentation_v2_2mm.nii.gz';
roi_label = {'Aon', 'Tub', 'PirF', 'PirT'};
parfile = '~/Documents/RestfMRI/SourceData/Supplementary_Figure_1_sourcedata/Suppl_Figure1_Parcellation_v2_2mm.nii.gz';
hemi = 2:3;
% 'first' | 'last'
ref_label = 'first';
% 'normal' | 'reverse'
ydir = 'reverse';
name_prefix = 'Figure2_figure_supplement1AB';
% Supplementary Figure 1D
roi = '~/Documents/RestfMRI/SourceData/Figure_2_sourcedata2/Figure_2_MNI152_ROI_ManualSegmentation_2mm.nii.gz';
roi_label = {'Aon', 'Tub', 'PirF', 'PirT'};
roi_label = fliplr( roi_label);
parfile = '~/Documents/RestfMRI/SourceData/Supplementary_Figure_1_sourcedata/SupplFigure1_TN_Parcellation_2mm.nii.gz';
hemi = 2:3;
% 'first' | 'last'
ref_label = 'last';
% 'normal' | 'reverse'
ydir = 'normal';
name_prefix = 'Figure2_figure_supplement1CD';
%% code to do the permutation test
figdir = '~/Documents/RestfMRI/Figures';
% permutation test
nbpermuts = 10000;
% % 'first' | 'last'
% ref_label = 'first';
% % 'normal' | 'reverse'
% ydir = 'reverse';
roi_vol = MRIread( roi);
roi_vol.vol = permute( roi_vol.vol, [2, 1, 3]);
par_vol = MRIread( parfile);
par_vol.vol = permute( par_vol.vol, [2, 1, 3]);
% 1:N, left; N:2*N, right.
N = floor( size( roi_vol.vol, 1) / 2);
roi_vals = unique( roi_vol.vol(:));
roi_vals( roi_vals == 0) = [];
nb_roi_vals = length( roi_vals);
par_vals = unique( par_vol.vol(:));
par_vals( par_vals == 0 ) = [];
nb_par_vals = length( par_vals);
% switch parcellation orders, hopefully this works
% antomical x parcellation, roi x par x hemi (all, left, right)
nb_overlaps = zeros( nb_roi_vals, nb_par_vals, 3);
reorder_nb_overlap = zeros( nb_roi_vals, nb_par_vals, 3);
hemi_name = {'LR', 'Left', 'Right'};
% this automic sorting method only works when the number of anatomical ROIs
% is smaller than the number of parcellations
figure;
SetPrintProp( gcf, 0.4, 0.6);
SetPrintProp( gcf, 0.2, 0.4)
sort_par_vals = cell( 3, 1);
for hemi_idx = hemi
tmp_roi_vol = roi_vol.vol;
tmp_par_vol = par_vol.vol;
if hemi_idx == 2
tmp_roi_vol( 1:N, :, :) = 0;
tmp_par_vol( 1:N, :, :) = 0;
elseif hemi_idx == 3
tmp_roi_vol( N+1:end, :, :) = 0;
tmp_par_vol( N+1:end, :, :) = 0;
end
if sum( tmp_roi_vol(:) ~= 0) ~= sum( tmp_par_vol(:) ~=0)
error( 'The anatomical template and parcellation results are not matched.');
end
for par_idx = 1 : nb_par_vals
cur_par_val = par_vals( par_idx);
nb_par_voxs = sum( tmp_par_vol(:) == cur_par_val);
for roi_idx = 1 : nb_roi_vals
cur_roi_val = roi_vals( roi_idx);
tmp = tmp_par_vol == cur_par_val & tmp_roi_vol == cur_roi_val;
nb_overlaps( roi_idx, par_idx, hemi_idx) = sum( tmp(:)) / nb_par_voxs;
end
end
switch lower( ref_label)
case 'last'
ref_row = nb_roi_vals;
case 'first'
ref_row = 1;
otherwise
error('Invalid reference label.');
end
[mval, midx] = max( nb_overlaps( :, :, hemi_idx), [], 1);
[~, start_col] = min( abs( ref_row - midx));
% old -> new column index
last_row = [];
last_row( 1, 1:2) = [ref_row, 1];
last_col = [];
last_col( 1, 1:2) = [start_col, 1];
for k = 2 : nb_roi_vals
tmp = nb_overlaps( :, :, hemi_idx);
tmp( :, last_col( :, 1)) = -1;
[tmp_mval, tmp_midx] = max( tmp, [], 1);
% row next to the last one
tmp_midx_diff = abs( last_row( k-1, 1) - tmp_midx);
tmp_midx_diff( last_col( :, 1)) = nb_par_vals * 2;
[min_val, min_ind] = min( tmp_midx_diff);
last_row( k, 1) = tmp_midx( min_ind);
last_row( k, 2) = k;
if k <= nb_par_vals
last_col( k, 1) = min_ind;
last_col( k, 2) = k;
end
end
% if nb_roi_vals > nb_par_vals
if nb_roi_vals < nb_par_vals
v = (1 : nb_par_vals)';
v( ismember( v, last_col( :, 1))) = [];
loc = [];
loc( :, 1) = v;
loc( :, 2) = size( last_col, 1) + 1 : length( tmp);
last_col = cat( 1, last_col, loc);
end
sort_par_vals{ hemi_idx} = last_col( :, 1);
reorder_nb_overlap( :, :, hemi_idx) = nb_overlaps( :, last_col( :, 1), hemi_idx);
xticklabel = num2cell( 1:nb_par_vals);
xticklabel = cellfun( @(x) ['C', num2str(x)], xticklabel, 'UniformOutput', false);
subplot( 3, 2, 2*(hemi_idx-1) + 1);
imagesc( nb_overlaps( :, :, hemi_idx));
title( ['Unsorted ', hemi_name{ hemi_idx}]);
set( gca, 'ytick', 1:nb_roi_vals,...
'yticklabels', roi_label,...
'xtick', 1:nb_par_vals,...
'xticklabel', xticklabel,...
'ydir', ydir, 'ticklength', [0, 0]);
colorbar;
caxis( [0, 1]);
subplot( 3, 2, 2*(hemi_idx-1) + 2);
imagesc( 100* reorder_nb_overlap( :, :, hemi_idx));
title( ['Sorted ', hemi_name{ hemi_idx}]);
set( gca, 'ytick', 1:nb_roi_vals,...
'yticklabels', roi_label,...
'xtick', 1:nb_par_vals,...
'xticklabel', xticklabel, 'ydir', ydir, 'ticklength', [0, 0]);
ylabel( 'Manual ROI'); xlabel( 'Parcellation');
c = colorbar;
caxis( [0, 100]);
set( c, 'ticks', 0:25:100);
xlabel( c, 'Percentage (%)');
end
[rb, r,b] = CustomColormap;
colormap( r);
permut_nb_overlaps = zeros( nb_roi_vals, nb_par_vals, 3, nbpermuts+1);
zscore = nan( nb_roi_vals, nb_par_vals, 3);
for hemi_idx = hemi
tmp_roi_vol = roi_vol.vol;
tmp_par_vol = par_vol.vol;
if hemi_idx == 2
tmp_roi_vol( 1:N, :, :) = 0;
tmp_par_vol( 1:N, :, :) = 0;
elseif hemi_idx == 3
tmp_roi_vol( N+1:end, :, :) = 0;
tmp_par_vol( N+1:end, :, :) = 0;
end
for pidx = 1 : nbpermuts + 1
fprintf( 'Run %d/%d, Permutation: %d/%d \r', hemi_idx, 3, pidx, nbpermuts+1);
if pidx > 1
tmp_loc = find( tmp_roi_vol(:));
tmp_val = tmp_roi_vol( tmp_loc);
tmp_roi_vol( tmp_loc) = tmp_val( randperm( length( tmp_loc)));
end
for par_idx = 1 : nb_par_vals
cur_par_val = sort_par_vals{ hemi_idx}( par_idx);
nb_par_voxs = sum( tmp_par_vol(:) == cur_par_val);
for roi_idx = 1 : nb_roi_vals
cur_roi_val = roi_vals( roi_idx);
tmp = tmp_par_vol == cur_par_val & tmp_roi_vol == cur_roi_val;
permut_nb_overlaps( roi_idx, par_idx, hemi_idx, pidx) = sum( tmp(:)) / nb_par_voxs;
end
end
end
fprintf( '\n');
[avg, sd] = normfit( permute( squeeze( permut_nb_overlaps( :, :, hemi_idx, 2:end)), [3 1 2]));
avg = squeeze( avg);
sd = squeeze( sd);
zscore( :, :, hemi_idx) = ( permut_nb_overlaps( :, :, hemi_idx, 1) - avg) ./ sd;
end
% FDR on left and right
tmp_z = zscore( :, :, hemi);
tmp_pval = G_z2p( tmp_z);
fdrp = fdr( tmp_pval(:), 0.001);
figure;
SetPrintProp( gcf, 0.4, 0.6);
SetPrintProp( gcf, 0.2, 0.4)
for hemi_idx = hemi
subplot( 3, 2, 2*(hemi_idx-1) + 1);
imagesc( 100* permut_nb_overlaps( :, :, hemi_idx, 1));
title( ['Sorted ', hemi_name{ hemi_idx}]);
set( gca, 'ytick', 1:nb_roi_vals, 'yticklabels', roi_label, 'xtick', 1:nb_par_vals, 'xticklabel', xticklabel, 'ydir', ydir,...
'ticklength', [0, 0], 'fontsize', 6);
ylabel( 'Manual ROI'); xlabel( 'Parcellation');
xtickangle( 45);
c = colorbar;
caxis( [0, 100]);
set( c, 'Limits', [0, 100], 'ticks', 0:25:100);
xlabel( c, 'Percentage (%)');
% show positive only
tmp_pval = G_z2p( zscore( :, :, hemi_idx)) + (1 - (zscore( :, :, hemi_idx) > 0));
subplot( 3, 2, 2*(hemi_idx-1) + 2);
G_Imagesc( zscore( :, :, hemi_idx), 'aterisks', tmp_pval, 'aterisks_level', fdrp);
c = colorbar;
caxis( [-10, 10]);
set( c, 'Limits', [-10, 10], 'ticks', -10:5:10);
set( gca, 'ytick', 1:nb_roi_vals, 'yticklabels', roi_label, 'xtick', 1:nb_par_vals, 'xticklabel', xticklabel, 'ydir', ydir,...
'ticklength', [0, 0], 'fontsize', 6);
ylabel( 'Manual ROI'); xlabel( 'Parcellation');
xlabel( c, 'z score');
xtickangle( 45);
end
% jet colormap for z score
colormap( jet);
print( gcf, '-dpdf', '-fillpage', fullfile( figdir, [name_prefix, '_1.pdf']));
% red colormap for percentage figure
[~, r] = CustomColormap;
colormap( r);
print( gcf, '-dpdf', '-fillpage', fullfile( figdir, [name_prefix, '_2.pdf']));