Skip to contents

Bulk RNA-seq yield many molecular readouts that are hard to interpret by themselves. One way of summarizing this information is by inferring transcription factor (TF) activities from prior knowledge.

In this notebook we showcase how to use decoupleR for transcription factor activity inference with a bulk RNA-seq data-set where the transcription factor FOXA2 was knocked out in pancreatic cancer cell lines.

The data consists of 3 Wild Type (WT) samples and 3 Knock Outs (KO). They are freely available in GEO.

Loading packages

First, we need to load the relevant packages:

## We load the required packages
library(decoupleR)
library(dplyr)
library(tibble)
library(tidyr)
library(ggplot2)
library(pheatmap)
library(ggrepel)

Loading the data-set

Here we used an already processed bulk RNA-seq data-set. We provide the normalized log-transformed counts, the experimental design meta-data and the Differential Expressed Genes (DEGs) obtained using limma.

For this example we use limma but we could have used DeSeq2, edgeR or any other statistical framework. decoupleR requires a gene level statistic to perform enrichment analysis but it is agnostic of how it was generated. However, we do recommend to use statistics that include the direction of change and its significance, for example the t-value obtained for limma(t) or DeSeq2(stat). edgeR does not return such statistic but we can create our own by weighting the obtained logFC by pvalue with this formula: -log10(pvalue) * logFC.

We can open the data like this:

inputs_dir <- system.file("extdata", package = "decoupleR")
data <- readRDS(file.path(inputs_dir, "bk_data.rds"))

From data we can extract the mentioned information. Here we see the normalized log-transformed counts:

# Remove NAs and set row names
counts <- data$counts %>%
          dplyr::mutate_if(~ any(is.na(.x)), 
                           ~ dplyr::if_else(is.na(.x), 0, .x)) %>% 
          tibble::column_to_rownames(var = "gene") %>% 
          as.matrix()

head(counts)
#>          PANC1.WT.Rep1 PANC1.WT.Rep2 PANC1.WT.Rep3 PANC1.FOXA2KO.Rep1
#> NOC2L        10.052588     11.949123     12.057774          12.312291
#> PLEKHN1       7.535115      8.125993      8.714880           8.048196
#> PERM1         6.281242      6.424582      6.589668           6.293285
#> ISG15        10.938252     11.469081     11.425415          11.549986
#> AGRN          6.956335      7.196108      7.522550           7.061549
#> C1orf159      9.546224      9.788721      9.794589           9.850830
#>          PANC1.FOXA2KO.Rep2 PANC1.FOXA2KO.Rep3
#> NOC2L             12.139918          11.494205
#> PLEKHN1            8.290154           8.621239
#> PERM1              6.486136           6.775344
#> ISG15             11.371464          11.178157
#> AGRN               7.485534           7.071555
#> C1orf159           9.988069           9.965357

The design meta-data:

design <- data$design

design
#> # A tibble: 6 × 2
#>   sample             condition    
#>   <chr>              <chr>        
#> 1 PANC1.WT.Rep1      PANC1.WT     
#> 2 PANC1.WT.Rep2      PANC1.WT     
#> 3 PANC1.WT.Rep3      PANC1.WT     
#> 4 PANC1.FOXA2KO.Rep1 PANC1.FOXA2KO
#> 5 PANC1.FOXA2KO.Rep2 PANC1.FOXA2KO
#> 6 PANC1.FOXA2KO.Rep3 PANC1.FOXA2KO

And the results of limma, of which we are interested in extracting the obtained t-value and p-value from the contrast:

# Extract t-values per gene
deg <- data$limma_ttop %>%
       dplyr::select(ID, logFC, t, P.Value) %>% 
       dplyr::filter(!is.na(t)) %>% 
       tibble::column_to_rownames(var = "ID") %>%
       as.matrix()

head(deg)
#>             logFC          t      P.Value
#> RHBDL2  -1.823940 -12.810588 3.030276e-06
#> PLEKHH2 -1.568830 -10.794453 9.932046e-06
#> HEG1    -1.725806  -9.788112 1.939734e-05
#> CLU     -1.786200  -9.761618 1.975813e-05
#> FHL1     2.087082   8.950191 3.552199e-05
#> RBP4    -1.728960  -8.529074 4.904579e-05

