clear all;

load C:\Users\mingfaifong\Documents\2013-01-16_TTX-CTZ-vehicle_patch-analysis\30events_01\ctrl_30events.mat;
dat.ctrl1.byCell.mean = stats.avg;
dat.ctrl1.byCell.std = stats.std;
dat.ctrl1.byCell.n = stats.n;
dat.ctrl1.miniSamp.value = sample.mEPSC;

load C:\Users\mingfaifong\Documents\2013-01-16_TTX-CTZ-vehicle_patch-analysis\30events_01\TTX_30events.mat;
dat.TTX.byCell.mean = stats.avg;
dat.TTX.byCell.std = stats.std;
dat.TTX.byCell.n = stats.n;
dat.TTX.miniSamp.value = sample.mEPSC;

load C:\Users\mingfaifong\Documents\2013-01-16_TTX-CTZ-vehicle_patch-analysis\30events_01\TTX-CTZ_30events.mat;
dat.TTXCTZ.byCell.mean = stats.avg;
dat.TTXCTZ.byCell.std = stats.std;
dat.TTXCTZ.byCell.n = stats.n;
dat.TTXCTZ.miniSamp.value = sample.mEPSC;

save TTXCNQXCTZChR2_alldat dat;

generateCellMeans;
load TTXCNQXCTZChR2_alldat;
dat.ctrl2.byCell.mean = avg.byCell.ctrl.value;
dat.ctrl2.byCell.std = avg.byCell.ctrl.std;
dat.ctrl2.byCell.n = avg.byCell.ctrl.n;

dat.CNQX.byCell.mean = avg.byCell.CNQX.value;
dat.CNQX.byCell.std = avg.byCell.CNQX.std;
dat.CNQX.byCell.n = avg.byCell.CNQX.n;

dat.CNQXstim1x.byCell.mean = avg.byCell.CNQXstim1x.value;
dat.CNQXstim1x.byCell.std = avg.byCell.CNQXstim1x.std;
dat.CNQXstim1x.byCell.n = avg.byCell.CNQXstim1x.n;

save TTXCNQXCTZChR2_alldat dat;

generateRandSamp;
load TTXCNQXCTZChR2_alldat;
dat.ctrl2.miniSamp.value = sample.mEPSC.ctrl;
dat.CNQX.miniSamp.value = sample.mEPSC.CNQX;
dat.CNQXstim1x.miniSamp.value = sample.mEPSC.CNQXstim1x;

save TTXCNQXCTZChR2_alldat dat;

clearvars -except dat

%% plot means

dat.ctrl1.overall.mean = nanmean(dat.ctrl1.byCell.mean);
dat.ctrl1.overall.stderr = nanstd(dat.ctrl1.byCell.mean)/sqrt(length(dat.ctrl1.byCell.n));
dat.TTX.overall.mean = nanmean(dat.TTX.byCell.mean);
dat.TTX.overall.stderr = nanstd(dat.TTX.byCell.mean)/sqrt(length(dat.TTX.byCell.n));
dat.TTXCTZ.overall.mean = nanmean(dat.TTXCTZ.byCell.mean);
dat.TTXCTZ.overall.stderr = nanstd(dat.TTXCTZ.byCell.mean)/sqrt(length(dat.TTXCTZ.byCell.n));
dat.ctrl2.overall.mean = nanmean(dat.ctrl2.byCell.mean);
dat.ctrl2.overall.stderr = nanstd(dat.ctrl2.byCell.mean)/sqrt(length(dat.ctrl2.byCell.n));
dat.CNQX.overall.mean = nanmean(dat.CNQX.byCell.mean);
dat.CNQX.overall.stderr = nanstd(dat.CNQX.byCell.mean)/sqrt(length(dat.CNQX.byCell.n));
dat.CNQXstim1x.overall.mean = nanmean(dat.CNQXstim1x.byCell.mean);
dat.CNQXstim1x.overall.stderr = nanstd(dat.CNQXstim1x.byCell.mean)/sqrt(length(dat.CNQXstim1x.byCell.n));

f1 = figure(1);
set(f1,'Position',[100 200 500 400]);
hold off;
% 
% % ctrl vs. TTX vs. TTX+CTZ
% bar(1,dat.ctrl1.overall.mean,'FaceColor','k','LineWidth',1); hold on;
% errorbar(1,dat.ctrl1.overall.mean,dat.ctrl1.overall.stderr,'-k','LineWidth',2);
% bar(2,dat.TTX.overall.mean,'FaceColor','k','LineWidth',1); hold on;
% errorbar(2,dat.TTX.overall.mean,dat.TTX.overall.stderr,'-k','LineWidth',2);
% bar(3,dat.TTXCTZ.overall.mean,'FaceColor','k','LineWidth',1); hold on;
% errorbar(3,dat.TTXCTZ.overall.mean,dat.TTXCTZ.overall.stderr,'-k','LineWidth',2);
% set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14,'LineWidth',2);
% title('Mean Amplitude'); ylabel('pA')
% %ylabel('Amplitude (pA)','FontSize',18)
% xlim([0.25 3.75]);
% ylim([0 21]);
% axis square;
% box off;

