generateCellMeans;

clearvars -except avg f* n_cutoff

%compute averages for each treatment condition, where n = cells
avg.byCell.ctrl1.mean = nanmean(avg.byCell.ctrl1.value);
avg.byCell.ctrl1.SE = std(avg.byCell.ctrl1.value)/sqrt(length(avg.byCell.ctrl1.value));
avg.byCell.ctrl1.n = length(avg.byCell.ctrl1.value);

avg.byCell.TTX.mean = nanmean(avg.byCell.TTX.value);
avg.byCell.TTX.SE = std(avg.byCell.TTX.value)/sqrt(length(avg.byCell.TTX.value));
avg.byCell.TTX.n = length(avg.byCell.TTX.value);

avg.byCell.TTXCTZ.mean = nanmean(avg.byCell.TTXCTZ.value);
avg.byCell.TTXCTZ.SE = std(avg.byCell.TTXCTZ.value)/sqrt(length(avg.byCell.TTXCTZ.value));
avg.byCell.TTXCTZ.n = length(avg.byCell.TTXCTZ.value);

%compute averages for each culture
avg.byCulture.mean(1,1) = nanmean(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'xeko')));
avg.byCulture.SE(1,1) = nanstd(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'xeko')))/sqrt(length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'xeko'))));
avg.byCulture.n(1,1) = length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'xeko')));
avg.byCulture.mean(2,1) = nanmean(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'zeno')));
avg.byCulture.SE(2,1) = nanstd(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'zeno')))/sqrt(length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'zeno'))));
avg.byCulture.n(2,1) = length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'zeno')));
avg.byCulture.mean(3,1) = nanmean(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyA')));
avg.byCulture.SE(3,1) = nanstd(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyA')))/sqrt(length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyA'))));
avg.byCulture.n(3,1) = length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyA')));
avg.byCulture.mean(4,1) = nanmean(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyB')));
avg.byCulture.SE(4,1) = nanstd(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyB')))/sqrt(length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyB'))));
avg.byCulture.n(4,1) = length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'anchovyB')));
avg.byCulture.mean(5,1) = nanmean(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyA')));
avg.byCulture.SE(5,1) = nanstd(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyA')))/sqrt(length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyA'))));
avg.byCulture.n(5,1) = length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyA')));
avg.byCulture.mean(6,1) = nanmean(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyB')));
avg.byCulture.SE(6,1) = nanstd(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyB')))/sqrt(length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyA'))));
avg.byCulture.n(6,1) = length(avg.byCell.ctrl1.value(strcmp(avg.byCell.ctrl1.cultureSeries,'stimpyB')));

avg.byCulture.mean(1,2) = nanmean(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'xeko')));
avg.byCulture.SE(1,2) = nanstd(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'xeko')))/sqrt(length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'xeko'))));
avg.byCulture.n(1,2) = length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'xeko')));
avg.byCulture.mean(2,2) = nanmean(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'zeno')));
avg.byCulture.SE(2,2) = nanstd(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'zeno')))/sqrt(length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'zeno'))));
avg.byCulture.n(2,2) = length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'zeno')));
avg.byCulture.mean(3,2) = nanmean(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyA')));
avg.byCulture.SE(3,2) = nanstd(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyA')))/sqrt(length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyA'))));
avg.byCulture.n(3,2) = length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyA')));
avg.byCulture.mean(4,2) = nanmean(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyB')));
avg.byCulture.SE(4,2) = nanstd(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyB')))/sqrt(length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyB'))));
avg.byCulture.n(4,2) = length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'anchovyB')));
avg.byCulture.mean(5,2) = nanmean(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyA')));
avg.byCulture.SE(5,2) = nanstd(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyA')))/sqrt(length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyA'))));
avg.byCulture.n(5,2) = length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyA')));
avg.byCulture.mean(6,2) = nanmean(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyB')));
avg.byCulture.SE(6,2) = nanstd(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyB')))/sqrt(length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyA'))));
avg.byCulture.n(6,2) = length(avg.byCell.TTX.value(strcmp(avg.byCell.TTX.cultureSeries,'stimpyB')));

