forked from ccarey/CBS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getsubject.m
executable file
·348 lines (268 loc) · 10.3 KB
/
getsubject.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
function [] = getsubject(subjectid,boldruns,structrun,fmruns,destpath,analysisdirs,usesingle,dicompath)
%GETSUBJECT Get subject data from the network and convert to spm.
% GETSUBJECT(subjectid, boldruns, structrun,fmruns,destpath[,dicompath]) downloads
% data using cbsget, processes it with spm and renames the files and
% puts them in an ordered directory structure.
%
% subjectid: the name of the subject in CBSCentral (in single quotes, e.g. 'my_subj_name')
% boldruns: the run numbers for the BOLD scans (in brackets if more than one run, e.g. 1 or [1,2])
% structrun: the run number for the structural scan (same as boldruns)
% fmruns: the run numbers for the fieldmap scan (same as boldruns)
% The fieldmap is optional. If you don't have one, put empty brackets, e.g. []
%
% destpath: the path where this data will go after conversion (in signle quotes, e.g. '/users/me/data')
% dicompath: the path to dicoms, in the event that you don't want to run
% cbsget, but you do have the dicoms already unzipped in a folder.
%% Do error checking on the inputs
% Has this been run before?
dircontents = dir([destpath '/' subjectid]);
%now checkedin python script
% if length(dircontents)~=0
% error([destpath '/' subjectid ' appears to have contents. Please pull data to a location with no contents to avoid file collisions.'])
% end
% Get current wd
startingwd = pwd;
% Keep track of the directories we create
newdirs = {};
%% If we're not using cbs_get, copy the files
if ~isempty(dicompath)
disp('Not running cbsget: copying files instead')
[status result] = system(['mkdir ' fullfile(destpath,subjectid)]);
if status~=0
error(['getsubject could not be run successfully! The subject directory may already exist.' 10 result])
end
newdirs{end+1} = fullfile(destpath,subjectid);
[status result] = system(['mkdir ' fullfile(destpath,subjectid,'RAW')]);
if status~=0
rmdirs(newdirs)
error(['getsubject could not be run successfully, is the path correct?' 10 result])
end
newdirs{end+1} = fullfile(destpath,subjectid,'RAW');
disp('Copying files as follows:')
cpcmd = ['cp ' dicompath '/* ' fullfile(destpath,subjectid,'RAW')];
disp(cpcmd);
[status result] = system(['cp ' dicompath '/* ' fullfile(destpath,subjectid,'RAW')]);
if status~=0
rmdirs(newdirs)
error(['getsubject could not be run successfully!' 10 result])
end
else
%% Construct the call to cbs_get
% An example call is:
% cbsget -r 4,5,6,7 -s 120418_spmtest
allruns = [boldruns structrun fmruns];
runstr = '';
for runnum = 1:length(allruns)
runstr = [runstr num2str(allruns(runnum)) ','];
end
% remove the last extraneous comma
runstr = runstr(1:end-1);
%cbscmd = ['cbsget -r ' runstr ' -s ' subjectid];
cbscmd = ['ArcGet.py -a cbscentral -r ' runstr ' -s ' subjectid ' -o ' destpath];
disp('Running ArcGet as follows:')
disp(cbscmd)
[status,result] = system(cbscmd);
if status~=0
rmdirs(newdirs)
error(['ArcGet could not be run successfully, make sure your config file is set up correctly and the MR session ID is correct!' 10 result])
end
%% Unzip the files to the specified directory
%unzipcmd = ['unzip ' subjectid '.zip -d ' destpath];
%disp('Unzipping data as follows:')
%disp(unzipcmd)
%[status,result] = system(unzipcmd);
%if status~=0
% rmdirs(newdirs)
% error(['DICOM archive could not be unzipped successfully!' 10 result])
%end
lscmd = ['ls ' destpath '/' subjectid '/RAW'];
disp('listing RAW directory:')
disp(lscmd)
[status,result] = system(lscmd);
if status~=0
% rmdirs(newdirs)
error(['Raw directory not successfully created!' 10 result])
end
disp('...complete!')
end
%% Create the directory structure
dirnames = {analysisdirs{1,:},'batch','preproc','output_files'};
for d = 1:length(dirnames)
mkcmd = ['mkdir ' destpath '/' subjectid '/' dirnames{d}];
disp('Making new directory as follows:')
disp(mkcmd)
[status,result] = system(mkcmd);
if status~=0
rmdirs(newdirs)
error(['mkdir command could not be run successfully!' 10 result])
end
newdirs{end+1} = [destpath '/' subjectid '/' dirnames{d}];
end
for d = 1:length(analysisdirs)
mkcmd = ['mkdir ' destpath '/' subjectid '/' analysisdirs{d} '/paradigms'];
disp('Making new directory as follows:')
disp(mkcmd)
[status,result] = system(mkcmd);
if status~=0
rmdirs(newdirs)
error(['mkdir command could not be run successfully!' 10 result])
end
newdirs{end+1} = [destpath '/' subjectid '/' analysisdirs{d} '/paradigms'];
end
%% Run spm convert on the files
disp('Converting files to SPM format...')
cd(fullfile(destpath,subjectid,'RAW'))
dicom_files = spm_select('list',fullfile(destpath,subjectid,'RAW'),'.dcm');
disp(['Number of DICOMs: ' num2str(length(dicom_files))])
hdrs = spm_dicom_headers(dicom_files);
if str2num(usesingle)
spm_dicom_convert(hdrs,'all','flat','nii')
else
spm_dicom_convert(hdrs,'all');
end
disp('...complete!')
%% Rename hdr and img files to smaller pathnames
for b = 1:length(boldruns)
disp('Moving Bold Runs...')
runstr = sprintf('%04d',boldruns(b));
newrunstr = sprintf('%03d',b);
% also make sub-directories for the paradigms
for d = 1:length(analysisdirs)
mkcmd = ['mkdir ' destpath '/' subjectid '/' analysisdirs{d} '/paradigms/run' newrunstr];
disp('Making new directory as follows:')
disp(mkcmd)
[status,result] = system(mkcmd);
if status~=0
rmdirs(newdirs)
error(['mkdir command could not be run successfully!' 10 result])
end
newdirs{end+1} = [destpath '/' subjectid '/' analysisdirs{d} '/paradigms/run' newrunstr];
end
bruns = dir([destpath '/' subjectid '/RAW/*' runstr '-*-*-*']);
if (length(bruns)<1)
rmdirs(newdirs)
error(['No dicoms found for run ' runstr ' - did SPM provide warning messages?'])
end
for br = 1:length(bruns)
brname = bruns(br).name;
parsed = regexp(brname,'-','split');
newname = ['f-run' newrunstr '-' sprintf('%03d',str2num(parsed{3})) brname(end-3:end)];
cmdname = ['mv ' destpath '/' subjectid '/RAW/' brname ' ' destpath '/' subjectid '/preproc/' newname];
[status,result] = system(cmdname);
if status~=0
rmdirs(newdirs)
error(['File could not be moved!' 10 result])
end
end
end
disp('...complete!')
disp('Moving Structural Run...')
runstr = sprintf('%03d',structrun(1));
srun = dir([destpath '/' subjectid '/RAW/*' runstr '-*-*-*']);
for sr = 1:length(srun)
srname = srun(sr).name;
newname = ['s-struct' srname(end-3:end)];
cmdname = ['mv ' destpath '/' subjectid '/RAW/' srname ' ' destpath '/' subjectid '/preproc/' newname];
[status,result] = system(cmdname);
if status~=0
rmdirs(newdirs)
error(['File could not be moved!' 10 result])
end
end
disp('...complete!')
if length(fmruns)==2
disp('Moving Field Maps...')
% magnitude
runstr = sprintf('%03d',fmruns(1));
mrun = dir([destpath '/' subjectid '/RAW/*' runstr '-*-*-*']);
for mr = 1:length(mrun)
mrname = mrun(mr).name;
newname = ['s-fieldmap_mag-' mrname(end-5:end)];
cmdname = ['mv ' destpath '/' subjectid '/RAW/' mrname ' ' destpath '/' subjectid '/preproc/' newname];
[status,result] = system(cmdname);
if status~=0
rmdirs(newdirs)
error(['File could not be moved!' 10 result])
end
end
% phase
runstr = sprintf('%03d',fmruns(2));
mrun = dir([destpath '/' subjectid '/RAW/*' runstr '-*-*-*']);
for mr = 1:length(mrun)
mrname = mrun(mr).name;
newname = ['s-fieldmap_phase' mrname(end-3:end)];
cmdname = ['mv ' destpath '/' subjectid '/RAW/' mrname ' ' destpath '/' subjectid '/preproc/' newname];
[status,result] = system(cmdname);
if status~=0
rmdirs(newdirs)
error(['File could not be moved!' 10 result])
end
end
disp('...complete!')
end
%% Archive the original files
origfiles = dir([destpath '/' subjectid '/RAW/*']);
thisdir = pwd();
cd([destpath '/' subjectid '/RAW/']);
tgzcmd = ['tar -cvzf ' destpath '/' subjectid '/RAW/' subjectid '.tar.gz ' '*'];
disp('Rearchiving data as follows:')
disp(tgzcmd)
[status,result] = system(tgzcmd);
if status~=0
rmdirs(newdirs)
error(['DICOM archive could not be created successfully!' 10 result])
end
disp('Complete!')
cd(thisdir)
disp('Removing uncompressed files...')
myfiles = dir([destpath '/' subjectid '/RAW/']);
tgzname = [subjectid '.tar.gz'];
for i = 1:length(myfiles)
if strcmp(myfiles(i).name,tgzname)
% do nothing
elseif strcmp(myfiles(i).name,'.')
% do nothing
elseif strcmp(myfiles(i).name,'..')
% do nothing
else
rmcmd = ['rm ' destpath '/' subjectid '/RAW/' myfiles(i).name];
[status,result] = system(rmcmd);
if status~=0
warning(['File could not be removed!' 10 result])
end
end
end
if ~exist('dicompath')
%disp('Removing zip file from CBS...')
cd(startingwd)
%rmcmd = ['rm ' subjectid '.zip'];
%[status,result] = system(rmcmd);
%if status~=0
% rmdirs(newdirs)
% error(['Original zip file could not be removed!' 10 result])
%end
disp('...Complete!')
end
disp('********************************************************')
disp('* Thank You! *')
disp('********************************************************')
end
function [] = rmdirs(dirarray)
disp('Removing top level directory...' )
disp(dirarray(1))
rmcmd = ['rm -r ' dirarray{1}];
[status,result] = system(rmcmd);
if status~=0
warning(['Directory could not be removed!' 10 result])
end
% disp('Removing created directories...')
% for i = 1:length(dirarray)
% rmcmd = ['rm -r ' dirarray{i}];
%
% [status,result] = system(rmcmd);
%
% if status~=0
% warning(['Directory could not be removed!' 10 result])
% end
% end
end