-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitt_createFsRois.m
159 lines (118 loc) · 4.19 KB
/
pitt_createFsRois.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
function [subs n] = pitt_createFsRois(baseDir,procStage)
%
% function subs = pitt_getSubs(baseDir)
%
% This funtion will return a cell array of subjects' directories that contains
% which subjects need to be processed at a given stage of the pipeline.
%
% Author: LMP 2012
%
%#ok<*REMFF1>
%% Check INPUT
if notDefined('baseDir')
baseDir = uigetdir(pwd,'Select a Base Directory');
end
if ~exist('procStage','var') || ~isstr(procStage)
procStage = 'freeseg';
end
%% Initialize the structure and the counters
subs.sort = {};
subs.anatproc = {};
subs.dti = {};
subs.freeseg = {};
subs.wbfibertrack = {};
subs.morifibertrack = {};
sns = 0;
sna = 0;
snd = 0;
snfs = 0;
snwbf = 0;
snmf = 0;
%% Return a cell array with the full paths to all subject directories
dirsTextFile = fullfile(baseDir,'.dirs.txt');
cmd = sprintf('ls -1 %s > %s', baseDir, dirsTextFile); system(cmd);
dirs = textread(dirsTextFile,'%s');
cmd = sprintf('rm %s', dirsTextFile); system(cmd);
% Loop over the array and return only those that are directories
for i = sort(1:numel(dirs),'descend');
if isdir(fullfile(baseDir,dirs{i})) && ~strcmp(dirs{i},'logs') && ~strcmp(dirs{i},'freesurfer')
dirs{i} = fullfile(baseDir,dirs{i});
else
dirs(i) = [];
end
end
% Return the total number of directories
n = numel(dirs);
%% Loop over the dirs and find which subjects need to be processed at each
% stage of the pipeline.
% Do it all here
% This is going to be a bunch of if statements to check if a given
% subject has the hidden files left behind after a given stage in the
% pipeline has been run.
for ii=1:numel(dirs)
mrdir = fullfile(dirs{ii},'mrDiffusion');
sorted = fullfile(mrdir,'.sorted');
anatproc = fullfile(mrdir,'.anatproc');
dtiproc = fullfile(mrdir,'.dtiproc');
fseg = fullfile(mrdir,'.freeseg');
wbfiberproc = fullfile(mrdir,'.wholebrainfiberproc');
morifiberproc = fullfile(mrdir,'.morifiberproc');
% Check for subjects that need to be sorted.
if ~exist(sorted,'file')
sns = sns+1;
subs.sort{sns} = dirs{ii};
end
% Check for subjects that need anatomy file processed
if ~exist(anatproc,'file') && exist(sorted,'file')
sna = sna+1;
subs.anatproc{sna} = dirs{ii};
end
% Check for subjects that need diffusion data processed
if ~exist(dtiproc,'file') && exist(anatproc,'file')
snd = snd+1;
subs.dti{snd} = dirs{ii};
end
% Check for subjects that need freesurfer segmentation done
if exist(fseg,'file') && exist(anatproc,'file')
snfs = snfs+1;
subs.freeseg{snfs} = dirs{ii};
end
% Check for subjects that need whole-brain fibers created
if ~exist(wbfiberproc,'file') && exist(dtiproc,'file')
snwbf = snwbf+1;
subs.wbfibertrack{snwbf} = dirs{ii};
end
% Check for subjects that need mori-fibers created
if ~exist(morifiberproc,'file') && exist(wbfiberproc,'file') && exist(dtiproc,'file')
snmf = snmf+1;
subs.morifibertrack{snmf} = dirs{ii};
end
end
%% Return subs cell array to the user switching on procStage input
switch lower(procStage)
case 'sort'
subs = subs.sort;
case 'anatproc'
subs = subs.anatproc;
case 'dtiproc'
subs = subs.dti;
case 'freeseg'
subs = subs.freeseg;
case {'wbfibertrack', 'wholebrainfibertrack'}
subs = subs.wbfibertrack;
case {'morifibertrack', 'mori'}
subs = subs.morifibertrack;
end
for ii=1:numel(subs)
sd = fullfile(baseDir,'freesurfer');
% Set up the path to files needed to run the recon
mrdDir = fullfile(subs{ii},'mrDiffusion');
[~, subID ~] = fileparts(subs{ii});
% Path to the segfile
segFile = fullfile(sd,subID,'mri','aparc+aseg.mgz');
fprintf('\nRunning subject %s...\n',subID);
% CREATE ROIS: Here we will create the ROIs for each subject.
fprintf(' Creating freesurfer ROIs from the segmentation file: %s\n', segFile);
fs_roisFromAllLabels([segFile(1:end-4) '.nii.gz'],fullfile(mrdDir,'ROIs','freesurfer'),'nifti',fullfile(mrdDir,'t1','t1acpc.nii.gz'));
end
return