-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitt_segmentAnatomy.m
204 lines (141 loc) · 6.13 KB
/
pitt_segmentAnatomy.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
function subsProc = pitt_segmentAnatomy(baseDir)
%
% subsProc = pitt_segmentAnatomy(baseDir)
%
% Run freesurfer automatic segmentation on all subjects in 'baseDir' This
% function saves out the results of the segmentations to the baseDir
% withing a directory called 'freesurfer'. Within that directory can be
% found all the output files form the initial freesurfer run.
%
%
% INPUTS:
% baseDir - directory contatining all subjects that have data to be
% processed.
%
% OUTPUTS:
% subsProc - A cell array containing the paths to those subjects
% that were processed during the running of this
% function.
%
%
% Author: LMP [2012]
%% INPUTS
fprintf('\n [%s] \n', mfilename);
if notDefined('baseDir')
baseDir = uigetdir(pwd,'Select base directory');
end
%% Get subjects to process and advise the user RE time
subs = pitt_getSubs(baseDir,'freeseg');
% warn = sprintf('This process will run FREESURFER recon-all on %d subjects.\n This will take ~%d hours!\n Do you want to continue?',numel(subs),numel(subs)*10);
% resp = questdlg(warn,'pitt_segmentAnatomy','YES','NO','YES');
% if strcmp(resp,'NO')
% disp('User cancelled.')
% return
% end
%% Set subjects_dir env variable
sd = fullfile(baseDir,'freesurfer');
if ~exist(sd,'dir'), mkdir(sd); end
setenv('SUBJECTS_DIR',sd);
%% Initialize and format the log file
if ~isempty(subs)
logDir = fullfile(baseDir,'logs');
if ~exist(logDir,'dir'), mkdir(logDir); end
logFileName = fullfile(logDir,'segmentAnatomy.txt');
log = fopen(logFileName,'a');
fprintf(log,'\n\n\n\n\n\n-----%s------\n\n',getDateAndTime);
fprintf(log,'Running freesurfer for: %d subjects \n---\n',numel(subs));
fprintf('Running freesurfer for %d subjects...\n---\n',numel(subs));
else
fprintf(' No subjects found that can be segmented at this time.\n');
return
end
sp = 0;
subsProc = {};
err = 0;
errFlag = false;
%% Loop over subs and segment anatomicals using freesurfer
for ii=1:numel(subs)
% Set up the path to files needed to run the recon
mrdDir = fullfile(subs{ii},'mrDiffusion');
t1 = fullfile(mrdDir,'t1','t1acpc.nii.gz');
[~, subID ~] = fileparts(subs{ii});
% Path to the segfile
segFile = fullfile(sd,subID,'mri','aparc+aseg.mgz');
% Path to the work file
workFile = fullfile(mrdDir,'.workingfreeseg');
% Check for "working file": Only move forward if it does not exist. If
% it does then we'll skip to the end of this loop.
if ~exist(workFile,'file')
% In case this subject has been started previously, remove the
% directory and start anew.
if exist(fullfile(sd,subID),'dir') && ~exist(segFile,'file')
rmdir(fullfile(sd,subID),'s');
end
% Write the working file
workCmd = sprintf('echo %s > %s',getDateAndTime,workFile);
system(workCmd);
fprintf(log,'\nRunning subject %s...',subID); fprintf('\nRunning subject %s...\n',subID);
tic;
% RUN THE RECON-ALL: Build the recon command and run it
% ** This is what takes all the time **
cmd = sprintf('recon-all -i %s -subjid ''%s'' -all', t1, subID);
[status, ~] = system(cmd,'-echo');
endtime = num2str(toc/60);
% Check to see if it ran correctly and if it did write the hidden file
% that will signal that subject has been processed
if status == 0 && exist(segFile,'file')
fprintf(log,'success!\n');
fprintf('\n %s processed successfully.\n',mrvDirup(mrdDir));
sp = sp+1;
subsProc{sp} = mrvDirup(mrdDir);
tmgCmd = sprintf('echo %s > %s',getDateAndTime,(fullfile(mrdDir,'.freeseg')));
system(tmgCmd);
% CREATE ROIS: Here we will create the ROIs for each subject.
fprintf(' Creating freesurfer ROIs from the segmentation file: %s\n', segFile);
fprintf(log,' Creating freesurfer ROIs from the segmentation file: %s\n', segFile);
% First line will create the rois in .mat format for tracking with
% conTrack- the next line will then create the ROIs in nifti format
% for visualization in other viewers
fs_roisFromAllLabels(segFile,fullfile(mrdDir,'ROIs','freesurfer'),'mat',fullfile(mrdDir,'t1','t1acpc.nii.gz'));
fs_roisFromAllLabels([segFile(1:end-4) '.nii.gz'],fullfile(mrdDir,'ROIs','freesurfer'),'nifti',fullfile(mrdDir,'t1','t1acpc.nii.gz'));
fprintf(' fs_roisFromAllLabels: Success. \n Time elapsed: %s minutes.\n', endtime);
fprintf(log,' fs_roisFromAllLabels: Success. \n Time elapsed: %s minutes.\n', endtime);
else
if exist(workFile,'file')
fprintf(log,'subject being processed by another MATLAB');
else
fprintf(log,'failed.\n');
errFlag = true;
err = err+1;
subsErr{err} = subs{ii};
end
end
% Remove the working file
delete(workFile);
else
% Working file exists - so we're here.
fprintf('Another process is working on %s. Moving on...',subs{ii});
end
end
%% Show some outputs in the command window that will show which subjects
% have been processed correctly and which had errors.
if errFlag
fprintf('\nError(s) occurred. Please check the logFile: \n \t %s\n',logFileName);
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');
end
fprintf('\nThe following subjects were processed successfully:\n');
fprintf(log,'\nThe following subjects were processed successfully:\n');
for s = 1:numel(subsProc)
fprintf('%s\n',subsProc{s});
fprintf(log,'%s\n',subsProc{s});
end
fprintf(log,'\n---END---\n');
return
%#ok<*AGROW>