clear all;

dirID = 'C:\Users\mingfaifong\Documents\2013-01-16_TTX-CTZ-vehicle_patch-analysis\';
type = '30events_01';
ctrl = load([dirID,type,'\ctrl_',type(1:2),'events']);
TTX = load([dirID,type,'\TTX_',type(1:2),'events']);
TTXCTZ = load([dirID,type,'\TTX-CTZ_',type(1:2),'events']);

%compute averages for each condition
drugAvg.ctrl = mean(ctrl.stats.avg);
drugStderr.ctrl = std(ctrl.stats.avg)/length(ctrl.stats.avg);
drugAvg.TTX = mean(TTX.stats.avg);
drugStderr.TTX = std(TTX.stats.avg)/length(TTX.stats.avg);
drugAvg.TTXCTZ = mean(TTXCTZ.stats.avg);
drugStderr.TTXCTZ = std(TTXCTZ.stats.avg)/length(TTXCTZ.stats.avg);

%compute averages for each culture
for i = 1:length(ctrl.culturelist)
    cellAvg.ctrl(i) = mean(ctrl.stats.avg(ctrl.cellID(ctrl.cultureID==i)));
    cellStderr.ctrl(i) = std(ctrl.stats.avg(ctrl.cellID(ctrl.cultureID==i)))/sqrt(length(ctrl.culturelist));
end
for i = 1:length(TTX.culturelist)
    cellAvg.TTX(i) = mean(TTX.stats.avg(TTX.cellID(TTX.cultureID==i)));
    cellStderr.TTX(i) = std(TTX.stats.avg(TTX.cellID(TTX.cultureID==i)))/sqrt(length(TTX.culturelist));
end
for i = 1:length(TTXCTZ.culturelist)
    cellAvg.TTXCTZ(i) = mean(TTXCTZ.stats.avg(TTXCTZ.cellID(TTXCTZ.cultureID==i)));
    cellStderr.TTXCTZ(i) = std(TTXCTZ.stats.avg(TTXCTZ.cellID(TTXCTZ.cultureID==i)))/sqrt(length(TTXCTZ.culturelist));
end

%re-order cell averages so sister culture rows match up
tmp = cellAvg.ctrl(3);
cellAvg.ctrl(3) = cellAvg.ctrl(4);
cellAvg.ctrl(4) = tmp;


%% plots
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(1);

subplot(1,3,1);
hold off;
bar(1,drugAvg.ctrl,'FaceColor','k','LineWidth',1); hold on;
errorbar(1,drugAvg.ctrl,drugStderr.ctrl,'-k','LineWidth',2);
text(1,drugAvg.ctrl-0.5,['n=',num2str(length(ctrl.stats.avg))],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(2,drugAvg.TTX,'FaceColor','k','LineWidth',1); hold on;
errorbar(2,drugAvg.TTX,drugStderr.TTX,'-k','LineWidth',2);
text(2,drugAvg.TTX-0.5,['n=',num2str(length(TTX.stats.avg))],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(3,drugAvg.TTXCTZ,'FaceColor','k','LineWidth',1); hold on;
errorbar(3,drugAvg.TTXCTZ,drugStderr.TTXCTZ,'-k','LineWidth',2);
text(3,drugAvg.TTXCTZ-0.5,['n=',num2str(length(TTXCTZ.stats.avg))],'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)
xlim([0.25 3.75]);
ylim([0 21]);
box off;
title('Mean+SEM for each treatment type');


subplot(1,3,2);
hold off;
x_offset = 0.6:(1.4-0.6)/length(ctrl.stats.avg):1.4;
for i = 1:length(ctrl.stats.avg)
    plot(x_offset(i),ctrl.stats.avg(i),'bo','MarkerSize',5);
    %plot(1,ctrl.stats.avg(i),'ko','MarkerSize',5); hold on;
    errorbar(x_offset(i),ctrl.stats.avg(i),ctrl.stats.std(i),'-','LineWidth',0.5,'Color',[0.5 0.5 0.5]);
end
x_offset = 1.6:(2.4-1.6)/length(TTX.stats.avg):2.4;
for i = 1:length(TTX.stats.avg)
    plot(x_offset(i),TTX.stats.avg(i),'bo','MarkerSize',5);
    %plot(2,TTX.stats.avg(i),'bo','MarkerSize',5);
    errorbar(x_offset(i),TTX.stats.avg(i),TTX.stats.std(i),'-','LineWidth',0.5,'Color',[0.5 0.5 0.5]);
end
x_offset = 2.6:(3.4-2.6)/length(TTXCTZ.stats.avg):3.4;
for i = 1:length(TTXCTZ.stats.avg)
    plot(x_offset(i),TTXCTZ.stats.avg(i),'ro','MarkerSize',5);
    %plot(3,TTXCTZ.stats.avg(i),'ro','MarkerSize',5);
    errorbar(x_offset(i),TTXCTZ.stats.avg(i),TTXCTZ.stats.std(i),'-','LineWidth',0.5,'Color',[0.5 0.5 0.5]);
end
set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14);
ylabel('Amplitude (pA)','FontSize',16)
xlim([0.25 3.75]);
ylim([0 100]);
box off
title('Mean+STD for each cell');


subplot(1,3,3);
hold off;
x_offset = -0.125:0.05:0.125;
for i = 1:length(ctrl.culturelist)
    plot([1 2 3]+x_offset(i),[cellAvg.ctrl(i) cellAvg.TTX(i) cellAvg.TTXCTZ(i)],'o-','LineWidth',2.5,'Color',plotcolors{i}); hold on;
    errorbar([1 2 3]+x_offset(i),[cellAvg.ctrl(i) cellAvg.TTX(i) cellAvg.TTXCTZ(i)],[cellStderr.ctrl(i) cellStderr.TTX(i) cellStderr.TTXCTZ(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)
xlim([0.25 3.75]);
ylim([5 32]);
box off;
title('Mean+SEM for each culture');

%% plot means with cultures (not cells) as grouping scheme

figure(2);
hold off;
bar(1,mean(cellAvg.ctrl),'FaceColor','k','LineWidth',1); hold on;
errorbar(1,mean(cellAvg.ctrl),std(cellAvg.ctrl)/length(cellAvg.ctrl),'-k','LineWidth',2);
text(1,mean(cellAvg.ctrl)-0.5,['n=',num2str(length(cellAvg.ctrl))],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(2,mean(cellAvg.TTX),'FaceColor','k','LineWidth',1); hold on;
errorbar(2,mean(cellAvg.TTX),std(cellAvg.TTX)/length(cellAvg.TTX),'-k','LineWidth',2);
text(2,mean(cellAvg.TTX)-0.5,['n=',num2str(length(cellAvg.TTX))],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(3,mean(cellAvg.TTXCTZ),'FaceColor','k','LineWidth',1); hold on;
errorbar(3,mean(cellAvg.TTXCTZ),std(cellAvg.TTXCTZ)/length(cellAvg.TTXCTZ),'-k','LineWidth',2);
text(3,mean(cellAvg.TTXCTZ)-0.5,['n=',num2str(length(cellAvg.TTXCTZ))],'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)
xlim([0.25 3.75]);
ylim([0 21]);
box off;
title('Mean+SEM for each treatment type (n is cultures)');
