-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitt_processAnatomy.m
351 lines (280 loc) · 11.9 KB
/
pitt_processAnatomy.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
function anatProc = pitt_processAnatomy(baseDir,processDiffusion,force)
% anatProc = pitt_processAnatomy(baseDir,processDiffusion,force)
%
% Process subjects' raw anatomical t1-weighted data through the mrVista
% pipeline. The user can provide a list of subjects as a cell array, or, by
% default this function will recursively search through any directory in
% baseDir and look for the existence of a hidden file (.anatproc) that will
% have be created by this function when it is run successfully. An optinal
% input argument will foce the processing of a subject regardless of the
% existence of the anatproc hidden file. This is useful for those cases
% where a subject's alignment needs to be fixed.
%
% This funciton will:
% (1) Convert the hdr/img pair to nifti file format
% (2) Set anatomical landmarks (ac-pc) using mrAnatAverageAcpcNifti
% (3) Save the resulting image in the 't1' directory as 't1acpc.nii.gz'
%
%
% DIRECTORY ORGANIZATION:
% As of right now the directory structure is:
% DTI -> A229 -> mrDiffusion -> dti60trilin -> dt6.mat [...]
% -> raw -> dti68_1024x1024
% -> t1 -> t1acpc.nii.gz
% INPUT:
% baseDir - The directory which contains the subjects' data
% directories.
%
% force - Boolean flag - setting this flag to true will cause the
% reprocessing of any antomical data that had been
% previously processed regardless of the existence of a
% sorted hidden file.
%
% OUTPUTS:
% anatproc - a cell array containing a list of all the directories
% that were processed by this funciton, successfully.
%
% EXAMPLE USAGE:
% baseDir = '/Users/lmperry/pitt/data/DTI';
% force = false;
% anatProc = pitt_processAnatomy(baseDir,force);
% Supression
%#ok<*AGROW>
%#ok<*WNTAG>
%#ok<*REMFF1>
% NOTES:
%
% **** Think about how to go about checking to make sure that we have
% diffusion data and a valid anatomical image. The anatomical image has to
% be sa*.hdr or ms*.hdr (i'm not exactly sure what the differecnes are but
% it seems like msa is bias-corrected). The cs*.hdr files don't work at all
% -they seem to be image masks or something else strange.
% DONE:
% Might want to do error logging here too. *DONE*
%
% It might be worthwhile seperating these two funcitons (sort and anat) so
% that we don't have to do it all at the same time. I could imagine that we
% might want to be able to search for cases where no anatomy exists and
% allow the user to try to find a valid option... That's something to think
% about.
% TO DO:
% Add a force option to this function and the procDiffusion function.
% * DONE *
%
% Might want to add the ability to reprocess only certain subjects ->
% like processDiffusion
%
% Update the screen outputs so they're more informative. * DONE *
%
% How about a next subject dialog - or quit for now. * DONE **
%% Check Inputs
if notDefined('baseDir');
baseDir = uigetdir(pwd,'Select Base Directory');
if baseDir == 0; error('User cancelled.'), end
end
if notDefined('force')
force = false;
end
if notDefined('processDiffusion')
getFlag = true;
processDiffusion = false;
else
getFlag = false;
end
anatDirs = getAnatDirs(baseDir,force);
%% Initialize error log files
% We want to keep track of those subjects that do not have DTI data or
% ANATOMY data
if ~isempty(anatDirs)
logDir = fullfile(mrvDirup(anatDirs{1},3),'logs');
if ~exist(logDir,'dir'), mkdir(logDir); end
else
logDir = fullfile(baseDir,'logs');
end
log = fopen(fullfile(logDir,'processAnatomy.txt'),'a');
fprintf(log,'\n\n\n\n\n\n-----%s------\n\n',getDateAndTime);
%% Initialize counters
anatProc = {};
ap = 0;
err = 0;
errFlag = false;
%% ANATOMICAL PROCESSING
% IF all subjects are already processed display that to the output window
fprintf('\n[pitt_processAnatomy] ... \n\n%s subjects will be processed...\n',num2str(numel(anatDirs)));
fprintf(log,'\n[pitt_processAnatomy] ... \n%s subjects will be processed...\n',num2str(numel(anatDirs)));
if isempty(anatDirs),
fprintf('All subjects already processed.');
fprintf(log,'\nAll subjects already processed.\n');
end
for ii = 1:numel(anatDirs)
% Here we should define the mrdDir
mrdDir = mrvDirup(anatDirs{ii});
% HERE we should check for and write the "working" file
% HERE: CHECK FOR AND WRITE THE "WORKING" FILE, which will allow us to
% keep track of which subjects are currently being worked on by another
% process.
% Path to the work file
workFile = fullfile(mrdDir,'.workinganatproc');
if ~exist(workFile,'file')
fprintf('\nProcessing %s\n', fileparts(fileparts(anatDirs{ii})));
% Write the working file
workCmd = sprintf('echo %s > %s',getDateAndTime,workFile);
system(workCmd);
anatFile = dir([anatDirs{ii} '/*.hdr']);
anatFile = fullfile(anatDirs{ii},anatFile.name);
if exist(anatFile,'file') && ~isdir(anatFile) % Hack because of the empty cell above!
[~, f ~] = fileparts(anatFile);
anatOutFile = fullfile(anatDirs{ii},[f '.nii.gz']);
t1AcpcFile = fullfile(anatDirs{ii},'t1acpc.nii.gz');
% Convert structural image to NIFTI
if ~exist(t1AcpcFile,'file')
cmd = ['fslchfiletype NIFTI_GZ ' anatFile ' ' anatOutFile];
[status result] = system(cmd);
if status == 0
% Do the acpc alignment - prompt the user to make sure it's good.
an = 0;
while an ~= 1 || isempty(an)
mrAnatAverageAcpcNifti(anatOutFile,t1AcpcFile);
an = questdlg('Does the alignment look good?','ACPC ALIGNMENT','YES','NO','YES');
if strcmp('YES',an), an = 1;
try
close 1 2 3;
catch cmsg %#ok<NASGU>
warning('WINDOWS CANNOT BE CLOSED... PLEASE CLOSE ALL WINDOWS MANUALLY')
end
else an = 0;
end
end
else
fprintf(log,'%s \n\t %s\n',anatDirs{ii},result);
end
end
% HERE: REMOVE THE "WORKING FILE"
delete(workFile);
% AFTER IT'S ALL SAID AND DONE
% Check that the anat file has been written to disk.
if exist(t1AcpcFile,'file')
anatCmd = sprintf('echo %s > %s',getDateAndTime,fullfile(mrdDir,'.anatproc'));
system(anatCmd);
% Track the processed directories for return
ap = ap+1;
anatProc{ap} = anatDirs{ii};
% Prompt the user to continue
if ii<numel(anatDirs) && ii~=1
prompt = sprintf('Subject %d of %d: \n Would you like to continue with the next subject?',ii+1,numel(anatDirs));
answ = questdlg(prompt,'ACPC ALIGNMENT','YES','NO','YES');
if strcmp('YES',answ), answ = 1; else answ = 0; end
if answ ~=1
warning('User Cancelled: pitt_processAnatomy');
return
end
end
else
fprintf('Error: %s was not created.\n',t1AcpcFile);
errFlag = true;
fprintf(log,'Error in: %s\n %s was not found.',anatDirs{ii},t1AcpcFile);
err = err + 1;
subsErr{err} = anatDirs{ii};
end
else
fprintf('Error in: %s\n',anatFile);
errFlag = true;
fprintf(log,'Error in: %s\n',anatFile);
end
% HERE: END THE CHECK FOR THE "WORKING FILE"
else
fprintf('\nSkipping %s - "working" file found.\n',anatDirs{ii});
end
end
%% Show some outputs to the command window that will show which subjects
% have been processed correctly and which had errors.
% If the anat files are missing, or if there was a problem with the
% nifti conversion then throw the error flag.
if errFlag
fprintf('\nError(s) occurred. Please check the logFile: \n');
fprintf('\nThe following subjects returned errors:\n');
fprintf(log,'\nThe following subjects returned errors:\n');
for e = 1:numel(subsErr)
fprintf('%s\n',subsErr{e});
fprintf(log,'%s\n',subsErr{e});
end
else
fprintf('\nNo errors occurred during execution.\n');
fprintf(log,'\nNo errors occurred during execution.\n');
end
if~isempty(anatDirs)
fprintf('\nThe following subjects were processed successfully:\n');
fprintf(log,'\nThe following subjects were processed successfully:\n');
for s = 1:numel(anatDirs)
fprintf('%s\n',anatDirs{s});
fprintf(log,'%s\n',anatDirs{s});
end
end
%% Send the anatDirs out to pitt_preprocessDiffusion
% Determine if we should pass the output to the diffusion function
if getFlag == 1 && ~isempty(anatDirs)
processDiffusion = getDiffusionFlag;
end
% Initialize diffusion processing.
if processDiffusion == 1 && ~isempty(anatDirs)
% ??? Will this work with dti proc? It may have to be numbers - not full paths.
% Get to the raw level of the subject's directory
for dd = 1:numel(anatProc)
anatProc{dd} = fullfile(mrvDirup(anatProc{dd}),'raw');
end
fprintf('\nSending subjects to pitt_preprocessDiffusion to process diffusion data...\n');
pitt_preprocessDiffusion(anatProc);
end
return
%% :::: FUNCTIONS ::::
% ::: getAnatDirs ::: Return a cell array with the full paths to all
% subject's anatomy directories.
function anatDirs = getAnatDirs(baseDir,force)
if ~iscell(baseDir)
dirsTextFile = fullfile(baseDir,'.anatDirs.txt');
cmd = sprintf('ls -1 %s > %s', baseDir, dirsTextFile); system(cmd);
dirs = textread(dirsTextFile,'%s');
cmd = sprintf('rm %s', dirsTextFile); system(cmd);
for i = sort(1:numel(dirs),'descend');
if ~force && isdir(fullfile(baseDir,dirs{i},'mrDiffusion','t1')) ...
&& ~exist(fullfile(baseDir,dirs{i},'mrDiffusion','.anatproc'),'file') ...
&& exist(fullfile(baseDir,dirs{i},'mrDiffusion','.sorted'),'file')
dirs{i} = fullfile(baseDir,dirs{i},'mrDiffusion','t1');
elseif force && isdir(fullfile(baseDir,dirs{i},'mrDiffusion','t1'))
dirs{i} = fullfile(baseDir,dirs{i},'mrDiffusion','t1');
else
dirs(i) = [];
end
end
anatDirs = dirs;
else
% What was passed in must be a cell array of anatomy directories.
% but we should check for the existence of anatproc!!! && sorted!!!
dirs = baseDir;
for i = sort(1:numel(dirs),'descend');
if ~force && isdir(fullfile(dirs{i})) ...
&& ~exist(fullfile(mrvDirup(dirs{i}),'.anatproc'),'file') ...
&& exist(fullfile(mrvDirup(dirs{i}),'.sorted'),'file')
dirs{i} = fullfile(dirs{i});
elseif force && isdir(dirs{i})
dirs{i} = fullfile(dirs{i});
else
dirs(i) = [];
end
end
anatDirs = dirs;
end
return
% :::: getAnatFlag :::: Prompt user to process anatomical images now or
% later - which will send the anat dirs to pitt_processAnatomy.m
function processDiffusion = getDiffusionFlag
response = questdlg('Would you like to process diffusion data NOW? If you select "NO" you can run pitt_preprocessDiffusion at a later time.','pitt_processAnatomy');
switch lower(response)
case {'yes'}
processDiffusion = true;
case {'no'}
processDiffusion = false;
case {'cancel'}
error('User cancelled');
end
return