% This batch script analyses the Face fMRI dataset available from the SPM site:
% http://www.fil.ion.ucl.ac.uk/spm/data/face_rep/face_rep_SPM5.html
% as described in the manual Chapter 29.

% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging

% Guillaume Flandin
% $Id: face_repetition_single_spm5_batch.m 30 2008-05-20 11:16:55Z guillaume $

%% Path containing data
%--------------------------------------------------------------------------
data_path = 'C:\work\data\face_rep';

%% Set Matlab path
%--------------------------------------------------------------------------
addpath('C:\work\spm5');
addpath('C:\work\misc'); % path containing editfilenames.m

%% Initialise SPM defaults
%--------------------------------------------------------------------------
spm('Defaults','fMRI');

spm_jobman('initcfg'); % SPM8 only

%% WORKING DIRECTORY (useful for .ps only)
%--------------------------------------------------------------------------
clear jobs
jobs{1}.util{1}.cdir.directory = cellstr(data_path);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SPATIAL PREPROCESSING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Select functional and structural scans
%--------------------------------------------------------------------------
f = spm_select('FPList', fullfile(data_path,'RawEPI'), '^sM.*\.img$');
a = spm_select('FPList', fullfile(data_path,'Structural'), '^sM.*\.img$');

%% REALIGN
%--------------------------------------------------------------------------
jobs{2}.spatial{1}.realign{1}.estwrite.data{1} = cellstr(f);

%% SLICE TIMING CORRECTION
%--------------------------------------------------------------------------
jobs{3}.temporal{1}.st.scans{1} = editfilenames(f,'prefix','r');
jobs{3}.temporal{1}.st.nslices = 24;
jobs{3}.temporal{1}.st.tr = 2;
jobs{3}.temporal{1}.st.ta = 2-2/24;
jobs{3}.temporal{1}.st.so = 24:-1:1;
jobs{3}.temporal{1}.st.refslice = 12;

%% COREGISTRATION
%--------------------------------------------------------------------------
jobs{4}.spatial{1}.coreg{1}.estimate.ref = editfilenames(f(1,:),'prefix','mean');
jobs{4}.spatial{1}.coreg{1}.estimate.source = cellstr(a);

%% SEGMENT
%--------------------------------------------------------------------------
jobs{4}.spatial{2}.preproc.data = cellstr(a);

%% NORMALIZE
%--------------------------------------------------------------------------
jobs{4}.spatial{3}.normalise{1}.write.subj.matname  = editfilenames(a,'suffix','_seg_sn','ext','.mat');
ff = editfilenames(f(1,:),'prefix','mean');
jobs{4}.spatial{3}.normalise{1}.write.subj.resample = [editfilenames(f,'prefix','ar'); {ff{1}}];
jobs{4}.spatial{3}.normalise{1}.write.roptions.vox  = [3 3 3];

jobs{4}.spatial{3}.normalise{2}.write.subj.matname  = editfilenames(a,'suffix','_seg_sn','ext','.mat');
jobs{4}.spatial{3}.normalise{2}.write.subj.resample = editfilenames(a,'prefix','m');
jobs{4}.spatial{3}.normalise{2}.write.roptions.vox  = [1 1 1.5];

%% SMOOTHING
%--------------------------------------------------------------------------
jobs{4}.spatial{4}.smooth.data = editfilenames(f,'prefix','war');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% RUN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
save('face_batch_preprocessing.mat','jobs');
%spm_jobman('interactive',jobs);
spm_jobman('run',jobs);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CLASSICAL STATISTICAL ANALYSIS (CATEGORICAL)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Load onsets
%--------------------------------------------------------------------------
onsets    = load(fullfile(data_path,'sots.mat'));
condnames = {'N1' 'N2' 'F1' 'F2'};

%% OUTPUT DIRECTORY
%--------------------------------------------------------------------------
clear jobs
jobs{1}.util{1}.md.basedir = cellstr(data_path);
jobs{1}.util{1}.md.name = 'categorical';

%% MODEL SPECIFICATION AND ESTIMATION
%--------------------------------------------------------------------------
jobs{2}.stats{1}.fmri_spec.dir = cellstr(fullfile(data_path,'categorical'));
jobs{2}.stats{1}.fmri_spec.timing.units = 'scans';
jobs{2}.stats{1}.fmri_spec.timing.RT = 2;
jobs{2}.stats{1}.fmri_spec.timing.fmri_t = 24;
jobs{2}.stats{1}.fmri_spec.timing.fmri_t0 = 12;
jobs{2}.stats{1}.fmri_spec.sess.scans = editfilenames(f,'prefix','swar');
for i=1:numel(condnames)
    jobs{2}.stats{1}.fmri_spec.sess.cond(i).name = condnames{i};
    jobs{2}.stats{1}.fmri_spec.sess.cond(i).onset = onsets.sot{i};
    jobs{2}.stats{1}.fmri_spec.sess.cond(i).duration = 0;
end
jobs{2}.stats{1}.fmri_spec.sess.multi_reg = editfilenames(f(1,:),'prefix','rp_','ext','.txt');
jobs{2}.stats{1}.fmri_spec.fact(1).name = 'Fame';
jobs{2}.stats{1}.fmri_spec.fact(1).levels = 2;
jobs{2}.stats{1}.fmri_spec.fact(2).name = 'Rep';
jobs{2}.stats{1}.fmri_spec.fact(2).levels = 2;
jobs{2}.stats{1}.fmri_spec.bases.hrf.derivs = [1 1];