% ctrl vs. CNQX vs. CNQX+stim
bar(1,dat.ctrl2.overall.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(1,dat.ctrl2.overall.mean,dat.ctrl2.overall.stderr,'-k','LineWidth',2);
bar(2,dat.CNQX.overall.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(2,dat.CNQX.overall.mean,dat.CNQX.overall.stderr,'-k','LineWidth',2);
bar(3,dat.CNQXstim1x.overall.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(3,dat.CNQXstim1x.overall.mean,dat.CNQXstim1x.overall.stderr,'-k','LineWidth',2);
set(gca,'XTick',[1,2,3],'XTickLabel',{'control','CNQX','CNQX+stim'},'FontSize',14,'LineWidth',2);
title('Mean Amplitude'); ylabel('pA')
% ylabel('Amplitude (pA)','FontSize',18)
xlim([0.25 3.75]);
ylim([0 21]);
axis square;
box off;

% % TTX vs. CNQX
% bar(1,dat.TTX.overall.mean/dat.ctrl1.overall.mean*100,'FaceColor','k','LineWidth',1); hold on;
% errorbar(1,dat.TTX.overall.mean/dat.ctrl1.overall.mean*100,dat.TTX.overall.stderr/dat.ctrl1.overall.mean*100,'-k','LineWidth',2);
% bar(2,dat.CNQX.overall.mean/dat.ctrl2.overall.mean*100,'FaceColor','k','LineWidth',1); hold on;
% errorbar(2,dat.CNQX.overall.mean/dat.ctrl2.overall.mean*100,dat.CNQX.overall.stderr/dat.ctrl2.overall.mean*100,'-k','LineWidth',2);
% set(gca,'XTick',[1,2],'XTickLabel',{'TTX','CNQX'},'FontSize',14,'LineWidth',2);
% title('Mean Amplitude');
% ylabel('% of Control','FontSize',18)
% plot([0 2.75],[100 100],':k');
% xlim([0.25 3.75]);
% %ylim([0 2]);
% axis square;
% box off;

%% Cumulative Histograms

plotcolors = {[0 0 0],[0 0.3 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
bin = 4:1:400;

dat.ctrl1.miniSamp.nhist = histc(reshape(dat.ctrl1.miniSamp.value,[numel(dat.ctrl1.miniSamp.value),1]),bin);
dat.ctrl1.miniSamp.cumhist = cumsum(dat.ctrl1.miniSamp.nhist)/max(cumsum(dat.ctrl1.miniSamp.nhist));
dat.TTX.miniSamp.nhist = histc(reshape(dat.TTX.miniSamp.value,[numel(dat.TTX.miniSamp.value),1]),bin);
dat.TTX.miniSamp.cumhist = cumsum(dat.TTX.miniSamp.nhist)/max(cumsum(dat.TTX.miniSamp.nhist));
dat.TTXCTZ.miniSamp.nhist = histc(reshape(dat.TTXCTZ.miniSamp.value,[numel(dat.TTXCTZ.miniSamp.value),1]),bin);
dat.TTXCTZ.miniSamp.cumhist = cumsum(dat.TTXCTZ.miniSamp.nhist)/max(cumsum(dat.TTXCTZ.miniSamp.nhist));
dat.ctrl2.miniSamp.nhist = histc(reshape(dat.ctrl2.miniSamp.value,[numel(dat.ctrl2.miniSamp.value),1]),bin);
dat.ctrl2.miniSamp.cumhist = cumsum(dat.ctrl2.miniSamp.nhist)/max(cumsum(dat.ctrl2.miniSamp.nhist));
dat.CNQX.miniSamp.nhist = histc(reshape(dat.CNQX.miniSamp.value,[numel(dat.CNQX.miniSamp.value),1]),bin);
dat.CNQX.miniSamp.cumhist = cumsum(dat.CNQX.miniSamp.nhist)/max(cumsum(dat.CNQX.miniSamp.nhist));
dat.CNQXstim1x.miniSamp.nhist = histc(reshape(dat.CNQXstim1x.miniSamp.value,[numel(dat.CNQXstim1x.miniSamp.value),1]),bin);
dat.CNQXstim1x.miniSamp.cumhist = cumsum(dat.CNQXstim1x.miniSamp.nhist)/max(cumsum(dat.CNQXstim1x.miniSamp.nhist));

f2 = figure(2);
set(f2,'Position',[100 200 500 400]);
hold off;

% % TTX vs contrl only
% plot(bin+0.5,dat.ctrl1.miniSamp.cumhist*100,'Color',plotcolors{1},'LineWidth',3);
% hold on;
% plot(bin+0.5,dat.TTX.miniSamp.cumhist*100,'Color',plotcolors{2},'LineWidth',3);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100]);
% title('Cumulative Histogram');
% xlabel('Amplitude (pA)','FontSize',18);
% ylabel('% of Events','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;
% xlim([0 80]);
% ylim([0 101]);
% legend('control','TTX','Location','SouthEast');

% % TTX vs. TTX+CTZ
% plot(bin+0.5,dat.ctrl1.miniSamp.cumhist*100,'Color',plotcolors{1},'LineWidth',3);
% hold on;
% plot(bin+0.5,dat.TTX.miniSamp.cumhist*100,'Color',plotcolors{2},'LineWidth',3);
% plot(bin+0.5,dat.TTXCTZ.miniSamp.cumhist*100,'Color',plotcolors{4},'LineWidth',3);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100]);
% title('Cumulative Histogram');
% xlabel('Amplitude (pA)','FontSize',18);
% ylabel('% of Events','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;
% xlim([0 80]);
% ylim([0 101]);
% legend('control','TTX','TTX+CTZ','Location','SouthEast');
% 
% % CNQX only
% plot(bin+0.5,dat.ctrl2.miniSamp.cumhist*100,'Color',plotcolors{1},'LineWidth',3);
% hold on;
% plot(bin+0.5,dat.CNQX.miniSamp.cumhist*100,'Color',plotcolors{3},'LineWidth',3);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100]);
% title('Cumulative Histogram');
% xlabel('Amplitude (pA)','FontSize',18);
% ylabel('% of Events','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;
% xlim([0 80]);
% ylim([0 101]);
% legend('control','CNQX','Location','SouthEast');

% 
% CNQX vs. CNQX+stim
plot(bin+0.5,dat.ctrl2.miniSamp.cumhist*100,'Color',plotcolors{1},'LineWidth',3);
hold on;
plot(bin+0.5,dat.CNQX.miniSamp.cumhist*100,'Color',plotcolors{3},'LineWidth',3);
plot(bin+0.5,dat.CNQXstim1x.miniSamp.cumhist*100,'Color',plotcolors{6},'LineWidth',3);
set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100]);
title('Cumulative Histogram');
xlabel('Amplitude (pA)','FontSize',18);
ylabel('% of Events','FontSize',18)
%ylim([0 2]);
axis square;
box off;
xlim([0 80]);
ylim([0 101]);
legend('control','CNQX','CNQX+stim','Location','SouthEast');

