Skip to content

Instantly share code, notes, and snippets.

@jebard
Created November 14, 2018 15:13
Show Gist options
  • Save jebard/74ff9911c2e1e44d2137e47f59562f97 to your computer and use it in GitHub Desktop.
Save jebard/74ff9911c2e1e44d2137e47f59562f97 to your computer and use it in GitHub Desktop.
Seurat to Veloctyo Code -- This is a general code chunk for running Velocyto on a Seurat object generated with 10x single-cell sequencing data.
library(velocyto.R)
library(pagoda2)
library(Seurat)
### ASSUMES Seurat object has already been computed -- in this case the object cells.combined is the result of seurat
# IMPORTANT!!! create a loom file using the velocyto command line tools first
ldat <- read.loom.matrices("filtered.loom")
# get the spliced and unspliced ratios from the loom file
emat <- ldat$spliced
nmat <- ldat$unspliced
#get the cellular embeddings from the Seurat tSNE object
wt.subset <- SubsetData(object = cells.combined, ident.use = c("WT"))
wt.emb <- wt.subset@dr$tsne@cell.embeddings
wt.cell.dist <- as.dist(1-armaCor(t(wt.subset@dr$cca.aligned@cell.embeddings)))
# Next major step, make sure your loom object and your seurat object use the same
# In our case, we had to append the sample name to the start of the emat and nmat (based on how we created the seurat object)
colnames(emat) <- paste("WT_",substring(colnames(emat),14,29),sep="")
colnames(nmat) <- paste("WT_",substring(colnames(nmat),14,29),sep="")
fit.quantile <- 0.02
wt_rvel.cd <- gene.relative.velocity.estimates(
emat,nmat,deltaT=1,kCells=20,
cell.dist=cell.dist,fit.quantile=fit.quantile,n.cores=24)
# Next important step -- in order to conserve tSNE coloring based on the cluster analysis
# This generates the TSNEPlot, and than extracts out the colors used for each cluster. Note the res.0.4 will change based on
# the resolution you ran the TSNE dimension reduction.
gg <- TSNEPlot(wt.subset,group.by="res.0.4")
ggplot_build(gg)$data
colors <- as.list(ggplot_build(gg)$data[[1]]$colour)
names(colors) <- rownames(wt.emb)
# Finally, create the plot
show.velocity.on.embedding.cor(wt.emb,wt_rvel.cd,n=100,scale='sqrt',
cell.colors=ac(colors,alpha=0.5),
cex=1.2,arrow.scale=20,show.grid.flow=T,
min.grid.cell.mass=1.5,grid.n=40,arrow.lwd=1.5,
do.par=F,cell.border.alpha = 0.1,
n.cores=8,main="WT Cell Louvain")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment