% script to compute figures of analyzed EMG data
% Open matfile which contains a struct all with timecourse data as well as
% condition info. Likeability 1=positive, -1=negative. EventValValence 1=positive,
% -1=negative, EventValReason
% Marijn Struiksma February 2016

%% Required directories

% ==> Name your analysis directory where the results are stored:
%sjCode	SjNr	markertrial	incCorr_mean	incZygo_mean	likeConditie_mean	eventValence_mean	ZLkblCorrPctTrial_mean	ZAfctCorrPctTrial_mean	ZLkblZygoPctTrial_mean	ZAfctZygoPctTrial_mean
wdir = 'M:\jvbproject\Björn\ace-brat3\Analysis\Matlab\results\';
% load z-scores file
fileZ = dir([wdir, 'acebrat3_zscores.txt']);
zscores=load([wdir,fileZ.name]); %loads struct all that contains the segments data: struct segment
% zscores contains the following columns:
% [SjNr	list	incCorr	incZygo	markertrial	likeConditie	EventValValenceConditie	ZLikeCorrPrTrial	ZLikeZygoPrTrial
% ZNameCorrPrTrial	ZNameZygoPrTrial	ZEventValValenceCorrPrTrial	ZEventValValenceZygoPrTrial	ZEventValReasonCorrPrTrial	ZEventValReasonZygoPrTrial]
% [24	2	1	1	39	1	1	-0.00323	-0.32239	0.27376	-0.36402	0.1613	-0.37435	0.46955	-0.35419]
% Trials are included for -1.98 =< z <= 1.98

% load matfile with segement results
%file = dir([wdir, 'timecourse2sjs.mat']);
file = dir([wdir, 'ACE-BRaT2_timecourse_60sjs_26-Feb-2016.mat']);
load([wdir,file.name]); %loads struct all that contains the segments data: struct segment
% segment=segment(1:2); % only necessary for debugging. put in comment for final analysis

% dir where figures should be stored
fdir = 'M:\jvbproject\Björn\ACE-BRaT2\Analysis\Matlab\figures\';
if (exist(fdir)~=7)
    mkdir(fdir);
end




%% compute mean timecourse per participant per condition
dims=size(segment);
nrsjs=dims(2);
nrtrials=length(segment(1).bin100ms);

AllLikePosCorr=[];
AllLikeNegCorr=[];
AllLikePosEventValPosCorr=[];
AllLikePosEventValNegCorr=[];
AllLikeNegEventValPosCorr=[];
AllLikeNegEventValNegCorr=[];

