library(tidytof)
library(dplyr)
library(ggplot2)
count <- dplyr::count
Often, high-dimensional cytometry experiments collect tens or hundreds or millions of cells in total, and it can be useful to downsample to a smaller, more computationally tractable number of cells - either for a final analysis or while developing code.
To do this, {tidytof}
implements the tof_downsample()
verb, which allows downsampling using 3 methods: downsampling to an integer number of cells, downsampling to a fixed proportion of the total number of input cells, or downsampling to a fixed cellular density in phenotypic space.
tof_downsample()
Using {tidytof}
’s built-in dataset phenograph_data
, we can see that the original size of the dataset is 1000 cells per cluster, or 3000 cells in total:
data(phenograph_data)
phenograph_data |>
dplyr::count(phenograph_cluster)
#> # A tibble: 3 × 2
#> phenograph_cluster n
#> <chr> <int>
#> 1 cluster1 1000
#> 2 cluster2 1000
#> 3 cluster3 1000
To randomly sample 200 cells per cluster, we can use tof_downsample()
using the “constant” method
:
phenograph_data |>
# downsample
tof_downsample(
group_cols = phenograph_cluster,
method = "constant",
num_cells = 200
) |>
# count the number of downsampled cells in each cluster
count(phenograph_cluster)
#> # A tibble: 3 × 2
#> phenograph_cluster n
#> <chr> <int>
#> 1 cluster1 200
#> 2 cluster2 200
#> 3 cluster3 200
Alternatively, if we wanted to sample 50% of the cells in each cluster, we could use the “prop” method
:
phenograph_data |>
# downsample
tof_downsample(
group_cols = phenograph_cluster,
method = "prop",
prop_cells = 0.5
) |>
# count the number of downsampled cells in each cluster
count(phenograph_cluster)
#> # A tibble: 3 × 2
#> phenograph_cluster n
#> <chr> <int>
#> 1 cluster1 500
#> 2 cluster2 500
#> 3 cluster3 500
And finally, we might also be interested in taking a slightly different approach to downsampling that reduces the number of cells not to a fixed constant or proportion, but to a fixed density in phenotypic space. For example, the following scatterplot demonstrates that there are certain areas of phenotypic density in phenograph_data
that contain more cells than others along the cd34
/cd38
axes:
rescale_max <-
function(x, to = c(0, 1), from = range(x, na.rm = TRUE)) {
x / from[2] * to[2]
}
phenograph_data |>
# preprocess all numeric columns in the dataset
tof_preprocess(undo_noise = FALSE) |>
# plot
ggplot(aes(x = cd34, y = cd38)) +
geom_hex() +
coord_fixed(ratio = 0.4) +
scale_x_continuous(limits = c(NA, 1.5)) +
scale_y_continuous(limits = c(NA, 4)) +
scale_fill_viridis_c(
labels = function(x) round(rescale_max(x), 2)
) +
labs(
fill = "relative density"
)
To reduce the number of cells in our dataset until the local density around each cell in our dataset is relatively constant, we can use the “density” method
of tof_downsample
:
phenograph_data |>
tof_preprocess(undo_noise = FALSE) |>
tof_downsample(method = "density", density_cols = c(cd34, cd38)) |>
# plot
ggplot(aes(x = cd34, y = cd38)) +
geom_hex() +
coord_fixed(ratio = 0.4) +
scale_x_continuous(limits = c(NA, 1.5)) +
scale_y_continuous(limits = c(NA, 4)) +
scale_fill_viridis_c(
labels = function(x) round(rescale_max(x), 2)
) +
labs(
fill = "relative density"
)
Thus, we can see that the density after downsampling is more uniform (though not exactly uniform) across the range of cd34
/cd38
values in phenograph_data
.
For more details, check out the documentation for the 3 underlying members of the tof_downsample_*
function family (which are wrapped by tof_downsample
):
tof_downsample_constant
tof_downsample_prop
tof_downsample_density
sessionInfo()
#> R Under development (unstable) (2024-11-20 r87352)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Ventura 13.7.1
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
#>
#> locale:
#> [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> 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] tidyr_1.3.1 stringr_1.5.1
#> [3] HDCytoData_1.27.0 flowCore_2.19.0
#> [5] SummarizedExperiment_1.37.0 Biobase_2.67.0
#> [7] GenomicRanges_1.59.1 GenomeInfoDb_1.43.2
#> [9] IRanges_2.41.1 S4Vectors_0.45.2
#> [11] MatrixGenerics_1.19.0 matrixStats_1.4.1
#> [13] ExperimentHub_2.15.0 AnnotationHub_3.15.0
#> [15] BiocFileCache_2.15.0 dbplyr_2.5.0
#> [17] BiocGenerics_0.53.3 generics_0.1.3
#> [19] forcats_1.0.0 ggplot2_3.5.1
#> [21] dplyr_1.1.4 tidytof_1.1.0
#>
#> loaded via a namespace (and not attached):
#> [1] jsonlite_1.8.9 shape_1.4.6.1 magrittr_2.0.3
#> [4] farver_2.1.2 rmarkdown_2.29 zlibbioc_1.53.0
#> [7] vctrs_0.6.5 memoise_2.0.1 htmltools_0.5.8.1
#> [10] S4Arrays_1.7.1 curl_6.0.1 SparseArray_1.7.2
#> [13] sass_0.4.9 parallelly_1.39.0 bslib_0.8.0
#> [16] lubridate_1.9.3 cachem_1.1.0 commonmark_1.9.2
#> [19] igraph_2.1.1 mime_0.12 lifecycle_1.0.4
#> [22] iterators_1.0.14 pkgconfig_2.0.3 Matrix_1.7-1
#> [25] R6_2.5.1 fastmap_1.2.0 GenomeInfoDbData_1.2.13
#> [28] future_1.34.0 digest_0.6.37 colorspace_2.1-1
#> [31] AnnotationDbi_1.69.0 irlba_2.3.5.1 RSQLite_2.3.8
#> [34] labeling_0.4.3 filelock_1.0.3 cytolib_2.19.0
#> [37] fansi_1.0.6 yardstick_1.3.1 timechange_0.3.0
#> [40] httr_1.4.7 polyclip_1.10-7 abind_1.4-8
#> [43] compiler_4.5.0 bit64_4.5.2 withr_3.0.2
#> [46] doParallel_1.0.17 viridis_0.6.5 DBI_1.2.3
#> [49] hexbin_1.28.5 ggforce_0.4.2 MASS_7.3-61
#> [52] lava_1.8.0 embed_1.1.4 rappdirs_0.3.3
#> [55] DelayedArray_0.33.2 tools_4.5.0 future.apply_1.11.3
#> [58] nnet_7.3-19 glue_1.8.0 grid_4.5.0
#> [61] Rtsne_0.17 recipes_1.1.0 gtable_0.3.6
#> [64] tzdb_0.4.0 class_7.3-22 data.table_1.16.2
#> [67] hms_1.1.3 tidygraph_1.3.1 utf8_1.2.4
#> [70] XVector_0.47.0 RcppAnnoy_0.0.22 markdown_1.13
#> [73] ggrepel_0.9.6 BiocVersion_3.21.1 foreach_1.5.2
#> [76] pillar_1.9.0 RcppHNSW_0.6.0 splines_4.5.0
#> [79] tweenr_2.0.3 lattice_0.22-6 survival_3.7-0
#> [82] bit_4.5.0 RProtoBufLib_2.19.0 tidyselect_1.2.1
#> [85] Biostrings_2.75.1 knitr_1.49 gridExtra_2.3
#> [88] xfun_0.49 graphlayouts_1.2.1 hardhat_1.4.0
#> [91] timeDate_4041.110 stringi_1.8.4 UCSC.utils_1.3.0
#> [94] yaml_2.3.10 evaluate_1.0.1 codetools_0.2-20
#> [97] ggraph_2.2.1 tibble_3.2.1 BiocManager_1.30.25
#> [100] cli_3.6.3 uwot_0.2.2 rpart_4.1.23
#> [103] munsell_0.5.1 jquerylib_0.1.4 Rcpp_1.0.13-1
#> [106] globals_0.16.3 png_0.1-8 parallel_4.5.0
#> [109] gower_1.0.1 readr_2.1.5 blob_1.2.4
#> [112] listenv_0.9.1 glmnet_4.1-8 viridisLite_0.4.2
#> [115] ipred_0.9-15 ggridges_0.5.6 scales_1.3.0
#> [118] prodlim_2024.06.25 purrr_1.0.2 crayon_1.5.3
#> [121] rlang_1.1.4 KEGGREST_1.47.0