-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNMRI225_run.m
229 lines (182 loc) · 5.1 KB
/
NMRI225_run.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
%% This script will generate a DARTEL normalized template from bigger FOV images
root_dir=[pwd '/'];
cfg=[];
cfg.rootdir=root_dir;
cfg.channels={'hrT1','hrFLAIR'};
cfg.fov=0;
cfg.vx=1.5;
cfg.bet_f=0.45;
cfg.sm=0;
cfg.make_tmpl=1;
cfg.autoset_origin=0;
cfg.coreg=2; % always coregister
cfg.DARTEL_classes=[1 1 1 1 1 0]; % use all classes
cfg.mask_grp=0;
cfg.mask_ind=0;
cfg.modulated=[0 0 0 0 0 0];
cfg.unmodulated=[1 1 1 1 1 0];
cfg.group='controls';
cfg.ext_temp='';
% bb = [[-120 -140 -130];[120 120 120]]; % bigger BB
bb = [[-100 -140 -130];[100 120 120]]; % MNI bigger BB
C=2;
if (~isempty(cfg.group))
group=[cfg.group '.'];
else
group='';
end
if isnumeric(cfg.vx)
switch length(cfg.vx)
case 1
vox = [ cfg.vx cfg.vx cfg.vx ];
if (cfg.vx==1.5)
vxs='';
else
vxs=['.' num2str(cfg.vx)];
end
case 3
vox = cfg.vx;
vxs = ['.' num2str(cfg.vx(1)) 'x' num2str(cfg.vx(2)) 'x' num2str(cfg.vx(3))];
otherwise
error('Voxelsize wrong, not 3 or 1-dimensional');
end
else
error('Voxel size is not numeric')
end
%% Unzip the files, if needed
l=dir(fullfile(root_dir,'storage','*','anat','*.gz'));
for n=1:length(l)
fprintf('Unzipping %s (%d/%d)\n',l(n).name,n,length(l))
system(['gzip -d ' fullfile(l(n).folder,l(n).name)]);
end
%% Setup Files
l=dir(fullfile(root_dir,'storage','*','anat','*.hrT1.nii'));
% now make a file list
Hfiles={{},{}}; % empty 2-channel cell array
for n=1:length(l)
% this is present
T1=fullfile(l(n).folder,l(n).name);
% now check FLAIR
bn=get_basename_or_root(l(n).name);
putative=dir(fullfile(l(n).folder,['*' bn '.hrFLAIR*.nii']));
if ~isempty(putative)
% found also FLAIR, add file
Hfiles{1}=[Hfiles{1},{T1}];
Hfiles{2}=[Hfiles{2},{fullfile(putative.folder,putative.name)}];
end
end
%% Deal with sorting of files in VBM folder
%check native files
cfg.files=[];
for i=1:length(cfg.channels)
if ~isempty(cfg.group)
chandir=fullfile(root_dir,[cfg.group '.' cfg.channels{i} '.native']);
else
chandir=fullfile(root_dir,[cfg.channels{i} '.native']);
end
if ~exist(chandir,'dir')
mkdir(chandir)
fprintf('Created native space dir for channel=%s\n', cfg.channels{i});
end
% now parse images
madelinks=0;
for imgi=1:length(Hfiles{i})
[pa, fi, ext]=fileparts(strtok(Hfiles{i}{imgi},','));
if ~strcmp(pa,chandir)
if ~exist(fullfile(chandir,[fi ext]),'file')
[status, cmdout]=system(['ln -s "' strtok(Hfiles{i}{imgi},',') '" "' fullfile(chandir,[fi ext]) '"']);
if status~=0
error(['Could not make symbolic link. Error=' cmdout])
else
cfg.files{i}{imgi}=fullfile(chandir,[fi ext]);
madelinks=madelinks+1;
end
else
cfg.files{i}{imgi}=fullfile(chandir,[fi ext]);
end
end
end
if madelinks>0
fprintf('Made %d symbolic links for channel=%s\n',madelinks,cfg.channels{i});
end
end
%% Setup Directories
Hnativedir=cell(1,C);
for c=1:C
% check for dir
Hnativedir{c}=fullfile(root_dir,[group cfg.channels{c} '.native']);
if ~exist(Hnativedir{c},'dir')
error(['Native dir not found for group: ' cfg.channels{c} ', ' Hnativedir{c}])
end
end
if length(cfg.channels)>1
chtxt=[strjoin(cfg.channels,'_') '.'];
else
chtxt='';
end
Hsegdir=fullfile(root_dir,[group chtxt 'segmented12/' ]);
if (~exist(Hsegdir,'dir'))
mkdir(Hsegdir)
end
Ddir=fullfile(root_dir,[group chtxt 'dartel/' ]);
if (~exist(Ddir,'dir'))
mkdir(Ddir)
end
Resdir=fullfile(root_dir,'results/');
if (~exist(Resdir,'dir'))
mkdir(Resdir)
end
Tmpldir=fullfile(root_dir,'templates/');
if (~exist(Tmpldir,'dir'))
mkdir(Tmpldir)
end
DNdir=[root_dir group chtxt 'normalized' vxs '/' ];
if (~exist(DNdir,'dir'))
mkdir(DNdir)
end
%% Use the runner for the intial works
% actually, the origin setting is optional and only sensible if
% autoset_origin is enabled, which is not the case here
%
% cfg.jobs={'origin'};
%
% qcfg=[];
% qcfg.compile=1;
% qcfg.spm=1;
% qcfg.hold=1;
% qcfg.title='Set_origin_subjs';
% nmri_parrun(qcfg,'nmri_VBM_runner',cfg)
%% Set the Hfiles after origin setting
% for c=1:C
% Hfiles{c}=get_scans(cfg.files{c},Hnativedir{c},'o');
% end
% not needed if origin not re-set
%% now run the segment of T1 + FLAIR
cfg.jobs={'segment'};
%cfg.files=Hfiles; % set to re-oriented version
qcfg=[];
qcfg.compile=0;
qcfg.spm=1;
qcfg.hold=1;
qcfg.title=['Segment_T1-FLAIR_subjs'];
nmri_parrun(qcfg,'nmri_VBM_runner',cfg)
% nmri_VBM_runner(cfg)
%% check the segmentation output
system(['slicesdir ' fullfile(Hsegdir,'c1*.nii')])
system(['firefox ' fullfile(root_dir,'slicesdir/index.html')])
% this will open the slicesdir in firefox
%% now do intensity normalization
for c=1:C
% check for resampled/coreg first
Hbias=get_scans(cfg.files{c},Hsegdir,'m(r)?(o)?');
if isempty(Hbias)
error('Could not find the bias-corrected scans for this channel...should not happen. Probably there was a fault upstream, try to re-run completely')
end
% intensity normalize the BIAS corrected scans by grey matter, re-use the
% normalized FLAIR script for this
nf_process_FLAIR_flexible(char(Hbias),0,Hsegdir,Hsegdir,3,[],[],'');
end
%% The end
% In the v2 processing, we will not do more than that. The spatial
% normalization and template making will be done via the Python script,
% see process.py