CollecTRI network

CollecTRI is a comprehensive resource containing a curated collection of TFs and their transcriptional targets compiled from 12 different resources. This collection provides an increased coverage of transcription factors and a superior performance in identifying perturbed TFs compared to our previous DoRothEA network and other literature based GRNs. Similar to DoRothEA, interactions are weighted by their mode of regulation (activation or inhibition).

For this example we will use the human version (mouse and rat are also available). We can use decoupleR to retrieve it from OmniPath. The argument split_complexes keeps complexes or splits them into subunits, by default we recommend to keep complexes together.

net <- decoupleR::get_collectri(organism = 'human', 
                                split_complexes = FALSE)

net
#> # A tibble: 43,178 × 3
#>    source target   mor
#>    <chr>  <chr>  <dbl>
#>  1 MYC    TERT       1
#>  2 SPI1   BGLAP      1
#>  3 SMAD3  JUN        1
#>  4 SMAD4  JUN        1
#>  5 STAT5A IL2        1
#>  6 STAT5B IL2        1
#>  7 RELA   FAS        1
#>  8 WT1    NR0B1      1
#>  9 NR0B2  CASP1      1
#> 10 SP1    ALDOA      1
#> # ℹ 43,168 more rows

Activity inference with Univariate Linear Model (ULM)

To infer TF enrichment scores we will run the Univariate Linear Model (ulm) method. For each sample in our dataset (mat) and each TF in our network (net), it fits a linear model that predicts the observed gene expression based solely on the TF’s TF-Gene interaction weights. Once fitted, the obtained t-value of the slope is the score. If it is positive, we interpret that the TF is active and if it is negative we interpret that it is inactive.

ulm
ulm

To run decoupleR methods, we need an input matrix (mat), an input prior knowledge network/resource (net), and the name of the columns of net that we want to use.

# Run ulm
sample_acts <- decoupleR::run_ulm(mat = counts, 
                                  net = net, 
                                  .source = 'source', 
                                  .target = 'target',
                                  .mor = 'mor', 
                                  minsize = 5)

sample_acts
#> # A tibble: 3,480 × 5
#>    statistic source condition           score p_value
#>    <chr>     <chr>  <chr>               <dbl>   <dbl>
#>  1 ulm       ABL1   PANC1.FOXA2KO.Rep1 -0.428  0.669 
#>  2 ulm       ABL1   PANC1.FOXA2KO.Rep2 -0.104  0.917 
#>  3 ulm       ABL1   PANC1.FOXA2KO.Rep3  0.335  0.738 
#>  4 ulm       ABL1   PANC1.WT.Rep1       0.142  0.887 
#>  5 ulm       ABL1   PANC1.WT.Rep2      -0.344  0.731 
#>  6 ulm       ABL1   PANC1.WT.Rep3      -0.523  0.601 
#>  7 ulm       AHR    PANC1.FOXA2KO.Rep1  1.58   0.113 
#>  8 ulm       AHR    PANC1.FOXA2KO.Rep2  1.70   0.0885
#>  9 ulm       AHR    PANC1.FOXA2KO.Rep3  1.85   0.0640
#> 10 ulm       AHR    PANC1.WT.Rep1       1.38   0.169 
#> # ℹ 3,470 more rows

Visualization

From the obtained results we will observe the most variable activities across samples in a heat-map:

n_tfs <- 25

# Transform to wide matrix
sample_acts_mat <- sample_acts %>%
                   tidyr::pivot_wider(id_cols = 'condition', 
                                      names_from = 'source',
                                      values_from = 'score') %>%
                   tibble::column_to_rownames('condition') %>%
                   as.matrix()

# Get top tfs with more variable means across clusters
tfs <- sample_acts %>%
       dplyr::group_by(source) %>%
       dplyr::summarise(std = stats::sd(score)) %>%
       dplyr::arrange(-abs(std)) %>%
       head(n_tfs) %>%
       dplyr::pull(source)

sample_acts_mat <- sample_acts_mat[,tfs]

# Scale per sample
sample_acts_mat <- scale(sample_acts_mat)

