clear; clc;

%% DEFINITIONS

cwd = '/SCR10/pauline/EmoAnt_fMRI/conn_analysis/';
subjdirs = '/SCR10/pauline/raw/';
load('/SCR10/pauline/scripts/subj_T1_trial.mat');
subj_T0_trial = strrep(subj_T1,'T1','T0');

NSUB = numel(subj_T1(:,1));
NSES = 2;
TR=2;
ROIsdir='/SCR10/pauline/EmoAnt_fMRI/conn_analysis/ROIs_Group/T0T1_GroupxTime_con0011/';
ROIs = {'rIFGorb','rIFGtri','lIN','lANG','rSMG','lSg25'};    % Define ROI names/files for every subjects
NROIs=numel(ROIs);

FUNCTIONAL_FILE = repmat({{}},[NSUB,NSES]);
STRUCTURAL_FILE = repmat({{}},[NSUB,NSES]);
GM_FILE = repmat({{}},[NSUB,NSES]);
WM_FILE = repmat({{}},[NSUB,NSES]);
CSF_FILE = repmat({{}},[NSUB,NSES]);
FIELDMAP_FILE = repmat({{}},[NSUB,NSES]);
SPM_FILE = repmat({{}},[NSUB,NSES]);
ART_FILE = repmat({{}},[NSUB,NSES]);

for sub = 1:NSUB;
    subpath = cell2mat(fullfile(subjdirs,subj_T0_trial(sub),'/EmoAntMRI_conn'));
    cd(subpath);
    FUNCTIONAL_FILE{sub}{1} = cellstr(spm_select('FPList','fMRI','^wau.*'));
    STRUCTURAL_FILE{sub}{1} = cellstr(spm_select('FPList','sMRI','^w\d.*'));
    GM_FILE{sub}{1} = cellstr(spm_select('FPList','sMRI','^wc1.*'));
    WM_FILE{sub}{1} = cellstr(spm_select('FPList','sMRI','^wc2.*'));
    CSF_FILE{sub}{1} = cellstr(spm_select('FPList','sMRI','^wc3.*'));
    FIELDMAP_FILE{sub}{1} = cellstr(spm_select('FPList','fieldmap','^vdm.*'));
    SPM_FILE{sub}{1} = char(spm_select('FPListRec','stat_fMRI','^SPM.*'));
    ART_FILE{sub}{1} = cellstr(spm_select('FPList','fMRI','^art_outliers_and_movement.*'));
end;

for sub = 1:NSUB;
    subpath = cell2mat(fullfile(subjdirs,subj_T1(sub),'/EmoAntMRI_conn'));
    cd(subpath);
    FUNCTIONAL_FILE{sub}{2} = cellstr(spm_select('FPList','fMRI','^wau.*'));
    STRUCTURAL_FILE{sub}{2} = cellstr(spm_select('FPList','sMRI','^w\d.*'));
    GM_FILE{sub}{2} = cellstr(spm_select('FPList','sMRI','^wc1.*'));
    WM_FILE{sub}{2} = cellstr(spm_select('FPList','sMRI','^wc2.*'));
    CSF_FILE{sub}{2} = cellstr(spm_select('FPList','sMRI','^wc3.*'));
    FIELDMAP_FILE{sub}{2} = cellstr(spm_select('FPList','fieldmap','^vdm.*'));
    SPM_FILE{sub}{2} = char(spm_select('FPListRec','stat_fMRI','^SPM.*'));
    ART_FILE{sub}{2} = cellstr(spm_select('FPList','fMRI','^art_outliers_and_movement.*'));
end;

cd(cwd);


%% CONN-SPECIFIC SECTION: RUNS PREPROCESSING/SETUP/DENOISING/ANALYSIS STEPS
%% Prepares batch structure
clear batch;
batch.filename=fullfile(cwd,'conn_batch_T0T1_trial.mat');            % New conn_*.mat experiment name
%batch.parallel.N=NSUBJECTS;                                  % One process per subject (uses default Grid-settings profile)
%batch.parallel.profile='LSF computer cluster'; 

