|
| 1 | +--- |
| 2 | +title: "GO Similarity Analysis" |
| 3 | +author: "Harvard Chan Bioinformatics Core" |
| 4 | +date: "`r Sys.Date()`" |
| 5 | +format: |
| 6 | + html: |
| 7 | + number-sections: false |
| 8 | + default-image-extension: svg |
| 9 | + lightbox: true |
| 10 | + callout-icon: false |
| 11 | + format-links: true |
| 12 | + toc: true |
| 13 | + theme: sandstone |
| 14 | + echo: true |
| 15 | + eval: true |
| 16 | + message: false |
| 17 | + warning: false |
| 18 | + code-copy: true |
| 19 | + code-overflow: wrap |
| 20 | + code-fold: true |
| 21 | + code-line-numbers: true |
| 22 | + embed-resources: true |
| 23 | + standalone: true |
| 24 | + html-math-method: katex |
| 25 | + fig-align: center |
| 26 | + fig-height: 4 |
| 27 | + fig-width: 4 |
| 28 | + grid: |
| 29 | + sidebar-width: 250px |
| 30 | + body-width: 900px |
| 31 | + margin-width: 300px |
| 32 | + comments: |
| 33 | + hypothesis: true |
| 34 | +execute: |
| 35 | + freeze: auto |
| 36 | + keep-md: true |
| 37 | +params: |
| 38 | + # Directory that hosts the pathway & DEG tables (one CSV per contrast) |
| 39 | + deg_result_dir: "https://raw.githubusercontent.com/bcbio/bcbioR-test-data/main/rnaseq/DEG_visualization" |
| 40 | + # **Vectors** of design‑matrix columns & contrasts to analyse |
| 41 | + column: !expr c("sample_type") |
| 42 | + contrasts: !expr c("normal_vs_tumor") |
| 43 | +--- |
| 44 | + |
| 45 | +## Overview |
| 46 | + |
| 47 | +::: {.callout-note title="Workflow summary"} |
| 48 | +- Load pathway–gene associations that pass an FDR < 0.05 filter |
| 49 | +- Keep pathways with ≥ 5 genes |
| 50 | +- Map each retained pathway to its GO identifier via **msigdbr** |
| 51 | +- Compute pair‑wise semantic similarity matrices with **simplifyEnrichment** |
| 52 | +- Cluster and visualise with **simplifyGO** |
| 53 | +::: |
| 54 | + |
| 55 | +## Setup |
| 56 | + |
| 57 | +```{r setup, include=FALSE} |
| 58 | +suppressPackageStartupMessages({ |
| 59 | + library(simplifyEnrichment) |
| 60 | + library(glue) |
| 61 | + library(dplyr) |
| 62 | + library(msigdbr) |
| 63 | + library(data.table) |
| 64 | + library(tidyr) |
| 65 | + library(purrr) |
| 66 | +}) |
| 67 | +``` |
| 68 | + |
| 69 | +## Load pathway tables |
| 70 | + |
| 71 | +```{r load-data} |
| 72 | +# All combinations of column & contrast supplied by the user |
| 73 | +input_grid <- expand_grid( |
| 74 | + column = params$column, |
| 75 | + contrast = params$contrasts |
| 76 | +) |
| 77 | +
|
| 78 | +# Read & combine pathway tables for all combinations |
| 79 | +pathway_gene_data <- map_dfr( |
| 80 | + seq_len(nrow(input_grid)), |
| 81 | + function(i) { |
| 82 | + col_i <- input_grid$column[i] |
| 83 | + con_i <- input_grid$contrast[i] |
| 84 | + file_url <- glue( |
| 85 | + "{params$deg_result_dir}/full_{col_i}_{con_i}_pathways.csv" |
| 86 | + ) |
| 87 | + fread(file_url, showProgress = FALSE) %>% |
| 88 | + mutate( |
| 89 | + column = col_i, |
| 90 | + contrast = con_i |
| 91 | + ) |
| 92 | + } |
| 93 | +) %>% |
| 94 | + filter(padj < 0.05) %>% # FDR threshold |
| 95 | + separate_rows(genes, sep = ",") %>% |
| 96 | + rename(gene = genes, padj_pathway = padj) %>% |
| 97 | + filter(gene != "" & !is.na(gene)) %>% |
| 98 | + unite("comb", c("column", "contrast"), remove = FALSE, sep = ":") |
| 99 | +pathway_gene_data <- split(pathway_gene_data, pathway_gene_data$comb) |
| 100 | +msig_all <- msigdbr(species = "Homo sapiens") %>% |
| 101 | + distinct(gs_name, gs_exact_source, gs_collection, gs_subcollection) |
| 102 | +``` |
| 103 | + |
| 104 | +## Map pathways → GO IDs |
| 105 | + |
| 106 | +```{r map-go} |
| 107 | +pathway_valid <- function(pathway_gene, msig) { |
| 108 | + valid_pathways_df <- pathway_gene %>% |
| 109 | + distinct(pathway, gene) %>% |
| 110 | + count(pathway, name = "gene_count") %>% |
| 111 | + filter(gene_count >= 5) |
| 112 | +
|
| 113 | + cat("Retained", nrow(valid_pathways_df), "pathways after filtering\n") |
| 114 | + head(valid_pathways_df) |
| 115 | +
|
| 116 | + matched_sets <- msig %>% |
| 117 | + filter(gs_name %in% valid_pathways_df$pathway) %>% |
| 118 | + arrange(gs_collection, gs_name, gs_exact_source) |
| 119 | +
|
| 120 | + go_ids <- split(matched_sets$gs_exact_source, matched_sets$gs_subcollection) |
| 121 | +
|
| 122 | + # Sanity‑check for unmatched pathway names |
| 123 | + unmatched <- setdiff(valid_pathways_df$pathway, msig_all$gs_name) |
| 124 | + if (length(unmatched) > 0) { |
| 125 | + message( |
| 126 | + "Pathways not mapped to GO: ", |
| 127 | + paste(unmatched, collapse = ", ") |
| 128 | + ) |
| 129 | + } |
| 130 | + return(go_ids) |
| 131 | +} |
| 132 | +valid_pathway_gene <- map(pathway_gene_data, pathway_valid, msig = msig_all) |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +## Compute similarity & clustering |
| 138 | + |
| 139 | +```{r comb} |
| 140 | +#| results: asis |
| 141 | +#| fig-width: 8 |
| 142 | +#| fig-height: 4 |
| 143 | +#| echo: false |
| 144 | +
|
| 145 | +for (comb in names(valid_pathway_gene)) { |
| 146 | + go_ids <- valid_pathway_gene[[comb]] |
| 147 | + onts <- grep(names(go_ids), pattern = "GO:", value = TRUE) |
| 148 | +
|
| 149 | + cat("### Pathway Set for ", comb, "\n\n") |
| 150 | + cat("::: {.panel-tabset}\n\n") |
| 151 | +
|
| 152 | + for (i in onts) { |
| 153 | + go_ont <- gsub("GO:", "", i) |
| 154 | + cat("### Ontology ", i, "\n\n", sep = "") |
| 155 | +
|
| 156 | + # Start a new graphics device so we can embed the figure inline |
| 157 | + { |
| 158 | + ont_mat <- GO_similarity(go_ids[[i]], ont = go_ont) |
| 159 | + cl_df <- simplifyGO(ont_mat, plot = FALSE) |
| 160 | + ht_clusters(ont_mat, cl_df$cluster) |
| 161 | + } |
| 162 | +
|
| 163 | + cat("\n\n") |
| 164 | + } |
| 165 | +
|
| 166 | + cat(":::\n\n") |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +## Session Info |
| 171 | + |
| 172 | +```{r session-info} |
| 173 | +sessionInfo() |
| 174 | +``` |
0 commit comments