% Exercise: run the example project using a batch script

% where this script is located
codeDir     = pwd;
bidsDir     = fullfile(codeDir, "ds004134");

%% Filename and parallel processing options

BATCH.filename = fullfile(codeDir, 'conn_projects', 'conn_script.mat');

BATCH.parallel.N       = 35;
BATCH.parallel.profile = 'Grid Engine computer cluster';

%% Setup

% We first need to setup a conn project

BATCH.Setup.isnew     = 1; % create a new conn project or use an existing one? 1=create new, 0=use existing
BATCH.Setup.done      = 1; % run SETUP procesing steps 0=just fills in the, 1=yes
BATCH.Setup.overwrite = 1; % overwrite file contained within the conn_projects/conn_tutorial directory? 1=yes,0=no
BATCH.Setup.add       = 0;

% how many subjects?
subjects              = spm_select('List', bidsDir, 'dir', 'sub-.*');
subjects              = cellstr(subjects);

% remove subject 1 in this example. something is wrong with their data.
subjects              = subjects(2:length(subjects));


% how many sessions?
sessions              = {'ses-01'};

BATCH.Setup.nsubjects = length(subjects); 

BATCH.Setup.RT              = NaN; % NaN
BATCH.Setup.acquisitiontype = 1; % 1=continuous

% ------------------------- functionals --------------------------------- %

% loop over subjects
for isub = 1:length(subjects)

    % this subject's directory
    csubDir        = fullfile(bidsDir, subjects{isub});

    % find all BOLD scans
    scans           = spm_select('FPListRec', csubDir, 'sub.*_bold\.nii\.gz');
    scans           = cellstr(scans);

    % add these scans to the proper field within the BATCH structure
    for iscan = 1:length(scans)

        BATCH.Setup.functionals{isub}{iscan} = scans{iscan};

    end

end

% ------------------------- anatomicals --------------------------------- %

% loop over subjects
for isub = 1:length(subjects)

    % this subject's directory
    csubDir = fullfile(bidsDir, subjects{isub});

    scan     = spm_select('FPListRec', csubDir, '^sub.*_T1w\.nii.gz$');

    BATCH.Setup.structurals{isub} = scan;

end

% ------------------------- conditions ---------------------------------- %
% conditions

BATCH.Setup.conditions.missingdata        = 1;
for isub = 1:length(subjects)

    % this subject's directory
    csubDir = fullfile(bidsDir, subjects{isub});

    eventsFile           = spm_select('FPListRec', csubDir, 'sub.*_events.tsv');
    eventsFile           = cellstr(eventsFile);

    BATCH.Setup.conditions.importfile{isub}{1} = eventsFile{1};

end
BATCH.Setup.conditions.add                = 0;

% ------------------------- MISC ---------------------------------------- %

BATCH.Setup.analyses                 = 1;
BATCH.Setup.analysisunits            = 2;
BATCH.Setup.outputfiles              = [0 0 0 0 0 0];

conn_batch(BATCH);
