Skip to contents

The following is a full example of usage of the widget with a Seurat object.

First, install the R dependencies:

install.packages("seurat")
install.packages("devtools")
devtools::install_github("mojaveazure/seurat-disk")

Download the dataset, load and preprocess the Seurat object, and configure the Vitessce widget:

library(vitessceR)
library(Seurat)

# Download example dataset
url <- "https://cf.10xgenomics.com/samples/cell/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz"
save_dir <- file.path("data", "seurat")
dir.create(save_dir)
download.file(url, destfile = file.path(save_dir, "filtered_gene_bc_matrices.tar.gz"))
untar(file.path(save_dir, "filtered_gene_bc_matrices.tar.gz"), exdir = save_dir)

# Load example dataset
pbmc.data <- Read10X(data.dir = file.path(save_dir, "filtered_gene_bc_matrices", "hg19"))

# Process example dataset (run PCA and cluster)
pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200)
pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
all.genes <- rownames(pbmc)
pbmc <- ScaleData(pbmc, features = all.genes)
pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc))
pbmc <- FindNeighbors(pbmc, dims = 1:10)
pbmc <- FindClusters(pbmc, resolution = 0.5)

pbmc <- ScaleData(pbmc, features = all.genes, do.center = FALSE)

# Create Vitessce view config
vc <- VitessceConfig$new("My config")
dataset <- vc$add_dataset("My dataset")$add_object(SeuratWrapper$new(
  pbmc,
  cell_set_metas = c("seurat_clusters"),
  cell_embeddings = c("pca"),
  cell_embedding_names = c("PCA"),
  out_dir = file.path("data", "seurat_basic"), use_cache = TRUE
))
scatterplot <- vc$add_view(dataset, Component$SCATTERPLOT, mapping = "PCA")
status <- vc$add_view(dataset, Component$STATUS)
desc <- vc$add_view(dataset, Component$DESCRIPTION)
desc <- desc$set_props(description = "Visualization of a Seurat object containing the PBMC 3K dataset.")
genes <- vc$add_view(dataset, Component$GENES)
heatmap <- vc$add_view(dataset, Component$HEATMAP)
vc$link_views(
  list(scatterplot, heatmap),
  list(CoordinationType$GENE_EXPRESSION_COLORMAP_RANGE),
  list(c(0.0, 0.05))
)
vc$layout(hconcat(
  vconcat(scatterplot, heatmap),
  vconcat(genes, vconcat(desc, status))
))

# Render the Vitessce widget
vc$widget(theme = "light")