# Choose color palette
colors <- rev(RColorBrewer::brewer.pal(n = 11, name = "RdBu"))
colors.use <- grDevices::colorRampPalette(colors = colors)(100)

my_breaks <- c(seq(-2, 0, length.out = ceiling(100 / 2) + 1),
               seq(0.05, 2, length.out = floor(100 / 2)))

# Plot
pheatmap::pheatmap(mat = sample_acts_mat,
                   color = colors.use,
                   border_color = "white",
                   breaks = my_breaks,
                   cellwidth = 15,
                   cellheight = 15,
                   treeheight_row = 20,
                   treeheight_col = 20)

We can also infer TF activities from the t-values of the DEGs between KO and WT:

# Run ulm
contrast_acts <- decoupleR::run_ulm(mat = deg[, 't', drop = FALSE], 
                                    net = net, 
                                    .source = 'source', 
                                    .target = 'target',
                                    .mor='mor', 
                                    minsize = 5)

contrast_acts
#> # A tibble: 580 × 5
#>    statistic source condition   score p_value
#>    <chr>     <chr>  <chr>       <dbl>   <dbl>
#>  1 ulm       ABL1   t          1.08    0.280 
#>  2 ulm       AHR    t          1.19    0.234 
#>  3 ulm       AIRE   t         -0.155   0.877 
#>  4 ulm       AP1    t          2.42    0.0154
#>  5 ulm       APEX1  t          0.877   0.380 
#>  6 ulm       AR     t         -0.264   0.792 
#>  7 ulm       ARID1A t         -0.236   0.813 
#>  8 ulm       ARID3A t          1.85    0.0639
#>  9 ulm       ARID3B t          1.24    0.215 
#> 10 ulm       ARID4A t         -0.0674  0.946 
#> # ℹ 570 more rows

Let’s show the changes in activity between KO and WT:


# Filter top TFs in both signs
f_contrast_acts <- contrast_acts %>%
                   dplyr::mutate(rnk = NA)

msk <- f_contrast_acts$score > 0

f_contrast_acts[msk, 'rnk'] <- rank(-f_contrast_acts[msk, 'score'])
f_contrast_acts[!msk, 'rnk'] <- rank(-abs(f_contrast_acts[!msk, 'score']))

tfs <- f_contrast_acts %>%
       dplyr::arrange(rnk) %>%
       head(n_tfs) %>%
       dplyr::pull(source)

f_contrast_acts <- f_contrast_acts %>%
                   filter(source %in% tfs)

colors <- rev(RColorBrewer::brewer.pal(n = 11, name = "RdBu")[c(2, 10)])

p <- ggplot2::ggplot(data = f_contrast_acts, 
                     mapping = ggplot2::aes(x = stats::reorder(source, score), 
                                            y = score)) + 
     ggplot2::geom_bar(mapping = ggplot2::aes(fill = score),
                       color = "black",
                       stat = "identity") +
     ggplot2::scale_fill_gradient2(low = colors[1], 
                                   mid = "whitesmoke", 
                                   high = colors[2], 
                                   midpoint = 0) + 
     ggplot2::theme_minimal() +
     ggplot2::theme(axis.title = element_text(face = "bold", size = 12),
              axis.text.x = ggplot2::element_text(angle = 45, 
                                                  hjust = 1, 
                                                  size = 10, 
                                                  face = "bold"),
              axis.text.y = ggplot2::element_text(size = 10, 
                                                  face = "bold"),
              panel.grid.major = element_blank(), 
              panel.grid.minor = element_blank()) +
     ggplot2::xlab("TFs")

p

The TFs GLI3 and SPDEF are deactivated in KO when compared to WT, while MUC and NFKB1 seem to be activated.

We can further visualize the most differential target genes in each TF along their p-values to interpret the results. For example, let’s see the genes that are belong to SP1:

tf <- 'SP1'

df <- net %>%
      dplyr::filter(source == tf) %>%
      dplyr::arrange(target) %>%
      dplyr::mutate(ID = target, color = "3") %>%
      tibble::column_to_rownames('target')

inter <- sort(dplyr::intersect(rownames(deg), rownames(df)))

df <- df[inter, ]