AllLikePosZygo=[];
AllLikeNegZygo=[];
AllLikePosEventValPosZygo=[];
AllLikePosEventValNegZygo=[];
AllLikeNegEventValPosZygo=[];
AllLikeNegEventValNegZygo=[];
meanInclusionPr=[];
ppdetails=[];
counter=1;
for iSjs = 1:nrsjs
    %disp('sjnr:');
    %disp(iSjs);
    %disp('trialnr:');
    LikePosCorr=[];
    LikeNegCorr=[];
    LikePosEventValPosCorr=[];
    LikePosEventValNegCorr=[];
    LikeNegEventValPosCorr=[];
    LikeNegEventValNegCorr=[];
    
    LikePosZygo=[];
    LikeNegZygo=[];
    LikePosEventValPosZygo=[];
    LikePosEventValNegZygo=[];
    LikeNegEventValPosZygo=[];
    LikeNegEventValNegZygo=[];
    inclusionPr=[];
    for iTrials = 1:nrtrials
        %disp(iTrials);
        %make sure the correct zscore info is used
        if ~zscores(counter,1)==iSjs
            disp('sj does not match');
            disp(iSjs);
        end
        if ~zscores(counter,2)==iTrials
            disp('trial does not match');
            disp(iTrials);
        end
        % should trial be included? based on baseline
        inclusion=(segment(iSjs).incCorr(iTrials))*(segment(iSjs).incZygo(iTrials));
        %disp(inclusion)
        % should trial be included? based on zscore
        % [SjNr	list	incCorr	incZygo	markertrial	likeConditie	EventValValenceConditie	ZLikeCorrPrTrial	ZLikeZygoPrTrial
% ZNameCorrPrTrial	ZNameZygoPrTrial	ZEventValValenceCorrPrTrial	ZEventValValenceZygoPrTrial	ZEventValReasonCorrPrTrial	ZEventValReasonZygoPrTrial]
        PzscoresCorrLike=abs(zscores(counter,7));
        inclusionCorrLike=(max(PzscoresCorrLike)<=1.96)*inclusion;
        PzscoresZygoLike=abs(zscores(counter,8));
        inclusionZygoLike=(max(PzscoresZygoLike)<=1.96)*inclusion;
        PzscoresCorrEventVal=abs(zscores(counter,11));
        inclusionCorrEventVal=(max(PzscoresCorrEventVal)<=1.96)*inclusion;
        PzscoresZygoEventVal=abs(zscores(counter,12));
        inclusionZygoEventVal=(max(PzscoresZygoEventVal)<=1.96)*inclusion;
        
        % Likeability condition
        condLike=segment(iSjs).likeConditie(iTrials);
        % EventVal condition
        condEventVal=segment(iSjs).eventValence(iTrials);
        
        % Collect timecourse data
        if inclusionCorrLike==1
            switch condLike
                case 1 %positive
                    LikePosCorr=vertcat(LikePosCorr,segment(iSjs).bin100ms(iTrials).CorrPr);
                case -1 %negative
                    LikeNegCorr=vertcat(LikeNegCorr,segment(iSjs).bin100ms(iTrials).CorrPr);
            end
        end
        
        if inclusionZygoLike==1
            switch condLike
                case 1 %positive
                    LikePosZygo=vertcat(LikePosZygo,segment(iSjs).bin100ms(iTrials).ZygoPr);
                case -1 %negative
                    LikeNegZygo=vertcat(LikeNegZygo,segment(iSjs).bin100ms(iTrials).ZygoPr);
            end
        end
        
        if inclusionCorrEventVal==1
            switch condLike
                case 1 %positive
                    switch condEventVal
                        case 1 %positive
                            LikePosEventValPosCorr=vertcat(LikePosEventValPosCorr,segment(iSjs).bin100ms(iTrials).CorrPr);
                        case -1 %negative
                            LikePosEventValNegCorr=vertcat(LikePosEventValNegCorr,segment(iSjs).bin100ms(iTrials).CorrPr);
                    end
                case -1 %negative
                    switch condEventVal
                        case 1 %positive
                            LikeNegEventValPosCorr=vertcat(LikeNegEventValPosCorr,segment(iSjs).bin100ms(iTrials).CorrPr);
                        case -1 %negative
                            LikeNegEventValNegCorr=vertcat(LikeNegEventValNegCorr,segment(iSjs).bin100ms(iTrials).CorrPr);
                    end
            end
        end
        
        if inclusionZygoEventVal==1
            switch condLike
                case 1 %positive
                    switch condEventVal
                        case 1 %positive
                            LikePosEventValPosZygo=vertcat(LikePosEventValPosZygo,segment(iSjs).bin100ms(iTrials).ZygoPr);
                        case -1 %negative
                            LikePosEventValNegZygo=vertcat(LikePosEventValNegZygo,segment(iSjs).bin100ms(iTrials).ZygoPr);
                    end
                case -1 %negative
                    switch condEventVal
                        case 1 %positive
                            LikeNegEventValPosZygo=vertcat(LikeNegEventValPosZygo,segment(iSjs).bin100ms(iTrials).ZygoPr);
                        case -1 %negative
                            LikeNegEventValNegZygo=vertcat(LikeNegEventValNegZygo,segment(iSjs).bin100ms(iTrials).ZygoPr);
                    end
            end
        end
        
        counter=counter+1;
    end %iTrials
    
    % compute means per participant
    AllLikePosCorr=vertcat(AllLikePosCorr,mean(LikePosCorr));
    AllLikeNegCorr=vertcat(AllLikeNegCorr,mean(LikeNegCorr));
    AllLikePosEventValPosCorr=vertcat(AllLikePosEventValPosCorr,mean(LikePosEventValPosCorr));
    AllLikePosEventValNegCorr=vertcat(AllLikePosEventValNegCorr,mean(LikePosEventValNegCorr));
    AllLikeNegEventValPosCorr=vertcat(AllLikeNegEventValPosCorr,mean(LikeNegEventValPosCorr));
    AllLikeNegEventValNegCorr=vertcat(AllLikeNegEventValNegCorr,mean(LikeNegEventValNegCorr));
    
    AllLikePosZygo=vertcat(AllLikePosZygo,mean(LikePosZygo));
    AllLikeNegZygo=vertcat(AllLikeNegZygo,mean(LikeNegZygo));
    AllLikePosEventValPosZygo=vertcat(AllLikePosEventValPosZygo,mean(LikePosEventValPosZygo));
    AllLikePosEventValNegZygo=vertcat(AllLikePosEventValNegZygo,mean(LikePosEventValNegZygo));
    AllLikeNegEventValPosZygo=vertcat(AllLikeNegEventValPosZygo,mean(LikeNegEventValPosZygo));
    AllLikeNegEventValNegZygo=vertcat(AllLikeNegEventValNegZygo,mean(LikeNegEventValNegZygo));
