Skip to content

Commit

Permalink
Trial Multi0cell count display
Browse files Browse the repository at this point in the history
Adjusted 3D display code for displaying multiple cell counter types.
  • Loading branch information
aaronbwong committed Oct 8, 2024
1 parent 5b3b24e commit bea9601
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ data/*
*.backup
gen/*
*.csv
*.asv
5 changes: 4 additions & 1 deletion src/_general/test_histology_pipeline.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,18 @@
% Extract results from ImageJ Cell Counter
% --- Data specific ---
cellcountpath = 'gen\cell-count\output\2023-141-09279\';
slice_path = 'W:\AnalyzedData\Histology\2023-141-09279\downsampledImages\j3_old';
cellcountpath = 'W:\AnalyzedData\Histology\2023-141-09279\CellCounterResults';
slice_path = 'Z:\Falk Bronnle\DATA\Brain 02888-22007\TIFFS 10x\Slices';
cellcountpath = 'Z:\Falk Bronnle\DATA\Brain 02888-22007\TIFFS 10x\Cellcount';
% ---------------------
[histology_points,slice_order] = AW_cellcounter2histology(slice_path,cellcountpath,resize_factor);

% Convert points in histology images to CCF coordinates
ccf_points = AP_histology2ccf(histology_points,slice_path);

% Display points in 3D with brain mesh outline
h_scatter = AW_view_celldistribution_volume(tv,cell2mat(ccf_points));
h_scatter = AW_view_celldistribution_volume(tv,slice_order);


% Concatenate points and round to nearest integer coordinate
Expand Down
55 changes: 43 additions & 12 deletions src/summary/AW_cellcounter2histology.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [histology_points,slice_order] = AW_cellcounter2histology(slice_path,cellcountpath,resize_factor)
function [ccf_points,slice_order] = AW_cellcounter2histology(slice_path,cellcountpath,resize_factor)
%
% slice_path: path to folder that contains
% 1) downsampled images for alignment
Expand All @@ -7,31 +7,62 @@
% 4) atlas2histology_tform.mat: from AP_histology


% load slice_order data
slice_order_fn = [slice_path filesep 'slice_order.csv'];
slice_order = readtable(slice_order_fn);

% load cell counter data
cellcount_path_dir = dir([cellcountpath filesep '*.xml*']);
cellcount_fn = natsortfiles(cellfun(@(path,fn) [path filesep fn], ...
{cellcount_path_dir.folder},{cellcount_path_dir.name},'uni',false));
n_cellcount = length(cellcount_fn);

hist_points = cell(n_cellcount,1);
% preallocate cell arrays
markers = cell(n_cellcount,1);
im_fn = cell(n_cellcount,1);

% loop through all cell counter files
for ii = 1:n_cellcount
[im_fn{ii},hist_points{ii}] = readImageJCellCount(cellcount_fn{ii});
end
[im_fn{ii},markers{ii}] = readImageJCellCount(cellcount_fn{ii});
nTypes = length(markers{ii});

% load slice_order data
slice_order_fn = [slice_path filesep 'slice_order.csv'];
slice_order = readtable(slice_order_fn);
% apply resize factor
for typeIdx = 1:nTypes
markers{ii}(typeIdx).MarkerXY_rz = resize_factor*markers{ii}(typeIdx).MarkerXY;
end

for ii = 1:n_cellcount
% collect metadata (names, type #)
if ii == 1
markerIdx = [markers{ii}.Type];
markerNames = [markers{ii}.Name];
else
markerIdx = union([markers{ii}.Type],markerIdx,'stable');
markerNames = union([markers{ii}.Name],markerNames,'stable');
end

% add cell_counter file name to slice_order table
im_num = find(ismember(slice_order.ori_filename,im_fn{ii}));
% resize_factor = slice_order.resize_factor(ii);
if ~isempty(im_num)
slice_order.histology_points{im_num} = resize_factor*hist_points{ii};
[~,name,ext] = fileparts(cellcount_fn{ii});
slice_order.cellcount_fn{im_num} = [name,ext];
end
end
histology_points = slice_order.histology_points;

% allocate markers XY to table
nTypes = length(markerNames);
for ii = 1:n_cellcount
for tt = 1:nTypes
typeIdx = matches([markers{ii}.Name],markerNames(tt));
slice_order.(markerNames(tt)){ii} = markers{ii}(typeIdx).MarkerXY_rz;
end
end

% converting to CCF markers XY to table
for tt = 1:nTypes
histology_points = slice_order.(markerNames(tt));
ccf_points_temp = AP_histology2ccf(histology_points,slice_path);
slice_order.(markerNames(tt)+"_ccf") = ccf_points_temp;
end

ccf_points = slice_order(:,endsWith(slice_order.Properties.VariableNames,'_ccf'));

end
30 changes: 24 additions & 6 deletions src/summary/AW_view_celldistribution_volume.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
function h_scatter = AW_view_celldistribution_volume(tv,ccf_points_cat)
function h_scatter = AW_view_celldistribution_volume(tv,slice_order)
% AP_view_celldistribution_volume(tv,ccf_points_cat)
%
% Plot histology warped onto CCF volume

ccf_points = slice_order(:,endsWith(slice_order.Properties.VariableNames,'_ccf'));
nTypes = size(ccf_points,2);
cellTypeNames = ccf_points.Properties.VariableNames;
% if nTypes > 1
% disp(ColNames)
% typeIdx = input('which cell type do you want to plot?');
% end


% Create figure
gui_fig = figure;
Expand All @@ -28,11 +36,21 @@

% Draw all datapoints
% ccf_points_cat is in native CCF order [AP/DV/ML]
h_scatter = scatter3(axes_atlas,ccf_points_cat(:,1),... % AP
ccf_points_cat(:,3),... % ML
ccf_points_cat(:,2),... % DV
5,'red','filled','o');

Color = [ 1,0,0;...
0,1,0;...
0,0.5,1;...
1,0,1;];
MrkrSz = 10;
for typeIdx = 1:nTypes
ccf_points_cat = cell2mat(ccf_points.(cellTypeNames{typeIdx}));
h_scatter(typeIdx) = scatter3(axes_atlas,ccf_points_cat(:,1),... % AP
ccf_points_cat(:,3),... % ML
ccf_points_cat(:,2),... % DV
MrkrSz,Color(typeIdx,:),'filled','o');
end

legend(["Brain",cellTypeNames],'Interpreter','none');
end


% % Attempt plotting as 3D surface
Expand Down
59 changes: 38 additions & 21 deletions src/summary/readImageJCellCount.m
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
function [im_fn,MarkerXY] = readImageJCellCount(filename,typeIdx)
function [im_fn,Markers] = readImageJCellCount(filename)

%% load XML

S = readstruct(filename);
% data structure
% S -> ImageProperties
% ->
% ->
% S.Marker_Data.Marker_Type: struct array of different marker types
% S.Marker_Data.Marker_Type.Marker: 1xn struct array with fields:
% MarkerX
% MarkerY
% MarkerZ
if ~exist('typeIdx','var') || isempty(typeIdx);typeIdx = 1;end

im_fn = S.Image_Properties.Image_Filename;
if isfield(S.Marker_Data.Marker_Type,'Marker')
MarkerXY = [ [S.Marker_Data.Marker_Type(typeIdx).Marker.MarkerX]',...
[S.Marker_Data.Marker_Type(typeIdx).Marker.MarkerY]' ];
else
MarkerXY = NaN(0,2);
%% load XML

S = readstruct(filename);
% data structure
% S -> ImageProperties
% ->
% ->
% S.Marker_Data.Marker_Type: struct array of different marker types
% S.Marker_Data.Marker_Type.Marker: 1xn struct array with fields:
% MarkerX
% MarkerY
% MarkerZ

im_fn = S.Image_Properties.Image_Filename;

Markers = struct;
nTypes = length(S.Marker_Data.Marker_Type);
for typeIdx = 1:nTypes
Markers(typeIdx).Type = S.Marker_Data.Marker_Type(typeIdx).Type;
if isfield(S.Marker_Data.Marker_Type,'Name')
Markers(typeIdx).Name = S.Marker_Data.Marker_Type(typeIdx).Name;
else
Markers(typeIdx).Name = "";
end
Markers(typeIdx).MarkerXY = readCellType(S,typeIdx);
end
end

function MarkerXY = readCellType(S,typeIdx)
if ~exist('typeIdx','var') || isempty(typeIdx);typeIdx = 1;end
if isfield(S.Marker_Data.Marker_Type,'Marker') && ...
~all(ismissing(S.Marker_Data.Marker_Type(typeIdx).Marker))
MarkerXY = [ [S.Marker_Data.Marker_Type(typeIdx).Marker.MarkerX]',...
[S.Marker_Data.Marker_Type(typeIdx).Marker.MarkerY]' ];
else
MarkerXY = NaN(0,2);
end
end

0 comments on commit bea9601

Please sign in to comment.