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']);


x_ctrl.samples(1,:) = reshape(ctrl.sample.mEPSC,1,size(ctrl.sample.mEPSC,1)*size(ctrl.sample.mEPSC,2));
x_TTX.samples(1,:) = reshape(TTX.sample.mEPSC,1,size(TTX.sample.mEPSC,1)*size(TTX.sample.mEPSC,2));
x_TTXCTZ.samples(1,:) = reshape(TTXCTZ.sample.mEPSC,1,size(TTXCTZ.sample.mEPSC,1)*size(TTXCTZ.sample.mEPSC,2));
x_minmini(1,:) = min([x_ctrl.samples(1,:) x_TTX.samples(1,:) x_TTXCTZ.samples(1,:)]);
x_maxmini(1,:) = max([x_ctrl.samples(1,:) x_TTX.samples(1,:) x_TTXCTZ.samples(1,:)]);

x_ctrl.samples(2,:) = x_ctrl.samples(1,:) - mean(x_ctrl.samples(1,:));
x_TTX.samples(2,:) = x_TTX.samples(1,:) - mean(x_TTX.samples(1,:));
x_TTXCTZ.samples(2,:) = x_TTXCTZ.samples(1,:) - mean(x_TTXCTZ.samples(1,:));
x_minmini(2,:) = min([x_ctrl.samples(2,:) x_TTX.samples(2,:) x_TTXCTZ.samples(2,:)]);
x_maxmini(2,:) = max([x_ctrl.samples(2,:) x_TTX.samples(2,:) x_TTXCTZ.samples(2,:)]);

x_ctrl.samples(3,:) = x_ctrl.samples(1,:) - median(x_ctrl.samples(1,:));
x_TTX.samples(3,:) = x_TTX.samples(1,:) - median(x_TTX.samples(1,:));
x_TTXCTZ.samples(3,:) = x_TTXCTZ.samples(1,:) - median(x_TTXCTZ.samples(1,:));
x_minmini(3,:) = min([x_ctrl.samples(3,:) x_TTX.samples(3,:) x_TTXCTZ.samples(3,:)]);
x_maxmini(3,:) = max([x_ctrl.samples(3,:) x_TTX.samples(3,:) x_TTXCTZ.samples(3,:)]);

n_resize = min([length(x_ctrl.samples(1,:)) length(x_TTX.samples(1,:)) length(x_TTXCTZ.samples(1,:))]);
for i = 1:3
    x_ctrl.ranksamp(i,:) = sort(randsample(x_ctrl.samples(i,:),n_resize));
    x_TTX.ranksamp(i,:) = sort(randsample(x_TTX.samples(i,:),n_resize));
    x_TTXCTZ.ranksamp(i,:) = sort(randsample(x_TTXCTZ.samples(i,:),n_resize));
    ctrl_TTX.rankstat(i,:) = regstats(x_TTX.ranksamp(i,:),x_ctrl.ranksamp(i,:));
    TTX_TTXCTZ.rankstat(i,:) = regstats(x_TTXCTZ.ranksamp(i,:),x_TTX.ranksamp(i,:));
    ctrl_TTXCTZ.rankstat(i,:) = regstats(x_TTXCTZ.ranksamp(i,:),x_ctrl.ranksamp(i,:));
end

%% plots

