# Data visualization with epiregulon.extra

# Introduction

epiregulon.extra is a companion package to epiregulon and provides functions to visualize transcription factor activity and network. It also provides statistical tests to identify transcription factors with differential activity and network topology. This tutorial continues from the reprogram-seq example included in epiregulon. We will use the gene regulatory networks constructed by epiregulon and continue with visualization and network analysis.

For full documentation of the `epiregulon` package, please refer to the epiregulon book.

# Installation

    if (!require("BiocManager", quietly=TRUE))
        install.packages("BiocManager")

    BiocManager::install("epiregulon.extra")

# Data preparation

To continue with the visualization functions, we will first need the gene expression matrix. We can download the data from scMultiome.

    library(scMultiome)
    mae <- scMultiome::reprogramSeq()

    GeneExpressionMatrix <- mae[["GeneExpressionMatrix"]]
    rownames(GeneExpressionMatrix) <- rowData(GeneExpressionMatrix)$name

    reducedDim(GeneExpressionMatrix, "UMAP_Combined") <-
        reducedDim(mae[["TileMatrix500"]], "UMAP_Combined")

    GeneExpressionMatrix$hash_assignment <- factor(as.character(GeneExpressionMatrix$hash_assignment),
                                                   levels=c("HTO10_GATA6_UTR",
                                                            "HTO2_GATA6_v2",
                                                            "HTO8_NKX2.1_UTR",
                                                            "HTO3_NKX2.1_v2",
                                                            "HTO1_FOXA2_v2",
                                                            "HTO4_mFOXA1_v2",
                                                            "HTO6_hFOXA1_UTR",
                                                            "HTO5_NeonG_v2"))

# Calculate TF activity

Next we load the regulon object previously calculated by epiregulon. Here we just use the pruned version of regulon object in which only relevant columns are kept. Using epiregulon, we can calculate activity of transcription factors included in the regulon object.

    library(epiregulon)
    library(epiregulon.extra)

    ##

    data(regulon)

    score.combine <- calculateActivity(
        expMatrix=GeneExpressionMatrix,
        regulon=regulon,
        mode="weight",
        exp_assay="normalizedCounts",
        normalize=FALSE
    )

    ## calculating TF activity from regulon using

    ## Warning in calculateActivity(expMatrix = GeneExpressionMatrix, regulon = regulon, : The weight column contains multiple subcolumns but no cluster information was provided.
    ##           Using first column to compute activity...

    ## aggregating regulons...

    ## creating weight matrix...

    ## calculating activity scores...

    ## normalize by the number of targets...

# Perform differential activity

We perform a differential analysis of transcription factor activity across groups of cells. This function is adapted from `findMarkers` of scran. It performs pairwise t-tests across clusters and combines the p-values according to `pval.type`.

    library(epiregulon.extra)
    markers <- findDifferentialActivity(
        activity_matrix=score.combine,
        clusters=GeneExpressionMatrix$hash_assignment,
        pval.type="some",
        direction="up"
    )

We can specify the top transcription factors of interest

    markers.sig <- getSigGenes(markers, topgenes=5)

    ## Using a cutoff of 0.022 for class HTO10_GATA6_UTR for direction equal to any

    ## Using a cutoff of 0.019 for class HTO2_GATA6_v2 for direction equal to any

    ## Using a cutoff of 0.005 for class HTO8_NKX2.1_UTR for direction equal to any

    ## Using a cutoff of 0.0016 for class HTO3_NKX2.1_v2 for direction equal to any

    ## Using a cutoff of 0.00066 for class HTO1_FOXA2_v2 for direction equal to any

    ## Using a cutoff of 9e-04 for class HTO4_mFOXA1_v2 for direction equal to any

    ## Using a cutoff of 6e-04 for class HTO6_hFOXA1_UTR for direction equal to any

    ## Using a cutoff of 0.00025 for class HTO5_NeonG_v2 for direction equal to any

# Visualize the results

We visualize the known differential transcription factors by bubble plot

    plotBubble(
        activity_matrix=score.combine,
        tf=c("NKX2-1", "GATA6", "FOXA1", "FOXA2"),
        clusters=GeneExpressionMatrix$hash_assignment
    )

