% Jon Newman
% Multi-parameter activity assay for cultures transfected with
% optical exciters and silencers.
%
% This script combines data from both cultures on which this experiment was
% conducted
%   - venus-20708, 2012.05.03, 21 div, 17 dpt
%   - venus-20709, 2012.04.16, 18 div, 17 dpt

% Plot parameters
h = 22; % mm
w1 = 17; % mm
wg = 1.6; % mm
w2 = 3.4;
s = 3;
wt = w1+wg+w2+s;

close all
figure('Units','centimeters','Position',[1 1 30 10])

ap = {};
as = {};

axes('Units','centimeters','Position',[5 5 h/10 h/10]);
set(gca,'Fontsize',12,'TickDir','Out','TickLength',[0.015 0.025],'XTick',[0]);
box off
hold on

col = [
    [000 000 000];
    [000 100 255];
    [220 000 000];
    [000 255 000];
    ]/255;
    


for j = 1:2
    switch j
        
        case {1}
            % Venus Data
            base = 'J:/newmonster/2012_05_03_Venus-MultiClamp/venus-20708/';
            
            fids = {
                'multi-u_dead-time_2012-05-03_hoss-r3_venus_20708_01';
                'multi-u_dead-time_2012-05-03_hoss-r3_venus_20708_01_cnqx';
                'multi-u_dead-time_2012-05-04_hoss-r3_venus_20708_01_ap5';
                'multi-u_dead-time_2012-05-08_hoss-r3_venus_20708_01_bic';
                };
            
            load('venus_20708_bad_unit.mat')
            

            sym2 = ':';
        case {2}
            %% Tommy data
            base = 'J:/newmonster/2012_04_16_Tommy-MultiClamp/tommy-20709/';
            
            fids = {
                'multi-u_dead-time_2012-04-16_hoss-r3_tommy_20709_01';
                'multi-u_dead-time_2012-04-17_hoss-r3_tommy_20709_01_cnqx';
                'multi-u_dead-time_2012-04-18_hoss-r3_tommy_20709_01_ap5';
                'multi-u_dead-time_2012-04-18_hoss-r3_tommy_20709_01_bic';
                };
            
            load('tommy_20709_bad_unit.mat')
            
            sym2 = '-';
    end
    
    for f = 1:length(fids)
        
        
        fid = fids{f};
        s = loadspk([base fid '.spk']);
        d = loaddig([base fid '.dig']);
        
        
        
        %% Decode dig file
        b = dec2bin(d.state,32);
        u = bin2dec(b(:,1:29))/10e6;
        c = bin2dec(b(:,30:31));
        t = bin2dec(b(:,32));
        
        %% ChR2
        st.time = d.time(t & c == 0);
        st.channel = ones(size(st.time));
        st.type = u(t & c == 0);
        ss = SqueakySpk(fid,s.meta.fs_Hz,1,s,st);
        ss.RemoveChannel([1 8 33 57 64 35 24 60]);
        ss.RemoveUnit([0 bu]);
        
        
        ss.PeriStimHistogram(1,[-0.01 0.025]*1000,ss.st_type > 0.45 & ss.st_type < 0.55,0)
        stairs(ss.psh.t,ss.psh.hist/max(ss.unit),sym2,'Color',col(f,:))
        drawnow
        
    end
end

set(gca,'Xlim',[-0.01 0.025])

%%
% saveas(gcf,'combined_results_alldrug_60sec.fig')
% export_fig('-pdf','-transparent','combined_results_alldrug_60sec')