%% Ranked Amplitude Plots

maxdraw.TTXonly = min([numel(dat.ctrl1.miniSamp.value),numel(dat.TTX.miniSamp.value)]);
maxdraw.CNQXonly = min([numel(dat.ctrl2.miniSamp.value),numel(dat.CNQX.miniSamp.value)]);
maxdraw.allTTX = min([numel(dat.ctrl1.miniSamp.value),numel(dat.TTX.miniSamp.value),numel(dat.TTXCTZ.miniSamp.value)]);
maxdraw.allCNQX = min([numel(dat.ctrl2.miniSamp.value),numel(dat.CNQX.miniSamp.value),numel(dat.CNQXstim1x.miniSamp.value)]);

dat.ctrl1.miniSamp.rankTTXonly = randsample(reshape(dat.ctrl1.miniSamp.value,[numel(dat.ctrl1.miniSamp.value),1]),maxdraw.TTXonly);
dat.ctrl1.miniSamp.rankallTTX = randsample(reshape(dat.ctrl1.miniSamp.value,[numel(dat.ctrl1.miniSamp.value),1]),maxdraw.allTTX);
dat.TTX.miniSamp.rankTTXonly = randsample(reshape(dat.TTX.miniSamp.value,[numel(dat.TTX.miniSamp.value),1]),maxdraw.TTXonly);
dat.TTX.miniSamp.rankallTTX = randsample(reshape(dat.TTX.miniSamp.value,[numel(dat.TTX.miniSamp.value),1]),maxdraw.allTTX);
dat.TTXCTZ.miniSamp.rankTTXonly = randsample(reshape(dat.TTXCTZ.miniSamp.value,[numel(dat.TTXCTZ.miniSamp.value),1]),maxdraw.TTXonly);
dat.TTXCTZ.miniSamp.rankallTTX = randsample(reshape(dat.TTXCTZ.miniSamp.value,[numel(dat.TTXCTZ.miniSamp.value),1]),maxdraw.allTTX);
dat.ctrl2.miniSamp.rankCNQXonly = randsample(reshape(dat.ctrl2.miniSamp.value,[numel(dat.ctrl2.miniSamp.value),1]),maxdraw.CNQXonly);
dat.ctrl2.miniSamp.rankallCNQX = randsample(reshape(dat.ctrl2.miniSamp.value,[numel(dat.ctrl2.miniSamp.value),1]),maxdraw.allCNQX);
dat.CNQX.miniSamp.rankCNQXonly = randsample(reshape(dat.CNQX.miniSamp.value,[numel(dat.CNQX.miniSamp.value),1]),maxdraw.CNQXonly);
dat.CNQX.miniSamp.rankallCNQX = randsample(reshape(dat.CNQX.miniSamp.value,[numel(dat.CNQX.miniSamp.value),1]),maxdraw.allCNQX);
dat.CNQXstim1x.miniSamp.rankCNQXonly = randsample(reshape(dat.CNQXstim1x.miniSamp.value,[numel(dat.CNQXstim1x.miniSamp.value),1]),maxdraw.CNQXonly);
dat.CNQXstim1x.miniSamp.rankallCNQX = randsample(reshape(dat.CNQXstim1x.miniSamp.value,[numel(dat.CNQXstim1x.miniSamp.value),1]),maxdraw.allCNQX);