Then visualize the most differential transcription factors by clusters

    plotBubble(
        activity_matrix=score.combine,
        tf=markers.sig$tf,
        clusters=GeneExpressionMatrix$hash_assignment
    )

Visualize the known differential transcription factors by violin plot.

    plotActivityViolin(
        activity_matrix=score.combine,
        tf=c("NKX2-1", "GATA6", "FOXA1", "FOXA2", "AR"),
        clusters=GeneExpressionMatrix$hash_assignment,
        boxplot = TRUE
    )

Visualize the known differential transcription factors by UMAP

    options(ggrastr.default.dpi=100)

    ActivityPlot <- plotActivityDim(
          sce=GeneExpressionMatrix,
          activity_matrix=score.combine,
          tf=c("NKX2-1", "GATA6", "FOXA1", "FOXA2", "AR"),
          dimtype="UMAP_Combined",
          label="Clusters",
          point_size=0.3,
          ncol=3,
          rasterise=TRUE
    )

    ActivityPlot

In contrast, the gene expression of the transcription factors is very sparse

    options(ggrastr.default.dpi=100)

    ActivityPlot <- plotActivityDim(
        sce=GeneExpressionMatrix,
        activity_matrix=counts(GeneExpressionMatrix),
        tf=c("NKX2-1", "GATA6", "FOXA1", "FOXA2", "AR"),
        dimtype="UMAP_Combined",
        label="Clusters",
        point_size=0.3,
        ncol=3,
        limit=c(0, 2),
        colors=c("grey", "blue"),
        legend.label="GEX",
        rasterise=TRUE
    )
    ActivityPlot

Visualize the activity of the selected transcription factors by heat map. Due to the maximum size limit for this vignette, the output is not shown here.

    plotHeatmapRegulon(
        sce=GeneExpressionMatrix,
        tfs=c("GATA6", "NKX2-1"),
        regulon=regulon,
        regulon_cutoff=0,
        downsample=1000,
        cell_attributes="hash_assignment",
        col_gap="hash_assignment",
        exprs_values="normalizedCounts",
        name="regulon heatmap",
        column_title_rot=45,
        raster_quality=4
    )

    plotHeatmapActivity(
        activity=score.combine,
        sce=GeneExpressionMatrix,
        tfs=c("GATA6", "NKX2-1", "FOXA1", "FOXA2"),
        downsample=5000,
        cell_attributes="hash_assignment",
        col_gap="hash_assignment",
        name="Activity",
        column_title_rot=45,
        raster_quality=3
    )

# Geneset enrichment

Sometimes we are interested to know what pathways are enriched in the regulon of a particular TF. We can perform geneset enrichment using the enricher function from clusterProfiler.

We retrieve the genesets from msigdb

    H <- msigdbr::msigdbr(species = "Homo sapiens", collection = "H")
    genesets <- data.frame(geneset=H$gs_name,
                           genes=H$gene_symbol)

We perform gene set enrichment on the regulons

    enrichresults <- regulonEnrich(
        TF=c("GATA6", "NKX2-1"),
        regulon=regulon,
        weight="weight",
        weight_cutoff=0.1,
        genesets=genesets
    )

    ## GATA6

    ## NKX2-1

    enrichPlot(results=enrichresults)

# Network analysis

We can visualize the genesets as a network

    plotGseaNetwork(
        tf=names(enrichresults),
        enrichresults=enrichresults,
        p.adj_cutoff=0.1,
        ntop_pathways=10
    )

# Differential networks

We are interested in understanding the differential networks between two conditions and determining which transcription factors account for the differences in the topology of networks. The pruned regulons with cluster-specific test statistics computed by `pruneRegulon` can be used to generate cluster-specific networks based on user-defined cutoffs and to visualize differential networks for transcription factors of interest. In this dataset, the GATA6 gene was only expressed in cluster 1 (C1) and NKX2-1 was only expressed in cluster 3 (C3). If we visualize the target genes of GATA6, we can see that C1 has many more target genes of GATA6 compared to C5, a cluster that does not express GATA6. Similarly, NKX2-1 target genes are confined to C3 which is the only cluster that exogenously expresses NKX2-1.

    plotDiffNetwork(regulon,
        cutoff=0.1,
        tf=c("GATA6"),
        weight="weight",
        clusters=c("C1", "C5"),
        layout="stress"
    )

    ## Building graph using weight as edge weights

    plotDiffNetwork(regulon,
        cutoff=0.1,
        tf=c("NKX2-1"),
        weight="weight",
        clusters=c("C3", "C5"),
        layout="stress"
    )

    ## Building graph using weight as edge weights

