# Domain segmentation (STARmap PLUS mouse brain)

Here, we demonstrate BANKSY domain segmentation on a STARmap PLUS dataset of the mouse brain from Shi et al. (2022).

    library(Banksy)

    library(data.table)
    library(SummarizedExperiment)
    library(SpatialExperiment)

    library(scater)
    library(cowplot)
    library(ggplot2)

# Data preprocessing

Data from the study is available from the Single Cell Portal. We analyze data from `well11`. The data comprise 1,022 genes profiled at subcellular resolution in 43,341 cells.

    #' Change paths accordingly
    gcm_path <- "../data/well11processed_expression_pd.csv.gz"
    mdata_path <- "../data/well11_spatial.csv.gz"

    #' Gene cell matrix
    gcm <- fread(gcm_path)
    genes <- gcm$GENE
    gcm <- as.matrix(gcm[, -1])
    rownames(gcm) <- genes

    #' Spatial coordinates and metadata
    mdata <- fread(mdata_path, skip = 1)
    headers <- names(fread(mdata_path, nrows = 0))
    colnames(mdata) <- headers
    #' Orient spatial coordinates
    xx <- mdata$X
    yy <- mdata$Y
    mdata$X <- max(yy) - yy
    mdata$Y <- max(xx) - xx
    mdata <- data.frame(mdata)
    rownames(mdata) <- colnames(gcm)

    locs <- as.matrix(mdata[, c("X", "Y", "Z")])

    #' Create SpatialExperiment
    se <- SpatialExperiment(
        assay = list(processedExp = gcm),
        spatialCoords = locs,
        colData = mdata
    )

# Running BANKSY

Run BANKSY in domain segmentation mode with `lambda=0.8`. This places larger weights on the mean neighborhood expression and azimuthal Gabor filter in constructing the BANKSY matrix. We adjust the resolution to yield 23 clusters based on the results from Maher et al. (2023) (see Fig. 1, 2).

> Note that the parameter values for domain segmentation for datasets generated using the older Visium v1 / v2 55um technologies are `lambda = 0.2` and `k_geom = 18`. See the note in the tutorial on the main page for more details.

    lambda <- 0.8
    k_geom <- 30
    npcs <- 50
    aname <- "processedExp"
    se <- Banksy::computeBanksy(se, assay_name = aname, k_geom = k_geom)

    set.seed(1000)
    se <- Banksy::runBanksyPCA(se, lambda = lambda, npcs = npcs)

    set.seed(1000)
    se <- Banksy::clusterBanksy(se, lambda = lambda, npcs = npcs, resolution = 0.8)

Cluster labels are stored in the `colData` slot:

    head(colData(se))
    #> DataFrame with 6 rows and 4 columns
    #>           X         Y clust_M1_lam0.8_k50_res0.8   sample_id
    #>   <numeric> <numeric>                   <factor> <character>
    #> 1   24225.5   23984.2                         10    sample01
    #> 2   24849.2   22679.1                         10    sample01
    #> 3   24488.3   22970.3                         10    sample01
    #> 4   24371.4   23727.5                         10    sample01
    #> 5   24362.2   23300.6                         10    sample01
    #> 6   24644.5   23112.8                         10    sample01

Visualize clustering results:

    cnames <- colnames(colData(se))
    cnames <- cnames[grep("^clust", cnames)]

    plotColData(se, x = "X", y = "Y", point_size = 0.01, colour_by = cnames[1]) +
        scale_color_manual(values = pals::glasbey()) +
        coord_equal() +
        theme(legend.position = "none")

