# Multi-sample analysis (10x Visium Human DLPFC)

#### 4 June 2025

# Contents

Here, we demonstrate BANKSY analysis on 10x Visium data of the human dorsolateral prefrontal cortex from Maynard et al (2018). The data comprise 12 samples obtained from 3 subjects, with manual annotation of the layers in each sample. We will focus on 4 of the 12 samples from subject 3, demonstrating multi-sample analysis with BANKSY.

    library(Banksy)
    library(SummarizedExperiment)
    library(SpatialExperiment)
    library(Seurat)

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

# 1 Loading the data

We fetch the data for all 12 DLPFC samples with the spatialLIBD package. This might take awhile.

    library(spatialLIBD)
    library(ExperimentHub)

    ehub <- ExperimentHub::ExperimentHub()
    spe <- spatialLIBD::fetch_data(type = "spe", eh = ehub)

After the download is completed, we trim the SpatialExperiment object, retaining only the counts and some metadata such as the sample identifier and pathology annotations. This saves some memory.

    imgData(spe) <- NULL
    assay(spe, "logcounts") <- NULL
    reducedDims(spe) <- NULL
    rowData(spe) <- NULL
    colData(spe) <- DataFrame(
        sample_id = spe$sample_id,
        clust_annotation = factor(
            addNA(spe$layer_guess_reordered_short),
            exclude = NULL, labels = seq(8)
        ),
        in_tissue = spe$in_tissue,
        row.names = colnames(spe)
    )
    invisible(gc())

Next, subset `spe` to samples from the last subject (samples `151673`, `151674`, `151675`, `151676`). This stores each sample in its own SpatialExperiment object, all placed in a list.

    sample_names <- as.character(151673:151676)
    spe_list <- lapply(sample_names, function(x) spe[, spe$sample_id == x])
    rm(spe)
    invisible(gc())

# 2 Data preprocessing

Using Seurat, we perform basic normalisation of the data, and select the top 2000 highly variable features from each sample. Other methods for normalisation and feature selection may also be used. We take the union of these features for downstream analysis.

    #' Normalize data
    seu_list <- lapply(spe_list, function(x) {
        x <- as.Seurat(x, data = NULL)
        NormalizeData(x, scale.factor = 5000, normalization.method = 'RC')
    })

    #' Compute HVGs
    hvgs <- lapply(seu_list, function(x) {
        VariableFeatures(FindVariableFeatures(x, nfeatures = 2000))
    })
    hvgs <- Reduce(union, hvgs)

    #' Add data to SpatialExperiment and subset to HVGs
    aname <- "normcounts"
    spe_list <- Map(function(spe, seu) {
        assay(spe, aname) <- GetAssayData(seu)
        spe[hvgs,]
        }, spe_list, seu_list)
    rm(seu_list)
    invisible(gc())

# 3 Running BANKSY

To run BANKSY across multiple samples, we first compute the BANKSY neighborhood feature matrices for each sample separately. We use `k_geom=6` corresponding to the first-order neighbors in 10x Visium assays (`k_geom=18` corresponding to first and second-order neighbors may also be used).

    compute_agf <- FALSE
    k_geom <- 6
    spe_list <- lapply(spe_list, computeBanksy, assay_name = aname,
                       compute_agf = compute_agf, k_geom = k_geom)

We then merge the samples to perform joint dimensional reduction and clustering:

    spe_joint <- do.call(cbind, spe_list)
    rm(spe_list)
    invisible(gc())

When running multi-sample BANKSY PCA, the `group` argument may be provided. This specifies the grouping variable for the cells or spots across the samples. Features belonging to cells or spots corresponding to each level of the grouping variable will be z-scaled separately. In this case, `sample_id` in `colData(spe_joint)` gives the grouping based on the sample of origin.

    lambda <- 0.2
    use_agf <- FALSE
    spe_joint <- runBanksyPCA(spe_joint, use_agf = use_agf, lambda = lambda, group = "sample_id", seed = 1000)

Run UMAP on the BANKSY embedding:

    spe_joint <- runBanksyUMAP(spe_joint, use_agf = use_agf, lambda = lambda, seed = 1000)

Finally, we obtain cluster labels for spots across all 4 samples. We use `connectClusters` for visual comparison of the manual annotations and BANKSY clusters.

    res <- 0.7
    spe_joint <- clusterBanksy(spe_joint, use_agf = use_agf, lambda = lambda, resolution = res, seed = 1000)
    cnm <- sprintf("clust_M%s_lam%s_k50_res%s", as.numeric(use_agf), lambda, res)
    spe_joint <- connectClusters(spe_joint)

