-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_clsm.m
205 lines (140 loc) · 6.56 KB
/
run_clsm.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
% EXCUTE the preprocessing
%--------------------------------------------------------------------------
global CLSM
tic; ST = clock;
fprintf('\n=======================================================================\n');
fprintf(' Static Functional Connectivity ...\n');
fprintf('=======================================================================\n\n');
% OPEN MATLABPOOL IF POSSIBLE
%--------------------------------------------------------------------------
try parpool; end;
% Flag for Debug mode
%--------------------------------------------------------------------------
DEBUGmode = 0;
% SPECIFY your own study
%__________________________________________________________________________
fMRIpath = CLSM.fMRIpath;
LESIONpath = CLSM.LESIONpath;
normalList = CLSM.normalList;
lesionList = CLSM.lesionList;
OUTpath = CLSM.anal.OUTpath;
prefix = CLSM.prep.prefix;
% PARAMETERS FOR TEMPORAL FMRI DATA PROCESSING
%--------------------------------------------------------------------------
TR = CLSM.prep.TR; % TR time: volume acquisition time
BW = CLSM.prep.BW; % frequency range for bandpass filter
dummyoff = CLSM.prep.dummyoff; % num. of dummy data from beginning
FILTPARAM = [TR BW]; % set filtering parameters
fmridir = CLSM.prep.fmridir; % fmri directory
lesiondir = CLSM.anal.lesiondir; % fmri directory
% REGRESSORS SELECTION
%--------------------------------------------------------------------------
REGRESSORS(1) = CLSM.prep.GS;
REGRESSORS(2) = CLSM.prep.WM;
REGRESSORS(3) = CLSM.prep.CSF;
% Scrubbing option
%--------------------------------------------------------------------------
FDthr = CLSM.anal.FDthr;
doScrubbing = CLSM.anal.doScrubbing;
% Find Reference File
%--------------------------------------------------------------------------
subjpath = fullfile(fMRIpath,'Data',normalList{1},fmridir);
fn_nii = sprintf('^%s.*._cleaned_bpf.nii$',prefix);
fns = spm_select('FPList',subjpath,fn_nii);
if isempty(fns)
fn_nii = sprintf('^%s.*._cleaned_bpf.img$',prefix);
fns = spm_select('FPList',subjpath,fn_nii);
end
try
vref=spm_vol(fns(1,:));
catch
fprintf('Cannot find cleaned_bpf image in [%s] folder.\n',fmridir);
msg_on_handle=sprintf('Preprocessing first!');
set(handles.analcorr_status,'String',msg_on_handle);
set(handles.analcorr_status,'ForegroundColor','k');
set(handles.analcorr_status,'FontWeight','normal'); return
end
if length(vref)>1,vref=vref(1);end;
DIM = vref.dim(1:3);
[idbrainmask, idgm, idwm, idcsf] = fmri_load_maskindex(vref);
% CORRELATION ANALYSIS USING TIME SERIES
%--------------------------------------------------------------------------
set(handles.run_analysis,'ForegroundColor',[1 1 1]);
set(handles.run_analysis,'BackgroundColor',CLSM.colorblue);
pause(0.1);
nlesion = length(lesionList);
for c=1:nlesion,
lesionname = lesionList{c};
fprintf(' [%03d/%03d] lesion %s, calculating clsm ... (%.1f min.) \n',c,nlesion,lesionname,toc/60);
msg_on_handle=sprintf('lesion %03d/%03d (computing clsm...) ',c,nlesion);
set(handles.analcorr_status,'String',msg_on_handle);
set(handles.analcorr_status,'ForegroundColor',CLSM.colorblue);
set(handles.analcorr_status,'FontWeight','bold'); pause(1);
% Get lesion mask
%----------------------------------------------------------------------
SAVEmode=1;
fn_lesion = spm_select('FPList',fullfile(LESIONpath,'Data',lesionname,lesiondir),'^w.*.nii');
lesion = get_lesion_mask(fn_lesion,vref,idbrainmask,SAVEmode);
% Functional Connectivity
%------------------------------------------------------------------
h = waitbar(0,'1','Name',sprintf('CLSM for %03d/%03d lesion...', c, nlesion));
for k=1:length(normalList)
waitbar(k/length(normalList),h,sprintf('%d/%d',k,length(normalList)));
normalsubj = normalList{k};
subjpath = fullfile(fMRIpath,'Data',normalsubj,fmridir);
fn_nii = sprintf('^%s.*._cleaned_bpf.nii$',prefix);
fns = spm_select('FPList',subjpath,fn_nii);
if ~exist(fns,'file'),
fprintf(' rs-fmri for [%s] does not exist!\n',normalsubj);
continue
else
vs = spm_vol(fns);
end
Z = spm_read_vols(vs);
Z = reshape(Z, prod(vs(1).dim), length(vs));
% Compute Frame-wise displacement for scrubbing time-series
%------------------------------------------------------------------
fn_motion = dir(fullfile(subjpath,'rp_*.txt'));
fn_motion = fullfile(subjpath,fn_motion(1).name);
if ~exist(fn_motion,'file'),
fprintf('Cannot find rp*.txt file in\n%s\n',subjpath);
break;
end
motion = dlmread(fn_motion);
FD_val = compute_fd(motion(dummyoff+1:end,:),'spm');
if doScrubbing,
% scrubbing 1 back and 2 forward neighbors as performed by Power et al
idxScrubbing = find(FD_val>FDthr);
idxScrubbing_b1 = idxScrubbing-1;
idxScrubbing_a1 = idxScrubbing+1;
idxScrubbing_a2 = idxScrubbing+2;
idxScrubbing = [idxScrubbing(:); idxScrubbing_b1(:); idxScrubbing_a1(:); idxScrubbing_a2(:)];
idxScrubbing = unique(idxScrubbing);
idxScrubbing(idxScrubbing==0)=[];
Z(:,idxScrubbing) = [];
fprintf(' : scrubbing %d scans by FD>%.1f ...\n', length(idxScrubbing), FDthr);
end
% Lesion-based functional connectivity
%------------------------------------------------------------------
zs=fmri_connectivity(Z(idbrainmask,:),DIM,lesion,idbrainmask);
% WRITE RESULTS ...
%------------------------------------------------------------------
vo = vref;
SAVEpath=fullfile(OUTpath,'clsm_zmaps',lesionname,CLSM.prep.fmridir); mkdir(SAVEpath);
SAVEname=sprintf('zscore_%s_%s.nii',lesionname,normalsubj);
vo.fname=fullfile(SAVEpath, SAVEname);
vo.dt=[16 0];
IMG = zeros(vref.dim);
IMG(idbrainmask) = zs;
spm_write_vol(vo,IMG);
end
delete(h); % remove progress bar
fprintf('\n');
end
set(handles.run_analysis,'ForegroundColor',CLSM.colorblue);
set(handles.run_analysis,'BackgroundColor',[248 248 248]./256);
pause(0.1);
msg_on_handle=sprintf('Static FC was done ... ');
set(handles.analcorr_status,'String',msg_on_handle);
set(handles.analcorr_status,'ForegroundColor','k');
set(handles.analcorr_status,'FontWeight','normal');