end %iSjs

%% compute mean timecourse and standard deviation per condition
MeanLikePosCorr=[];
MeanLikeNegCorr=[];
MeanLikePosEventValPosCorr=[];
MeanLikePosEventValNegCorr=[];
MeanLikeNegEventValPosCorr=[];
MeanLikeNegEventValNegCorr=[];
StdLikePosCorr=[];
StdLikeNegCorr=[];
StdLikePosEventValPosCorr=[];
StdLikePosEventValNegCorr=[];
StdLikeNegEventValPosCorr=[];
StdLikeNegEventValNegCorr=[];

MeanLikePosZygo=[];
MeanLikeNegZygo=[];
MeanLikePosEventValPosZygo=[];
MeanikePosEventValNegZygo=[];
MeanLikeNegEventValPosZygo=[];
MeanLikeNegEventValNegZygo=[];
StdLikePosZygo=[];
StdLikeNegZygo=[];
StdLikePosEventValPosZygo=[];
StdikePosEventValNegZygo=[];
StdLikeNegEventValPosZygo=[];
StdLikeNegEventValNegZygo=[];

MeanLikePosCorr=mean(AllLikePosCorr);
MeanLikeNegCorr=mean(AllLikeNegCorr);
MeanLikePosEventValPosCorr=mean(AllLikePosEventValPosCorr);
MeanLikePosEventValNegCorr=mean(AllLikePosEventValNegCorr);
MeanLikeNegEventValPosCorr=mean(AllLikeNegEventValPosCorr);
MeanLikeNegEventValNegCorr=mean(AllLikeNegEventValNegCorr);
StdLikePosCorr=std(AllLikePosCorr);
StdLikeNegCorr=std(AllLikeNegCorr);
StdLikePosEventValPosCorr=std(AllLikePosEventValPosCorr);
StdLikePosEventValNegCorr=std(AllLikePosEventValNegCorr);
StdLikeNegEventValPosCorr=std(AllLikeNegEventValPosCorr);
StdLikeNegEventValNegCorr=std(AllLikeNegEventValNegCorr);

MeanLikePosZygo=mean(AllLikePosZygo);
MeanLikeNegZygo=mean(AllLikeNegZygo);
MeanLikePosEventValPosZygo=mean(AllLikePosEventValPosZygo);
MeanLikePosEventValNegZygo=mean(AllLikePosEventValNegZygo);
MeanLikeNegEventValPosZygo=mean(AllLikeNegEventValPosZygo);
MeanLikeNegEventValNegZygo=mean(AllLikeNegEventValNegZygo);
StdLikePosZygo=std(AllLikePosZygo);
StdLikeNegZygo=std(AllLikeNegZygo);
StdLikePosEventValPosZygo=std(AllLikePosEventValPosZygo);
StdikePosEventValNegZygo=std(AllLikePosEventValNegZygo);
StdLikeNegEventValPosZygo=std(AllLikeNegEventValPosZygo);
StdLikeNegEventValNegZygo=std(AllLikeNegEventValNegZygo);