We can also visualize how transcription factors relate to other transcription factors in C1 cluster.

    selected <- which(regulon$weight[, "C1"] > 0 &
        regulon$tf %in% c("GATA6", "FOXA1", "AR"))

    C1_network <- buildGraph(regulon[selected, ],
        weights="weight",
        cluster="C1"
    )

    ## Building graph using weight as edge weights

    plotEpiregulonNetwork(C1_network,
        layout="sugiyama",
        tfs_to_highlight=c("GATA6", "FOXA1", "AR")) +
        ggplot2::ggtitle("C1")

To systematically examine the differential network topology between two clusters, we perform an edge subtraction between two graphs, using weights computed by `pruneRegulon`. We then calculate the degree centrality of the weighted differential graphs and if desired, normalize the differential centrality against the total number of edges. The default normalization function is `sqrt` as it preserves both the difference in the number of edges (but scaled by sqrt) and the differences in the weights. If the user only wants to examine the differences in the averaged weights, the `FUN` argument can be changed to `identity`. Finally, we rank the transcription factors by (normalized) differential centrality. For demonstration purpose, the regulon list is truncated but the full list would contain all the transcription factors.

    # rank by differential centrality
    C1_network <- buildGraph(regulon, weights="weight", cluster="C1")

    ## Building graph using weight as edge weights

    C5_network <- buildGraph(regulon, weights="weight", cluster="C5")

    ## Building graph using weight as edge weights

    diff_graph <- buildDiffGraph(C1_network, C5_network, abs_diff=FALSE)
    diff_graph <- addCentrality(diff_graph)
    diff_graph <- normalizeCentrality(diff_graph)
    rank_table <- rankTfs(diff_graph)

    library(ggplot2)
    ggplot(rank_table, aes(x=rank, y=centrality)) +
        geom_point() +
        ggrepel::geom_text_repel(data=rank_table, aes(label=tf)) +
        theme_classic()

