clc
clearvars

%unique id for this experiment UPDATE each time
UUID = 'VGT_test';
filename = 'filt_neurons.mat'; %UPDATE this for each experiment. Use the bigger version, not small


%%
load(filename)

output_dir=fullfile('figs');
mkdir(output_dir)


%% Make png files for each slice in the dataset, YFP+ cells

gene_to_plot = 'YFP'; %now this is YFP, but maybe change this for other genetic lines or viruses
gene_num = find(ismember(filt_neurons.genes(:,1),gene_to_plot)); 

cutoff = 2; %default is 2, cutoff for considering a cell positive

slices = unique(filt_neurons.slice);

%Find distribution of positive cell counts, find scale for plotting
counts = nonzeros(filt_neurons.expmat(filt_neurons.expmat(:,gene_num)>= cutoff,gene_num));

figure;
hold on;
histogram(counts);
%prettyFig();
ylabel('Number of neurons');
xlabel('YFP counts per positive cell');
title(['Total Positive Cells = ', num2str(numel(counts))])

exportgraphics(gcf,fullfile(output_dir,[UUID,'_Histogram_positive_cellcounts.png']),'ContentType','Image','Resolution',300);
close all;

%80th percentile for max plotting value
max_scale = prctile(counts,80);

pos_for_gene_counts = filt_neurons.expmat(:,gene_num);
pos_for_gene_counts = full(pos_for_gene_counts);
pos_for_gene_counts(pos_for_gene_counts==0)=nan;

%% 
for s = 1:numel(slices)
    figure;
    hold on;
    in_slice=filt_neurons.slice==slices(s);

%    plot all neurons in grey
    scatter(filt_neurons.pos(in_slice,1), ...
    filt_neurons.pos(in_slice,2), ...
    2, ...
    [0.8,0.8,0.8],...
    'filled' ...
    );
    xl=xlim;
    yl=ylim;
    set(gca, ...
        'ydir','reverse' ...
        );
    axis off
     pbaspect([range(xl),range(yl),1]);
     
      %plot all positive cells
        scatter(filt_neurons.pos(in_slice,1), ...
        filt_neurons.pos(in_slice,2), ...
        2, ...
        pos_for_gene_counts(in_slice),...
        'filled', ...
         'MarkerFaceAlpha', 0.5,...
        'MarkerEdgeAlpha', 0.5...
            );

        xl=xlim;
        yl=ylim;
        set(gca, ...
            'ydir','reverse' ...
            );
        axis off
         pbaspect([range(xl),range(yl),1]);

     colormap(gca,flipud(copper));
     %colormap('cool')
     clim([cutoff-1 max_scale]);
     colorbar
     title([gene_to_plot,' counts']);
        
      exportgraphics(gcf,fullfile(output_dir,[UUID,'_Slice_',num2str(s),'_', gene_to_plot,'_counts.png']),'ContentType','Image','Resolution',300);
%       print(gcf,fullfile(output_dir,[UUID,'_Slice_',num2str(s),'_', gene_to_plot,'_counts.svg']),'-dsvg');
       close all;
    
end
