-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_pathwaygoi.R
194 lines (171 loc) · 6.96 KB
/
module_pathwaygoi.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
## pathway GOI module
pathway_UI <- function(id) {
ns <- NS(id)
fluidPage(
theme =
shinytheme("flatly"),
titlePanel(
"GSEA: Interrogation of pathways containing a gene of interest"
),#end title
h4("Positive NES is upregulated in Primitive cells and negative NES is upregulated in Monocytic cells"),
sidebarLayout(
sidebarPanel(
useShinyjs(),
#gene list dropdown menu of all genes from DE table
selectizeInput(
ns("Pathwaygenechoice"),
label=
"Choose a gene of interest",
choices =
NULL,
selected = NULL,
options = list(maxItems = 1) #only one gene can be selected at once
),
hr(),
#pathway set dropdown list
selectInput(ns("genefilechoice"), "Choose gmt file to plot pathways containing the gene of interest",
choices = c(Hallmark = "hallmark", GOall = "GOall", GOmolecular = "GOmolec",
GOcellcomp = "GOcellcomp", GObio = "GObio", TFtargets = "TFtargets",
allRegular = "allReg", Wiki = "wiki", Reactome = "reactome", KEGG = "KEGG",
Positional = "positional", Biocarta = "biocarta", lsc = "lsc", aeg = "aeg")),
hr(),
#color palette options for GOI
paletteUI(ns("paletteGOI")),
hr(),
sliderUI(ns("goiheightslider"), 200, 1000, 400, "Adjust plot height"
),
hr(),
sliderUI(ns("goiwidthslider"), 200, 1000, 600, "Adjust plot width"
),
downloadButton(
ns("downloadGOI"),
label =
"Download Plot"
)
),
mainPanel(
shinycssloaders::withSpinner( #add loading spinners
plotOutput(
ns("PathwaysGenePlot")
)
)
)
)
)
}
pathway_Server <- function(id, dds, t2g, dds.res) {
moduleServer(id, function(input,output,session) {
ens2gene <- t2g[,c(2,3)]
#object for pathway choice files to use with the goi pathway input
gene_gsea_file_values <- list("hallmark" = pathways.hallmark,
"goall" = pathways.GOall,
"GOmolec" = pathways.GOmolec,
"GOcellcomp" = pathways.GOcellcomp,
"GObio" = pathways.GObio,
"TFtargets" = pathways.TFtargets,
"allReg" = pathways.allReg,
"wiki" = pathways.Wiki,
"reactome" = pathways.Reactome,
"KEGG" = pathways.KEGG,
"positional" = pathways.Positional,
"biocarta" = pathways.Positional,
"lsc" = pathways.lsc,
"aeg" = pathways.aeg)
#reactive title
gsea_gene_title <-
eventReactive(input$Pathwaygenechoice, {
paste(input$Pathwaygenechoice)
})
#reactive color palette from module
colorGOI <-
paletteServer("paletteGOI")
#height and width sliders
goiheights <-
sliderServer("goiheightslider")
goiwidths <-
sliderServer("goiwidthslider")
# Extract the dds results in a tidy format
res <- results(dds, tidy = TRUE)
# Add the human name of the gene to the last column, because that's what all of the pathways are annotated using
res <- inner_join(res, ens2gene, by = c("row" = "ensembl_gene_id"))
colnames(res)[8] <- 'HS_Symbol'
# Select only the human gene symbol and the 'stat' from the results, remove NAs, and average the test stat for duplicate gene symbols
res2 <- res %>%
dplyr::select(HS_Symbol, stat) %>%
na.omit() %>%
distinct() %>%
group_by(HS_Symbol) %>%
summarize(stat = mean(stat))
# Reconfigure the data
ranks <- deframe(res2)
ranks <- sort(ranks)
genecentricgseaplot <- reactive({
#load chosen pathway file based on reactive input
genepathwaygsea <- (gene_gsea_file_values[[input$genefilechoice]])
#load fgsea table data for chosen pathway
fgseaRes <-
fgsea::fgsea(pathways = genepathwaygsea,
stats = ranks,
nproc = 10)
#create tidy table
fgseaResTidy <- fgseaRes %>%
as_tibble() %>%
arrange(desc(NES))
#create object for storing pathways that contain the chosen gene
goi_paths <- genepathwaygsea %>% keep(grepl(input$Pathwaygenechoice, genepathwaygsea))
goi_paths <- list(grep(input$Pathwaygenechoice, genepathwaygsea))
#filter gsea table for pathways in which the GOI is in the leading edge
goi_paths <- fgseaResTidy %>%
dplyr::filter(grepl(input$Pathwaygenechoice, leadingEdge)) %>%
mutate(., class = ifelse(NES <0, 'Mono', 'Prim'))
#create object for gene reactive input
GOI <- input$Pathwaygenechoice
#make a column that says yes if goi in that pathway
goi_paths$GOI <- "pathways with GOI"
#filter gsea table to find pathways that do not include the GOI in the leading edge
nongoi_paths <- fgseaResTidy %>%
dplyr::filter(!grepl(input$Pathwaygenechoice, leadingEdge)) %>%
mutate(., class = ifelse(NES <0, 'Mono', 'Prim'))
#put no for pathways that do not contain the goi
nongoi_paths$GOI <- "pathways NOT with GOI"
#bind the two filtered data frames into one for plotting
allgoi_paths <- rbind.data.frame(goi_paths, nongoi_paths)
})
output$PathwaysGenePlot <- renderPlot(
width = function() goiwidths(),
height = function() goiheights(),
{
ggplot(genecentricgseaplot(), aes(
x = class,
y = NES,
color = (padj < 0.05)
)) +
geom_boxplot() +
scale_color_viridis_d(option = colorGOI()) +
facet_wrap( ~ GOI, scales = "free") +
theme_light(base_size = 18) +
theme(axis.title = element_text(face = "bold"), title = element_text(face = "bold")) +
geom_hline(yintercept = 0, linetype = "dashed") +
plot_annotation(
title = "Pathways with and without Gene of Interest",
theme =
theme(
plot.title =
element_text(
face = "bold",
hjust = 0.5,
size = 16
)
)
)
})
output$downloadGOI <- downloadHandler(
filename = function() { paste("Gene of Interest Plot", '.png', sep='') },
content = function(file) {
ggsave(file, device = "png", width = 8, height = 6, units = "in",dpi = 72)
}
)
# #gene list for gene centric pathway analysis
updateSelectizeInput(session,"Pathwaygenechoice", choices = dds.res$Gene, server = TRUE)
})
}