avg.byCulture.mean(1,3) = nanmean(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'xeko')));
avg.byCulture.SE(1,3) = nanstd(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'xeko')))/sqrt(length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'xeko'))));
avg.byCulture.n(1,3) = length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'xeko')));
avg.byCulture.mean(2,3) = nanmean(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'zeno')));
avg.byCulture.SE(2,3) = nanstd(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'zeno')))/sqrt(length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'zeno'))));
avg.byCulture.n(2,3) = length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'zeno')));
avg.byCulture.mean(3,3) = nanmean(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyA')));
avg.byCulture.SE(3,3) = nanstd(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyA')))/sqrt(length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyA'))));
avg.byCulture.n(3,3) = length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyA')));
avg.byCulture.mean(4,3) = nanmean(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyB')));
avg.byCulture.SE(4,3) = nanstd(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyB')))/sqrt(length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyB'))));
avg.byCulture.n(4,3) = length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'anchovyB')));
avg.byCulture.mean(5,3) = nanmean(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyA')));
avg.byCulture.SE(5,3) = nanstd(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyA')))/sqrt(length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyA'))));
avg.byCulture.n(5,3) = length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyA')));
avg.byCulture.mean(6,3) = nanmean(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyB')));
avg.byCulture.SE(6,3) = nanstd(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyB')))/sqrt(length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyA'))));
avg.byCulture.n(6,3) = length(avg.byCell.TTXCTZ.value(strcmp(avg.byCell.TTXCTZ.cultureSeries,'stimpyB')));

%compute averages for each treatment condition, where n = cultures
avg.byCulture.ctrl1.mean = nanmean(avg.byCulture.mean(:,1));
avg.byCulture.ctrl1.SE = std(avg.byCulture.mean(:,1))/sqrt(length(avg.byCulture.mean(:,1)));
avg.byCulture.ctrl1.n = length(avg.byCulture.mean(:,1));

avg.byCulture.TTX.mean = nanmean(avg.byCulture.mean(:,2));
avg.byCulture.TTX.SE = std(avg.byCulture.mean(:,2))/sqrt(length(avg.byCulture.mean(:,2)));
avg.byCulture.TTX.n = length(avg.byCulture.mean(:,2));

avg.byCulture.TTXCTZ.mean = nanmean(avg.byCulture.mean(:,3));
avg.byCulture.TTXCTZ.SE = std(avg.byCulture.mean(:,3))/sqrt(length(avg.byCulture.mean(:,3)));
avg.byCulture.TTXCTZ.n = length(avg.byCulture.mean(:,3));

%% plots

f1 = figure(1);
set(f1,'Position',[24    39   868   778]);

