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