-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
962 lines (868 loc) · 40 KB
/
app.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
##
## Interactive Visualization KRAS APMS
##
library(ComplexHeatmap)
library(InteractiveComplexHeatmap)
library(tidyverse)
library(ggwordcloud)
library(shiny)
library(shinydashboard)
library(shinyjs)
# necessary for upload to shinyapps.io
# library(BiocManager)
# options(repos = BiocManager::repositories())
# helper function for heatmap generation
source('functions.R')
# load data
load('data/data.Rdata')
# action function for InteractiveComplexHeatmap:
# needs to be defined here
click_action = function(df, output) {
output[["go_info_heatmap"]] = renderUI({
if(!is.null(df)) {
df <- as_tibble(df)
# distinguish between click on similarity heatmap and click on data heatmaps
if (df$heatmap == 'Similarity') {
go_id1 = df$row_label
go_id2 = df$column_label
HTML(str_glue(
"<pre>
## Row GO ID
<a href='http://amigo.geneontology.org/amigo/term/{go_id1}' target='_blank'>{go_id1}</a>: {get_go_term(go_id1, df_annotation)} <button id='{go_id1}' class='go_sel_button'>Select</button>
## Column GO ID:
<a href='http://amigo.geneontology.org/amigo/term/{go_id2}' target='_blank'>{go_id2}</a>: {get_go_term(go_id2, df_annotation)} <button id='{go_id2}' class='go_sel_button'>Select</button>
</pre>"
))
} else {
go_id1 = df$row_label
HTML(str_glue(
"<pre>
## Row GO ID
<a href='http://amigo.geneontology.org/amigo/term/{go_id1}' target='_blank'>{go_id1}</a>: {get_go_term(go_id1, df_annotation)} <button id='{go_id1}' class='go_sel_button'>Select</button>
</pre>"
))
}
}
})
}
brush_action = function(df, output) {
output[["go_info_heatmap"]] = renderUI({
if(!is.null(df)) {
df <- as_tibble(df)
# extract GO IDs from brushed area
go_id <- c(df %>% filter(heatmap == 'Similarity') %>%
select(row_label, column_label) %>%
as.list %>%
flatten %>%
flatten_chr,
df %>% filter(heatmap != 'Similarity') %>%
select(row_label) %>%
as.list %>%
flatten %>%
flatten_chr)
go_text = str_glue("<a href='http://amigo.geneontology.org/amigo/term/{go_id}' target='_blank'>{go_id}</a>: {get_go_term(go_id, df_annotation)} <button id='{go_id}' class='go_sel_button'>Select</button>") %>%
str_c(collapse='\n')
HTML(str_glue("<pre>{go_text}</pre>"))
}
})
}
# color schemes
conditions <- c('unstim', 'dmog', 'egf', 'il6', 'pge2', 'tnfa')
conditions_hq <- c('unstim.', 'DMOG', 'EGF', 'IL-6', 'PGE2', 'TNF\u03B1')
conditions_colors <- c("#001524","#12616D","#75964A","#A1869E","#FF7D00","#78290F")
mut_stats <- c('wt', 'g12c', 'g12d', 'g12v')
mut_stats_hq <- c('WT', 'G12C', 'G12D', 'G12V')
mut_stats_colors <- c('#A98743', '#437C90', '#255957', '#8F2844')
# get choices for inputs
choices_clusters <- tibble(cluster = clusters) %>%
count(cluster, name = 'count') %>%
arrange(-count) %>%
filter(count >= 20) %>%
pull(cluster)
choices_mut_status <- df_apms %>%
pull(mut_status) %>%
unique %>%
set_names(nm = str_to_upper(.))
choices_condition <- df_apms %>%
pull(condition) %>%
unique %>%
set_names(nm = case_when(. != 'unstim' ~ str_to_upper(.),
T ~ str_to_title(.)))
choices_cond_conc <- df_apms %>%
select(concentration, condition) %>%
distinct %>%
mutate(choice = str_c(condition, concentration, sep='_')) %>%
pull(choice) %>%
set_names(nm = case_when(. != 'unstim_none' ~ str_to_upper(.),
T ~ 'Unstim'))
choices_anova_factors <- df_anova %>%
group_by(term) %>%
summarise(n_components = str_count(unique(term), ':')) %>%
ungroup %>%
arrange(n_components, term) %>%
pull(term)
# set default settings for initializing
default_seltype <- 'process'
default_id <- 'GO:1903578'
default_mut_status <- choices_mut_status
default_cond_conc <- choices_cond_conc
default_anova_factors <- c('condition', 'mut_status', 'concentration')
default_anova_pval <- 0.05
default_gsea_pval <- 0.05
ui <- dashboardPage(
dashboardHeader(
title = 'KRAS APMS Visualization',
dropdownMenu(type = 'notifications', headerText = 'Further links',
icon = icon('info'), badgeStatus = NULL, # TODO add link to article
notificationItem(text = 'Source code', icon = icon('github'), href = 'https://github.com/PhilippJunk/kras_apms_vis'),
notificationItem(text = 'Contact Scientific', icon = icon('envelope'), href = 'mailto:[email protected]'),
notificationItem(text = 'Contact Technical', icon = icon('envelope'), href = 'mailto:[email protected]?subject=Shiny App KRAS APMS'))),
dashboardSidebar(
sidebarMenu(
menuItem(text = 'Overview',
tabName = 'overview',
icon = NULL,
menuSubItem(text = 'Heatmap',
tabName = 'overview-heatmap',
icon = NULL),
menuSubItem(text = 'Semantic Clusters',
tabName = 'overview-clusters',
icon = NULL)
),
menuItem(text = 'Invidivial GO Term',
tabName = 'specific',
icon = NULL,
menuSubItem(text = 'Summary',
tabName = 'specific-summary',
icon = NULL),
menuSubItem(text = 'Summed Intensities',
tabName = 'specific-sum',
icon = NULL),
menuSubItem(text = 'Individual Intensities',
tabName = 'specfic-indiv',
icon = NULL)
),
menuItem(text = 'Settings',
tabName = 'settings',
icon = icon('gear', verify_fa = FALSE)),
menuItem(text = 'Help',
tabName = 'help',
icon = icon('question')),
hr(),
h4('Ontology Control Panel', style = 'text-align:center'),
# Radio buttons on how to select GO terms
radioButtons(inputId = 'seltype', label = 'Selection Type',
choiceValues = c('id', 'process'),
choiceNames = c('ID', 'Process'),
selected = default_seltype, inline = TRUE),
# Input for selection of GO terms
selectInput(inputId = 'id', label = 'ID/Process',
choices = default_id, selected = default_id,
multiple = FALSE),
# History of selected GO terms
actionButton('id_history', 'View History', icon = icon('history')),
# Random selection of GO term
actionButton('id_random', 'Random GO term', icon = icon('dice')),
hr(),
h4('Data Control Panel', style = 'text-align:center'),
# Input for selection of mutation status
selectInput(inputId = 'mut_status', label = 'Selection Mutation Status',
choices = choices_mut_status,
selected = default_mut_status, multiple = TRUE),
# Input for selection of condition/concentration
selectInput(inputId = 'cond_conc', label = 'Select Condition/Concentration',
choices = choices_cond_conc, selected = default_cond_conc,
multiple = TRUE)
)
),
dashboardBody(
useShinyjs(),
tags$head(tags$link(rel = 'stylesheet', type = 'text/css', href = 'custom_css.css'),
tags$script(src = "custom_button.js")),
tabItems(
tabItem(tabName = 'overview-heatmap',
h2('Overview Semantic Distance Heatmap'),
fluidRow(box(width = 12, title = 'Heatmap Controls',
collapsible = TRUE, collapsed = TRUE,
selectizeInput(inputId = 'ht_ref_anova_mut',
label = 'Reference group: ANOVA mutation status',
choices = choices_mut_status, selected = NULL,
options = list(maxItems = 1,
onInitialize = I('function() { this.setValue(""); }'))),
selectizeInput(inputId = 'ht_ref_anova_cond',
label = 'Reference group: ANOVA condition',
choices = choices_condition, selected = NULL,
options = list(maxItems = 1,
onInitialize = I('function() { this.setValue(""); }'))),
selectizeInput(inputId = 'ht_ref_gsea_mut',
label = 'Reference group: GSEA mutation status',
choices = choices_mut_status, selected = NULL,
options = list(maxItems = 1,
onInitialize = I('function() { this.setValue(""); }'))),
selectizeInput(inputId = 'ht_ref_gsea_cond',
label = 'Reference group: GSEA condition/concentration',
choices = choices_cond_conc,
selected = 'unstim_none',
options = list(maxItems = 1)),
hr(),
radioButtons(inputId = 'ht_reduce',
label = 'Only show GO terms that are signficantly different in the data shown in the heatmap?',
choiceValues = c(TRUE, FALSE), choiceNames = c('Yes', 'No'),
selected = TRUE, inline = TRUE),
radioButtons(inputId = 'ht_clusters', label = 'Show all clusters?',
choiceValues = c(TRUE, FALSE), choiceNames = c('Yes', 'No'),
selected = TRUE, inline = TRUE),
conditionalPanel(condition = 'input.ht_clusters == "FALSE"',
selectInput(inputId = 'ht_cluster_sel',
label = 'Select clusters to show',
choices = choices_clusters,
selected = 1, multiple = T)),
fluidRow(column(2 ,actionButton(inputId = 'ht_apply', label = 'Render heatmap')),
column(10, hidden(span(style = 'color:red', id = 'error_ht',
'Rendering the heatmap with the current settings not possible. Please choose other settings.')))))),
fluidRow(box(originalHeatmapOutput("ht", title = NULL, width = '1000px'),
id = 'orig_ht', width = 12, title = "Full heatmap")),
fluidRow(box(subHeatmapOutput("ht", title = NULL),
id = 'sub_ht', width = 12, title = "Sub-heatmap")),
shinyjs::hidden(fluidRow(title = 'OutputPanel', HeatmapInfoOutput('ht', title=NULL))),
fluidRow(box(htmlOutput("go_info_heatmap"),
title = 'GO terms in selected area', width = 12,
collapsible = T))),
tabItem(tabName = 'overview-clusters',
h2('Overview GO Semantic Clusters'),
uiOutput('cluster_wc_tabs'),
fluidRow(box(htmlOutput('go_info_clusters'),
title = 'GO terms in selected cluster', width = 12,
collapsible = T))),
tabItem(tabName = 'specific-summary',
h2('Summary Selected GO term'),
fluidRow(box(htmlOutput('ontology_info'),
title = 'Process Information', width = 12)),
fluidRow(box(plotOutput('plot_proteins_overview'),
downloadButton('dl_png_proteins_overview', label = 'Download PNG'),
downloadButton('dl_csv_proteins_overview', label = 'Download CSV'),
title = 'Samples per protein', width = 12)),
fluidRow(box(plotOutput('plot_goprocess_info'),
downloadButton('dl_png_goprocess_info', label = 'Download PNG'),
downloadButton('dl_csv_goprocess_info', label = 'Download CSV'),
title = 'Number of identified proteins', width = 12))),
tabItem(tabName = 'specific-sum',
h2('Summed LFQ intensities'),
fluidRow(box(plotOutput('plot_lfqsum'),
downloadButton('dl_png_lfqsum', label = 'Download PNG'),
downloadButton('dl_csv_lfqsum', label = 'Download CSV'),
title = 'Summed LFQ intensities', width = 12)),
fluidRow(box(
title = 'Differential Analysis: ANOVA', width = 12, collapsible = T,
sidebarLayout(mainPanel(dataTableOutput('table_anova')),
sidebarPanel(sliderInput(inputId = 'anova_pval',
label = 'Set cutoff for adjusted p-value',
min = 0.01, max = 0.1,
value = default_anova_pval),
selectInput(inputId = 'anova_factors',
label = 'Select terms to display in table',
choices = choices_anova_factors,
selected = default_anova_factors,
multiple = TRUE),
downloadButton('dl_csv_anova', label = 'Download CSV'))))),
fluidRow(box(
title = 'Differential Analysis: GSEA', width = 12, collapsible = T,
sidebarLayout(mainPanel(dataTableOutput('table_gsea')),
sidebarPanel(sliderInput(inputId = 'gsea_pval',
label = 'Set cutoff for adjusted p-value',
min = 0.01, max = 0.1,
value = default_gsea_pval),
radioButtons(inputId = 'gsea_comparible_contrasts',
label = 'Show only comparible (either comparible on mutation status, or comparible on condition/concentration) contrasts?',
choiceValues = c(TRUE, FALSE),
choiceNames = c('Yes', 'No'),
selected = TRUE, inline = TRUE),
downloadButton('dl_csv_gsea', label = 'Download CSV')))))),
tabItem(tabName = 'specfic-indiv',
h2('Individual Proteins LFQ intensities'),
fluidRow(box(
sidebarLayout(mainPanel(plotOutput('plot_proteins')),
sidebarPanel(selectizeInput(inputId = 'indiv_proteins',
label = 'Select proteins to plot',
choices = character(),
options = list(maxItems = 10)))),
downloadButton('dl_png_proteins', label = 'Download PNG'),
downloadButton('dl_csv_proteins', label = 'Download CSV'),
width = 12))),
tabItem(tabName = 'settings',
h2('Settings'),
fluidRow(column(width = 6,
box(numericInput('settings_plot_height', 'Set height of plot downloads (in mm)',
value = 150, max = 300),
numericInput('settings_plot_width', 'Set width of plot downloads (in mm)',
value = 500, max = 1000),
numericInput('settings_plot_dpi', 'Set resolution of plot downloads (DPI)',
value = 100, max = 300),
width = NULL, title = 'Download settings')),
column(width = 6,
box(numericInput('settings_n_history', 'Set number of elements shown',
value = 20, min=1),
width = NULL, title = 'History Settings'),
box(numericInput('settings_n_proteins', 'Set number of proteins shown',
value = 50, min=1),
width = NULL, title = 'Settings "Samples per Proteins"')))),
tabItem(tabName = 'help',
h2('Help'),
fluidRow(box(includeMarkdown('www/helppage/help_context.md'),
title = 'Context', width = 6),
box(img(src = 'helppage/help_context.png', style = 'width:100%'),
title = 'Overview App', width = 6)),
fluidRow(box(includeMarkdown('www/helppage/help_sections.md'),
title = 'Sections', width = 12)),
fluidRow(box(includeMarkdown('www/helppage/help_interactive.md'),
title = 'Interactivity', width = 12)),
fluidRow(box(includeMarkdown('www/helppage/help_outputs.md'),
title = 'Outputs', width = 12)))
)
)
)
# Server logic
server <- function(input, output, session) {
##################################################################
## REACTIVE VALUES: general data frames
# reactive subset of df_apms
dfr_apms <- reactive({
# return data frame derived from df_apms filtered by
# - selected GO process
# - selected mutations
# - selected combinations of conditions/concentrations
validate(need(input$id, 'Please select an ontology term.'),
need(input$mut_status, 'Please select at least one mutation status'),
need(input$cond_conc, 'Please select at least one condition'))
df_ontology %>%
filter(id == input$id) %>%
select(hgnc) %>%
left_join(df_apms, by = 'hgnc') %>%
filter(mut_status %in% input$mut_status) %>%
filter(str_glue('{condition}_{concentration}') %in% input$cond_conc)
})
# reactive subset of df_sum
dfr_sum <- reactive({
# return data frame derived from df_sum filtered by
# - selected GO process
# - selected mutations
# - selected combinations of conditions/concentrations
validate(need(input$id, 'Please select an ontology term.'),
need(input$mut_status, 'Please select at least one mutation status'),
need(input$cond_conc, 'Please select at least one condition'))
df_sum %>%
filter(id == input$id) %>%
filter(mut_status %in% input$mut_status) %>%
filter(str_glue('{condition}_{concentration}') %in% input$cond_conc)
})
# reactive subset of df_anova
dfr_anova <- reactive({
# returns data frame derived from df_anova filtered by
# - selected GO process
# - selected mutations
# - selected combinations of conditions/concentrations
# - selected p_value
# - selected interactions
validate(need(input$id, 'Please select an ontology term.'),
need(input$mut_status, 'Please select at least one mutation status'),
need(input$cond_conc, 'Please select at least one condition'))
# get selected samples
selected_samples <- expand_grid(mut_status = input$mut_status,
cond_conc = input$cond_conc) %>%
mutate(condition = str_extract(cond_conc, '^[:alnum:]+(?=_)'),
concentration = str_extract(cond_conc, '(?<=_)[:alnum:]+$')) %>%
select(-cond_conc)
# perform filtering
df <- df_anova %>%
filter(id == input$id) %>%
filter(p_adj <= input$anova_pval) %>%
filter(term %in% input$anova_factors) %>%
arrange(factor(term, levels = input$anova_factors)) %>%
group_by(term) %>%
arrange(p_adj, .by_group = T) %>%
ungroup %>%
select(term, group_higher, group_lower, estimate, p_adj)
if (nrow(df) > 0) {
df <- df %>%
group_by(term) %>%
nest %>%
# filtering by selected samples
mutate(data = map2(term, data, function(t, d) {
# based on term, construct filter
if (t == 'mut_status') {selected <- selected_samples$mut_status}
if (t == 'condition') {selected <- selected_samples$condition}
if (t == 'concentration') {selected <- selected_samples$concentration}
if (t == 'mut_status:condition') {selected <- str_glue('{selected_samples$mut_status}:{selected_samples$condition}')}
if (t == 'mut_status:concentration') {selected <-str_glue('{selected_samples$mut_status}:{selected_samples$concentration}')}
if (t == 'condition:concentration') {selected <- str_glue('{selected_samples$condition}:{selected_samples$concentration}')}
if (t == 'mut_status:condition:concentration') {selected <- str_glue('{selected_samples$mut_status}:{selected_samples$condition}:{selected_samples$concentration}')}
d %>%
filter(group_higher %in% selected & group_lower %in% selected)
})) %>%
unnest(cols = data) %>%
ungroup
}
df
})
# reactive subset of df_gsea
dfr_gsea <- reactive({
# return data frame derived from df_gsea filtered by
# - selected GO process
# - selected mutations
# - selected combinations of conditions/concentrations
# - selected p_value
# - if setting activated, only show contrasts where only one factor changes
validate(need(input$id, 'Please select an ontology term.'),
need(input$mut_status, 'Please select at least one mutation status'),
need(input$cond_conc, 'Please select at least one condition'))
selected_samples <- expand_grid(a = input$mut_status, b = input$cond_conc) %>%
mutate(selected_samples = str_glue('{a}_{b}')) %>%
pull(selected_samples)
df <- df_gsea %>%
filter(id == input$id) %>%
filter(enriched_in %in% selected_samples & enriched_against %in% selected_samples) %>%
filter(p_adj <= input$gsea_pval) %>%
group_by(enriched_in) %>%
arrange(desc(NES), .by_group = T) %>%
ungroup %>%
select(id, enriched_in, enriched_against, NES, p_adj)
if (as.logical(input$gsea_comparible_contrasts)) {
df <- df %>%
filter(
(str_extract(enriched_in, '[:alnum:]+(?=_)') == str_extract(enriched_against, '[:alnum:]+(?=_)')) |
(str_extract(enriched_in, '(?<=_)[:alnum:]+_[:alnum:]+') == str_extract(enriched_against, '(?<=_)[:alnum:]+_[:alnum:]+'))
)}
df
})
##################################################################
## REACTIVE VALUES: data frames for plots
dfr_plot_proteins_overview <- reactive({
n_samples <- df_apms %>% pull(label) %>% unique %>% length
dfr_apms() %>%
group_by(hgnc) %>%
summarise(count = n(),
perc = 100 * n()/n_samples) %>%
ungroup %>%
arrange(desc(perc)) %>%
mutate(hgnc = factor(hgnc, levels = hgnc))
})
dfr_plot_goprocess_info <- reactive({
n_term <- df_annotation %>% filter(id == input$id) %>% pull(n_all) %>% head(1)
dfr_apms() %>%
select(-label, -LFQ) %>%
distinct %>%
group_by(group, mut_status, condition, concentration) %>%
summarise(perc = 100 * n()/n_term,
count = n()) %>%
ungroup %>%
arrange(-perc) %>%
select(group, condition, count, perc) %>%
mutate(group = factor(group, levels = unique(group)))
})
dfr_plot_proteins <- reactive({
validate(need(input$indiv_proteins, 'Please select at least one protein to plot.'))
dfr_apms() %>%
filter(hgnc %in% input$indiv_proteins) %>%
mutate(hgnc = factor(hgnc, levels = input$indiv_proteins)) %>%
mutate(mut_status = str_to_upper(mut_status))
})
##################################################################
## REACTIVE VALUES: plots
# heatmap
ht <- reactive({
make_ht(dist_mat, clusters, df_gsea, df_anova, ht_settings())
})
# construct plot for overview over individual proteins
reac_plot_proteins_overview <- reactive({
validate(need(input$settings_n_proteins, 'Please provide the number of proteins to show in Settings.'))
df <- dfr_plot_proteins_overview()
n_total <- df$hgnc %>% unique %>% length
df <- df %>%
slice_max(perc, n=settings_n_proteins())
n_here <- min(settings_n_proteins(), n_total)
ggplot(df, aes(x = hgnc, y = perc)) +
geom_bar(stat = 'identity', color = 'black', fill = 'gray') +
scale_x_discrete(guide = guide_axis(angle = 45)) +
labs(x = 'HGNC', y = 'Percentage of samples',
caption = str_glue('Showing {n_here} of {n_total} proteins.')) +
theme_minimal() +
theme(plot.background = element_rect(fill = 'white', color = 'white')) +
NULL
})
# construct plot for information on GO process
reac_plot_goprocess_info <- reactive({
dfr_plot_goprocess_info() %>%
ggplot(aes(x = group, y = perc, fill = condition)) +
geom_bar(stat = 'identity', position = 'dodge', color = 'black', alpha=0.7) +
scale_fill_manual(values = conditions_colors,
breaks = conditions,
labels = conditions_hq) +
scale_x_discrete(guide = guide_axis(angle = 45)) +
labs(x = 'Group', y = 'Percentage of identified\nproteins in GO term',
fill = 'Condition') +
theme_minimal() +
theme(plot.background = element_rect(fill = 'white', color = 'white')) +
NULL
})
# construct plot of sum of LFQ intensities
reac_plot_lfqsum <- reactive({
dfr_sum() %>%
# mutate(mut_status = str_to_upper(mut_status)) %>%
mutate(condition = case_when(condition == 'unstim' ~ condition,
T ~ str_to_upper(condition))) %>%
ggplot(aes(x = mut_status, y = sum_LFQ, fill=mut_status)) +
geom_boxplot(color = 'black', alpha=0.8) +
geom_point(size = 2, position = position_jitter(height=0, width=0.2)) +
facet_grid(cols = vars(condition, concentration)) +
scale_x_discrete(guide = guide_axis(angle = 45),
breaks = mut_stats,
labels = mut_stats_hq) +
scale_fill_manual(values = mut_stats_colors,
breaks = mut_stats,
labels = mut_stats_hq) +
labs(x = 'Mutation Status', y = 'log2(Sum(LFQ))', fill = 'Mutation Status') +
theme_bw(base_size = 15) +
NULL
})
# construct plot for individual proteins
reac_plot_proteins <- reactive({
plot <- dfr_plot_proteins() %>%
ggplot(aes(x = str_glue('{condition}_{concentration}'),
y = log2(LFQ), fill = condition)) +
geom_boxplot(color = 'black', alpha=0.8) +
geom_point(position = position_jitter(height=0, width=0.2)) +
scale_x_discrete(guide = guide_axis(angle = 45)) +
scale_fill_manual(values = conditions_colors,
breaks = conditions,
labels = conditions_hq) +
labs(x = 'Condition/Concentration', y = 'log2(LFQ)', fill = 'Condition') +
theme_bw() +
NULL
# adjust faceting depending on number of proteins to show
if (length(input$indiv_proteins) > 1 & length(input$mut_status) > 1) {
plot <- plot + facet_grid(hgnc ~ mut_status, scales = 'free')
} else if (length(input$mut_status) > 1) {
plot <- plot + facet_grid(. ~ mut_status, scales = 'free')
} else if(length(input$indiv_proteins) > 1) {
plot <- plot + facet_grid(hgnc ~ .)
}
plot
})
##################################################################
## REACTIVE VALUES: others
# history of selected ids
id_history <- reactiveVal(value = NULL)
# choices for id selection
choices_id <- reactive({
validate(need(input$mut_status, 'Please select at least one mutation status'),
need(input$cond_conc, 'Please select at least one condition'))
df_sum %>%
filter(mut_status %in% input$mut_status) %>%
filter(str_glue('{condition}_{concentration}') %in% input$cond_conc) %>%
pull(id) %>%
unique
})
# manually validate settings because numericInput does not
settings_plot_height <- reactive({
max_value <- 300
min(max_value, input$settings_plot_height)
})
settings_plot_width <- reactive({
max_value <- 1000
min(max_value, input$settings_plot_width)
})
settings_plot_dpi <- reactive({
max_value <- 300
min(max_value, input$settings_plot_dpi)
})
settings_n_history <- reactive({
min_value <- 1
max(min_value, input$settings_n_history)
})
settings_n_proteins <- reactive({
min_value <- 1
max(min_value, input$settings_n_proteins)
})
##################################################################
## RENDERED ELEMENTS
# render cluster word clouds in tab overview
output$cluster_wc_tabs <- renderUI({
tabs <- tibble(cluster = clusters) %>%
count(cluster, name = 'count') %>%
filter(count >= 20) %>%
arrange(-count) %>%
pull(cluster) %>%
map(function(cl) {
n_terms <- sum(clusters == cl)
p_wordcloud <- df_wordcloud %>%
filter(cluster == cl) %>%
slice_min(padj, n=20) %>%
ggplot(aes(label = keyword, size = -log10(padj), color=keyword)) +
geom_text_wordcloud() +
scale_size_area(max_size = 30) +
labs(caption = str_glue('{n_terms} GO terms in cluster {cl}')) +
theme_minimal() + NULL
tab_title = str_glue('Cluster {cl}')
tabPanel(tab_title, value = cl,
renderPlot({p_wordcloud}))
})
do.call(tabBox, c(tabs, list(width = 12, title = 'Cluster Wordclouds', id='cluster_wc')))
})
# render all go terms in currently selected cluster
output$go_info_clusters <- renderUI({
# get currently active tab and extract GO terms
go_id <- rownames(dist_mat)[clusters == as.numeric(input$cluster_wc)]
if(length(go_id > 0)) {
go_text = str_glue("<a href='http://amigo.geneontology.org/amigo/term/{go_id}' target='_blank'>{go_id}</a>: {get_go_term(go_id, df_annotation)} <button id='{go_id}' class='go_sel_button'>Select</button>") %>%
str_c(collapse='\n')
HTML(str_glue("<pre>{go_text}</pre>"))
}
})
# render information about ontology term currently displayed
output$ontology_info <- renderUI({
# get related GO terms
relatives <- tibble(dist = dist_mat[rownames(dist_mat) == input$id,],
id = colnames(dist_mat)) %>%
arrange(-dist) %>%
filter(id != input$id) %>% head(5) %>% pull(id)
relatives_text = str_glue("<a href='http://amigo.geneontology.org/amigo/term/{relatives}' target='_blank'>{relatives}</a>: {get_go_term(relatives, df_annotation)} <button id='{relatives}' class='go_sel_button'>Select</button>") %>%
str_c(collapse='\n')
col2 <- HTML(
h5('Closest relatives in data set') %>% as.character,
str_glue("<pre>{relatives_text}</pre>"))
# get information
annotation <- df_annotation %>%
filter(id == input$id) %>% head(1)
col1 <- HTML(
h4(annotation$id) %>% as.character,
h4(annotation$process) %>% as.character,
p(annotation$definition) %>% as.character,
hr() %>% as.character,
p('Size whole set: ', annotation$n_all) %>% as.character,
p('Number found in APMS: ', annotation$n_found) %>% as.character)
# assemble UI
fluidRow(column(6, col1),
column(6, col2))
})
# render plot for information on GO process
output$plot_goprocess_info <- renderPlot({
reac_plot_goprocess_info()
})
# render plot of sum of LFQ intensities
output$plot_lfqsum <- renderPlot({
reac_plot_lfqsum()
})
# render plot for overview over individual proteins
output$plot_proteins_overview <- renderPlot({
reac_plot_proteins_overview()
})
# render plot for individual proteins
output$plot_proteins <- renderPlot({
reac_plot_proteins()
})
# render ANOVA table
output$table_anova <- renderDataTable({
dfr_anova() %>%
mutate(estimate = round(estimate, 2)) %>%
mutate(p_adj = scales::scientific(p_adj)) %>%
rename('Adj. P-value' = p_adj)
},
options = list(
paging = FALSE
)
)
# render GSEA table
output$table_gsea <- renderDataTable({
dfr_gsea() %>%
mutate(p_adj = scales::scientific(p_adj)) %>%
mutate(NES = round(NES, 2)) %>%
rename('Adj. P-value' = p_adj)
},
options = list(
paging = FALSE
))
##################################################################
## DOWNLOAD HANDLERS
# for goprocess_info
output$dl_png_goprocess_info <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_proteinsPerCondition.png'),
content = function(con) {ggsave(con, reac_plot_goprocess_info(), device = 'png',
height = settings_plot_height(),
width = settings_plot_width(),
dpi = settings_plot_dpi(),
units = 'mm', limitsize = F)},
contentType = 'image/png'
)
output$dl_csv_goprocess_info <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_proteinsPerCondition.csv'),
content = function(con) {write.csv(dfr_plot_goprocess_info(), con)},
contentType = 'text/csv'
)
# for lfqsum
output$dl_png_lfqsum <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_sumLFQ.png'),
content = function(con) {ggsave(con, reac_plot_lfqsum(), device = 'png',
height = settings_plot_height(),
width = settings_plot_width(),
dpi = settings_plot_dpi(),
units = 'mm', limitsize = F)},
contentType = 'image/png'
)
output$dl_csv_lfqsum <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_sumLFQ.csv'),
content = function(con) {write.csv(dfr_sum(), con)},
contentType = 'text/csv'
)
# for proteins_overview
output$dl_png_proteins_overview <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_conditionsPerProtein.png'),
content = function(con) {ggsave(con, reac_plot_proteins_overview(), device = 'png',
height = settings_plot_height(),
width = settings_plot_width(),
dpi = settings_plot_dpi(),
units = 'mm', limitsize = F)},
contentType = 'image/png'
)
output$dl_csv_proteins_overview <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_conditionsPerProtein.csv'),
content = function(con) {write.csv(dfr_plot_proteins_overview(), con)},
contentType = 'text/csv'
)
# for proteins
output$dl_png_proteins <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_indivProteins.png'),
content = function(con) {ggsave(con, reac_plot_proteins(), device = 'png',
height = settings_plot_height(),
width = settings_plot_width(),
dpi = settings_plot_dpi(),
units = 'mm', limitsize = F)},
contentType = 'image/png'
)
output$dl_csv_proteins <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_indivProteins.csv'),
content = function(con) {write.csv(dfr_plot_proteins(), con)},
contentType = 'text/csv'
)
# for anova
output$dl_csv_anova <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_anova.csv'),
content = function(con) {write.csv(dfr_anova(), con)},
contentType = 'text/csv'
)
# for gsea
output$dl_csv_gsea <- downloadHandler(
filename = str_glue('{str_replace(input$id, ":", "")}_gsea.csv'),
content = function(con) {write.csv(dfr_gsea(), con)},
contentType = 'text/csv'
)
##################################################################
## OBSERVERS
# add trigger to extract settings for plotting the heatmap
# tied to ht_apply action button
ht_settings <- bindEvent({
reactive({
list(ref_anova_mut = if(input$ht_ref_anova_mut != '') {input$ht_ref_anova_mut} else {NULL},
ref_anova_cond = if(input$ht_ref_anova_cond != '') {input$ht_ref_anova_cond} else {NULL},
ref_gsea_mut = if(input$ht_ref_gsea_mut != '') {input$ht_ref_gsea_mut} else {NULL},
ref_gsea_cond = if(input$ht_ref_gsea_cond != '') {input$ht_ref_gsea_cond} else {NULL},
reduce = as.logical(input$ht_reduce),
all_clusters = as.logical(input$ht_clusters),
selected_clusters = as.numeric(input$ht_cluster_sel))
})
},
input$ht_apply,
ignoreNULL = FALSE)
# catch settings where heatmap returns empty and then does not update
observe({
if (!is.null(ht())) {
shinyjs::show('orig_ht')
shinyjs::show('sub_ht')
shinyjs::hide('error_ht')
makeInteractiveComplexHeatmap(
input, output, session, ht(), "ht",
click_action = click_action, brush_action = brush_action)
}
else {
shinyjs::hide('orig_ht')
shinyjs::hide('sub_ht')
shinyjs::show('error_ht')
}
})
# observer for updating history of selected ids
bindEvent({
observe({
id_history(c(input$id, id_history()))
})
},
input$id)
# add observers to selection of processes
bindEvent({
observe({
choices <- choices_id()
current <- input$id
# add names depending on which seltype has been selected
if (input$seltype == 'process') {
choices_names <- left_join(
tibble(id = choices),
df_annotation,
by = 'id') %>%
mutate(name = str_glue('{id} {process}')) %>%
pull(name)
names(choices) <- choices_names
}
# check if current selection still in choices
if (current %in% choices) {
selected <- current
} else {
selected <- choices[1]
}
updateSelectInput(inputId = 'id', choices = choices, selected = selected)
})},
input$seltype,
choices_id(),
ignoreNULL = FALSE
)
# add trigger to id history
bindEvent({
observe({
showModal(modalDialog(
p(str_glue('Showing the last {settings_n_history()} selected GO IDs.')),
{
go_id <- id_history() %>% head(settings_n_history())
if(length(go_id > 0)) {
go_text = str_glue("<a href='http://amigo.geneontology.org/amigo/term/{go_id}' target='_blank'>{go_id}</a>: {get_go_term(go_id, df_annotation)} <button id='{go_id}' class='go_sel_button'>Select</button>") %>%
str_c(collapse='\n')
HTML(str_glue("<pre>{go_text}</pre>"))
}},
title = 'GO ID selection history',
easyClose = TRUE,
fade = TRUE,
size = 'l'))
})
},
input$id_history)
# add trigger to random id selection
bindEvent({
observe({
updateSelectInput(inputId = 'id', selected = sample(choices_id(), 1))
})
},
input$id_random)
# add observation to selection of individual proteins to visualize
# Updated based on which GO process is selected
observe({
df_proteins <- dfr_apms() %>%
group_by(hgnc) %>%
summarise(count = n()) %>%
ungroup %>%
arrange(desc(count))
updateSelectizeInput(
inputId = 'indiv_proteins',
choices = df_proteins$hgnc,
selected = df_proteins$hgnc[1],
server = TRUE
)
})
}
# Run the application
shinyApp(ui = ui, server = server)