0001 function dmri_label_file_to_FA_volume(label_dir, ...
0002 fs_dir, ref_struct_niigz_file, ...
0003 ref_FA_niigz_file, trans_info_dir, ...
0004 output_dir, Nprocess, mode)
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
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 if nargin < 6
0057 error('Please check input arguments.');
0058 end
0059 if ~exist('Nprocess', 'var')
0060 Nprocess = 2;
0061 end
0062 if exist(output_dir, 'dir') ~= 7
0063 mkdir(output_dir);
0064 end
0065 if ~exist('mode', 'var')
0066 mode = 0;
0067 end
0068
0069
0070
0071
0072
0073 process_list = cell(0);
0074 start = tic;
0075
0076
0077 function terminate_fcn
0078
0079 for k=1:length(process_list)
0080 process_list{k}.destroy();
0081 end
0082 end
0083
0084 label_files = dir([label_dir, filesep, 'parcel*.label']);
0085 Nfiles = length(label_files);
0086 Nfile_per_proc = ceil(Nfiles / Nprocess);
0087 work_dir = tempname(output_dir);
0088 mkdir(work_dir);
0089
0090 if mode == 0 || mode == 1
0091 d = dir(fullfile(output_dir, filesep, 'parcel*.nii.gz'));
0092 Nexist = length(d);
0093 if Nexist ~= 0
0094 warning('Parcellation volume files are already exist. overwrite them.');
0095 delete(fullfile(output_dir, filesep, 'parcel*.nii.gz'));
0096 delete(fullfile(output_dir, filesep, 'parcel*.asc'));
0097 delete(fullfile(output_dir, filesep, 'qform'));
0098 end
0099
0100
0101
0102
0103 fprintf('Creating Parcel volume files from Parcel label files.\n');
0104 files = cell(0, 1);
0105 fids = cell(0, 1);
0106 for k=1:Nfiles
0107 if mod(k, Nfile_per_proc) == 1
0108 Nth_script_file = length(files)+1;
0109 script_file = fullfile(work_dir, filesep, ['script', num2str(Nth_script_file), '.sh']);
0110
0111
0112 isok = dmri_script_file_create(script_file);
0113 if isok == false, error('Failed to create script.'); end
0114
0115
0116 fid = fopen(script_file, 'a');
0117 if fid == -1
0118 error('File open failed. processing terminated.');
0119 end
0120 fids{Nth_script_file} = fid;
0121 files{Nth_script_file} = script_file;
0122 end
0123
0124
0125
0126
0127 fs_vol_file = fullfile(work_dir, filesep, ['tmp1_' num2str(k), '.nii.gz']);
0128 fprintf(fid, ['mri_label2vol', ...
0129 ' --label ' fullfile(label_dir, filesep, ['parcel' num2str(k), '.label']), ...
0130 ' --temp ' fs_dir, '/mri/brain.nii.gz', ...
0131 ' --o ' fs_vol_file ' --identity >/dev/null 2>&1 \n']);
0132
0133 struct_vol_file = fullfile(work_dir, filesep, ['tmp0_' num2str(k), '.nii.gz']);
0134 cmd = dmri_image_transform_cmd_get(fs_vol_file, struct_vol_file, ref_struct_niigz_file, 'fs2struct', trans_info_dir);
0135 fprintf(fid, '%s\n', cmd);
0136
0137
0138 fa_vol_file = fullfile(work_dir, filesep, ['tmp2_' num2str(k), '.nii.gz']);
0139 cmd = dmri_image_transform_cmd_get(struct_vol_file, fa_vol_file, ref_FA_niigz_file, 'struct2fa_nointerp', trans_info_dir);
0140 fprintf(fid, '%s\n', cmd);
0141
0142
0143 fa_bin_file = fullfile(output_dir, filesep, ['parcel' num2str(k), '.nii.gz']);
0144 fprintf(fid, '%s\n', ['fslmaths', ...
0145 ' ' fa_vol_file, ...
0146 ' -thr 0', ...
0147 ' -bin ', fa_bin_file]);
0148
0149
0150 fa_ascii_file = fullfile(output_dir, filesep, ['parcel' num2str(k), '-v.asc']);
0151 fprintf(fid, '%s\n', ['fsl2ascii', ...
0152 ' ', fa_bin_file, ...
0153 ' ', fa_ascii_file]);
0154
0155
0156 fprintf(fid, '%s\n', ['mv', ...
0157 ' ', [fa_ascii_file, '00000'], ...
0158 ' ', fa_ascii_file]);
0159
0160 end
0161
0162
0163 for k=1:length(fids), fclose(fids{k}); end
0164
0165
0166
0167 onCleanup(@terminate_fcn);
0168
0169
0170 runtime = java.lang.Runtime.getRuntime();
0171 for k=1:length(files)
0172 cmd = files{k};
0173 process_list{length(process_list)+1} = runtime.exec(cmd);
0174 end
0175
0176
0177 while(1)
0178 d = dir(fullfile(output_dir, filesep, 'parcel*.nii.gz'));
0179 Ncreated = length(d);
0180 fprintf('Now processing (%d/%d)\n', Ncreated, Nfiles);
0181 if Ncreated >= Nfiles
0182 disp('Done.');
0183 break;
0184 end
0185 pause(30);
0186 end
0187 end
0188
0189
0190
0191
0192 if mode == 0 || mode == 2
0193 fprintf('All the Parcel volumes into single volume....');
0194
0195
0196
0197
0198 qform_file = fullfile(output_dir, filesep, 'qform');
0199 cmd = ['fslorient -getqform ', fullfile(output_dir, filesep, 'parcel1.nii.gz'),...
0200 ' > ' qform_file];
0201 dmri_system(cmd);
0202
0203 parcel_all_file = fullfile(output_dir, filesep, 'parcel_all.nii.gz');
0204 copyfile(fullfile(output_dir, filesep, 'parcel1.nii.gz'), parcel_all_file);
0205 for k=2:Nfiles
0206
0207 parcel_n_file = fullfile(output_dir, filesep, ['parcel' num2str(k), '.nii.gz']);
0208 dmri_add_images(parcel_all_file, parcel_n_file, parcel_all_file);
0209 end
0210 fprintf('Done.\n');
0211 end
0212
0213
0214 if mode == 0 || mode == 2
0215 disp('Converting parcels into FA coordinate vertices...');
0216 parcels_mm_file = fullfile(output_dir, filesep, 'parcel_mm_coord.mat');
0217 save_mask_coordinate(output_dir, qform_file, parcels_mm_file, ...
0218 Nfiles, ref_FA_niigz_file);
0219 delete(fullfile(output_dir, filesep, 'parcel*.asc'));
0220
0221 end
0222
0223
0224
0225
0226 if mode == 0 || mode == 3
0227 fprintf('Creating Parcel ex-mask volume files.\n');
0228 parcel_all_file = fullfile(output_dir, filesep, 'parcel_all.nii.gz');
0229 fids_ex_mask = cell(0);
0230 files_ex_mask = cell(0);
0231 for k=1:Nfiles
0232 parcel_n_file = fullfile(output_dir, filesep, ['parcel' num2str(k), '.nii.gz']);
0233 parcel_n_ex_file = fullfile(output_dir, filesep, ['parcel_ex' num2str(k), '.nii.gz']);
0234 if mod(k, Nfile_per_proc) == 1
0235 Nth_script_file = length(files_ex_mask)+1;
0236 script_file = fullfile(work_dir, filesep, ['ex_mask_script', num2str(Nth_script_file), '.sh']);
0237
0238
0239 isok = dmri_script_file_create(script_file);
0240 if isok == false, error('Failed to create script.'); end
0241
0242
0243 fid = fopen(script_file, 'a');
0244 if fid == -1
0245 error('File open failed. processing terminated.');
0246 end
0247 fids_ex_mask{Nth_script_file} = fid;
0248 files_ex_mask{Nth_script_file} = script_file;
0249 end
0250
0251 cmd = ['fslmaths ' parcel_all_file, ...
0252 ' -sub ', parcel_n_file, ...
0253 ' ', parcel_n_ex_file];
0254 fprintf(fid, '%s\n', cmd);
0255 end
0256
0257
0258 for k=1:length(fids_ex_mask), fclose(fids_ex_mask{k}); end
0259
0260 process_list = cell(0);
0261
0262
0263 onCleanup(@terminate_fcn);
0264
0265
0266 for k=1:length(files_ex_mask)
0267 process_list{k} = runtime.exec(files_ex_mask{k});
0268 end
0269
0270
0271 while(1)
0272 d = dir(fullfile(output_dir, filesep, 'parcel_ex*.nii.gz'));
0273 Ncreated = length(d);
0274 fprintf('Now processing (%d/%d)\n', Ncreated, Nfiles);
0275 if Ncreated >= Nfiles
0276 disp('Done.');
0277 break;
0278 end
0279 pause(30);
0280 end
0281 end
0282
0283
0284 rmdir(work_dir, 's')
0285
0286 toc(start);
0287
0288 end