% Resting state conn ROI-to-ROI Analysis
% --------------------------------------------------------
clear; clc;

script_dir = fileparts(mfilename('fullpath'));
config = readdictionary(fullfile(script_dir, '..', '..', 'paths.json'));

% Path to SPM12 toolbox - for preprocessing
addpath(genpath(config{'programs'}('spm_path')));

% path to CONN toolbox - for denoising
addpath(genpath(config{'programs'}('conn_path')));

subjects = config{'subjects'}(1:end);

rois_dir = values(config{'rois'});

func_imgs = cellstr(config{'data'}{'preprocessed_func'}{'swur_func_path'}(subjects));
anat_imgs = cellstr(config{'data'}{'raw'}{'anat_t1_path'}(subjects));

grey_imgs = cellstr(config{'data'}{'preprocessed_anat'}{'wc1_anat_t1_path'}(subjects));
white_imgs = cellstr(config{'data'}{'preprocessed_anat'}{'wc2_anat_t1_path'}(subjects));
csf_imgs = cellstr(config{'data'}{'preprocessed_anat'}{'wc3_anat_t1_path'}(subjects));

outlier_file = cellstr(config{'data'}{'outliers'}{'outliers_mat_path'}(subjects));
motion_file = cellstr(config{'data'}{'outliers'}{'mvt_txt_path'}(subjects));


TR = 0.7; % TR of the data in s
nsub = length(func_imgs);

batch_dir = fullfile(char(config{'dir'}('results_dir')), 'RRC', 'RRC_CONN_test.mat');

% Define batch properies 

BATCH.filename = batch_dir;
BATCH.Setup.done = 1;
BATCH.Setup.isnew=1; % 1=new CONN project
BATCH.Setup.RT = TR;
BATCH.Setup.nsubjects = nsub;
BATCH.Setup.acquisitiontype = 1; % continous (default value)
BATCH.Setup.outputfiles = [1,1,0,0,0,1]; % write nii volumes for confound p-maps, confound-corrected timeseries, r-maps, p-maps and FDR-p-maps
BATCH.Setup.overwrite=0;


BATCH.Setup.structurals = anat_imgs;
BATCH.Setup.functionals = func_imgs;
BATCH.Setup.masks.White = white_imgs;
BATCH.Setup.masks.CSF = csf_imgs;



%create the unsmoothed volumes
BATCH.Setup.secondarydatasets{1}.functionals_type = 2; % 1-primary dataset, 2-remove 's' from filename, 3-alternative filename-change rule, 4-explicitely specify
BATCH.Setup.secondarydatasets{1}.functionals_label = 'unsmoothed volumes';




% FC analysis
BATCH.Setup.rois.names = {
    'vPMC_lh', 'vPMC_rh', ...
    'pMTG_lh', 'pMTG_rh', ...
    'IFG_lh', 'IFG_rh', ...
    };
noROIs = length(BATCH.Setup.rois.names);

for iroi = 1:noROIs
    BATCH.Setup.rois.files{iroi}{1}   = fullfile(rois_dir{iroi});
    display(BATCH.Setup.rois.files{iroi}{1})
    BATCH.Setup.rois.dimensions{iroi} = 1; 
end


BATCH.Setup.rois.multiplelabels    = zeros(1, noROIs);
BATCH.Setup.rois.regresscovariates = zeros(1, noROIs);
BATCH.Setup.rois.mask              = zeros(1, noROIs);
BATCH.Setup.rois.dataset           = ones(1, noROIs)*1; % pointing to unsmoothed MNI volumes (0 for primary dataset)
BATCH.Setup.rois.add               = 0; % take ony the rois that I give (not AAL atlas)

% Covariates 1st-Level
BATCH.Setup.covariates.names={'realignment', 'scrubbing'};
BATCH.Setup.covariates.files={motion_file, outlier_file};



BATCH.preprocessing.steps = {'functional_roiextract'} % multiple ROIs, the intersection of all ROIs will be used as mask
Setup.preprocessing.roi_names = {BATCH.Setup.rois.names}
BATCH.preprocessing.overwrite = 0;
BATCH.preprocessing.done = 1;


BATCH.Denoising.filter = [0.008 0.09];
BATCH.Denoising.confounds.names = {'realignment','scrubbing','White Matter','CSF'};
BATCH.Denoising.confounds.dimensions = {inf, inf, 5, 5};
BATCH.Denoising.confounds.deriv = {1, 0, 0, 0};
BATCH.Denoising.overwrite = 0; 
BATCH.Denoising.done = 1;

BATCH.conditions.names = {'rest'};
BATCH.conditions.onsets{1}{1} = [0];
BATCH.conditions.durations{1}{1} = [inf];

BATCH.Setup.analyses = [1]; % Vector of index to analysis types (1: ROI-to-ROI; 2: Seed-to-voxel; 3: Voxel-to-voxel; 4: Dynamic FC
% BATCH.analysisunits = 1; % PSC units (percent signal change) -2 for raw units

BATCH.Analysis.measure = 1; % connectivity measure used, 1 = 'correlation (bivariate)', 2 = 'correlation (semipartial)', 3 = 'regression (bivariate)', 4 = 'regression (multivariate)'
BATCH.Analysis.weight = 'hrf'; % 1 = 'none', 2 = 'hrf', 3 = 'hanning' (2-default)
BATCH.Analysis.modulation = 0; % standard weighted GLM analyses
BATCH.Analysis.type = 1; % 1 = 'ROI-to-ROI', 2 = 'Seed-to-Voxel', 3 = 'all'
% BATCH.Analysis.sources = {BATCH.Setup.rois.names};
BATCH.Analysis.analysis_number = 'RRC-01';
BATCH.Analysis.overwrite = 0;
BATCH.Analysis.done = 1;


conn_batch(BATCH);    




