% match barseq to mapseq
clearvars
clc
load filt_neurons-bc-somas-proj-axonal-clust_nocompr.mat

%

% read projection matrix
t=readtable('YW144.nbcm.tsv', ...
    'FileType','delimitedtext');
t
% truncate barcoders
insitu_length=size(filt_neurons.soma_bc,2);
map_bc=char(t.vbc_read_col);
map_bc=map_bc(:,1:insitu_length);

% translate to double
code='GTAC';
[~,map_bc]=ismember(map_bc,code);

barcoded_idx=find(filt_neurons.is_barcoded);

%

% ideally this should be done in a matrix, but it takes a lot of ram. Break
% this up in a for loop
mismatch_tolerance=1;

projmat=zeros(numel(filt_neurons.id),size(t,2)-1);
for nn=1:sum(filt_neurons.is_barcoded)
    d=pdist2(filt_neurons.soma_bc(barcoded_idx(nn),:),map_bc,'hamming');
    [~,match_idx]=min(d);
    if min(d)<=mismatch_tolerance&&sum(d==min(d))==1 % within mismatch tolerance and unique match
        projmat(barcoded_idx(nn),:)=t{match_idx,2:end};
    end
end
% size(d)

projmat
imagesc(log10(projmat));
colorbar;