# Session Info

    sessionInfo()

    ## R version 4.6.1 (2026-06-24)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 26.04 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.32.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] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] ggplot2_4.0.3               epiregulon.extra_1.9.1
    ##  [3] epiregulon_2.3.3            scMultiome_1.13.0
    ##  [5] SingleCellExperiment_1.35.2 MultiAssayExperiment_1.39.0
    ##  [7] SummarizedExperiment_1.43.0 Biobase_2.73.1
    ##  [9] GenomicRanges_1.65.1        Seqinfo_1.3.0
    ## [11] IRanges_2.47.2              S4Vectors_0.51.5
    ## [13] MatrixGenerics_1.25.0       matrixStats_1.5.0
    ## [15] ExperimentHub_3.3.1         AnnotationHub_4.3.2
    ## [17] BiocFileCache_3.3.0         dbplyr_2.6.0
    ## [19] BiocGenerics_0.59.10        generics_0.1.4
    ## [21] BiocStyle_2.41.0
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] splines_4.6.1           ggplotify_0.1.3         filelock_1.0.3
    ##   [4] tibble_3.3.1            polyclip_1.10-7         enrichit_0.2.0
    ##   [7] lifecycle_1.0.5         httr2_1.3.0             doParallel_1.0.17
    ##  [10] processx_3.9.0          lattice_0.22-9          MASS_7.3-65
    ##  [13] backports_1.5.1         magrittr_2.0.5          sass_0.4.10
    ##  [16] rmarkdown_2.31          jquerylib_0.1.4         yaml_2.3.12
    ##  [19] metapod_1.21.0          otel_0.2.0              ggtangle_0.1.2
    ##  [22] DBI_1.3.0               buildtools_1.0.0        RColorBrewer_1.1-3
    ##  [25] abind_1.4-8             purrr_1.2.2             msigdbr_26.1.0
    ##  [28] ggraph_2.2.2            yulab.utils_0.2.4       tweenr_2.0.3
    ##  [31] rappdirs_0.3.4          aisdk_1.4.12            gdtools_0.5.1
    ##  [34] circlize_0.4.18         enrichplot_1.33.0       ggrepel_0.9.8
    ##  [37] irlba_2.3.7             tidytree_0.4.8          maketools_1.3.2
    ##  [40] codetools_0.2-20        DelayedArray_0.39.3     scuttle_1.23.1
    ##  [43] DOSE_4.7.2              ggforce_0.5.0           tidyselect_1.2.1
    ##  [46] shape_1.4.6.1           aplot_0.3.1             farver_2.1.2
    ##  [49] ScaledMatrix_1.21.0     viridis_0.6.5           jsonlite_2.0.0
    ##  [52] GetoptLong_1.1.1        BiocNeighbors_2.7.2     tidygraph_1.3.1
    ##  [55] scater_1.41.2           iterators_1.0.14        systemfonts_1.3.2
    ##  [58] foreach_1.5.2           tools_4.6.1             ggnewscale_0.5.2
    ##  [61] treeio_1.37.0           Rcpp_1.1.2              glue_1.8.1
    ##  [64] gridExtra_2.3.1         SparseArray_1.13.2      xfun_0.60
    ##  [67] qvalue_2.45.0           dplyr_1.2.1             HDF5Array_1.41.0
    ##  [70] withr_3.0.3             BiocManager_1.30.27     fastmap_1.2.0
    ##  [73] rhdf5filters_1.25.0     rsvd_1.0.5              callr_3.8.0
    ##  [76] digest_0.6.39           R6_2.6.1                gridGraphics_0.5-1
    ##  [79] colorspace_2.1-3        Cairo_1.7-0             GO.db_3.23.1
    ##  [82] RSQLite_3.53.3          h5mread_1.5.0           tidyr_1.3.2
    ##  [85] fontLiberation_0.1.0    graphlayouts_1.2.4      httr_1.4.8
    ##  [88] htmlwidgets_1.6.4       S4Arrays_1.13.0         scatterpie_0.2.6
    ##  [91] pkgconfig_2.0.3         gtable_0.3.6            blob_1.3.0
    ##  [94] ComplexHeatmap_2.29.0   S7_0.2.2                XVector_0.53.0
    ##  [97] sys_3.4.3               clusterProfiler_4.21.0  htmltools_0.5.9
    ## [100] fontBitstreamVera_0.1.1 clue_0.3-68             scales_1.4.0
    ## [103] png_0.1-9               ggfun_0.2.1             knitr_1.51
    ## [106] reshape2_1.4.5          rjson_0.2.23            checkmate_2.3.4
    ## [109] nlme_3.1-169            curl_7.1.0              cachem_1.1.0
    ## [112] rhdf5_2.57.1            GlobalOptions_0.1.4     stringr_1.6.0
    ## [115] BiocVersion_3.24.0      vipor_0.4.7             parallel_4.6.1
    ## [118] AnnotationDbi_1.75.0    ggrastr_1.0.2           pillar_1.11.1
    ## [121] grid_4.6.1              vctrs_0.7.3             BiocSingular_1.29.0
    ## [124] tidydr_0.0.6            beachmat_2.29.0         cluster_2.1.8.2
    ## [127] beeswarm_0.4.0          evaluate_1.0.5          cli_3.6.6
    ## [130] compiler_4.6.1          rlang_1.3.0             crayon_1.5.3
    ## [133] labeling_0.4.3          ps_1.9.3                ggbeeswarm_0.7.3
    ## [136] plyr_1.8.9              fs_2.1.0                ggiraph_0.9.6
    ## [139] stringi_1.8.7           BiocParallel_1.47.0     viridisLite_0.4.3
    ## [142] babelgene_22.9          assertthat_0.2.1        Biostrings_2.81.5
    ## [145] lazyeval_0.2.3          scrapper_1.7.3          GOSemSim_2.39.2
    ## [148] fontquiver_0.2.1        Matrix_1.7-5            patchwork_1.3.2
    ## [151] bit64_4.8.2             Rhdf5lib_2.1.0          KEGGREST_1.53.5
    ## [154] igraph_2.3.3            memoise_2.0.1           bslib_0.11.0
    ## [157] ggtree_4.3.0            bit_4.6.0               ape_5.8-1
    ## [160] gson_0.2.0