## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    warning = FALSE,
    message = FALSE,
    dev = "png",
    fig_crop = FALSE,
    fig.width = 7,
    fig.height = 5
)
options(bitmapType = "cairo")
library(SummarizedHeatmap)

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

## ----create-------------------------------------------------------------------
set.seed(2026)
mat <- matrix(
    rnorm(48),
    nrow = 8,
    dimnames = list(paste0("gene", seq_len(8)), paste0("sample", seq_len(6)))
)
row_info <- S4Vectors::DataFrame(
    feature_class = rep(c("marker", "background"), each = 4),
    row.names = rownames(mat)
)
column_info <- S4Vectors::DataFrame(
    condition = rep(c("control", "treated"), each = 3),
    batch = rep(c("batch 1", "batch 2"), 3),
    row.names = colnames(mat)
)

hm <- SummarizedHeatmap(
    mat,
    rowData = row_info,
    colData = column_info
)
hm

## ----state--------------------------------------------------------------------
rowOrder(hm)
colOrder(hm)
class(rowDendro(hm))
class(colDendro(hm))

## ----manual-col-order---------------------------------------------------------
hm_manual_cols <- SummarizedHeatmap(
    mat,
    rowData = row_info,
    colData = column_info,
    colOrder = rev(colnames(mat))
)
colOrder(hm_manual_cols)
is.null(colDendro(hm_manual_cols))

## ----from-se-single-----------------------------------------------------------
se <- SummarizedExperiment::SummarizedExperiment(
    assays = list(scaled = mat),
    rowData = row_info,
    colData = column_info
)
hm_from_se <- SummarizedHeatmap(se)
hm_from_se
SummarizedExperiment::assayNames(hm_from_se) # "scaled", preserved from se

## ----from-se-multi------------------------------------------------------------
se_multi <- SummarizedExperiment::SummarizedExperiment(
    assays = list(raw = mat, scaled = mat / max(abs(mat))),
    rowData = row_info,
    colData = column_info
)
hm_scaled <- SummarizedHeatmap(se_multi, assay = "scaled")
SummarizedExperiment::assayNames(hm_scaled)

## ----from-se-error, error=TRUE------------------------------------------------
try({
SummarizedHeatmap(se_multi)
})

## ----components---------------------------------------------------------------
main <- plotHeatmapMain(hm) +
    ggplot2::labs(fill = "Scaled expression") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1))
column_annotation <- plotColData(hm, vars = c("condition", "batch"))
row_annotation <- plotRowData(hm, vars = "feature_class")
column_tree <- plotColDendro(hm)
row_tree <- plotRowDendro(hm)

class(main)
class(column_annotation)

## ----complete, height = 9-----------------------------------------------------
complete <- plotHeatmap(hm)
complete

## ----manual-col-order-plot----------------------------------------------------
plotHeatmap(hm_manual_cols)

## ----custom-layout------------------------------------------------------------
stacked <- patchwork::wrap_plots(
    list(column_tree, main),
    ncol = 1, heights = c(1, 5), guides = "collect"
)
stacked

## ----free-layout--------------------------------------------------------------
custom <- patchwork::wrap_plots(
    list(patchwork::free(column_annotation, type = "space"), main),
    ncol = 1, heights = c(1, 5), guides = "collect"
)
custom

## ----extra-plot---------------------------------------------------------------
display_samples <- colnames(mat)[colOrder(hm)]
qc <- ggplot2::ggplot(
    data.frame(
        sample = factor(display_samples, levels = display_samples),
        mean = colMeans(mat)[display_samples]
    ),
    ggplot2::aes(x = sample, y = mean)
) +
    ggplot2::geom_point() +
    ggplot2::labs(x = NULL, y = "Mean\nassay\nvalue") +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1))

(qc / main) + patchwork::plot_layout(heights = c(1, 5))

## ----custom-grid--------------------------------------------------------------
row_palette <- c(marker = "#8DA0CB", background = "#E5C494")
col_palette <- c(
    control = "#66C2A5",
    treated = "#FC8D62",
    `batch 1` = "#386CB0",
    `batch 2` = "#F0027F"
)

pColDendro <- plotColDendro(hm)
pColData <- plotColData(hm, palette = col_palette)
pRowDendro <- plotRowDendro(hm)
pRowData <- patchwork::free(
    plotRowData(
        hm, vars = "feature_class", side = "top", palette = row_palette
    ),
    type = "space"
)
pMain <- plotHeatmapMain(hm)

design <- "
##AG
##BG
CDEG
"
patchwork::wrap_plots(
    A = pColDendro, B = pColData, C = pRowDendro, D = pRowData, E = pMain,
    G = patchwork::guide_area(),
    design = design,
    heights = c(1, 1, 8),
    widths = c(1, 1, 8, 2),
    guides = "collect"
)

## ----subset-rows--------------------------------------------------------------
hm_rows <- hm[1:5, ]
is.null(rowDendro(hm_rows))
rowOrder(hm_rows)
identical(colDendro(hm_rows), colDendro(hm))
identical(colOrder(hm_rows), colOrder(hm))

## ----subset-cols--------------------------------------------------------------
hm_cols <- hm[, c("sample1", "sample3", "sample5")]
is.null(colDendro(hm_cols))
colOrder(hm_cols)
identical(rowDendro(hm_cols), rowDendro(hm))
identical(rowOrder(hm_cols), rowOrder(hm))

## ----subset-both--------------------------------------------------------------
hm_both <- hm[1:5, c("sample1", "sample3", "sample5")]
is.null(rowDendro(hm_both))
is.null(colDendro(hm_both))

## ----recluster----------------------------------------------------------------
hm_both <- clusterHeatmap(hm_both)
validObject(hm_both)
class(rowDendro(hm_both))
class(colDendro(hm_both))

## ----assay-replace------------------------------------------------------------
hm_new_values <- hm
SummarizedExperiment::assay(hm_new_values) <-
    SummarizedExperiment::assay(hm_new_values) * -1
is.null(rowDendro(hm_new_values))
is.null(colDendro(hm_new_values))
rowOrder(hm_new_values)
colOrder(hm_new_values)

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

