-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.R
executable file
·2426 lines (1832 loc) · 116 KB
/
server.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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# R server code interacting with the main webpage
library(shiny)
library(data.table)
# Default files, tables for contribution calculations, a file of shared constants, and the html input elements we interact with
default_taxonomic_hierarchy_table_filename = "www/Data/97_otu_taxonomy_split_with_header.txt"
default_function_hierarchy_table_filename = "www/Data/brite_hierarchy.tab"
default_metadata_table_filename = "www/Data/mice_samplemap.txt"
default_contribution_table_filename = "www/Data/mice_metagenome_contributions_K01516_removed.txt"
default_otu_table_filename = "www/Data/otu_table_even_2.txt"
picrust_normalization_table_filename = "www/Data/16S_13_5_precalculated.tab.gz"
picrust_ko_table_directory = "www/Data/individual_picrust_otu_tables/"
picrust_ko_table_suffix = "_genomic_content.tab"
picrust_nsti_table_filename = "www/Data/otu_nsti_values.tab"
constants_filename = "www/Javascript/constants.js"
html_elements = c("taxonomic_abundance_table", "genomic_content_table", "contribution_table", "function_abundance_table", "custom_taxonomic_hierarchy_table", "custom_function_hierarchy_table", "metadata_table")
log_filename = "www/Data/app.log"
# Read the shared constants table
constants_table = fread(constants_filename, header=F)
unlinked_name = unname(unlist(constants_table[unlist(constants_table[,2,with=F]) == "unlinked_name",4,with=F]))
default_taxonomic_summary_level = unname(unlist(constants_table[unlist(constants_table[,2,with=F]) == "default_taxonomic_summary_level",4,with=F]))
default_function_summary_level = unname(unlist(constants_table[unlist(constants_table[,2,with=F]) == "default_function_summary_level",4,with=F]))
default_metadata_factor = unname(unlist(constants_table[unlist(constants_table[,2,with=F]) == "default_metadata_factor",4,with=F]))
options(shiny.maxRequestSize = as.numeric(unlist(constants_table[unlist(constants_table[,2,with=F]) == "max_upload_size",4,with=F])))
options(stringsAsFactors = F)
# Tables shared between sessions
default_taxonomic_hierarchy_table = fread(default_taxonomic_hierarchy_table_filename, header=T, showProgress=F)
default_function_hierarchy_table = fread(default_function_hierarchy_table_filename, header=T, showProgress=F)
default_metadata_table = fread(default_metadata_table_filename, header=T, showProgress=F)
# Constant to mark entries for comparison in the contribution table
comparison_tag = "_comparison"
# Filtering constants
relative_abundance_cutoff = 0.005
### Shiny server session code ###
shinyServer(function(input, output, session) {
# Start pings to maintain contact between the browser session and the server
session$sendCustomMessage("maintain_connection", 0)
### Session variables tracked on the server ###
# Map of previous file paths to uploaded files, used to detect when a new file has been selected but has not finished uploading
old_file_paths = reactiveValues()
old_file_paths[["taxonomic_abundance_table"]] = NULL
old_file_paths[["genomic_content_table"]] = NULL
old_file_paths[["contribution_table"]] = NULL
old_file_paths[["custom_taxonomic_hierarhcy_table"]] = NULL
old_file_paths[["custom_function_hierarchy_table"]] = NULL
old_file_paths[["metadata_table"]] = NULL
old_file_paths[["function_abundance_table"]] = NULL
# Map of flags indicating that a new file has been selected, used to detect when a new file has been selected but has not finished uploading
new_file_flags = reactiveValues()
new_file_flags[["taxonomic_abundance_table"]] = FALSE
new_file_flags[["genomic_content_table"]] = FALSE
new_file_flags[["contribution_table"]] = FALSE
new_file_flags[["custom_taxonomic_hierarchy_table"]] = FALSE
new_file_flags[["custom_function_hierarchy_table"]] = FALSE
new_file_flags[["metadata_table"]] = FALSE
new_file_flags[["function_abundance_table"]] = FALSE
# Map of sample indices for tables that need to be sent to the browser, used to determine whether a new sample is being requested
previous_sample_indices = reactiveValues()
previous_sample_indices[["contribution_table"]] = -1
previous_sample_indices[["otu_table"]] = -1
# Map of tables that hold data to be sent to the browser
tracked_tables = reactiveValues()
tracked_tables[["contribution_table"]] = NULL
tracked_tables[["otu_table"]] = NULL
### Reactive values for keeping track of choices made on the upload page ###
# Reactive elements that keep track of the name of the first level of the hierarchies (what show up in the OTU and contribution tables)
first_taxonomic_level = reactive({
# If the example is being displayed, an automatic single level taxonomic hierarchy is being used, or there is no custom taxonomic hierarchy, use the default
if (input$example_visualization == "TRUE" | input$taxonomic_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL" | is.null(input$custom_taxonomic_hierarchy_table)){
return(colnames(default_taxonomic_hierarchy_table)[1])
# Otherwise, use the custom taxonomic hierarchy
} else {
# Read in the taxonomic hierarchy table
taxonomic_hierarchy_table = process_input_file('custom_taxonomic_hierarchy_table')
# Return the name of the first column
return(colnames(taxonomic_hierarchy_table)[1])
}
})
first_function_level = reactive({
# If the example is being displayed, an automatic single level function hierarchy is being used, or there is no custom function hierarchy, use the default
if (input$example_visualization == "TRUE" | input$function_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL" | is.null(input$custom_function_hierarchy_table)){
return(colnames(default_function_hierarchy_table)[1])
# Otherwise, use the custom function hierarchy
} else {
# Read in the function hierarchy table
function_hierarchy_table = process_input_file('custom_function_hierarchy_table')
# Return the name of the first column
return(colnames(function_hierarchy_table)[1])
}
})
first_metadata_level = reactive({
# If the example is being displayed or there is no uploaded metadata table, use the default
if (input$example_visualization == "TRUE" | is.null(input$metadata_table)){
return(colnames(default_metadata_table)[1])
# Otherwise, use the selected metadata table
} else {
# Read in the metadata table
metadata_table = process_input_file('metadata_table')
# Return the name of the first column
return(colnames(metadata_table)[1])
}
})
# Reactive elements that keep track of the current choice for summary levels and metadata factors
taxonomic_summary_level = reactive({
# If the example is not being displayed, look at the taxonomic level of detail selector
if (input$example_visualization != "TRUE"){
# If we're using an automatic single level taxonomic hierarchy, then we just use the default first taxonomic level
if (input$taxonomic_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL"){
return(first_taxonomic_level())
# Otherwise, if something hasn't been selected (currently having trouble setting a selected choice when we update the dropdown), load the hierarchy and check for the default taxonomic summary level, otherwise use the first column
} else if (is.null(input$taxonomic_level_of_detail_selector)){
# If a custom taxonomic hierarchy has been selected, we refer to it
if (input$taxonomic_hierarchy_choice == "CUSTOM"){
# If the custom taxonoimc hierarchy has been uploaded, we refer to it
if (!is.null(input$custom_taxonomic_hierarchy_table)){
# Read in the taxonomic hierarchy table
taxonomic_hierarchy_table = process_input_file('custom_taxonomic_hierarchy_table')
# If the default exists, use it
if (default_taxonomic_summary_level %in% colnames(taxonomic_hierarchy_table)){
return(default_taxonomic_summary_level)
}
# Otherwise, return the first column name
return(colnames(taxonomic_hierarchy_table)[1])
}
# Otherwise, a custom taxonomic hierarchy has not been selected and there is no selection
return(NULL)
}
# Otherwise, we're looking at the default taxonomic hierarchy, in which case we just return the default since nothing has been selected
return(default_taxonomic_summary_level)
}
# Otherwise, we just look at their choice
return(unname(input$taxonomic_level_of_detail_selector))
# Otherwise, we use the default for the example
} else {
return(default_taxonomic_summary_level)
}
})
function_summary_level = reactive({
# If the example is not being displayed, look at the function level of detail selector
if (input$example_visualization != "TRUE"){
# If we're using an automatic single level function hierarchy, then we just use the default first function level
if (input$function_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL"){
return(first_function_level())
# Otherwise, if something hasn't been selected (currently having trouble setting a selected choice when we update the dropdown), load the hierarchy and check for the default function summary level, otherwise use the first column
} else if (is.null(input$function_level_of_detail_selector)){
# If a custom function hierarchy has been selected, we refer to it
if (input$function_hierarchy_choice == "CUSTOM"){
# If the custom taxonoimc hierarchy has been uploaded, we refer to it
if (!is.null(input$custom_function_hierarchy_table)){
# Read in the function hierarchy table
function_hierarchy_table = process_input_file('custom_function_hierarchy_table')
# If the default exists, use it
if (default_function_summary_level %in% colnames(function_hierarchy_table)){
return(default_function_summary_level)
}
# Otherwise, return the first column name
return(colnames(function_hierarchy_table)[1])
}
# Otherwise, a custom function hierarchy has not been selected and there is no selection
return(NULL)
}
# Otherwise, we're looking at the default function hierarchy, in which case we just return the default since nothing has been selected
return(default_function_summary_level)
}
# Otherwise, we just look at their choice
return(unname(input$function_level_of_detail_selector))
# Otherwise, we use the default for the example
} else {
return(default_function_summary_level)
}
})
metadata_factor = reactive({
# If the example is not being displayed, look at the metdata factor selector
if (input$example_visualization != "TRUE"){
# If something hasn't been selected, we don't have a metadata factor
if (is.null(input$metadata_factor_selector)){
return(NULL)
}
# If N/A is selected, we don't have a metadata factor
if (input$metadata_factor_selector == "N/A"){
return(NULL)
}
# Otherwise, we return the selection
return(unname(input$metadata_factor_selector))
# Otherwise, we just use the default metadata factor for the example
} else {
return(default_metadata_factor)
}
})
# Reactive element to calculate partial contribution factors for the taxonomic hierarchy and function hierarchy
taxonomic_partial_contribution_table = reactive({
# Initialize as the default taxonomic hierarchy table
taxonomic_hierarchy_table = default_taxonomic_hierarchy_table
# If the example is not being displayed, the custom taxonomic hierarchy option is selected, and a custom taxonomic hierarchy file has been uploaded, use the custom taxonomic hierarchy table
if (input$example_visualization != "TRUE" & input$taxonomic_hierarchy_choice == "CUSTOM" & !is.null(input$custom_taxonomic_hierarchy_table)){
taxonomic_hierarchy_table = process_input_file('custom_taxonomic_hierarchy_table')
# Otherwise, if the example is not being displayed, the automatic single level taxonomic hierarchy option is selected, and a taxonomic abundance table has been uploaded, use the taxon names from the taxonomic abundance table
} else if (input$example_visualization != "TRUE" & input$taxonomic_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL" & !is.null(input$taxonomic_abundance_table)){
# Read the otu table to generate the taxonomic heirarchy and assign the appropriate column name
taxonomic_hierarchy_table = process_input_file('taxonomic_abundance_table')[,1,with=F]
colnames(taxonomic_hierarchy_table) = first_taxonomic_level()
}
# Make rows of the taxonomic hierarchy table unique
taxonomic_hierarchy_table = taxonomic_hierarchy_table[!duplicated(taxonomic_hierarchy_table)]
# Sum the occurrences of each element in the first column
taxonomic_hierarchy_table = taxonomic_hierarchy_table[,.N, by = eval(first_taxonomic_level())]
# Rename normalization column for consistency
colnames(taxonomic_hierarchy_table)[2] = "partial_contribution_factor"
return(taxonomic_hierarchy_table)
})
function_partial_contribution_table = reactive({
# Initialize as the default function hierarchy table
function_hierarchy_table = default_function_hierarchy_table
# If the example is not being displayed, the custom function hierarchy option is selected, and a custom function hierarchy file has been uploaded, use the custom function hierarchy table
if (input$example_visualization != "TRUE" & input$function_hierarchy_choice == "CUSTOM" & !is.null(input$custom_function_hierarchy_table)){
function_hierarchy_table = process_input_file('custom_function_hierarchy_table')
# Otherwise, if the example is not being displayed and the automatic single level function hierarchy option is selected, check whether a function abundance table is being used and which linking method is being used
} else if (input$example_visualization != "TRUE" & input$function_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL"){
# Gather set of functions that need to exist in the hierarchy
present_functions = c()
# If a function abundance table is being used, add functions in that table to the list
if (input$function_abundance_choice == "PRESENT" & !is.null(input$function_abundance_table)){
present_functions = c(present_functions, unlist(process_input_file('function_abundance_table')[,1,with=F]))
}
# If the default PICRUSt methodology is being used to link, add the functions from the default function hierarchy table
if (input$contribution_method_choice == "PICRUST"){
present_functions = c(present_functions, unlist(default_function_hierarchy_table[,1,with=F]))
# Otherwise, if custom genomic content is being used to link, add the functions from the custom genomic content table
} else if (input$contribution_method_choice == "GENOMIC_CONTENT" & !is.null(input$genomic_content_table)){
present_functions = c(present_functions, unlist(process_input_file('genomic_content_table')[,2,with=F]))
# Otherwise, if a custom contribution table is being used to link, add the functions from the custom contribution table
} else if (input$contribution_method_choice == "CONTRIBUTION" & !is.null(input$contribution_table)){
present_functions = c(present_functions, unlist(process_input_file('contribution_table')[,1,with=F]))
}
# Create a single column function hierarchy using the present functions
function_hierarchy_table = data.table(a = present_functions)
colnames(function_hierarchy_table) = first_function_level()
}
# Make rows of the function hierarchy table unique
function_hierarchy_table = function_hierarchy_table[!duplicated(function_hierarchy_table)]
# Sum the occurrences of each element in the first column
function_hierarchy_table = function_hierarchy_table[,.N, by = eval(first_function_level())]
# Rename normalization column for consistency
colnames(function_hierarchy_table)[2] = "partial_contribution_factor"
return(function_hierarchy_table)
})
# Observer that updates the flags indicating that a new file has been seleted for upload (necessary to detect when a new file has been selected but has not finished being uploaded)
observe({
if (!is.null(input$new_file_trigger)){
isolate({
new_file_flags[[input$new_file_trigger]] = TRUE
})
}
})
# Observers that update dropdown menu options when selected files change
observe({
# If a custom taxonomic hierarchy has been selected, we refer to it
if (input$taxonomic_hierarchy_choice == "CUSTOM"){
# If a custom taxonomic hierarchy has been uploaded, we refer to it
if (!is.null(input$custom_taxonomic_hierarchy_table)){
isolate({
# Read in the taxonomic hierarchy table
taxonomic_hierarchy_table = process_input_file('custom_taxonomic_hierarchy_table')
# If we weren't able to read the taxonomic hierarchy table, report that the file was an invalid table format
if (is.null(taxonomic_hierarchy_table)){
session$sendCustomMessage("taxonomic_hierarchy_dropdown_labels", list("N/A"))
session$sendCustomMessage("abort", "The selected taxonomic hierarchy table was in an invalid table format. Please select a file in a valid table format.")
# Otherwise, get the column names of the hierarchy
} else {
# If there is more than one column, then resolution order is highest at the first column and then from lowest to second highest from left to right for the rest of the columns, so reorder the labels
if (ncol(taxonomic_hierarchy_table) > 1){
session$sendCustomMessage("taxonomic_hierarchy_dropdown_labels", colnames(taxonomic_hierarchy_table)[c(2:ncol(taxonomic_hierarchy_table), 1)])
# Otherwise with a single column the order of the single label doesn't matter
} else {
session$sendCustomMessage("taxonomic_hierarchy_dropdown_labels", list(colnames(taxonomic_hierarchy_table)))
}
}
})
# Otherwise, we send the N/A string
} else {
session$sendCustomMessage("taxonomic_hierarchy_dropdown_labels", list("N/A"))
}
# Otherwise, if an automatic single level taxonomic hierarchy has been selected, we send the N/A string
} else if (input$taxonomic_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL"){
session$sendCustomMessage("taxonomic_hierarchy_dropdown_labels", list("N/A"))
# Otherwise, we use the default taxonomic hierarchy, which has to have its column labels reordered for the dropdown to be in resolution order
} else {
session$sendCustomMessage("taxonomic_hierarchy_dropdown_labels", colnames(default_taxonomic_hierarchy_table)[c(2:ncol(default_taxonomic_hierarchy_table), 1)])
}
})
observe({
# If a custom function hierarchy has been selected, we refer to it
if (input$function_hierarchy_choice == "CUSTOM"){
# If a custom function hierarchy has been uploaded, we refer to it
if (!is.null(input$custom_function_hierarchy_table)){
isolate({
# Read in the funciton hierarchy table
function_hierarchy_table = process_input_file('custom_function_hierarchy_table')
# If we weren't able to read the taxonomic hierarchy table, report that the file was an invalid table format
if (is.null(function_hierarchy_table)){
session$sendCustomMessage("function_hierarchy_dropdown_labels", list("N/A"))
session$sendCustomMessage("abort", "The selected function hierarchy table was in an invalid table format. Please select a file in a valid table format.")
# Otherwise, get the column names of the hierarchy
} else {
# If there is more than one column, then resolution order is highest at the first column and then from lowest to second highest from left to right for the rest of the columns, so reorder the labels
if (ncol(function_hierarchy_table) > 1){
session$sendCustomMessage("function_hierarchy_dropdown_labels", colnames(function_hierarchy_table)[c(2:ncol(function_hierarchy_table), 1)])
# Otherwise with a single column the order of the single label doesn't matter
} else {
session$sendCustomMessage("function_hierarchy_dropdown_labels", list(colnames(function_hierarchy_table)))
}
}
})
# Otherwise, we send the N/A string
} else {
session$sendCustomMessage("function_hierarchy_dropdown_labels", list("N/A"))
}
# Otherwise, if an automatic single level function hierarchy has been selected, we send the N/A string
} else if (input$function_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL"){
session$sendCustomMessage("function_hierarchy_dropdown_labels", list("N/A"))
# Otherwise, we use the default function hierarchy, which has to have its column labels reordered for the dropdown to be in resolution order
} else {
session$sendCustomMessage("function_hierarchy_dropdown_labels", colnames(default_function_hierarchy_table)[2:ncol(default_function_hierarchy_table)])
}
})
observe({
# If the option to upload metadata has been selected, we refer to it
if (input$metadata_choice == "PRESENT"){
# If the metadata table has been uploaded, we refer to it
if (!is.null(input$metadata_table)){
isolate ({
metadata_table = process_input_file('metadata_table')
# If we weren't able to read the metadata table, report that the file was an invalid table format
if (is.null(metadata_table)){
session$sendCustomMessage("metadata_table_labels", list("N/A"))
session$sendCustomMessage("abort", "The selected sample grouping table was in an invalid table format. Please select a file in a valid table format.")
# Otherwise, get the column names of the hierarchy
} else {
# If there is only one column, then we send the N/A string
if (ncol(metadata_table) < 2){
session$sendCustomMessage("metadata_table_labels", list("N/A"))
# Otherwise, we can send the non-sample columns, lead by the N/A option as the default
} else {
session$sendCustomMessage("metadata_table_labels", c("N/A", colnames(metadata_table)[2:ncol(metadata_table)]))
}
}
})
# Otherwise, we send the N/A string
} else {
session$sendCustomMessage("metadata_table_labels", list("N/A"))
}
# Otherwise, we send the N/A string
} else {
session$sendCustomMessage("metadata_table_labels", list("N/A"))
}
})
# Observers that handle requests for sample data from the browser
observe({
if (!is.null(input$contribution_sample_request)){
isolate({
contribution_sample = input$contribution_sample_request
if (contribution_sample != previous_sample_indices[["contribution_table"]]){
previous_sample_indices[["contribution_table"]] = contribution_sample
# If the new sample exists in the table, then we can return it
if (contribution_sample >= 0 & contribution_sample < length(tracked_tables[["contribution_table"]])){
session$sendCustomMessage(type="contribution_sample_return", tracked_tables[["contribution_table"]][contribution_sample + 1])
}
# If we just sent the last sample, then we can get rid of the table to free up memory
if (contribution_sample == length(tracked_tables[["contribution_table"]]) + 1){
tracked_tables[["contribution_table"]] = NULL
}
}
})
}
})
observe({
if (!is.null(input$taxonomic_abundance_sample_request)){
isolate({
otu_sample = input$taxonomic_abundance_sample_request
if (otu_sample != previous_sample_indices[["otu_table"]]){
previous_sample_indices[["otu_table"]] = otu_sample
# If the new sample exists in the table, then we can return it
if (otu_sample >= 0 & otu_sample < length(tracked_tables[["otu_table"]])){
session$sendCustomMessage(type="taxonomic_abundance_sample_return", tracked_tables[["otu_table"]][otu_sample + 1])
}
# If we just sent the last sample, then we can get rid of the table to free up memory
if (otu_sample == length(tracked_tables[["otu_table"]]) + 1){
tracked_tables[["otu_table"]] = NULL
}
}
})
}
})
observe({
session$sendCustomMessage("maintain_connection", input$contact)
})
# Helper functions
# process_input_file(input_element)
#
# Checks if the input file associated with the specified html input element is available to read and if so then reads in the table and returns it
process_input_file = function(input_element){
input_table_file = input[[input_element]]
# If the file exists, grab the file path, otherwise send the signal to retry and return NULL
if (is.null(input_table_file)){
session$sendCustomMessage("retry_upload", "retry")
return(NULL)
}
# Otherwise, grab the name and file path'
input_table_file_name = input_table_file$name
input_table_file_path = input_table_file$datapath
# If a new file has been selected and there was previously a file, check to see if the new file path is different
if (new_file_flags[[input_element]] & !is.null(old_file_paths[[input_element]])){
# If the new file path is the same as the old file path, then the file has not been updated and we send the signal to retry the upload process
if (input_table_file_path == old_file_paths[[input_element]]){
session$sendCustomMessage("retry_upload", "retry")
return(NULL)
}
}
# Reset the new file flag and update the old file path since the file has updated correctly
new_file_flags[[input_element]] = FALSE
old_file_paths[[input_element]] = input_table_file_path
# Read the table and return it
input_table = NULL
tryCatch({
# If the file is gzipped, we unzip and read it, otherwise just read it normally
if (grepl("gz$", input_table_file_name)){
input_table = fread(paste('zcat ', input_table_file_path, sep=""), header=TRUE)
} else {
input_table = fread(input_table_file_path, header=TRUE)
}
}, error = function(e) {
input_table = NULL
})
return(input_table)
}
# validate_number_of_columns(input_table, column_number, comparison, table_name)
#
# Checks that there are the appropriate number of columns in the table based on the column number and comparison provided. If there are an incorrect number of columns, send an abort signal and return FALSE
validate_number_of_columns = function(input_table, column_number, comparison, table_name){
num_cols = ncol(input_table)
if (comparison == "eq"){
if (num_cols != column_number){
session$sendCustomMessage("abort", paste("The ", table_name, " should have ", column_number, " column(s) but instead has ", num_cols, " column(s)", sep=""))
return(FALSE)
}
} else if (comparison == "lt"){
if (num_cols >= column_number){
session$sendCustomMessage("abort", paste("The ", table_name, " should have fewer than ", column_number, " column(s) but instead has ", num_cols, " column(s)", sep=""))
return(FALSE)
}
} else if (comparison == "le"){
if (num_cols > column_number){
session$sendCustomMessage("abort", paste("The ", table_name, " should have fewer than or equal to ", column_number, " column(s) but instead has ", num_cols, " column(s)", sep=""))
return(FALSE)
}
} else if (comparison == "gt"){
if (num_cols <= column_number){
session$sendCustomMessage("abort", paste("The ", table_name, " should have more than ", column_number, " column(s) but instead has ", num_cols, " column(s)", sep=""))
return(FALSE)
}
} else if (comparison == "ge"){
if (num_cols < column_number){
session$sendCustomMessage("abort", paste("The ", table_name, " should have more than or equal to ", column_number, " column(s) but instead has ", num_cols, " column(s)", sep=""))
return(FALSE)
}
}
return(TRUE)
}
# validate_unique_rows(input_table, id_column_names, element_name, table_name)
#
# Checks that no rows correspond to the same entity in the table as identified by the indicated ID columns. If there are any duplicates, send an abort signal and return FALSE
validate_unique_rows = function(input_table, id_column_names, element_name, table_name){
id_columns = input_table[,id_column_names,with=F]
duplicate_elements = duplicated(id_columns)
# If there are duplicated elements, send an abort signal and return FALSE
if (any(duplicate_elements)){
duplicate_id_columns = id_columns[duplicate_elements]
duplicate_element_ids = sapply(1:nrow(duplicate_id_columns), function(row){
return(paste(unlist(duplicate_id_columns[row]), collapse = "-"))
})
session$sendCustomMessage("abort", paste("The following ", element_name, " have multiple rows in the ", table_name, ": ", paste(unique(duplicate_element_ids), collapse = " "), sep=""))
return(FALSE)
}
return(TRUE)
}
# validate_unique_columns(input_table, columns, element_name, table_name)
#
# Checks that no columns in the provided set of columns have the same name. If there are any duplicates, send an abort signal and return FALSE
validate_unique_columns = function(input_table, columns, element_name, table_name){
column_names_to_check = colnames(input_table)[columns]
duplicate_column_names = duplicated(column_names_to_check)
# If there are duplicated column names, send an abort signal and return FALSE
if (any(duplicate_column_names)){
session$sendCustomMessage("abort", paste("The following ", element_name, " have multiple columns in the ", table_name, ": ", paste(unique(column_names_to_check[duplicate_column_names]), collapse = " "), sep=""))
return(FALSE)
}
return(TRUE)
}
# validate_elements_from_first_found_in_second(first_element_set, second_element_set, element_name, first_table_name, second_table_name)
#
# Checks that no elements are present in the first table and not in the second. If there are elements in the first table that are not in the second, send an abort signal and return FALSE
validate_elements_from_first_found_in_second = function(first_element_set, second_element_set, element_name, first_table_name, second_table_name){
extra_elements = !(first_element_set %in% second_element_set)
# If there are elements in the first set that are not in the second set, send an abort signal and return FALSE
if (any(extra_elements)){
session$sendCustomMessage("abort", paste("The following ", element_name, " are in the ", first_table_name, " but are not present in the ", second_table_name, ": ", paste(unique(first_element_set[extra_elements]), collapse = " "), sep=""))
return(FALSE)
}
return(TRUE)
}
# check_elements_from_first_found_in_second(first_element_set, second_element_set, element_name, first_table_name, second_table_name)
#
# Checks that no elements are present in the first table and not in the second. If there are elements in the first table that are not in the second, report them to the user but continue with data processing
check_elements_from_first_found_in_second = function(first_element_set, second_element_set, element_name, first_table_name, second_table_name){
extra_elements = !(first_element_set %in% second_element_set)
# If there are elements in the first set that are not in the second set, send an abort signal and return FALSE
if (any(extra_elements)){
session$sendCustomMessage("warning", paste("The following ", element_name, " are in the ", first_table_name, " but are not present in the ", second_table_name, ": ", paste(unique(first_element_set[extra_elements]), collapse = " "), sep=""))
}
}
# filter_elements_from_first_not_found_in_second(first_table, second_table, first_table_column_name, second_table_column_name, element_name, first_table_name, second_table_name)
#
# Filter elements out of the first table that do not appear in the second table
filter_elements_from_first_not_found_in_second = function(first_table, second_table, first_table_column_name, second_table_column_name, element_name, first_table_name, second_table_name){
first_element_set = first_table[[first_table_column_name]]
second_element_set = second_table[[second_table_column_name]]
extra_elements = !(first_element_set %in% second_element_set)
# If there are elements in the first set that are not in the second set, send a warning
if (any(extra_elements)){
session$sendCustomMessage("warning", paste("The following ", element_name, " are in the ", first_table_name, " but are not present in the ", second_table_name, " and will be automatically removed: ", paste(unique(first_element_set[extra_elements]), collapse = " "), sep=""))
}
return(first_table[!extra_elements])
}
# validate_tables_match(otu_table, contribution_table, function_abundance_table, taxonomic_hierarchy_table, function_hierarchy_table, metadata_table)
#
# Checks whether the tables match with consistent labels and samples
validate_tables_match = function(otu_table, contribution_table, function_abundance_table, taxonomic_hierarchy_table, function_hierarchy_table, metadata_table){
# If the metadata table is not empty, check that samples in the OTU table are in the metadata table
if (nrow(metadata_table) > 0){
if (!validate_elements_from_first_found_in_second(otu_table[[first_metadata_level()]], metadata_table[[first_metadata_level()]], "samples", "OTU table", "sample grouping table")){
return(FALSE)
}
}
# If the metadata table is not empty, check that samples in the contribution table are in the metadata table
if (nrow(metadata_table) > 0){
if (!validate_elements_from_first_found_in_second(contribution_table[[first_metadata_level()]], metadata_table[[first_metadata_level()]], "samples", "contribution table", "sample grouping table")){
return(FALSE)
}
}
return(TRUE)
}
# process_otu_table_file()
#
# Processes the otu table specified from the upload page
process_otu_table_file = function(){
otu_table_file = input$taxonomic_abundance_table
# If the file is unavailable, check whether a file was actually selected
if (is.null(otu_table_file)){
# If no file was ever selected, send an abort signal
if (!new_file_flags[["taxonomic_abundance_table"]]){
session$sendCustomMessage("abort", "No file was selected for the taxonomic abundance table. Please select one.")
return(NULL)
# Otherwise, if a file was selected but hasn't fully uploaded yet, we send the signal to retry
} else {
session$sendCustomMessage("retry_upload", "retry")
return(NULL)
}
}
# Read the otu table
otu_table = process_input_file("taxonomic_abundance_table")
# If we weren't able to read the otu table, report that the file was an invalid table format
if (is.null(otu_table)){
session$sendCustomMessage("abort", "The selected taxonomic abundance table was in an invalid table format. Please select a file in a valid table format.")
return(NULL)
}
# Check that there is more than 1 column
column_num_validated = validate_number_of_columns(otu_table, 1, 'gt', "OTU table")
# If there were fewer than 2 columns, return NULL
if (!column_num_validated){
return(NULL)
}
# Rename the OTU label column for consistency
colnames(otu_table)[1] = first_taxonomic_level()
unique_otus_validated = validate_unique_rows(otu_table, first_taxonomic_level(), "taxa", "OTU table")
# If there are duplicated OTUs, return NULL
if (!unique_otus_validated){
return(NULL)
}
unique_samples_validated = validate_unique_columns(otu_table, 2:ncol(otu_table), "samples", "OTU table")
# If there are duplicated samples, return NULL
if (!unique_samples_validated){
return(NULL)
}
# Format the OTU table
otu_table = format_otu_table(otu_table)
# Convert the OTU names to character type
otu_table[,(first_taxonomic_level()) := as.character(get(first_taxonomic_level()))]
return(otu_table)
}
# process_genomic_content_table_file()
#
# Processes the genomic content table specified from the upload page
process_genomic_content_table_file = function(){
genomic_content_table_file = input$genomic_content_table
# If the file is unavailable, check whether a file was actually selected
if (is.null(genomic_content_table_file)){
# If no file was ever selected, send an abort signal
if (!new_file_flags[["genomic_content_table"]]){
session$sendCustomMessage("abort", "The custom genomic content option was chosen, but no custom genomic content file was selected. Please select one or choose a different option.")
return(NULL)
# Otherwise, if a file was selected but hasn't fully uploaded yet, we send the signal to retry
} else {
session$sendCustomMessage("retry_upload", "retry")
return(NULL)
}
}
# Read in the genomic content table
genomic_content_table = process_input_file("genomic_content_table")
# If we weren't able to read the genomic content table, report that the file was an invalid table format
if (is.null(genomic_content_table)){
session$sendCustomMessage("abort", "The selected genomic content table was in an invalid table format. Please select a file in a valid table format.")
return(NULL)
}
# Check that there are 3 columns
column_num_validated = validate_number_of_columns(genomic_content_table, 3, 'eq', "genomic content table")
# If there are not exactly 3 columns, return NULL
if (!column_num_validated){
return(NULL)
}
# Rename the ID columns for consistency
colnames(genomic_content_table) = c(first_taxonomic_level(), first_function_level(), "copy_number")
unique_genomic_content_validated = validate_unique_rows(genomic_content_table, c(first_taxonomic_level(), first_function_level()), paste(first_taxonomic_level(), "-", first_function_level(), " pairs", sep=""), "genomic content table")
# If there is duplicated genomic content, return NULL
if (!unique_genomic_content_validated){
return(NULL)
}
# Convert the otu names and function names to character type
genomic_content_table[,(first_taxonomic_level()) := as.character(get(first_taxonomic_level()))]
genomic_content_table[,(first_function_level()) := as.character(get(first_function_level()))]
return(genomic_content_table)
}
# process_contribution_table_file()
#
# Processes the contribution table specified from the upload page
process_contribution_table_file = function(){
contribution_table_file = input$contribution_table
# If the file is unavailable, check whether a file was actually selected
if (is.null(contribution_table_file)){
# If no file was ever selected, send an abort signal
if (!new_file_flags[["contribution_table"]]){
session$sendCustomMessage("abort", "The custom contribution option was chosen, but no custom contribution file was selected. Please select one or choose a different option.")
return(NULL)
# Otherwise, if a file was selected but hasn't fully uploaded yet, we send the signal to retry
} else {
session$sendCustomMessage("retry_upload", "retry")
return(NULL)
}
}
# Read in the contribution table
contribution_table = process_input_file("contribution_table")
# If we weren't able to read the contribution table, report that the file was an invalid table format
if (is.null(contribution_table)){
session$sendCustomMessage("abort", "The selected contribution table was in an invalid table format. Please select a file in a valid table format.")
return(NULL)
}
# Check that there are at least 6 columns
column_num_validated = validate_number_of_columns(contribution_table, 6, 'ge', "contribution table")
# If there are fewer than 6 columns, return NULL
if (!column_num_validated){
return(NULL)
}
# Reorder and drop columns to match internal contribution table format
contribution_table = contribution_table[,c(2,3,1,6),with=F]
# Rename columns for consistency
colnames(contribution_table) = c(first_metadata_level(), first_taxonomic_level(), first_function_level(), "contribution")
unique_contributions_validated = validate_unique_rows(contribution_table, c(first_metadata_level(), first_taxonomic_level(), first_function_level()), paste("contributions (combinations of ", first_metadata_level(), ", ", first_taxonomic_level(), ", and ", first_function_level(), ")", sep=""), "contribution table")
# If there are duplicate rows, return NULL
if (!unique_contributions_validated){
return(NULL)
}
# Convert sample names, otu names, and function names to character type
contribution_table[,(first_metadata_level()) := as.character(get(first_metadata_level()))]
contribution_table[,(first_taxonomic_level()) := as.character(get(first_taxonomic_level()))]
contribution_table[,(first_function_level()) := as.character(get(first_function_level()))]
return(contribution_table)
}
# process_function_abundance_table_file()
#
# Processes the function abundance table specified from the upload page
process_function_abundance_table_file = function(){
# If the option to upload a table of function abundances for comparison is selected, read it in
if (input$function_abundance_choice == "PRESENT"){
function_abundance_table_file = input$function_abundance_table
# If the file is unavailable, check whether a file was actually selected
if (is.null(function_abundance_table_file)){
# If no file was ever selected, send an abort signal
if (!new_file_flags[["function_abundance_table"]]){
session$sendCustomMessage("abort", "The option to upload function abundances for comparison was chosen, but no function abundance file was selected. Please select one or choose the option to upload no function abundances.")
return(NULL)
# Otherwise, if a file was selected but hasn't fully uploaded yet, we send the signal to retry
} else {
session$sendCustomMessage("retry_upload", "retry")
return(NULL)
}
}
# Read in the function abundance table
function_abundance_table = process_input_file("function_abundance_table")
# If we weren't able to read the function abundance table, report that the file was an invalid table format
if (is.null(function_abundance_table)){
session$sendCustomMessage("abort", "The selected function abundance table was in an invalid table format. Please select a file in a valid table format.")
return(NULL)
}
# Check that there are at least two columns
column_num_validated = validate_number_of_columns(function_abundance_table, 1, 'gt', "function abundance table")
# Rename columns for consistency
colnames(function_abundance_table)[1] = first_function_level()
# If there is one or fewer columns, return NULL
if (!column_num_validated){
return(NULL)
}
unique_functions_validated = validate_unique_rows(function_abundance_table, first_function_level(), paste(first_function_level(), "s", sep=""), "function abundance table")
# If there are duplicate functions, return NULL
if (!unique_functions_validated){
return(NULL)
}
unique_samples_validated = validate_unique_columns(function_abundance_table, 2:ncol(function_abundance_table), "samples", "function abundance table")
# If there are duplicate samples, return NULL
if (!unique_samples_validated){
return(NULL)
}
# Convert the function names to character type
function_abundance_table[,(first_function_level()) := as.character(get(first_function_level()))]
return(function_abundance_table)
}
# Othwerise, return a default empty table
return(data.table())
}
# process_taxonomic_hierarchy_table_file()
#
# Processes the taxonomic hierarchy table specified from the upload page
process_taxonomic_hierarchy_table_file = function(){
# If the option to use the default taxonomic hierarchy table is selected, then return the default
if (input$taxonomic_hierarchy_choice == "DEFAULT"){
return(default_taxonomic_hierarchy_table)
}
# Othwerise, if the option to use an automatical single level taxonomic hierarchy is selected, then use the taxon IDs from the otu table to make the taxonomic hierarchy
if (input$example_visualization != "TRUE" & input$taxonomic_hierarchy_choice == "AUTOMATIC_SINGLE_LEVEL"){
# If the file is unavailable, check whether a file was actually selected
if (is.null(input$taxonomic_abundance_table)){
# If no file was ever selected, send an abort signal
if (!new_file_flags[["taxonomic_abundance_table"]]){
session$sendCustomMessage("abort", "The option to generate a single level taxonomic hierarchy was chosen, but no taxonomic abundance file was selected. Please select one or choose a different taxonomy option.")
return(NULL)
# Otherwise, if a file was selected but hasn't fully uploaded yet, we send the signal to retry
} else {
session$sendCustomMessage("retry_upload", "retry")
return(NULL)
}
}
# Read the otu table to generate the taxonomic heirarchy and assign the appropriate column name
taxonomic_hierarchy_table = process_input_file('taxonomic_abundance_table')[,1,with=F]
colnames(taxonomic_hierarchy_table) = first_taxonomic_level()
return(taxonomic_hierarchy_table)
}
# Othwerise, we read the custom taxonomic hierarchy table