Browse code

expanding the functionality of enhance_table, with the geneset ridge lines (option for coloring them)

Federico Marini authored on 30/01/2023 13:54:35
Showing 1 changed files

... ...
@@ -19,9 +19,14 @@
19 19
 #' available in `res_enrich`. Lists the gene sets to be displayed.
20 20
 #' @param chars_limit Integer, number of characters to be displayed for each
21 21
 #' geneset name.
22
+#' @param plot_style Character value, one of "point" or "ridgeline". Defines the 
23
+#' style of the plot to summarize visually the table.
24
+#' @param ridge_color Character value, one of "gs_id" or "gs_score", controls the
25
+#' fill color of the ridge lines. If selecting "gs_score", the `z_score` column
26
+#' must be present in the enrichment results table - see `get_aggrscores()` to do
27
+#' that.
22 28
 #' @param plot_title Character string, used as title for the plot. If left `NULL`,
23
-#' it defaults to a general description of the plot and of the DE contrast
24
-#'
29
+#' it defaults to a general description of the plot and of the DE contrast.
25 30
 #'
26 31
 #' @return A `ggplot` object
27 32
 #' @export
... ...
@@ -64,6 +69,20 @@
64 69
 #'   anno_df,
65 70
 #'   n_gs = 10
66 71
 #' )
