1 Introduction

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.

2 Setting up the data

We will illustrate the use of snifter by generating some toy data. First, we’ll load the needed libraries, and set a random seed to ensure the simulated data are reproducible (note: it is good practice to ensure that a t-SNE embedding is robust by running the algorithm multiple times).

library("snifter")
library("ggplot2")
theme_set(theme_bw())
set.seed(42)

n_obs <- 500
n_feats <- 200
means_1 <- rnorm(n_feats)
means_2 <- rnorm(n_feats)
counts_a <- replicate(n_obs, rnorm(n_feats, means_1))
counts_b <- replicate(n_obs, rnorm(n_feats, means_2))
counts <- t(cbind(counts_a, counts_b))
label <- rep(c("A", "B"), each = n_obs)

3 Running t-SNE

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.

fit <- fitsne(counts, random_state = 42L)
ggplot() +
    aes(fit[, 1], fit[, 2], colour = label) +
    geom_point(pch = 19) +
    scale_colour_discrete(name = "Cluster") +
    labs(x = "t-SNE 1", y = "t-SNE 2")

4 Projecting new data into an existing embedding

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(counts), nrow(counts) / 2)
train_ind <- setdiff(seq_len(nrow(counts)), test_ind)
train_mat <- counts[train_ind, ]
test_mat <- counts[test_ind, ]

train_label <- label[train_ind]
test_label <- label[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 = "Cluster") +
    scale_shape_discrete(name = NULL) +
    labs(x = "t-SNE 1", y = "t-SNE 2")

Session information

sessionInfo()
#> R version 4.2.0 RC (2022-04-19 r82224)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.15-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.15-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] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.3.5    snifter_1.6.0    BiocStyle_2.24.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] reticulate_1.24      tidyselect_1.1.2     xfun_0.30           
#>  [4] bslib_0.3.1          purrr_0.3.4          lattice_0.20-45     
#>  [7] basilisk.utils_1.8.0 colorspace_2.0-3     vctrs_0.4.1         
#> [10] generics_0.1.2       htmltools_0.5.2      yaml_2.3.5          
#> [13] utf8_1.2.2           rlang_1.0.2          jquerylib_0.1.4     
#> [16] pillar_1.7.0         withr_2.5.0          glue_1.6.2          
#> [19] DBI_1.1.2            lifecycle_1.0.1      stringr_1.4.0       
#> [22] munsell_0.5.0        gtable_0.3.0         evaluate_0.15       
#> [25] labeling_0.4.2       knitr_1.38           fastmap_1.1.0       
#> [28] parallel_4.2.0       fansi_1.0.3          highr_0.9           
#> [31] Rcpp_1.0.8.3         scales_1.2.0         filelock_1.0.2      
#> [34] BiocManager_1.30.17  magick_2.7.3         jsonlite_1.8.0      
#> [37] farver_2.1.0         basilisk_1.8.0       dir.expiry_1.4.0    
#> [40] png_0.1-7            digest_0.6.29        stringi_1.7.6       
#> [43] bookdown_0.26        dplyr_1.0.8          rprojroot_2.0.3     
#> [46] grid_4.2.0           here_1.0.1           cli_3.3.0           
#> [49] tools_4.2.0          magrittr_2.0.3       sass_0.4.1          
#> [52] tibble_3.1.6         crayon_1.5.1         pkgconfig_2.0.3     
#> [55] ellipsis_0.3.2       Matrix_1.4-1         assertthat_0.2.1    
#> [58] rmarkdown_2.14       R6_2.5.1             compiler_4.2.0