# Clustering and metaclustering

## Timothy Keyes

### 2025-04-16

    library(tidytof)
    library(dplyr)

Often, clustering single-cell data to identify communities of cells with shared characteristics is a major goal of high-dimensional cytometry data analysis.

To do this, `{tidytof}` provides the `tof_cluster()` verb. Several clustering methods are implemented in `{tidytof}`, including the following:

- FlowSOM
- k-means
- PhenoGraph
- Supervised distance-based clustering
- X-shift

Each of these methods are wrapped by `tof_cluster()`.

## Clustering with `tof_cluster()`

To demonstrate, we can apply the PhenoGraph clustering algorithm to `{tidytof}`’s built-in `phenograph_data`. Note that `phenograph_data` contains 3000 total cells (1000 each from 3 clusters identified in the original PhenoGraph publication). For demonstration purposes, we also metacluster our PhenoGraph clusters using k-means clustering.

    data(phenograph_data)

    set.seed(203L)

    phenograph_clusters <-
        phenograph_data |>
        tof_preprocess() |>
        tof_cluster(
            cluster_cols = starts_with("cd"),
            num_neighbors = 50L,
            distance_function = "cosine",
            method = "phenograph"
        ) |>
        tof_metacluster(
            cluster_col = .phenograph_cluster,
            metacluster_cols = starts_with("cd"),
            num_metaclusters = 3L,
            method = "kmeans"
        )

    phenograph_clusters |>
        dplyr::select(sample_name, .phenograph_cluster, .kmeans_metacluster) |>
        head()
    #> # A tibble: 6 × 3
    #>   sample_name            .phenograph_cluster .kmeans_metacluster
    #>   <chr>                  <chr>               <chr>
    #> 1 H1_PhenoGraph_cluster1 5                   2
    #> 2 H1_PhenoGraph_cluster1 1                   2
    #> 3 H1_PhenoGraph_cluster1 5                   2
    #> 4 H1_PhenoGraph_cluster1 1                   2
    #> 5 H1_PhenoGraph_cluster1 1                   2
    #> 6 H1_PhenoGraph_cluster1 5                   2

The outputs of both `tof_cluster()` and `tof_metacluster()` are a `tof_tbl` identical to the input tibble, but now with the addition of an additional column (in this case, “.phenograph_cluster” and “.kmeans_metacluster”) that encodes the cluster id for each cell in the input `tof_tbl`. Note that all output columns added to a tibble or `tof_tbl` by `{tidytof}` begin with a full-stop (”.”) to reduce the likelihood of collisions with existing column names.

Because the output of `tof_cluster` is a `tof_tbl`, we can use `dplyr`’s `count` method to assess the accuracy of our clustering procedure compared to the original clustering from the PhenoGraph paper.

    phenograph_clusters |>
        dplyr::count(phenograph_cluster, .kmeans_metacluster, sort = TRUE)
    #> # A tibble: 4 × 3
    #>   phenograph_cluster .kmeans_metacluster     n
    #>   <chr>              <chr>               <int>
    #> 1 cluster2           1                    1000
    #> 2 cluster3           3                    1000
    #> 3 cluster1           2                     995
    #> 4 cluster1           3                       5

Here, we can see that our clustering procedure groups most cells from the same PhenoGraph cluster with one another (with a small number of mistakes).

To change which clustering algorithm `tof_cluster` uses, alter the `method` flag.

    # use the kmeans algorithm
    phenograph_data |>
        tof_preprocess() |>
        tof_cluster(
            cluster_cols = contains("cd"),
            method = "kmeans"
        )

    # use the flowsom algorithm
    phenograph_data |>
        tof_preprocess() |>
        tof_cluster(
            cluster_cols = contains("cd"),
            method = "flowsom"
        )

To change the columns used to compute the clusters, change the `cluster_cols` flag. And finally, if you want to return a one-column `tibble` that only includes the cluster labels (as opposed to the cluster labels added as a new column to the input `tof_tbl`), set `augment` to `FALSE`.

    # will result in a tibble with only 1 column (the cluster labels)
    phenograph_data |>
        tof_preprocess() |>
        tof_cluster(
            cluster_cols = contains("cd"),
            method = "kmeans",
            augment = FALSE
        ) |>
        head()
    #> # A tibble: 6 × 1
    #>   .kmeans_cluster
    #>   <chr>
    #> 1 9
    #> 2 9
    #> 3 2
    #> 4 19
    #> 5 12
    #> 6 19

