## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
    collapse  = TRUE,
    comment   = "#>",
    fig.align = "center",
    fig.width = 6,
    fig.height = 5,
    message   = FALSE
)
library(levi)

## ----data---------------------------------------------------------------------
hub_net <- system.file("extdata", "hub_network.dat", package = "levi")
genes   <- c("HUB", paste0("N", 1:8))

# Four control and four treated replicates on a log2 scale. The hub and its
# first two neighbours respond to treatment; the rest do not.
set.seed(2026)
expression <- matrix(rnorm(9 * 8, mean = 6, sd = 0.3), 9, 8,
                     dimnames = list(genes, paste0("s", 1:8)))
groups <- rep(c("control", "treated"), each = 4)
expression[c("HUB", "N1", "N2"), groups == "treated"] <-
    expression[c("HUB", "N1", "N2"), groups == "treated"] + 1.5

## ----landscape, fig.height=5--------------------------------------------------
lfc <- rowMeans(expression[, groups == "treated"]) -
       rowMeans(expression[, groups == "control"])

set.seed(1)
res <- levi(
    expressionInput         = data.frame(ID = genes, logFC = lfc),
    networkCoordinatesInput = hub_net,
    fileTypeInput           = "dat",
    geneSymbolInput         = "ID",
    readExpColumn           = readExpColumn("logFC-logFC"),
    signal_mode             = "logfc",
    resolutionValueInput    = 20,
    smoothValueInput        = 30,
    n_perm                  = 199)

res$regions$summary[, c("Region", "Direction", "Cells", "Mass",
                        "PSpatial", "Significant")]

## ----region-genes-------------------------------------------------------------
head(leviRegionGenes(res, top_n = 3))

## ----replicate, fig.height=5--------------------------------------------------
set.seed(1)
rep_res <- leviReplicateInference(
    expression, groups, test = "treated", control = "control",
    networkCoordinatesInput = hub_net, fileTypeInput = "dat",
    resolutionValueInput = 20, smoothValueInput = 30)

rep_res$regions$summary[, c("Region", "Direction", "Mass", "PSpatial",
                            "Significant")]
rep_res$metadata$possible_permutations

## ----tfce---------------------------------------------------------------------
tfce <- leviGraphTFCEInference(
    expression, groups, hub_net, test = "treated", control = "control")
tfce$statistic[order(tfce$statistic$PGlobal), ][1:4, ]

## ----rewiring-----------------------------------------------------------------
scores <- setNames(tfce$statistic[["T"]], tfce$statistic$Gene)
set.seed(1)
rew <- leviGraphRewiringInference(scores, hub_net, threshold = 1.5,
                                  n_perm = 199)
rew$regions$summary[, c("Region", "Direction", "Nodes", "Mass", "PSpatial")]

## ----moran--------------------------------------------------------------------
set.seed(1)
moran <- leviGraphMoran(scores, hub_net, n_perm = 199)
moran$global
head(leviGraphGetisOrd(scores, hub_net, n_perm = 199, seed = 1)[,
    c("Gene", "GiStar", "PTwoSided", "PAdjusted", "Class")])

## ----parallel, eval=FALSE-----------------------------------------------------
# library(BiocParallel)
# tfce_par <- leviGraphTFCEInference(
#     expression, groups, hub_net, test = "treated", control = "control",
#     n_perm = 999, BPPARAM = MulticoreParam(4))

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