jobs{2}.stats{2}.fmri_est.spmmat = cellstr(fullfile(data_path,'categorical','SPM.mat'));
save(fullfile(data_path,'categorical_spec.mat'),'jobs');

%% INFERENCE
%--------------------------------------------------------------------------
jobs{2}.stats{3}.results.spmmat = cellstr(fullfile(data_path,'categorical','SPM.mat'));
jobs{2}.stats{3}.results.conspec(1).contrasts = Inf;
jobs{2}.stats{3}.results.conspec(1).threshdesc = 'FWE';

jobs{2}.stats{4}.results.spmmat = cellstr(fullfile(data_path,'categorical','SPM.mat'));
jobs{2}.stats{4}.results.conspec(1).titlestr = 'main effect of Rep (masked [incl.] by ...)';
jobs{2}.stats{4}.results.conspec(1).contrasts = 3;
jobs{2}.stats{4}.results.conspec(1).threshdesc = 'none';
jobs{2}.stats{4}.results.conspec(1).thresh = 0.001;
jobs{2}.stats{4}.results.conspec(1).extent = 0;
jobs{2}.stats{4}.results.conspec(1).mask.contrasts = 5;
jobs{2}.stats{4}.results.conspec(1).mask.thresh = 0.001;
jobs{2}.stats{4}.results.conspec(1).mask.mtype = 0;

jobs{2}.stats{5}.con.spmmat = cellstr(fullfile(data_path,'categorical','SPM.mat'));
jobs{2}.stats{5}.con.consess{1}.fcon.name = 'Movement-related effects';
fcont = [zeros(6,3*4) eye(6)];
for i=1:size(fcont,1)
	jobs{2}.stats{5}.con.consess{1}.fcon.convec{1,i} = fcont(i,:);
end
jobs{2}.stats{6}.results.spmmat = cellstr(fullfile(data_path,'categorical','SPM.mat'));
jobs{2}.stats{6}.results.conspec(1).contrasts = 17;
jobs{2}.stats{6}.results.conspec(1).threshdesc = 'FWE';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% RUN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
save('face_batch_categorical.mat','jobs');
%spm_jobman('interactive',jobs);
spm_jobman('run',jobs);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CLASSICAL STATISTICAL ANALYSIS (PARAMETRIC)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Load onsets
onsets = load(fullfile(data_path,'sots.mat'));

%% OUTPUT DIRECTORY
%--------------------------------------------------------------------------
clear jobs
jobs{1}.util{1}.md.basedir = cellstr(data_path);
jobs{1}.util{1}.md.name = 'parametric';

%% MODEL SPECIFICATION AND ESTIMATION
%--------------------------------------------------------------------------
j = load(fullfile(data_path,'categorical_spec.mat'));
jobs{2} = j.jobs{2};
jobs{2}.stats{1}.fmri_spec.sess.cond(2).pmod.name = 'Lag';
jobs{2}.stats{1}.fmri_spec.sess.cond(2).pmod.param = onsets.itemlag{2};
jobs{2}.stats{1}.fmri_spec.sess.cond(2).pmod.poly = 2;
jobs{2}.stats{1}.fmri_spec.sess.cond(4).pmod.name = 'Lag';
jobs{2}.stats{1}.fmri_spec.sess.cond(4).pmod.param = onsets.itemlag{4};
jobs{2}.stats{1}.fmri_spec.sess.cond(4).pmod.poly = 2;
jobs{2}.stats{1}.fmri_spec.dir = cellstr(fullfile(data_path,'parametric'));
jobs{2}.stats{1}.fmri_spec.bases.hrf.derivs = [0 0];

jobs{2}.stats{2}.fmri_est.spmmat = cellstr(fullfile(data_path,'parametric','SPM.mat'));

%% INFERENCE
%--------------------------------------------------------------------------
jobs{2}.stats{3}.con.spmmat = cellstr(fullfile(data_path,'parametric','SPM.mat'));
jobs{2}.stats{3}.con.consess{1}.fcon.name = 'Famous Lag';
fcont = [zeros(2,6) eye(2)];
for i=1:size(fcont,1)
	jobs{2}.stats{3}.con.consess{1}.fcon.convec{1,i} = fcont(i,:);
end

jobs{2}.stats{4}.results.spmmat = cellstr(fullfile(data_path,'parametric','SPM.mat'));
jobs{2}.stats{4}.results.conspec(1).contrasts = Inf;
jobs{2}.stats{4}.results.conspec(1).threshdesc = 'FWE';

jobs{2}.stats{5}.results.spmmat = cellstr(fullfile(data_path,'parametric','SPM.mat'));
jobs{2}.stats{5}.results.conspec(1).titlestr = 'Famous Lag (masked [incl.] by ...)';
jobs{2}.stats{5}.results.conspec(1).contrasts = 9;
jobs{2}.stats{5}.results.conspec(1).threshdesc = 'none';
jobs{2}.stats{5}.results.conspec(1).thresh = 0.001;
jobs{2}.stats{5}.results.conspec(1).extent = 0;
jobs{2}.stats{5}.results.conspec(1).mask.contrasts = 5;
jobs{2}.stats{5}.results.conspec(1).mask.thresh = 0.05;
jobs{2}.stats{5}.results.conspec(1).mask.mtype = 0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% RUN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
save('face_batch_parametric.mat','jobs');
%spm_jobman('interactive',jobs);
spm_jobman('run',jobs);
