This is a quick walkthrough demonstrating how to generate SWNE plots alongside the Pagoda2 pipeline using a 3k PBMC dataset as an example.

To save time we will be using the pre-computed Pagoda2 object pbmc3k_pagoda2.Robj, which can be downloaded here.

First let’s load the required libraries

library(pagoda2)
library(swne)
library(Matrix)

Next let’s load the Pagoda2 object

p2 <- readRDS("/media/Home_Raid1/yan/swne/Data/pbmc3k_pagoda2.Robj")

Most scRNA-seq pipelines only use a subset of highly overdispersed genes for analysis. We’ll pull out those variable genes here, as well as the cluster labels.

## Pull out variable genes
n.od.genes <- 1.5e3
var.info <- p2$misc$varinfo; var.info <- var.info[order(var.info$lp, decreasing = F),];
od.genes <- rownames(var.info[1:n.od.genes,])
length(od.genes)
## [1] 1500
## Pull out clusters
clusters <- p2$clusters$PCA$multilevel
table(clusters)
## clusters
##   1   2   3   4   5   6   7   8 
## 356 159 596 323  35 171 563 493

The easiest way to generate an SWNE embedding is to use the wrapper function RunSWNE

## Run SWNE
genes.embed <- c("MS4A1", "GNLY", "CD3E", "CD14",
                 "FCER1A", "FCGR3A", "LYZ", "PPBP", "CD8A")
swne.embedding <- RunSWNE(p2, k = 16, var.genes = od.genes, genes.embed = genes.embed,
                          sample.groups = clusters)
## calculating variance fit ... using gam [1] "1500 variable genes to use"
## running PCA using 1500 OD genes ..
## Loading required package: irlba
## .. done
## Initial stress        : 0.14333
## stress after  10 iters: 0.06453, magic = 0.092
## stress after  20 iters: 0.04505, magic = 0.500
## stress after  30 iters: 0.04254, magic = 0.500
## stress after  40 iters: 0.04207, magic = 0.500
## stress after  50 iters: 0.04205, magic = 0.500
## Plot SWNE
PlotSWNE(swne.embedding, alpha.plot = 0.4, sample.groups = clusters,
         do.label = T, label.size = 3.5, pt.size = 1.5, show.legend = F,
         seed = 42)

We can also make a t-SNE plot for comparison.

tsne.scores <- p2$embeddings$PCA$tSNE
PlotDims(tsne.scores, sample.groups = clusters, pt.size = 0.75, label.size = 3.5, alpha = 0.3,
         show.legend = F, seed = 42, show.axes = F)

Extract cluster colors for compatibility with other plotting methods (i.e. Monocle)

color.mapping <- ExtractSWNEColors(swne.embedding, sample.groups = clusters, seed = 42)
color.mapping
##         7         1         8         2         4         6         3 
## "#FF61CC" "#F8766D" "#7CAE00" "#00BFC4" "#00A9FF" "#00BE67" "#C77CFF" 
##         5 
## "#CD9600"