for i = 1:1
    ctrl_TTX.rankstat(i,:).beta(2)
    %subplot(3,3,i*3-2);
    subplot(1,3,1)
    hold off;
    plot(x_ctrl.ranksamp(i,:),x_TTX.ranksamp(i,:),'ko');
    hold on;
    x_low = floor(x_minmini(i,:))-5;
    x_high = floor(x_maxmini(i,:));
    plot([x_low x_high],[x_low x_high],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
    plot([x_low x_high],[x_low+ctrl_TTX.rankstat(i,:).beta(1) x_high*ctrl_TTX.rankstat(i,:).beta(2)+ctrl_TTX.rankstat(i,:).beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
    %text(x_high*0.98,x_high*0.02,['R^2=',num2str(ctrl_TTX.rankstat(i,:).rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
    axis([-20 50 -20 50]);
    axis square

    %subplot(3,3,i*3-1);
    subplot(1,3,2)
    hold off;
    plot(x_TTX.ranksamp(i,:),x_TTXCTZ.ranksamp(i,:),'ko');
    hold on;
    plot([x_low x_high],[x_low x_high],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
    plot([x_low x_high],[x_low+TTX_TTXCTZ.rankstat(i,:).beta(1) x_high*TTX_TTXCTZ.rankstat(i,:).beta(2)+TTX_TTXCTZ.rankstat(i,:).beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
    %text(x_high*0.98,x_high*0.02,['R^2=',num2str(ctrl_TTX.rankstat(i,:).rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
    axis([-20 50 -20 50]);
    axis square

%         subplot(3,3,i*3);
subplot(1,3,3)
    hold off;
    plot(x_ctrl.ranksamp(i,:),x_TTXCTZ.ranksamp(i,:),'ko');
    hold on;
    plot([x_low x_high],[x_low x_high],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
    plot([x_low x_high],[x_low+ctrl_TTXCTZ.rankstat(i,:).beta(1) x_high*ctrl_TTXCTZ.rankstat(i,:).beta(2)+ctrl_TTXCTZ.rankstat(i,:).beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
    %text(x_high*0.98,x_high*0.02,['R^2=',num2str(ctrl_TTX.rankstat(i,:).rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
    axis([-20 50 -20 50]);
    axis square

end

















%%
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));

samples.adjMean.ctrl = samples.ctrl - mean(samples.ctrl);
samples.adjMean.TTX = samples.TTX - mean(samples.TTX);
samples.adjMean.TTXCTZ = samples.TTXCTZ - mean(samples.TTXCTZ);

samples.adjMedian.ctrl = samples.ctrl - median(samples.ctrl);
samples.adjMedian.TTX = samples.TTX - median(samples.TTX);
samples.adjMedian.TTXCTZ = samples.TTXCTZ - median(samples.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);

rankdata.adjMean.ctrl = sort(randsample(samples.adjMean.ctrl,n_resize));
rankdata.adjMean.TTX = sort(randsample(samples.adjMean.TTX,n_resize));
rankdata.adjMean.TTXCTZ = sort(randsample(samples.adjMean.TTXCTZ,n_resize));
stats.adjMean.ctrl_TTX = regstats(rankdata.adjMean.TTX,rankdata.adjMean.ctrl);
stats.adjMean.TTX_TTXCTZ = regstats(rankdata.adjMean.TTXCTZ,rankdata.adjMean.TTX);
stats.adjMean.ctrl_TTXCTZ = regstats(rankdata.adjMean.TTXCTZ,rankdata.adjMean.ctrl);

rankdata.adjMedian.ctrl = sort(randsample(samples.adjMedian.ctrl,n_resize));
rankdata.adjMedian.TTX = sort(randsample(samples.adjMedian.TTX,n_resize));
rankdata.adjMedian.TTXCTZ = sort(randsample(samples.adjMedian.TTXCTZ,n_resize));
stats.adjMedian.ctrl_TTX = regstats(rankdata.adjMedian.TTX,rankdata.adjMedian.ctrl);
stats.adjMedian.TTX_TTXCTZ = regstats(rankdata.adjMedian.TTXCTZ,rankdata.adjMedian.TTX);
stats.adjMedian.ctrl_TTXCTZ = regstats(rankdata.adjMedian.TTXCTZ,rankdata.adjMedian.ctrl);


%% plots
figure;

minmini = min([samples.ctrl, samples.TTX, samples.TTXCTZ]);
maxmini = max([samples.ctrl, samples.TTX, samples.TTXCTZ]);

subplot(3,3,1);
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');

subplot(3,3,2);
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');

subplot(3,3,3);
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');

for i = 1:3
    subplot(3,3,i)
    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;
end



maxmini = max([samples.adjMean.ctrl, samples.adjMean.TTX, samples.adjMean.TTXCTZ]);

subplot(3,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');

subplot(3,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');

subplot(3,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');

for i = 4:6
    subplot(3,3,i)
    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;
end