df[,c('logfc', 't_value', 'p_value')] <- deg[inter, ]

df <- df %>%
      dplyr::mutate(color = dplyr::if_else(mor > 0 & t_value > 0, '1', color)) %>%
      dplyr::mutate(color = dplyr::if_else(mor > 0 & t_value < 0, '2', color)) %>%
      dplyr::mutate(color = dplyr::if_else(mor < 0 & t_value > 0, '2', color)) %>%
      dplyr::mutate(color = dplyr::if_else(mor < 0 & t_value < 0, '1', color))

colors <- rev(RColorBrewer::brewer.pal(n = 11, name = "RdBu")[c(2, 10)])

p <- ggplot2::ggplot(data = df, 
                     mapping = ggplot2::aes(x = logfc, 
                                            y = -log10(p_value), 
                                            color = color,
                                            size = abs(mor))) + 
     ggplot2::geom_point(size = 2.5, 
                         color = "black") + 
     ggplot2::geom_point(size = 1.5) +
     ggplot2::scale_colour_manual(values = c(colors[2], colors[1], "grey")) +
     ggrepel::geom_label_repel(mapping = ggplot2::aes(label = ID,
                                                      size = 1)) + 
     ggplot2::theme_minimal() +
     ggplot2::theme(legend.position = "none") +
     ggplot2::geom_vline(xintercept = 0, linetype = 'dotted') +
     ggplot2::geom_hline(yintercept = 0, linetype = 'dotted') +
     ggplot2::ggtitle(tf)

p

Here blue means that the sign of multiplying the mor and t-value is negative, meaning that these genes are “deactivating” the TF, and red means that the sign is positive, meaning that these genes are “activating” the TF.

Session information

#> ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14)
#>  os       Ubuntu 22.04.5 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       UTC
#>  date     2024-10-19
#>  pandoc   3.4 @ /usr/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  package      * version date (UTC) lib source
#>  backports      1.5.0   2024-05-23 [1] RSPM
#>  BiocManager    1.30.25 2024-08-28 [1] RSPM
#>  BiocParallel   1.38.0  2024-04-30 [1] Bioconduc~
#>  BiocStyle    * 2.32.1  2024-06-16 [1] Bioconduc~
#>  bit            4.5.0   2024-09-20 [1] RSPM
#>  bit64          4.5.2   2024-09-22 [1] RSPM
#>  bookdown       0.41    2024-10-16 [1] RSPM
#>  bslib          0.8.0   2024-07-29 [1] RSPM
#>  cachem         1.1.0   2024-05-16 [1] RSPM
#>  cellranger     1.1.0   2016-07-27 [1] RSPM
#>  checkmate      2.3.2   2024-07-29 [1] RSPM
#>  cli            3.6.3   2024-06-21 [1] RSPM
#>  codetools      0.2-20  2024-03-31 [2] CRAN (R 4.4.1)
#>  colorspace     2.1-1   2024-07-26 [1] RSPM
#>  crayon         1.5.3   2024-06-20 [1] RSPM
#>  curl           5.2.3   2024-09-20 [1] RSPM
#>  decoupleR    * 2.9.7   2024-10-19 [1] Bioconductor
#>  desc           1.4.3   2023-12-10 [1] RSPM
#>  digest         0.6.37  2024-08-19 [1] RSPM
#>  dplyr        * 1.1.4   2023-11-17 [1] RSPM
#>  evaluate       1.0.1   2024-10-10 [1] RSPM
#>  fansi          1.0.6   2023-12-08 [1] RSPM
#>  farver         2.1.2   2024-05-13 [1] RSPM
#>  fastmap        1.2.0   2024-05-15 [1] RSPM
#>  fs             1.6.4   2024-04-25 [1] RSPM
#>  generics       0.1.3   2022-07-05 [1] RSPM
#>  ggplot2      * 3.5.1   2024-04-23 [1] RSPM
#>  ggrepel      * 0.9.6   2024-09-07 [1] RSPM
#>  glue           1.8.0   2024-09-30 [1] RSPM
#>  gtable         0.3.5   2024-04-22 [1] RSPM
#>  highr          0.11    2024-05-26 [1] RSPM
#>  hms            1.1.3   2023-03-21 [1] RSPM
#>  htmltools      0.5.8.1 2024-04-04 [1] RSPM
#>  htmlwidgets    1.6.4   2023-12-06 [1] RSPM
#>  httr           1.4.7   2023-08-15 [1] RSPM
#>  igraph         2.0.3   2024-03-13 [1] RSPM
#>  jquerylib      0.1.4   2021-04-26 [1] RSPM
#>  jsonlite       1.8.9   2024-09-20 [1] RSPM
#>  knitr          1.48    2024-07-07 [1] RSPM
#>  labeling       0.4.3   2023-08-29 [1] RSPM
#>  later          1.3.2   2023-12-06 [1] RSPM
#>  lattice        0.22-6  2024-03-20 [2] CRAN (R 4.4.1)
#>  lifecycle      1.0.4   2023-11-07 [1] RSPM
#>  logger         0.3.0   2024-03-05 [1] RSPM
#>  lubridate      1.9.3   2023-09-27 [1] RSPM
#>  magrittr       2.0.3   2022-03-30 [1] RSPM
#>  Matrix         1.7-1   2024-10-18 [1] RSPM (R 4.4.0)
#>  munsell        0.5.1   2024-04-01 [1] RSPM
#>  OmnipathR      3.12.4  2024-10-02 [1] Bioconduc~
#>  parallelly     1.38.0  2024-07-27 [1] RSPM
#>  pheatmap     * 1.0.12  2019-01-04 [1] RSPM
#>  pillar         1.9.0   2023-03-22 [1] RSPM
#>  pkgconfig      2.0.3   2019-09-22 [1] RSPM
#>  pkgdown        2.1.1   2024-09-17 [1] RSPM
#>  prettyunits    1.2.0   2023-09-24 [1] RSPM
#>  progress       1.2.3   2023-12-06 [1] RSPM
#>  purrr          1.0.2   2023-08-10 [1] RSPM
#>  R6             2.5.1   2021-08-19 [1] RSPM
#>  ragg           1.3.3   2024-09-11 [1] RSPM
#>  rappdirs       0.3.3   2021-01-31 [1] RSPM
#>  RColorBrewer   1.1-3   2022-04-03 [1] RSPM
#>  Rcpp           1.0.13  2024-07-17 [1] RSPM
#>  readr          2.1.5   2024-01-10 [1] RSPM
#>  readxl         1.4.3   2023-07-06 [1] RSPM
#>  rlang          1.1.4   2024-06-04 [1] RSPM
#>  rmarkdown      2.28    2024-08-17 [1] RSPM
#>  rvest          1.0.4   2024-02-12 [1] RSPM
#>  sass           0.4.9   2024-03-15 [1] RSPM
#>  scales         1.3.0   2023-11-28 [1] RSPM
#>  selectr        0.4-2   2019-11-20 [1] RSPM
#>  sessioninfo    1.2.2   2021-12-06 [1] RSPM
#>  stringi        1.8.4   2024-05-06 [1] RSPM
#>  stringr        1.5.1   2023-11-14 [1] RSPM
#>  systemfonts    1.1.0   2024-05-15 [1] RSPM
#>  textshaping    0.4.0   2024-05-24 [1] RSPM
#>  tibble       * 3.2.1   2023-03-20 [1] RSPM
#>  tidyr        * 1.3.1   2024-01-24 [1] RSPM
#>  tidyselect     1.2.1   2024-03-11 [1] RSPM
#>  timechange     0.3.0   2024-01-18 [1] RSPM
#>  tzdb           0.4.0   2023-05-12 [1] RSPM
#>  utf8           1.2.4   2023-10-22 [1] RSPM
#>  vctrs          0.6.5   2023-12-01 [1] RSPM
#>  vroom          1.6.5   2023-12-05 [1] RSPM
#>  withr          3.0.1   2024-07-31 [1] RSPM
#>  xfun           0.48    2024-10-03 [1] RSPM
#>  xml2           1.3.6   2023-12-04 [1] RSPM
#>  yaml           2.3.10  2024-07-26 [1] RSPM
#> 
#>  [1] /usr/local/lib/R/site-library
#>  [2] /usr/local/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────