Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Last active May 15, 2023 23:56
Show Gist options
  • Select an option

  • Save vjcitn/b7a44c748c193663525ec227812f0af3 to your computer and use it in GitHub Desktop.

Select an option

Save vjcitn/b7a44c748c193663525ec227812f0af3 to your computer and use it in GitHub Desktop.
produce a plotly biplot for labeled PBMCs
# this code will process 4k PBMCs from TENx with SingleR and scater
# compute some approximate PCs and produce an interactive scatterplot
# in PC space with a biplot
ii = rownames(installed.packages())
if (!("BiocManager" %in% ii))install.packages("BiocManager")
req = c("celldex", "TENxPBMCData", "SingleR", "irlba", "scater",
"scales", "ggplot2", "devtools", "plotly")
needed = setdiff(req, ii)
if (length(needed)>0) BiocManager::install(needed, ask=FALSE, update=FALSE)
library(celldex)
hd = celldex::HumanPrimaryCellAtlasData()
library(TENxPBMCData)
vp = TENxPBMCData("pbmc4k")
vp
hassym = which(!is.na(rowData(vp)$Symbol))
vps = vp[hassym,]
rownames(vps) = rowData(vps)$Symbol
library(scater)
vps = logNormCounts(vps)
vps
library(SingleR)
vsing = SingleR(vps, hd, hd$label.main)
vps$label.main = vsing$labels
vpssds = rowSds(assay(vps))
kp = which(vpssds > quantile(vpssds, .8))
vpslim = vps[kp,]
mat = t(as.matrix(assay(vpslim,2)))
library(irlba)
apca = prcomp_irlba(mat, 4)
bb = "ba60f07ac0d972de80d549f8659a19707a263edc"
fbcode = devtools::source_url("https://gist.githubusercontent.com/vjcitn/ebcb221cdb8b913d3f41d81be43c1294/raw/3f88df12c169ef0afdd138c578879217545f3045/filtered_biplot.R", sha1=bb)
filtered_biplot = fbcode$value
library(scales)
library(ggplot2)
npl = filtered_biplot(apca, vpslim, nvar=10)
library(plotly)
ggplotly(npl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment