## ----echo=FALSE, results="hide", message=FALSE--------------------------------
require(knitr)
opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE)

## ----setup, echo=FALSE, message=FALSE-----------------------------------------
library(scran)
library(BiocParallel)
set.seed(100)

self <- BiocStyle::Biocpkg("scran")

## -----------------------------------------------------------------------------
library(scran)
library(scRNAseq)
sce <- GrunPancreasData()
sce

# Quickly analyzing it to generate bits and pieces that we can
# reuse in the rest of this vignette.
library(scrapper)
results <- analyze.se(sce, num.threads=2)
sce.ready <- results$x

## -----------------------------------------------------------------------------
library(scran)
clusters <- quickCluster(sce.ready)
sce.normed <- computeSumFactors(sce.ready, clusters=clusters)
summary(sizeFactors(sce.normed))

## -----------------------------------------------------------------------------
subout <- quickSubCluster(sce.ready, groups=clusters)
table(metadata(subout)$subcluster) # formatted as '<parent>.<subcluster>'

## -----------------------------------------------------------------------------
var.explained <- metadata(sce.ready)$PCA$variance.explained
stats <- rowData(sce.ready)
total.tech.var.in.hvgs <- sum(stats[stats$hvg,"fitted"])
total.var.in.hvgs <- sum(stats[stats$hvg,"variances"])
chosen <- denoisePCANumber(var.explained, total.tech.var.in.hvgs, total.var.in.hvgs)
chosen

## -----------------------------------------------------------------------------
reducedDim(sce.ready, "PCA.denoised") <- reducedDim(sce.ready, "PCA")[,1:chosen]

## -----------------------------------------------------------------------------
# Using the first 200 HVs, which are the most interesting anyway.
of.interest <- order(rowData(sce.ready)$residuals, decreasing=TRUE)[1:200]
cor.pairs <- correlatePairs(sce.ready, subset.row=of.interest)
cor.pairs

## -----------------------------------------------------------------------------
cor.pairs2 <- correlatePairs(sce.ready, subset.row=of.interest, block=sce.ready$donor)

## -----------------------------------------------------------------------------
cor.genes <- correlateGenes(cor.pairs)
cor.genes

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

