-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdegreeAnalysis.m
More file actions
executable file
·349 lines (277 loc) · 10.6 KB
/
degreeAnalysis.m
File metadata and controls
executable file
·349 lines (277 loc) · 10.6 KB
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
%% SETUP
% Clear
clear; close all; clc;
%% Set paths & directories
% Shuffle random seed. Necessary in array parallelization to avoid
% repeating same random seed across arrays.
rng('shuffle');
% Find general path (enclosing folder of current directory)
path{1} = strsplit(pwd, '/');
path{3,1} = strjoin(path{1}(1:end-1),'/');
path{4,1} = strjoin(path{1}, '/');
path{1,1} = strjoin(path{1}(1:end-2),'/');
path{2,1} = fullfile(path{1},'MATLAB');
% Set required subdirectories
path{5,1} = fullfile(path{3},'Data');
path{6,1} = fullfile(path{3},'Functions');
path{7,1} = fullfile(path{3},'Results','EC');
% Add relevant paths
addpath(genpath(path{6}));
%% Set file names & load data
% Define files to load
loadFile = 'LEICA90_CIC_EC';
% Load data
load(fullfile(path{7}, loadFile));
% File to save
S = strsplit(loadFile, '_');
fileName = strcat(S{1}, '_', S{2}, '_EC');
clear loadFile S
%% Reset paths & directories
% Find general path (enclosing folder of current directory)
path{1} = strsplit(pwd, '/');
path{3,1} = strjoin(path{1}(1:end-1),'/');
path{4,1} = strjoin(path{1}, '/');
path{1,1} = strjoin(path{1}(1:end-2),'/');
path{2,1} = fullfile(path{1},'MATLAB');
% Set required subdirectories
path{5,1} = fullfile(path{3},'Data');
path{6,1} = fullfile(path{4},'Functions');
path{7,1} = fullfile(path{3},'Results','EC');
%% Analyze network using network-based statistic (NBS)
% Add path to NBS
addpath(fullfile(path{2}, 'BCT', 'NBS', 'directed')),
% Load network labels
load(fullfile(path{5}, 'Atlases', 'AAL', 'AAL_labels.mat'));
% Convert labels to strings
labelROI = strings(size(label90,1), 1);
for k = 1:size(label90,1)
labelROI(k,1) = string(label90(k,:));
end
clear k label90
% Convert labels to mirrored version
labelROI = LR_version_symm(labelROI);
% Convert EC to 3D format
EC3 = nan(N.ROI, N.ROI, sum(N.subjects));
k = 0;
for c = 1:N.conditions
for s = 1:N.subjects(c)
k = k+1;
EC3(:,:,k) = EC(:,:,c,s);
end
end
%% Analyze network using network-based statistic (NBS)
% Add path to NBS
addpath(fullfile(path{2}, 'BCT', 'NBS', 'directed')),
% Load network labels
load(fullfile(path{5}, 'Atlases', 'AAL', 'AAL_labels.mat'));
% Convert labels to strings
labelROI = strings(size(label90,1), 1);
for k = 1:size(label90,1)
labelROI(k,1) = string(label90(k,:));
end
clear k label90
% Convert labels to mirrored version
labelROI = LR_version_symm(labelROI);
% Convert EC to 3D format
EC3 = nan(N.ROI, N.ROI, sum(N.subjects));
k = 0;
for c = 1:N.conditions
for s = 1:N.subjects(c)
k = k+1;
EC3(:,:,k) = EC(:,:,c,s);
end
end
clear c s k
% Set GLM parameters
GLM.perms = 5000; % number of permutations (scalar)
GLM.X = horzcat(ones(size(I,1),1), I); % design matrix (observations x conditions)
GLM.contrast = [0, 1, -1]; % set contrast element of intercept term to zero
% Set statistical test to run
conttype = {[0, 1, -1], [1, -1]}; % set contrast element of intercept term to zero
testtype = {'ttest', 'ftest'}; % GLM.test accepts 'ttest' or 'ftest'
stattype = {'Extent', 'Intensity'}; % STATS.size accepts 'Extent' or 'Intensity'
STATS.alpha = 0.05; % significance threshold
% Preallocate storage array
nbs = cell(numel(1:2*4), numel(conttype), numel(testtype), numel(stattype));
% Run NBS over test types, statistical sizes, and multiple threshold
for c = 1:numel(conttype)
GLM.contrast = conttype{c};
GLM.X = GLM.X(:,c:end);
for test = 1:numel(testtype)
GLM.test = testtype{test};
for stat = 1:numel(stattype)
STATS.size = stattype{stat};
for thresh = 1:2*4
STATS.thresh = 0.5*thresh; % test statistic threshold
nbs{thresh, c, test, stat} = NBSdirected(EC3, GLM, STATS);
end
end
end
end
clear stat test thresh EC3
% Save results
save(fullfile(path{7}, fileName), 'nbs', '-append');
%% Compare group-level degree distributions
% Add paths
addpath(fullfile(path{2}, 'BCT'));
% Preallocate storage arrays
densities = nan(N.conditions, max(N.subjects));
degree.in = nan(N.ROI, N.conditions, max(N.subjects), 2);
degree.out = nan(N.ROI, N.conditions, max(N.subjects), 2);
degree.global.h = nan(3,2);
degree.global.p = nan(3,2);
% Convert h, p to tables
degree.global.h = array2table(degree.global.h, 'VariableNames',{'In','Out'}, 'RowNames',{'Unnormalized', 'Normalized', 'Density'});
degree.global.p = array2table(degree.global.p, 'VariableNames',{'In','Out'}, 'RowNames',{'Unnormalized', 'Normalized', 'Density'});
% Compute densities and strengths
for c = 1:N.conditions
for s = 1:N.subjects(c)
% Compute densities
densities(c,s) = density_dir(EC(:,:,c,s));
% Unnormalized strengths
degree.in(:,c,s,1) = sum(EC(:,:,c,s), 2);
degree.out(:,c,s,1) = sum(EC(:,:,c,s), 1);
% Density-normalized degrees
% degree.in(:,c,s,2) = degree.in(:,c,s,1) ./ densities(c,s);
% degree.out(:,c,s,2) = degree.out(:,c,s,1) ./ densities(c,s);
end
end
clear s c
% Compare unnormalized strength distributions
s11 = degree.in(:,1,:,1); s12 = degree.in(:,2,:,1); % in-strengths
s21 = degree.out(:,1,:,1); s22 = degree.out(:,2,:,1); % out-strengths
[degree.global.h{'Unnormalized','In'}, degree.global.p{'Unnormalized','In'}] = kstest2(s11(:), s12(:)); % unnormalized in-strengths
[degree.global.h{'Unnormalized','Out'}, degree.global.p{'Unnormalized','Out'}] = kstest2(s21(:), s22(:)); % unnormalized out-strengths
% Compare normalized strength distributions
s11 = degree.in(:,1,:,2); s12 = degree.in(:,2,:,2); % in-strengths
s21 = degree.out(:,1,:,2); s22 = degree.out(:,2,:,2); % out-strengths
[degree.global.h{'Normalized','In'}, degree.global.p{'Normalized','In'}] = kstest2(s11(:), s12(:)); % normalized in-strengths
[degree.global.h{'Normalized','Out'}, degree.global.p{'Normalized','Out'}] = kstest2(s21(:), s22(:)); % normalized out-strengths
clear s11 s12 s21 s22
% Compare densities
[degree.global.h{'Density','In'}, degree.global.p{'Density','Out'}] = kstest2(densities(1,:), densities(2,:));
% Visualize densities
D(1) = figure;
histogram(densities(1,:)); hold on;
histogram(densities(2,:));
title('Connection Densities of Hopf-Based Effective Connectivity');
xlabel('Density');
ylabel('Counts');
legend(labels.Properties.VariableNames);
% Display strength distributions
D(2) = figure;
for w = 1:2
subplot(2,2, 2*w-1);
for c = 1:N.conditions
histogram(degree.in(:,c,:,w)); hold on;
end
title('In-Strength Distribution of Hopf-Based Effective Connectivity');
xlabel('In-Strength');
ylabel('Counts');
legend(labels.Properties.VariableNames);
subplot(2,2, 2*w);
for c = 1:N.conditions
histogram(degree.out(:,c,:,w)); hold on;
end
title('Out-Strength Distribution of Hopf-Based Effective Connectivity');
xlabel('Out-Strength');
ylabel('Counts');
legend(labels.Properties.VariableNames);
end
clear s c
% Save figure
savefig(D, fullfile(path{7}, fileName), 'compact');
% Save results
save(fullfile(path{7}, fileName), 'degree', '-append');
%% Compare node-level degree distributions
% Preallocate storage arrays
degree.ROI.p = nan(N.ROI, 2, 2);
degree.ROI.h = nan(N.ROI, 2, 2);
% Test for significant differences between patients and controls
for roi = 1:N.ROI
% for w = 1:2
cin = squeeze(degree.in(roi,1,:,w)); cin = cin(~isnan(cin));
cout = squeeze(degree.out(roi,1,:,w)); cout = cout(~isnan(cout));
pin = squeeze(degree.in(roi,2,:,w)); pin = pin(~isnan(pin));
pout = squeeze(degree.out(roi,2,:,w)); pout = pout(~isnan(pout));
[degree.ROI.h(roi, w, 1), degree.ROI.p(roi, w, 1)] = kstest2(cin, pin);
[degree.ROI.h(roi, w, 2), degree.ROI.p(roi, w, 2)] = kstest2(cout, pout);
% end
end
clear roi w point cin cout pin pout
% Run FDR multiple comparison correction
degree.ROI.FDR = zeros(N.ROI, 2, 2);
% for w = 1:2
for point = 1:2
[ind] = FDR_benjHoch(degree.ROI.p(:,w,point), pval.target);
degree.ROI.FDR(ind, w, point) = 1;
end
% end
degree.ROI.FDR = logical(degree.ROI.FDR);
clear ind w point
% Run Bonferroni multiple comparison correction
degree.ROI.Bonferroni = (degree.ROI.p < (pval.target/N.ROI));
degree.ROI.Bonferroni = logical(degree.ROI.Bonferroni);
% Run Dunn-Sidak multiple comparison correction
alpha = 1-(1-pval.target)^(1/N.ROI);
degree.ROI.Sidak = (degree.ROI.p < alpha);
degree.ROI.Sidak = logical(degree.ROI.Sidak);
clear alpha
% Locate significantly different distributions
sig{1,1} = find(degree.ROI.FDR(:,1,1)); % unnormalized, in-strength
sig{1,2} = find(degree.ROI.FDR(:,1,2)); % unnormalized, out-strength
sig{2,1} = find(degree.ROI.FDR(:,2,1)); % normalized, out-strength
sig{2,2} = find(degree.ROI.FDR(:,2,2)); % normalized, out-strength
% Visualize entropies
D(3) = figure;
% Mean Un-Normalized In-Strength
subplot(2,2,1);
p = bar(mean(degree.in(:,:,:,1),3,'omitnan')); hold on;
errorbar((1:N.ROI)-0.15, mean(degree.in(:,1,:,1),3,'omitnan'), std(degree.in(:,1,:,1),0,3,'omitnan'), '.b');
errorbar((1:N.ROI)+0.15, mean(degree.in(:,2,:,1),3,'omitnan'), std(degree.in(:,2,:,1),0,3,'omitnan'), '.r');
scatter(sig{1,1}, 6.2*ones(numel(sig{1,1}),1), '*k');
xticklabels(labelROI);
ylabel('Mean In-Strength');
title('Mean In-Strength Per ROI');
legend(p, labels.Properties.VariableNames);
clear p
% Mean Normalized In-Strength
subplot(2,2,3);
p = bar(mean(degree.in(:,:,:,2),3,'omitnan')); hold on;
errorbar((1:N.ROI)-0.15, mean(degree.in(:,1,:,2),3,'omitnan'), std(degree.in(:,1,:,2),0,3,'omitnan'), '.b');
errorbar((1:N.ROI)+0.15, mean(degree.in(:,2,:,2),3,'omitnan'), std(degree.in(:,2,:,2),0,3,'omitnan'), '.r');
scatter(sig{2,1}, 18*ones(numel(sig{2,1}),1), '*k');
xticklabels(labelROI);
ylabel('Mean Normalized In-Strength');
title('Mean Normalized In-Strength Per ROI');
legend(p, labels.Properties.VariableNames)
clear p
% Mean Out-Strength
subplot(2,2,2);
p = bar(mean(degree.out(:,:,:,1),3,'omitnan')); hold on;
errorbar((1:N.ROI)-0.15, mean(degree.out(:,1,:,1),3,'omitnan'), std(degree.out(:,1,:,1),0,3,'omitnan'), '.b');
errorbar((1:N.ROI)+0.15, mean(degree.out(:,2,:,1),3,'omitnan'), std(degree.out(:,2,:,1),0,3,'omitnan'), '.r');
legend(labels.Properties.VariableNames);
scatter(sig{1,2}, 6.2*ones(numel(sig{1,2}),1), '*k');
xticklabels(labelROI);
ylabel('Mean Out-Strength');
title('Mean Out-Strength Per ROI');
legend(p, labels.Properties.VariableNames);
clear p
% Mean Normalized Out-Strength
subplot(2,2,4);
p = bar(mean(degree.out(:,:,:,2),3,'omitnan')); hold on;
errorbar((1:N.ROI)-0.15, mean(degree.out(:,1,:,2),3,'omitnan'), std(degree.out(:,1,:,2),0,3,'omitnan'), '.b');
errorbar((1:N.ROI)+0.15, mean(degree.out(:,2,:,2),3,'omitnan'), std(degree.out(:,2,:,2),0,3,'omitnan'), '.r');
legend(labels.Properties.VariableNames);
scatter(sig{2,2}, 18*ones(numel(sig{2,2}),1), '*k');
xticklabels(labelROI);
ylabel('Mean Normalized Out-Strength');
title('Mean Normalized Out-Strength Per ROI');
legend(p, labels.Properties.VariableNames);
clear p
% Save figure
savefig(D, fullfile(path{7}, fileName), 'compact');
clear D
% Save results
save(fullfile(path{7}, fileName), 'degree', '-append');