subplot(2,2,1);
hold off;
bar(1,avg.byCell.ctrl1.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(1,avg.byCell.ctrl1.mean,avg.byCell.ctrl1.SE,'-k','LineWidth',2);
text(1,avg.byCell.ctrl1.mean-0.5,['n=',num2str(avg.byCell.ctrl1.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(2,avg.byCell.TTX.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(2,avg.byCell.TTX.mean,avg.byCell.TTX.SE,'-k','LineWidth',2);
text(2,avg.byCell.TTX.mean-0.5,['n=',num2str(avg.byCell.TTX.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(3,avg.byCell.TTXCTZ.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(3,avg.byCell.TTXCTZ.mean,avg.byCell.TTXCTZ.SE,'-k','LineWidth',2);
text(3,avg.byCell.TTXCTZ.mean-0.5,['n=',num2str(avg.byCell.TTXCTZ.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);

set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14);
% ylabel('Amplitude (pA)','FontSize',16)
% ylabel('Frequency (Hz)','FontSize',16)
% ylim([0 4.5]);
xlim([0.25 3.75]);
box off;
title(['Mean+SEM for each treatment type',10,'(where n is number of cells)']);

%figure(2);
subplot(2,2,2);
hold off;
bar(1,avg.byCulture.ctrl1.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(1,avg.byCulture.ctrl1.mean,avg.byCulture.ctrl1.SE,'-k','LineWidth',2);
text(1,avg.byCulture.ctrl1.mean-0.5,['n=',num2str(avg.byCulture.ctrl1.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(2,avg.byCulture.TTX.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(2,avg.byCulture.TTX.mean,avg.byCulture.TTX.SE,'-k','LineWidth',2);
text(2,avg.byCulture.TTX.mean-0.5,['n=',num2str(avg.byCulture.TTX.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(3,avg.byCulture.TTXCTZ.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(3,avg.byCulture.TTXCTZ.mean,avg.byCulture.TTXCTZ.SE,'-k','LineWidth',2);
text(3,avg.byCulture.TTXCTZ.mean-0.5,['n=',num2str(avg.byCulture.TTXCTZ.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);

set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14);
% ylabel('Amplitude (pA)','FontSize',16)
% ylabel('Frequency (Hz)','FontSize',16)
% ylim([0 4.5]);
xlim([0.25 3.75]);
box off;
title(['Mean+SEM for each treatment type',10,'(where n is number of cultures)']);


%figure(3);
subplot(2,2,3);
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple
x_offset = -0.125:0.05:0.125;
hold off;
for i = 1:5
    plot([1 2 3]+x_offset(i),avg.byCulture.mean(i,:),'o-','LineWidth',2.5,'Color',plotcolors{i}); hold on;
    errorbar([1 2 3]+x_offset(i),avg.byCulture.mean(i,:),avg.byCulture.SE(i,:),'LineWidth',2.5,'Color',plotcolors{i});
end
set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14);
% ylabel('Amplitude (pA)','FontSize',16)
% ylabel('Frequency (Hz)','FontSize',16)
% ylim([0 7.5]);
xlim([0.25 3.75]);
box off;
%legend('xeko','','zeno','','anchovyA','','anchovyB','','stimpyA','','Location','NorthWest','FontSize',12);
%legend('xeko','zeno','anchovyA','anchovyB','stimpyA','Location','NorthWest','FontSize',12);
title('Mean+SEM for each culture');

%figure(4);
subplot(2,2,4);
markersize = 15;
x_inc = repmat([1 1.1 0.9 1.2 0.8 1.3 0.7]',20,1);
hold off;
y_tmp = sortrows(avg.byCell.ctrl1.value);
x_tmp = x_inc(1:length(y_tmp));
plot(x_tmp,y_tmp,'.k','MarkerSize',markersize);
hold on;
y_tmp = sortrows(avg.byCell.TTX.value);
x_tmp = x_inc(1:length(y_tmp));
plot(x_tmp+1,y_tmp,'.k','MarkerSize',markersize);
y_tmp = sortrows(avg.byCell.TTXCTZ.value);
x_tmp = x_inc(1:length(y_tmp));
plot(x_tmp+2,y_tmp,'.k','MarkerSize',markersize);


set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14);
% ylabel('Amplitude (pA)','FontSize',16)
% ylabel('Frequency (Hz)','FontSize',16)
% ylim([0 16]);
xlim([0.25 3.75]);
box off;
title('Mean amplitude for each cell');

%% quick sig test
[h p] = ttest2(avg.byCell.ctrl1.value,avg.byCell.TTX.value)
[h p] = ttest2(avg.byCell.ctrl1.value,avg.byCell.TTXCTZ.value)
[h p] = ttest2(avg.byCell.TTX.value,avg.byCell.TTXCTZ.value)

[h p] = ttest2(avg.byCulture.mean(:,1),avg.byCulture.mean(:,2))
[h p] = ttest2(avg.byCulture.mean(:,1),avg.byCulture.mean(:,3))
[h p] = ttest2(avg.byCulture.mean(:,2),avg.byCulture.mean(:,3))

%% real stats
stats.treatment = [repmat({'control'},[avg.byCell.ctrl1.n,1]); repmat({'TTX'},[avg.byCell.TTX.n,1]); repmat({'TTX+CTZ'},[avg.byCell.TTXCTZ.n,1])];
stats.cellMeans = [avg.byCell.ctrl1.value;avg.byCell.TTX.value;avg.byCell.TTXCTZ.value];
[p a s] = anova1(stats.cellMeans,stats.treatment);
[c m h nms] = multcompare(s,'ctype','bonferroni');

[p a s] = anova1(avg.byCulture.mean,{'ctrl1','TTX','TTX+CTZ'});
[c m h nms] = multcompare(s,'ctype','bonferroni');

%% no correction
stats.treatment = [repmat({'control'},[avg.byCell.ctrl1.n,1]); repmat({'TTX'},[avg.byCell.TTX.n,1]); repmat({'TTX+CTZ'},[avg.byCell.TTXCTZ.n,1])];
stats.cellMeans = [avg.byCell.ctrl1.value;avg.byCell.TTX.value;avg.byCell.TTXCTZ.value];
[p a s] = anova1(stats.cellMeans,stats.treatment);
[c m h nms] = multcompare(s);

[p a s] = anova1(avg.byCulture.mean,{'ctrl1','TTX','TTX+CTZ'});
[c m h nms] = multcompare(s);


%% cumhists & rankorder

generateRandSamp;
samp.ctrl1 = reshape(sample.mEPSC.ctrl1,1,size(sample.mEPSC.ctrl1,1)*size(sample.mEPSC.ctrl1,2));
samp.TTX = reshape(sample.mEPSC.TTX,1,size(sample.mEPSC.TTX,1)*size(sample.mEPSC.TTX,2));
samp.TTXCTZ = reshape(sample.mEPSC.TTXCTZ,1,size(sample.mEPSC.TTXCTZ,1)*size(sample.mEPSC.TTXCTZ,2));

maxmini = max([samp.ctrl1, samp.TTX, samp.TTXCTZ]);

dt = 1; %pA
bin = 0:dt:ceil(maxmini/dt)*dt;
histdata.ctrl1 = histc(samp.ctrl1,bin);
histdata.TTX = histc(samp.TTX,bin);
histdata.TTXCTZ = histc(samp.TTXCTZ,bin);

cumhistdata.ctrl1 = cumsum(histdata.ctrl1);
cumhistdata.TTX = cumsum(histdata.TTX);
cumhistdata.TTXCTZ = cumsum(histdata.TTXCTZ);

n_resize = min([length(samp.ctrl1),length(samp.TTX),length(samp.TTXCTZ)]);
rankdata.ctrl1 = sort(randsample(samp.ctrl1,n_resize));
rankdata.TTX = sort(randsample(samp.TTX,n_resize));
rankdata.TTXCTZ = sort(randsample(samp.TTXCTZ,n_resize));
stats.ctrl1_TTX = regstats(rankdata.TTX,rankdata.ctrl1);
stats.TTX_TTXCTZ = regstats(rankdata.TTXCTZ,rankdata.TTX);
stats.ctrl1_TTXCTZ = regstats(rankdata.TTXCTZ,rankdata.ctrl1);

plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple

% figure;
subplot(2,3,[1 2]);
hold off;
plot(bin+0.5,smooth(histdata.ctrl1,3),'-','LineWidth',3,'Color',plotcolors{1});
hold on;
plot(bin+0.5,smooth(histdata.TTX,3),'-','LineWidth',3,'Color',plotcolors{2});
plot(bin+0.5,smooth(histdata.TTXCTZ,3),'-','LineWidth',3,'Color',plotcolors{3});
legend('control','TTX','TTX+CTZ','Location','NorthEast','FontSize',12);
xlim([0 50]);
ylim([0 1.02*max([histdata.ctrl1 histdata.TTX histdata.TTXCTZ])]);
set(gca,'XTick',[0:5:50],'FontSize',14);
xlabel('Amplitude (pA)','FontSize',16)
ylabel('Counts');
box off;

subplot(2,3,3);
hold off;
plot(bin,cumhistdata.ctrl1/max(cumhistdata.ctrl1),'-','LineWidth',3,'Color',plotcolors{1});
hold on;
plot(bin,cumhistdata.TTX/max(cumhistdata.TTX),'-','LineWidth',3,'Color',plotcolors{2});
plot(bin,cumhistdata.TTXCTZ/max(cumhistdata.TTXCTZ),'-','LineWidth',3,'Color',plotcolors{3});
legend('control','TTX','TTX+CTZ','Location','SouthEast','FontSize',12);
set(gca,'YTick',[0:0.2:100],'XTick',[0:10:50],'FontSize',14);
xlabel('Amplitude (pA)','FontSize',16)
ylabel('Fraction of Events');
box off;
xlim([0 50]);
ylim([0 1.02]);
axis square;


subplot(2,3,4);
hold off;
plot(rankdata.ctrl1,rankdata.TTX,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.ctrl1_TTX.beta(1) maxmini*stats.ctrl1_TTX.beta(2)+stats.ctrl1_TTX.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.ctrl1_TTX.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
%axis([0 maxmini 0 maxmini]);
axis([0 100 0 100]);
set(gca,'XTick',[0:50:200],'YTick',[0:50:200],'FontSize',14);
title('Ranked Amplitude (pA)');
xlabel('Control','FontSize',16)
ylabel('TTX','FontSize',16)
box off;
axis square;

subplot(2,3,5);
hold off;
plot(rankdata.TTX,rankdata.TTXCTZ,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.TTX_TTXCTZ.beta(1) maxmini*stats.TTX_TTXCTZ.beta(2)+stats.TTX_TTXCTZ.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.TTX_TTXCTZ.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
axis([0 maxmini 0 maxmini]);
axis([0 100 0 100]);
set(gca,'XTick',[0:50:200],'YTick',[0:50:200],'FontSize',14);
title('Ranked Amplitude (pA)');
xlabel('TTX','FontSize',16)
ylabel('TTX+CTZ','FontSize',16)
box off;
axis square;

subplot(2,3,6);
hold off;
plot(rankdata.ctrl1,rankdata.TTXCTZ,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.ctrl1_TTXCTZ.beta(1) maxmini*stats.ctrl1_TTXCTZ.beta(2)+stats.ctrl1_TTXCTZ.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.ctrl1_TTXCTZ.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
axis([0 maxmini 0 maxmini]);
axis([0 100 0 100]);
set(gca,'XTick',[0:50:200],'YTick',[0:50:200],'FontSize',14);
title('Ranked Amplitude (pA)');
xlabel('Control','FontSize',16)
ylabel('TTX+CTZ','FontSize',16)
box off;
axis square;



%% test scaling (traditional)
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple

%rankdata.scaledTTX = (rankdata.TTX-stats.ctrl1_TTX.beta(1))/stats.ctrl1_TTX.beta(2);
rankdata.scaledTTX = rankdata.TTX/stats.ctrl1_TTX.beta(2);
figure(3);
plot(rankdata.ctrl1,rankdata.TTX,'ko');
hold on;
plot(rankdata.ctrl1,rankdata.scaledTTX,'ro');


%samples.scaledTTX = reshape((TTX.sample.mEPSC-stats.ctrl1_TTX.beta(1))/stats.ctrl1_TTX.beta(2),1,size(TTX.sample.mEPSC,1)*size(TTX.sample.mEPSC,2));
samp.scaledTTX = reshape((sample.mEPSC.TTX-stats.ctrl1_TTX.beta(1))/stats.ctrl1_TTX.beta(2),1,size(sample.mEPSC.TTX,1)*size(sample.mEPSC.TTX,2));
%samples.scaledTTX = reshape(TTX.sample.mEPSC/stats.ctrl1_TTX.beta(2),1,size(TTX.sample.mEPSC,1)*size(TTX.sample.mEPSC,2));
samp.scaledTTXCTZ = reshape((sample.mEPSC.TTXCTZ-stats.ctrl1_TTXCTZ.beta(1))/stats.ctrl1_TTXCTZ.beta(2),1,size(sample.mEPSC.TTXCTZ,1)*size(sample.mEPSC.TTXCTZ,2));
%samples.TTXCTZ = reshape(TTXCTZ.sample.mEPSC,1,size(TTXCTZ.sample.mEPSC,1)*size(TTXCTZ.sample.mEPSC,2));
%maxmini = max([samples.ctrl1, samples.TTX, samples.TTXCTZ]);

dt = 1; %pA
bin = 0:dt:ceil(maxmini/dt)*dt;
histdata.scaledTTX = histc(samp.scaledTTX,bin);
histdata.scaledTTXCTZ = histc(samp.scaledTTXCTZ,bin);

cumhistdata.scaledTTX = cumsum(histdata.scaledTTX);
cumhistdata.scaledTTXCTZ = cumsum(histdata.scaledTTXCTZ);


figure(4);
hold off;
plot(bin,cumhistdata.ctrl1/max(cumhistdata.ctrl1),'-','LineWidth',3,'Color',plotcolors{1});
hold on;
plot(bin,cumhistdata.scaledTTX/max(cumhistdata.scaledTTX),'-','LineWidth',3,'Color',plotcolors{2});
plot(bin,cumhistdata.scaledTTXCTZ/max(cumhistdata.scaledTTXCTZ),'-','LineWidth',3,'Color',plotcolors{3});
%legend('control','TTX','TTX+CTZ','Location','SouthEast','FontSize',12);
%set(gca,'YTick',[0:0.2:100],'XTick',[0:10:50],'FontSize',14);
xlabel('Amplitude (pA)','FontSize',16)
ylabel('Fraction of Events');
box off;
xlim([0 50]);
ylim([0 1.02]);
axis square;
