Skip to content
A_SpecifyIndividualRegressorMats_noMotion_withFeature.m 11.4 KiB
Newer Older
Julian Kosciessa's avatar
Julian Kosciessa committed
function SpecifyIndividualRegressorMats_noMotion_withFeature

% Create SPM experimental condition info for each subject for later
% batching with tardis

% v3: create Dim-Att regressors, include cue onset, probe onset regressors
% use FAST modelinstead of AR(1): Corbin, N., Todd, N., Friston, K. J., & Callaghan, M. F. (2018). Accurate modeling of temporal correlations in rapidly sampled fMRI time series. Human Brain Mapping, 26(3), 839?14. http://doi.org/10.1002/hbm.24218
% - AP to change back
% v4: AP remove -1 from design timing, but add durations to cue (1 volume) and
% probe (2).

% N = 44 YA + 53 OA;
% AP remove 2131, 2237, 1215 (see oNe Note)

% JQK: set up 2 regressor matrices for F-tests: load + stimulus target feature 

IDs = {'1117';'1118';'1120';'1124';'1125';'1126';'1131';'1132';'1135';'1136';...
    '1151';'1160';'1164';'1167';'1169';'1172';'1173';'1178';'1182';'1214';...
    '1216';'1219';'1223';'1227';'1228';'1233';'1234';'1237';'1239';'1240';'1243';...
    '1245';'1247';'1250';'1252';'1257';'1261';'1265';'1266';'1268';'1270';'1276';'1281';...
    '2104';'2107';'2108';'2112';'2118';'2120';'2121';'2123';'2125';'2129';'2130';...
    '2132';'2133';'2134';'2135';'2139';'2140';'2145';'2147';'2149';'2157';...
    '2160';'2201';'2202';'2203';'2205';'2206';'2209';'2210';'2211';'2213';'2214';...
    '2215';'2216';'2217';'2219';'2222';'2224';'2226';'2227';'2236';'2238';...
    '2241';'2244';'2246';'2248';'2250';'2251';'2252';'2258';'2261'};

pn.root = '/Volumes/LNDG/Projects/StateSwitch/dynamic/data/mri/task/analyses/G_GLM/';
addpath([pn.root, 'T_tools/spm12/']);
spm fmri


%Path to Base Dir of local output
pn.root = '/Volumes/LNDG/Projects/StateSwitch/dynamic/data/mri/task/analyses/G_GLM/';
pn.batchPath = [pn.root, 'B_data/D_batchFiles1stLevelGLM_v3/'];
mkdir(pn.batchPath);
%addpath([pn.root, 'T_tools/spm12/']); % add spm functions