%% create figures
% set the line colors
set(0,'DefaultAxesColorOrder',[1 0 0;0 1 0],...
      'DefaultAxesLineStyleOrder','-|--|:')
%x=0:0.1:49;
x=-1.9:0.1:50;

%Corrugator figures
plot(x,MeanLikeNegCorr(1:1:520),x,MeanLikePosCorr(1:1:520))
title('Corrugator Activation Likeability Exp 2','FontSize',12)
legend('dislikeable','likeable')
v=axis;
axis([-2 50 v(3) v(4)])
xlabel('time*1000 ms')
ylabel('activation as % of baseline')
annotation('line',[0.432 0.432],[0.10 0.92]);
annotation('line',[0.503 0.503],[0.10 0.92]);
annotation('line',[0.785 0.785],[0.10 0.92]);
annotation('line',[0.803 0.803],[0.10 0.92]);
annotation('line',[0.848 0.848],[0.10 0.92]);
saveas(gcf,[fdir 'LikeCorr.tiff']);
saveas(gcf,[fdir 'LikeCorr.jpg']);



plot(x,MeanLikePosEventValNegCorr(1:1:520),x,MeanLikePosEventValPosCorr(1:1:520),x,MeanLikeNegEventValNegCorr(1:1:520),x,MeanLikeNegEventValPosCorr(1:1:520))
title('Corrugator Activation Likeability Exp 2','FontSize',12)
legend('likeable-neg','likeable-pos','dislikeable-neg','dislikeable-pos')
v=axis;
axis([-2 50 v(3) v(4)])
xlabel('time*1000 ms')
ylabel('activation as % of baseline')
annotation('line',[0.432 0.432],[0.10 0.92]);
annotation('line',[0.503 0.503],[0.10 0.92]);
annotation('line',[0.785 0.785],[0.10 0.92]);
annotation('line',[0.803 0.803],[0.10 0.92]);
annotation('line',[0.848 0.848],[0.10 0.92]);
saveas(gcf,[fdir 'LikeEventValCorr.tiff']);
saveas(gcf,[fdir 'LikeEventValCorr.jpg']);

%% Zygomaticus figures
plot(x,MeanLikeNegZygo(30:1:520),x,MeanLikePosZygo(30:1:520))
title('Zygomaticus Activation Likeability','FontSize',12)
legend('dislikeable','likeable')
%axis tight
axis([0 49 70 220])
xlabel('time*1000 ms')
ylabel('activation as % of baseline')
annotation('line',[0.421 0.421],[0.10 0.92]);
annotation('line',[0.505 0.505],[0.10 0.92]);
annotation('line',[0.790 0.790],[0.10 0.92]);
annotation('line',[0.810 0.810],[0.10 0.92]);
annotation('line',[0.858 0.858],[0.10 0.92]);
saveas(gcf,[fdir 'LikeZygo.tiff']);
saveas(gcf,[fdir 'LikeZygo.jpg']);
plot(x,MeanLikePosEventValNegZygo(30:1:520),x,MeanLikePosEventValPosZygo(30:1:520),x,MeanLikeNegEventValNegZygo(30:1:520),x,MeanLikeNegEventValPosZygo(30:1:520))
title('Zygomaticus Activation Likeability','FontSize',12)
legend('likeable-neg','likeable-pos','dislikeable-neg','dislikeable-pos')
%axis tight
axis([0 49 70 220])
xlabel('time*1000 ms')
ylabel('activation as % of baseline')
annotation('line',[0.421 0.421],[0.10 0.92]);
annotation('line',[0.505 0.505],[0.10 0.92]);
annotation('line',[0.790 0.790],[0.10 0.92]);
annotation('line',[0.810 0.810],[0.10 0.92]);
annotation('line',[0.858 0.858],[0.10 0.92]);
saveas(gcf,[fdir 'LikeEventValZygo.tiff']);
saveas(gcf,[fdir 'LikeEventValZygo.jpg']);

