-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateFolderStructure.m
420 lines (349 loc) · 16.6 KB
/
createFolderStructure.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
function [ success , DCMinfo ] = createFolderStructure( datasetConfigs , dataPath , dataTBV , subjectIndex )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
success = false;
DCMinfo = struct();
idx_info = 1;
% -------------------------------------------------------------------------
% Find DICOM files in dataPath
% -------------------------------------------------------------------------
D = dir(fullfile(dataPath,'*.ima'));
% Check if the total number of files is incorrect
if (length(D) < sum(datasetConfigs.volumes)) && (length(D) > 5)
disp('[createFolderStructure] Fewer files than expected...');
% Maybe the files are *.dcm!
elseif (length(D) < sum(datasetConfigs.volumes)) && (length(D) < 5)
D = dir(fullfile(dataPath,'*.dcm'));
end
% Extract all file names
files = extractfield(D,'name')';
nFiles = length(files);
% -------------------------------------------------------------------------
% Check if names on files match
% -------------------------------------------------------------------------
% Retrieve first DICOM file
firstDCMfile = dicominfo(fullfile(D(1).folder,D(1).name));
% Check if the patient given name exists. If it does, the DICOM files were
% not anonimized.
if isfield(firstDCMfile.PatientName,'GivenName')
% Compare the name given in datasetConfigs with the GivenName+Surname
% in the DICOM file.
if ~strcmpi(datasetConfigs.subjects{subjectIndex},...
[firstDCMfile.PatientName.GivenName firstDCMfile.PatientName.FamilyName])
disp('[createFolderStructure] Check if files correspond to subject!')
fprintf('Name on DCM files: %s %s\n',...
firstDCMfile.PatientName.GivenName,firstDCMfile.PatientName.FamilyName);
fprintf('Name provided: %s\n',datasetConfigs.subjects{subjectIndex});
x = input('[createFolderStructure] Do you wish to proceed anyway (Y/N)?','s');
switch lower(x)
case 'y'
disp('[createFolderStructure] Proceeding...');
otherwise
return
end
end
else % the GivenName field does not exist
% When using an anonimization standard, the name in datasetConfigs
% should match the PatientName.FamilyName field in the DICOM file
if ~strcmpi(datasetConfigs.subjects{subjectIndex},firstDCMfile.PatientName.FamilyName)
disp('[createFolderStructure] Check if files correspond to subject!')
fprintf('Name on DCM files: %s \n',firstDCMfile.PatientName.FamilyName);
fprintf('Name provided: %s\n',datasetConfigs.subjects{subjectIndex});
x = input('[createFolderStructure] Do you wish to proceed anyway (Y/N)?','s');
switch lower(x)
case 'y'
disp('[createFolderStructure] Proceeding...');
otherwise
return
end
end
end
% -------------------------------------------------------------------------
% Extract series
% -------------------------------------------------------------------------
series = zeros(nFiles,1);
% Iterate on the files and search for the series number
% The filenames are formated as <subjectID>.<MR>.<series>.(...)
seriesSplitIdx = 3;
for ii = 1:nFiles
auxnamesplit = strsplit(files{ii},'.');
series(ii) = str2double(auxnamesplit{seriesSplitIdx});
if isnan(series(ii)) % or maybe the filenames are formatted as <subjectID>.<MR>.<ICNAS_CRANIO>.<series>.(...)
seriesSplitIdx = 4;
series(ii) = str2double(auxnamesplit{seriesSplitIdx});
end
end
% Find the unique series numbers
[seriesNumbers , seriesIdx] = unique(series);
% Confirm the series numbers with the information in the first DICOM file
% of each series/run
READ_HEADERS = false;
for ii = 1:length(seriesNumbers)
file_idx = seriesIdx(ii);
dcmInfo = dicominfo(fullfile(D(file_idx).folder,D(file_idx).name));
if dcmInfo.SeriesNumber ~= seriesNumbers(ii)
fprintf('[createFolderStructure] Series numbers do not match between filename and DICOM header: M1=%i M2=%i \n',...
seriesNumbers(ii),dcmInfo.SeriesNumber);
x = input('[createFolderStructure] Do you wish to read all DICOM headers (a Parallel pool will run this) (Y/N)?','s');
switch lower(x)
case 'y'
READ_HEADERS = true;
break
otherwise
return
end
end
end
% If the user chooses to read all DICOM headers, this block will run
if READ_HEADERS
series = zeros(nFiles,1);
parfor ii = 1:nFiles
dcmInfo = dicominfo(fullfile(D(ii).folder,D(ii).name));
series(ii) = dcmInfo.SeriesNumber;
end
% Find the unique series numbers
seriesNumbers = unique(series);
end
% Retrieve number of files per series
seriesVolumes = hist(series,length(1:seriesNumbers(end)));
seriesVolumes = seriesVolumes(seriesVolumes~=0);
% -------------------------------------------------------------------------
% Check for incomplete runs or extra runs
% -------------------------------------------------------------------------
nRuns = length(datasetConfigs.runs);
% Number of series larger than expected number of runs
if length(seriesNumbers) > nRuns
% Find series with strange number of volumes
ignoreS = seriesNumbers(ismember(seriesVolumes,datasetConfigs.volumes) == 0);
% More than one anatomical
% This is assessed using the number of volumes of the first run
% (anatomical).
if sum(seriesVolumes == datasetConfigs.volumes(1)) > 1
boolInput = false;
disp(['[createFolderStructure] More than one run of anatomical data detected: ' num2str(seriesNumbers(seriesVolumes == datasetConfigs.volumes(1))')])
while ~boolInput
x = input('Please input the ones to ignore [<series numbers>]: ','s');
if ~ismember(str2num(x),seriesNumbers(seriesVolumes == datasetConfigs.volumes(1)))
disp('!---> ERROR: Incorrect series number.');
else
ignoreS = [ str2num(x) ignoreS ];
boolInput = true;
end
end
end
% % More than one localiser
% if sum(seriesVolumes == datasetConfigs.volumes(2)) > 1
%
% boolInput = false;
% disp(['[createFolderStructure] More than one localiser run data detected: ' num2str(seriesNumbers(seriesVolumes == datasetConfigs.volumes(2))')])
% while ~boolInput
% x = input('Please input the ones to ignore [<series numbers>]: ','s');
%
% if ~ismember(str2double(x),seriesNumbers(seriesVolumes == datasetConfigs.volumes(2)))
% disp('!---> ERROR: Incorrect series number.');
% else
% ignoreS = [ str2double(x) ignoreS ];
% boolInput = true;
% end
% end
%
% end
disp(['[createFolderStructure] Ignoring files with series number of ' num2str(ignoreS')]);
files(ismember(series,ignoreS)) = [];
idx_to_delete = ismember(seriesNumbers,ignoreS);
seriesNumbers(idx_to_delete) = [];
seriesVolumes(idx_to_delete) = [];
% Still more series than expected
if length(seriesNumbers) > nRuns
ignoreS = [];
boolInput = false;
disp(['[createFolderStructure] ' num2str(length(seriesNumbers) - nRuns) ' extra series remain.']);
while ~boolInput
disp(['[createFolderStructure] Current series: ' mat2str(seriesNumbers) '.'])
x = input('Please input the ones to ignore [<series numbers>]: ','s');
if length(str2num(x)) > length(seriesNumbers) - nRuns
disp(['!---> ERROR: Too many series to delete. Choose only ' length(seriesNumbers) - nRuns ]);
elseif ~ismember(str2num(x),seriesNumbers)
disp('!---> ERROR: Incorrect series number.');
else
ignoreS = [ str2num(x) ignoreS ];
boolInput = true;
end
end
disp(['[createFolderStructure] Ignoring files with series number of ' num2str(ignoreS)]);
files(ismember(series,ignoreS)) = [];
idx_to_delete = ismember(seriesNumbers,ignoreS);
seriesNumbers(idx_to_delete) = [];
seriesVolumes(idx_to_delete) = [];
end
% Number of series smaller than expected number of runs
elseif length(seriesNumbers) < nRuns
disp('[createFolderStructure] !---> ERROR: Unsufficient data.')
boolInput = false;
while ~boolInput
x = input('[createFolderStructure] Do you wish to proceed anyway (Y/N)?','s');
switch lower(x)
case 'y'
nRuns = length(seriesNumbers);
boolInput = true;
otherwise
return
end
end
end
% -------------------------------------------------------------------------
% Check for incorrect number of volumes in all runs
% -------------------------------------------------------------------------
if any(datasetConfigs.volumes ~= seriesVolumes)
disp('[createFolderStructure] Run volumes do not match the expected:');
disp(['Expected: ' num2str(datasetConfigs.volumes)])
disp(['Input: ' num2str(seriesVolumes)]);
boolInput = false;
while ~boolInput
x = input('[createFolderStructure] Do you wish to proceed anyway (Y/N)?','s');
switch lower(x)
case 'y'
boolInput = true;
otherwise
return
end
end
end
% -------------------------------------------------------------------------
% Check if Experiment folder exists
% -------------------------------------------------------------------------
if exist(datasetConfigs.path,'dir') ~= 7
mkdir(datasetConfigs.path,'ANALYSIS');
disp('[createFolderStructure] Experiment folder created.')
end
% -------------------------------------------------------------------------
% Create Subject Folder
% -------------------------------------------------------------------------
subjectFolder = fullfile(datasetConfigs.path,datasetConfigs.subjects{subjectIndex});
boolInput = false;
if exist(subjectFolder,'dir') == 7
while ~boolInput
x = input('[createFolderStructure] Subject Folder already exists. Do you wish to overwrite (Y), stop (N) or proceed (P)?','s');
switch lower(x)
case 'y'
rmdir(subjectFolder,'s')
mkdir(subjectFolder)
boolInput = true;
case 'n'
return
case 'p'
success = true;
return
otherwise
disp('[createFolderStructure] !---> ERROR: Invalid input.')
end
end
else
mkdir(subjectFolder)
end
% -------------------------------------------------------------------------
% Iterate on the runs
% -------------------------------------------------------------------------
for r = 1:nRuns
% Create folder structure
switch datasetConfigs.runs{r}
case 'anatomical'
mkdir(subjectFolder,datasetConfigs.runs{r});
dataFolder = fullfile(subjectFolder,datasetConfigs.runs{r},'DICOM');
niifolder = fullfile(subjectFolder,datasetConfigs.runs{r});
otherwise
runFolderName = ['r-' datasetConfigs.runs{r}];
mkdir(subjectFolder,runFolderName);
mkdir(fullfile(subjectFolder,runFolderName),'DICOM'); % IMA files
mkdir(fullfile(subjectFolder,runFolderName),'PRT'); % PRT files
mkdir(fullfile(subjectFolder,runFolderName),'f'); % .nii
mkdir(fullfile(subjectFolder,runFolderName),'af'); % .nii after STC
% mkdir(fullfile(subjectFolder,['r-' datasetConfigs.runs{r}]),'raf'); % .nii after realign
mkdir(fullfile(subjectFolder,runFolderName),'wraf'); % .nii after warp
mkdir(fullfile(subjectFolder,runFolderName),'swraf'); % .nii after smooth
mkdir(fullfile(subjectFolder,runFolderName),'ANALYSIS'); % GLM stuff
niifolder = fullfile(subjectFolder,runFolderName,'f');
dataFolder = fullfile(subjectFolder,runFolderName,'DICOM');
% Copy Protocol (PRT) file
prtGood = true;
auxDir = dir(fullfile(dataTBV,[datasetConfigs.prtPrefix{r-1} '*.prt']));
if ~isempty(auxDir) % If a .prt file was found
keepDir = auxDir;
if length(auxDir) > 1 % If more than one was found
boolInput = false;
disp(['!! More than one ' datasetConfigs.runs{r} ' run prt file detected !!'])
disp(extractfield(auxDir,'name')');
while ~boolInput
x = input(sprintf('Please input the number (1:%i) of the PRT to keep: ',length(auxDir)),'s');
if isnan(str2double(x)) || str2double(x) > length(auxDir)
fprintf('!---> ERROR: Please insert a number between 1 and %i/n.',length(auxDir));
else
keepDir = auxDir(str2double(x));
boolInput = true;
end
end
end
copyfile(fullfile(keepDir.folder,keepDir.name),...
fullfile(subjectFolder,runFolderName,'PRT'));
else
fprintf('!! No .prt file found in TBV folder for %s !! \n',datasetConfigs.runs{r});
prtGood = false;
end
% Convert PRT to mat file
if prtGood
prtFile = xff(fullfile(subjectFolder,runFolderName,'PRT',keepDir.name));
names = prtFile.ConditionNames';
onsets = cell(1,length(names));
durations = cell(1,length(names));
onoff = prtFile.OnOffsets;
for c = 1:length(names)
onsets{c} = onoff(onoff(:,1)==c,2)';
durations{c} = (onoff(onoff(:,1)==c,3)-onoff(onoff(:,1)==c,2)+1)';
end
save(fullfile(subjectFolder,runFolderName,'PRT',[keepDir.name(1:end-4) '.mat']),'names','onsets','durations')
end
end
% Copy DICOM files of the series/run
fprintf('Copying %s files...\n',datasetConfigs.runs{r});
if seriesSplitIdx == 3
search_name = [auxnamesplit{1} '.' auxnamesplit{2} '.' num2str(seriesNumbers(r),'%.4i') '*'];
elseif seriesSplitIdx == 4
search_name = [auxnamesplit{1} '.' auxnamesplit{2} '.' auxnamesplit{3} '.' num2str(seriesNumbers(r),'%.4i') '*'];
end
copyfile( fullfile(dataPath,search_name) , dataFolder );
% Extract important header information
auxdir = dir(fullfile(dataFolder,search_name));
if ~ strcmp(datasetConfigs.runs{r},'anatomical')
dcmHeader = dicominfo(fullfile(auxdir(1).folder,auxdir(1).name));
DCMinfo(idx_info).sliceTimes = dcmHeader.Private_0019_1029;
DCMinfo(idx_info).sliceNumber = length(DCMinfo(idx_info).sliceTimes);
[~,DCMinfo(idx_info).sliceVector] = sort(DCMinfo(idx_info).sliceTimes);
DCMinfo(idx_info).TR = dcmHeader.RepetitionTime / 1000;
DCMinfo(idx_info).TA = DCMinfo(idx_info).TR-(DCMinfo(idx_info).TR/DCMinfo(idx_info).sliceNumber);
DCMinfo(idx_info).RefSlice = DCMinfo(idx_info).sliceVector(1);
DCMinfo(idx_info).EchoTime = dcmHeader.EchoTime / 1000;
idx_info = idx_info + 1;
end
% Renonimize (necessary due to inconsistent series info on the header)
disp('Re-anonimizing...')
values.StudyInstanceUID = dicomuid;
values.SeriesInstanceUID = dicomuid;
values.PatientName = datasetConfigs.subjects{subjectIndex};
for p = 1:numel(auxdir)
dicomanon(fullfile(auxdir(p).folder,auxdir(p).name), ...
fullfile(dataFolder, sprintf('%s-%s-%04d.dcm', datasetConfigs.subjects{subjectIndex}, datasetConfigs.runs{r}, p)) , ...
'update', values, ...
'WritePrivate',true);
end
% Convert to .nii
search_name = [datasetConfigs.subjects{subjectIndex} '-' datasetConfigs.runs{r} '-*'];
auxdir = dir(fullfile(dataFolder,search_name));
fileList = cellfun(@(x) fullfile(auxdir(1).folder,x),{auxdir.name}','UniformOutput',false);
convertDCMtoNII( fileList , niifolder );
end
save(fullfile(subjectFolder,'DCMinfo.mat'),'DCMinfo');
mkdir(fullfile(subjectFolder,'batch'));
mkdir(fullfile(subjectFolder,'mr'));
success = true;
disp('[createFolderStructure] Folder structure creation completed.')
end