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

## ----install, eval=FALSE------------------------------------------------------
# # You can install the stable version of barbieQ like so:
# if (!require("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# 
# BiocManager::install("barbieQ")
# 
# ## Alternatively, you can install the development version of barbieQ from GitHub
# # devtools::install_github("Oshlack/barbieQ")

## ----library------------------------------------------------------------------
suppressPackageStartupMessages({
  library(barbieQ)
  library(magrittr)
  library(tidyr)
  library(dplyr)
  library(ggplot2)
  library(circlize)
  library(logistf)
  library(igraph)
  library(data.table)
  library(ComplexHeatmap)
  library(limma)
  library(SummarizedExperiment)
  library(S4Vectors)
})
set.seed(2025)

## ----data---------------------------------------------------------------------
data(monkeyHSPC, package = "barbieQ")

## ----example, fig.width=8, fig.height=6---------------------------------------
## Passing `object`, `sampleMetadata` and `factorColors` for optional
exampleBBQ <- createBarbieQ(
  object = SummarizedExperiment::assay(monkeyHSPC), 
  sampleMetadata = SummarizedExperiment::colData(monkeyHSPC)$sampleMetadata
)

## ----update metadata----------------------------------------------------------
updateSampleMetadata <- exampleBBQ$sampleMetadata %>% 
  as.data.frame() %>% 
  select(Celltype, Months) %>% 
  mutate(Phase = ifelse(Months < 6, "early", ifelse(Months >=55, "late", "mid"))) %>% 
  mutate(Celltype = gsub("(Gr).*", "\\1", Celltype))

SummarizedExperiment::colData(exampleBBQ)$sampleMetadata <- S4Vectors::DataFrame(updateSampleMetadata)

exampleBBQ$sampleMetadata

## ----subset samples-----------------------------------------------------------
flag_sample <- exampleBBQ$sampleMetadata$Phase == "mid"
exampleBBQ <- exampleBBQ[, flag_sample]
exampleBBQ$sampleMetadata

## ----tag top------------------------------------------------------------------
## Check out minimum group size.
table(exampleBBQ$sampleMetadata$Celltype)
## Tag top Barcodes.
exampleBBQ <- tagTopBarcodes(barbieQ = exampleBBQ, nSampleThreshold = 6)

## ----tag top v1, fig.width=8, fig.height=6------------------------------------
## visualize contribution of top vs. bottom barcodes
plotBarcodePareto(barbieQ = exampleBBQ) |> plot()

## ----tag top v2, fig.width=5, fig.height=3------------------------------------
## visualize collective contribution of top vs. bottom barcodes
plotBarcodeSankey(barbieQ = exampleBBQ) |> plot()

## ----subset top barcodes------------------------------------------------------
flag_barcode <- SummarizedExperiment::rowData(exampleBBQ)$isTopBarcode$isTop
exampleBBQ <- exampleBBQ[flag_barcode,]

## ----sample pair cor, fig.width=6, fig.height=4-------------------------------
## visualize sample pair wise correlation
plotSamplePairCorrelation(barbieQ = exampleBBQ) |> plot()

## ----test diffProp, fig.width=6, fig.height=5---------------------------------
## test Barcode differential proportion between sample groups
exampleBBQ <- testBarcodeSignif(
  barbieQ = exampleBBQ,
  contrastFormula = "(CelltypeNK_CD56n_CD16p) - (CelltypeB+CelltypeGr+CelltypeT+CelltypeNK_CD56p_CD16n)/4",
  method = "diffProp"
)

## ----vis MA plot, fig.width=8, fig.height=4-----------------------------------
plotBarcodeMA(exampleBBQ) |> plot()

## ----vis testing HP, fig.width=14, fig.height=6-------------------------------
plotSignifBarcodeHeatmap(exampleBBQ) |> plot()

## ----vis testing prop, fig.width=8, fig.height=5------------------------------
plotSignifBarcodeProportion(exampleBBQ) |> plot()

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

