-
Notifications
You must be signed in to change notification settings - Fork 1
/
3_Enrichment.Rmd
332 lines (271 loc) · 9.22 KB
/
3_Enrichment.Rmd
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
---
title: "Enrichment Analysis"
author: "Steve Pederson"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
autodep = TRUE,
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.align = "center"
)
```
# Setup
```{r loadPackages}
library(tidyverse)
library(magrittr)
library(edgeR)
library(scales)
library(pander)
library(goseq)
library(msigdbr)
library(AnnotationDbi)
library(RColorBrewer)
```
```{r setOpts}
theme_set(theme_bw())
panderOptions("table.split.table", Inf)
panderOptions("table.style", "rmarkdown")
panderOptions("big.mark", ",")
```
```{r samplesAndLabels}
samples <- read_csv("data/samples.csv") %>%
distinct(sampleName, .keep_all = TRUE) %>%
dplyr::select(sample = sampleName, sampleID, genotype) %>%
mutate(
genotype = factor(genotype, levels = c("WT", "Het", "Hom")),
mutant = genotype %in% c("Het", "Hom"),
homozygous = genotype == "Hom"
)
genoCols <- samples$genotype %>%
levels() %>%
length() %>%
brewer.pal("Set1") %>%
setNames(levels(samples$genotype))
```
```{r loadFits}
dgeList <- read_rds("data/dgeList.rds")
fit <- read_rds("data/fit.rds")
entrezGenes <- dgeList$genes %>%
dplyr::filter(!is.na(entrezid)) %>%
unnest(entrezid) %>%
dplyr::rename(entrez_gene = entrezid)
```
```{r formatP}
formatP <- function(p, m = 0.0001){
out <- rep("", length(p))
out[p < m] <- sprintf("%.2e", p[p<m])
out[p >= m] <- sprintf("%.4f", p[p>=m])
out
}
```
# Introduction
Enrichment analysis for this datast present some challenges.
Despite normalisation to account for gene length and GC bias, some appeared to still be present in the final results.
In addition, the confounding of incomplete rRNA removal with genotype may lead to other distortions in both DE genes and ranking statistics.
Two methods for enrichment analysis will be undertaken.
1. Testing for enrichment within discrete sets of DE genes as defined in the previous steps
2. Testing for enrichment within ranked lists, regardless of DE status or statistical significance.
Testing for enrichment *within discrete gene sets* will be performed using `goseq` as this allows for the incorporation of a single covariate as a predictor of differential expression.
GC content, gene length and correlation with rRNA removal can all be supplied as separate covariates.
Testing for enrichment *with ranked lists* will be performed using `fry` as this can take a dgeList directly.
However, in order for testing to be robust in the context of bias, normalised counts after CQN will be used for generation of a new dgeList for testing.
# Databases used for testing
Data was sourced using the `msigdbr` package.
The initial database used for testing was the Hallmark Gene Sets, with mappings from gene-set to EntrezGene IDs performed by the package authors.
# Discrete DE Gene Sets
```{r topTables}
topTables <- list.files(
path = "output", pattern = "Vs.+csv", full.names = TRUE
) %>%
sapply(read_csv, simplify = FALSE) %>%
set_names(basename(names(.))) %>%
set_names(str_remove(names(.), ".csv")) %>%
lapply(function(x){
x %>%
mutate(
entrezid = dgeList$genes$entrezid[gene_id]
)
})
```
## Effects of the *psen2^S4Ter^ mutation
The first step of analysis using `goseq`, regardless of the geneset, is estimation of the probability weight function (PWF) which quantifies the probablity of a gene being considered as DE based on a single covariate.
Two possible covariates (GC content & Gene Length) were checked
```{r pwf}
pwf <- c("gc_content", "length") %>%
sapply(
function(x){
topTables$psen2VsWT %>%
dplyr::select(gene_id, DE, covariate = one_of(x)) %>%
distinct(gene_id, .keep_all = TRUE) %>%
with(
nullp(
DEgenes = structure(
as.integer(DE), names = gene_id
),
genome = "danRer10",
id = "ensGene",
bias.data = covariate,
plot.fit = FALSE
)
)
},
simplify = FALSE
)
```
```{r checkBias, fig.width=8, fig.height=4, fig.cap = "*Bias data based on each of the possible covariates. Both appeared to impact the probability of a gene being DE to some extent, with gene length being selected as the covariate for usage inthe goseq model.*"}
par(mfrow = c(1, length(pwf)))
names(pwf) %>%
lapply(function(x){
plotPWF(pwf[[x]], main = x)
})
par(mfrow = c(1, 1))
```
### Hallmark Gene Sets
```{r hm}
hm <- msigdbr("Danio rerio", category = "H") %>%
left_join(entrezGenes) %>%
dplyr::filter(!is.na(gene_id))
```
```{r hmByGene}
hmByGene <- hm %>%
split(f = .$gene_id) %>%
lapply(extract2, "gs_name")
```
Mappings are required from gene to pathway, and Ensembl identifiers were used to map from gene to pathway, based on the mappings in the previously used annotations (Ensembl Release 96).
A total of `r length(hmByGene)` Ensembl IDs were mapped to pathways from the hallmark gene sets.
This contrasts with `r length(unique(hm$entrez_gene))` EntrezGene IDs.
```{r hmGoSeq}
hmGoseq <- goseq(pwf$length, gene2cat = hmByGene) %>%
as_tibble %>%
dplyr::filter(numDEInCat > 0) %>%
mutate(
adjP = p.adjust(over_represented_pvalue, method = "bonf"),
FDR = p.adjust(over_represented_pvalue, method = "fdr")
) %>%
dplyr::select(-contains("under"))
```
No hallmark genesets were considered to be enriched after adjusting for the observed length bias in the data.
### KEGG Gene Sets
The same mapping process was applied to KEGG gene sets.
```{r kg}
kg <- msigdbr("Danio rerio", category = "C2", subcategory = "CP:KEGG") %>%
left_join(entrezGenes) %>%
dplyr::filter(!is.na(gene_id))
```
```{r kgByGene}
kgByGene <- kg %>%
split(f = .$gene_id) %>%
lapply(extract2, "gs_name")
```
A total of `r length(kgByGene)` Ensembl IDs were mapped to pathways from the KEGG gene sets.
This contrasts with `r length(unique(kg$entrez_gene))` EntrezGene IDs.
```{r kgGoseq}
kgGoseq <- goseq(pwf$length, gene2cat = kgByGene) %>%
as_tibble %>%
dplyr::filter(numDEInCat > 0) %>%
mutate(
adjP = p.adjust(over_represented_pvalue, method = "bonf"),
FDR = p.adjust(over_represented_pvalue, method = "fdr")
) %>%
dplyr::select(-contains("under"))
```
```{r kgGoseqTable}
kgGoseq %>%
dplyr::slice(1:5) %>%
dplyr::rename(p = over_represented_pvalue) %>%
mutate(
p = formatP(p),
adjP = formatP(adjP),
FDR = formatP(FDR)
) %>%
dplyr::select(
Category = category,
DE = numDEInCat,
`Set Size` = numInCat,
p,
`p~bonf~` = adjP,
`p~FDR~` = FDR
) %>%
pander(
justify = "lrrrrr",
caption = paste(
"The", nrow(.), "most highly-ranked KEGG pathways.",
"Bonferroni-adjusted p-values are the most stringent and give high",
"confidence when below 0.05.",
)
)
```
Notably, the KEGG gene-set for Ribosomal genes was detected as enriched.
This is likely to be due to the previosuly discussed contaminant.
### GO Gene Sets
The same mapping process was applied to GO gene sets.
```{r goSummaries}
goSummaries <- url("https://uofabioinformaticshub.github.io/summaries2GO/data/goSummaries.RDS") %>%
readRDS() %>%
mutate(
Term = Term(id),
gs_name = Term %>% str_to_upper() %>% str_replace_all("[ -]", "_"),
gs_name = paste0("GO_", gs_name)
)
```
```{r go}
go <- msigdbr("Danio rerio", category = "C5") %>%
left_join(entrezGenes) %>%
dplyr::filter(!is.na(gene_id))
```
```{r goByGene}
minPath <- 3
goByGene <- go %>%
left_join(goSummaries) %>%
dplyr::filter(shortest_path >= minPath) %>%
split(f = .$gene_id) %>%
lapply(extract2, "gs_name")
```
A total of `r length(goByGene)` Ensembl IDs were mapped to pathways from the GO gene sets.
This contrasts with `r length(unique(go$entrez_gene))` EntrezGene IDs.
In addition GO genesets were restricted to those with `r minPath` or more steps back to each ontology root.
```{r goGoseq}
goGoseq <- goseq(pwf$length, gene2cat = goByGene) %>%
as_tibble %>%
dplyr::filter(numDEInCat > 0) %>%
mutate(
adjP = p.adjust(over_represented_pvalue, method = "bonf"),
FDR = p.adjust(over_represented_pvalue, method = "fdr")
) %>%
dplyr::select(-contains("under"))
```
```{r goGoseqTable}
goGoseq %>%
dplyr::filter(adjP < 0.05) %>%
dplyr::rename(p = over_represented_pvalue) %>%
mutate(
p = formatP(p),
adjP = formatP(adjP),
FDR = formatP(FDR)
) %>%
dplyr::select(
Category = category,
DE = numDEInCat,
`Set Size` = numInCat,
p,
`p~bonf~` = adjP,
`p~FDR~` = FDR
) %>%
pander(
justify = "lrrrrr",
caption = paste(
"The", nrow(.), "most highly-ranked GO terms.",
"Bonferroni-adjusted p-values are the most stringent and give high",
"confidence when below 0.05, and all terms reached this threshold.",
"However, most terms indicate the presence of rRNA once again."
)
)
```
## Effects of Homozygous Vs Heterozygous mutants
As only a small number of genes were detected as being classified as DE for this comparison, no testing was performed on this as a discrete set of DE genes.