# Session info

    sessionInfo()
    #> R version 4.5.0 RC (2025-04-04 r88126 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] HDCytoData_1.27.0           flowCore_2.20.0
    #>  [3] SummarizedExperiment_1.38.0 Biobase_2.68.0
    #>  [5] GenomicRanges_1.60.0        GenomeInfoDb_1.44.0
    #>  [7] IRanges_2.42.0              S4Vectors_0.46.0
    #>  [9] MatrixGenerics_1.20.0       matrixStats_1.5.0
    #> [11] ExperimentHub_2.16.0        AnnotationHub_3.16.0
    #> [13] BiocFileCache_2.16.0        dbplyr_2.5.0
    #> [15] BiocGenerics_0.54.0         generics_0.1.3
    #> [17] forcats_1.0.0               ggplot2_3.5.2
    #> [19] dplyr_1.1.4                 tidytof_1.2.0
    #>
    #> loaded via a namespace (and not attached):
    #>   [1] jsonlite_2.0.0          shape_1.4.6.1           magrittr_2.0.3
    #>   [4] farver_2.1.2            rmarkdown_2.29          vctrs_0.6.5
    #>   [7] memoise_2.0.1           sparsevctrs_0.3.3       htmltools_0.5.8.1
    #>  [10] S4Arrays_1.8.0          curl_6.2.2              SparseArray_1.8.0
    #>  [13] sass_0.4.10             parallelly_1.43.0       bslib_0.9.0
    #>  [16] lubridate_1.9.4         cachem_1.1.0            igraph_2.1.4
    #>  [19] mime_0.13               lifecycle_1.0.4         iterators_1.0.14
    #>  [22] pkgconfig_2.0.3         Matrix_1.7-3            R6_2.6.1
    #>  [25] fastmap_1.2.0           GenomeInfoDbData_1.2.14 future_1.40.0
    #>  [28] digest_0.6.37           colorspace_2.1-1        AnnotationDbi_1.70.0
    #>  [31] RSQLite_2.3.9           labeling_0.4.3          filelock_1.0.3
    #>  [34] cytolib_2.20.0          yardstick_1.3.2         timechange_0.3.0
    #>  [37] httr_1.4.7              polyclip_1.10-7         abind_1.4-8
    #>  [40] compiler_4.5.0          bit64_4.6.0-1           withr_3.0.2
    #>  [43] doParallel_1.0.17       viridis_0.6.5           DBI_1.2.3
    #>  [46] ggforce_0.4.2           MASS_7.3-65             lava_1.8.1
    #>  [49] rappdirs_0.3.3          DelayedArray_0.34.0     tools_4.5.0
    #>  [52] future.apply_1.11.3     nnet_7.3-20             glue_1.8.0
    #>  [55] grid_4.5.0              recipes_1.2.1           gtable_0.3.6
    #>  [58] tzdb_0.5.0              class_7.3-23            tidyr_1.3.1
    #>  [61] data.table_1.17.0       hms_1.1.3               utf8_1.2.4
    #>  [64] tidygraph_1.3.1         XVector_0.48.0          ggrepel_0.9.6
    #>  [67] BiocVersion_3.21.1      foreach_1.5.2           pillar_1.10.2
    #>  [70] stringr_1.5.1           RcppHNSW_0.6.0          splines_4.5.0
    #>  [73] tweenr_2.0.3            lattice_0.22-7          survival_3.8-3
    #>  [76] bit_4.6.0               RProtoBufLib_2.20.0     tidyselect_1.2.1
    #>  [79] Biostrings_2.76.0       knitr_1.50              gridExtra_2.3
    #>  [82] xfun_0.52               graphlayouts_1.2.2      hardhat_1.4.1
    #>  [85] timeDate_4041.110       stringi_1.8.7           UCSC.utils_1.4.0
    #>  [88] yaml_2.3.10             evaluate_1.0.3          codetools_0.2-20
    #>  [91] ggraph_2.2.1            tibble_3.2.1            BiocManager_1.30.25
    #>  [94] cli_3.6.4               rpart_4.1.24            munsell_0.5.1
    #>  [97] jquerylib_0.1.4         Rcpp_1.0.14             globals_0.16.3
    #> [100] png_0.1-8               parallel_4.5.0          gower_1.0.2
    #> [103] readr_2.1.5             blob_1.2.4              listenv_0.9.1
    #> [106] glmnet_4.1-8            viridisLite_0.4.2       ipred_0.9-15
    #> [109] ggridges_0.5.6          scales_1.3.0            prodlim_2024.06.25
    #> [112] purrr_1.0.4             crayon_1.5.3            rlang_1.1.6
    #> [115] KEGGREST_1.48.0