%% SETUP & PREPROCESSING step (using default values for most parameters, see help conn_batch to define non-default values)
% CONN Setup                                           % Default options (uses all ROIs in conn/rois/ directory); see conn_batch for additional options 
% CONN Setup.preprocessing                               (realignment/coregistration/segmentation/normalization/smoothing)
batch.Setup.isnew=1;
batch.Setup.nsubjects=NSUB;
batch.Setup.RT=TR;                                     % TR (seconds)
batch.Setup.voxelresolution=3;                         % Same as functional
batch.Setup.analyses=[1,2];                            % ROI-to-ROI + Seed-to-voxel

batch.Setup.functionals=repmat({{}},[NSUB,NSES]);    % Point to functional volumes for each subject/session
batch.Setup.structurals=repmat({{}},[NSUB,NSES]);
batch.Setup.structurals=repmat({{}},[NSUB,NSES]);
batch.Setup.masks.Grey=repmat({{}},[NSUB,NSES]);
batch.Setup.masks.White=repmat({{}},[NSUB,NSES]);
batch.Setup.masks.CSF=repmat({{}},[NSUB,NSES]);
batch.Setup.spmfiles=repmat({{}},[NSUB,NSES]);
batch.Setup.covariates.files{1}=repmat({{}},[NSUB,NSES]);
batch.Setup.covariates.names={'art_outliers_and_movements'};

for nsub=1:NSUB,
    for nses=1:NSES
        batch.Setup.functionals{nsub}{nses}=FUNCTIONAL_FILE{nsub}{nses};
        batch.Setup.structurals{nsub}{nses}=STRUCTURAL_FILE{nsub}{nses};
        batch.Setup.masks.Grey{nsub}{nses}=GM_FILE{nsub}{nses};
        batch.Setup.masks.White{nsub}{nses}=WM_FILE{nsub}{nses};
        batch.Setup.masks.CSF{nsub}{nses}=CSF_FILE{nsub}{nses};
        batch.Setup.spmfiles{nsub}{nses}=SPM_FILE{nsub}{nses};
        batch.Setup.covariates.files{1}{nsub}{nses}=ART_FILE{nsub}{nses};
    end
end;

batch.Setup.rois.names=repmat({{}},[NROIs,1]);
batch.Setup.rois.files=repmat({{}},[NROIs,1]);
for nroi = 1:NROIs, 
    batch.Setup.rois.names{nroi}=ROIs{nroi}; 
    batch.Setup.rois.files{nroi}=strcat(ROIsdir,'0004_F_Group_x_Time_',ROIs{nroi},'.nii,1');
end; 

conn_importspm('', 'breakconditionsbysession',true);

%batch.Setup.done=1;
batch.Setup.overwrite='Yes';                            

% uncomment the following 3 lines if you prefer to run one step at a time:
% conn_batch(batch); % runs Preprocessing and Setup steps only
% clear batch;
% batch.filename=fullfile(cwd,'conn_NYU.mat');            % Existing conn_*.mat experiment name

%% DENOISING step
% CONN Denoising                                    % Default options (uses White Matter+CSF+realignment+scrubbing+conditions as confound regressors); see conn_batch for additional options 
batch.Denoising.filter=[0.01, inf];                 % frequency filter (band-pass values, in Hz)
batch.Denoising.done=1;
batch.Denoising.overwrite='Yes';

% uncomment the following 3 lines if you prefer to run one step at a time:
% conn_batch(batch); % runs Denoising step only
% clear batch;
% batch.filename=fullfile(cwd,'conn_NYU.mat');            % Existing conn_*.mat experiment name

%% FIRST-LEVEL ANALYSIS step
% CONN Analysis                                     % Default options (uses all ROIs in conn/rois/ as connectivity sources); see conn_batch for additional options 
batch.Analysis.measure=3;                           % regression (bivariate)
batch.Analysis.modulation=1;                        % gPPI
batch.Analysis.done=1;
batch.Analysis.overwrite='Yes';

%% Run all analyses
conn_batch(batch);

%% CONN Display
% launches conn gui to explore results
conn
conn('load',fullfile(cwd,'conn_batch_T0T1_trial.mat'));
conn gui_results
