# sechm

# Getting started

The sechm package is a wrapper around the ComplexHeatmap package to facilitate the creation of annotated heatmaps from objects of the Bioconductor class SummarizedExperiment (and extensions thereof).

## Package installation

    if (!requireNamespace("BiocManager", quietly = TRUE))
        install.packages("BiocManager")
    BiocManager::install("sechm")

## Example data

To showcase the main functions, we will use an example object which contains (a subset of) RNAseq of mouse hippocampi after Forskolin-induced long-term potentiation:

    suppressPackageStartupMessages({
      library(SummarizedExperiment)
      library(sechm)
    })
    data("Chen2017", package="sechm")
    SE <- Chen2017

This is taken from Chen et al., 2017.

# Example usage

## Basic functionalities

The `sechm` function simplifies the generation of heatmaps from `SummarizedExperiment`. It minimally requires, as input, a `SummarizedExperiment` object and a set of genes (or features, i.e. rows of `sechm`) to plot:

    g <- c("Egr1", "Nr4a1", "Fos", "Egr2", "Sgk1", "Arc", "Dusp1", "Fosb", "Sik1")
    sechm(SE, features=g)

    ## Using assay 'logFC'

    # with row scaling:
    sechm(SE, features=g, do.scale=TRUE)

    ## Using assay 'logFC'

The assay can be selected, and any `rowData` or `colData` columns can be specified as annotation:

    rowData(SE)$meanLogCPM <- rowMeans(assays(SE)$logcpm)
    sechm(SE, features=g, assayName="logFC", top_annotation=c("Condition","Time"), left_annotation=c("meanLogCPM"))

Column names are ommitted by default, but can be displayed:

    sechm(SE, features=g, do.scale=TRUE, show_colnames=TRUE)

    ## Using assay 'logFC'

Since `sechm` uses the ComplexHeatmap engine for plotting, any argument of `ComplexHeatmap::Heatmap` can be passed:

    sechm(SE, features=g, do.scale=TRUE, row_title="My genes")

    ## Using assay 'logFC'

When plotting a lot of rows, by default row names are not shown (can be overriden), but specific genes can be highlighted with the `mark` argument:

    sechm(SE, features=row.names(SE), mark=g, do.scale=TRUE, top_annotation=c("Condition","Time"))

    ## Using assay 'logFC'

We can also add gaps using the same columns:

    sechm(SE, features=g, do.scale=TRUE, top_annotation="Time", gaps_at="Condition")

    ## Using assay 'logFC'

## Row ordering

By default, rows are sorted using the MDS angle method (can be altered with the `sort.method` argument); this can be disabled with:

    # reverts to clustering:
    sechm(SE, features=row.names(SE), do.scale=TRUE, sortRowsOn=NULL)

    ## Using assay 'logFC'

    # no reordering:
    sechm(SE, features=row.names(SE), do.scale=TRUE, sortRowsOn=NULL,
          cluster_rows=FALSE)

    ## Using assay 'logFC'

It is also possible to combine sorting with clusters using the `toporder` argument, or using gaps:.

    # we first cluster rows, and save the clusters in the rowData:
    rowData(SE)$cluster <- as.character(kmeans(t(scale(t(assay(SE)))),5)$cluster)
    sechm(SE, features=1:30, do.scale=TRUE, toporder="cluster",
          left_annotation="cluster", show_rownames=FALSE)

    ## Using assay 'logFC'

    ## Timing stopped at: 0.001 0 0

    sechm(SE, features=1:30, do.scale=TRUE, gaps_row="cluster",
          show_rownames=FALSE)

    ## Using assay 'logFC'

## Color scale

`sechm` tries to guess whether the data plotted are centered around zero, and adjusts the scale accordingly (this can be disable with `breaks=FALSE`). It also performs a quantile capping to avoid extreme values taking most of the color scale, which is especially relevant when plotting for instance fold-changes. This can be controlled with the `breaks` argument. Consider the three following examples:

    library(ComplexHeatmap)
    g2 <- c(g,"Gm14288",tail(row.names(SE)))
    draw(
        sechm(SE, features=g2, assayName="logFC", breaks=1, column_title="breaks=1") +
        sechm(SE, features=g2, assayName="logFC", breaks=0.995,
              column_title="breaks=0.995", name="logFC(2)") +
        sechm(SE, features=g2, assayName="logFC", breaks=0.985,
              column_title="breaks=0.985", name="logFC(3)"),
        merge_legends=TRUE)

With `breaks=1`, the scale is made symmetric, but not quantile capping is performed. In this way, most of the colorscale is taken by the difference between one datapoint (first gene) and the rest, making it difficult to distinguish patterns in the genes at the bottom. Instead, with `breaks=0.985`, the color scale is linear up until the 0.985 quantile of the data, and ordinal after this. This reduces our capacity to distinguish variations between the extreme values, but enables us to visualize the others better.

Manual breaks can also be defined. The colors themselves can be passed as follows:

    # not run
    sechm(SE, features=g2, hmcols=viridisLite::cividis(10))

## Annotation colors

Annotation colors can be passed with the `anno_colors` argument, but the simplest is to store them in the object’s metadata:

    metadata(SE)$anno_colors

    ## $Time
    ##          30          60         120        <NA>
    ## "#90EE90FF" "#65BE61FF" "#3A9034FF" "#006400FF"
    ##
    ## $Condition
    ##     Control   Forskolin
    ## "lightgrey"   "Darkred"

    metadata(SE)$anno_colors$Condition <- c(Control="white", Forskolin="black")
    sechm(SE, features=g2, top_annotation="Condition")

    ## Using assay 'logFC'

Heatmap colors can be passed on in the same way:

    metadata(SE)$hmcols <- c("darkred","white","darkblue")
    sechm(SE, g, do.scale = TRUE)

    ## Using assay 'logFC'

