-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_BIDS_App.m
293 lines (255 loc) · 10.3 KB
/
spm_BIDS_App.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
% SPM BIDS App
% SPM: http://www.fil.ion.ucl.ac.uk/spm/
% BIDS: http://bids.neuroimaging.io/
% App: https://github.com/BIDS-Apps/SPM/
%
% See also:
% BIDS Validator: https://github.com/INCF/bids-validator
% Copyright (C) 2016-2017 Wellcome Trust Centre for Neuroimaging
% Guillaume Flandin
% $Id$
%==========================================================================
%-BIDS App structure
%==========================================================================
BIDS_App = struct(...
'dir','', ... % BIDS root directory
'outdir','', ... % output directory
'level','', ... % first or second level analysis [participant*,group*]
'participants',{{}}, ... % label of participants to be considered
'config','',... % configuration script
'temp',true); % create local temporary copy of input files
%==========================================================================
%-Input arguments
%==========================================================================
if numel(inputs) == 0, inputs = {'--help'}; end
if numel(inputs) == 1
switch inputs{1}
case {'-v','--version'}
fprintf('%s BIDS App, %s %s, version %s\n',...
spm('version'), upper(spm_check_version), version, ...
deblank(fileread('/version')));
case {'-h','--help'}
fprintf([...
'Usage: bids/spm BIDS_DIR BIDS_MODEL OUTPUT_DIR LEVEL [OPTIONS]\n',...
' bids/spm [ -h | --help | -v | --version ]\n',...
'\n',...
'Mandatory inputs:\n',...
' BIDS_DIR Input directory following the BIDS standard\n',...
' BIDS_MODEL Input file following the BIDS-model standard\n',...
' OUTPUT_DIR Output directory\n',...
' LEVEL Level of the analysis that will be performed\n',...
' {participant,group}\n',...
'\n',...
'Options:\n',...
' --participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]\n',...
' Label(s) of the participant(s) to analyse\n',...
' -h, --help Print usage\n',...
' -v, --version Print version information and quit\n']);
case {'--gui'}
cd(spm('Dir'));
waitfor(spm_Welcome);
waitfor(spm_figure('FindWin','Menu'));
exit(0);
otherwise
fprintf([...
'bids/spm: ''%s'' is not a valid syntax.\n',...
'See ''bids/spm --help''.\n'],inputs{1});
end
exit(0);
end
if numel(inputs) < 2
error('An output directory has to be specified.');
elseif numel(inputs) < 3
error('Missing argument participant/group.');
end
BIDS_App.dir = inputs{1};
BIDS_App.model = inputs{2};
BIDS_App.outdir = inputs{3};
BIDS_App.level = inputs{4};
i = 5;
while i <= numel(inputs)
arg = inputs{i};
switch arg
case '--participant_label'
arg = 'participants';
case '--config'
arg = 'config';
otherwise
warning('Unknown input argument "%s".',arg);
arg = strtok(arg,'-');
end
j = 1;
while true
i = i + 1;
if i <= numel(inputs)
if inputs{i}(1) == '-', break; end
BIDS_App.(arg){j} = inputs{i};
j = j + 1;
else
break;
end
end
end
%==========================================================================
%-Validation of input arguments
%==========================================================================
%- bids_dir
%--------------------------------------------------------------------------
if ~exist(BIDS_App.dir,'dir')
error('BIDS directory "%s" does not exist.',BIDS_App.dir);
end
%- bids_model
%--------------------------------------------------------------------------
if ~exist(BIDS_App.model,'file')
error('BIDS-model file "%s" does not exist.',BIDS_App.dir);
end
%- level [participant/group] & output_dir
%--------------------------------------------------------------------------
if strncmp('participant',BIDS_App.level,11)
if ~exist(BIDS_App.outdir,'dir')
sts = mkdir(BIDS_App.outdir);
if ~sts
error('BIDS output directory could not be created.');
end
end
elseif strncmp('group',BIDS_App.level,5)
if ~exist(BIDS_App.outdir,'dir')
error('BIDS output directory "%s" does not exist.',BIDS_App.outdir);
end
else
error('Unknown analysis level "%s".',BIDS_App.level);
end
%-Configuration file
%--------------------------------------------------------------------------
if ~isempty(BIDS_App.config)
if numel(BIDS_App.config) > 1
error('More than one configuration file provided.');
end
BIDS_App.config = char(BIDS_App.config);
if isempty(fileparts(BIDS_App.config))
BIDS_App.config = fullfile(fileparts(mfilename('fullpath')),BIDS_App.config);
end
if isempty(spm_file(BIDS_App.config,'ext'))
BIDS_App.config = [BIDS_App.config '.m'];
end
if ~spm_existfile(BIDS_App.config)
error('Cannot find configuration file "%s".',BIDS_App.config);
end
else
BIDS_App.config = fullfile(fileparts(mfilename('fullpath')),...
['pipeline_' BIDS_App.level '.m']);
if ~spm_existfile(BIDS_App.config)
error('No default configuration file found for "%s" level.',BIDS_App.level);
end
end
%==========================================================================
%-Parse BIDS directory and validate list of participants
%==========================================================================
%-Call BIDS Validator
%--------------------------------------------------------------------------
[status, result] = system('bids-validator --version');
if ~status
[status, result] = system(['bids-validator "' BIDS_App.dir '"']);
if status~=0
fprintf('%s\n',result);
exit(1);
end
end
%-Parse BIDS directory
%--------------------------------------------------------------------------
BIDS = spm_BIDS(BIDS_App.dir);
% spm('Run', fullfile(fileparts(mfilename('fullpath')),'spm_BIDS_update.m'));
%- --participant_label
%--------------------------------------------------------------------------
if isempty(BIDS_App.participants)
BIDS_App.participants = spm_BIDS(BIDS,'subjects');
else
df = setdiff(BIDS_App.participants,spm_BIDS(BIDS,'subjects'));
if ~isempty(df)
error('Participant directory "%s" does not exist.',df{1});
end
end
BIDS_App.participants = cellfun(@(s) ['sub-' s], ...
BIDS_App.participants, 'UniformOutput',false);
%==========================================================================
%-SPM Initialisation
%==========================================================================
spm('defaults','fmri');
spm_jobman('initcfg');
%==========================================================================
%-(Temporary) Copy of input data and uncompress image files
%==========================================================================
atExit = '';
BIDS_App.tmpdir = BIDS_App.dir;
if strncmp('participant',BIDS_App.level,11) && ~isempty(BIDS_App.participants)
% if BIDS_App.temp
% %-Create temporary directory
% %------------------------------------------------------------------
% BIDS_App.tmpdir = BIDS_App.outdir;
% %BIDS_App.tmpdir = tempname(BIDS_App.outdir);
% %sts = mkdir(BIDS_App.tmpdir);
% %if ~sts
% % error('Output temporary directory could not be created.');
% %end
% %atExit = onCleanup(@() rmdir(BIDS_App.tmpdir,'s'));
% %-Copy participants' data
% %------------------------------------------------------------------
% for s=1:numel(BIDS_App.participants)
% %fprintf('Temporary directory: %s\n',...
% % fullfile(BIDS_App.tmpdir,BIDS_App.participants{s}));
% sts = copyfile(fullfile(BIDS_App.dir,BIDS_App.participants{s}),...
% fullfile(BIDS_App.tmpdir,BIDS_App.participants{s}));
% if ~sts
% error('Data could not be temporarily copied.');
% end
% end
% end
% %-Uncompress gzipped NIfTI files
% %----------------------------------------------------------------------
% for s=1:numel(BIDS_App.participants)
% niigz = spm_select('FPListRec',...
% fullfile(BIDS_App.tmpdir,BIDS_App.participants{s}),'^.*\.nii\.gz$');
% if ~isempty(niigz)
% niigz = cellstr(niigz);
% for i=1:numel(niigz)
% gunzip(niigz{i});
% delete(niigz{i});
% end
% end
% end
%-Update BIDS structure to point to new local files
%----------------------------------------------------------------------
BIDS = spm_changepath(BIDS,BIDS.dir,BIDS_App.tmpdir);
BIDS = spm_changepath(BIDS,'.nii.gz','.nii');
end
%-Simplify BIDS structure to only contain participants under study
%--------------------------------------------------------------------------
[idx,jdx] = ismember({BIDS.subjects.name},BIDS_App.participants);
jdx = jdx(idx); idx = find(idx);
BIDS.subjects = BIDS.subjects(idx(jdx));
%==========================================================================
%-Analysis level: participant*
%==========================================================================
if strncmp('participant',BIDS_App.level,11)
BIDS_ORIG = BIDS;
for s=1:numel(BIDS_App.participants)
BIDS = BIDS_ORIG;
BIDS.subjects = BIDS.subjects(s);
spm('FnBanner',['BIDS ' upper(BIDS_App.level) ' ' BIDS.subjects.name]);
spm('Run',BIDS_App.config);
BIDS = BIDS_ORIG;
end
% make sure relevant files are stored in BIDS_App.outdir
end
%==========================================================================
%-Analysis level: group*
%==========================================================================
if strncmp('group',BIDS_App.level,5)
spm('FnBanner',['BIDS ' upper(BIDS_App.level)]);
spm('Run',BIDS_App.config);
% make sure relevant files are stored in BIDS_App.outdir
end
%==========================================================================
%-Delete temporary files and exit
%==========================================================================
%delete(atExit);