Once joint clustering is performed, we split the samples into their own `SpatialExperiment` objects:

    spe_list <- lapply(sample_names, function(x) spe_joint[, spe_joint$sample_id == x])
    rm(spe_joint)
    invisible(gc())

As an optional step, we smooth the cluster labels of each sample separately. This can be done if smooth spatial domains are expected in the biological sample or tissue in question.

    spe_list <- lapply(spe_list, smoothLabels, cluster_names = cnm, k = 6L, verbose = FALSE)
    names(spe_list) <- paste0("sample_", sample_names)

The raw and smoothed cluster labels are stored in the `colData` slot of each `SingleCellExperiment` or `SpatialExperiment` object.

    #> DataFrame with 6 rows and 5 columns
    #>                      sample_id clust_annotation in_tissue
    #>                    <character>         <factor> <logical>
    #> AAACAAGTATCTCCCA-1      151673                3      TRUE
    #> AAACAATCTACTAGCA-1      151673                1      TRUE
    #> AAACACCAATAACTGC-1      151673                7      TRUE
    #> AAACAGAGCGACTCCT-1      151673                3      TRUE
    #> AAACAGCTTTCAGAAG-1      151673                5      TRUE
    #> AAACAGGGTCTATATT-1      151673                6      TRUE
    #>                    clust_M0_lam0.2_k50_res0.7 clust_M0_lam0.2_k50_res0.7_smooth
    #>                                      <factor>                          <factor>
    #> AAACAAGTATCTCCCA-1                          2                                 3
    #> AAACAATCTACTAGCA-1                          1                                 1
    #> AAACACCAATAACTGC-1                          7                                 7
    #> AAACAGAGCGACTCCT-1                          3                                 3
    #> AAACAGCTTTCAGAAG-1                          6                                 6
    #> AAACAGGGTCTATATT-1                          4                                 4

# 4 Parsing BANKSY output

We can compare BANKSY clusters to pathology annotations using several cluster comparison measures such as the adjusted Rand index (ARI) or normalized mutual information (NMI) with `compareClusters`. The function computes the selected comparison measure between all pairs of cluster labels:

    compareClusters(spe_list$sample_151673, func = 'ARI')
    #>                                   clust_annotation clust_M0_lam0.2_k50_res0.7
    #> clust_annotation                             1.000                      0.543
    #> clust_M0_lam0.2_k50_res0.7                   0.543                      1.000
    #> clust_M0_lam0.2_k50_res0.7_smooth            0.558                      0.890
    #>                                   clust_M0_lam0.2_k50_res0.7_smooth
    #> clust_annotation                                              0.558
    #> clust_M0_lam0.2_k50_res0.7                                    0.890
    #> clust_M0_lam0.2_k50_res0.7_smooth                             1.000

We evaluate the ARI and NMI for each sample:

    ari <- sapply(spe_list, function(x) as.numeric(tail(compareClusters(x, func = "ARI")[, 1], n = 1)))
    ari
    #> sample_151673 sample_151674 sample_151675 sample_151676
    #>         0.558         0.575         0.529         0.541

    nmi <- sapply(spe_list, function(x) as.numeric(tail(compareClusters(x, func = "NMI")[, 1], n = 1)))
    nmi
    #> sample_151673 sample_151674 sample_151675 sample_151676
    #>         0.684         0.695         0.670         0.672

Visualise pathology annotation and BANKSY cluster on spatial coordinates with the ggspavis package:

    # Use scater:::.get_palette('tableau10medium')
    pal <- c(
        "#729ECE", "#FF9E4A", "#67BF5C", "#ED665D", "#AD8BC9",
        "#A8786E", "#ED97CA", "#A2A2A2", "#CDCC5D", "#6DCCDA"
    )

    plot_bank <- lapply(spe_list, function(x) {
        df <- cbind.data.frame(
            clust=colData(x)[[sprintf("%s_smooth", cnm)]], spatialCoords(x))
        ggplot(df, aes(x=pxl_row_in_fullres, y=pxl_col_in_fullres, col=clust)) +
            geom_point(size = 0.5) +
            scale_color_manual(values = pal) +
            theme_classic() +
            theme(
                legend.position = "none",
                axis.text.x=element_blank(),
                axis.text.y=element_blank(),
                axis.ticks=element_blank(),
                axis.title.x=element_blank(),
                axis.title.y=element_blank()) +
            labs(title = "BANKSY clusters") +
            coord_equal()
    })

    plot_anno <- lapply(spe_list, function(x) {
        df <- cbind.data.frame(
            clust=colData(x)[['clust_annotation']], spatialCoords(x))
        ggplot(df, aes(x=pxl_row_in_fullres, y=pxl_col_in_fullres, col=clust)) +
            geom_point(size = 0.5) +
            scale_color_manual(values = pal) +
            theme_classic() +
            theme(
                legend.position = "none",
                axis.text.x=element_blank(),
                axis.text.y=element_blank(),
                axis.ticks=element_blank(),
                axis.title.x=element_blank(),
                axis.title.y=element_blank()) +
            labs(title = sprintf("Sample %s", x$sample_id[1])) +
            coord_equal()
    })

    plot_list <- c(plot_anno, plot_bank)

    plot_grid(plotlist = plot_list, ncol = 4, byrow = TRUE)

