clear all;
f_datcol = 3; %amplitudes
n_cutoff = 30;
isctrl = 0;

%% EXTRACT ORIGINAL DATA %%

plot_title = 'worm19308-19309';
load('C:\Users\mingfaifong\Documents\2012-07-27_TTX-CTZ-vehicle_patch-analysis\ctrl\zeno7_alldata'); dat_ctrl = dat;
load('C:\Users\mingfaifong\Documents\2012-07-27_TTX-CTZ-vehicle_patch-analysis\TTX\worm19308_alldata'); dat_TTX = dat;
load('C:\Users\mingfaifong\Documents\2012-07-27_TTX-CTZ-vehicle_patch-analysis\TTX-CTZ\worm19309_alldata'); dat_TTXCTZ = dat;
for i = 1:max([size(dat_TTX,1),size(dat_TTXCTZ,1)])
    dat_ctrl{i} = nan(100,19);
end

%% GENERATING SAMPLES %%

sampdata_ctrl = [];
for i = 1:size(dat_ctrl,1)
    if length(dat_ctrl{i}) >= n_cutoff
        sampdata_ctrl = [sampdata_ctrl; randsample(dat_ctrl{i}(:,f_datcol),n_cutoff)];
    end
end

sampdata_TTX = [];
for i = 1:size(dat_TTX,1)
    if length(dat_TTX{i}) >= n_cutoff
        sampdata_TTX = [sampdata_TTX; randsample(dat_TTX{i}(:,f_datcol),n_cutoff)];
    end
end

sampdata_TTXCTZ = [];
for i = 1:size(dat_TTXCTZ,1)
    if length(dat_TTXCTZ{i}) >= n_cutoff
        sampdata_TTXCTZ = [sampdata_TTXCTZ; randsample(dat_TTXCTZ{i}(:,f_datcol),n_cutoff)];
    end
end

maxsamp = min([length(sampdata_ctrl),length(sampdata_TTX),length(sampdata_TTXCTZ)]);
sampdata_ctrl = randsample(sampdata_ctrl,maxsamp);
sampdata_TTX = randsample(sampdata_TTX,maxsamp);
sampdata_TTXCTZ = randsample(sampdata_TTXCTZ,maxsamp);

rankdata_ctrl = sort(sampdata_ctrl);
rankdata_TTX = sort(sampdata_TTX);
rankdata_TTXCTZ = sort(sampdata_TTXCTZ);

maxmini = max([sampdata_ctrl; sampdata_TTX; sampdata_TTXCTZ]);


%% PLOTTING %%

close all;

figure(1);
subplot(1,3,1);
plot(rankdata_ctrl,rankdata_TTX,'ko'); xlabel('Ranked Ctrl mEPSC Amplitude (pA)'); ylabel('Ranked TTX mEPSC Amplitude (pA)');
hold on; plot([0 100],[0 100],'k:');
axis square;
subplot(1,3,2);
plot(rankdata_ctrl,rankdata_TTXCTZ,'ko'); xlabel('Ranked Ctrl mEPSC Amplitude (pA)'); ylabel('Ranked TTX+CTZ mEPSC Amplitude (pA)');
hold on; plot([0 100],[0 100],'k:');
title(plot_title,'FontSize',16);
axis square;
subplot(1,3,3);
plot(rankdata_TTX,rankdata_TTXCTZ,'ko'); xlabel('Ranked TTX mEPSC Amplitude (pA)'); ylabel('Ranked TTX+CTZ mEPSC Amplitude (pA)');
hold on; plot([0 100],[0 100],'k:');
% hold on; plot([0 maxmini],[0 maxmini],'k:');
axis([0 100 0 100]);
axis square;    

figure(2);
bin = 4:0.5:ceil(maxmini);
cumhist_ctrl = cumsum(histc(rankdata_ctrl,bin));
cumhist_TTX = cumsum(histc(rankdata_TTX,bin));
cumhist_TTXCTZ = cumsum(histc(rankdata_TTXCTZ,bin));
plot(bin,cumhist_ctrl/max(cumhist_ctrl),'-k','LineWidth',3);
hold on;
plot(bin,cumhist_TTX/max(cumhist_TTX),'-b','LineWidth',3);
plot(bin,cumhist_TTXCTZ/max(cumhist_TTXCTZ),'-r','LineWidth',3);
legend('control','TTX','TTX+CTZ','Location','SouthEast');
axis square
axis([0 maxmini 0 1.05]);
xlabel('mEPSC Amplitude (pA)'); ylabel('Fraction of Events');
title(plot_title,'FontSize',16);

