0001 function out = prt_run_design(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 job = varargin{1};
0022
0023
0024
0025 if isfield(job,'hrfover')
0026 job.fmri_des.hrfover = job.hrfover;
0027 job.fmri_des.hrfdel = job.hrfdel;
0028 end
0029
0030
0031
0032 fname = 'PRT.mat';
0033 fname = fullfile(job.dir_name{1},fname);
0034
0035
0036
0037 ngroup = length(job.group);
0038
0039
0040
0041 nmasks = length(job.mask);
0042 for i = 1:nmasks
0043 mod_names{i} = job.mask(i).mod_name;
0044 masks(i).mod_name = mod_names{i};
0045 masks(i).fname = char(job.mask(i).fmask);
0046 end
0047
0048 mod_names_uniq = unique(mod_names);
0049
0050 if nmasks ~= length(mod_names_uniq);
0051 out.files{1} = [];
0052 beep;
0053 sprintf('Names of mask modalities repeated! Please correct!')
0054 return
0055 end
0056
0057
0058
0059
0060
0061 if isfield(job.group(1).select,'modality')
0062
0063 nmod_scans = length(job.group(1).select.modality);
0064 for g = 1:ngroup
0065
0066 clear mod_names_mod
0067 nmod = length(job.group(g).select.modality);
0068 nsub = length(job.group(g).select.modality(1).subjects);
0069
0070
0071 if nmod ~= nmod_scans
0072 out.files{1} = [];
0073 beep;
0074 sprintf('Numbers of modalities in groups 1 and %d differ!',g)
0075 disp('Please correct!')
0076 return
0077 else
0078 if nmod ~= nmasks
0079 out.files{1} = [];
0080 beep;
0081 sprintf('Number of modalities in group %d different from number of masks!',g)
0082 disp('Please correct!')
0083 return
0084 else
0085
0086 PRT.group(g).gr_name = job.group(g).gr_name;
0087
0088 for s = 1:nsub,
0089 subj_name = sprintf('S%d',s);
0090 for m = 1:nmod,
0091 modnm = job.group(g).select.modality(m).mod_name;
0092 ns = length(job.group(g).select.modality(m).subjects);
0093 if ~isempty(job.group(g).select.modality(m).rt_subj)
0094 rt_subj = job.group(g).select.modality(m).rt_subj(:);
0095 if length(rt_subj) ~= ns
0096 out.files{1} = [];
0097 beep
0098 sprintf('Number of regression targets must be the number of subjects/scans! ')
0099 disp('Please correct!')
0100 return
0101 else
0102 PRT.group(g).subject(s).modality(m).rt_subj = rt_subj(s);
0103 end
0104 else
0105 PRT.group(g).subject(s).modality(m).rt_subj = [];
0106 end
0107 if ~isempty(job.group(g).select.modality(m).covar{1})
0108 try
0109 load(char(job.group(g).select.modality(m).covar{1}));
0110 if exist('R','var')
0111 if size(R,1)==ns
0112 PRT.group(g).subject(s).modality(m).covar = R(s,:);
0113 else
0114 out.files{1} = [];
0115 beep
0116 sprintf('Number of covariates must be the number of subjects/scans! ')
0117 disp('Please correct!')
0118 return
0119 end
0120 else
0121 out.files{1} = [];
0122 beep
0123 sprintf('Covariates file must contain ''R'' variable! ')
0124 disp('Please correct!')
0125 return
0126 end
0127 catch
0128 beep
0129 sprintf('Could not load %s file!',char(job.group(g).select.modality(m).covar{1}))
0130 out.files{1} = [];
0131 return
0132 end
0133 else
0134 PRT.group(g).subject(s).modality(m).covar = [];
0135 end
0136 mod_names_mod{m} = modnm;
0137 if isempty(intersect(mod_names_uniq,modnm)),
0138 out.files{1} = [];
0139 beep
0140 sprintf('Incorrect modality name %s for subject %d group %d! ',modnm,s,g)
0141 disp('Please correct!')
0142 return
0143 end
0144 if nsub ~= ns
0145 out.files{1} = [];
0146 beep
0147 sprintf('Number of subjects in modality %d and 1 of group %d are different! ',m,g)
0148 disp('Please correct!')
0149 return
0150 else
0151 PRT.group(g).subject(s).subj_name = subj_name;
0152 PRT.group(g).subject(s).modality(m).mod_name = job.group(g).select.modality(m).mod_name;
0153 PRT.group(g).subject(s).modality(m).design = 0;
0154 PRT.group(g).subject(s).modality(m).scans = char(job.group(g).select.modality(m).subjects{s});
0155 end
0156 end
0157 if nmod ~= length(unique(mod_names_mod));
0158 out.files{1} = [];
0159 beep;
0160 sprintf('Names of modalities in group %d repeated! Please correct!',g)
0161 return
0162 end
0163 end
0164 end
0165 end
0166 PRT.group(g).hrfoverlap = job.fmri_des.hrfover;
0167 PRT.group(g).hrfdelay = job.fmri_des.hrfdel;
0168 end
0169 else
0170
0171 for g = 1:ngroup,
0172
0173 nmod_subjs = length(job.group(1).select.subject{1});
0174 nsubj = length(job.group(g).select.subject);
0175 nsubj1 = length(job.group(1).select.subject);
0176
0177 if nsubj ~= nsubj1,
0178 disp('Warning: unbalanced groups.')
0179 end
0180 for j = 1:nsubj,
0181 clear mod_names_subj
0182 subj_name = sprintf('S%d',j);
0183 nmod = length(job.group(g).select.subject{j});
0184
0185 if nmod ~= nmod_subjs
0186 out.files{1} = [];
0187 beep
0188 sprintf('Numbers of modalities in subjects 1 and %d from group %d differ!',j,g)
0189 disp('Please correct!')
0190 return
0191 else
0192 if nmod ~= nmasks
0193 out.files{1} = [];
0194 beep
0195 sprintf('Number of modalities in group %d subject %d different from number of masks!',g,j)
0196 disp('Please correct!')
0197 return
0198 else
0199 for k = 1:nmod,
0200 modnm = job.group(g).select.subject{j}(k).mod_name;
0201 TR = job.group(g).select.subject{j}(k).TR;
0202 mod_names_subj{k} = modnm;
0203 if isempty(intersect(mod_names_uniq,modnm)),
0204 out.files{1} = [];
0205 beep
0206 sprintf('Incorrect modality name %s for subject %d group %d! ',modnm,j,g)
0207 disp('Please correct!')
0208 return
0209 end
0210 clear design
0211 if isfield(job.group(g).select.subject{j}(k).design,'load_SPM')
0212
0213 try
0214 load(job.group(g).select.subject{j}(k).design.load_SPM{1});
0215 catch
0216 out.files{1} = [];
0217 beep
0218 disp('Could not load SPM.mat file!')
0219 return
0220 end
0221 switch lower(SPM.xBF.UNITS)
0222 case 'scans'
0223 unit = 0;
0224 case 'seconds'
0225 unit = 1;
0226 case 'secs'
0227 unit = 1;
0228 end
0229 nscans = length(job.group(g).select.subject{j}(k).scans);
0230 ncond = length(SPM.Sess(1).U);
0231 for c = 1:ncond
0232 conds(c).cond_name = SPM.Sess(1).U(c).name{1};
0233 conds(c).onsets = SPM.Sess(1).U(c).ons;
0234 conds(c).durations = SPM.Sess(1).U(c).dur;
0235 end
0236 checked_conds = prt_check_design(conds,TR,unit,job.fmri_des.hrfover,job.fmri_des.hrfdel);
0237 design.conds = checked_conds.conds;
0238 design.stats = checked_conds.stats;
0239 design.TR = TR;
0240 design.unit = unit;
0241 maxcond = max([design.conds(:).scans]);
0242 if nscans>1 && nscans < maxcond
0243 sprintf('Design of subject %d, group %d, modality %d, exceeds time series!',j,g,k)
0244 disp('Corresponding events were discarded')
0245 for l = 1:length(design.conds)
0246 ovser = find(design.conds(l).scans > nscans);
0247 inser = find(design.conds(l).scans <= nscans);
0248 design.conds(l).discardedscans = [design.conds(l).discardedscans, design.conds(l).scans(ovser)];
0249 design.conds(l).scans = design.conds(l).scans(inser);
0250 design.conds(l).blocks = design.conds(l).blocks(inser);
0251 end
0252 end
0253 else
0254 if isfield(job.group(g).select.subject{j}(k).design,'no_design')
0255
0256 design = 0;
0257 else
0258
0259 nscans = length(job.group(g).select.subject{j}(k).scans);
0260 unit = job.group(g).select.subject{j}(k).design.new_design.unit;
0261
0262 if ~isempty(job.group(g).select.subject{j}(k).design.new_design.multi_conds{1})
0263 multi_fname = job.group(g).select.subject{j}(k).design.new_design.multi_conds{1};
0264
0265 try
0266 load(multi_fname);
0267 catch
0268 beep
0269 sprintf('Could not load %s file!',multi_fname)
0270 out.files{1} = [];
0271 return
0272 end
0273 try
0274 multicond.names = names;
0275 catch
0276 beep
0277 disp('No "names" found in the .mat file, please select another file!')
0278 out.files{1} = [];
0279 return
0280 end
0281 try
0282 multicond.durations = durations;
0283 catch
0284 beep
0285 disp('No "durations" found in the .mat file, please select another file!')
0286 out.files{1} = [];
0287 return
0288 end
0289 try
0290 multicond.onsets = onsets;
0291 catch
0292 beep
0293 disp('No "onsets" found in the .mat file, please select another file!')
0294 out.files{1} = [];
0295 return
0296 end
0297 try
0298 multicond.rt_trial = rt_trial;
0299 catch
0300 multicond.rt_trial = cell(length(multicond.onsets),1);
0301 end
0302 for mc = 1:length(multicond.onsets)
0303 conds(mc).cond_name = multicond.names{mc};
0304 conds(mc).onsets = multicond.onsets{mc};
0305 conds(mc).durations = multicond.durations{mc};
0306 conds(mc).rt_trial = multicond.rt_trial{mc};
0307 if isfield(conds(mc),'rt_trial')
0308 lons = length(conds(mc).onsets);
0309 lreg = length(conds(mc).rt_trial);
0310 if lreg ~= lons
0311 out.files{1} = [];
0312 beep
0313 sprintf('Number of regression targets must be the number of trials!')
0314 disp('Please correct')
0315 return
0316 end
0317 end
0318 end
0319 design.conds = conds;
0320 else
0321 design.conds = job.group(g).select.subject{j}(k).design.new_design.conds;
0322 covar = [];
0323 end
0324 ncond = length(design.conds);
0325 for c = 1:ncond
0326 lons = length(design.conds(c).onsets);
0327 ldur = length(design.conds(c).durations);
0328 if ldur == 1
0329 design.conds(c).durations = repmat(design.conds(c).durations, 1, lons);
0330 ldur = length(design.conds(c).durations);
0331 end
0332 if ldur ~= lons
0333 out.files{1} = [];
0334 beep
0335 sprintf('The onsets and durations of condition %d do not have the same size!', c)
0336 disp('Please correct')
0337 return
0338 end
0339 if isfield(design.conds(c),'rt_trial') && ~isempty(design.conds(c).rt_trial)
0340 lreg = length(design.conds(c).rt_trial);
0341 if lreg ~= lons
0342 out.files{1} = [];
0343 beep
0344 sprintf('Number of regression targets must be the number of trials!')
0345 disp('Please correct')
0346 return
0347 end
0348 elseif ~isfield(design.conds(c),'rt_trial')
0349 design.conds(c).rt_trial=[];
0350 end
0351 end
0352 checked_conds = prt_check_design(design.conds,TR,unit,job.fmri_des.hrfover,job.fmri_des.hrfdel);
0353 design.conds = checked_conds.conds;
0354 design.stats = checked_conds.stats;
0355 design.TR = checked_conds.TR;
0356 design.unit = unit;
0357 design.covar = covar;
0358 maxcond = max([design.conds(:).scans]);
0359 if nscans>1 && nscans < maxcond
0360 sprintf('Design of subject %d, group %d, modality %d, exceeds time series!',j,g,k)
0361 disp('Corresponding events were discarded')
0362 for l = 1:length(design.conds)
0363 ovser = find(design.conds(l).scans > nscans);
0364 inser = find(design.conds(l).scans <= nscans);
0365 design.conds(l).discardedscans = [design.conds(l).discardedscans, design.conds(l).scans(ovser)];
0366 design.conds(l).scans = design.conds(l).scans(inser);
0367 design.conds(l).blocks = design.conds(l).blocks(inser);
0368 end
0369 end
0370 end
0371 end
0372
0373 PRT.group(g).gr_name = job.group(g).gr_name;
0374 PRT.group(g).subject(j).subj_name = subj_name;
0375 PRT.group(g).subject(j).modality(k) = job.group(g).select.subject{j}(k);
0376 PRT.group(g).subject(j).modality(k).TR = job.group(g).select.subject{j}(k).TR;
0377 PRT.group(g).subject(j).modality(k).design = design;
0378 PRT.group(g).subject(j).modality(k).scans = char(job.group(g).select.subject{j}(k).scans);
0379
0380 end
0381 end
0382 end
0383 if nmod ~= length(unique(mod_names_subj));
0384 out.files{1} = [];
0385 beep;
0386 sprintf('Names of modalities in subject %d group %d repeated! Please correct!',j,g)
0387 return
0388 end
0389 end
0390 PRT.group(g).hrfoverlap = job.fmri_des.hrfover;
0391 PRT.group(g).hrfdelay = job.fmri_des.hrfdel;
0392 end
0393 end
0394
0395
0396
0397 PRT.masks = masks;
0398
0399
0400
0401 disp('Saving PRT.mat.......>>')
0402 if spm_check_version('MATLAB','7') < 0
0403 save(fname,'-V6','PRT');
0404 else
0405 save(fname,'PRT');
0406 end
0407
0408
0409
0410 if job.review
0411 prt_data_review('UserData',{PRT,job.dir_name{1}});
0412 end
0413
0414
0415
0416 out.files{1} = fname;
0417
0418 for g = 1:ngroup
0419 out.(sprintf('gr_name%d',g)) = PRT.group(g).gr_name;
0420 end
0421
0422 for m = 1:numel(mod_names_uniq)
0423 out.(sprintf('mod_name%d',m)) = mod_names_uniq{m};
0424 end
0425
0426 disp('Done')
0427
0428 return
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459