26  TF Activity inference analysis

This section is highly similar to Pathway Activity inference analysis as it makes use of the same source package and visualizations.

Transcriptional Factor (TF) Activity inference analysis can also be carried out in a SC dataset. Same as with the previous chapter, we will use decoupleR (P.Badia-i-Mompel et al. 2022). For this, dorothea is used. This allows for the computation of activity scores on a cell basis depicting how much (or little) each cell is enriched in a TF and its dowstream targets (regulon).

In order to visualize the enrichment of our cells in the pathways, the results need to be computed:

# Define your sample and assay.
sample <- your_seurat_object
assay <- "your_normalized_data_assay"

# Retrieve prior knowledge network.
network <- decoupleR::get_dorothea(organism = "human",
                                   levels = c("A", "B", "C"))

# Run weighted means algorithm.
activities <- decoupleR::run_wmean(mat = as.matrix(sample@assays[[assay]]@data),
                                   network = network,
                                   .source = "source",
                                   .targe = "target",
                                   .mor = "mor",
                                   times = 100,
                                   minsize = 5)

With this, we can proceed to plot our results with SCpubr::do_TFActivityPlot().

26.1 Heatmap of averaged scores

The most informative and, perhaps, straightforward approach is to visualize the resulting scores averaged by the groups we have defined, as a heatmap. This is the default output of SCpubr::do_TFActivityPlot().

# General heatmap.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities)
p <- out$heatmaps$average_scores
p

Retrieve the heatmap output.

26.2 Set the scale limits.

We can set the limits of the color scale by using min.cutoff and/or max.cutoff:

# Set the scale limits.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 min.cutoff = -2,
                                 max.cutoff = 2)
p <- out$heatmaps$average_scores
p

Set scale limits

However, be mindful that if enforce_symmetry = TRUE, this will override these settings, to achieve a symmetrical color scale:

# Effect of enforce symmetry on the scale limits.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 min.cutoff = -2,
                                 max.cutoff = 1,
                                 enforce_symmetry = TRUE)
p <- out$heatmaps$average_scores
p

Effect of enforce_symmetry in min.cutoff and max.cutoff

This effect will also be seen if only min.cutoff or max.cutoff is set and the other end of the color scale has a higher absolute value.

Take-home message

Be mindful when using min.cutoff and max.cutoff, as this behavior might be overriden by enforce_symmetry = TRUE. Consider setting it to FALSE.

26.3 Select the number of top TFs to plot

We can select the number of TFs to include in the analysis by using n_tfs variable.

# Increase number of TFs included in the analysis.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 n_tfs = 40)
p <- out$heatmaps$average_scores
p

Increase the number of reported TFs.

26.4 Feature plots of the scores

Perhaps we are interested into visualizing the scores as a Feature plot. This way we can observe trends of enrichment in the activities. This can be achieved by providing plot_FeaturePlots = TRUE.

# Retrieve feature plots.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 plot_FeaturePlots = TRUE)
p1 <- SCpubr::do_DimPlot(sample)
p2 <- out$feature_plots$GLI2
p <- p1 | p2
p

Retrieve the feature plot output.

26.5 Geyser plots of the scores

In the same fashion as with the feature plots, we can also visualize the scores as Geyser plots, to get a sense of the distribution of the scores alongside the groups. This can be achieved by providing plot_GeyserPlots = TRUE.

# Retrieve Geyser plots.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 plot_GeyserPlots = TRUE)
p1 <- SCpubr::do_DimPlot(sample)
p2 <- out$geyser_plots$GLI2
p <- p1 | p2
p

Retrieve the geyser plot output.

26.6 Use non-symmetrical color scales

If one wants to just plot a continuous color scale for the Feature plots and the Geyser plots, this can be achieved by using enforce_symmetry = FALSE.

# Use non-symmetrical color scale.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 plot_GeyserPlots = TRUE,
                                 plot_FeaturePlots = TRUE,
                                 enforce_symmetry = FALSE)
p1 <- out$feature_plots$GLI2
p2 <- out$geyser_plots$GLI2

out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 plot_GeyserPlots = TRUE,
                                 plot_FeaturePlots = TRUE,
                                 enforce_symmetry = TRUE)
p3 <- out$feature_plots$GLI2
p4 <- out$geyser_plots$GLI2

p <- (p1 | p2) / (p3 | p4)
p

Apply a non-symmetrical color scale.

26.7 Order geysers by the mean

We can also decide not to order the Geyser plots by the mean of the values. We can do that by providing geyser_order_by_mean = FALSE.

# Not order Geyser plot by mean values.
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 plot_GeyserPlots = TRUE,
                                 enforce_symmetry = TRUE,
                                 geyser_order_by_mean = FALSE)
p1 <- out$geyser_plots$GLI2

out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 plot_GeyserPlots = TRUE,
                                 enforce_symmetry = TRUE,
                                 geyser_order_by_mean = TRUE)
p2 <- out$geyser_plots$GLI2

p <- p1 | p2
p

Order by mean values.

26.8 Split the heatmap into different groups

We can also further divide the heatmap into groups. This can be achieved using split.by parameter.

# Split the heatmap by another variable.
sample$split.me <- ifelse(sample$seurat_clusters %in% c("0", "3", "7"), "Group A","Group B")

out <- SCpubr::do_TFActivityPlot(sample = sample,
                                 activities = activities,
                                 split.by = "split.me")
p <- out$heatmaps$average_scores
p

Split the heatmap by another variable.

26.9 Control the color scale

Again, we can control the color scale using min.cutoff and max.cutoff. This will apply to all plot types:

# Control the color scale
out <- SCpubr::do_TFActivityPlot(sample = sample,
                                      activities = activities,
                                      split.by = "split.me",
                                      min.cutoff = 0.1,
                                      max.cutoff = 0.7,
                                      plot_FeaturePlots = TRUE,
                                      plot_GeyserPlots = TRUE)
p <- out$heatmaps$average_scores
p

Control the color scale - heatmap.

```{r} #| fig-width: 14 #| fig-height: 8 #| fig-cap: “Control the color scale - FeaturePlot and GeyserPlot”

p1 <- out\(feature_plots\)PI3K p2 <- out\(geyser_plots\)PI3K p <- p1 | p2 p