snifter 1.2.0
snifter provides an R wrapper for the openTSNE implementation of fast interpolated t-SNE (FI-tSNE). It is based on basilisk and reticulate. This vignette aims to provide a brief overview of typical use when applied to scRNAseq data, but it does not provide a comprehensive guide to the available options in the package.
It is highly advisable to review the documentation in snifter and the openTSNE documentation to gain a full understanding of the available options.
We will illustrate the use of snifter using data from scRNAseq and single cell utility functions provided by scuttle, scater and scran - first we load these libraries and set a random seed to ensure the t-SNE visualisation is reproducible (note: it is good practice to ensure that a t-SNE embedding is robust by running the algorithm multiple times).
library("snifter")
library("scRNAseq")
library("scran")
library("scuttle")
library("scater")
library("ggplot2")
theme_set(theme_bw())
set.seed(42)
Before running t-SNE, we first load data generated by Zeisel et al. from scRNAseq. We filter this data to remove genes expressed only in a small number of cells, estimate normalisation factors using scran and generate 20 principal components. We will use these principal components to generate the t-SNE embedding later.
data <- ZeiselBrainData()
data <- data[rowMeans(counts(data) != 0) > 0.05, ]
data <- computeSumFactors(data, cluster = quickCluster(data))
data <- logNormCounts(data)
data <- runPCA(data, ncomponents = 20)
## Convert this to a factor to use as colouring variable later
data$level1class <- factor(data$level1class)
The main functionality of the package lies in the fitsne
function. This function returns a matrix of t-SNE co-ordinates. In this case,
we pass in the 20 principal components computed based on the
log-normalised counts. We colour points based on the discrete
cell types identified by the authors.
mat <- reducedDim(data)
fit <- fitsne(mat, random_state = 42L)
ggplot() +
aes(fit[, 1], fit[, 2], colour = data$level1class) +
geom_point(pch = 19) +
scale_colour_discrete(name = "Cell type") +
labs(x = "t-SNE 1", y = "t-SNE 2")
The openTNSE package, and by extension snifter, also allows the embedding of new data into an existing t-SNE embedding. Here, we will split the data into “training” and “test” sets. Following this, we generate a t-SNE embedding using the training data, and project the test data into this embedding.
test_ind <- sample(nrow(mat), nrow(mat) / 2)
train_ind <- setdiff(seq_len(nrow(mat)), test_ind)
train_mat <- mat[train_ind, ]
test_mat <- mat[test_ind, ]
train_label <- data$level1class[train_ind]
test_label <- data$level1class[test_ind]
embedding <- fitsne(train_mat, random_state = 42L)
Once we have generated the embedding, we can now project
the unseen test
data into this t-SNE embedding.
new_coords <- project(embedding, new = test_mat, old = train_mat)
ggplot() +
geom_point(
aes(embedding[, 1], embedding[, 2],
colour = train_label,
shape = "Train"
)
) +
geom_point(
aes(new_coords[, 1], new_coords[, 2],
colour = test_label,
shape = "Test"
)
) +
scale_colour_discrete(name = "Cell type") +
scale_shape_discrete(name = NULL) +
labs(x = "t-SNE 1", y = "t-SNE 2")
sessionInfo()
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.2 LTS
#>
#> Matrix products: default
#> BLAS: /home/biocbuild/bbs-3.13-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.13-bioc/R/lib/libRlapack.so
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_GB 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
#>
#> attached base packages:
#> [1] parallel stats4 stats graphics grDevices utils datasets
#> [8] methods base
#>
#> other attached packages:
#> [1] scater_1.20.0 ggplot2_3.3.3
#> [3] scran_1.20.0 scuttle_1.2.0
#> [5] scRNAseq_2.5.10 SingleCellExperiment_1.14.0
#> [7] SummarizedExperiment_1.22.0 Biobase_2.52.0
#> [9] GenomicRanges_1.44.0 GenomeInfoDb_1.28.0
#> [11] IRanges_2.26.0 S4Vectors_0.30.0
#> [13] BiocGenerics_0.38.0 MatrixGenerics_1.4.0
#> [15] matrixStats_0.58.0 snifter_1.2.0
#> [17] BiocStyle_2.20.0
#>
#> loaded via a namespace (and not attached):
#> [1] AnnotationHub_3.0.0 BiocFileCache_2.0.0
#> [3] igraph_1.2.6 lazyeval_0.2.2
#> [5] BiocParallel_1.26.0 digest_0.6.27
#> [7] ensembldb_2.16.0 htmltools_0.5.1.1
#> [9] magick_2.7.2 viridis_0.6.1
#> [11] fansi_0.4.2 magrittr_2.0.1
#> [13] memoise_2.0.0 ScaledMatrix_1.0.0
#> [15] cluster_2.1.2 limma_3.48.0
#> [17] Biostrings_2.60.0 prettyunits_1.1.1
#> [19] colorspace_2.0-1 blob_1.2.1
#> [21] rappdirs_0.3.3 xfun_0.23
#> [23] dplyr_1.0.6 crayon_1.4.1
#> [25] RCurl_1.98-1.3 jsonlite_1.7.2
#> [27] glue_1.4.2 gtable_0.3.0
#> [29] zlibbioc_1.38.0 XVector_0.32.0
#> [31] DelayedArray_0.18.0 BiocSingular_1.8.0
#> [33] scales_1.1.1 DBI_1.1.1
#> [35] edgeR_3.34.0 Rcpp_1.0.6
#> [37] viridisLite_0.4.0 xtable_1.8-4
#> [39] progress_1.2.2 reticulate_1.20
#> [41] dqrng_0.3.0 bit_4.0.4
#> [43] rsvd_1.0.5 metapod_1.0.0
#> [45] httr_1.4.2 dir.expiry_1.0.0
#> [47] ellipsis_0.3.2 farver_2.1.0
#> [49] pkgconfig_2.0.3 XML_3.99-0.6
#> [51] sass_0.4.0 dbplyr_2.1.1
#> [53] locfit_1.5-9.4 utf8_1.2.1
#> [55] labeling_0.4.2 tidyselect_1.1.1
#> [57] rlang_0.4.11 later_1.2.0
#> [59] AnnotationDbi_1.54.0 munsell_0.5.0
#> [61] BiocVersion_3.13.1 tools_4.1.0
#> [63] cachem_1.0.5 generics_0.1.0
#> [65] RSQLite_2.2.7 ExperimentHub_2.0.0
#> [67] evaluate_0.14 stringr_1.4.0
#> [69] fastmap_1.1.0 yaml_2.2.1
#> [71] knitr_1.33 bit64_4.0.5
#> [73] purrr_0.3.4 KEGGREST_1.32.0
#> [75] AnnotationFilter_1.16.0 sparseMatrixStats_1.4.0
#> [77] mime_0.10 biomaRt_2.48.0
#> [79] compiler_4.1.0 beeswarm_0.3.1
#> [81] filelock_1.0.2 curl_4.3.1
#> [83] png_0.1-7 interactiveDisplayBase_1.30.0
#> [85] tibble_3.1.2 statmod_1.4.36
#> [87] bslib_0.2.5.1 stringi_1.6.2
#> [89] highr_0.9 basilisk.utils_1.4.0
#> [91] GenomicFeatures_1.44.0 lattice_0.20-44
#> [93] bluster_1.2.0 ProtGenerics_1.24.0
#> [95] Matrix_1.3-3 vctrs_0.3.8
#> [97] pillar_1.6.1 lifecycle_1.0.0
#> [99] BiocManager_1.30.15 jquerylib_0.1.4
#> [101] BiocNeighbors_1.10.0 bitops_1.0-7
#> [103] irlba_2.3.3 httpuv_1.6.1
#> [105] rtracklayer_1.52.0 R6_2.5.0
#> [107] BiocIO_1.2.0 bookdown_0.22
#> [109] promises_1.2.0.1 gridExtra_2.3
#> [111] vipor_0.4.5 assertthat_0.2.1
#> [113] rjson_0.2.20 withr_2.4.2
#> [115] GenomicAlignments_1.28.0 Rsamtools_2.8.0
#> [117] GenomeInfoDbData_1.2.6 hms_1.1.0
#> [119] grid_4.1.0 beachmat_2.8.0
#> [121] basilisk_1.4.0 rmarkdown_2.8
#> [123] DelayedMatrixStats_1.14.0 shiny_1.6.0
#> [125] ggbeeswarm_0.6.0 restfulr_0.0.13