File: plot_bootstrapped_contribution.R

package info (click to toggle)
r-bioc-mutationalpatterns 3.16.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,360 kB
  • sloc: sh: 8; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 4,965 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#' Plot the bootstrapped signature contributions
#'
#' Plot the signature contributions retrieved with 'fit_to_signatures_bootstrapped'.
#' The function can plot both the absolute or the relative signature contribution.
#' The graph can be plotted as either a jitter plot or as a barplot.
#'
#' @param contri_boots  matrix showing  signature contributions across bootstrap iterations.
#' @param mode Either "absolute" for absolute number of mutations, or
#' "relative" for relative contribution, default = "absolute"
#' @param plot_type Either "jitter" for a jitter plot, "barplot" for a barplot, or "dotplot" for a dotplot
#'
#' @return A ggplot2 graph
#' @export
#' @importFrom magrittr %>%
#' @import ggplot2
#' @examples
#' ## Get the bootstrapped signature contributions
#' ## See 'count_indel_contexts()' for more info on how to do this.
#' contri_boots <- readRDS(system.file("states/bootstrapped_snv_refit.rds",
#'   package = "MutationalPatterns"
#' ))
#'
#' ## Plot bootstrapped contribution
#' plot_bootstrapped_contribution(contri_boots)
#'
#' ## Plot bootstrapped contribution with relative contributions
#' plot_bootstrapped_contribution(contri_boots, mode = "relative")
#'
#' ## Plot bootstrapped contribution with a barplot
#' plot_bootstrapped_contribution(contri_boots, plot_type = "barplot")
#' 
#' ## Plot bootstrapped contribution with a dotplot
#' plot_bootstrapped_contribution(contri_boots, plot_type = "dotplot", mode = "absolute")
plot_bootstrapped_contribution <- function(contri_boots,
                                           mode = c("absolute", "relative"),
                                           plot_type = c("jitter", "barplot", "dotplot")) {
  # Match arguments
  mode <- match.arg(mode)
  plot_type <- match.arg(plot_type)

  # These variables use non standard evaluation.
  # To avoid R CMD check complaints we initialize them to NULL.
  sig <- contri <- lower <- upper <- percentage <- NULL

  # Change variables based on relative or absolute mode.
  if (mode == "relative") {
    contri_boots <- contri_boots / rowSums(contri_boots)
    ylab_text <- "Relative mutation contribution"
    jitter_height <- 0.02
  } else {
    ylab_text <- "Nr. contributed mutations"
    jitter_height <- 0.2
  }

  # Convert contri_boots into a long format.
  contri_tb <- contri_boots %>%
    as.data.frame() %>%
    tibble::rownames_to_column("exp") %>%
    tidyr::gather(key = "sig", value = "contri", -exp) %>%
    dplyr::mutate(
      sample = gsub("_[^_]+$", "", exp),
      sample = factor(sample, levels = unique(sample)),
      sig = factor(sig, levels = unique(sig))
    )
  
  # Determine number of signatures
  nr_sigs <- length(unique(contri_tb$sig))

  if (plot_type == "jitter") {
    # Create basis for jitter figure
    fig <- ggplot(contri_tb, aes(x = sig, y = contri, color = sig)) +
      geom_jitter(stat = "identity", height = jitter_height, size = 0.3) +
      scale_color_discrete(guide = "none") +
      facet_grid(sample ~ .) +
      labs(y = ylab_text)
  } else if (plot_type == "barplot") {
    # Calculate values for barplot
    contri_tb2 <- contri_tb %>%
      dplyr::group_by(sample, sig) %>%
      dplyr::summarise(
        mean = mean(contri),
        lower = quantile(contri, 0.025),
        upper = quantile(contri, 0.975)
      ) %>%
      dplyr::ungroup()
    # Create basis for barplot figure
    fig <- ggplot(contri_tb2, aes(x = sig, y = mean, fill = sig)) +
      geom_bar(stat = "identity") +
      geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
      scale_fill_discrete(guide = "none") +
      facet_grid(sample ~ .) +
      labs(y = ylab_text)
  } else if (plot_type == "dotplot") {
    contri_tb3 <- contri_tb %>%
      dplyr::group_by(sample, sig) %>%
      dplyr::summarise(
        mean = mean(contri[contri != 0]),
        percentage = sum(contri != 0)/dplyr::n()
      ) %>%
      dplyr::ungroup() %>%
      dplyr::filter(!is.na(mean)) %>% 
      dplyr::mutate(sample = factor(sample, levels = rev(levels(sample))))
    
    max_dot_size <- dplyr::case_when(
      nr_sigs >= 40 ~ 5,
      nr_sigs >= 30 ~ 7,
      nr_sigs >= 20 ~ 8,
      nr_sigs >= 10 ~ 10,
      TRUE ~ 15
    )
    
    fig <- ggplot(contri_tb3, aes(x = sig, y = sample)) +
      geom_point(aes(color = percentage, size = mean)) +
      scale_color_distiller(palette = "RdYlBu", limits = c(0, 1)) +
      scale_size_continuous(range = c(1,max_dot_size)) +
      labs(size = "mean contribution", 
           colour = "percentage contribution",
           y = "Sample")
  }
  
  ## Add extra labels to figure for all types
  fig <- fig +
    labs(x = "Signature") +
    theme_classic() +
    theme(
      axis.text.x = element_text(angle = 90, size = 10, hjust = 1, vjust = 0.5),
      text = element_text(size = 12),
      strip.text.y = element_text(size = 8)
    )
  if (plot_type == 'dotplot') {
    fig <- fig + 
      theme(panel.grid.major = element_line(colour = "gray92"))
  }
  return(fig)
}