72
+#' 
73
+#' # using the ridge line as a style, also coloring by the Z score
74
+#' res_enrich_withscores <- get_aggrscores(
75
+#'   res_enrich,
76
+#'   res_de,
77
+#'   anno_df
78
+#' )
79
+#' enhance_table(res_enrich_withscores,
80
+#'   res_de,
81
+#'   anno_df,
82
+#'   n_gs = 10, 
83
+#'   plot_style = "ridgeline",
84
+#'   ridge_color = "gs_score"
85
+#' )
67 86
 enhance_table <- function(res_enrich,
68 87
                           res_de,
69 88
                           annotation_obj,
... ...
@@ -71,6 +90,8 @@ enhance_table <- function(res_enrich,
71 90
                           n_gs = 50,
72 91
                           gs_ids = NULL,
73 92
                           chars_limit = 70,
93
+                          plot_style = c("point", "ridgeline"),
94
+                          ridge_color = c("gs_id", "gs_score"),
74 95
                           plot_title = NULL) {
75 96
   if (!is.null(gtl)) {
76 97
     checkup_gtl(gtl)
... ...
@@ -79,7 +100,10 @@ enhance_table <- function(res_enrich,
79 100
     res_enrich <- gtl$res_enrich
80 101
     annotation_obj <- gtl$annotation_obj
81 102
   }
82
-
103
+  
104
+  plot_style <- match.arg(plot_style, c("point", "ridgeline"))
105
+  ridge_color <- match.arg(ridge_color, c("gs_id", "gs_score"))
106
+  
83 107
   n_gs <- min(n_gs, nrow(res_enrich))
84 108
 
85 109
   gs_to_use <- unique(
... ...
@@ -128,27 +152,89 @@ enhance_table <- function(res_enrich,
128 152
   gs_fulllist$gs_desc <- factor(gs_fulllist$gs_desc, levels = rev(levels(gs_fulllist$gs_desc)))
129 153
   max_lfc <- max(abs(range(gs_fulllist$log2FoldChange)))
130 154
 
131
-  p <- ggplot(
132
-    gs_fulllist, aes_string(
133
-      x = "log2FoldChange",
134
-      y = "gs_desc",
135
-      fill = "gs_id",
136
-      text = "gene_name"
137
-    )
138
-  ) +
139
-    scale_x_continuous(limits = c(-max_lfc, max_lfc)) +
140
-    geom_point(alpha = 0.7, shape = 21, size = 2) +
141
-    theme_minimal() +
142
-    geom_vline(aes(xintercept = 0), col = "steelblue", alpha = 0.4) +
143
-    theme(legend.position = "none") +
144
-    scale_y_discrete(
145
-      name = "",
146
-      labels = paste0(
147
-        substr(as.character(unique(gs_fulllist$gs_desc)), 1, chars_limit),
148
-        " | ", unique(gs_fulllist$gs_id)
155
+  # common elements here
156
+  gs_labels <- paste0(
157
+    substr(as.character(unique(gs_fulllist$gs_desc)), 1, chars_limit),
158
+    " | ", unique(gs_fulllist$gs_id)
159
+  )
160
+  
161
+  if (plot_style == "point") {
162
+    p <- ggplot(
163
+      gs_fulllist, aes_string(
164
+        x = "log2FoldChange",
165
+        y = "gs_desc",
166
+        fill = "gs_id",
167
+        text = "gene_name"
149 168
       )
150 169
     ) +
151
-    labs(x = "log2 Fold Change")
170
+      scale_x_continuous(limits = c(-max_lfc, max_lfc)) +
171
+      geom_point(alpha = 0.7, shape = 21, size = 2) +
172
+      theme_minimal() +
173
+      geom_vline(aes(xintercept = 0), col = "steelblue", alpha = 0.4) +
174
+      theme(legend.position = "none") +
175
+      scale_y_discrete(
176
+        name = "",
177
+        labels = gs_labels
178
+      ) +
179
+      labs(x = "log2 Fold Change")
180
+    
181
+  } else if (plot_style == "ridgeline") {
182
+    
183
+    if (ridge_color == "gs_score" & is.null(res_enrich$z_score)) {
184
+      message("Fallback to plotting the ridgelines according to geneset id (Z score required)")
185
+      ridge_color <- "gs_id"
186
+    }  
187
+    
188
+    if (ridge_color == "gs_score") {
189
+      gs_fulllist$gs_zscore <- res_enrich$z_score[match(gs_fulllist$gs_id, res_enrich$gs_id)]
190
+      p <- ggplot(
191
+        gs_fulllist, aes_string(
192
+          x = "log2FoldChange",
193
+          y = "gs_desc",
194
+          fill = "gs_zscore"
195
+        )
196
+      ) +
197
+        scale_x_continuous(limits = c(-max_lfc, max_lfc)) + 
198
+        scale_fill_gradient2(low = "#313695", mid = "#FFFFE5", high = "#A50026") + 
199
+        ggridges::geom_density_ridges(
200
+          aes_string(group = "gs_id"),
201
+          point_color = "#00000066",
202
+          jittered_points = TRUE, scale = .95, rel_min_height = .01,
203
+          point_shape = "|", point_size = 3, size = 0.25,
204
+          position = ggridges::position_points_jitter(height = 0)) +
205
+        theme_minimal() +
206
+        geom_vline(aes(xintercept = 0), col = "steelblue", alpha = 0.4) +
207
+        scale_y_discrete(
208
+          name = "",
209
+          labels = gs_labels
210
+        ) +
211
+        labs(x = "log2 Fold Change")
212
+    }
213
+    else if (ridge_color == "gs_id") {
214
+      p <- ggplot(
215
+        gs_fulllist, aes_string(
216
+          x = "log2FoldChange",
217
+          y = "gs_desc",
218
+          fill = "gs_id"
219
+        )
220
+      ) +
221
+        scale_x_continuous(limits = c(-max_lfc, max_lfc)) + 
222
+        ggridges::geom_density_ridges(
223
+          aes_string(group = "gs_id"),
224
+          point_color = "#00000066",
225
+          jittered_points = TRUE, scale = .95, rel_min_height = .01,
226
+          point_shape = "|", point_size = 3, size = 0.25,
227
+          position = ggridges::position_points_jitter(height = 0)) +
228
+        theme_minimal() +
229
+        geom_vline(aes(xintercept = 0), col = "steelblue", alpha = 0.4) +
230
+        theme(legend.position = "none") +
231
+        scale_y_discrete(
232
+          name = "",
233
+          labels = gs_labels
234
+        ) +
235
+        labs(x = "log2 Fold Change")
236
+    }
237
+  }
152 238
 
153 239
   if (is.null(plot_title)) {
154 240
     p <- p + ggtitle(paste0("Enrichment overview - ", this_contrast))