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']);
% ctrl = load([dirID,type,'\ctrl_30events']);
% TTX = load([dirID,type,'\TTX_30events']);
% TTXCTZ = load([dirID,type,'\TTX-CTZ_30events']);

samples.ctrl = reshape(ctrl.sample.mEPSC,1,size(ctrl.sample.mEPSC,1)*size(ctrl.sample.mEPSC,2));
samples.TTX = reshape(TTX.sample.mEPSC,1,size(TTX.sample.mEPSC,1)*size(TTX.sample.mEPSC,2));
samples.TTXCTZ = reshape(TTXCTZ.sample.mEPSC,1,size(TTXCTZ.sample.mEPSC,1)*size(TTXCTZ.sample.mEPSC,2));
maxmini = max([samples.ctrl, samples.TTX, samples.TTXCTZ]);

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

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

n_resize = min([length(samples.ctrl),length(samples.TTX),length(samples.TTXCTZ)]);
rankdata.ctrl = sort(randsample(samples.ctrl,n_resize));
rankdata.TTX = sort(randsample(samples.TTX,n_resize));
rankdata.TTXCTZ = sort(randsample(samples.TTXCTZ,n_resize));
stats.ctrl_TTX = regstats(rankdata.TTX,rankdata.ctrl);
stats.TTX_TTXCTZ = regstats(rankdata.TTXCTZ,rankdata.TTX);
stats.ctrl_TTXCTZ = regstats(rankdata.TTXCTZ,rankdata.ctrl);


%% 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(2);
subplot(2,3,[1 2]);
hold off;
plot(bin+0.5,smooth(histdata.ctrl,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.ctrl 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.ctrl/max(cumhistdata.ctrl),'-','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.ctrl,rankdata.TTX,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.ctrl_TTX.beta(1) maxmini*stats.ctrl_TTX.beta(2)+stats.ctrl_TTX.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.ctrl_TTX.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
axis([0 maxmini 0 maxmini]);
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]);
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.ctrl,rankdata.TTXCTZ,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.ctrl_TTXCTZ.beta(1) maxmini*stats.ctrl_TTXCTZ.beta(2)+stats.ctrl_TTXCTZ.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.ctrl_TTXCTZ.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
axis([0 maxmini 0 maxmini]);
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.ctrl_TTX.beta(1))/stats.ctrl_TTX.beta(2);
rankdata.scaledTTX = rankdata.TTX/stats.ctrl_TTX.beta(2);
figure(3);
plot(rankdata.ctrl,rankdata.TTX,'ko');
hold on;
plot(rankdata.ctrl,rankdata.scaledTTX,'ro');


%samples.scaledTTX = reshape((TTX.sample.mEPSC-stats.ctrl_TTX.beta(1))/stats.ctrl_TTX.beta(2),1,size(TTX.sample.mEPSC,1)*size(TTX.sample.mEPSC,2));
samples.scaledTTX = reshape(TTX.sample.mEPSC/stats.ctrl_TTX.beta(2),1,size(TTX.sample.mEPSC,1)*size(TTX.sample.mEPSC,2));
%samples.TTXCTZ = reshape(TTXCTZ.sample.mEPSC,1,size(TTXCTZ.sample.mEPSC,1)*size(TTXCTZ.sample.mEPSC,2));
%maxmini = max([samples.ctrl, samples.TTX, samples.TTXCTZ]);

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

cumhistdata.scaledTTX = cumsum(histdata.scaledTTX);
%cumhistdata.TTXCTZ = cumsum(histdata.TTXCTZ);


figure(4);
hold off;
plot(bin,cumhistdata.ctrl/max(cumhistdata.ctrl),'-','LineWidth',3,'Color',plotcolors{1});
hold on;
plot(bin,cumhistdata.scaledTTX/max(cumhistdata.scaledTTX),'-','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;