%%
load('PSTH data.mat', 'Control_PSTHz','Control_bPSTHz', 'Dreadd_PSTHz','Dreadd_bPSTHz', 'Control_sorted', 'Dreadd_sorted')
%%

% bin comparison with bonferroni correction
% any cell having at least 2 significant bins
binNum = 46;
BonferroniCorrection = 0.05/binNum
threshold = abs(norminv(BonferroniCorrection/2))
%%
Sorted_Control_PSTHz = Control_PSTHz(:,Control_sorted(1:37));
Sorted_Control_bPSTHz = Control_bPSTHz(:,Control_sorted(1:37));

CtrlFreqSigBins = cell(37,1);

CtrlBurstSigNum = zeros(37,1);

for i = 1:37
 PostPSTHz = abs(Sorted_Control_PSTHz(25:70,i));
 CtrlFreqSigBins{i} = find(PostPSTHz > threshold);
 
 
 
 PostBPSTHz = abs(Sorted_Control_bPSTHz(25:70,i));
 CtrlBurstSigNum (i) = sum(PostBPSTHz > threshold);
end

CtrlFreqSigNum = cellfun(@length, CtrlFreqSigBins);


CtrlAvgZscoreSigBin = zeros(37,1);
for i = 1:37
    CtrlAvgZscoreSigBin(i) = mean(Control_PSTHz(CtrlFreqSigBins{i},Control_sorted(i)));
end

%ControlBurstNull = find(sum(Control_bPSTHz(1:20,Control_sorted(1:37))==0));
%CtrlBurstSigNum(find(Control_sorted==ControlBurstNull)) = NaN;
%%
Sorted_Dreadd_PSTHz = Dreadd_PSTHz(:,Dreadd_sorted(1:36));
Sorted_Dreadd_bPSTHz = Dreadd_bPSTHz(:,Dreadd_sorted(1:36));

ExpFreqSigBins = cell(36,1);

ExpBurstSigNum = zeros(36,1);

for i = 1:36
 PostPSTHz = abs(Dreadd_PSTHz(25:70,i));
 ExpFreqSigBins{i} = find(PostPSTHz > threshold);
 
 
 
 PostBPSTHz = abs(Dreadd_bPSTHz(25:70,i));
 ExpBurstSigNum (i) = sum(PostBPSTHz > threshold);
end

ExpFreqSigNum = cellfun(@length, ExpFreqSigBins);


ExpAvgZscoreSigBin = zeros(36,1);
for i = 1:36
    ExpAvgZscoreSigBin(i) = mean(Dreadd_PSTHz(ExpFreqSigBins{i},Dreadd_sorted(i)));
end

%DreaddBurstNull = find(sum(Dreadd_bPSTHz(1:20,Dreadd_sorted(1:37))==0));
%ExpBurstSigNum(DreaddBurstNull) = NaN;

%%

figure
subplot(211)
[~,sorted] = sort(CtrlFreqSigNum);
plot(1:37,CtrlFreqSigNum(sorted),'ko')
%ylim([0 20])
xlim([0 37])
hold on
plot([0 40], [1 1], 'k--')
title('Firing rate')
ylabel('Number of significant bins')

subplot(212)
plot(1:37,CtrlBurstSigNum(sorted),'ko')
%ylim([0 20])
xlim([0 37])
hold on
plot([0 40], [1 1], 'k--')
title('Burst rate')
ylabel('Number of significant bins')

%%
figure
subplot(211)
[~,sorted] = sort(ExpFreqSigNum);
plot(1:36,ExpFreqSigNum(sorted),'ro')
%ylim([0 6])
xlim([0 36])
hold on
plot([0 40], [1 1], 'k--')
title('Firing rate')
ylabel('Number of significant bins')
xlabel('Cell Number')

subplot(212)
plot(1:36,ExpBurstSigNum(sorted),'ro')
%ylim([0 6])
xlim([0 36])
hold on
plot([0 40], [1 1], 'k--')
title('Burst rate')
ylabel('Number of significant bins')
xlabel('Cell Number')
%%

CtrlModulated = find(CtrlFreqSigNum > 0);
ExpModulated = find(ExpFreqSigNum > 0);

figure
bar([length(CtrlModulated)*100/length(CtrlFreqSigBins), (length(CtrlFreqSigBins)-length(CtrlModulated))*100/length(CtrlFreqSigBins); ...,
    length(ExpModulated)*100/length(ExpFreqSigNum), (length(ExpFreqSigNum)-length(ExpModulated))*100/length(ExpFreqSigNum)],'stacked')
set(gca, 'xticklabel', {'Control (n=37)', 'hM4Di (n=36)'})
ylabel('Cell num')
legend({'Modulated', 'Unmodulated'})
title('Firing rate')

