Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
function OPTIMA_repair_convert_GE_dwi2_to_BIDS(BIDS, biomax_table)
%
% performes dicom2niix for 2nd dwi experiment and saves data in BIDS format
%
% Required fields of convert_GE_BIDS:
%
% BIDS - BIDS structure
% biomax_table - biomax_out information in table format
%
%
% file history:
% created M. Czisch 5.5.2020
% last edited M. Czisch 12.5.2020
% adapted for OPTIMA 13.11.2020
%
% this script is needed since dicom2niix has problems with the manual
% stopping of dwi2, which causes the volumes to have weird slice numbers sometimes
%
% as it occured previously that a database recovery messes up the order of
% files, there is a check if the general structure of the subdirectory is
% OK
%
% the present script reads the number of slices in the dwi, and deleted all slices
% of the last incomplete volume before dcm2niix
test_flag = 0;
i = 1; % day 1
max_series = size(BIDS.input.data_dir(i).subdir,2); % number of series in day 1
for j = 3:max_series %ignore '.' and '..'
if logical(strfind(BIDS.input.data_dir(i).SeriesDescription{j},'polar'))
if biomax_table{{'Pepolar'},{'Ser'}}
% changes 5.5.2020, as in OPTIMA
% check number of dicom slices
% and delete incomplete volumes
cd(BIDS.input.data_dir(i).name);
cd(BIDS.input.data_dir(i).subdir{j});
if exist('saved_dwi2') % obviously, the script had been run before
tmp_files = dir('a*');
else
tmp_files = dir('i*');
end
% check if the dicom files are in the correct format
% as after a database recovery, this might be corrupted
for k=1:size(tmp_files,1)
input_name = tmp_files(k).name;
tmp = strsplit(input_name,'MRDC.');
tmp_num(k) = str2num(tmp{2});
tmp_num2(k) = k;
end
%check if dwi file format is correct
n_slices = double(BIDS.input.data_dir(i).N_of_slices{j});
if (max(tmp_num-tmp_num2) > (n_slices/2 -1)) || (min(tmp_num-tmp_num2) < - (n_slices/2 -1))
fprintf('\nWARNING: directory looks as if database is corrupted!\n');
fprintf('resorting performed\n\n');
resort_flag = 1;
else
fprintf('\ndirectory looks normal!\n');
fprintf('no action required\n\n');
resort_flag = 0;
end
if resort_flag
[B,I]=sort(tmp_num);
for k = 1:size(tmp_files,1)
%output_name(k).name = tmp_files(I(k)).name;
counter = sprintf('%04d', k);
cmd_out = ['cp ',tmp_files(I(k)).name,' a', counter,'.MRDC.', num2str(k)];
system(cmd_out);
end
cmd_out = ['mkdir saved_dwi2'];
system(cmd_out);
cmd_out = ['mv i* saved_dwi2'];
system(cmd_out);
tmp_str = sprintf('a*MRDC.*');
elseif ~resort_flag && size(dir('a*'),1)
tmp_str = sprintf('a*MRDC.*');
else
tmp_str = sprintf('i*MRDC*');
end
tmp = dir(tmp_str);
n_images = size(tmp,1);
full_images = fix(n_images/n_slices);
for b = (full_images*n_slices)+1:n_images
cmd_str = ['rm ', tmp(b).name];
system(cmd_str);
end
% make sure that directory only contains the dicom files
cmd_out = ['mv saved_dwi2 ..'];
system(cmd_out);
cmd_str0 = ['repair_BIDS_1_0_2_dwi2.m run on ',date];
cmd_str0 = sprintf('echo ''%s'' >> %s/dwi/logfile_%s_dwi_dcm2nii.txt', cmd_str0, BIDS.output.dir, BIDS.input.data_dir(1).PatientID{3})
fprintf('\n%s', cmd_str0);
if resort_flag
cmd_str = sprintf('dcm2niix -f sub-%s_run-2_dwi -z y -o %s/dwi . >> %s/dwi/logfile_%s_dwi_dcm2nii.txt',...
BIDS.input.data_dir(i).PatientID{j}, BIDS.output.dir, BIDS.output.dir, BIDS.input.data_dir(1).PatientID{3});
else
cmd_str = sprintf('dcm2niix -f sub-%s_run-2_dwi -z y -o %s/dwi . >> %s/dwi/logfile_%s_dwi_dcm2nii.txt',...
BIDS.input.data_dir(i).PatientID{j}, BIDS.output.dir, BIDS.output.dir, BIDS.input.data_dir(1).PatientID{3});
end
fprintf('\n%s', cmd_str);
if ~test_flag
system(['rm ', BIDS.output.dir,'/dwi/*run-2_dwi*']);
system(cmd_str0);
system(cmd_str);
cmd_str = sprintf('cp %s/*MRDC.1 %s/dicom/sub-%s_run-2_dwi.dicom.1 >> %s/dwi/logfile_%s_dwi_dcm2nii.txt', ...
BIDS.input.data_dir(i).subdir{j}, BIDS.output.dir, BIDS.input.data_dir(i).PatientID{j}, BIDS.output.dir, BIDS.input.data_dir(1).PatientID{3});
system(cmd_str);
end
% and bring back the original backuped files
cmd_out = ['mv ../saved_dwi2 .'];
system(cmd_out);
end
end
end