-
Notifications
You must be signed in to change notification settings - Fork 15
/
rsHRF.m
executable file
·338 lines (310 loc) · 13.8 KB
/
rsHRF.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
function rsHRF(varargin)
%% resting state BOLD-fMRI HRF deconvolution and connectivity analysis
if nargin==2
job = varargin{1}; flag = varargin{2};
if ~isfield(job,'para_global')
job.para_global = rsHRF_global_para;
end
switch flag
case 'display'
rsHRF_viewer(job);
case 'volume'
% delete temporary files (generated wm/csf/brainmask)
flag_delete = job.para_global.delete_files;
%input images, cell file
nii_file = job.images;
%get 1st file name (out name)
ref_nii = nii_file{1}; %funtional space template
[fpath,raw_outname,~] = fileparts(ref_nii);
job.ref_nii = ref_nii;
job.raw_outname = raw_outname;
%out directory
outdir = job.outdir{1};
if isempty(outdir)
outdir = fpath;
else
if ~exist(outdir,'dir')
mkdir(outdir)
end
end
% NIfTI data
[data,v] = rsHRF_read_NIfTI_job(job);
% explicit mask
nii_mask = job.mask{1};
if isempty(nii_mask)
nii_mask = job.para_global.mask_nii ;
end
if ~isempty(nii_mask) %brain mask
nii_masknew = spm_file(nii_mask,'prefix',['bin_',raw_outname]);
spm_imcalc({ref_nii;nii_mask},nii_masknew,'(i1~=0).*(i2~=0)',{0,0,0,16});
v0 = spm_vol(nii_masknew);
datam = spm_read_vols(v0);
datam(isnan(datam))=0;
voxel_ind = find(datam~=0); clear datam
if flag_delete
delete(v0.fname)
end
else % var>0 as mask
data_var = nanvar(data,0,1);
voxel_ind = find(data_var>0); clear data_var
end
fprintf('# %d voxels inside defaut mask ...\n',length(voxel_ind))
% check ROI information
if isfield(job,'genericROI')
ROI = job.genericROI;
else
ROI = [];
end
if ~isempty(ROI)
flag_ROI = 1;
[mat,atlas,nii]= rsHRF_check_ROI(ROI, ref_nii,flag_delete);
roinfo = [mat;atlas;nii];
nvar = size(roinfo,1);
Nscans = size(data,1);
if job.Denoising.which1st == 3;
job.Denoising.which1st = 2;
end
if job.Denoising.which1st==2
data_ROI = zeros(Nscans,nvar);
for j=1:nvar
data_ROI(:,j) = nanmean(data(:,roinfo{j,1}),2);
end
[data,data_nuisancerm] = rsHRF_denoise_job(job,data_ROI);
elseif job.Denoising.which1st==1
[data,data_nuisancerm] = rsHRF_denoise_job(job,data);
data_ROI = zeros(Nscans,nvar);
data_ROI_nuisancerm = zeros(Nscans,nvar);
for j=1:nvar
data_ROI(:,j) = nanmean(data(:,roinfo{j,1}),2);
data_ROI_nuisancerm(:,j) = nanmean(data_nuisancerm(:,roinfo{j,1}),2);
end
data = data_ROI;
data_nuisancerm = data_ROI_nuisancerm;
clear data_ROI data_ROI_nuisancerm
end
else
flag_ROI=0;
data = data(:,voxel_ind);
[data,data_nuisancerm] = rsHRF_denoise_job(job,data);
end
if flag_ROI
if isfield(job,'prefix')
if job.savedata.job_save
save(fullfile(outdir,[job.prefix,raw_outname, '.mat']), 'roinfo','-v7.3');
end
else
if job.job_save
save(fullfile(outdir,[raw_outname, '_roinfo.mat']), 'roinfo');
end
end
end
flag_nii_gii = 1; %NIfTI
if isfield(job,'HRFE') % deconvolution
rsHRF_deconv_job(job,data,data_nuisancerm,flag_ROI, outdir, v, voxel_ind, flag_nii_gii);
else
rsHRF_conn_job(job,data,flag_ROI, outdir, v, voxel_ind, flag_nii_gii)
end
case 'mesh'
%input images, cell file
gii_file = job.images;
[fpath,raw_outname,~] = fileparts(gii_file{1});
job.raw_outname = raw_outname;
job.rmoutlier = 0;
%out directory
outdir = job.outdir{1};
if isempty(outdir)
outdir = fpath;
else
if ~exist(outdir,'dir')
mkdir(outdir)
end
end
% GIfTI data
data = rsHRF_read_GIfTI_job(job);
v.dim = [size(data,2), 1];
% explicit mask
gii_mask = job.mask{1};
if ~isempty(gii_mask) %brain mask
datam = gifti(gii_mask);
datam.cdata(isnan(datam.cdata))=0;
vertex_ind = find(datam.cdata~=0); clear datam
else % var>0 as mask
data_var = nanvar(data,0,1);
vertex_ind = find(data_var>0); clear data_var
end
fprintf('# %d vertexs inside the mask ...\n',length(vertex_ind))
% check ROI information
if isfield(job,'genericROI')
ROI = job.genericROI;
else
ROI = [];
end
if ~isempty(ROI)
flag_ROI = 1;
[~,~,~,atlasmeh,gii]= rsHRF_check_ROI(ROI);
roinfo = [atlasmeh; gii];
nvar = size(roinfo,1);
Nscans = size(data,1);
if job.Denoising.which1st == 3;
job.Denoising.which1st = 2;
end
if job.Denoising.which1st==2
data_ROI = zeros(Nscans,nvar);
for j=1:nvar
data_ROI(:,j) = nanmean(data(:,roinfo{j,1}),2);
end
[data,data_nuisancerm] = rsHRF_denoise_job(job,data_ROI);
elseif job.Denoising.which1st==1
[data,data_nuisancerm] = rsHRF_denoise_job(job,data);
data_ROI = zeros(Nscans,nvar);
data_ROI_nuisancerm = zeros(Nscans,nvar);
for j=1:nvar
data_ROI(:,j) = nanmean(data(:,roinfo{j,1}),2);
data_ROI_nuisancerm(:,j) = nanmean(data_nuisancerm(:,roinfo{j,1}),2);
end
data = data_ROI;
data_nuisancerm = data_ROI_nuisancerm;
clear data_ROI data_ROI_nuisancerm
end
else
flag_ROI=0;
data = data(:,vertex_ind);
[data,data_nuisancerm] = rsHRF_denoise_job(job,data);
end
if flag_ROI
if isfield(job,'prefix')
if job.savedata.job_save
save(fullfile(outdir,[job.prefix,raw_outname, '.mat']), 'roinfo','-v7.3');
end
else
if job.job_save
save(fullfile(outdir,[raw_outname, '_roinfo.mat']), 'roinfo');
end
end
end
flag_nii_gii = 2; %GIfTI
if isfield(job,'HRFE') % deconvolution
rsHRF_deconv_job(job,data,data_nuisancerm,flag_ROI, outdir, v, vertex_ind, flag_nii_gii);
else
rsHRF_conn_job(job,data,flag_ROI, outdir, v, vertex_ind, flag_nii_gii)
end
case 'sig'
rsHRF_ROI_sig_job(job)
end
return
else
code_path = fileparts(which('rsHRF.m')) ;
a = imread(fullfile(code_path,'rsHRF_logo.png'));
pos = [0.4 0.3 0.2 0.3236];
if ismac % Code to run on Mac plaform
elseif isunix % Code to run on Linux plaform
pos = [0.4 0.3 0.23 0.35];
elseif ispc % Code to run on Windows platform
end
S.fig = figure('Visible','on',...
'numbertitle','off',...
'menubar','none',...
'units','normalized',...
'color','w',...
'position',pos,...[563 98 480 360],... %0.4 0.3 0.15 0.38
'name',['rsHRF v2.4(',getenv('USERNAME'),')'],...
'resize','off');
axes('parent',S.fig,'units','normalized','position',[0.15 0.45 [1 0.7825]*0.76]); %[0 0.59 1 0.43]
imagesc(a, 'AlphaData', 0.6);
axis off;
%% ROI signal HRF deconvolution
S.pb(1) = uicontrol('parent',S.fig,...
'unit','norm',...
'pos',[0.05,0.05,0.4,0.12],...
'style','pushbutton',...'pushbutton',...
'string','Signals',...
'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.sig_rsHRF'');',...
'backgroundc','w',...
'foregroundcolor',0*[1 1 1],... 'fontname','Segoe Script',...
'fontname','Calibri',...'Times New Roman',...
'fontunits', 'normalized',...
'fontsize', 0.4,...
'fontweight','bold');
%% ROI-wise HRF deconvolution
S.pb(2) = uicontrol('parent',S.fig,...
'unit','norm',...
'pos',[0.05,0.2,0.4,0.12],...
'style','pushbutton',...'pushbutton',...
'string','ROIs-volume',...
'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.ROI_rsHRF'');',...
'backgroundc','w',...
'foregroundcolor',0*[1 1 1],... 'fontname','Segoe Script',...
'fontname','Calibri',...'Times New Roman',...
'fontunits', 'normalized',...
'fontsize', 0.4,...
'fontweight','bold');
%% (Surface) ROI-wise HRF deconvolution
S.pb(3) = uicontrol('parent',S.fig,...
'unit','norm',...
'pos',[0.55,0.2,0.4,0.12],...
'style','pushbutton',...'pushbutton',...
'string','ROIs-surface',...
'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.SurfROI_rsHRF'');',...
'backgroundc','w',...
'foregroundcolor',0*[1 1 1],... 'fontname','Segoe Script',...
'fontname','Calibri',...'Times New Roman',...
'fontunits', 'normalized',...
'fontsize', 0.4,...
'fontweight','bold');
%% voxelwise HRF deconvolution
S.pb(4) = uicontrol('parent',S.fig,...
'unit','norm',...
'pos',[0.05,0.35,0.4,0.12],...
'style','pushbutton',...'pushbutton',...
'string','Voxels',...
'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.vox_rsHRF'');',...
'backgroundc','w',...
'foregroundcolor',0*[1 1 1],... 'fontname','Segoe Script',...
'fontname','Calibri',...'Times New Roman',...
'fontunits', 'normalized',...
'fontsize', 0.4,...
'fontweight','bold');
%% vertexwise HRF deconvolution
S.pb(5) = uicontrol('parent',S.fig,...
'unit','norm',...
'pos',[0.55,0.35,0.4,0.12],...
'style','pushbutton',...'pushbutton',...
'string','Vertices',...
'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.mesh_rsHRF'');',...
'backgroundc','w',...
'foregroundcolor',0*[1 1 1],... 'fontname','Segoe Script',...
'fontname','Calibri',...'Times New Roman',...
'fontunits', 'normalized',...
'fontsize', 0.4,...
'fontweight','bold');
S.pb(6) = uicontrol('parent',S.fig,...
'unit','norm',...
'pos',[0.55,0.05,0.4,0.12],...
'style','pushbutton',...'pushbutton',...
'string','Display',...
'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.display_HRF'');',...
'backgroundc','w',...
'foregroundcolor',0*[1 1 1],... 'fontname','Segoe Script',...
'fontname','Calibri',...'Times New Roman',...
'fontunits', 'normalized',...
'fontsize', 0.4,...
'fontweight','bold');
if nargin==1
Modality=upper(varargin{1});
switch Modality
case 'CONN'
set(S.pb(1),'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.sig_conn'');');
set(S.pb(2),'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.ROI_conn'');');
set(S.pb(3),'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.SurfROI_conn'');');
set(S.pb(4),'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.vox_conn'');');
set(S.pb(5),'CallBack','spm_jobman(''interactive'','''',''spm.tools.rsHRF.mesh_conn'');');
set(S.pb(6),'enable','off')
set(S.pb(:),'foregroundcolor',0*[1 1 1]) %0.3 0.6 0
text(80,360,'Connectivity Analysis','Color',[0.3 0.6 0],'Fontsize',13,'Fontweight','bold');
otherwise
text(80,360,'Resting State HRF','Color',[1 0.6 0],'Fontsize',13,'Fontweight','bold');
end
else
text(80,360,'Resting State HRF','Color',[1 0.6 0],'Fontsize',13,'Fontweight','bold');
end
end