0001 function dmri_fodf_calc(data_niftigz_file, bval_file, bvec_file, ...
0002 area_niftigz_file, ...
0003 mask_niftigz_file, ...
0004 output_mif_file)
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 if nargin ~= 6
0045 error('Please check input arguments.');
0046 end
0047 if exist(data_niftigz_file, 'file') ~= 2
0048 error('Specified diffusion weighted data file not found.');
0049 end
0050 if exist(area_niftigz_file, 'file') ~= 2
0051 error('Specified brain area file not found.');
0052 end
0053 if exist(mask_niftigz_file, 'file') ~= 2
0054 error('Specified brain mask file not found.');
0055 end
0056 if exist(bval_file, 'file') ~= 2
0057 error('Specified bval_file not found.');
0058 end
0059 if exist(bvec_file, 'file') ~= 2
0060 error('Specified bvec_file not found.');
0061 end
0062 if isempty(output_mif_file)
0063 error('Output filename is empty.');
0064 end
0065
0066
0067
0068
0069
0070 if exist(output_mif_file, 'file') == 2
0071 copyfile(output_mif_file, [output_mif_file, '.bak'], 'f');
0072 delete(output_mif_file);
0073 end
0074
0075 output_dir = fileparts(output_mif_file);
0076 work_dir = tempname;
0077 mkdir(work_dir);
0078
0079
0080 copyfile(data_niftigz_file, work_dir);
0081 copyfile(area_niftigz_file, work_dir);
0082 copyfile(mask_niftigz_file, work_dir);
0083 system(['gunzip ', fullfile(work_dir, filesep, '*.nii.gz')]);
0084
0085
0086 [p, f, e] = fileparts(data_niftigz_file);
0087 temp_data_nifti_file = fullfile(work_dir, filesep, strrep([f, e], 'nii.gz', 'nii'));
0088 [p, f, e] = fileparts(area_niftigz_file);
0089 temp_area_nifti_file = fullfile(work_dir, filesep, strrep([f, e], 'nii.gz', 'nii'));
0090 [p, f, e] = fileparts(mask_niftigz_file);
0091 temp_mask_nifti_file = fullfile(work_dir, filesep, strrep([f, e], 'nii.gz', 'nii'));
0092
0093 temp_response_file = fullfile(work_dir, filesep, 'response.txt');
0094 temp_bvecbval_mrtrix_file = fullfile(work_dir, filesep, 'gradients-x.txt');
0095
0096
0097 convert_bval_format_to_mrtrix(bval_file, bvec_file, temp_bvecbval_mrtrix_file);
0098
0099
0100 cmd = ['estimate_response', ...
0101 ' ' , temp_data_nifti_file, ...
0102 ' -grad ', temp_bvecbval_mrtrix_file, ...
0103 ' ' , temp_area_nifti_file, ...
0104 ' -lmax 6', ...
0105 ' ', temp_response_file];
0106 dmri_system(cmd, '-echo');
0107
0108
0109 cmd = ['csdeconv', ...
0110 ' ' , temp_data_nifti_file, ...
0111 ' -grad ', temp_bvecbval_mrtrix_file, ...
0112 ' ' , temp_response_file, ...
0113 ' -lmax 6', ...
0114 ' -mask ', temp_mask_nifti_file, ...
0115 ' ', output_mif_file];
0116 [status, result] = dmri_system(cmd, '-echo');
0117 if status ~= 0
0118 error(result);
0119 end
0120
0121
0122 copyfile(temp_response_file, output_dir);
0123 copyfile(temp_bvecbval_mrtrix_file, output_dir);
0124 rmdir(work_dir, 's');
0125
0126
0127 function [isok] = convert_bval_format_to_mrtrix(bval_file, bvecs_file, bvecbval_mrtrix_file)
0128
0129 rotbvecs = load(bvecs_file, '-ascii');
0130 rotbvecs = rotbvecs';
0131
0132
0133 rotbvecs(:, 1) = -1 * rotbvecs(:, 1);
0134
0135
0136 bvals = load(bval_file, '-ascii');
0137
0138 bb = [rotbvecs, bvals'];
0139 saveascii(bb, bvecbval_mrtrix_file, 8);