% 2022-02-04
% Plot manually-counted nuclei in IHC images from xlsx table
nuclei = "D:\Harvard\Manuscripts\Summit paper\Figures\Figure 5\B & C\Fig 5 - Fungal nuclei counts.xlsx";

sheets = {'8102021-Brain1-AL up','8102021-Brain2-AL down','8102021-Brain3-AL up'};
dir = "D:\Harvard\Manuscripts\Summit paper\Figures\Figure 5\";

colors =[         0         0    1.0000;...
    1.0000         0         0;...
         0    1.0000         0;...
         0         0    0.1724;...
    1.0000    0.1034    0.7241;...
    1.0000    0.8276         0;...
         0    0.3448         0;...
    0.5172    0.5172    1.0000;...
    0.6207    0.3103    0.2759;...
         0    1.0000    0.7586];
     
[neuropilCts,symScore] = deal(NaN(3,10));
hemiCtArray = NaN(3,2,10);

for j=1:numel(sheets)
    [num, txt, raw] = xlsread(nuclei,sheets{j});

    numSlices = size(num,1)-4;

    %% Total nuclei counted

    totNuclei = sum(nansum(num(1:numSlices,2:size(num,2))));

    %% Plot 2D scatter - neuropil total vs. nuclei count per plane    
    idx = 2:2:size(num,2);
    
    for i = 1:numel(idx)
        cts = reshape(num(1:numSlices, idx(i):idx(i)+1),1,2*numSlices);
        normCts = cts/totNuclei;
        neuropilCts(j,i) = nanmean(normCts);
    end
end

%% Plot average neuropiil occupancy across 3 samples, per neuropil per brain 
% % Fig 5C

figure;
hold on;

wholeBrainCts = neuropilCts;

for i=1:3
    wholeBrainCts(i,:) = neuropilCts(i,:)/sum(neuropilCts(i,:)); 
end

% Re-ordering neuropil for plot roughly from lateral to medial
reorder = [10,9,6,4,7,3,1,5,2,8];

reorderCts = wholeBrainCts(:,reorder);
for i=1:10
    scatter(repmat(i,1,3),reorderCts(:,i),'MarkerFaceColor','k','MarkerEdgeColor','none','MarkerFaceAlpha',0.5);
end

ylim([0 0.45]);
xlabels = txt(1,idx);
xlabels = xlabels(reorder);
set(gca,'xtick',1:10,'xticklabel',xlabels);

ylabel('Fraction of fungal nuclei');
xlabel('Neuropil');
xlim([0.5 10.5]);
ylim([0 0.6]);

set(gca,'TickLength',[0 0],'FontSize',18);
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 5.5 4]);
xtickangle(45);

print(strcat(dir,'Mean neuropil occupancy across samples_whole brain.png'),'-dpng');
print(strcat(dir,'Mean neuropil occupancy across samples_whole brain.pdf'),'-dpdf');
close();