load ('CBdata.mat', 'norm_CB', 'depth')
load('PRh axon data.mat', 'norm_MouseYFP','MouseDepth')
%%
samedepth = dsearchn(MouseDepth, depth);


%% Averaging two bins in MouseYFP
temp1 = norm_MouseYFP(1:2:end,:);
temp2 = norm_MouseYFP(2:2:end,:);

norm_MouseYFPcorr = (temp1+temp2)./2;
norm_MouseYFPcorr(387,:) = norm_MouseYFP(772,:);

%% Effect size
effect = nanmean(norm_MouseYFPcorr,2) .* nanmean(norm_CB,2);

%%
figure

boundedline(depth, nanmean(norm_MouseYFPcorr,2), nanstd(norm_MouseYFPcorr,[],2)./sqrt(size(norm_MouseYFPcorr,2)),'g-') 

hold on
boundedline(depth, mean(norm_CB,2), std(norm_CB,[],2)./sqrt(size(norm_CB,2)),'r-') 
plot(depth,effect,'k','linew',2)
xlim([50 1000])
ylim([0 1])
xlabel('Depth (um)')
ylabel('Normmalized Amplitude')


% Layer boundaries (Lefort et al., 2009, Neuron)
L1 = 128;
L23 = 418;
L4 = 588;
L5 = 890;
L6 = 1154;
yvals = get(gca,'Ylim');

hold on

plot([L1 L1], yvals, 'r--')
plot([L23 L23], yvals, 'r--')
plot([L4 L4], yvals, 'r--')
plot([L5 L5], yvals, 'r--')




legend({'YFP', 'Chicago blue', 'Effect'})
%% Quantify Area under the curve
interval = depth(2)-depth(1);
area = effect*interval;
startidx = dsearchn(depth,50)+1; %MouseYFP starts from 50 um
TotalArea = sum(area(startidx:end));

LayerBoundaries = [L1; L23; L4; L5];
LayerIdx = zeros(4,1);
for i = 1:4
    LayerIdx(i) = dsearchn(depth, LayerBoundaries(i));
end


LayerIdx = [startidx-1; LayerIdx; 387];

LayerAreas = zeros(5,1);
for i = 1:length(LayerIdx)-1
    LayerAreas(i) = sum(area(LayerIdx(i)+1:LayerIdx(i+1)));
end

Percentage = LayerAreas./TotalArea;
Percentage = Percentage*100