# Session information

    sessionInfo()
    #> R version 4.5.2 (2025-10-31)
    #> Platform: x86_64-pc-linux-gnu
    #> Running under: Ubuntu 24.04.3 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.26.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=C
    #>  [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] stats4    stats     graphics  grDevices utils     datasets  methods
    #> [8] base
    #>
    #> other attached packages:
    #>  [1] ExperimentHub_3.1.0         AnnotationHub_4.1.0
    #>  [3] BiocFileCache_3.1.0         dbplyr_2.5.1
    #>  [5] spatialLIBD_1.23.0          cowplot_1.2.0
    #>  [7] scater_1.39.1               ggplot2_4.0.1
    #>  [9] harmony_1.2.4               Rcpp_1.1.0.8.2
    #> [11] data.table_1.18.0           scran_1.39.0
    #> [13] scuttle_1.21.0              Seurat_5.4.0
    #> [15] SeuratObject_5.3.0          sp_2.2-0
    #> [17] SpatialExperiment_1.21.0    SingleCellExperiment_1.33.0
    #> [19] SummarizedExperiment_1.41.0 Biobase_2.71.0
    #> [21] GenomicRanges_1.63.1        Seqinfo_1.1.0
    #> [23] IRanges_2.45.0              S4Vectors_0.49.0
    #> [25] BiocGenerics_0.57.0         generics_0.1.4
    #> [27] MatrixGenerics_1.23.0       matrixStats_1.5.0
    #> [29] Banksy_1.7.0                BiocStyle_2.39.0
    #>
    #> loaded via a namespace (and not attached):
    #>   [1] bitops_1.0-9             spatstat.sparse_3.1-0    httr_1.4.7
    #>   [4] RColorBrewer_1.1-3       doParallel_1.0.17        tools_4.5.2
    #>   [7] sctransform_0.4.2        DT_0.34.0                R6_2.6.1
    #>  [10] lazyeval_0.2.2           uwot_0.2.4               GetoptLong_1.1.0
    #>  [13] withr_3.0.2              gridExtra_2.3            progressr_0.18.0
    #>  [16] cli_3.6.5                spatstat.explore_3.6-0   fastDummies_1.7.5
    #>  [19] labeling_0.4.3           sass_0.4.10              S7_0.2.1
    #>  [22] spatstat.data_3.1-9      ggridges_0.5.7           pbapply_1.7-4
    #>  [25] Rsamtools_2.27.0         dbscan_1.2.4             aricode_1.0.3
    #>  [28] dichromat_2.0-0.1        sessioninfo_1.2.3        parallelly_1.46.1
    #>  [31] attempt_0.3.1            maps_3.4.3               limma_3.67.0
    #>  [34] pals_1.10                RSQLite_2.4.5            BiocIO_1.21.0
    #>  [37] shape_1.4.6.1            ica_1.0-3                spatstat.random_3.4-3
    #>  [40] dplyr_1.1.4              Matrix_1.7-4             ggbeeswarm_0.7.3
    #>  [43] abind_1.4-8              lifecycle_1.0.5          yaml_2.3.12
    #>  [46] edgeR_4.9.2              SparseArray_1.11.10      Rtsne_0.17
    #>  [49] paletteer_1.7.0          grid_4.5.2               blob_1.2.4
    #>  [52] promises_1.5.0           dqrng_0.4.1              crayon_1.5.3
    #>  [55] miniUI_0.1.2             lattice_0.22-7           beachmat_2.27.1
    #>  [58] cigarillo_1.1.0          mapproj_1.2.12           KEGGREST_1.51.1
    #>  [61] magick_2.9.0             sys_3.4.3                maketools_1.3.2
    #>  [64] pillar_1.11.1            knitr_1.51               ComplexHeatmap_2.27.0
    #>  [67] metapod_1.19.1           rjson_0.2.23             future.apply_1.20.1
    #>  [70] codetools_0.2-20         glue_1.8.0               spatstat.univar_3.1-5
    #>  [73] vctrs_0.6.5              png_0.1-8                spam_2.11-3
    #>  [76] gtable_0.3.6             rematch2_2.1.2           cachem_1.1.0
    #>  [79] xfun_0.55                S4Arrays_1.11.1          mime_0.13
    #>  [82] survival_3.8-3           RcppHungarian_0.3        iterators_1.0.14
    #>  [85] statmod_1.5.1            bluster_1.21.0           fitdistrplus_1.2-4
    #>  [88] ROCR_1.0-11              nlme_3.1-168             bit64_4.6.0-1
    #>  [91] filelock_1.0.3           RcppAnnoy_0.0.22         bslib_0.9.0
    #>  [94] irlba_2.3.5.1            vipor_0.4.7              KernSmooth_2.23-26
    #>  [97] otel_0.2.0               colorspace_2.1-2         DBI_1.2.3
    #> [100] tidyselect_1.2.1         bit_4.6.0                compiler_4.5.2
    #> [103] curl_7.0.0               httr2_1.2.2              BiocNeighbors_2.5.0
    #> [106] DelayedArray_0.37.0      plotly_4.11.0            rtracklayer_1.71.3
    #> [109] scales_1.4.0             lmtest_0.9-40            rappdirs_0.3.3
    #> [112] stringr_1.6.0            digest_0.6.39            goftest_1.2-3
    #> [115] spatstat.utils_3.2-0     rmarkdown_2.30           benchmarkmeData_1.0.4
    #> [118] RhpcBLASctl_0.23-42      XVector_0.51.0           htmltools_0.5.9
    #> [121] pkgconfig_2.0.3          fastmap_1.2.0            GlobalOptions_0.1.3
    #> [124] rlang_1.1.7              htmlwidgets_1.6.4        shiny_1.12.1
    #> [127] farver_2.1.2             jquerylib_0.1.4          zoo_1.8-15
    #> [130] jsonlite_2.0.0           BiocParallel_1.45.0      mclust_6.1.2
    #> [133] config_0.3.2             RCurl_1.98-1.17          BiocSingular_1.27.1
    #> [136] magrittr_2.0.4           dotCall64_1.2            patchwork_1.3.2
    #> [139] viridis_0.6.5            reticulate_1.44.1        leidenAlg_1.1.5
    #> [142] stringi_1.8.7            MASS_7.3-65              plyr_1.8.9
    #> [145] parallel_4.5.2           listenv_0.10.0           ggrepel_0.9.6
    #> [148] deldir_2.0-4             Biostrings_2.79.4        sccore_1.0.6
    #> [151] splines_4.5.2            tensor_1.5.1             circlize_0.4.17
    #> [154] locfit_1.5-9.12          igraph_2.2.1             spatstat.geom_3.6-1
    #> [157] RcppHNSW_0.6.0           buildtools_1.0.0         reshape2_1.4.5
    #> [160] ScaledMatrix_1.19.0      XML_3.99-0.20            BiocVersion_3.23.1
    #> [163] evaluate_1.0.5           golem_0.5.1              BiocManager_1.30.27
    #> [166] foreach_1.5.2            httpuv_1.6.16            RANN_2.6.2
    #> [169] tidyr_1.3.2              purrr_1.2.1              polyclip_1.10-7
    #> [172] benchmarkme_1.0.8        clue_0.3-66              future_1.68.0
    #> [175] scattermore_1.2          rsvd_1.0.5               xtable_1.8-4
    #> [178] restfulr_0.0.16          RSpectra_0.16-2          later_1.4.5
    #> [181] viridisLite_0.4.2        tibble_3.3.0             GenomicAlignments_1.47.0
    #> [184] memoise_2.0.1            beeswarm_0.4.0           AnnotationDbi_1.73.0
    #> [187] cluster_2.1.8.1          shinyWidgets_0.9.0       globals_0.18.0