## ----style, echo=FALSE, results='hide', message=FALSE, cache=FALSE---------
library(BiocStyle)
library(knitr)
opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE, cache=TRUE)
opts_chunk$set(fig.asp=1)

## ---- cache=FALSE, echo=FALSE, results="hide"------------------------------
simpleSingleCell:::.compile("tenx") # PBMC

## --------------------------------------------------------------------------
library(TENxBrainData)
sce <- TENxBrainData20k() # downloads once and caches it for future use.
sce

## --------------------------------------------------------------------------
counts(sce)
object.size(counts(sce))
file.info(path(counts(sce)))$size

## --------------------------------------------------------------------------
tmp <- counts(sce)
tmp <- log2(tmp + 1)
tmp

## --------------------------------------------------------------------------
library(scater)
sce <- calculateQCMetrics(sce, compact=TRUE) # compacting for clean output.
sce$scater_qc

## --------------------------------------------------------------------------
bpp <- MulticoreParam(2)
bpp

## --------------------------------------------------------------------------
bpp <- SnowParam(5)
bpp

## ---- eval=FALSE-----------------------------------------------------------
#  bpp <- BatchtoolsParam(10, cluster="slurm",
#  	resources=list(walltime=20000, memory=8000, ncpus=1))

## --------------------------------------------------------------------------
alt <- calculateQCMetrics(sce, BPPARAM=MulticoreParam(2), compact=TRUE)

## ---- echo=FALSE-----------------------------------------------------------
if (!isTRUE(all.equal(alt, sce))) {
	stop("parallelization changes the result")
}

## --------------------------------------------------------------------------
all.equal(alt, sce) 

## --------------------------------------------------------------------------
sce.pbmc <- readRDS("pbmc_data.rds")

## --------------------------------------------------------------------------
library(scran)
library(BiocNeighbors)
snn.gr <- buildSNNGraph(sce.pbmc, BNPARAM=AnnoyParam(), use.dimred="PCA")

## --------------------------------------------------------------------------
clusters <- igraph::cluster_walktrap(snn.gr)
table(Exact=sce.pbmc$Cluster, Approx=clusters$membership)

## --------------------------------------------------------------------------
library(BiocSingular)

# As the name suggests, it is random, so we need to set the seed.
set.seed(999)    
r.out <- BiocSingular::runPCA(t(logcounts(sce.pbmc)), rank=20, 
    BSPARAM=RandomParam(deferred=TRUE))
str(r.out)

# For comparison:    
i.out <- BiocSingular::runPCA(t(logcounts(sce.pbmc)), rank=20, 
    BSPARAM=IrlbaParam(fold=Inf))
str(i.out)

## --------------------------------------------------------------------------
sessionInfo()