%SCR PIPELINE 2/3
%This script is formated to run from desktop, not HPC

%BELOW, YOU WILL HAVE TO SPECIFY AS DATA. AFTER AUTONOMATE IT WAS SAVED HERE:
%P:\3013074.03\Test_data\physiological_data\brainAmp\SCR\brainampconvertertest

%02-05-2019 MCH

%NOTE!: run this script outside the cluster, otherwise the writing to excel
%won't work

%still add: 
% - that it can run for the other tasks as well
% - that it can read in the CSp
% - that it can read the autonomate files with manually adjusted values

%% Specify file you want to analyze and indicate which session and run

session = 3;
run = 2;
subject = [21];
filepath = '/project/3013067.03/Remote_Fear_Memory/preprocessed_physiodata/SCR/'    


for subjects = subject
    data = tdfread(fullfile([filepath 'SCR_',num2str(subjects,'%02.f'),'0' num2str(session) '0' num2str(run) '.txt']),'tab')
    
    %check if there are files for the different conditions with adjusted values
    triggers = [11, 12, 21, 22, 44];
    for trigger = triggers
        if isfile(fullfile([filepath '/SCR_',num2str(subjects,'%02.f'),'0' num2str(session) '0' num2str(run) '_' num2str(trigger) '.txt']))
            new_data = tdfread(fullfile([filepath '/SCR_',num2str(subjects,'%02.f'),'0' num2str(session) '0' num2str(run) '_' num2str(trigger) '.txt']), 'tab')
            new_data_index = 0 
            for i = 1:size(data.Condition,1)
                if data.Condition(i) == trigger
                    new_data_index = new_data_index + 1
                    data.t2pValue(i) = new_data.Response(new_data_index)
                end
            end
        end
    end

    %% Identify session
    if(session ==1)
        if(run == 1)
            SesName = 'Pre_resting';
        elseif(run == 5)
            SesName = 'Post_resting';
        elseif(run == 3)
            SesName = 'learning';
        end
    elseif(session ==2)
        if(run == 2)
            SesName = 'extinction';
        end
    elseif(session ==3)
        if(run == 2)
            SesName = 'reinstatement';
        end
    end


    %% Conditioning: Sort file: find subjects, conditions and codes

        overview = []; % will contain subID, Condition, startindex and endindex
        [r,c] = size(data.Condition);
        old_subID = 0;
        old_condition = 0;
        old_event = 0;
        %Loop to identify beginning of new subject or condition, and add sub id,
        %condition ID and start index.
        for i = 1:r
    %         new_ssubID = data.File(i,5:6);
    %         new_subID = str2num(new_ssubID);
            new_condition = data.Condition(i);
            new_event = data.Event(i);
            if(new_condition ~= old_condition)
                overview = [overview; new_condition i];
            end
    %         old_ssubID = data.File(i,5:6);
    %         old_subID = str2num(old_ssubID);
            old_condition = data.Condition(i);
            old_event = data.Event(i);
        end
        %Now loop again to add end index
        [r2,c2] = size(overview);
        for i = 1:r2-1
            overview(i,3) = (overview(i+1,2)-1);
        end
        overview(end,3)=r;

        %% Sort for each subject

        for iSub = [subjects']

            SavePath = ['/home/memory/jetdvos/Documents/data/test/results.mat'];
            try
                t2p_results = load(SavePath);
            catch
                warning(['Creating new subject results file for sub ' num2str(iSub)]);
            end
    %         InfoFile = ['P:\3013074.03\Test_data\matlab_data\sub' num2str(iSub) '\general.mat'];
    %         %Find out CS+ and CS- codes
    %         Info = load(InfoFile);
    %         if Info.CSplus == 1
    %             CSp_marker = 4;
    %             CSm_marker = 5;
    %         else
    %             CSp_marker = 5;
    %             CSm_marker = 4;
    %         end
            rundir = fullfile(['/project/3013067.03/Remote_Fear_Memory/behavioral_logs/sub', num2str(subjects,'%02.f'),'/sub' , num2str(subjects,'%02.f'),'_session' , num2str(session), '_' , SesName]);
            fid = fopen(fullfile([rundir, '/',num2str(subjects,'%02.f') , num2str(session,'%02.f'), num2str(run,'%02.f'),'_FA_Session1_all_sequence.txt']));
            tline = fgetl(fid);
            shockroom = str2double(tline(end));
            %room_1 = [1 2 3 5 7 11 13 15 17 19 21 23 25 27 29 31 35];
            %room_2 = [4 6 8 9 10 12 14 16 18 20 22 24 26 28 30 32 34];
            if shockroom == 1
                CSp_marker = [11 12];
                CSm_marker = [21 22];
            elseif shockroom == 2
                CSp_marker = [21 22];
                CSm_marker = [11 12];
            end
            %Check the autonomate text file to detect which markers were followed
            %by shocks.
            %Change this to the code in create_batch_list to find the file
            %automatically
    %         sesNo = session;
    %         runNo = run;
            subNo =iSub;
            noResting = [1 10];
            rundir = fullfile(['/project/3013067.03/Remote_Fear_Memory/behavioral_logs/sub', num2str(subjects,'%02.f'),'/sub' , num2str(subjects,'%02.f'),'_session' , num2str(session), '_' , SesName]);
            if (ismember(subjects,noResting) && session == 1) || session == 2
                fullfilename = dir([rundir, '/','*','run_1','*','.txt']);
            elseif session == 1
                fullfilename = dir([rundir, '/','*','run_2','*','.txt']);
            elseif session == 3
                fullfilename = dir([rundir, '/','*','full','*','.txt']);
            end
            ffn = [fullfilename.folder '/' fullfilename.name];
    %         if subNo == 1
    %             if(sesNo == 1)
    %                 rundir = fullfile(['P:/3013074.03/Test_data/physiological_data/brainAmp/sub_0', num2str(subNo),'/']);
    %                 fullfilename = dir([rundir, '*','loc','*', 'run_',num2str(runNo),'*','.txt']);
    %                 ffn = [fullfilename.folder '\' fullfilename.name];
    %             elseif(sesNo ==2)
    %                 rundir = fullfile(['P:/3013074.03/Test_data/physiological_data/brainAmp/sub_0', num2str(subNo),'/']);
    %                 fullfilename = dir([rundir, '*', 'ses',num2str(sesNo), '*', 'run_',num2str(runNo),'*','.txt']);
    %                 ffn = [fullfilename.folder '\' fullfilename.name];
    %             end
    %         elseif subNo < 10
    %             if(subNo == 8)&&(sesNo ==2)
    %                 %skipthis
    %             else
    %                 rundir = fullfile(['P:/3013074.03/Test_data/physiological_data/brainAmp/sub_0', num2str(subNo),'/']);
    %                 fullfilename = dir([rundir, '*', 'ses',num2str(sesNo), '*', 'run_',num2str(runNo),'*','.txt']);
    %                 ffn = [fullfilename.folder '\' fullfilename.name];
    %             end
    %             
    %         else
    %             rundir = fullfile(['P:/3013074.03/Test_data/physiological_data/brainAmp/sub_', num2str(subNo),'/']);
    %             fullfilename = dir([rundir, '*', 'ses',num2str(sesNo), '*', 'run_',num2str(runNo),'*','.txt']);
    %             ffn = [fullfilename.folder '\' fullfilename.name];
    %         end

            raw = tdfread(ffn,'tab');
            names = fieldnames(raw);
            name = names{1,1};
            rawlist = getfield(raw,name);
            [~,~,markers] = find(rawlist(:,2));
            shocks = any(markers == 44,2);
            shockIs = find(shocks);
            CSP_shock_index=[];
            for index = [shockIs']
                if ismember(markers(index-1),CSp_marker)
                    CSP_shock_index =[CSP_shock_index; index-1];
                end
            end
            CPsfind = any(markers == CSp_marker,2);
            CPs = find(CPsfind);
            NoShockCSp = setdiff(CPs,CSP_shock_index);
            [~,AnalyzableNum] = ismember(NoShockCSp,CPs);
            %This works

            [subIndex] = any(overview);
            [subindices,~,~] = find(subIndex);
            for j = 1:size(overview,1)
                if (overview(j,1) == CSp_marker(1))
                    startindex = overview(j,2);
                    endindex = overview(j+1,3);
                    allpoints = data.t2pValue(startindex:endindex);
                    noshockpoints = allpoints(AnalyzableNum);
                    t2p_results.(SesName).CSp = noshockpoints;
                    t2p_results.(SesName).CSpAllTrials = data.t2pValue(startindex:endindex);
                    t2p_results.(SesName).CSpNoShockTrialNums = AnalyzableNum;
                elseif(overview(j,1) == CSm_marker(1))
                    startindex = overview(j,2);
                    endindex = overview(j+1,3);
                    t2p_results.(SesName).CSm = data.t2pValue(startindex:endindex);
                elseif(overview(j,1) == 44)
                    startindex = overview(j,2);
                    endindex = overview(j,3);
                    t2p_results.(SesName).shock = data.t2pValue(startindex:endindex);
                end
            end
            save(SavePath,'-struct','t2p_results');

        end


    %source_dir =('/home/memory/jetdvos/Documents/data/test/');
    %source_dir =('M:/Documents/data/test/');
    %addpath(source_dir);
    if exist(fullfile([rundir, '/', num2str(subjects,'%02.f') , num2str(session,'%02.f'), num2str(run,'%02.f'), '-donders_learning_', 'C.txt']), 'file')
        group = 1;
    elseif exist(fullfile([rundir, '/', num2str(subjects,'%02.f') , num2str(session,'%02.f'), num2str(run,'%02.f'), '-donders_learning_', 'R.txt']), 'file')
        group = 2;
    end
    %recent_group = [1 3 5 7 8 11 17 18 19 22 25 28 30 34];
    %remote_group = [2 4 6 9 10 12 13 14 15 16 20 21 23 24 26 27 29 31 32 35];
    %if ismember(subjects,recent_group)
    %    group = 1;
    %elseif ismember(subjects,remote_group)
    %    group = 2;
    %end
    xlRange = iSub + 1;
    xlRange = num2str(xlRange);

    if session == 1
        mean_CSp = mean(t2p_results.learning.CSp);
        mean_CSm = mean(t2p_results.learning.CSm);
        mean_CSp_All = mean(t2p_results.learning.CSpAllTrials);
        mean_shock = mean(t2p_results.learning.shock);
        mean_CSpE = mean(t2p_results.learning.CSp(1:end/2));
        mean_CSpL = mean(t2p_results.learning.CSp(end/2:end));
        mean_CSmE = mean(t2p_results.learning.CSm(1:end/2));
        mean_CSmL = mean(t2p_results.learning.CSm(end/2:end));
        filename = 'P:/3013067.03/Remote_Fear_Memory/preprocessed_physiodata/SCR/SCR_session1.xlsx';
        sheet = 1;
        xlswrite(filename,[iSub, group,mean_CSp, mean_CSm, mean_CSp_All, mean_CSpE, mean_CSpL, mean_CSmE, mean_CSmL, mean_shock],sheet,xlRange);
        sheet = 2;
        xlswrite(filename,[iSub, group,t2p_results.learning.CSp'],sheet,xlRange);
        sheet = 3;
        xlswrite(filename,[iSub, group,t2p_results.learning.CSm'],sheet,xlRange);
        sheet = 4;
        xlswrite(filename,[iSub, group,t2p_results.learning.CSpAllTrials'],sheet,xlRange);
        sheet = 5;
        xlswrite(filename,[iSub, group,t2p_results.learning.shock'],sheet,xlRange);
    elseif session == 2
        mean_CSp = mean(t2p_results.extinction.CSp);
        mean_CSm = mean(t2p_results.extinction.CSm);
        mean_CSpE = mean(t2p_results.extinction.CSp(1:end/2));
        mean_CSpL = mean(t2p_results.extinction.CSp(end/2:end));
        mean_CSmE = mean(t2p_results.extinction.CSm(1:end/2));
        mean_CSmL = mean(t2p_results.extinction.CSm(end/2:end));
        CSp_recall = mean(t2p_results.extinction.CSp(1:2));
        CSm_recall = mean(t2p_results.extinction.CSm(1:2));
        CSp_last2 = mean(t2p_results.extinction.CSp(end-2:end));
        CSm_last2 = mean(t2p_results.extinction.CSm(end-2:end));
        filename = 'P:/3013067.03/Remote_Fear_Memory/preprocessed_physiodata/SCR/test_SCR_session2.xlsx';
        sheet = 1;
        xlswrite(filename,[iSub, group,mean_CSp, mean_CSm, CSp_recall, CSm_recall, mean_CSpE, mean_CSpL, mean_CSmE, mean_CSmL, CSp_last2, CSm_last2],sheet,xlRange);
        sheet = 2;
        xlswrite(filename,[iSub, group,t2p_results.extinction.CSp'],sheet,xlRange);
        sheet = 3;
        xlswrite(filename,[iSub, group,t2p_results.extinction.CSm'],sheet,xlRange);
    elseif session == 3
        mean_CSp = mean(t2p_results.reinstatement.CSp);
        mean_CSm = mean(t2p_results.reinstatement.CSm);
        mean_CSpE = mean(t2p_results.reinstatement.CSp(1:end/2));
        mean_CSpL = mean(t2p_results.reinstatement.CSp(end/2:end));
        mean_CSmE = mean(t2p_results.reinstatement.CSm(1:end/2));
        mean_CSmL = mean(t2p_results.reinstatement.CSm(end/2:end));
        filename = 'P:/3013067.03/Remote_Fear_Memory/preprocessed_physiodata/SCR/test_SCR_session3.xlsx';
        sheet = 1;
        xlswrite(filename,sheet,xlRange);
        sheet = 2;
        xlswrite(filename,sheet,xlRange);
        sheet = 3;
        xlswrite(filename,[iSub, group,t2p_results.reinstatement.CSm'],sheet,xlRange);
    end
end









