%% load spike data
dirID = 'C:\Users\USER\Documents\mingfaifong\2013-01-21_trouble22468_12-14DIV_MEA_CNQX-ChR2-stim-CL\';

spk.spont = loadspk([dirID 'trouble22478_12DIV_nodrug_02.spk']);
spk.cnqxstim = loadspk([dirID 'trouble22468_13DIV_40uM-CNQX-ChR2-stim-CL_03.spk']);

%% pick burst time
subplot(4,1,1);
plot(spk.spont.time,spk.spont.channel+0.15*randn(size(spk.spont.channel)),'.k');
subplot(4,1,2);
plot(spk.cnqxstim.time,spk.cnqxstim.channel+0.15*randn(size(spk.cnqxstim.channel)),'.k');

bin_spont = 0:0.01:max(spk.spont.time);
bin_cnqxstim = 0:0.01:max(spk.cnqxstim.time);
asdr_spont = histc(spk.spont.time,bin_spont);
asdr_cnqxstim = histc(spk.cnqxstim.time,bin_cnqxstim);

subplot(4,1,3);
stairs(bin_spont,asdr_spont,'-k');
subplot(4,1,4);
stairs(bin_cnqxstim,asdr_cnqxstim,'-k');

%% load raw data at burst time of interest
t1_spont = 53.89-1;
t1_cnqxstim = 419.09-1;

[r_spont t_spont] = loadraw([dirID 'trouble22478_12DIV_nodrug_02.spkflt'],[t1_spont t1_spont+5]);
[r_cnqxstim t_cnqxstim] = loadraw([dirID 'trouble22468_13DIV_40uM-CNQX-ChR2-stim-CL_03.spkflt'],[t1_cnqxstim t1_cnqxstim+5]);

% v_max = abs(min([max(max(r_spont)) max(max(r_cnqxstim))]));

%% plot raw by channel
for i = 1:64
subplot(2,1,1);
plot(t_spont,r_spont(i,:)/10^-6,'-k');
title(['channel ',num2str(i) ' (spontaneous)']);
v_max = max(abs([r_spont(i,:) r_cnqxstim(i,:)]/10^-6))+5;
xlim([t1_spont t1_spont+5]);
ylim([-v_max v_max]);
ylabel('Voltage (uV)','FontSize',14)
box off;
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
subplot(2,1,2);
plot(t_cnqxstim,r_cnqxstim(i,:)/10^-6,'-k');
title(['channel ',num2str(i) ' (CNQX+stimulation)']);
xlim([t1_cnqxstim t1_cnqxstim+5]);
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
ylim([-v_max v_max]);
xlabel('Time (sec)','FontSize',14); ylabel('Voltage (uV)','FontSize',14)
box off;
%export_fig(['C:\Users\Lab\Documents\2012-10-03_herbI5374_12-14DIV_MEA_CNQX-ChR2-stim-CL\herbI5374_raw_ch' num2str(i) '.pdf'],'-pdf','-m2');
if i<10
    ch = ['0' num2str(i)];
else
    ch = num2str(i);
end
export_fig([dirID 'raw data traces\' 'trouble22468_raw_ch' ch '.png'],'-png');
end;

%% plot rasters
subplot(2,1,1);
plot(spk.spont.time,spk.spont.channel+0.15*randn(size(spk.spont.channel)),'.k','MarkerSize',5);
title('spontaneous burst');
xlim([t1_spont t1_spont+5]);
ylim([0 64]);
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
subplot(2,1,2);
plot(spk.cnqxstim.time,spk.cnqxstim.channel+0.15*randn(size(spk.cnqxstim.channel)),'.k','MarkerSize',5);
title('activity evoked by ChR2 during 40uM CNQX application');
xlim([t1_cnqxstim t1_cnqxstim+5]);
ylim([0 64]);
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
export_fig([dirID 'raw data traces\' 'trouble22468_raw_raster_y-jitter.png'],'-png');

%% plot ch10 bursts
t1_spont = [53.89-1, 387.96-1];
t1_cnqxstim = [52.91-1, 205.49-1, 419.09-1];

for j = 1:length(t1_spont)
    for k = 1:length(t1_cnqxstim)
        [r_spont t_spont] = loadraw([dirID 'trouble22478_12DIV_nodrug_02.spkflt'],[t1_spont(j) t1_spont(j)+5]);
        [r_cnqxstim t_cnqxstim] = loadraw([dirID 'trouble22468_13DIV_40uM-CNQX-ChR2-stim-CL_03.spkflt'],[t1_cnqxstim(k) t1_cnqxstim(k)+5]);

        i = 10; %channel number
        subplot(2,1,1);
        plot(t_spont,r_spont(i,:)/10^-6,'-k');
        title(['channel ',num2str(i) ' (spontaneous)']);
        v_max = max(abs([r_spont(i,:) r_cnqxstim(i,:)]/10^-6))+5;
        xlim([t1_spont(j)+0.75 t1_spont(j)+2.75]);
        ylim([-v_max v_max]);
        ylabel('Voltage (uV)','FontSize',14)
        box off;
        set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
        subplot(2,1,2);
        plot(t_cnqxstim,r_cnqxstim(i,:)/10^-6,'-k');
        title(['channel ',num2str(i) ' (CNQX+stimulation)']);
        xlim([t1_cnqxstim(k)+0.75 t1_cnqxstim(k)+2.75]);
        set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
        ylim([-v_max v_max]);
        xlabel('Time (sec)','FontSize',14); ylabel('Voltage (uV)','FontSize',14)
        box off;
        export_fig([dirID 'raw data traces\' 'trouble22468_raw_ch10_combo' num2str(j) num2str(k) '.png'],'-png');
    end
end

%% tweet chosen traces
t1_spont = 53.89-1;
t1_cnqxstim = 419.09-1;

i = 10;

% [r_spont t_spont] = loadraw([dirID 'trouble22478_12DIV_nodrug_02.spkflt'],[t1_spont t1_spont+5]);
% [r_cnqxstim t_cnqxstim] = loadraw([dirID 'trouble22468_13DIV_40uM-CNQX-ChR2-stim-CL_03.spkflt'],[t1_cnqxstim t1_cnqxstim+5]);
figure(2);
subplot(2,1,1);
hold off;
plot(t_spont-t1_spont,r_spont(i,:)/10^-6,'-k');
title(['channel ',num2str(i) ' (spontaneous)']);
v_max = max(abs([r_spont(i,:) r_cnqxstim(i,:)]/10^-6))+5;
v_max = 70;

% xlim([t1_spont t1_spont+5]);
xlim([0.5 2.5]);
ylim([-v_max v_max]);
ylabel('Voltage (uV)','FontSize',14)
box off;
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
subplot(2,1,2);
hold off;
plot(t_cnqxstim-t1_cnqxstim+0.02,r_cnqxstim(i,:)/10^-6,'-k');
hold on;
plot([0.9373 0.9373+0.01],[40 40],'-b','LineWidth',2);

title(['channel ',num2str(i) ' (CNQX+stimulation)']);
% xlim([t1_cnqxstim t1_cnqxstim+5]);
xlim([0.5 2.5]);
set(gca,'TickDir','out','TickLength',[0.01 0.0025]);
ylim([-v_max v_max]);
xlabel('Time (sec)','FontSize',14); ylabel('Voltage (uV)','FontSize',14)
box off;

%% plot spike times
subplot(2,1,1); hold on;
plot(spk.spont.time(spk.spont.unit==1 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-3)-t1_spont,...
    ones(size(spk.spont.time(spk.spont.unit==1 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-3)))*60,'sr')
plot(spk.spont.time(spk.spont.unit==2 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-3)-t1_spont,...
    ones(size(spk.spont.time(spk.spont.unit==2 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-3)))*65,'sb')
plot(spk.spont.time(spk.spont.unit==3 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-3)-t1_spont,...
    ones(size(spk.spont.time(spk.spont.unit==3 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-3)))*70,'sg')
subplot(2,1,2); hold on;
plot(spk.cnqxstim.time(spk.cnqxstim.unit==1 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-3)-t1_cnqxstim+0.02,...
    ones(size(spk.cnqxstim.time(spk.cnqxstim.unit==1 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-3)))*60,'sr')
plot(spk.cnqxstim.time(spk.cnqxstim.unit==2 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-3)-t1_cnqxstim+0.02,...
    ones(size(spk.cnqxstim.time(spk.cnqxstim.unit==2 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-3)))*65,'sb')
plot(spk.cnqxstim.time(spk.cnqxstim.unit==3 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-3)-t1_cnqxstim+0.02,...
    ones(size(spk.cnqxstim.time(spk.cnqxstim.unit==3 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-3)))*70,'sg')

%%
export_fig(['C:\Users\USER\Documents\mingfaifong\UpScalingPaper_figures\2013-06-16\trouble22468_raw2.pdf'],'-pdf','-transparent','-r300');
%% find units
figure;

subplot(2,3,1);
plot(spk.spont.waveform(:,spk.spont.unit==1 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-2.5))

subplot(2,3,2);
plot(spk.spont.waveform(:,spk.spont.unit==2 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-2.5))

subplot(2,3,3);
plot(spk.spont.waveform(:,spk.spont.unit==3 & spk.spont.time>t1_spont+0.5 & spk.spont.time>t1_spont-2.5))

subplot(2,3,4);
plot(spk.cnqxstim.waveform(:,spk.cnqxstim.unit==1 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-2.5))

subplot(2,3,5);
plot(spk.cnqxstim.waveform(:,spk.cnqxstim.unit==2 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-2.5))

subplot(2,3,6);
plot(spk.cnqxstim.waveform(:,spk.cnqxstim.unit==3 & spk.cnqxstim.time>t1_cnqxstim+0.5 & spk.cnqxstim.time>t1_cnqxstim-2.5))
% 
% for i = 1:6
%     subplot(2,3,i)
%     axis([1 50 -5.5e-5 5.5e-5]);
%     axis square;
%     box off;
%     set(gca,'XTick',[],'YTick',[]);
% end

% export_fig(['C:\Users\USER\Documents\mingfaifong\UpScalingPaper_figures\2013-06-16\trouble22468_waveforms.pdf'],'-pdf','-transparent','-r300');