%figure
% subplot(121)
% pie([length(CtrlModulated), length(CtrlFreqSigNum)-length(CtrlModulated)])
% title('Control (n=37)')
% %legend({'Modulated', 'Unmodulated'})
% 
% subplot(122)
% pie([length(ExpModulated), length(ExpFreqSigNum)-length(ExpModulated)])
% title('hM4Di (n=36)')
% %legend({'Modulated', 'Unmodulated'})
% 
n1 = length(CtrlModulated); N1 = length(CtrlFreqSigBins);
n2 = length(ExpModulated); N2 = length(ExpFreqSigNum);
x1 = [repmat('a',N1,1); repmat('b',N2,1)];
x2 = [repmat(1,n1,1); repmat(2,N1-n1,1); repmat(1,n2,1); repmat(2,N2-n2,1)];
[tbl,chi2stat,pval] = crosstab(x1,x2)


%%
CtrlPosModulated = find(CtrlAvgZscoreSigBin> 0);
CtrlNegModulated = find(CtrlAvgZscoreSigBin < 0);

ExpPosModulated = find(ExpAvgZscoreSigBin> 0);
ExpNegModulated = find(ExpAvgZscoreSigBin < 0);


figure
bar([length(CtrlPosModulated)*100/length(CtrlFreqSigBins), length(CtrlNegModulated)*100/length(CtrlFreqSigBins), ...,
    (length(CtrlFreqSigBins)-(length(CtrlPosModulated)+length(CtrlNegModulated)))*100/length(CtrlFreqSigBins); 
length(ExpPosModulated)*100/length(ExpFreqSigNum), length(ExpNegModulated)*100/length(ExpFreqSigNum), ...,
(length(ExpFreqSigBins)-(length(ExpPosModulated)+length(ExpNegModulated)))*100/length(ExpFreqSigBins)],'stacked')
set(gca, 'xticklabel', {'Control (n=37)', 'hM4Di (n=36)'})
ylabel('Cell num')
legend({'Postively Modulated', 'Negatively Modulated'})
title('Firing rate')


%%
figure
PlotAllDataPoints(CtrlFreqSigNum, ExpFreqSigNum)
set(gca,'xticklabel', {'Control', 'hM4Di'})

p = ranksum (CtrlFreqSigNum, ExpFreqSigNum)
text(1.2, 30, ['p = ', num2str(p)])


%%
CtrlModulated = find(CtrlBurstSigNum > 0);
ExpModulated = find(ExpBurstSigNum > 0);

CtrlBurstCellNum = length(CtrlBurstSigNum)
ExpBurstCellNum = length(ExpBurstSigNum)

figure
bar([length(CtrlModulated)*100/CtrlBurstCellNum, (CtrlBurstCellNum-length(CtrlModulated))*100/CtrlBurstCellNum; length(ExpModulated)*100/ExpBurstCellNum, (ExpBurstCellNum-length(ExpModulated))*100/ExpBurstCellNum],'stacked')
set(gca, 'xticklabel', {'Control (n=37)', 'hM4Di (n=36)'})
ylabel('Cell num')
legend({'Modulated', 'Unmodulated'})
title('Burst rate')

% figure
% subplot(121)
% pie([length(CtrlModulated), CtrlBurstCellNum-length(CtrlModulated)])
% title(['Control (n=', num2str(CtrlBurstCellNum), ')'])
% %legend({'Modulated', 'Unmodulated'})
% 
% subplot(122)
% pie([length(ExpModulated), ExpBurstCellNum-length(ExpModulated)])
% title(['hM4Di (n=', num2str(ExpBurstCellNum), ')'])
% %legend({'Modulated', 'Unmodulated'})

n1 = length(CtrlModulated); N1 = CtrlBurstCellNum;
n2 = length(ExpModulated); N2 = ExpBurstCellNum;
x1 = [repmat('a',N1,1); repmat('b',N2,1)];
x2 = [repmat(1,n1,1); repmat(2,N1-n1,1); repmat(1,n2,1); repmat(2,N2-n2,1)];
[tbl,chi2stat,pval] = crosstab(x1,x2)

%%
figure
PlotAllDataPoints(CtrlBurstSigNum, ExpBurstSigNum)
set(gca,'xticklabel', {'Control', 'hM4Di'})

p = ranksum (CtrlBurstSigNum, ExpBurstSigNum)
text(1.2, 25, ['p = ', num2str(p)])


%%
CtrlM = Control_PSTHz(:,Control_sorted(CtrlModulated)); %Modulated cells
ExpM = Dreadd_PSTHz(:,Dreadd_sorted(ExpModulated));

figure
boundedline(edges(1:end-1), mean(CtrlM,2), std(CtrlM,[],2)/sqrt(length(CtrlModulated)),'k') 
hold on
boundedline(edges(1:end-1), mean(ExpM,2), std(ExpM,[],2)/sqrt(length(ExpModulated)),'r') 

