all_times <- list() # store the time for each chunk knitr::knit_hooks$set(time_it = local({ now <- NULL function(before, options) { if (before) { now <<- Sys.time() } else { res <- difftime(Sys.time(), now, units = "secs") all_times[[options$label]] <<- res } } })) knitr::opts_chunk$set( tidy = TRUE, tidy.opts = list(width.cutoff = 95), message = FALSE, warning = FALSE, time_it = TRUE, error = TRUE )
This vignette demonstrates how to store and interact with dimensional reduction information (such as the output from RunPCA()
) in Seurat. For demonstration purposes, we will be using the 2,700 PBMC object that is available via the SeuratData package.
library(Seurat) library(SeuratData) pbmc <- LoadData("pbmc3k", type = "pbmc3k.final")
In Seurat v3.0, storing and interacting with dimensional reduction information has been generalized and formalized into the DimReduc
object. Each dimensional reduction procedure is stored as a DimReduc
object in the object@reductions
slot as an element of a named list. Accessing these reductions can be done with the [[
operator, calling the name of the reduction desired. For example, after running a principle component analysis with RunPCA()
, object[['pca']]
will contain the results of the PCA. By adding new elements to the list, users can add additional, and custom, dimensional reductions. Each stored dimensional reduction contains the following slots:
ProjectDim()
) are stored in this slot. Note that the cell loadings will remain unchanged after projection but there are now feature loadings for all featureTo access these slots, we provide the Embeddings()
,Loadings()
, and Stdev()
functions
pbmc[['pca']] head(Embeddings(pbmc, reduction = "pca")[, 1:5]) head(Loadings(pbmc, reduction = "pca")[, 1:5]) head(Stdev(pbmc, reduction = "pca"))
Seurat provides RunPCA()
(pca), and RunTSNE()
(tsne), and representing dimensional reduction techniques commonly applied to scRNA-seq data. When using these functions, all slots are filled automatically.
We also allow users to add the results of a custom dimensional reduction technique (for example, multi-dimensional scaling (MDS), or zero-inflated factor analysis), that is computed separately. All you need is a matrix with each cell's coordinates in low-dimensional space, as shown below.
Though not incorporated as part of the Seurat package, its easy to run multidimensional scaling (MDS) in R. If you were interested in running MDS and storing the output in your Seurat object:
# Before running MDS, we first calculate a distance matrix between all pairs of cells. # Here we use a simple euclidean distance metric on all genes, using scale.data as input d <- dist(t(GetAssayData(pbmc, slot = 'scale.data'))) # Run the MDS procedure, k determines the number of dimensions mds <- cmdscale(d = d, k = 2) # cmdscale returns the cell embeddings, we first label the columns to ensure downstream consistency colnames(mds) <- paste0("MDS_", 1:2) # We will now store this as a custom dimensional reduction called "mds" pbmc[['mds']] <- CreateDimReducObject(embeddings = mds, key = 'MDS_', assay = DefaultAssay(pbmc)) # We can now use this as you would any other dimensional reduction in all downstream functions DimPlot(pbmc, reduction = "mds", pt.size = 0.5) # If you wold like to observe genes that are strongly correlated with the first MDS coordinate pbmc <- ProjectDim(pbmc, reduction = "mds") # Display the results as a heatmap DimHeatmap(pbmc, reduction = "mds", dims = 1, cells = 500, projected = TRUE, balanced = TRUE) # Explore how the first MDS dimension is distributed across clusters VlnPlot(pbmc, features = "MDS_1") # See how the first MDS dimension is correlated with the first PC dimension FeatureScatter(pbmc, feature1 = "MDS_1", feature2 = "PC_1")
library(ggplot2) plot <- DimPlot(pbmc, reduction = "mds", pt.size = 0.5) ggsave(filename = "../output/images/pbmc_mds.jpg", height = 7, width = 12, plot = plot, quality = 50)
write.csv(x = t(as.data.frame(all_times)), file = "../output/timings/dim_reduction_vignette_times.csv")
Session Info
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.