-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsea.R
151 lines (119 loc) · 3.71 KB
/
gsea.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
# Title: GSEA of microarray data
# Author: Albert García López
pacman::p_load(tidyverse, janitor, vroom, readxl, skimr, clusterProfiler,
enrichplot, org.Hs.eg.db, DescTools, cowplot)
conflicted::conflict_prefer("filter", "dplyr")
conflicted::conflict_prefer("select", "dplyr")
conflicted::conflict_prefer("slice", "dplyr")
# Load results directory #######################################################
resDir <- "path_to_dir"
# Load processed data ##########################################################
df_processed <- vroom("diff_expression_file.csv")
# Exploratory Data Analysis ####################################################
# Data summary
skim(df_processed)
# Any duplicates?
df_processed %>%
sapply(anyDuplicated) %>%
enframe() %>%
filter(value > 0)
# GSEA #########################################################################
# Prepare gene_list_object:
# 1. Numeric vector: fold change or other type of numerical variable.
# 2. Named vector: every number has a name, the corresponding gene ID.
# 3. Sorted vector: number should be sorted in decreasing order.
gene_list <- df_processed %>%
select(entrez, logFC) %>%
mutate(logFC = -logFC) %>%
deframe() %>%
sort(decreasing = TRUE)
# Gene Ontology (GO)
set.seed(2)
gsea_go <- gseGO(
geneList = gene_list,
ont = "ALL",
OrgDb = org.Hs.eg.db,
keyType = "ENTREZID",
pAdjustMethod = "fdr",
pvalueCutoff = 0.05,
eps = 0,
verbose = FALSE
) %>%
setReadable(OrgDb = "org.Hs.eg.db", keyType = "ENTREZID")
# Ranked plots #################################################################
# Keywords
keywords <- c("proliferation", "death", "activation", "survival", "viable", "viability")
# Select pathways to plot
sel_paths <- gsea_go@result %>%
as_tibble() %>%
filter(str_detect(Description, paste(keywords, collapse = "|")))
sel_ids <- which(gsea_go@result$ID %in% sel_paths$ID)
# Plots
ranked_plots <- list()
for (i in 1:length(sel_ids)) {
metrics <- gsea_go@result %>%
as_tibble() %>%
slice(sel_ids[i]) %>%
select(setSize, NES, pvalue, p.adjust, qvalues)
ranked_plots[[i]] <- gseaplot2(
x = gsea_go,
geneSetID = sel_ids[i],
title = StrCap(gsea_go@result[sel_ids[i], "Description"]),
color = "mediumseagreen",
base_size = 25
) +
annotate(
geom = "text",
x = 10000,
y = -1.5,
size = 7,
label = paste0(
"Gene set size = ", metrics$setSize, "\n",
"NES = ", round(metrics$NES, 3), " / ",
"p-value = ", formatC(x = metrics$pvalue, digits = 3, format = "e"), " / ",
"adj. p-value = ", formatC(x = metrics$p.adjust, digits = 3, format = "e")
)
) +
annotate(
geom = "text",
x = 1500,
y = 2,
size = 9,
fontface = 2,
color = "#CF2E28",
label = "IL1B"
) +
annotate(
geom = "text",
x = 19000,
y = 2,
size = 9,
fontface = 2,
color = "#2062A6",
label = "Th17",
) +
theme(
plot.title = element_text(face = "bold"),
plot.margin = unit(c(0,1,0,1), "cm")
)
}
# Visualize and export plots ###################################################
pdf(
file = "file_name.pdf",
width = 35,
height = 20,
family = "sans",
compress = TRUE,
onefile = TRUE
)
plot_grid(plotlist = ranked_plots[1:6], align = "hv", axis = "tblr")
plot_grid(plotlist = ranked_plots[7:12], align = "hv", axis = "tblr")
plot_grid(plotlist = ranked_plots[13:18], align = "hv", axis = "tblr")
plot_grid(plotlist = ranked_plots[19:24], align = "hv", axis = "tblr")
plot_grid(plotlist = ranked_plots[25], align = "hv", axis = "tblr")
dev.off()
# Export metrics for the selected pathways #####################################
vroom_write(
x = sel_paths,
file = "file_name.tsv"
)