for indID = 1:numel(IDs)
    
    disp(['Processing ', IDs{indID}]);
    
    matlabbatch = cell(1);
    
    % specify general parameters
    
    matlabbatch{1}.spm.stats.fmri_spec.dir = {[pn.root, 'B_data/F_GLM1stLevelout_v1/' IDs{indID} '/']};
    matlabbatch{1}.spm.stats.fmri_spec.timing.units = 'scans';
    matlabbatch{1}.spm.stats.fmri_spec.timing.RT = 0.645;
    matlabbatch{1}.spm.stats.fmri_spec.timing.fmri_t = 16;
    matlabbatch{1}.spm.stats.fmri_spec.timing.fmri_t0 = 8;
    matlabbatch{1}.spm.stats.fmri_spec.fact = struct('name', {}, 'levels', {});
    matlabbatch{1}.spm.stats.fmri_spec.bases.hrf.derivs = [1 0];
    matlabbatch{1}.spm.stats.fmri_spec.volt = 1;
    matlabbatch{1}.spm.stats.fmri_spec.global = 'None';
    matlabbatch{1}.spm.stats.fmri_spec.mthresh = -Inf;
    matlabbatch{1}.spm.stats.fmri_spec.mask = {''};
    matlabbatch{1}.spm.stats.fmri_spec.cvi = 'AR(1)';
    
    
    % subjects with different run number
    
    if strcmp(IDs{indID}, '2131') || strcmp(IDs{indID}, '2237')
        numOfRuns = 2;
    else
        numOfRuns = 4;
    end
    
    for indRun = 1:numOfRuns
        
        % get regressor data
        pn.regressors = [pn.root, 'B_data/A_regressors/'];
        load([pn.regressors, IDs{indID}, '_Run',num2str(indRun),'_regressors.mat'])
        
        % change regressor matrix, such that column 4 represents trial-by-trial
        % indices of dimensionality
        blockOnsets=find(Regressors(:,1) == 1);
        for indBlock = 1:numel(blockOnsets)
            if indBlock == numel(blockOnsets)
                Regressors(blockOnsets(indBlock):end,4) = ...
                    Regressors(blockOnsets(indBlock),4);
            else
                Regressors(blockOnsets(indBlock):blockOnsets(indBlock+1)-1,4) = ...
                    Regressors(blockOnsets(indBlock),4);
            end
        end
        
        % For the scans, we need to specify separate 3D images via comma seperator
        % Under the hood: design-specified time points are extracted from 2D voxel*time matrix
        basefile = ['/Volumes/LNDG/Projects/StateSwitch/dynamic/data/mri/task/analyses/B3_PLS_mean_v3/B_data/BOLDin/',IDs{indID},'_run-',num2str(indRun),'_feat_detrended_bandpassed_FIX_MNI3mm_std.nii'];
        
        allFiles={};
        if strcmp(IDs{indID}, '2132') && indRun == 2
            disp(['Change #volumes for run2']);
            for indVol = 1:1040
                allFiles{indVol,1} = [basefile, ',', num2str(indVol)];
            end
        else
            for indVol = 1:1054
                allFiles{indVol,1} = [basefile, ',', num2str(indVol)];
            end
        end
        
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).scans = allFiles;
        
        % stimulus viewing condition
        % To get relevant-feature-specific conditions, we have to create 4
        % stim conditions: color, direction, size & luminance
        AttributeNames = {'color'; 'direction'; 'size'; 'luminance'};
        for indAtt = 1:4 % here: contained in target set
            AttOnsets = find(Regressors(:,3) == 1 & Regressors(:,6+indAtt) == 1);
            PM = Regressors(AttOnsets,4);
            AttDur = repmat(4, numel(AttOnsets),1);
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).name = AttributeNames{indAtt};
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).onset = AttOnsets; 
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).duration = AttDur;
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).tmod = 0;
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).pmod.name = 'Stim Load';
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).pmod.param = PM;
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).pmod.poly = 1;
            matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(indAtt).orth = 1;
        end
        
        % add cue regressor
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(5).name = 'CueOnset';
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(5).onset = find(Regressors(:,2) == 1); % IMPORTANT: SPM starts counting at 0
        onsets = matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(5).onset;
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(5).duration = repmat(3, numel(onsets), 1); clear onsets; % duration of 1 (VarToolbox) vs 0 (SPM convention)
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(5).tmod = 0;
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(5).pmod = struct('name', {}, 'param', {}, 'poly', {});
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(5).orth = 0;
        
        % add probe regressor
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(6).name = 'ProbeOnset';
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(6).onset = find(Regressors(:,11) == 1); % IMPORTANT: SPM starts counting at 0
        onsets = matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(6).onset;
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(6).duration = repmat(2, numel(onsets), 1); clear onsets; % duration of 1 (VarToolbox) vs 0 (SPM convention)
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(6).tmod = 0;
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(6).pmod = struct('name', {}, 'param', {}, 'poly', {});
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).cond(6).orth = 0;
        
        %% add regressors
        
        
%         MotConfoundFile=[pn.root, 'B_data/MotionParameters/' IDs{indID} '/' 'sess-' int2str(indRun) '/' IDs{indID} '_motionout_scol.txt'];
%         matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).multi_reg={MotConfoundFile
%             [pn.root, 'B_data/M_MotionParameters/' IDs{indID} '/' IDs{indID} '_sess-' int2str(indRun) '_motion_6dof.txt']};
        
