## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", warning = FALSE
)

## ----installation, eval=FALSE-------------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE)) {
#   install.packages("BiocManager")
# }
# 
# BiocManager::install("DAssemble")

## ----airway-example-----------------------------------------------------------
data("airway", package = "airway")
counts <- SummarizedExperiment::assay(airway, "counts")
keep_genes <- order(rowSums(counts), decreasing = TRUE)[seq_len(500L)]
counts <- counts[keep_genes, , drop = FALSE]
metadata <- as.data.frame(SummarizedExperiment::colData(airway))
metadata <- metadata[colnames(counts), , drop = FALSE]

se <- SummarizedExperiment::SummarizedExperiment(
  assays = list(counts = counts)
)

mae <- MultiAssayExperiment::MultiAssayExperiment(
  experiments = list(rnaseq = se),
  colData = S4Vectors::DataFrame(metadata)
)

res <- DAssemble::DAssemble(
  features = mae,
  assay_name = "rnaseq",
  core_method = "DESeq2",
  enhancers = c("WLX", "LR"),
  expVar = "dex",
  p_adj = "BH",
  enhancer_norm = "tmm",
  return_components = TRUE,
  return_subensembles = TRUE
)

head(res$res)
names(res$components)

## ----microbiome-example-------------------------------------------------------
data("GlobalPatterns", package = "phyloseq")
gp <- GlobalPatterns

otu <- as(phyloseq::otu_table(gp), "matrix")
metadata <- as.data.frame(phyloseq::sample_data(gp))

keep <- metadata$SampleType %in% c("Feces", "Soil")
otu <- otu[, keep, drop = FALSE]
keep_taxa <- order(rowSums(otu), decreasing = TRUE)[seq_len(250L)]
features <- as.data.frame(t(otu[keep_taxa, , drop = FALSE]))
metadata <- metadata[keep, , drop = FALSE]
metadata$group <- droplevels(factor(metadata$SampleType))
stopifnot(nlevels(metadata$group) == 2L)

se <- SummarizedExperiment::SummarizedExperiment(
  assays = list(counts = t(as.matrix(features)))
)

mae <- MultiAssayExperiment::MultiAssayExperiment(
  experiments = list(microbiome = se),
  colData = S4Vectors::DataFrame(metadata)
)

res <- DAssemble::DAssemble(
  features = mae,
  assay_name = "microbiome",
  core_method = NULL,
  enhancers = c("WLX", "LR", "KS"),
  expVar = "group",
  enhancer_norm = "clr",
  return_components = TRUE,
  return_subensembles = TRUE
)

head(res$res)
names(res$components)
head(res$ensembles)

## ----single-cell-example------------------------------------------------------
set.seed(1)
n_genes <- 120L
n_cells <- 20L

cell_type <- factor(rep(c("control", "treated"), each = n_cells / 2L))
counts <- matrix(
  stats::rnbinom(n_genes * n_cells, mu = 20, size = 5),
  nrow = n_genes,
  dimnames = list(
    paste0("gene", seq_len(n_genes)),
    paste0("cell", seq_len(n_cells))
  )
)

counts[seq_len(12L), cell_type == "treated"] <-
  counts[seq_len(12L), cell_type == "treated"] + 15L

metadata <- data.frame(
  CellType = cell_type,
  row.names = colnames(counts)
)

sce <- SingleCellExperiment::SingleCellExperiment(
  assays = list(counts = counts),
  colData = S4Vectors::DataFrame(metadata)
)

mae <- MultiAssayExperiment::MultiAssayExperiment(
  experiments = list(single_cell = sce),
  colData = S4Vectors::DataFrame(metadata)
)

res <- DAssemble::DAssemble(
  features = mae,
  assay_name = "single_cell",
  core_method = "edgeR",
  enhancers = c("WLX", "LR", "KS"),
  enhancer_norm = "TSS",
  expVar = "CellType",
  p_adj = "BH",
  return_components = TRUE,
  return_subensembles = TRUE
)

head(res$res)
names(res$components)

## ----session-info-------------------------------------------------------------
sessionInfo()

