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 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)

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.

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")

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(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")

Session information

sessionInfo()
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.5 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.12-bioc/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        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.18.0               ggplot2_3.3.2              
#>  [3] scuttle_1.0.0               scran_1.18.0               
#>  [5] scRNAseq_2.3.17             SingleCellExperiment_1.12.0
#>  [7] SummarizedExperiment_1.20.0 Biobase_2.50.0             
#>  [9] GenomicRanges_1.42.0        GenomeInfoDb_1.26.0        
#> [11] IRanges_2.24.0              S4Vectors_0.28.0           
#> [13] BiocGenerics_0.36.0         MatrixGenerics_1.2.0       
#> [15] matrixStats_0.57.0          snifter_1.0.0              
#> [17] BiocStyle_2.18.0           
#> 
#> loaded via a namespace (and not attached):
#>   [1] ggbeeswarm_0.6.0              colorspace_1.4-1             
#>   [3] ellipsis_0.3.1                bluster_1.0.0                
#>   [5] XVector_0.30.0                BiocNeighbors_1.8.0          
#>   [7] farver_2.0.3                  bit64_4.0.5                  
#>   [9] interactiveDisplayBase_1.28.0 AnnotationDbi_1.52.0         
#>  [11] xml2_1.3.2                    sparseMatrixStats_1.2.0      
#>  [13] knitr_1.30                    jsonlite_1.7.1               
#>  [15] Rsamtools_2.6.0               dbplyr_1.4.4                 
#>  [17] shiny_1.5.0                   BiocManager_1.30.10          
#>  [19] compiler_4.0.3                httr_1.4.2                   
#>  [21] dqrng_0.2.1                   basilisk_1.2.0               
#>  [23] assertthat_0.2.1              Matrix_1.2-18                
#>  [25] fastmap_1.0.1                 lazyeval_0.2.2               
#>  [27] limma_3.46.0                  later_1.1.0.1                
#>  [29] BiocSingular_1.6.0            htmltools_0.5.0              
#>  [31] prettyunits_1.1.1             tools_4.0.3                  
#>  [33] rsvd_1.0.3                    igraph_1.2.6                 
#>  [35] gtable_0.3.0                  glue_1.4.2                   
#>  [37] GenomeInfoDbData_1.2.4        dplyr_1.0.2                  
#>  [39] rappdirs_0.3.1                Rcpp_1.0.5                   
#>  [41] vctrs_0.3.4                   Biostrings_2.58.0            
#>  [43] ExperimentHub_1.16.0          rtracklayer_1.50.0           
#>  [45] DelayedMatrixStats_1.12.0     xfun_0.18                    
#>  [47] stringr_1.4.0                 beachmat_2.6.0               
#>  [49] mime_0.9                      lifecycle_0.2.0              
#>  [51] irlba_2.3.3                   ensembldb_2.14.0             
#>  [53] statmod_1.4.35                XML_3.99-0.5                 
#>  [55] AnnotationHub_2.22.0          edgeR_3.32.0                 
#>  [57] scales_1.1.1                  zlibbioc_1.36.0              
#>  [59] basilisk.utils_1.2.0          hms_0.5.3                    
#>  [61] promises_1.1.1                ProtGenerics_1.22.0          
#>  [63] AnnotationFilter_1.14.0       yaml_2.2.1                   
#>  [65] curl_4.3                      gridExtra_2.3                
#>  [67] memoise_1.1.0                 reticulate_1.18              
#>  [69] biomaRt_2.46.0                stringi_1.5.3                
#>  [71] RSQLite_2.2.1                 BiocVersion_3.12.0           
#>  [73] GenomicFeatures_1.42.0        filelock_1.0.2               
#>  [75] BiocParallel_1.24.0           rlang_0.4.8                  
#>  [77] pkgconfig_2.0.3               bitops_1.0-6                 
#>  [79] evaluate_0.14                 lattice_0.20-41              
#>  [81] purrr_0.3.4                   labeling_0.4.2               
#>  [83] GenomicAlignments_1.26.0      bit_4.0.4                    
#>  [85] tidyselect_1.1.0              magrittr_1.5                 
#>  [87] bookdown_0.21                 R6_2.4.1                     
#>  [89] magick_2.5.0                  generics_0.0.2               
#>  [91] DelayedArray_0.16.0           DBI_1.1.0                    
#>  [93] withr_2.3.0                   pillar_1.4.6                 
#>  [95] RCurl_1.98-1.2                tibble_3.0.4                 
#>  [97] crayon_1.3.4                  BiocFileCache_1.14.0         
#>  [99] rmarkdown_2.5                 viridis_0.5.1                
#> [101] progress_1.2.2                locfit_1.5-9.4               
#> [103] grid_4.0.3                    blob_1.2.1                   
#> [105] digest_0.6.27                 xtable_1.8-4                 
#> [107] httpuv_1.5.4                  munsell_0.5.0                
#> [109] openssl_1.4.3                 viridisLite_0.3.0            
#> [111] beeswarm_0.2.3                vipor_0.4.5                  
#> [113] askpass_1.1