|
| 1 | +# ---- Code to reproduce Figure 1a ---- # |
| 2 | + |
| 3 | +# Unload all previously loaded packages + remove previous environment |
| 4 | +rm(list = ls(all = TRUE)) |
| 5 | +pacman::p_unload() |
| 6 | + |
| 7 | +# Set working directory |
| 8 | +GaitiLabUtils::set_wd() |
| 9 | + |
| 10 | +# ---- Setup script ---- # |
| 11 | + |
| 12 | +# Load required packages |
| 13 | +pacman::p_load(dplyr, ComplexHeatmap, RColorBrewer, tidyr, circlize) |
| 14 | + |
| 15 | +# Helper function for drawing Heatmaps |
| 16 | +cell_border_fun <- function(j, i, x, y, width, height, fill) { |
| 17 | + grid.rect( |
| 18 | + x = x, |
| 19 | + y = y, |
| 20 | + width = width, |
| 21 | + height = height, |
| 22 | + gp = gpar(col = "black", lwd = 1, fill = NA) |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +# Required inputs |
| 27 | +params <- list( |
| 28 | + plot_dir = "output/figures", |
| 29 | + input_table_path = "misc/Table S1.xlsx" # can be downloaded online, see publication |
| 30 | +) |
| 31 | + |
| 32 | +GaitiLabUtils::create_dir(params$plot_dir) |
| 33 | + |
| 34 | +# ---- Load data & Data wrangling ---- # |
| 35 | +mutations_oi <- c("IDH", "CDKN2A", "TP53", "ATRX", "BRAF") |
| 36 | +regions_oi <- c("PT", "TE", "TC") |
| 37 | + |
| 38 | +df <- readxl::read_excel( |
| 39 | + params$input_table_path, |
| 40 | + sheet = "Patient and sample info", |
| 41 | + skip = 2 |
| 42 | +) %>% |
| 43 | + tidyr::fill(everything()) %>% |
| 44 | + mutate( |
| 45 | + IDH = ifelse( |
| 46 | + stringr::str_detect(`Molecular alterations`, "IDH wildtype"), |
| 47 | + "WT", |
| 48 | + "MUT" |
| 49 | + ), |
| 50 | + CDKN2A = ifelse( |
| 51 | + stringr::str_detect( |
| 52 | + `Molecular alterations`, |
| 53 | + "CDKN2A homozygous deletion" |
| 54 | + ), |
| 55 | + "DEL", |
| 56 | + "WT" |
| 57 | + ), |
| 58 | + TP53 = ifelse( |
| 59 | + stringr::str_detect( |
| 60 | + `Molecular alterations`, |
| 61 | + "TP53 p\\.C277Wfs\\*27 mutation" |
| 62 | + ) | |
| 63 | + stringr::str_detect(`Molecular alterations`, "P53 Mutated"), |
| 64 | + "MUT", |
| 65 | + "WT" |
| 66 | + ), |
| 67 | + ATRX = ifelse( |
| 68 | + stringr::str_detect(`Molecular alterations`, "ATRX retained"), |
| 69 | + "WT", |
| 70 | + "MUT" |
| 71 | + ), |
| 72 | + BRAF = ifelse( |
| 73 | + stringr::str_detect(`Molecular alterations`, "BRAF mutation"), |
| 74 | + "MUT", |
| 75 | + "WT" |
| 76 | + ), |
| 77 | + ) %>% |
| 78 | + group_by_at(c( |
| 79 | + "Patient ID", |
| 80 | + "Sex", |
| 81 | + "IDH", |
| 82 | + "CDKN2A", |
| 83 | + "TP53", |
| 84 | + "ATRX", |
| 85 | + "BRAF", |
| 86 | + "Region annotation" |
| 87 | + )) %>% |
| 88 | + summarise(n = n()) %>% |
| 89 | + pivot_wider(names_from = "Region annotation", values_from = "n") |
| 90 | + |
| 91 | +col_annot_df <- df %>% |
| 92 | + ungroup() %>% |
| 93 | + dplyr::select(all_of(c("Sex"))) %>% |
| 94 | + pull() |
| 95 | + |
| 96 | + |
| 97 | +# ---- Create Figure 1a ---- # |
| 98 | + |
| 99 | +col_annot <- HeatmapAnnotation( |
| 100 | + Sex = col_annot_df, |
| 101 | + which = "col", |
| 102 | + col = list( |
| 103 | + Sex = c("Male" = "white", "Female" = "black") |
| 104 | + ), |
| 105 | + gp = gpar(col = "black") |
| 106 | +) |
| 107 | + |
| 108 | +heatmap_df <- df %>% |
| 109 | + ungroup() %>% |
| 110 | + dplyr::select(all_of(mutations_oi)) |
| 111 | +heatmap_df <- as.matrix(heatmap_df) |
| 112 | +rownames(heatmap_df) <- df %>% pull(`Patient ID`) |
| 113 | +ht1 <- Heatmap( |
| 114 | + t(heatmap_df), |
| 115 | + name = "Molecular features", |
| 116 | + col = c("WT" = "white", "MUT" = "grey", "DEL" = "black"), |
| 117 | + cluster_rows = FALSE, |
| 118 | + cluster_columns = FALSE, |
| 119 | + border = TRUE, |
| 120 | + cell_fun = cell_border_fun, |
| 121 | + show_column_names = FALSE, |
| 122 | + top_annotation = col_annot, |
| 123 | +) |
| 124 | + |
| 125 | +heatmap_df2 <- df %>% |
| 126 | + ungroup() %>% |
| 127 | + dplyr::select(all_of(regions_oi)) |
| 128 | +heatmap_df2 <- as.matrix(heatmap_df2) |
| 129 | +rownames(heatmap_df2) <- df %>% pull(`Patient ID`) |
| 130 | +heatmap_df2 <- ifelse(is.na(heatmap_df2), 0, heatmap_df2) |
| 131 | +col_scheme <- brewer.pal(5, "Blues") |
| 132 | +col_scheme[1] <- "white" |
| 133 | + |
| 134 | +ht2 <- Heatmap( |
| 135 | + t(heatmap_df2), |
| 136 | + name = "Number of samples from region", |
| 137 | + col = circlize::colorRamp2(c(0, 1, 2, 3, 4), col_scheme), |
| 138 | + cluster_rows = FALSE, |
| 139 | + cluster_columns = FALSE, |
| 140 | + border = TRUE, |
| 141 | + cell_fun = cell_border_fun |
| 142 | +) |
| 143 | + |
| 144 | +heatmap <- ht1 %v% ht2 |
| 145 | + |
| 146 | +pdf( |
| 147 | + file = file.path(params$plot_dir, "Fig1a.pdf"), |
| 148 | + width = 10, |
| 149 | + height = 10 |
| 150 | +) |
| 151 | +draw(heatmap, ht_gap = unit(0.5, "cm")) |
| 152 | +dev.off() |
0 commit comments