Skip to content

Commit

Permalink
v1.7
Browse files Browse the repository at this point in the history
- New command line options (preprocessing)
- Option to automatically download Elastix when not found in path
- Initial support for a new way to handle NIFTIs to preserve the original header and orientation
  • Loading branch information
delucaal committed Dec 11, 2023
1 parent 19e07e6 commit 09fc5f8
Show file tree
Hide file tree
Showing 12 changed files with 4,900 additions and 44 deletions.
6 changes: 3 additions & 3 deletions CommandLine/mrtd_coordsys_check.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function mrtd_coordsys_check(varargin)
disp('mrtd_coordsys_check');
coptions = varargin;
if(length(varargin{1}) > 1)
coptions = varargin{1};
end
% if(length(varargin{1}) > 1)
% coptions = varargin{1};
% end
% disp(varargin)

if(isempty(coptions) || isempty(coptions{1}) || strcmpi(coptions{1},'-help'))
Expand Down
8 changes: 4 additions & 4 deletions CommandLine/mrtd_coordsys_fix.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function mrtd_coordsys_fix(varargin)
disp('mrtd_coordsys_fix');
coptions = varargin;
if(length(varargin{1}) > 1)
coptions = varargin{1};
end
% if(length(varargin{1}) > 1)
% coptions = varargin{1};
% end
% disp(varargin)

if(isempty(coptions) || isempty(coptions{1}) || strcmpi(coptions{1},'-help'))
Expand All @@ -24,7 +24,7 @@ function mrtd_coordsys_fix(varargin)
if(isempty(file_in))
error('Need to specify the target .nii file');
end
auto_fix = GiveValueForName(coptions,'-nii');
auto_fix = GiveValueForName(coptions,'-auto');
if(isempty(auto_fix))
auto_fix = 0;
end
Expand Down
20 changes: 13 additions & 7 deletions CommandLine/mrtd_neuro.m
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
function mrtd_neuro(varargin)
disp('mrtd_neuro');
coptions = varargin;
if(length(varargin{1}) > 1)
coptions = varargin{1};
end
% if(length(varargin{1}) > 1)
% coptions = varargin{1};
% end
% disp(varargin)

if(isempty(coptions) || isempty(coptions{1}) || strcmpi(coptions{1},'-help'))
help = 'This tool applies some popular neuroimaging pipelines';
help = 'This tool performs some neuroimaging tasks';
help = [help newline];
help = [help newline 'FOD-based usage: mrtd_neuro -t1 t1_file.nii -flair flair.nii -apply cat12pipeline -out output_folder'];
help = [help newline 'FOD-based usage: mrtd_neuro -t1 t1_file.nii -flair flair.nii -apply cat12pipeline/brainextraction -out output_folder'];
help = [help newline];
help = [help newline '-t1: T1-weighted .nii file'];
help = [help newline '-flair: FLAIR .nii file'];
help = [help newline '-out: output folder'];
help = [help newline '-apply: "cat12pipeline"'];
help = [help newline '-out: output folder (for cat12) or output file'];
help = [help newline '-apply: "cat12pipeline" or "brainextraction"'];
help = [help newline];
fprintf(help);

Expand Down Expand Up @@ -44,6 +44,10 @@ function mrtd_neuro(varargin)
error('Missing mandatory argument -out');
end

if(isempty(t1_file))
error('Missing mandatory argument -t1');
end

