%% linear comp
clear all;
f_datcol = 3; %amplitudes
n_cutoff = 30;

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\anchovy3_alldata');
dat_TTX = dat;
load('C:\Users\mingfaifong\Documents\2012-07-27_TTX-CTZ-vehicle_patch-analysis\TTX-CTZ\anchovy2_alldata');
dat_TTXCTZ = dat;

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)];
        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)]);
maxsamp = min([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]);
maxmini = max([sampdata_TTX; sampdata_TTXCTZ]);

close all;
figure;
% subplot(1,3,1);
% plot(rankdata_ctrl,rankdata_TTX,'ro'); xlabel('Ranked Ctrl mEPSC Amplitude (pA)'); ylabel('Ranked TTX mEPSC Amplitude (pA)');
% hold on; plot([0 maxmini],[0 maxmini],'k--');
% axis square;
% subplot(1,3,2);
% plot(rankdata_ctrl,rankdata_TTXCTZ,'bo'); xlabel('Ranked Ctrl mEPSC Amplitude (pA)'); ylabel('Ranked TTX+CTZ mEPSC Amplitude (pA)');
% hold on; plot([0 maxmini],[0 maxmini],'k--');
% title('zeno7109','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 maxmini],[0 maxmini],'k--');
axis square;
title('anchovy32','FontSize',16);

%%



for i_cultureID = 1:length(cultureID)
    load(['C:\Users\mingfaifong\Documents\2012-06-21_anchovy\' cultureID{i_cultureID} '_alldata.mat']);
    sampdata = [];
    clear byCulture*
    
    % -- generating sample data -- %
    for i_dat = 1:length(dat)
        if length(dat{i_dat}) >= n_cutoff
            sampdata = [sampdata; randsample(dat{i_dat}(:,f_datcol),n_cutoff)];
        end
    end
    
    bin = 4:1:150;
    byCulture_nhist = histc(sampdata,bin);
    byCulture_cumhist = cumsum(byCulture_nhist);
    byCulture_avg = nanmean(sampdata);
    byCulture_n = length(sampdata);
    byCulture_stderr = nanstd(sampdata)/sqrt(length(sampdata));
    
figure(2); hold on;
bar(i_cultureID,byCulture_avg,'FaceColor',plotcolors{i_cultureID},'LineWidth',1)
errorbar(i_cultureID,byCulture_avg,byCulture_stderr,'-k','LineWidth',2);
figure(3); hold on;
plot(1:length(byCulture_cumhist),byCulture_cumhist/max(byCulture_cumhist),'Color',plotcolors{i_cultureID},'LineWidth',3);

end











%% get directory names of analysis (all folders containing drug conditions)

clear all; close all;
parentdir = 'C:\Users\mingfaifong\Documents\';
filelist = dir(parentdir);
dirlist = {filelist([filelist.isdir]).name};
%dirlist = dirlist(~(strcmp('.',dirlist)|strcmp('..',dirlist)|~cellfun(@isempty,strfind(dirlist,'patch-analysis'))))';
dirlist = dirlist(~cellfun(@isempty,strfind(dirlist,'DIV')))';





%% batch created "alldata" files for each experiment

for i = 1:length(dirlist)
    dirID = dirlist{i};
    uloc = strfind(dirID,'_');
    cultureID = dirID(uloc(1)+1:uloc(2)-1);
    dirID_full = [parentdir,dirID,'/ascii/'];
    y = dir(dirID_full); z = [y.isdir];
    fidlist = {y(~z).name}';
    
    % replacing all commas with nothing
    for i = 1:length(fidlist)
        fid = fidlist{i};
        replaceinfile(',','',[dirID_full fid],'-nobak');
    end
    
    % importing data
    for i = 1:length(fidlist)
        fid = fidlist{i};
        dat{i,1} = importdata([dirID_full fid]);
        dat{i,1} = sortrows(dat{i},2);
    end
    
    % combine files from same recording
    i_combine = {};
    i_remove = [];
    keyboard;
    for i = 1:length(i_combine)
        for j = 1:length(i_combine{i})-1
            dat{i_combine{i}(1)} = [dat{i_combine{i}(1)}; dat{i_combine{i}(j+1)}];
            i_remove(end+1) = i_combine{i}(j+1)
            fidlist{i_combine{i}(1)} = [fidlist{i_combine{i}(1)},'; ',fidlist{i_combine{i}(j+1)}]
        end
    end
    dat(i_remove) = [];
    fidlist(i_remove) = [];

    % add frequencies in Hz
    for i = 1:length(dat)
        dat{i,1}(:,19) = [1000./diff(dat{i,1}(:,2)); NaN]; %frequency in Hz
    end

    % save data by cultureID
    dat_tmp = dat;
%    save([dirID_full(1:length(dirID_full)-6) cultureID '_alldata'],'dat','fidlist');
    save([parentdir dirID '/' cultureID '_alldata'],'dat','fidlist');

end

%%
for i_cultureID = 1:length(cultureID)
    load(['C:\Users\mingfaifong\Documents\2012-06-21_anchovy\' cultureID{i_cultureID} '_alldata.mat']);
    sampdata = [];
    clear byCulture*
    
    % -- generating sample data -- %
    for i_dat = 1:length(dat)
        if length(dat{i_dat}) >= n_cutoff
            sampdata = [sampdata; randsample(dat{i_dat}(:,f_datcol),n_cutoff)];
        end
    end
    
    bin = 4:1:150;
    byCulture_nhist = histc(sampdata,bin);
    byCulture_cumhist = cumsum(byCulture_nhist);
    byCulture_avg = nanmean(sampdata);
    byCulture_n = length(sampdata);
    byCulture_stderr = nanstd(sampdata)/sqrt(length(sampdata));
    
figure(2); hold on;
bar(i_cultureID,byCulture_avg,'FaceColor',plotcolors{i_cultureID},'LineWidth',1)
errorbar(i_cultureID,byCulture_avg,byCulture_stderr,'-k','LineWidth',2);
figure(3); hold on;
plot(1:length(byCulture_cumhist),byCulture_cumhist/max(byCulture_cumhist),'Color',plotcolors{i_cultureID},'LineWidth',3);

end