%         matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).multi_reg={[pn.root, 'B_data/M_MotionParameters/' IDs{indID} '/' IDs{indID} '_sess-' int2str(indRun) '_motion_6dof.txt']};
        
        
        %% add general session information
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).multi = {''};
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).regress = struct('name', {}, 'val', {});
        matlabbatch{1}.spm.stats.fmri_spec.sess(indRun).hpf = 128;
        matlabbatch{1}.spm.stats.fmri_spec.fact = struct('name', {}, 'levels', {});
        matlabbatch{1}.spm.stats.fmri_spec.bases.hrf.derivs = [1 0];
        matlabbatch{1}.spm.stats.fmri_spec.volt = 1;
        matlabbatch{1}.spm.stats.fmri_spec.global = 'None';
        matlabbatch{1}.spm.stats.fmri_spec.mthresh = -Inf;
        matlabbatch{1}.spm.stats.fmri_spec.mask = {[pn.root, 'B_data/E_standards/GM_MNI3mm_mask.nii']};
        matlabbatch{1}.spm.stats.fmri_spec.cvi = 'AR(1)';
    end % session loop (i.e. runs)
    
    %% Estimate Model
    
    matlabbatch{2}.spm.stats.fmri_est.spmmat(1) = cfg_dep('fMRI model specification: SPM.mat File', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','spmmat'));
    matlabbatch{2}.spm.stats.fmri_est.write_residuals = 0;
    matlabbatch{2}.spm.stats.fmri_est.method.Classical = 1;
    
%     %% Now contrasts: Simple Contrast Effects (for now)
%     matlabbatch{3}.spm.stats.con.spmmat(1) = cfg_dep('fMRI Contrast Manager: SPM.mat File', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','spmmat'));
%     
%     % With only one HRF derivative
%         
%      matlabbatch{3}.spm.stats.con.consess{1}.tcon.name = 'Stimulus Condition';
%      matlabbatch{3}.spm.stats.con.consess{1}.tcon.weights = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
%      matlabbatch{3}.spm.stats.con.consess{1}.tcon.sessrep = 'replsc';
% 
%      matlabbatch{3}.spm.stats.con.consess{2}.tcon.name = 'Load PM 1';
%      matlabbatch{3}.spm.stats.con.consess{2}.tcon.weights = [0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
%      matlabbatch{3}.spm.stats.con.consess{2}.tcon.sessrep = 'replsc';
% 
% %      matlabbatch{3}.spm.stats.con.consess{3}.tcon.name = 'Load PM 2';
% %      matlabbatch{3}.spm.stats.con.consess{3}.tcon.weights = [0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
% %      matlabbatch{3}.spm.stats.con.consess{3}.tcon.sessrep = 'replsc';
% % 
% %     matlabbatch{3}.spm.stats.con.consess{3}.tcon.name = 'Dim 3';
% %     matlabbatch{3}.spm.stats.con.consess{3}.tcon.weights = [0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
% %     matlabbatch{3}.spm.stats.con.consess{3}.tcon.sessrep = 'replsc';
% %     
% %     matlabbatch{3}.spm.stats.con.consess{4}.tcon.name = 'Dim 4';
% %     matlabbatch{3}.spm.stats.con.consess{4}.tcon.weights = [0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
% %     matlabbatch{3}.spm.stats.con.consess{4}.tcon.sessrep = 'replsc';
%     
%     
%     %     matlabbatch{4}.spm.stats.results.spmmat = cfg_dep('fMRI Results Report: SPM.mat File', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','spmmat'));
%     %     matlabbatch{4}.spm.stats.results.conspec.titlestr = 'Dim 1 < Dim2-4';
%     %     matlabbatch{4}.spm.stats.results.conspec.contrasts = 5;
%     %     matlabbatch{4}.spm.stats.results.conspec.threshdesc = 'FWE';
%     %     matlabbatch{4}.spm.stats.results.conspec.thresh = 0.05;
%     %     matlabbatch{4}.spm.stats.results.conspec.extent = 20;
%     %     matlabbatch{4}.spm.stats.results.conspec.conjunction = 1;
%     %     matlabbatch{4}.spm.stats.results.conspec.mask.none = 1;
%     %     matlabbatch{4}.spm.stats.results.units = 1;
%     %     matlabbatch{4}.spm.stats.results.export{1}.pdf = true;
%     
    save([pn.batchPath, IDs{indID},'_SPM1stBatchGLM.mat'], 'matlabbatch');
    
end % subject loop

end % finish completely