-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDEGs.R
494 lines (433 loc) · 23.5 KB
/
DEGs.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
library(shiny)
library(shinydashboard)
library(stringr)
library(shinyjs)
linebreaks <- function(n){HTML(strrep(br(), n))}
DEGsUI <- function(id, label = "degs"){
fluidPage(
shinyjs::useShinyjs(),
fluidRow(
br(),
p("Please select the threshold for the p-value and fold change (FC). The FC threshold will be then transformed to log2 values."),
p("Only the DEGs which have both the p-value and the FC less than the respective thresholds will be considered in the analyses below."),
br(),
h4("Parameter thresholds:"),
column(4,
selectInput(NS(id,"pval"),
p("Adjusted P-value threshold"),
choices = list("NS"=1, "0.05"=0.05, "0.01"=0.01, "0.001"=0.001),
selected = 0.05),
br(),
textOutput(NS(id,"selected_pval"))),
column(4,
selectInput(NS(id,"FC"),
p("FC threshold"),
choices = list("NS"=1, "1.2"=1.2, "1.5"=1.5, "2"=2),
selected = 1.2),
br(),
textOutput(NS(id,"selected_FC"))),
column(4,
p("Thresholds Help"),
helpText("Note: in the options on the left, NS stands for non-significant;",
"therefore, the DEGs will not be filtered for either adjusted p-value",
"or fold change"))
),
linebreaks(4),
h4("Download files as ZIP folder?"),
fluidRow(
column(3,
checkboxGroupInput(NS(id, "zips"), "Files to be downloaded as ZIP:",
choices = c("Plots"="plots",
"Unfiltered DEGs" = "unfiltered DEGs",
"Filtered DEGs" = "filtered DEGs"))),
column(5, downloadButton(NS(id,'save_zip'), 'Download selected files as a ZIP folder')),
column(6,
helpText("Please note that the few moments may be required to download the files, ",
"especially if all three options are chosen"))
),
linebreaks(5),
h4("Output plots"),
p("Below you can find the following plots:"),
tags$ol(
tags$li("Overview of the sex-biased DEGs - number of DEGs, chromosome fractions of shared DEGs, sex-biased DEGs cellular locations"),
tags$li("Presence heatmaps of specific genes of interest - top 20 most frequent sex-specific DEGs, mitochondrial genes and X-escaping genes"),
tags$li("Percentage of known cell type markers"),
tags$li("Presence heatmaps of neuropsychiatric disease markers"),
tags$li("Hormone targets enrichment"),
tags$li("Percentage of androgen and estrogen response elements (ARE, ERE)"),
tags$li("Functional Enrichment - gene ontology (biological processes, cellular component, molecular function) and KEGG pathways")
),
linebreaks(2),
fluidRow(
tabBox(width = 18,
height = 1400,
title = "Overview of sex-biased DEGs",
id = "Overview_DEGs",
tabPanel(strong("The number of sex-biased DEGs"),
p("Below the number of sex-biased DEGs in each group for each sex and cell type can be found. "),
linebreaks(2),
imageOutput(NS(id,"num_degs_plot")),
linebreaks(45),
downloadButton(NS(id,'save_num_degs_plot'), 'Download plot as PNG'),
),
tabPanel(strong("Shared DEGs among datasets, colored by chromosome annotation"),
p("Below the fractions of which chromosome (Autosome, X or Y) the sex-biased DEGs belong to, ordered by how many groups share the DEGs. The fractions are in logaritmic scale to enhance the lower fractions. "),
linebreaks(2),
imageOutput(NS(id,"chr_fraction")),
linebreaks(45),
downloadButton(NS(id,'save_chr_fraction_plot'), 'Download plot as PNG')
),
tabPanel(strong('Sex-biased gene locations'),
p("In the plot, the frequency of cellular location is indicated by the size, while the color represents the cellular compartment. "),
linebreaks(2),
imageOutput(NS(id,"GeneLocation")),
linebreaks(45),
downloadButton(NS(id,'save_GeneLocation_plot'), 'Download plot as PNG')
)
)
),
linebreaks(5),
fluidRow(
tabBox(width = 18,
height = 1000,
title = "Presence heatmaps of specific genes of interest",
id = "Ind_Hmps",
tabPanel(strong("Top 20 most differentially present genes"),
imageOutput(NS(id,"mostdiffgenes")),
linebreaks(20),
downloadButton(NS(id,'save_mostdiffgenes_plot'), 'Download plot as PNG')
),
tabPanel(strong("Mitochondrial genes"),
imageOutput(NS(id,"MTgenes")),
linebreaks(20),
downloadButton(NS(id,'save_MTgenes_plot'), 'Download plot as PNG')
),
tabPanel(strong('X-escaping genes'),
imageOutput(NS(id,"Xescapinggenes")),
linebreaks(20),
downloadButton(NS(id,'save_Xescapinggenes_plot'), 'Download plot as PNG')
)
)
),
linebreaks(2),
box( height = 900, width = 1200,
strong('Percentage of known cell type markers from McKenzie et al. 2018'),
imageOutput(NS(id,"McKenzie"))
),
column(3,
downloadButton(NS(id,'save_McKenzie_plot'), 'Download plot as PNG')
),
linebreaks(2),
box( height = 900, width = 1200,
strong("Presence heatmaps of neuropsychiatric disease markers from Chlamydas et al. 2022"),
imageOutput(NS(id,"Chlamydas"))
),
column(3,
downloadButton(NS(id,'save_Chlamydas_plot'), 'Download plot as PNG')
),
linebreaks(2),
box( height = 1300, width = 900,
strong("Hormone targets enrichment from Jadhav et al. 2022"),
imageOutput(NS(id,"Hormones"))
),
column(3,
downloadButton(NS(id,'save_Hormones_plot'), 'Download plot as PNG')
),
linebreaks(2),
fluidRow(
column(6,
box( height = 900, width = 500,
strong('Percentages of ARE sites among the sex-biased DEGs'),
imageOutput(NS(id,"ARE"))),
downloadButton(NS(id,'save_ARE_plot'), 'Download plot as PNG')),
column(6,
box( height = 900, width = 500,
strong('Percentages of ERE sites among the sex-biased DEGs'),
imageOutput(NS(id,"ERE"))),
downloadButton(NS(id,'save_ERE_plot'), 'Download plot as PNG'))
),
linebreaks(5),
fluidRow(
tabBox(width = 18,
height = 2000,
title = "Functional Enrichment",
id = "Functional_enrichment",
tabPanel(strong("Gene Ontology (GO) - Biological Processes"),
imageOutput(NS(id,"GOBP")),
linebreaks(70),
downloadButton(NS(id,'save_GOBP_plot'), 'Download plot as PNG')
),
tabPanel(strong("Gene Ontology (GO) - Cellular Components"),
imageOutput(NS(id,"GOCC")),
linebreaks(70),
downloadButton(NS(id,'save_GOCC_plot'), 'Download plot as PNG')
),
tabPanel(strong("Gene Ontology (GO) - Molecular Functions"),
imageOutput(NS(id,"GOMF")),
linebreaks(70),
downloadButton(NS(id,'save_GOMF_plot'), 'Download plot as PNG')
),
tabPanel(strong("Kyoto Encyclopedia of Genes and Genomes (KEGG) - pathway enrichment"),
imageOutput(NS(id,"KEGG")),
linebreaks(70),
downloadButton(NS(id,'save_KEGG_plot'), 'Download plot as PNG')
)
)
),
)
}
DEGsServer <- function(id) {
moduleServer(id, function(input, output, session) {
output$selected_pval <- renderText({
paste0("You have selected this adjusted p-value threshold: ", input$pval)
})
output$selected_FC <- renderText({
paste0("You have selected this FC threshold: ", input$FC)
})
observe({
shinyjs::toggleState("save_zip", condition = !is.null(input$zips))
})
output$save_zip <- downloadHandler(
filename = paste0("HumanBrainSexSingleCell_download.zip"),
contentType = "application/zip",
content = function(file){
plt_dir <- paste0("www/Plots/pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","))
plt_files <- list.dirs(plt_dir, full.names = T, recursive = T)
unfilt_DEGs_dir <- "data/Unfiltered_DEGs"
unfilt_files <- list.dirs(unfilt_DEGs_dir, full.names = T, recursive = T)
filt_DEGs_dir <- paste0("data/Filtered_DEGs/pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","))
filt_files <- list.dirs(filt_DEGs_dir, full.names = T, recursive = T)
files_ls <- list("plots"=plt_files,
"unfiltered DEGs"=unfilt_files,
"filtered DEGs"=filt_files)
files <- unlist(files_ls[names(files_ls) %in% input$zips], use.names = F)
#create the zip file
zip(file, files)
}
)
output$num_degs_plot <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Number_of_DEGs.png")))
list(src = filename, height = 1200, width = 750)
}, deleteFile = FALSE)
output$save_num_degs_plot <- downloadHandler(
filename = paste0("Number_of_DEGs_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Number_of_DEGs.png"))), file)
}
)
output$chr_fraction <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Chr_fractions.png")))
list(src = filename, height = 1200, width = 750)
}, deleteFile = FALSE)
output$save_chr_fraction_plot <- downloadHandler(
filename = paste0("Chr_fraction_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Chr_fractions.png"))), file)
}
)
output$GeneLocation <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Location.png")))
list(src = filename, height = 1200, width = 900)
}, deleteFile = FALSE)
output$save_GeneLocation_plot <- downloadHandler(
filename = paste0("Location_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Location.png"))), file)
}
)
output$mostdiffgenes <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/top_20_most_diff_genes.png")))
list(src = filename, height = 800, width = 1400)
}, deleteFile = FALSE)
output$save_mostdiffgenes_plot <- downloadHandler(
filename = paste0("Top_20_most_diff_genes_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/top_20_most_diff_genes.png"))), file)
}
)
output$MTgenes <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/MT_genes.png")))
list(src = filename, height = 800, width = 1400)
}, deleteFile = FALSE)
output$save_MTgenes_plot <- downloadHandler(
filename = paste0("MT_genes_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/MT_genes.png"))), file)
}
)
output$Xescapinggenes <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/X_escaping_genes.png")))
list(src = filename, height = 800, width = 1400)
}, deleteFile = FALSE)
output$save_Xescapinggenes_plot <- downloadHandler(
filename = paste0("X_escaping_genes_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/X_escaping_genes.png"))), file)
}
)
output$McKenzie <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/McKenzie_perc.png")))
list(src = filename, height = 800, width = 1400)
}, deleteFile = FALSE)
output$save_McKenzie_plot <- downloadHandler(
filename = paste0("McKenzie_percentages_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/McKenzie_perc.png"))), file)
}
)
output$Chlamydas <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Chlamydas_hmp.png")))
list(src = filename, height = 800, width = 1400)
}, deleteFile = FALSE)
output$save_Chlamydas_plot <- downloadHandler(
filename = paste0("Chlamydas_hmp_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Chlamydas_hmp.png"))), file)
}
)
output$Hormones <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Hormone_target_enrichment.png")))
list(src = filename, height = 1200, width = 800)
}, deleteFile = FALSE)
output$save_Hormones_plot <- downloadHandler(
filename = paste0("Hormone_target_enrichment_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/Hormone_target_enrichment.png"))), file)
}
)
output$ARE <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/ARE.png")))
list(src = filename, height = 800, width = 700)
}, deleteFile = FALSE)
output$save_ARE_plot <- downloadHandler(
filename = paste0("ARE_sites_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/ARE.png"))), file)
}
)
output$ERE <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/ERE.png")))
list(src = filename, height = 800, width = 700)
}, deleteFile = FALSE)
output$save_ERE_plot <- downloadHandler(
filename = paste0("ERE_sites_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/ERE.png"))), file)
}
)
output$GOBP <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/GO_BP.png")))
list(src = filename, height = 1600, width = 1400)
}, deleteFile = FALSE)
output$save_GOBP_plot <- downloadHandler(
filename = paste0("GO_BP_sites_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/GO_BP.png"))), file)
}
)
output$GOCC <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/GO_CC.png")))
list(src = filename, height = 1600, width = 1400)
}, deleteFile = FALSE)
output$save_GOCC_plot <- downloadHandler(
filename = paste0("GO_CC_sites_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/GO_CC.png"))), file)
}
)
output$GOMF <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/GO_MF.png")))
list(src = filename, height = 1600, width = 1400)
}, deleteFile = FALSE)
output$save_GOMF_plot <- downloadHandler(
filename = paste0("GO_MF_sites_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/GO_MF.png"))), file)
}
)
output$KEGG <- renderImage({
filename <- normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/KEGG.png")))
list(src = filename, height = 1600, width = 1400)
}, deleteFile = FALSE)
output$save_KEGG_plot <- downloadHandler(
filename = paste0("KEGG_sites_pval_", str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","), ".png"),
contentType = "image/png",
content = function(file) {
file.copy(normalizePath(file.path(paste0("www/Plots/pval_",
str_replace(input$pval, "\\.", ","), "_FC_", str_replace(input$FC, "\\.", ","),
"/KEGG.png"))), file)
}
)
})
}