mainDir = '/Path to the main analysis directory/';
dataDir = [mainDir '1_Data/'];
cd (dataDir);

subjList =  {'1', '2', '5', '6', '8', '10', '11', '12', '13', '15', '16', '18', '19', '20', '23', '24', '25', '26', '28', '30', '31', '34', '35', '36'}; % without the low accuracy ones
subjList = sort_nat(subjList); % sort_nat is a custom function that sorts numerically & alphabatically (1 comes before 10)
preprocess = 0;
delete_inter_files = 0; %delete intermediate files from preprocessing that will not be used by the first-level analysis
firstlevel = 0;
secondlevel = 1;

%% Preprocessing

if preprocess == 1
    disp('------Preprocessing started------');
    for subj = subjList
        disp(strcat('Subject:   ',subj));
        clear matlabbatch;
        cd(dataDir);
        files = [char(subj),'/f*'];
        listdir = dir(files);
        funFiles = {};
        for i=1:size(listdir)
            funFiles{1}{i} = [listdir(i).name,',1'];
        end;
        cd(char(strcat(dataDir,subj)));
        preprocessing_job(funFiles);
    end;
    disp('------Preprocessing ended------');
end;


%% Delete intermediate files
if delete_inter_files == 1
    disp('------Deleting intermediate files------');
    for subj = subjList
        clear matlabbatch;
        cd(dataDir);
        cd(char(subj));
        delete('waf*.nii');
        delete('af*.nii');
    end;
end;
        

%% First-level analysis
if firstlevel == 1
    disp('------First-level analysis started------');
    contrastTable = readtable(fullfile(mainDir,'scripts','contrasts.xlsx'));
    for subj = subjList
        disp(strcat('Subject:   ',subj));
        clear matlabbatch;
        cd (dataDir);
        files = [char(subj),'/swaf*'];
        listdir = dir(files);
        funFiles = {};
        for i=1:size(listdir)
            funFiles{i} = char(strcat(dataDir, subj, '/',listdir(i).name,',1'));
        end;   
        funFiles = funFiles'; %functional files
        cd(char(strcat(dataDir,subj)));
        
        firstlevel_job(subj,mainDir,funFiles,contrastTable);
    end;
    disp('------First-level analysis ended------');
end;


%% Second-level analysis

if secondlevel == 1
    disp('------Second-level analysis started------');
    covariateTable = readtable('covariates.xlsx','ReadRowNames',true); % this has the covariate values for each subject
    contrastTable = readtable(fullfile(mainDir,'scripts','contrasts.xlsx'));
    % Contrast output folder
    noContrasts = height(contrastTable); % number of contrasts
    disp('---Preparing contrasts---');
    for contrast = 1:noContrasts
        disp(['Contrast: ',int2str(contrast)]);
        clear matlabbatch;
        outputDir = [];
        folderName ='';
        cd (mainDir);
        cd ('3_SecondLevel'); %the directory that has all the output directories for each contrast
        folders = dir('./');
        folders = folders([folders.isdir] & ~strncmpi('.', {folders.name}, 1)); %remove folder names with '.' ('.','..','.DS_Store'). This is for MAC only
        for i=1:size(folders)
            folderName = folders(i).name;
            numbersInFolderName = regexp(folderName,'\d*','Match'); %the first number is the contrast the second (if there is) is which part of the exp
            if strcmp(char(numbersInFolderName(1)),int2str(contrast)) %each contrast folder should start with the contrast number
                outputDir = {fullfile(mainDir,'3_SecondLevel',folderName)};
%                     disp(outputFolder);
            end;
        end;

     % Contrast files
        cd(mainDir);
        cd('2_FirstLevel');
        contrastFiles = subjList; %so that we create a cell array of the same size, which is also the number of contrast images
        contrastImage = ['con_00',sprintf('%02d',contrast),'.img'];
        
        for i=1:size(subjList(:))
            contrastFiles{i} = fullfile(mainDir,'2_FirstLevel',subjList{i});
            contrastFiles{i} = [contrastFiles{i},'/',contrastImage];
        end;
        
        contrastFiles = contrastFiles';
        %disp(contrastFiles);
        
      % Covariate (finger sense scores)
        covariateList = covariateTable{subjList,1}'; %magical command that gets the covariates only for the current participants
        
        secondlevel_job(outputDir,contrastFiles,covariateList);
    end;
    disp('------Second-level analysis ended------');
end;

%% Results


cd(mainDir);

