0001 function remove_physio(proc,sinfo)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 error(nargchk(2,2,nargin,'struct'))
0024
0025
0026
0027
0028 nproc = length(proc);
0029 defidx = [];
0030 for iproc = 1:nproc
0031 if strcmp(proc{iproc}.type,'defs')
0032 defidx = iproc;
0033 rootdir = proc{iproc}.rootdir;
0034 verbose = proc{iproc}.verbose;
0035 end
0036 end
0037
0038 stats_dir = 'stats';
0039
0040 logstub = '';
0041 if ~isempty(logstub)
0042 logfile = fullfile(proc{defidx}.logdir, sprintf('%s_%s.txt',logstub,datestr(datenum(now),'yymmdd_HHMM')));
0043 logfid = fopen(logfile,'wt');
0044 fprintf('Writing logfile: %s\n', logfile);
0045 else
0046 logfid = 1;
0047 end
0048
0049 if proc{defidx}.use_mc moco_str = '_mc'; else moco_str = ''; end
0050
0051 start_dir = pwd;
0052
0053 nsub_proc = length(sinfo);
0054 for isub = 1:nsub_proc
0055 subid = sinfo(isub).id;
0056 sdirs = check_subdirs(rootdir,subid,verbose);
0057
0058
0059
0060
0061 phys_fname = fullfile(sdirs.subdir, sprintf('%s_phys.mat', subid));
0062 fprintf(logfid,'Loading physio data from: %s\n', phys_fname);
0063 load(phys_fname)
0064
0065 nruns = length(sinfo(isub).use_runs);
0066 for irun = 1:nruns
0067 run_id = sinfo(isub).use_runs(irun);
0068
0069
0070 if ~run_id == phys.ri(irun).id
0071 error(sprintf('Run ID mismatch: phys.ri.id=%d, run_id=%d', phys.ri(irun).id, run_id))
0072 end
0073
0074 rdirs = check_rundirs(sdirs.physiodir, run_id,verbose);
0075
0076 for iproc = 1:nproc
0077 cp = proc{iproc};
0078 switch cp.type
0079 case 'eval_model'
0080
0081 epifname = fullfile(sdirs.epidir, sprintf('Y_run%d%s_bet.nii.gz', run_id, moco_str));
0082
0083
0084 try
0085 check_exist(epifname);
0086 catch
0087 fprintf(logfid,'WARNING: Wanted run %d but could not find %s\n', run_id, epifname);
0088 continue
0089 end
0090
0091 fsl_str = sprintf('avwval %s dim1', epifname);
0092 [status, nx] = unix(fsl_str);
0093 nx = str2num(nx);
0094
0095 fsl_str = sprintf('avwval %s dim2', epifname);
0096 [status, ny] = unix(fsl_str);
0097 ny = str2num(ny);
0098
0099 fsl_str = sprintf('avwval %s dim3', epifname);
0100 [status, nz] = unix(fsl_str);
0101 nslices = str2num(nz);
0102
0103
0104
0105 fsl_str = sprintf('avwstats %s -M', epifname);
0106 [status, mean_intensity] = unix(fsl_str);
0107 mean_intensity = str2num(mean_intensity);
0108 thresh = mean_intensity*cp.intensity_cutoff/100;
0109
0110
0111 [fpath, fstub, fext] = fileparts(epifname);
0112 meanfname = fullfile(fpath, sprintf('%s_mean.nii.gz', strtok(fstub,'.')));
0113 fsl_str = sprintf('/usr/local/fsl/bin/avwmaths %s -Tmean %s', epifname, meanfname);
0114 fprintf(logfid,'%s\n', fsl_str);
0115 status = unix(fsl_str);
0116 if status
0117 error('Failed to calculate mean of input EPI data file')
0118 end
0119
0120 for islice = 1:nslices
0121
0122 featdir = fullfile(rdirs.rundir, sprintf('slice%02d.feat', islice));
0123
0124 check_dir(featdir);
0125
0126 funcfname = fullfile(sdirs.epidir, sprintf('slice%02d_data.nii.gz', islice));
0127
0128
0129 fsf = create_fsf;
0130 fsf = set_fsf_defaults(fsf, 'remove_physio');
0131
0132 fsf.tr = cp.TR;
0133 fsf.fsldir = featdir;
0134 fsf.outputdir = featdir;
0135 fsf.feat_files{1} = funcfname;
0136 fsf.npts = phys.ps.sinfo.nvol(run_id);
0137
0138
0139 nsets = length(phys.ri(irun).ev_info);
0140 nev = 0;
0141 ev = create_ev;
0142 ev_names = {};
0143
0144 ev_info = phys.ri(irun).ev_info;
0145 for iset = 1:nsets
0146 nev_in_set = ev_info(iset).nev;
0147 for iev = 1:nev_in_set
0148 cev = create_ev;
0149 cev.shape = 2;
0150 cev.fname = ev_info(iset).fnames{islice,iev};
0151
0152 nev = nev+1;
0153 ev(nev) = cev;
0154 ev_names{nev} = ev_info(iset).ev_names{iev};
0155 end
0156 end
0157 fsf.ev = ev;
0158
0159 fsf.evs_orig = nev;
0160 fsf.evs_real = nev;
0161
0162
0163
0164
0165
0166 for iev = 1:nev
0167 fsf.ev(iev).ortho = zeros(1,nev);
0168 end
0169
0170
0171
0172
0173
0174
0175
0176
0177 fsf = setup_fsl_con(fsf, ev_names, cp.conlist, cp.Fconlist);
0178
0179
0180 write_fsf(fsf);
0181
0182
0183
0184 fsf_fname = fullfile(featdir, 'fsf.mat');
0185 save(fsf_fname, 'fsf');
0186
0187
0188 cd(featdir)
0189
0190
0191 fsl_str = sprintf('avwroi %s %s 0 %d 0 %d %d 1 0 %d', epifname, funcfname, nx, ny, islice-1, phys.ps.sinfo.nvol(run_id));
0192 fprintf(logfid,'%s\n', fsl_str);
0193 status = unix(fsl_str);
0194 if status
0195 error('Failed to extract relevant slice from the EPI data file')
0196 end
0197
0198
0199 slicemeanfname = fullfile(sdirs.epidir, sprintf('slice%02d_data_mean.nii.gz', islice));
0200 fsl_str = sprintf('avwroi %s %s 0 %d 0 %d %d 1 0 1', meanfname, slicemeanfname, nx, ny, islice-1);
0201 fprintf(logfid,'%s\n', fsl_str);
0202 status = unix(fsl_str);
0203 if status
0204 error('Failed to extract relevant slice from the mean data file')
0205 end
0206
0207
0208
0209
0210
0211
0212 fsl_str = '/usr/local/fsl/bin/feat_model design';
0213
0214 fprintf(logfid,'%s\n', fsl_str);
0215 status = unix(fsl_str);
0216 if status
0217 error('Checking of model failed')
0218 end
0219
0220
0221 if exist(stats_dir)
0222 unix_str = sprintf('rm -rf %s', stats_dir);
0223 unix(unix_str);
0224 end
0225
0226
0227 fsl_str = sprintf(['/usr/local/fsl/bin/film_gls -rn %s -noest' ...
0228 ' %s design.mat %1.4f'], stats_dir, funcfname, thresh);
0229 fprintf(logfid,'%s\n', fsl_str);
0230 status = unix(fsl_str);
0231 if status
0232 error('FEAT run failed or was aborted')
0233 end
0234
0235 if cp.add_mean_to_resid
0236
0237
0238
0239
0240
0241
0242
0243 resid_fname = fullfile(stats_dir, 'res4d.nii.gz');
0244 fsl_str = sprintf('/usr/local/fsl/bin/avwmaths %s -add %s %s', resid_fname, slicemeanfname, resid_fname);
0245 fprintf(logfid,'%s\n', fsl_str);
0246 status = unix(fsl_str);
0247 if status
0248 error('Failed to add mean back into the residuals')
0249 end
0250 end
0251
0252
0253 delete(funcfname);
0254 delete(slicemeanfname);
0255
0256 cd(start_dir)
0257 end
0258
0259
0260
0261
0262
0263 stats_flist = dir(fullfile(rdirs.rundir,'slice01.feat/stats/*.nii.gz'));
0264 nstats_images = length(stats_flist);
0265 for iimg = 1:nstats_images
0266 merge_list = {};
0267 for islice = 1:nslices
0268 merge_list{islice} = fullfile(rdirs.rundir, sprintf('slice%02d.feat', islice),'stats',stats_flist(iimg).name);
0269 end
0270
0271 outfname = fullfile(rdirs.fullvolstatsdir,stats_flist(iimg).name);
0272 fsl_str = sprintf('/usr/local/fsl/bin/avwmerge -z %s %s', outfname, cell2str(merge_list, ' '));
0273 fprintf(logfid,'%s\n', fsl_str);
0274 status = unix(fsl_str);
0275 if status
0276 error(sprintf('Failed to merge slice files into: %s', outfname))
0277 end
0278 end
0279
0280
0281 case 'calc_stats'
0282 fprintf(logfid, sprintf(['\nEvaluating statistical significance of the' ...
0283 ' physiological variables in run %d\n'], run_id));
0284 slice_featdir = fullfile(rdirs.rundir,'slice01.feat');
0285
0286
0287 orig_maskfname = fullfile(sdirs.epidir, sprintf('Y_run%d%s_bet_mask.nii.gz', run_id, moco_str));
0288 maskfname = fullfile(fullvoldir,'mask.nii.gz');
0289 unix_str = sprintf('cp %s %s', orig_maskfname, maskfname);
0290 status = unix(unix_str);
0291 if status
0292 error('Error copying mask file')
0293 end
0294
0295
0296 unix_str = sprintf('cp %s %s', fullfile(slice_featdir,stats_dir,'dof'), rdirs.fullvolstatsdir);
0297 status = unix(unix_str);
0298 if status
0299 error('Error copying dof file')
0300 end
0301
0302
0303 epifname = fullfile(sdirs.epidir, sprintf('Y_run%d%s_bet.nii.gz', run_id, moco_str));
0304 funcfname = fullfile(rdirs.fullvoldir,'filtered_func_data.nii.gz');
0305 unix_str = sprintf('cp %s %s', epifname, funcfname);
0306 fprintf(logfid,'%s\n', unix_str);
0307 status = unix(unix_str);
0308 if status
0309 error('Error copying original EPI data')
0310 end
0311
0312
0313 example_funcfname = fullfile(rdirs.fullvoldir, 'example_func.nii.gz');
0314 fsl_str = sprintf('/usr/local/fsl/bin/avwroi %s %s 0 1', funcfname, example_funcfname);
0315 fprintf(logfid,'%s\n', fsl_str);
0316 status = unix(fsl_str);
0317 if status
0318 error('Error extracting example functional volume')
0319 end
0320
0321
0322 fsf_fname = fullfile(slice_featdir,'fsf.mat');
0323 check_exist(fsf_fname);
0324 load(fsf_fname)
0325
0326
0327 fsf = set_fsf_defaults(fsf, 'evaluate_physio');
0328 fsf.outputdir = rdirs.fullvoldir;
0329 fsf.fsldir = rdirs.fullvoldir;
0330 fsf.feat_files{1} = rdirs.fullvoldir;
0331
0332 write_fsf(fsf);
0333
0334
0335 save(fullfile(rdirs.fullvoldir, 'fsf.mat'), 'fsf')
0336
0337
0338 cd(rdirs.fullvoldir)
0339
0340 fsl_str = sprintf('/usr/local/fsl/bin/feat %s', 'design.fsf');
0341 fprintf(logfid,'%s\n', fsl_str);
0342 status = unix(fsl_str);
0343 if status
0344 error('Problems running FEAT')
0345 end
0346 cd(start_dir)
0347
0348
0349
0350 case 'spat_reg'
0351
0352 fsf_fname = fullfile(rdirs.fullvoldir,'fsf.mat');
0353 check_exist(fsf_fname);
0354 load(fsf_fname)
0355
0356 fsf = set_fsf_defaults(fsf, 'coreg');
0357 fsf.outputdir = rdirs.fullvoldir;
0358 fsf.fsldir = rdirs.fullvoldir;
0359 if ~iscell(fsf.feat_files)
0360 fsf.feat_files = {rdirs.fullvoldir};
0361 end
0362
0363
0364 coplanar_stub = sprintf('%s_coplanar', subid);
0365 coplanar_fname = fullfile(sdirs.anatdir,sprintf('%s.nii.gz', coplanar_stub));
0366 fsf.initial_highres_files{1} = coplanar_fname;
0367
0368
0369 hires_stub = sprintf('%s_hires', subid);
0370 hires_fname = fullfile(sdirs.anatdir,sprintf('%s.nii.gz', hires_stub));
0371 fsf.highres_files{1} = hires_fname;
0372
0373 write_fsf(fsf);
0374
0375
0376 cd(rdirs.fullvoldir)
0377
0378 fsl_str = sprintf('/usr/local/fsl/bin/feat %s', 'design.fsf');
0379 fprintf(logfid,'%s\n', fsl_str);
0380 status = unix(fsl_str);
0381 if status
0382 error('Problems running FEAT')
0383 end
0384
0385
0386 save(fullfile(rdirs.fullvoldir, 'fsf.mat'), 'fsf')
0387
0388 cd(start_dir)
0389
0390 otherwise
0391 end
0392 end
0393 end
0394 end
0395 end
0396
0397 function sdirs = check_subdirs(rootdir,subid, verbose)
0398 subdir = fullfile(rootdir, subid);
0399 epidir = fullfile(subdir, 'epi');
0400
0401 fsldir = fullfile(subdir, 'fsl');
0402 check_dir(fsldir, verbose);
0403
0404 anatdir = fullfile(subdir, 'anat');
0405 check_dir(anatdir);
0406
0407 physiodir = fullfile(subdir, 'physio_resid');
0408 check_dir(physiodir, verbose);
0409
0410
0411 sdirs = struct('subdir', subdir, 'epidir', epidir, 'fsldir', fsldir, ...
0412 'anatdir', anatdir, 'physiodir', physiodir);
0413 end
0414
0415 function rdirs = check_rundirs(rootdir, runid, verbose)
0416 rundir = fullfile(rootdir,sprintf('run%d', runid));
0417 check_dir(rundir, verbose);
0418
0419 fullvoldir = fullfile(rundir, 'fullvol');
0420 check_dir(fullvoldir, verbose);
0421
0422 fullvolstatsdir = fullfile(fullvoldir, 'stats');
0423 check_dir(fullvolstatsdir, verbose);
0424
0425
0426 rdirs = struct('rundir', rundir, 'fullvoldir', fullvoldir, ...
0427 'fullvolstatsdir', fullvolstatsdir);
0428 end