The default assay to be displayed and the default annotation fields to show can be specified in the `default_view` metadata element, as follows:

    metadata(SE)$default_view <- list(
      assay="logFC",
      top_annotation="Condition"
    )

Finally, it is also possible to set colors as package-wide options:

    setSechmOption("hmcols", value=c("white","grey","black"))
    sechm(SE, g, do.scale = TRUE)

At the moment, the following arguments can be set as global options: `assayName`, `hmcols`, `left_annotation`, `right_annotation`, `top_annotation`, `bottom_annotation`, `anno_colors`, `gaps_at`, `breaks`.

To remove the predefined colors:

    resetAllSechmOptions()
    metadata(SE)$hmcols <- NULL
    metadata(SE)$anno_colors <- NULL

In order of priority, the arguments in the function call trump the object’s metadata, which trumps the global options.

# crossHm

Because `sechm` produces a `Heatmap` object from ComplexHeatmap, it is possible to combine them:

    sechm(SE, features=g) + sechm(SE, features=g)

    ## Warning: Heatmap/annotation names are duplicated: logFC

However, doing so involves manual work to ensure that the labels and colors are nice and coherent, and that the rows names match. As a convenience, we provide the `crossHm` function to handle these issues. `crossHm` works with a list of `SummarizedExperiment` objects:

    # we build another SE object and introduce some variation in it:
    SE2 <- SE
    assays(SE2)$logcpm <- jitter(assays(SE2)$logcpm, factor=1000)
    crossHm(list(SE1=SE, SE2=SE2), g, do.scale = TRUE,
            top_annotation=c("Condition","Time"))

    ## Using assay 'logFC'
    ## Using assay 'logFC'

Scaling is applied to the datasets separately. A unique color scale can be enforced:

    crossHm(list(SE1=SE, SE2=SE2), g, do.scale = TRUE,
            top_annotation=c("Condition","Time"), uniqueScale = TRUE)

    ## Using assay 'logFC'
    ## Using assay 'logFC'

# Other convenience functions

The package also includes a number of other convenience functions which we briefly describe here (see the functions’ help for more information):

- `log2FC()` adds two assays to an SE object, containing per-sample log2-foldchanges, as well as scaledLFC (variance-scaled log2-foldchanges, but without centering, so that the controls stay around 0) relative to the mean of the (specified) controls.
- The `getDEA()` and `getDEGs()` functions can return a specific DEA or its set of differentially-expressed features, provided that the DEA results tables are each saved in a column of rowData (i.e. the whole table in one column), with a column name starting with `DEA.`.
- The `meltSE()` function can be used to extract a dataframe (suitable for ggplot) containing colData, rowData, and assay data for a given subset of features.

# Session info

    ## R version 4.6.0 (2026-04-24)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 24.04.4 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    ## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
    ##
    ## locale:
    ##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    ##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
    ##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    ##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
    ##  [9] LC_ADDRESS=C               LC_TELEPHONE=C
    ## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
    ##
    ## time zone: Etc/UTC
    ## tzcode source: system (glibc)
    ##
    ## attached base packages:
    ## [1] grid      stats4    stats     graphics  grDevices utils     datasets
    ## [8] methods   base
    ##
    ## other attached packages:
    ##  [1] sechm_1.20.0                ComplexHeatmap_2.28.0
    ##  [3] SummarizedExperiment_1.42.0 Biobase_2.72.0
    ##  [5] GenomicRanges_1.64.0        Seqinfo_1.2.0
    ##  [7] IRanges_2.46.0              S4Vectors_0.50.0
    ##  [9] BiocGenerics_0.58.0         generics_0.1.4
    ## [11] MatrixGenerics_1.24.0       matrixStats_1.5.0
    ## [13] BiocStyle_2.40.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] circlize_0.4.18     shape_1.4.6.1       rjson_0.2.23
    ##  [4] xfun_0.57           bslib_0.10.0        GlobalOptions_0.1.4
    ##  [7] lattice_0.22-9      tools_4.6.0         curl_7.1.0
    ## [10] parallel_4.6.0      ca_0.71.1           cluster_2.1.8.2
    ## [13] Matrix_1.7-5        randomcoloR_1.1.0.1 RColorBrewer_1.1-3
    ## [16] lifecycle_1.0.5     compiler_4.6.0      farver_2.1.2
    ## [19] stringr_1.6.0       codetools_0.2-20    clue_0.3-68
    ## [22] seriation_1.5.8     htmltools_0.5.9     sys_3.4.3
    ## [25] buildtools_1.0.0    sass_0.4.10         yaml_2.3.12
    ## [28] crayon_1.5.3        jquerylib_0.1.4     DelayedArray_0.38.1
    ## [31] cachem_1.1.0        iterators_1.0.14    TSP_1.2.7
    ## [34] abind_1.4-8         foreach_1.5.2       digest_0.6.39
    ## [37] Rtsne_0.17          stringi_1.8.7       maketools_1.3.2
    ## [40] fastmap_1.2.0       colorspace_2.1-2    cli_3.6.6
    ## [43] SparseArray_1.12.0  magrittr_2.0.5      S4Arrays_1.12.0
    ## [46] scales_1.4.0        registry_0.5-1      rmarkdown_2.31
    ## [49] XVector_0.52.0      png_0.1-9           GetoptLong_1.1.1
    ## [52] evaluate_1.0.5      knitr_1.51          V8_8.2.0
    ## [55] doParallel_1.0.17   rlang_1.2.0         Rcpp_1.1.1-1.1
    ## [58] glue_1.8.1          BiocManager_1.30.27 jsonlite_2.0.0
    ## [61] R6_2.6.1