-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_HCP_data.m
253 lines (216 loc) · 10.8 KB
/
extract_HCP_data.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
function extract_HCP_data(list_path,beacon)
%---------------------------- Input variables -----------------------------
%download folder
dest_folder = './DATA';
% HCP release
HCP_release = 'HCP_1200';
%block to execute
flag_RP = 0;
flag_IB_stats = 0; %ImageBased stats
flag_IB_tcs = 1; %ImageBased tcs
flag_struct = 0; %aseg.stats
% parameters for IB
TR = 0.72;
HP = 2000;
WBCOMMAND = 'wb_command';
BCMODE = 'CORRECT';
OUTSTRING = [];
%--------------------------------------------------------------------------
%setup MatlabMailFeedback
mail = '[email protected]';
DeltaTime = 60*6; %every 6 hours sends a beacon
sendstatus(mail);
global SENDSTATUS_ERROR_MSG
if nargin == 1
beacon = 0;
end
%subject list file
fid = fopen(list_path);
ING = 'stats_dm'; %it is not used for loading stats, just to doublecheck results
subj = fgetl(fid); %get first subj ID
while subj > 0
disp(['Doing subj: ',subj])
SENDSTATUS_ERROR_MSG = ['Failed when doing subj: ',subj];
subj_local_path = [dest_folder,'/',subj];
if flag_struct
% each block has a separate output file
output_path = [dest_folder,'/',subj,'_'];
%------------download files------------
FILE = {}; FILE_STATUS = [];% init
FILE{end+1} = ['T1w/',subj,'/stats/aseg.stats'];
for l = 1:length(FILE)
FILE_STATUS(l) = HcpDownloadCommand(subj,FILE{l},dest_folder,'HCP_release',HCP_release);
end
%--------------------------------------
%-----------save extracted info--------
for l = 1:length(FILE_STATUS)
system(['cp ',subj_local_path,'/',FILE{l},' ',output_path,'aseg.stats']);
end
%--------------------------------------
end
if flag_RP
% each block has a separate output file
output_path = [dest_folder,'/',subj,'_QC_RP.mat'];
%------------download files------------
FILE = {}; FILE_STATUS = []; TMP = [];% init
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_LR/Movement_Regressors.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_LR/Movement_Regressors.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_RL/Movement_Regressors.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_RL/Movement_Regressors.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_LR/Movement_RelativeRMS.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_LR/Movement_RelativeRMS.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_RL/Movement_RelativeRMS.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_RL/Movement_RelativeRMS.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_LR/rfMRI_REST1_LR_SBRef.nii.gz';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_LR/rfMRI_REST2_LR_SBRef.nii.gz';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_RL/rfMRI_REST1_RL_SBRef.nii.gz';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_RL/rfMRI_REST2_RL_SBRef.nii.gz';
%
% FILE{end+1} = ['unprocessed/3T/rfMRI_REST1_LR/',subj,'_3T_rfMRI_REST1_LR_SBRef.nii.gz'];
% FILE{end+1} = ['unprocessed/3T/rfMRI_REST1_LR/',subj,'_3T_rfMRI_REST1_LR_SBRef.nii.gz'];
% FILE{end+1} = ['unprocessed/3T/rfMRI_REST2_RL/',subj,'_3T_rfMRI_REST2_RL_SBRef.nii.gz'];
% FILE{end+1} = ['unprocessed/3T/rfMRI_REST2_RL/',subj,'_3T_rfMRI_REST2_RL_SBRef.nii.gz'];
for l = 1:length(FILE)
FILE_STATUS(l) = HcpDownloadCommand(subj,FILE{l},dest_folder,'HCP_release',HCP_release);
end
%--------------------------------------
%-------------process files------------
for l = 1:4 % four runs
if ~FILE_STATUS(l)
%load RP and save first 6 columns (discard expantion terms)
rp = load([subj_local_path,'/',FILE{l}]);
TMP(l).RP = rp(:,1:6);
%RelativeRMS
TMP(l).relRMS.series = load([subj_local_path,'/',FILE{l+4}]);
TMP(l).relRMS.mean = mean(TMP(l).relRMS.series);
%Compute FDJenk and FDPower
FDtmp = fmri_rp_metrics(rp,'HCP',[subj_local_path,'/',FILE{l+8}]);
TMP(l).FDJenk.series = FDtmp.FDjenk;
TMP(l).FDJenk.mean = mean(FDtmp.FDjenk);
TMP(l).FDPower.series = FDtmp.FDpower;
TMP(l).FDPower.mean = mean(FDtmp.FDpower);
end
end
%--------------------------------------
%-----------save extracted info--------
if ~FILE_STATUS(1); REST1_LR = TMP(1); else REST1_LR = []; end
if ~FILE_STATUS(2); REST2_LR = TMP(2); else REST2_LR = []; end
if ~FILE_STATUS(3); REST1_RL = TMP(3); else REST1_RL = []; end
if ~FILE_STATUS(4); REST2_RL = TMP(4); else REST2_RL = []; end
try
save(output_path,'REST1_LR','REST2_LR','REST1_RL','REST2_RL','-append');
catch
save(output_path,'REST1_LR','REST2_LR','REST1_RL','REST2_RL');
end
%--------------------------------------
end
if flag_IB_stats
% each block has a separate output file
output_path = [dest_folder,'/',subj,'_QC_IB_stats.mat'];
%------------download files------------
FILE = {}; FILE_STATUS = []; TMP = [];% init
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_LR/rfMRI_REST1_LR_Atlas_stats.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_LR/rfMRI_REST2_LR_Atlas_stats.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST1_RL/rfMRI_REST1_RL_Atlas_stats.txt';
FILE{end+1} = 'MNINonLinear/Results/rfMRI_REST2_RL/rfMRI_REST2_RL_Atlas_stats.txt';
for l = 1:length(FILE)
FILE_STATUS(l) = HcpDownloadCommand(subj,FILE{l},dest_folder,'HCP_release',HCP_release);
end
%--------------------------------------
%-------------process files------------
for l = 1:4 % four runs
if ~FILE_STATUS(l)
%load stats
stats = readtable([subj_local_path,'/',FILE{l}]);
% % replace HCP processing path with subjID
% stats.TCSName = subj;
%table2struct
stats = table2struct(stats);
TMP(l).stats= stats;
end
end
%--------------------------------------
%-----------save extracted info--------
if ~FILE_STATUS(1); REST1_LR = TMP(1); else REST1_LR = []; end
if ~FILE_STATUS(2); REST2_LR = TMP(2); else REST2_LR = []; end
if ~FILE_STATUS(3); REST1_RL = TMP(3); else REST1_RL = []; end
if ~FILE_STATUS(4); REST2_RL = TMP(4); else REST2_RL = []; end
try
save(output_path,'REST1_LR','REST2_LR','REST1_RL','REST2_RL','-append');
catch
save(output_path,'REST1_LR','REST2_LR','REST1_RL','REST2_RL');
end
%--------------------------------------
end
if flag_IB_tcs
% each block has a separate output file
output_path = [dest_folder,'/',subj,'_QC_IB_tcs.mat'];
%------------download files------------
FILE = {}; FILE_STATUS = []; TMP = [];% init
count = 0;
for run = {'1','2'}
for phase = {'LR','RL'}
count = count + 1;
run_name = ['rfMRI_REST',run{1},'_',phase{1}];
FILE{end+1} = ['MNINonLinear/Results/',run_name,'/',run_name,'_Atlas.dtseries.nii'];
FILE{end+1} = ['MNINonLinear/Results/',run_name,'/Movement_Regressors.txt']; %re-download just in case RP was not run
FILE{end+1} = ['MNINonLinear/Results/',run_name,'/',run_name,'_WM.txt'];
FILE{end+1} = ['MNINonLinear/Results/',run_name,'/',run_name,'_CSF.txt'];
FILE{end+1} = ['MNINonLinear/Results/',run_name,'/',run_name,'_hp2000.ica/.fix']; %noise
FILE{end+1} = ['MNINonLinear/Results/',run_name,'/',run_name,'_hp2000.ica/filtered_func_data.ica/melodic_mix']; %ICA components
FILE{end+1} = ['MNINonLinear/Results/',run_name,'/',run_name,'_Atlas_hp2000_clean_bias.dscalar.nii']; %bias field
%FILE{end+1} = ['MNINonLinear/Results/',run_name,'/',run_name,'_Atlas_stats.txt'];
% count the number of files per run
if count == 1; NfilesPerRun = length(FILE); end
end
end
for l = 1:length(FILE)
FILE_STATUS(l) = HcpDownloadCommand(subj,FILE{l},dest_folder,'HCP_release',HCP_release);
end
%--------------------------------------
%-------------process files------------
for l = 1:4% four runs
if ~FILE_STATUS( (NfilesPerRun*(l-1) + 1) )
if beacon; sendbeacon(mail,DeltaTime); end
% Run a modified version of the HCP's RestingStateStats
TCs = RestingStateStats_mod( [subj_local_path,'/',FILE{(NfilesPerRun*(l-1) + 2)}] ,...
HP,...%HP
TR,...%TR
[subj_local_path,'/',FILE{(NfilesPerRun*(l-1) + 6)}],...
[subj_local_path,'/',FILE{(NfilesPerRun*(l-1) + 5)}],...%NOISE components
WBCOMMAND,...%WB command
[subj_local_path,'/',FILE{(NfilesPerRun*(l-1) + 1)}],... %input series
[subj_local_path,'/',FILE{(NfilesPerRun*(l-1) + 7)}],...%BIAS
'',...output prefix
'',...dlabel
BCMODE,...bcmode
OUTSTRING,...outstring
[subj_local_path,'/',FILE{(NfilesPerRun*(l-1) + 3)}],...
[subj_local_path,'/',FILE{(NfilesPerRun*(l-1) + 4)}]);
TMP(l).TCs= TCs;
end
end
% %--------------------------------------
%
% %-----------save extracted info--------
if ~FILE_STATUS((NfilesPerRun*0 + 1)); REST1_LR = TMP(1); else REST1_LR = []; end
if ~FILE_STATUS((NfilesPerRun*1 + 1)); REST1_RL = TMP(2); else REST1_RL = []; end %NB: the order is differnt from previous metrics
if ~FILE_STATUS((NfilesPerRun*2 + 1)); REST2_LR = TMP(3); else REST2_LR = []; end
if ~FILE_STATUS((NfilesPerRun*3 + 1)); REST2_RL = TMP(4); else REST2_RL = []; end
try
save(output_path,'REST1_LR','REST2_LR','REST1_RL','REST2_RL','-append');
catch
save(output_path,'REST1_LR','REST2_LR','REST1_RL','REST2_RL');
end
%--------------------------------------
end
%--------------remove files------------
% remove the whole subj folder
system(['rm -r ',subj_local_path]);
%--------------------------------------
%get next subj ID
subj = fgetl(fid);
end
fclose(fid);
return
end