Visualize joint UMAPs for each sample:

    umap_bank <- lapply(spe_list, function(x) {
        plotReducedDim(x,
            "UMAP_M0_lam0.2",
            colour_by = sprintf("%s_smooth", cnm),
            point_size = 0.5
        ) +
            theme(legend.position = "none") +
            labs(title = "BANKSY clusters")
    })

    umap_anno <- lapply(spe_list, function(x) {
        plotReducedDim(x,
            "UMAP_M0_lam0.2",
            colour_by = "clust_annotation",
            point_size = 0.5
        ) +
            theme(legend.position = "none") +
            labs(title = sprintf("Sample %s", x$sample_id[1]))
    })

    umap_list <- c(umap_anno, umap_bank)

    plot_grid(plotlist = umap_list, ncol = 4, byrow = TRUE)

# 5 Session information

Vignette runtime:

    #> Time difference of 4.225057 mins

    sessionInfo()
    #> R version 4.5.0 (2025-04-11 ucrt)
    #> Platform: x86_64-w64-mingw32/x64
    #> Running under: Windows Server 2022 x64 (build 20348)
    #>
    #> Matrix products: default
    #>   LAPACK version 3.12.1
    #>
    #> locale:
    #> [1] LC_COLLATE=C
    #> [2] LC_CTYPE=English_United States.utf8
    #> [3] LC_MONETARY=English_United States.utf8
    #> [4] LC_NUMERIC=C
    #> [5] LC_TIME=English_United States.utf8
    #>
    #> time zone: America/New_York
    #> tzcode source: internal
    #>
    #> attached base packages:
    #> [1] stats4    stats     graphics  grDevices utils     datasets  methods
    #> [8] base
    #>
    #> other attached packages:
    #>  [1] ExperimentHub_2.99.5        AnnotationHub_3.99.5
    #>  [3] BiocFileCache_2.99.5        dbplyr_2.5.0
    #>  [5] spatialLIBD_1.21.5          cowplot_1.1.3
    #>  [7] scater_1.37.0               ggplot2_3.5.2
    #>  [9] harmony_1.2.3               Rcpp_1.0.14
    #> [11] data.table_1.17.4           scran_1.37.0
    #> [13] scuttle_1.19.0              Seurat_5.3.0
    #> [15] SeuratObject_5.1.0          sp_2.2-0
    #> [17] SpatialExperiment_1.19.1    SingleCellExperiment_1.31.0
    #> [19] SummarizedExperiment_1.39.0 Biobase_2.69.0
    #> [21] GenomicRanges_1.61.0        GenomeInfoDb_1.45.4
    #> [23] IRanges_2.43.0              S4Vectors_0.47.0
    #> [25] BiocGenerics_0.55.0         generics_0.1.4
    #> [27] MatrixGenerics_1.21.0       matrixStats_1.5.0
    #> [29] Banksy_1.5.0                BiocStyle_2.37.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.0
    #>   [7] sctransform_0.4.2        DT_0.33                  R6_2.6.1
    #>  [10] lazyeval_0.2.2           uwot_0.2.3               GetoptLong_1.0.5
    #>  [13] withr_3.0.2              gridExtra_2.3            progressr_0.15.1
    #>  [16] cli_3.6.5                spatstat.explore_3.4-3   fastDummies_1.7.5
    #>  [19] labeling_0.4.3           sass_0.4.10              spatstat.data_3.1-6
    #>  [22] ggridges_0.5.6           pbapply_1.7-2            Rsamtools_2.25.0
    #>  [25] dbscan_1.2.2             aricode_1.0.3            dichromat_2.0-0.1
    #>  [28] sessioninfo_1.2.3        parallelly_1.45.0        attempt_0.3.1
    #>  [31] maps_3.4.3               limma_3.65.1             pals_1.10
    #>  [34] RSQLite_2.4.0            BiocIO_1.19.0            shape_1.4.6.1
    #>  [37] ica_1.0-3                spatstat.random_3.4-1    dplyr_1.1.4
    #>  [40] Matrix_1.7-3             ggbeeswarm_0.7.2         abind_1.4-8
    #>  [43] lifecycle_1.0.4          yaml_2.3.10              edgeR_4.7.2
    #>  [46] SparseArray_1.9.0        Rtsne_0.17               paletteer_1.6.0
    #>  [49] grid_4.5.0               blob_1.2.4               promises_1.3.3
    #>  [52] dqrng_0.4.1              crayon_1.5.3             miniUI_0.1.2
    #>  [55] lattice_0.22-7           beachmat_2.25.1          mapproj_1.2.12
    #>  [58] KEGGREST_1.49.0          magick_2.8.6             pillar_1.10.2
    #>  [61] knitr_1.50               ComplexHeatmap_2.25.0    metapod_1.17.0
    #>  [64] rjson_0.2.23             future.apply_1.11.3      codetools_0.2-20
    #>  [67] glue_1.8.0               spatstat.univar_3.1-3    vctrs_0.6.5
    #>  [70] png_0.1-8                spam_2.11-1              gtable_0.3.6
    #>  [73] rematch2_2.1.2           cachem_1.1.0             xfun_0.52
    #>  [76] S4Arrays_1.9.1           mime_0.13                survival_3.8-3
    #>  [79] RcppHungarian_0.3        iterators_1.0.14         tinytex_0.57
    #>  [82] statmod_1.5.0            bluster_1.19.0           fitdistrplus_1.2-2
    #>  [85] ROCR_1.0-11              nlme_3.1-168             bit64_4.6.0-1
    #>  [88] filelock_1.0.3           RcppAnnoy_0.0.22         bslib_0.9.0
    #>  [91] irlba_2.3.5.1            vipor_0.4.7              KernSmooth_2.23-26
    #>  [94] colorspace_2.1-1         DBI_1.2.3                tidyselect_1.2.1
    #>  [97] bit_4.6.0                compiler_4.5.0           curl_6.2.3
    #> [100] httr2_1.1.2              BiocNeighbors_2.3.1      DelayedArray_0.35.1
    #> [103] plotly_4.10.4            rtracklayer_1.69.0       bookdown_0.43
    #> [106] scales_1.4.0             lmtest_0.9-40            rappdirs_0.3.3
    #> [109] stringr_1.5.1            digest_0.6.37            goftest_1.2-3
    #> [112] spatstat.utils_3.1-4     rmarkdown_2.29           benchmarkmeData_1.0.4
    #> [115] RhpcBLASctl_0.23-42      XVector_0.49.0           htmltools_0.5.8.1
    #> [118] pkgconfig_2.0.3          fastmap_1.2.0            GlobalOptions_0.1.2
    #> [121] rlang_1.1.6              htmlwidgets_1.6.4        UCSC.utils_1.5.0
    #> [124] shiny_1.10.0             farver_2.1.2             jquerylib_0.1.4
    #> [127] zoo_1.8-14               jsonlite_2.0.0           BiocParallel_1.43.3
    #> [130] mclust_6.1.1             config_0.3.2             RCurl_1.98-1.17
    #> [133] BiocSingular_1.25.0      magrittr_2.0.3           dotCall64_1.2
    #> [136] patchwork_1.3.0          viridis_0.6.5            reticulate_1.42.0
    #> [139] leidenAlg_1.1.5          stringi_1.8.7            MASS_7.3-65
    #> [142] plyr_1.8.9               parallel_4.5.0           listenv_0.9.1
    #> [145] ggrepel_0.9.6            deldir_2.0-4             Biostrings_2.77.1
    #> [148] sccore_1.0.6             splines_4.5.0            tensor_1.5
    #> [151] circlize_0.4.16          locfit_1.5-9.12          igraph_2.1.4
    #> [154] spatstat.geom_3.4-1      RcppHNSW_0.6.0           reshape2_1.4.4
    #> [157] ScaledMatrix_1.17.0      XML_3.99-0.18            BiocVersion_3.22.0
    #> [160] evaluate_1.0.3           golem_0.5.1              BiocManager_1.30.25
    #> [163] foreach_1.5.2            httpuv_1.6.16            RANN_2.6.2
    #> [166] tidyr_1.3.1              purrr_1.0.4              polyclip_1.10-7
    #> [169] benchmarkme_1.0.8        clue_0.3-66              future_1.49.0
    #> [172] scattermore_1.2          rsvd_1.0.5               xtable_1.8-4
    #> [175] restfulr_0.0.15          RSpectra_0.16-2          later_1.4.2
    #> [178] viridisLite_0.4.2        tibble_3.2.1             GenomicAlignments_1.45.0
    #> [181] memoise_2.0.1            beeswarm_0.4.0           AnnotationDbi_1.71.0
    #> [184] cluster_2.1.8.1          shinyWidgets_0.9.0       globals_0.18.0