f3 = figure(3);
set(f3,'Position',[100 200 500 400]);
hold off;


% TTX vs. control
hold off;
plot([0 200],[0 200],'-','Color',[0.7 0.7 0.7],'LineWidth',2);
hold on;
plot(sortrows(dat.ctrl1.miniSamp.rankTTXonly),sortrows(dat.TTX.miniSamp.rankTTXonly),'ok','MarkerSize',5);
axis([0 100 0 100]);
set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100],'YTick',[0:20:100]);
title('Ranked Amplitude (pA)');
xlabel('Control','FontSize',18);
ylabel('TTX','FontSize',18)
%ylim([0 2]);
axis square;
box off;

% % ctrl vs. TTX+CTZ
% hold off;
% plot([0 200],[0 200],'-','Color',[0.7 0.7 0.7],'LineWidth',2);
% hold on;
% plot(sortrows(dat.ctrl1.miniSamp.rankallTTX),sortrows(dat.TTXCTZ.miniSamp.rankallTTX),'ok','MarkerSize',5);
% axis([0 100 0 100]);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100],'YTick',[0:20:100]);
% title('Ranked Amplitude (pA)');
% xlabel('Control','FontSize',18);
% ylabel('TTX+CTZ','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;

% % TTX vs. TTX+CTZ
% hold off;
% plot([0 200],[0 200],'-','Color',[0.7 0.7 0.7],'LineWidth',2);
% hold on;
% plot(sortrows(dat.TTX.miniSamp.rankallTTX),sortrows(dat.TTXCTZ.miniSamp.rankallTTX),'ok','MarkerSize',5);
% axis([0 100 0 100]);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100],'YTick',[0:20:100]);
% title('Ranked Amplitude (pA)');
% xlabel('TTX','FontSize',18);
% ylabel('TTX+CTZ','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;
% 
% % CNQX vs. control
% hold off;
% plot([0 200],[0 200],'-','Color',[0.7 0.7 0.7],'LineWidth',2);
% hold on;
% plot(sortrows(dat.ctrl2.miniSamp.rankCNQXonly),sortrows(dat.CNQX.miniSamp.rankCNQXonly),'ok','MarkerSize',5);
% axis([0 100 0 100]);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100],'YTick',[0:20:100]);
% title('Ranked Amplitude (pA)');
% xlabel('Control','FontSize',18);
% ylabel('CNQX','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;
% 
% % control vs. CNQX+stim
% hold off;
% plot([0 200],[0 200],'-','Color',[0.7 0.7 0.7],'LineWidth',2);
% hold on;
% plot(sortrows(dat.ctrl2.miniSamp.rankallCNQX),sortrows(dat.CNQXstim1x.miniSamp.rankallCNQX),'ok','MarkerSize',5);
% axis([0 100 0 100]);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100],'YTick',[0:20:100]);
% title('Ranked Amplitude (pA)');
% xlabel('Control','FontSize',18);
% ylabel('CNQX+stim','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;

% % CNQX vs. CNQX+stim
% hold off;
% plot([0 200],[0 200],'-','Color',[0.7 0.7 0.7],'LineWidth',2);
% hold on;
% plot(sortrows(dat.CNQX.miniSamp.rankallCNQX),sortrows(dat.CNQXstim1x.miniSamp.rankallCNQX),'ok','MarkerSize',5);
% axis([0 100 0 100]);
% set(gca,'FontSize',14,'LineWidth',2,'XTick',[0:20:100],'YTick',[0:20:100]);
% title('Ranked Amplitude (pA)');
% xlabel('CNQX','FontSize',18);
% ylabel('CNQX+stim','FontSize',18)
% %ylim([0 2]);
% axis square;
% box off;
