## ----setup, include=FALSE-----------------------------------------------------
options(width=80)
knitr::opts_chunk$set(collapse=TRUE,
                      message=FALSE,
                      warning=FALSE,
                      comment="",
                      fig.align="center",
                      fig.wide=TRUE)

## ----message=FALSE, warning=FALSE---------------------------------------------
library(SingleCellExperiment)
library(TENxPBMCData)

sce <- TENxPBMCData(dataset="pbmc4k")
sce

## ----message=FALSE, warning=FALSE---------------------------------------------
library(scrapper)

is_mito <- grepl("^MT-", rowData(sce)$Symbol_TENx)
table(is_mito)

## -----------------------------------------------------------------------------
sce <- quickRnaQc.se(sce, subsets=list(mito=is_mito))
sce <- sce[, sce$keep]
dim(sce)

## -----------------------------------------------------------------------------
cellsxgene <- rowSums(counts(sce) > 0)
sce <- sce[cellsxgene > floor(ncol(sce)*0.01), ]
dim(sce)

## -----------------------------------------------------------------------------
sce <- normalizeRnaCounts.se(sce, size.factors=sce$sum)
assayNames(sce)

## ----message=FALSE, warning=FALSE---------------------------------------------
library(GSEABase)
library(GSVA)

fname <- file.path(system.file("extdata", package="GSVAdata"),
                   "pbmc_cell_type_gene_set_signatures.gmt.gz")
gsets <- readGMT(fname)
gsets

## -----------------------------------------------------------------------------
gsvaAnnotation(sce) <- ENSEMBLIdentifier("org.Hs.eg.db")
gsvaAnnotation(sce)

## -----------------------------------------------------------------------------
gsvapar <- gsvaParam(sce, gsets)
gsvapar

## -----------------------------------------------------------------------------
gsvarownorm <- gsvaRowNorm(gsvapar)
gsvarownorm
assayNames(gsvarownorm)

## -----------------------------------------------------------------------------
gsvacolranks <- gsvaColRanks(gsvarownorm)
gsvacolranks
assayNames(gsvacolranks)

## -----------------------------------------------------------------------------
es <- gsvaColScores(gsvacolranks)
es

## ----eval=FALSE---------------------------------------------------------------
# es2 <- gsvaColScores(gsvacolranks, alternative_gsets)

## ----message=FALSE, warning=FALSE---------------------------------------------
library(bluster)

g <- makeSNNGraph(t(assay(es)), k=20)

## ----message=FALSE, warning=FALSE---------------------------------------------
library(igraph)

colLabels(es) <- factor(cluster_walktrap(g)$membership)
table(colLabels(es))

## -----------------------------------------------------------------------------
whmax <- apply(assay(es), 2, which.max)
gsxlab <- split(rownames(es)[whmax], colLabels(es))
gsxlab <- names(sapply(sapply(gsxlab, table), which.max))
colLabels(es) <- factor(gsub("[0-9]\\.", "", gsxlab))[colLabels(es)]
table(colLabels(es))

## ----scpcaclusters, echo=TRUE, message=FALSE, warning=FALSE, fig.height=5, fig.width=6, out.width="600px", fig.cap="Cell type assignments of PBMC scRNA-seq data, based on GSVA scores."----
library(RColorBrewer)

res <- prcomp(assay(es))
varexp <- res$sdev^2 / sum(res$sdev^2)
nclusters <- nlevels(colLabels(es))
hmcol <- colorRampPalette(brewer.pal(nclusters, "Set1"))(nclusters)
par(mar=c(4, 5, 1, 1))
plot(res$rotation[, 1], res$rotation[, 2], col=hmcol[colLabels(es)], pch=19,
     xlab=sprintf("PCA 1 (%.0f%%)", varexp[1]*100),
     ylab=sprintf("PCA 2 (%.0f%%)", varexp[2]*100),
     las=1, cex.axis=1.2, cex.lab=1.5)
mask <- colLabels(es) == "NK_CELLS_RESTING"
points(res$rotation[mask, 1], res$rotation[mask, 2], ## show the overlap better
       col=hmcol[colLabels(es)[mask]], pch=19)
legend("bottomright", gsub("_", " ", levels(colLabels(es))), fill=hmcol, inset=0.01)

## ----gsvaenrichment, echo=TRUE, fig.height=5, fig.width=5, out.width="600px", fig.cap="GSVA enrichment plot of the EOSINOPHILS gene set in the expression profile of the first cell annotated to that cell type."----
firstmonocytecell <- which(colLabels(es) == "MONOCYTES")[1]
par(mar=c(4, 5, 1, 1))
gsvaEnrichment(gsvacolranks, column=firstmonocytecell, geneSet="MONOCYTES",
               cex.axis=1.2, cex.lab=1.5, plot="ggplot")

## ----session_info, cache=FALSE------------------------------------------------
sessionInfo()