try
% cat_defaults
% cat12
Expand Down Expand Up @@ -76,5 +80,7 @@ function mrtd_neuro(varargin)
if(should_remove_flair == 1)
delete(flair_file);
end
elseif(strcmpi(method,'brainextraction'))
Neuro.BrainExtractionWithRegistration('nii_file',t1_file,'output',outfile);
end
end
64 changes: 53 additions & 11 deletions CommandLine/mrtd_preproc.asv
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ function mrtd_preproc(varargin)
' 4=[x z y] =[y z x] 6=[z x y]'];
help = [help newline '-grad_flip: how to flip the sign of the diffusion gradients 1=[x y z] 2=[-x y z] 3=[x -y z] 4=[x y -z]'];
help = [help newline '-epi: .nii file to perform EPI correction (T1 or T2 image)'];
help = [help newline '-epi_constraint: allow deformation on specific axis. Input between quotas "1 1 1"'];
help = [help newline '-epi_constraint: allow deformation on specific axis. Input as a three digit number: 101'];
help = [help newline '-epi_reg_mode: image to use for registration to structural. One between "fa" (default), "b0", "dwis"'];
help = [help newline '-epi_normcorr: 0 or 1. Use normalized correlation in place of mutual information to drive the registration.'];
help = [help newline '-denoise: 0 or 1. Perform MP-PCA denoising'];
help = [help newline '-sdc: 0 or 1. Perform signal drift correction'];
help = [help newline '-gibbs: 0 or 1. Perform Gibbs ringing correction'];
help = [help newline '-use_fsl: 0 or 1. Whether to perform the processing via FSL.'];
help = [help newline '-topup: reverse phase encoding file. If specified, processing will be performed via FSL.'];
help = [help newline];
fprintf(help);

Expand Down Expand Up @@ -84,7 +86,7 @@ function mrtd_preproc(varargin)
epi_constraint = [1 1 1];
else
pieces = strsplit(epi_constraint,' ');
epi_constraint = [str2double(pieces{1}) str2double(pieces{2}) str2double(pieces{3})];
epi_constraint = [str2double(pieces{1}(1)) str2double(pieces{1}(2)) str2double(pieces{1}(3))];
end
epi_reg_mode = GiveValueForName(coptions,'-epi_reg_mode');
if(isempty(epi_reg_mode))
Expand Down Expand Up @@ -114,6 +116,20 @@ function mrtd_preproc(varargin)
else
denoise = str2double(denoise);
end

use_fsl = GiveValueForName(coptions,'-use_fsl');
if(isempty(use_fsl))
use_fsl = 0;
else
use_fsl = str2double(use_fsl);
end

topup = GiveValueForName(coptions,'-topup');
if(isempty(topup))
topup = [];
else
use_fsl = 1;
end

ext = '.nii';
shift = 4;
Expand Down Expand Up @@ -158,17 +174,43 @@ function mrtd_preproc(varargin)
'grad_flip',grad_flip);

disp('Motion correction');
if(~isempty(epi_tgt))
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file,'epi_tgt',epi_tgt,'constraint_epi',epi_constraint,'epi_reg_mode',epi_reg_mode,'use_normcorr',epi_normcorr);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_trafo.mat']);
if(use_fsl == 0)
if(~isempty(epi_tgt))
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file,'epi_tgt',epi_tgt,'constraint_epi',epi_constraint,'epi_reg_mode',epi_reg_mode,'use_normcorr',epi_normcorr);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_trafo.mat']);
else
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_native.mat']);
end
else
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_native.mat']);
% First correct for motion and eventually EPI using FSL
fsl_preproc = strrep(temp_mat_file,'.mat','_fsl.nii');
if(~isempty(topup))
MRTQuant.PerformMocoEPI_FSL('mat_file',temp_mat_file,'output',fsl_preproc,...
'topup_nii',topup);
else
MRTQuant.PerformMocoEPI_FSL('mat_file',temp_mat_file,'output',fsl_preproc);
end
temp_mat_file_fsl = MRTQuant.QuickNiiBvalBvecToMat('nii_file',fsl_preproc,...
'bval_file',strrep(fsl_preproc,'.nii','.bval'),'bvec_file',strrep(fsl_preproc,'.nii','.bvec'),'grad_perm',grad_perm,...
'grad_flip',grad_flip);

if(~isempty(epi_tgt))
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file_fsl,'epi_tgt',epi_tgt,'constraint_epi',epi_constraint,'epi_reg_mode',epi_reg_mode,'use_normcorr',epi_normcorr,'do_moco',0);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file_fsl(1:end-4) '_MD_C_trafo.mat']);
else
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',temp_mat_file_fsl);
end
delete(temp_mat_file_fsl)
end

[fp,fn,~] = fileparts(output_file);
output_file = fullfile(fp,fn);
delimiter = strfind()
delete(temp_mat_file)
% [fp,fn,~] = fileparts(output_file);
% output_file = fullfile(fp,fn);
delimiter = strfind(output_file,'.nii');
if(isempty(delimiter))
delimiter = length(output_file);
end
output_file = output_file(1:delimiter);

MRTQuant.WriteNifti(mrt_data,[output_file '.nii']);

Expand Down
56 changes: 51 additions & 5 deletions CommandLine/mrtd_preproc.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function mrtd_preproc(varargin)
help = [help newline '-denoise: 0 or 1. Perform MP-PCA denoising'];
help = [help newline '-sdc: 0 or 1. Perform signal drift correction'];
help = [help newline '-gibbs: 0 or 1. Perform Gibbs ringing correction'];
help = [help newline '-use_fsl: 0 or 1. Whether to perform the processing via FSL.'];
help = [help newline '-topup: reverse phase encoding file. If specified, processing will be performed via FSL.'];
help = [help newline];
fprintf(help);

Expand Down Expand Up @@ -114,6 +116,20 @@ function mrtd_preproc(varargin)
else
denoise = str2double(denoise);
end

use_fsl = GiveValueForName(coptions,'-use_fsl');
if(isempty(use_fsl))
use_fsl = 0;
else
use_fsl = str2double(use_fsl);
end

topup = GiveValueForName(coptions,'-topup');
if(isempty(topup))
topup = [];
else
use_fsl = 1;
end

ext = '.nii';
shift = 4;
Expand Down Expand Up @@ -158,14 +174,44 @@ function mrtd_preproc(varargin)
'grad_flip',grad_flip);

disp('Motion correction');
% If an EPI tgt is specified, make sure it is Elastix "friendly"
if(~isempty(epi_tgt))
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file,'epi_tgt',epi_tgt,'constraint_epi',epi_constraint,'epi_reg_mode',epi_reg_mode,'use_normcorr',epi_normcorr);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_trafo.mat']);
temp_tgt = strrep(temp_file,'.nii','_target.nii');
MRTQuant.ConformSpatialDimensions('nii_file',epi_tgt,'output',temp_tgt);
else
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_native.mat']);
temp_tgt = [];
end


if(use_fsl == 0)
if(~isempty(epi_tgt))
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file,'epi_tgt',temp_tgt,'constraint_epi',epi_constraint,'epi_reg_mode',epi_reg_mode,'use_normcorr',epi_normcorr);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_trafo.mat']);
else
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file(1:end-4) '_MD_C_native.mat']);
end
else
% First correct for motion and eventually EPI using FSL
fsl_preproc = strrep(temp_mat_file,'.mat','_fsl.nii');
if(~isempty(topup))
MRTQuant.PerformMocoEPI_FSL('mat_file',temp_mat_file,'output',fsl_preproc,...
'topup_nii',topup);
else
MRTQuant.PerformMocoEPI_FSL('mat_file',temp_mat_file,'output',fsl_preproc);
end
temp_mat_file_fsl = MRTQuant.QuickNiiBvalBvecToMat('nii_file',fsl_preproc,...
'bval_file',strrep(fsl_preproc,'.nii','.bval'),'bvec_file',strrep(fsl_preproc,'.nii','.bvec'),'grad_perm',grad_perm,...
'grad_flip',grad_flip);

if(~isempty(epi_tgt))
MRTQuant.PerformMocoEPI('mat_file',temp_mat_file_fsl,'epi_tgt',temp_tgt,'constraint_epi',epi_constraint,'epi_reg_mode',epi_reg_mode,'use_normcorr',epi_normcorr,'do_moco',0);
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',[temp_mat_file_fsl(1:end-4) '_MD_C_trafo.mat']);
else
mrt_data = MRTQuant.EDTI_Data_2_MRIToolkit('mat_file',temp_mat_file_fsl);
end
delete([temp_mat_file_fsl(1:end-4) '*'])
end
delete([temp_mat_file(1:end-4) '*'])
% [fp,fn,~] = fileparts(output_file);
% output_file = fullfile(fp,fn);
delimiter = strfind(output_file,'.nii');
Expand Down
Loading

0 comments on commit 09fc5f8

Please sign in to comment.