## ----echo=FALSE, results="hide"-----------------------------------------------
knitr::opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE)
self <- BiocStyle::Biocpkg("augere.solo")

## -----------------------------------------------------------------------------
library(scRNAseq)
sce.z <- ZeiselBrainData()
sce.z

## ----fig.keep="none"----------------------------------------------------------
library(augere.solo)
outdir.z <- tempfile()
res.z <- runSolo(
    sce.z,
    qc.mito.regex = "^mt-",
    num.threads = 2, # speed it up a little
    output.dir = outdir.z
)

## -----------------------------------------------------------------------------
# Checking out the QC statistics.
res.z$qc.rna

# Checking out the reduced dimensions.
library(scater)
plotReducedDim(res.z$sce, "TSNE", colour_by = "graph.cluster")

# Checking out the marker rankings for each cluster.
# We use previewMarkers() just for a prettier print.
scrapper::previewMarkers(res.z$markers.rna[["1"]])

## -----------------------------------------------------------------------------
fname <- file.path(outdir.z, "report.Rmd")
cat(head(readLines(fname), 50), sep="\n")

## -----------------------------------------------------------------------------
reloaded <- readResult(file.path(outdir.z, "results", "markers-rna", "2"))
scrapper::previewMarkers(reloaded$x) # preview of the marker rankings
str(reloaded$metadata) # along with some metadata

## -----------------------------------------------------------------------------
sce.t <- TasicBrainData()
common.brain <- intersect(rownames(sce.z), rownames(sce.t))
combined.brain <- combineCols(
    sce.z[common.brain,],
    sce.t[common.brain,]
)
combined.brain$study <- rep(c("zeisel", "tasic"), c(ncol(sce.z), ncol(sce.t)))
combined.brain

## ----fig.keep="none"----------------------------------------------------------
outdir.com <- tempfile()
res.com <- runSolo(
    combined.brain,
    block = "study",
    qc.mito.regex = "^mt-",
    output.dir = outdir.com,

    # Just setting these options to reduce the runtime,
    # otherwise this vignette takes too long to build.
    num.threads = 2,
    suppress.plots = TRUE,
    save.results = FALSE
)

## -----------------------------------------------------------------------------
# Now we have an extra set of MNN-corrected coordinates. 
reducedDimNames(res.com$sce)

# Examining the distribution of clusters between studies.
table(res.com$sce$graph.cluster, res.com$sce$study)

# Visualizing the distribution in the UMAP space.
plotReducedDim(res.com$sce, "UMAP", colour_by = "study")

## -----------------------------------------------------------------------------
sce.k <- KotliarovPBMCData()
sce.k <- sce.k[,1:1000] # save some time for the build system.
sce.k

## ----fig.keep="none"----------------------------------------------------------
outdir.k <- tempfile()
res.k <- runSolo(
    sce.k,
    qc.mito.regex = "^MT-",
    adt.experiment = "ADT",
    output.dir = outdir.k,

    # Just setting these options to reduce the runtime,
    # otherwise this vignette takes too long to build.
    num.threads = 2,
    suppress.plots = TRUE,
    save.results = FALSE
)

## -----------------------------------------------------------------------------
# Now we have an extra set of combined coordinates. 
reducedDimNames(res.k$sce)

# Checking out the ADT-specific QC metrics.
res.k$qc.adt

# Checking out the reduced dimensions.
plotReducedDim(res.k$sce, "TSNE", colour_by = "graph.cluster")

# We can also look at the ADT-specific markers for a cluster.
scrapper::previewMarkers(res.k$markers.adt[[1]])

## ----fig.keep="none"----------------------------------------------------------
outdir.celldex <- tempfile()
sub.z <- sce.z[,1:1000] # subsetting to save some time for the build system.
pred.celldex <- runAnnotate(
    sub.z,
    configureReferenceAnnotation("MouseRNAseqData", "label.main"),
    output.dir = outdir.celldex,

    # Just setting these options to reduce the runtime,
    # otherwise this vignette takes too long to build.
    num.threads = 2,
    suppress.plots = TRUE,
    save.results = FALSE
)
table(pred.celldex$predictions[[1]]$labels)

## ----fig.keep="none"----------------------------------------------------------
outdir.tasic <- tempfile()
pred.tasic <- runAnnotate(
    sub.z,
    configureReferenceAnnotation(sce.t, "primary_type", ref.assay="counts", ref.aggregate=TRUE),
    output.dir = outdir.tasic,

    # Just setting these options to reduce the runtime,
    # otherwise this vignette takes too long to build.
    num.threads = 2,
    suppress.plots = TRUE,
    save.results = FALSE
)
table(pred.tasic$predictions[[1]]$labels)

## ----fig.keep="none"----------------------------------------------------------
outdir.multi <- tempfile()
pred.multi <- runAnnotate(
    sub.z,
    list(
        rnaseq=configureReferenceAnnotation("MouseRNAseqData", "label.main"),
        immgen=configureReferenceAnnotation("ImmGenData", "label.main")
    ),
    output.dir = outdir.multi,

    # Just setting these options to reduce the runtime,
    # otherwise this vignette takes too long to build.
    num.threads = 2,
    suppress.plots = TRUE,
    save.results = FALSE
)

# Results for annotation against the individual references:
names(pred.multi$predictions)

# As well as the combined results:
table(pred.multi$combined$labels, pred.multi$combined$reference)

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

