-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGSVA.qmd
More file actions
249 lines (201 loc) · 6.51 KB
/
GSVA.qmd
File metadata and controls
249 lines (201 loc) · 6.51 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---
title: "GSVA"
author: "Harvard Chan Bioinformatics Core"
date: "`r Sys.Date()`"
format:
html:
code-fold: true
code-tools: true
code-overflow: wrap
df-print: paged
highlight-style: pygments
number-sections: true
self-contained: true
theme: default
toc: true
toc-location: right
toc-expand: false
lightbox: true
params:
# set column name and contrasts to be factors of interest
column: "sample_type"
contrasts:
value:
- ["sample_type", "tumor", "normal"]
project_file: ../information.R
params_file: ../00_params/params-example.R # example data
functions_file: ../00_libs
# select from gene set repository at https://github.com/bcbio/resources/tree/main/gene_sets/gene_sets
# choose geneset, click "Raw', and copy url to work with mouse data
geneset_fn: https://raw.githubusercontent.com/bcbio/resources/main/gene_sets/gene_sets/20240904/human/h.all.v2024.1.Hs.entrez.gmt
---
```{r}
#| message: FALSE
#| warning: FALSE
# This set up the working directory to this file so all files can be found
library(rstudioapi)
library(tidyverse)
# NOTE: This code will check version, this is our recommendation, it may work
# . other versions
stopifnot(R.version$major >= 4) # requires R4
if (compareVersion(R.version$minor, "3.1") < 0) warning("We recommend >= R4.3.1")
stopifnot(compareVersion(as.character(BiocManager::version()), "3.18") >= 0)
```
This code is in this  revision.
```{r libraries}
#| message: FALSE
#| warning: FALSE
## load libraries
library(GSVA)
library(GSEABase)
library(reshape2)
library(ChIPpeakAnno)
library(org.Hs.eg.db)
# library(org.Mm.eg.db)
library(AnnotationDbi)
library(DESeq2)
library(limma)
library(gridExtra)
library(bcbioR)
library(ggprism)
library(knitr)
library(clusterProfiler)
colors <- cb_friendly_cols(1:15)
ggplot2::theme_set(theme_prism(base_size = 14))
opts_chunk[["set"]](
cache = F,
cache.lazy = FALSE,
dev = c("png", "pdf"),
error = TRUE,
highlight = TRUE,
message = FALSE,
prompt = FALSE,
tidy = FALSE,
warning = FALSE,
echo = T,
fig.height = 4)
# set seed for reproducibility
set.seed(1234567890L)
```
```{r load_params}
#| cache: FALSE
#| message: FALSE
#| warning: FALSE
source(params$project_file)
source(params$params_file)
purr::map(list.files(params$functions_file, pattern = "*.R$", full.names = T), source) %>% invisible()
column <- params$column
contrasts <- params$contrasts
subset_column <- params$subset_column
subset_value <- params$subset_value
```
```{r sanitize_datatable}
sanitize_datatable <- function(df, ...) {
# remove dashes which cause wrapping
DT::datatable(df, ...,
rownames = gsub("-", "_", rownames(df)),
colnames = gsub("-", "_", colnames(df))
)
}
```
# Overview
- Project: `r project`
- PI: `r PI`
- Analyst: `r analyst`
- Experiment: `r experiment`
- Aim: `r aim`
```{r load_data}
coldata <- load_coldata(
coldata_fn, column,
subset_column, subset_value
)
coldata[[contrasts[[1]][1]]] <- relevel(as.factor(coldata[[contrasts[[1]][1]]]), contrasts[[1]][3])
coldata$sample <- row.names(coldata)
counts <- load_counts(counts_fn)
counts <- counts[, coldata$sample]
```
# Method
Gene Set Variation Analysis (GSVA) is a non-parametric, unsupervised method for estimating variation of gene set enrichment through the samples of a expression data set. GSVA performs a change in coordinate systems, transforming the data from a gene by sample matrix to a gene-set by sample matrix, thereby allowing the evaluation of pathway enrichment for each sample. This new matrix of GSVA enrichment scores facilitates applying standard analytical methods like functional enrichment, survival analysis, clustering, CNV-pathway analysis or cross-tissue pathway analysis, in a pathway-centric manner. More info in the vignette [here](https://bioconductor.org/packages/release/bioc/vignettes/GSVA/inst/doc/GSVA.html).
Hänzelmann S, Castelo R, Guinney J (2013). “GSVA: gene set variation analysis for microarray and RNA-Seq data.” BMC Bioinformatics, 14, 7. doi:10.1186/1471-2105-14-7, [https://doi.org/10.1186/1471-2105-14-7](https://doi.org/10.1186/1471-2105-14-7)
# Data
```{r show_coldata}
coldata %>% sanitize_datatable()
```
```{r normalize_data}
dds <- DESeqDataSetFromMatrix(counts,
colData = coldata,
design = ~1
)
dds <- DESeq(dds)
norm_counts <- counts(dds, normalized = TRUE)
```
```{r ensembl_to_entrez}
## convert ensembl to entrez
entrezIDs_all <- convert2EntrezID(
IDs = rownames(norm_counts), orgAnn = "org.Hs.eg.db",
ID_type = "ensembl_gene_id"
)
entrezid <- mapIds(org.Hs.eg.db, keys = rownames(norm_counts), keytype = "ENSEMBL", column = "ENTREZID")
counts_entrez <- norm_counts
stopifnot(nrow(counts_entrez) == length(entrezid))
rownames(counts_entrez) <- entrezid
library(matrixStats)
if (any(duplicated(rownames(counts_entrez)))) {
counts_entrez <- rowsum(as.matrix(counts_entrez), group = rownames(counts_entrez))
}
```
# Prep and run GSVA
```{r load_genesets}
gene_sets <- read.gmt(params$geneset_fn)
genes_by_pathway <- split(gene_sets$gene, gene_sets$term)
```
```{r GSVA}
#| message: FALSE
#| warning: FALSE
gsvaPar <- GSVA::gsvaParam(counts_entrez, genes_by_pathway, kcdf = "Poisson")
gsva.es <- gsva(gsvaPar, verbose = F)
```
## Test for Significance
```{r limma}
mod <- model.matrix(~ factor(coldata[[column]]))
fit <- lmFit(gsva.es, mod)
fit <- eBayes(fit)
res <- topTable(fit, coef = paste0("factor(coldata[[column]])", contrasts[[1]][2]), number = Inf, sort.by = "P")
res %>% sanitize_datatable()
```
## Graph top 5 pathways
```{r graph_pathways}
#| results: 'asis'
scores <- t(gsva.es)
sig <- subset(res, res$adj.P.Val < 0.1)
if (nrow(sig) >= 5) {
pathways <- rownames(sig)[1:5]
} else if (nrow(sig) == 0) {
pathways <- c()
} else {
pathways <- rownames(sig)
}
if (length(pathways) > 0) {
to_graph <- data.frame(scores[, pathways]) %>%
rownames_to_column("sample") %>%
pivot_longer(!sample, names_to = "pathway", values_to = "enrichment_score")
to_graph <- left_join(to_graph, coldata)
for (single_pathway in pathways) {
cat("### ", single_pathway, "\n")
to_graph_single_pathway <- to_graph %>% filter(pathway == single_pathway)
p <- ggplot(to_graph_single_pathway, aes(x = .data[[column]], y = enrichment_score)) +
geom_boxplot() +
geom_point(alpha = 0.5) +
ggtitle(single_pathway)
print(p)
cat("\n\n")
}
} else {
cat("No pathways were detected as significantly enriched")
}
```
# R session
List and version of tools used for the QC report generation.
```{r}
sessionInfo()
```