Tree Gating and Visualisation

1 Overview

This vignette demonstrates an interpretable tree-gating workflow on one complete image from cytoGateR_example. A small two-lineage marker table keeps the build fast while exercising the same functions used by larger analyses.

library(cytoGateR)
data("cytoGateR_example", package = "cytoGateR")

image_ids <- SummarizedExperiment::colData(cytoGateR_example)$image_name
spe <- cytoGateR_example[, image_ids == image_ids[1L]]
spe
class: SpatialExperiment 
dim: 40 4363 
metadata(0):
assays(1): exprs
rownames(40): MPO H3 ... DNA1 DNA2
rowData names(0):
colnames(4363): 12_1 12_2 ... 12_4371 12_4372
colData names(28): sample_id image_name ... KNN_P_Treg KNN_P_Tumor
reducedDimNames(0):
mainExpName: IMMUcan_2022_CancerExample_v1
altExpNames(0):
spatialCoords names(2) : cell_x cell_y
imgData names(1): sample_id

2 Define a lineage table

Each row defines a cell type using positive and negative protein markers. Marker columns are list-columns so that each cell type may use a different number of markers.

lineage_table <- data.frame(
  cell_type = c("Tcell", "Bcell"),
  pos_markers = I(list("CD3e", "CD20")),
  neg_markers = I(list("CD20", "CD3e"))
)
lineage_table
  cell_type pos_markers neg_markers
1     Tcell        CD3e        CD20
2     Bcell        CD20        CD3e

3 Fit tree-gating models

For package-build speed, the example limits each tree to one split. Larger analyses can increase max_depth and use more detailed marker definitions.

tree_result <- run_tree_gating(
  spe,
  lineage_table,
  assay_name = "exprs",
  max_depth = 1,
  min_cells = 100,
  min_score = 0.1,
  uncert_thresh = 0.25,
  BPPARAM = BiocParallel::SerialParam()
)

The result contains the updated object, fitted trees, probability matrix, hard labels, and cleaned lineage table:

names(tree_result)
[1] "spe"           "trees"         "prob_mat"      "hard_label"   
[5] "lineage_table"
head(tree_result$prob_mat)
         Tcell      Bcell
[1,] 0.2420126 0.10872233
[2,] 0.1044628 0.20912039
[3,] 0.1894016 0.19044784
[4,] 0.2154774 0.14173300
[5,] 0.2096689 0.14963870
[6,] 0.2575994 0.07940613
table(tree_result$hard_label)

    Bcell     Tcell Uncertain 
      101       686      3576 

4 Inspect the fitted trees

print_celltype_tree(tree_result$trees$Tcell)
[depth 0] CD3e > 1.565  (sep = 2.78, n = 4363)
 |- low
 |  [leaf] depth =1 n_cells =1491
 `- high
    [leaf] depth =1 n_cells =2872
plot_celltype_tree(tree_result$trees$Tcell)

5 Select high-confidence cells

apply_cutoff_labels() applies a cell-type-specific probability cutoff. A cell receives a reference label only when exactly one cell type passes its cutoff.

core_result <- apply_cutoff_labels(
  tree_result,
  cutoff_fn = function(x) stats::quantile(x, 0.95, na.rm = TRUE),
  label_col = "core_group",
  unknown_label = "Unknown"
)

table(SummarizedExperiment::colData(core_result$spe)$core_group)

  Bcell   Tcell Unknown 
    219     219    3925 

6 Visualise expression and annotations

Marker distributions help assess whether the selected markers separate the expected populations.

plot_marker_density(
  core_result$spe,
  marker = "CD3e",
  assay_name = "exprs",
  image_col = "image_name"
)

The gated labels can also be displayed in spatial coordinates:

plot_labelled_cells(
  core_result$spe,
  col_label = "core_group",
  image_col = "image_name"
)

7 Visualise stored full-workflow probabilities

The packaged object contains probabilities from the full ten-lineage demo workflow. These provide a richer example for probability visualisation without rerunning the complete model during vignette construction.

plot_probability_map(
  spe,
  cell_type = "Bcell",
  image_col = "image_name"
)

plot_label_counts(
  spe,
  label_col = "knn_label"
)

8 Session information

sessionInfo()
R version 4.6.2 (2026-10-29)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 26.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] cytoGateR_0.99.1

loaded via a namespace (and not attached):
 [1] SummarizedExperiment_1.45.0 gtable_0.3.6               
 [3] rjson_0.2.23                xfun_0.63                  
 [5] ggplot2_4.0.5               ggrepel_0.9.8              
 [7] Biobase_2.75.1              lattice_0.23-1             
 [9] vctrs_0.7.3                 tools_4.6.2                
[11] generics_0.1.4              parallel_4.6.2             
[13] stats4_4.6.2                proxy_0.4-29               
[15] tibble_3.3.1                pkgconfig_2.0.3            
[17] Matrix_1.7-6                RColorBrewer_1.1-3         
[19] pals_1.10                   S7_1.0.0                   
[21] S4Vectors_0.53.1            lifecycle_1.0.5            
[23] compiler_4.6.2              farver_2.1.2               
[25] ggforce_0.6.0               mapproj_1.2.12             
[27] codetools_0.2-20            Seqinfo_1.5.0              
[29] graphlayouts_1.3.0          maps_3.4.4                 
[31] htmltools_0.5.9             sys_3.4.3                  
[33] buildtools_1.0.0            yaml_2.3.12                
[35] pillar_1.11.1               tidyr_1.3.2                
[37] MASS_7.3-66                 BiocParallel_1.49.0        
[39] SingleCellExperiment_1.37.1 cachem_1.1.0               
[41] DelayedArray_0.41.0         dbscan_1.3.0               
[43] viridis_0.6.5               magick_2.9.1               
[45] abind_1.4-8                 mclust_6.1.4               
[47] tidyselect_1.2.1            digest_0.6.39              
[49] dplyr_1.2.1                 purrr_1.2.2                
[51] labeling_0.4.3              maketools_1.3.2            
[53] polyclip_1.10-7             fastmap_1.2.0              
[55] grid_4.6.2                  colorspace_2.1-4           
[57] cli_3.6.6                   SparseArray_1.15.0         
[59] magrittr_2.0.5              S4Arrays_1.15.0            
[61] dichromat_2.0-1             ggraph_2.2.2               
[63] tidygraph_1.3.1             withr_3.0.3                
[65] scales_1.4.0                rmarkdown_2.32             
[67] XVector_0.55.0              matrixStats_1.5.0          
[69] igraph_2.3.4                otel_0.3.0                 
[71] gridExtra_2.3.1             SpatialExperiment_1.25.0   
[73] memoise_2.0.1               evaluate_1.0.5             
[75] knitr_1.52                  GenomicRanges_1.67.0       
[77] IRanges_2.49.0              viridisLite_0.4.3          
[79] rlang_1.3.0                 Rcpp_1.1.2                 
[81] glue_1.8.1                  tweenr_2.0.3               
[83] BiocGenerics_0.61.1         jsonlite_2.0.0             
[85] R6_2.6.1                    MatrixGenerics_1.27.0