-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnaseq_analysis.qmd
276 lines (200 loc) · 7.42 KB
/
rnaseq_analysis.qmd
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
---
title: "RNASeq Analysis"
author: "Otoniel Maya"
date: "16-Feb-2024"
format: html
editor: visual
execute:
eval: false
---
# Reporting data analysis
For this analysis try to keep a estructured folder tree in the same directory. Something like this:
```{r, eval=FALSE}
project_folders <- function() {
base::dir.create("00_raw_data")
base::dir.create("01_analysis")
base::dir.create("02_tidy_data")
base::dir.create("03_r_scripts")
base::dir.create("04_plots")
base::dir.create("99_results")
}
# project_folders()
```
You can follow your own system but keep a logical and sequential order. For this tutorial we will follow this convention using Snakemake inside the `analysis` folder.
This tutorial is a reproducible implementation from: [Research Technology Bioinformatics Tools for Life Science RNAseq tutorial](https://tuftsdatalab.github.io/Research_Technology_Bioinformatics/tutorials/IntroToRNAseq/lessons/05_Differential_Expression.html) to make it reproducible™.
### Packages
Initiate `renv`:
```{r, echo=FALSE}
#| warning: true
if (!require("renv", quietly = TRUE))
install.packages("renv", ask=FALSE)
renv::init(bioconductor = TRUE, force = TRUE)
```
### CRAN Packages installation on renv
```{r}
#| warning: false
pkgs <- c("here", "tidyverse", "yaml")
# Install the missing packages, including their dependencies
for (lib in pkgs[!pkgs %in% installed.packages()]) {
install.packages(lib, dependencies = TRUE)
}
```
Bioconductor missing dependency to handle Kallisto h5 files.
```{r, echo=FALSE}
#| warning: false
BiocManager::install('rhdf5', update = FALSE)
# BiocManager::install(c('SummarizedExperiment', 'HDO.db', 'org.Sc.sgd.db', 'DESeq2', 'apeglm', 'tximport', 'rhdf5', 'biomaRt'), update = FALSE)
# renv::status()
```
Usually `renv::init()` creates a renv.lock file but if something failes it will be necessary to take a renv snapshot manually:
```{r}
renv::snapshot()
```
Loading all the packages, however, to keep track of each function a syntax `library::function()` will be used in the follow code.
```{r}
#| warning: false
library(DESeq2)
library(ggplot2)
library(dplyr)
library(magrittr)
library(DEGreport)
library(pheatmap)
library(org.Sc.sgd.db)
library(clusterProfiler)
```
### Loading data
Don't forget to use here, it helps a lot with **`here::here()`**
```{r}
metadata <- readr::read_table(here::here("analysis/metadata.tsv"))
kallisto_dir <-"analysis/rnaseqSE_snakemake/02_mapping/kallisto"
lsdir <- list.dirs(here::here(kallisto_dir), recursive = FALSE)
kallisto_files <- paste0(lsdir, "/abundance.h5")
```
```{r}
ensembl <- biomaRt::useMart(biomart = "ensembl",
dataset = "scerevisiae_gene_ensembl")
t2g <- biomaRt::getBM(attributes = c("ensembl_transcript_id",
"ensembl_gene_id",
"external_gene_name"),
mart = ensembl)
txi_kallisto <- tximport::tximport(kallisto_files,
type = "kallisto",
tx2gene = t2g,
ignoreTxVersion = TRUE)
```
```{r}
dds <- DESeq2::DESeqDataSetFromTximport(txi_kallisto, colData=metadata, design = ~ condition)
dds <- DESeq2::DESeq(dds)
```
### PCA analysis
```{r}
rld <- DESeq2::rlog(dds, blind=TRUE)
DESeq2::plotPCA(rld, intgroup="condition") + ggplot2::geom_text(ggplot2::aes(label=name))
```
## Creating contrasts and running a Wald test
```{r}
contrast <- c("condition", "SNF2", "WT")
res_unshrunken <- DESeq2::results(dds, contrast=contrast)
DESeq2::summary(res_unshrunken)
```
### Shrinkage of the log2 fold changes
```{r}
res <- DESeq2::lfcShrink(dds, contrast=contrast, res=res_unshrunken, type="normal")
DESeq2::summary(res)
head(res)
```
### Filtering to find significant genes
```{r}
padj_cutoff <- 0.05 # False Discovery Rate cutoff
significant_results <- res[which(res$padj < padj_cutoff), ]
```
save results using customized file_name
```{r}
write.table(significant_results,
'analysis/significant_padj_0.05.txt',
quote=FALSE)
```
### Visualization
Simple plot for a single gene YOR290C (SNF2)
```{r}
plotCounts(dds, gene="YOR290C", intgroup="condition")
```
### Heatmap
Plot multiple genes in a heatmap: extract the counts from the rlog transformed object and select by row name using the list of genes:
```{r}
significant_results_sorted <- significant_results[order(significant_results$padj), ]
significant_genes_25 <- rownames(significant_results_sorted[1:25, ])
rld_counts <- SummarizedExperiment::assay(rld)
rld_counts_sig <- rld_counts[significant_genes_25, ]
colnames(rld_counts_sig) <- metadata$sample
annotation <- as.data.frame(metadata)
rownames(annotation) <- annotation$sample
annotation$sample <- NULL
pheatmap::pheatmap(rld_counts_sig,
cluster_rows = T,
show_rownames = F,
annotation_col = annotation,
border_color = NA,
fontsize = 10,
scale = "row",
fontsize_row = 8,
height = 20)
```
load previously saved result
```{r}
significant_results_test <- read.table("analysis/significant_padj_0.05.txt",
header=TRUE, row.names = 1)
```
### Volcano plot
Add another column in the results table to label the significant genes using threshold of padj\<0.05 and absolute value of log2foldchange \>=1
```{r}
#| echo: true
res_table <- res %>%
data.frame() %>%
tibble::rownames_to_column(var="gene") %>%
tibble::as_tibble()
res_table <- res_table %>%
dplyr::mutate(threshold_OE = padj < 0.05 & abs(log2FoldChange) >= 1)
# you can view the modified table
head(res_table)
```
make volcano plot, the significant genes will be labeled in red
```{r}
ggplot(res_table) +
geom_point(aes(x = log2FoldChange,
y = -log10(padj),
colour = threshold_OE)) +
scale_color_manual(values=c("black", "red")) + # black v.s. red dots
ggtitle("SNF2 against WT") + # this line defines the title of the plot
xlab("log2 fold change") + # this line defines the name of the x-axis
ylab("-log10 adjusted p-value") + # name of y-axis
scale_x_continuous(limits = c(-7.5,7.5)) + # the axis range is set to be from -7.5 to 7.5
theme(legend.position = "none", #c(0.9, 0.9),
plot.title = element_text(size = rel(1.5), hjust = 0.5),
axis.title = element_text(size = rel(1.25)))
```
### Functional analysis
Run GO enrichment analysis for the top 500 genes using clusterProfiler
```{r}
significant_results_sorted <- res[order(res$padj), ]
significant_genes_500 <- rownames(significant_results_sorted[1:500, ])
ego <- clusterProfiler::enrichGO(gene = significant_genes_500,
keyType = "ENSEMBL",
OrgDb = org.Sc.sgd.db::org.Sc.sgd.db)
```
Output results from GO analysis to a table
```{r}
cluster_summary <- data.frame(ego)
```
Dotplot for the top 50 genes
```{r}
clusterProfiler::dotplot(ego, showCategory=10)
```
Enrichmap clusters the 50 most significant (by padj) GO terms to visualize relationships between terms
```{r, eval=FALSE}
d <- GOSemSim::godata("org.Sc.sgd.db", ont = "BP")
compare_cluster_GO_emap <- enrichplot::pairwise_termsim(ego,
semData = d,
method="Wang")
clusterProfiler::emapplot(compare_cluster_GO_emap, showCategory = 10)
```