-
Notifications
You must be signed in to change notification settings - Fork 4
/
QCSC.m
365 lines (260 loc) · 16.7 KB
/
QCSC.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
function [DATA,STRDATA] = QCSC(adjs,motion,lens,edgeconthr,calc_node_STRs,include_zero_edges,save_edge_properties,save_output_location)
% This function calculates the correlation between edge weight and some
% measure of motion across participants.
%
% INPUTS:
%
% adjs = a cell array where each cell contains a subjects weighted connectivity matrix
%
% motion = some measure of head motion
%
% lens = a cell array where each cell contains a subjects distance matrix (length of each connection)
%
% edgeconthr = edge consistency-based threshold to use (see note below)
%
% calc_node_STRs = set to 1 to calculate node strengths and degrees
%
% include_zero_edges = set to 1 to include edges that have no connections
% in the QCSC correlation calculation
%
% save_edge_properties = set to 1 to save edge properties in a seperate
% file
%
% save_output_location = set the location of where output is saved
% (defaults to current directory)
%
% OUTPUTS:
%
% DATA = a structure of QCSC related information and edge properties. See
% below for explanation of all fields in this structure. Each contains a
% cell/vector of some property for each pipeline.
%
% STRDATA = a structure of all data related to node degree and strength.
% See below for explanation of all fields in this structure. Each contains
% a cell/vector of some property for each pipeline.
%
%
% Note if calc_node_STRs is set to 1, a group adjacency matrix mask
% will be created under using a threshold set by 'edgeconthr', which is
% then applied to each subjects adjacency matrix. These thresholded
% matrices are further thresholded at a number of different levels to get
% strength and degree values for the nodes. These two levels of
% thresholding will have no effect on edge consistency based thresholding,
% but will effect edge variability based thresholding
if nargin < 8
save_output_location = [];
end
% Note that DATA and DATA are combined into DATA, they are just kept
% seperate so they can be easily saved
% DATA.EdgeMatQCSC: a matrix of each edges QCSC correlation value
% DATA.EdgeMatQCSC_PVALS: a matrix of each edges QCSC correlation p value
% DATA.EdgeMatConsistency: a matrix of each edges consistency
% DATA.EdgeMatWeightVariability: a matrix of each edges weight variability
% DATA.mean_QCSC: the mean QCSC correlation
% DATA.median_QCSC: the median QCSC correlation
% DATA.QCSC: a vector of each edges QCSC correlation value
% DATA.QCSC_PVALS: a vector of each edges QCSC correlation p value
% DATA.EdgeConsistency: a vector of each edges consistency
% DATA.EdgeWeightVariability: a vector of each edges weight variability
% DATA.EdgeLength: a vector of each edges length
% DATA.EdgeMatLength: a matrix of each edges length
% DATA.EdgeCovariance: a vector of each edges weight covariance
% DATA.EdgeWeightVariance: a vector of each edges weight variance
% DATA.EdgeMatCovariance: a matrix of each edges weight covariance
% DATA.EdgeMatWeightVariance: a matrix of each edges weight variance
% DATA.EdgeWeight: a vector of each edges weight
% DATA.EdgeMatWeight: a matrix of each edges weight
% DATA.total_str_mean: the mean total strength (across participants)
% DATA.total_str_sd: standard deviation of total strength (across participants)
% DATA.density_mean: the mean density (across participants)
% DATA.density_sd: the standard deviation of density (across participants)
% DATA.MeanEdgeWeight_mean: the mean of mean edge weight (across participants)
% DATA.MeanEdgeWeight_sd: the mean of standard deviation of edge weight (across participants)
% DATA.SdEdgeWeight_mean: the standard deviation of mean edge weight (across participants)
% DATA.SdEdgeWeight_sd: the standard deviation of standard deviation of edge weight (across participants)
% Thresholds to check when calculating strength
threshs = [0 .05 .1:.1:1];
Nthr = length(threshs);
% STRDATA.STRcon: the node strength
% under a given edge based consistency threshold (columns)
STRDATA.STRcon = cell(1,Nthr);
% A cell matrix where each cell is the node degree
% under a given edge based consistency threshold (columns)
STRDATA.DEGcon = cell(1,Nthr);
% A cell matrix where each cell is the node strength
% under a given edge based variability threshold (columns)
STRDATA.STRvar = cell(1,Nthr);
% A cell matrix where each cell is the node degree
% under a given edge based variability threshold (columns)
STRDATA.DEGvar = cell(1,Nthr);
% A cell matrix where each cell is the QCStrength correlation for each node
% under a given edge based consistency threshold (columns)
STRDATA.QCSTR_con = cell(1,Nthr);
% A cell matrix where each cell is the QCStrength correlation p value for each node
% under a given edge based consistency threshold (columns)
STRDATA.QCSTR_pval_con = cell(1,Nthr);
% A cell matrix where each cell is the QCStrength correlation for each node
% under a given edge based variability threshold (columns)
STRDATA.QCSTR_var = cell(1,Nthr);
% A cell matrix where each cell is the QCStrength correlation p value for each node
% under a given edge based variability threshold (columns)
STRDATA.QCSTR_pval_var = cell(1,Nthr);
% A matrix of the propotion of nodes with a significant QCStrength
% correlation (p < .05) under a given edge based consistency threshold (columns)
STRDATA.PropNodeStr_con_sig = zeros(1,Nthr);
% A matrix of the propotion of nodes with a significant QCStrength
% correlation (p < .05) under a given edge based variability threshold (columns)
STRDATA.PropNodeStr_var_sig = zeros(1,Nthr);
STRDATA.threshs = threshs;
% Rarely, when calculating FA MRtrix can encounter an issue
% which results in a NaN value. This seems to be the result of the FA mask
% being slightly smaller than the brain mask, thus streamlines can exist
% just outside of FA values. When assigning FA values to streamlines and
% subsequently edge weights, this will result in a NaN value. We just set
% this value to 0 thereby excluding the edge for that subject. To be very
% conservative, we can also exclude an edge if for any subject a NaN is
% recorded. Doing so does not fundamentally change the results at all.
IgnoreEdgesWithNaN = 0;
tic
Nsubs = length(adjs);
[~, ~,groupMask] = connectomeGroupThreshold(adjs, edgeconthr);
NNodes = length(groupMask);
groupMaskTriu = triu(groupMask,1);
[indEdge] = find(groupMaskTriu(:)>0);
NEdges = length(indEdge);
subjEdges = zeros(Nsubs, NEdges);
subjEdgesLength = zeros(Nsubs, NEdges);
Ws = zeros(NNodes,NNodes,Nsubs);
den = zeros(Nsubs,1);
total_STR = zeros(Nsubs,1);
EdgeWeight_mean = zeros(Nsubs,1);
EdgeWeight_sd = zeros(Nsubs,1);
for subj=1:Nsubs
adjVect = adjs{subj}(:);
lengthVect = lens{subj}(:);
edge_weights = adjVect(indEdge);
subjEdges(subj,:) = edge_weights;
subjEdgesLength(subj,:) = lengthVect(indEdge);
W = adjs{subj}.*groupMask;
Ws(:,:,subj) = W;
Nnodes = size(W,1);
Nedges = nnz(W(~isnan(W)));
den(subj,1) = Nedges/((Nnodes^2-Nnodes)/2);
total_STR(subj,1) = nansum(edge_weights);
EdgeWeight_mean(subj,1) = nanmean(edge_weights(edge_weights>0));
EdgeWeight_sd(subj,1) = nanstd(edge_weights(edge_weights>0));
end
if IgnoreEdgesWithNaN == 1
NaN_edges = isnan(sum(subjEdges));
subjEdges(:,NaN_edges) = [];
subjEdgesLength(:,NaN_edges) = [];
NEdges = size(subjEdges,2);
end
if ~include_zero_edges
subjEdges(subjEdges==0) = nan;
end
DATA.total_str_mean = mean(total_STR);
DATA.total_str_sd = std(total_STR);
DATA.density_mean = mean(den);
DATA.density_sd = std(den);
DATA.MeanEdgeWeight_mean = mean(EdgeWeight_mean);
DATA.MeanEdgeWeight_sd = std(EdgeWeight_mean);
DATA.SdEdgeWeight_mean = mean(EdgeWeight_sd);
DATA.SdEdgeWeight_sd = std(EdgeWeight_sd);
if calc_node_STRs
threshs = [0 .05 .1:.1:1];
for thr = 1:length(threshs)
Str = zeros(Nsubs, NNodes);
Deg = zeros(Nsubs, NNodes);
Str2 = zeros(Nsubs, NNodes);
Deg2 = zeros(Nsubs, NNodes);
[~, ~, AdjMaskCon] = connectomeGroupThreshold(adjs,threshs(thr));
AdjMaskVar = threshold_consistency(Ws, threshs(thr));
for subj=1:Nsubs
Str(subj,:) = nansum(adjs{subj}.*AdjMaskCon);
Deg(subj,:) = nansum(double((adjs{subj}.*AdjMaskCon) > 0));
Str2(subj,:) = nansum(adjs{subj}.*AdjMaskVar);
Deg2(subj,:) = nansum(double((adjs{subj}.*AdjMaskVar) > 0));
end
STRDATA.STRcon{thr} = Str;
STRDATA.DEGcon{thr} = Deg;
STRDATA.STRvar{thr} = Str2;
STRDATA.DEGvar{thr} = Deg2;
[STRDATA.QCSTR_con{thr},STRDATA.QCSTR_pval_con{thr}] = corr(Str,motion,'type','Spearman');
[STRDATA.QCSTR_var{thr},STRDATA.QCSTR_pval_var{thr}] = corr(Str2,motion,'type','Spearman');
STRDATA.PropNodeStr_con_sig(thr) = sum(STRDATA.QCSTR_pval_con{thr} < .05)./length(STRDATA.QCSTR_pval_con{thr});
STRDATA.PropNodeStr_var_sig(thr) = sum(STRDATA.QCSTR_pval_var{thr} < .05)./length(STRDATA.QCSTR_pval_var{thr});
STRDATA.threshs = threshs;
end
else
STRDATA = [];
end
qcsc = zeros(1,NEdges);
qcsc_pval = zeros(1,NEdges);
Edge_Con = zeros(1,NEdges);
Edge_Var = zeros(1,NEdges);
Edge_Mat_Rho = zeros(NNodes);
Edge_Mat_Pval = zeros(NNodes);
Edge_Con_Mat = zeros(NNodes);
Edge_WeiVariability_Mat = zeros(NNodes);
Edge_Length = zeros(1,NEdges);
Edge_Length_Mat = zeros(NNodes);
Edge_Cov = zeros(1,NEdges);
Edge_Weight_variance = zeros(1,NEdges);
Edge_Cov_Mat = zeros(NNodes);
Edge_Weight_variance_mat = zeros(NNodes);
Edge_Weight = zeros(1,NEdges);
Edge_Weight_Mat = zeros(NNodes);
for edge = 1:NEdges
Edges2Corr = subjEdges(:,edge);
movement2corr = motion;
lengths2use = subjEdgesLength(:,edge);
nsubs = length(subjEdges(:,edge));
movement2corr(isnan(subjEdges(:,edge))) = [];
Edges2Corr(isnan(subjEdges(:,edge))) = [];
lengths2use(isnan(subjEdges(:,edge))) = [];
Edgecovvar = cov(Edges2Corr,movement2corr);
Edge_Cov(edge) = Edgecovvar(1,2);
Edge_Weight_variance(edge) = Edgecovvar(1,1);
Edge_Cov_Mat(indEdge(edge)) = Edge_Cov(edge);
Edge_Weight_variance_mat(indEdge(edge)) = Edgecovvar(1,1);
Edge_Con(edge) = sum(Edges2Corr~=0)/nsubs;
Edge_Var(edge) = std(Edges2Corr) / mean(Edges2Corr);
[qcsc(edge),qcsc_pval(edge)] = corr(movement2corr,Edges2Corr,'Type','Spearman');
Edge_Mat_Rho(indEdge(edge)) = qcsc(edge);
Edge_Mat_Pval(indEdge(edge)) = qcsc_pval(edge);
Edge_Con_Mat(indEdge(edge)) = Edge_Con(edge);
Edge_WeiVariability_Mat(indEdge(edge)) = Edge_Var(edge);
% If we are including subjects with an edge
% weight of 0 in the analysis, this can
% serverely distort the calculation of edge
% length. So I only calculate the mean length
% based on subjects who actually have an edge
% present
Edge_Length(edge) = mean(lengths2use(lengths2use>0));
Edge_Length_Mat(indEdge(edge)) = Edge_Length(edge);
Edge_Weight(edge) = mean(Edges2Corr);
Edge_Weight_Mat(indEdge(edge)) = Edge_Weight(edge);
end
%% Store output in structures
DATA.EdgeMatQCSC = Edge_Mat_Rho + Edge_Mat_Rho';
DATA.EdgeMatQCSC_PVALS = Edge_Mat_Pval + Edge_Mat_Pval';
DATA.EdgeMatConsistency = Edge_Con_Mat + Edge_Con_Mat';
DATA.EdgeMatWeightVariability = Edge_WeiVariability_Mat + Edge_WeiVariability_Mat';
DATA.mean_QCSC = nanmean(qcsc);
DATA.median_QCSC = nanmedian(qcsc);
DATA.QCSC = qcsc;
DATA.QCSC_PVALS = qcsc_pval;
DATA.EdgeConsistency = Edge_Con;
DATA.EdgeWeightVariability = Edge_Var;
DATA.EdgeLength = Edge_Length;
DATA.EdgeMatLength = Edge_Length_Mat + Edge_Length_Mat';
DATA.EdgeCovariance = Edge_Cov;
DATA.EdgeWeightVariance = Edge_Weight_variance;
DATA.EdgeMatCovariance = Edge_Cov_Mat + Edge_Cov_Mat';
DATA.EdgeMatWeightVariance = Edge_Weight_variance_mat + Edge_Weight_variance_mat;
DATA.EdgeWeight = Edge_Weight;
DATA.EdgeMatWeight = Edge_Weight_Mat + Edge_Weight_Mat';
timetaken = toc;
fprintf('Completed in %.4f seconds\n',timetaken)
DATA.motion = motion;