## ----knitr-options, echo = FALSE, message = FALSE, warning = FALSE------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
suppressPackageStartupMessages({
    library(simPIC)
    library(SingleCellExperiment)
})

microglia_vignette_deps <- c(
    "splatter",
    "VariantAnnotation",
    "bluster",
    "preprocessCore"
)
microglia_vignette_ready <- all(vapply(
    microglia_vignette_deps,
    requireNamespace,
    logical(1),
    quietly = TRUE
))

## ----extdata------------------------------------------------------------------
extdir.candidates <- c(
    file.path("inst", "extdata"),
    file.path("..", "inst", "extdata"),
    system.file("extdata", package = "simPIC")
)
extdir <- extdir.candidates[
    file.exists(file.path(extdir.candidates, "microglia_sce.rds"))
][1]
stopifnot(!is.na(extdir), nzchar(extdir), dir.exists(extdir))
list.files(extdir, pattern = "^microglia")

## ----sce-summary--------------------------------------------------------------
microglia_sce <- readRDS(file.path(extdir, "microglia_sce.rds"))
microglia_sce
head(colData(microglia_sce)[, c("Sample", "Library", "Batch", "sample_batch", "Pathology")])
peak.cols <- intersect(
    c("seqnames", "start", "end", "peak_id", "original_peakid"),
    colnames(rowData(microglia_sce))
)
head(rowData(microglia_sce)[, peak.cols])

## ----run-example, message = FALSE, warning = FALSE, eval = microglia_vignette_ready----
microglia_out <- simPICMicrogliaExample(
    min.cells = 20,
    pop.cv.bins = 10,
    eqtl.n = 0.05,
    similarity.scale = 2,
    eqtl.dist = 1e8,
    sparsify = FALSE,
    pca.ntop = 100,
    pca.components = 5,
    plot.n.samples = 4,
    seed = 101,
    verbose = TRUE
)

## ----inspect-example, eval = microglia_vignette_ready-------------------------
names(microglia_out)
microglia_out$plot_samples
microglia_out$params
microglia_out$sim

## ----plots, fig.width = 11, fig.height = 7, eval = microglia_vignette_ready----
microglia_out$plots$comparison$cell_pca_by_sample
microglia_out$plots$comparison$sample_pca

microglia_out$plots$bluster$cell_pca_by_sample
microglia_out$plots$bluster$silhouette_width
microglia_out$plots$bluster$neighborhood_purity

## ----object-summary, eval = microglia_vignette_ready--------------------------
names(microglia_out)
dim(microglia_out$sce)
dim(microglia_out$bigcounts)
dim(microglia_out$aggregated)
dim(microglia_out$sim)
dim(microglia_out$comparison_real)
dim(microglia_out$comparison_sim)

## ----bigcounts-details, eval = microglia_vignette_ready-----------------------
microglia_out$bigcounts
table(microglia_out$bigcounts$sample_batch)
table(microglia_out$bigcounts$Sample)

## ----comparison-summary, eval = microglia_vignette_ready----------------------
microglia_out$plot_samples
table(microglia_out$sce$Sample)
if ("Sample" %in% colnames(colData(microglia_out$sim))) {
    table(colData(microglia_out$sim)$Sample)
}

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

