-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeseq2_utils.R
264 lines (206 loc) · 10.6 KB
/
deseq2_utils.R
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
library(dplyr)
library(ggplot2)
library(topGO)
library(viridisLite)
library(stringr)
#####
# Function for classifying Deseq2 results output into Upregulated, Downregulated or Non-DEG categories based on
# log2 fold change and adjusted p-value
classify_DEGs <- function(deseq_res) {
deseq_res_df <- as.data.frame(deseq_res)
deseq_res_df <- rownames_to_column(deseq_res_df, var = "gene")
deseq_res_df$DEGs <- case_when(
deseq_res_df$padj < 0.05 & deseq_res_df$log2FoldChange > 2 ~ "Upregulated",
deseq_res_df$padj < 0.05 & deseq_res_df$log2FoldChange < -2 ~ "Downregulated",
TRUE ~ "Non-DEG"
)
deseq_res_df <- merge(deseq_res_df, annotation[c("query", "Preferred_name", "Description", "PFAMs", "GOs")],
by.x = "gene", by.y = "query", all.x = TRUE)
deseq_res_df$Preferred_name[is.na(deseq_res_df$Preferred_name)] <- "-"
deseq_res_df$Description[is.na(deseq_res_df$Description)] <- "-"
deseq_res_df_up <- deseq_res_df[deseq_res_df$DEGs == "Upregulated", ]
rownames(deseq_res_df_up) <- NULL
deseq_res_df_down <- deseq_res_df[deseq_res_df$DEGs == "Downregulated", ]
rownames(deseq_res_df_up) <- NULL
return(list(up = deseq_res_df_up, down = deseq_res_df_down, all = deseq_res_df))
}
# Function for plotting volcano plot for classified deseq2 output (see classify_DEGs function)
volcano_plot <- function(res_df, contrast) {
num_upregulated <- sum(res_df$DEGs == "Upregulated")
num_downregulated <- sum(res_df$DEGs == "Downregulated")
res_df_with_names <- res_df[res_df$Preferred_name != "-", ]
top_genes <- head(res_df_with_names[order(res_df_with_names$padj), ], 20)
name <- paste(c(contrast[2], contrast[3]), collapse = " vs ")
ggplot(res_df) +
geom_point(aes(x = log2FoldChange, y = -log10(padj), colour = DEGs), size = 0.5) +
geom_text(data = top_genes,
aes(x = log2FoldChange, y = -log10(padj), label = Preferred_name),
color = "black", size = 2, check_overlap = TRUE) +
ggtitle(name) +
xlab("log2FC") +
ylab("-log10 padj") +
theme(legend.position = "bottom", legend.key.size = unit(0.4, "cm"),
legend.text = element_text(size = 9),
plot.title = element_text(size = rel(1.1), hjust = 0.5),
axis.title = element_text(size = rel(0.8)),
axis.text = element_text(size = 7),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank()) +
scale_color_manual(name = NULL, values = c("Upregulated" = "orange",
"Downregulated" = "blue",
"Non-DEG" = "gray"),
labels = c(paste("Downregulated genes:", num_downregulated),
"Non-differentially expressed",
paste("Upregulated genes:", num_upregulated))) +
geom_vline(xintercept = c(-2, 2), linetype = "dashed", color = "black")+
geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "black")
}
#####
#Function that runs GO analysis on set of genes (also requires gene2GO list). Returns all terms from topGO run and reduced versions from rrvigo
#(all and unique). For each set calculates number of genes and fold change
run_GO_analysis <- function(degs_df, oli_geneID2GO) {
degs <- factor(as.integer(gene_names %in% degs_df$gene))
names(degs) <- gene_names
GOdata <- new("topGOdata", ontology = "BP", allGenes = degs, annot = annFUN.gene2GO, gene2GO = oli_geneID2GO)
resultFis <- runTest(GOdata, algorithm = "classic", statistic = "fisher")
resultsFis_df <- as.data.frame(score(resultFis))
resultsFis_df_signif <- subset(resultsFis_df, resultsFis_df$`score(resultFis)` < 0.01)
GO_set <- GenTable(GOdata, classic=resultFis, ranksOf="classicFisher",
topNodes = length(resultsFis_df_signif$`score(resultFis)`))
GO_subset <- subset(GO_set, Significant > 10)
GO_subset$classic <- gsub("<", "", GO_subset$classic)
simMatrix <- calculateSimMatrix(GO_subset$GO.ID,
orgdb="org.Hs.eg.db",
ont="BP",
method="Rel")
scores <- setNames(-log10(as.numeric(GO_subset$classic)), GO_subset$GO.ID)
reducedTerms <- reduceSimMatrix(simMatrix,
scores,
threshold=0.7,
orgdb="org.Hs.eg.db")
reducedTerms_uniq <- reducedTerms %>%
group_by(parentTerm) %>%
filter(row_number() == 1)
reducedTerms_uniq <- reducedTerms[!duplicated(reducedTerms$parentTerm), ]
reducedTerms_uniq <- as.data.frame(reducedTerms_uniq)
reducedTerms <- as.data.frame(reducedTerms)
#Get real number of genes per term for rrvgo output and calculate fold change:
filtered_oli_geneID2GO <- oli_geneID2GO[gene_names %in% degs_df$gene]
term_genes_subset <- length(filtered_oli_geneID2GO)
term_genes_all <- length(oli_geneID2GO)
calculate_num_genes <- function(term) {
sum(sapply(filtered_oli_geneID2GO, function(gene_list) term %in% gene_list))
}
calculate_fold_change <- function(num_genes, term) {
num_genes_term <- sum(sapply(oli_geneID2GO, function(gene_list) term %in% gene_list))
(num_genes / term_genes_subset) / (num_genes_term / term_genes_all)
}
reducedTerms_uniq$Significant <- sapply(reducedTerms_uniq$go, calculate_num_genes)
reducedTerms_uniq$fold_change <- mapply(calculate_fold_change, reducedTerms_uniq$Significant, reducedTerms_uniq$go)
reducedTerms_uniq$GO_id_desc <- paste(reducedTerms_uniq$go, "|", reducedTerms_uniq$term)
reducedTerms$Significant <- sapply(reducedTerms$go, calculate_num_genes)
reducedTerms$fold_change <- mapply(calculate_fold_change, reducedTerms$Significant, reducedTerms$go)
reducedTerms$GO_id_desc <- paste(reducedTerms$go, "|", reducedTerms$term)
GO_subset$fold_change <- (GO_subset$Significant / term_genes_subset) / (GO_subset$Annotated / term_genes_all)
GO_subset$score <- -log10(as.numeric(GO_subset$classic))
GO_subset$GO_id_desc <- paste(GO_subset$GO.ID, "|", GO_subset$Term)
return(list(reducedTerms_uniq = reducedTerms_uniq, GO_subset = GO_subset, reducedTerms = reducedTerms))
}
#####
# Function that plots GO histogram based on run_GO_analysis function results (see run_GO_analysis func)
plot_GO_hist <- function(rTerms_uniq_df, name) {
rTerms_uniq_df$GO_id_desc <- str_trunc(rTerms_uniq_df$GO_id_desc, width = 50, ellipsis = "...")
rTerms_uniq_df_top<- rTerms_uniq_df %>% top_n(25, fold_change)
ggplot(data = rTerms_uniq_df_top, aes(x = fold_change, y = reorder(GO_id_desc, fold_change), fill = Significant)) +
geom_bar(stat = "identity") +
geom_point(aes(x = fold_change, y = reorder(GO_id_desc, fold_change), size = score),
shape = 21, fill = "yellow") +
scale_fill_viridis_c(name = "Number of genes", option = "cividis") +
labs(
title = paste(name),
y = "GO term: biological process",
x = "Fold enrichment",
size = "Enrichment score"
) +
theme(
axis.text.y = element_text(size = 8),
plot.title = element_text(face = "bold", size = 10),
legend.text = element_text(size = 8),
legend.title = element_text(size = 8)
)
}
#####
# Function that finds genes from DEG list in annotation based on specific GO-term
get_term_genes <- function(degs, GO, oli_geneID2GO, annotation) {
degs2GO <- oli_geneID2GO[gene_names %in% degs$gene]
GOI <- names(Filter(function(gene_list) GO %in% gene_list, degs2GO))
GOI <- annotation[annotation$query %in% GOI, ]
return(GOI)
}
#####
# Function that creates a z-score matrix for set of genes (see get_term_genes function)
make_zscore_matrix <- function(GOI, normalized_counts) {
norm_expr_goi <- subset(normalized_counts, normalized_counts$gene %in% GOI$query)
norm_expr_goi$gene <- paste(norm_expr_goi$gene, " | ",
GOI$Preferred_name[match(norm_expr_goi$gene,
GOI$query)])
avg_norm_expr_goi <- data.frame(
gene = norm_expr_goi$gene,
egg = rowMeans(norm_expr_goi[, c(2:4)]),
blastula = rowMeans(norm_expr_goi[, c(5:7)]),
gastrula = rowMeans(norm_expr_goi[, c(6:9)]),
trochophore = rowMeans(norm_expr_goi[, c(10:13)]),
adult = rowMeans(norm_expr_goi[, c(14:16)])
)
gene <- avg_norm_expr_goi[,1]
vals <- as.matrix(avg_norm_expr_goi[,2:ncol(avg_norm_expr_goi)])
score <- NULL
for (i in 1:nrow(vals)) {
row <- vals[i,]
zscore <- (row - mean(row)) / sd(row)
score <- rbind(score, zscore)
}
row.names(score) <- gene
return(score)
}
#####
# Function that filters a DEGs list (see classify_DEGs function). The result table will have only genes (to some extent)
# that are related to tgf-beta, wnt, fgf, notch, sonic-hedgehog, etc pathways alongside transcriptional cis-regulators.
get_important_development_genes <- function(DEG_set) {
interesting_GOs <- c(
"GO:0000976", "GO:0000975", "GO:0000984", "GO:0001017", "GO:0044212",
"GO:0001228", "GO:0001077", "GO:0001205", "GO:0001209", "GO:0001211",
"GO:0001212", "GO:0001213", "GO:0140297", "GO:0001107", "GO:0033613",
"GO:0070491", "GO:0007219", "GO:0030179", "GO:0071363", "GO:0007169",
"GO:0007178", "GO:0000165", "GO:0000122", "GO:0010553", "GO:0045816",
"GO:0000978", "GO:0000980", "GO:0003002"
)
interesting_GOs <- paste(interesting_GOs, collapse = "|")
interesting_genes <- subset(DEG_set, grepl(interesting_GOs, DEG_set$GOs))
#I found out that many Forkhead-family genes don't have any GOs in eggNOG annotation. Thus this part of code exists:
forkhead_genes <- subset(DEG_set, grepl('Forkhead', DEG_set$PFAMs))
interesting_genes <- unique(rbind(interesting_genes, forkhead_genes))
return(interesting_genes)
}
#####
#Function that finds annotation for specific genes in list DEGs df based on name or/and description or/and PFAMs
get_genes_by_name <- function(name=NULL, description=NULL, pfam=NULL, datasets) {
GOIs <- data.frame()
for (dataset in datasets) {
subset_data <- data.frame()
if (!is.null(name)) {
subset_data <- rbind(subset_data, subset(dataset, grepl(name, Preferred_name)))
}
if (!is.null(description)) {
subset_data <- rbind(subset_data, subset(dataset, grepl(description, Description)))
}
if (!is.null(pfam)) {
subset_data <- rbind(subset_data, subset(dataset, grepl(pfam, PFAMs)))
}
GOIs <- rbind(GOIs, subset_data)
}
GOIs <- unique(GOIs)
GOIs <- annotation[annotation$query %in% GOIs$gene, ]
return(GOIs)
}