%% Too many figs, this one wasn't used
% Barcode Statistics and QC

% First, histograms!
% re-run with unmerged and allmerged for prepostQC
bcodeGen1=barcodeGen_allmerged;
bcodeGen2=barcodeGen_allmerged_scer;

% Extract lengths
lens1 = cellfun(@(s) numel(s.rawBarcode), bcodeGen1);
lens2 = cellfun(@(s) numel(s.rawBarcode), bcodeGen2);

% Define bin edges based on bcodeGen1
binEdges = min(lens1):range(lens1)/15:max(lens1);  % 30 bins by default
binEdges2 = min(lens2):range(lens2)/15:max(lens2);  % 30 bins by default
%binEdges3 = 110:50:860;
% (You can change 30 to adjust bin resolution)

% Figure with tiled layout
figure('Color','w');
t = tiledlayout(2,1); % 2 rows, 1 column
xlabel(t, 'Barcode length');
ylabel(t, 'Count');

% Histogram for bcodeGen1
ax1 = nexttile;
histogram(lens1, 'FaceColor', [0.5 0.5 0.5], 'BinEdges', binEdges);
hold on
xline(mean(lens1), 'r', 'LineWidth', 2); % vertical line at mean
ylabel('Count');
title('bcodeGen1');
grid on

% Histogram for bcodeGen2
ax2 = nexttile;
histogram(lens2, 'FaceColor', [0.5 0.5 0.5], 'BinEdges', binEdges2);
hold on
xline(mean(lens2), 'r', 'LineWidth', 2); % vertical line at mean
ylabel('Count');
title('bcodeGen2');
grid on

% Match X axes
linkaxes([ax1, ax2], 'x');


%% This is an unused piechart with the types of barcodes
% Data
labels = {'Multi FoV','Semi repetitive','Full repetitive','Regular'};

counts1 = [65 37 119 881];
counts2 = [84 8 11 448];

% Pie charts
figure('Color','w','Position',[100 100 900 400]);

subplot(1,2,1)
pie(counts1);
title(sprintf('bcodeGen1 (n=%d)', sum(counts1)));
legend(labels,'Location','eastoutside')

subplot(1,2,2)
pie(counts2);
title(sprintf('bcodeGen2 (n=%d)', sum(counts2)));

% Column chart alternative
figure('Color','w','Position',[100 100 600 400]);

counts = [counts1; counts2];
bar(counts','stacked'); % or bar(counts') for grouped bars
set(gca,'XTickLabel',labels,'XTickLabelRotation',30)
ylabel('Counts')
legend({'bcodeGen1','bcodeGen2'},'Location','northwest')
title('Category distribution')

%% This is E. barchart with the types of barcodes
labels = {'Regular','Full repetitive','Semi repetitive'}; % reversed

%[~,~,~,~] = periodic_finder(barcodeGen_allmerged_fixed,3);
%[~,~,~,~] = periodic_finder(barcodeGen_allmerged_scer,3);

% These were obtained by repfinder and counting multifov size
% Multifov must be removed, since its not a "type" of barcodes

counts1 = [820 108 41];   % reversed
counts2 = [449 13 33];     % reversed


% Normalize to percentages
counts = [counts1; counts2];
perc = counts ./ sum(counts,2) * 100;  % each row normalized to %

% 100% stacked bar chart
figure('Color','w','Position',[100 100 500 400]);
bar(perc,'stacked')
set(gca,'XTickLabel',{'bcodeGen1','bcodeGen2'})
ylabel('Percentage (%)')
legend(labels,'Location','eastoutside')
ylim([0 100])
title('Category distribution (normalized)')

