-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
4103 lines (3523 loc) · 144 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
########################## SERVER LOGIC ############################
server <- function(input, output, session) {
options(shiny.maxRequestSize = 1000 * 1024 ^ 2) ## This sets the size limit for AOViz, 100 mb right now
## These generate NULL data to prevent the loading swirl from starting before press "start" on the analyses
output$read_table_out <- renderDT(NULL)
output$read_plot <- renderDT(NULL)
output$b1_table_out <- renderDT(NULL)
output$bubble_out <- renderDT(NULL)
output$taxa_bar <- renderDT(NULL)
output$bar_table_out <- renderDT(NULL)
output$pcoa_plot_out <- renderDT(NULL)
output$pcoa_table_out <- renderDT(NULL)
output$ranked_table_out <- renderDT(NULL)
output$ranked_plot_out <- renderDT(NULL)
output$uni_pcoa_plot_out <- renderDT(NULL)
# ## These pass on the metadata column names to all of the drop down menus. They might be causing issues with my start buttons.
# observe({
# req(input$meta_file)
#
# ## Incorporate the meta file column names into every appropriate input
# meta_datafile <- meta_datafile_og()
# meta_colnames <- c(colnames(meta_datafile), "TaxaName")
# meta_colnames <- meta_colnames[meta_colnames != "SampleName"]
#
# updateCheckboxInput(session, "remove_low_reads")
#
#
#
# ## Update Selection - UniPCoA plot
#
#
# })
###### Upload tab ######
## Test data upload. To accomplish this, I think I need to duplicate my code for the scenario in which the test button is pressed.
# Function to load test data from app www/ directory
### Test data ###
## Main ASV table
observeEvent(input$test_data,{
main_datafile_upload <- reactive({
read.table(
file = "test_ASV.txt",
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
main_datafile_og <- reactive({
main_datafile = main_datafile_upload()
main_datafile[is.na(main_datafile)] <- 0
main_datafile
})
output$main_table <- renderDataTable({
read.table(
file = "test_ASV.txt",
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
## Metadata table
meta_datafile_og <- reactive({
read.table(
file = "test_metadata.txt",
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
output$meta_table <- renderDataTable({
read.table(
file = "test_metadata.txt",
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
## Filter the ASV table based on the present samples in the metadata table
main_datafile <- reactive({
# req(input$main_file)
main_data_table <- main_datafile_og()
meta_data_table <- meta_datafile()
meta_names <-
c(
meta_data_table$SampleName,
"Consensus.Lineage",
"rowID",
"Feature.ID",
"ReprSequence"
)
main_data_table <-
main_data_table[, names(main_data_table) %in% meta_names]
main_data_table
})
## Filter the metadata table based on present samples in ASV table
meta_datafile <- reactive({
meta_data_table <- meta_datafile_og()
main_data_table <- main_datafile_og()
meta_names <-
c(
meta_data_table$SampleName,
"Consensus.Lineage",
"rowID",
"Feature.ID",
"ReprSequence"
)
main_data_table <-
main_data_table[, names(main_data_table) %in% meta_names]
meta_data_table <-
meta_data_table %>% filter(SampleName %in% colnames(main_data_table))
# meta_data_table$TaxaName <- 1:nrow(meta_data_table)
meta_data_table <-
replace(meta_data_table, is.na(meta_data_table), "NA")
meta_data_table
})
## ASV contaminant table
output$contam_table <- renderDataTable({
read.table(
file = "test_contam.txt",
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
contam_datafile <- reactive({
read.table(
file = "test_contam.txt",
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
## Show the original table that was uploaded
output$proc_main <- renderDataTable({
main_datafile <- main_datafile()
output$proc_maintext <- renderText("This is your original data")
# req(input$main_file)
main_datafile
})
})
## Main ASV table
{
output$main_table <- renderDataTable({
req(input$main_file)
read.table(
file = input$main_file$datapath,
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
main_datafile_upload <- reactive({
req(input$main_file)
read.table(
file = input$main_file$datapath,
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
main_datafile_og <- reactive({
main_datafile = main_datafile_upload()
main_datafile[is.na(main_datafile)] <- 0
main_datafile
})
output$proc_main <- renderDataTable({
main_datafile <- main_datafile()
output$proc_maintext <- renderText("This is your original data")
# req(input$main_file)
main_datafile
})
## Metadata table
meta_datafile_og <- reactive({
req(input$meta_file)
read.table(
file = input$meta_file$datapath,
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
output$meta_table <- renderDataTable({
req(input$meta_file)
read.table(
file = input$meta_file$datapath,
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
## Filter the ASV table based on the present samples in the metadata table
main_datafile <- reactive({
# req(input$main_file)
main_data_table <- main_datafile_og()
meta_data_table <- meta_datafile()
meta_names <-
c(
meta_data_table$SampleName,
"Consensus.Lineage",
"rowID",
"Feature.ID",
"ReprSequence"
)
main_data_table <-
main_data_table[, names(main_data_table) %in% meta_names]
main_data_table
})
## Filter the metadata table based on present samples in ASV table
meta_datafile <- reactive({
meta_data_table <- meta_datafile_og()
main_data_table <- main_datafile_og()
meta_names <-
c(
meta_data_table$SampleName,
"Consensus.Lineage",
"rowID",
"Feature.ID",
"ReprSequence"
)
main_data_table <-
main_data_table[, names(main_data_table) %in% meta_names]
meta_data_table <-
meta_data_table %>% filter(SampleName %in% colnames(main_data_table))
# meta_data_table$TaxaName <- 1:nrow(meta_data_table)
meta_data_table <-
replace(meta_data_table, is.na(meta_data_table), "NA")
meta_data_table
})
## ASV contaminant table
output$contam_table <- renderDataTable({
req(input$contam_file)
read.table(
file = input$contam_file$datapath,
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
contam_datafile <- reactive({
req(input$contam_file)
read.table(
file = input$contam_file$datapath,
fill = TRUE,
header = TRUE,
sep = "\t"
)
})
}
## Show the original table that was uploaded
output$proc_main <- renderDataTable({
main_datafile <- main_datafile()
output$proc_maintext <- renderText("This is your original data")
# req(input$main_file)
main_datafile
})
#### Main table (Processing tab) #### I SHOULD REMOVE THIS FOR SIMPLICITY
data_tran_react <- reactive({
#req(input$main_file)
data_tran <- main_datafile()
## Converting collapsed tables to ASV tables, and standardizing formatting, including adding rowID columns
if (input$is_main_collapsed == TRUE) {
data_tran$Consensus.Lineage <- data_tran$Feature.ID
data_tran$rowID <- 1:nrow(data_tran)
} else {
if (input$is_main_collapsed == FALSE) {
if ("rowID" %in% colnames(data_tran)) {
data_tran
} else {
data_tran$rowID <- 1:nrow(data_tran)
}
}
}
data_tran
})
##### Transformed tables (processing tab) #####
## This needs to be changed to be only activated once, then again by any subsequent
data_tran_contam_filt_react <- reactive({
if (input$contam_filter == "Remove") {
contam_datafile <- contam_datafile()
data_tran <- data_tran_react()
rownames(data_tran) <- data_tran$Feature.ID
# data_tran <- data_tran[ ! data_tran$Feature.ID %in% contam_datafile$Feature.ID,]
data_tran[contam_datafile$Feature.ID, !names(data_tran) %in% c("Consensus.Lineage",
"Feature.ID",
"ReprSequence",
"rowID")] = 0
#rownames(data_tran) <- 1:nrow(data_tran)
}
## There is an issue here I haven't sorted out
if (input$contam_filter == "Analyze") {
contam_datafile <- contam_datafile()
data_tran <- data_tran_react()
data_tran <- data_tran[data_tran$Feature.ID %in% contam_datafile$Feature.ID, ]
}
if (input$contam_filter == "No action") {
data_tran <- data_tran_react()
}
# } else {
# data_tran
data_tran
})
output$proc_new_data <- renderDataTable({
data_tran <- data_tran_contam_filt_react()
output$proc_new_text <- renderText("This is your new dataframe")
data_tran
})
data_labels_react <- reactive({
data_tran <- data_tran_contam_filt_react()
## Need to set the labels here first, not elsewhere (like in other reactive elements)
if (input$is_main_collapsed == TRUE) {
labels <- data_tran$Consensus.Lineage
labels <- paste(labels, data_tran$rowID, sep = "_")
data_tran$Consensus.Lineage <-
gsub(" ", "", data_tran$Consensus.Lineage)
} else {
labels <- data_tran$Consensus.Lineage
labels <- paste(labels, data_tran$rowID, sep = "_")
data_tran$Consensus.Lineage <-
gsub(" ", "", data_tran$Consensus.Lineage)
}
## Cleans up labels and removes uncultured/ambiguous taxa
labels <- gsub("_[0-9]*$", "", labels)
labels <- gsub(" ", "", labels)
labels <- gsub("(;Ambiguous__taxa)", ";s__Ambiguous_taxa", labels)
labels <- gsub("(;Ambiguous_taxa)", ";s__Ambiguous_taxa", labels)
if (input$truncate_taxa == "Yes") {
labels <- paste(";", sep = "", labels)
labels <- gsub("(;\\s*Ambiguous_taxa)", "", labels)
labels <- gsub("(uncultured.*)", "", labels)
labels <- gsub("(__uncultured.*)", "", labels)
labels <- gsub("(unidenti.*)", "", labels)
labels <- gsub("(__unidenti.*)", "", labels)
labels <- gsub("(;.__Ambiguous_taxa)", "", labels)
labels <- gsub("(;._Ambiguous_taxa)", "", labels)
labels <- gsub("(;s__$)", "", labels)
labels <- gsub("(;g__$)", "", labels)
}
## Remove taxa pre-fixes associated with SILVA classifier (old):
if (input$remove_prefix == "Yes") {
labels <- gsub("(D_.__)", "", labels)
labels <- gsub(";$", "", labels)
}
labels <- gsub("(D_.__$)", "", labels)
## Remove taxa-prefixed associated with SILVA classifier (new):
if (input$remove_prefix == "Yes") {
labels <- gsub("(d__)", "", labels)
labels <- gsub("(p__)", "", labels)
labels <- gsub("(c__)", "", labels)
labels <- gsub("(o__)", "", labels)
labels <- gsub("(f__)", "", labels)
labels <- gsub("(g__)", "", labels)
labels <- gsub("(s__)", "", labels)
labels <- gsub(" ", "", labels)
labels <- gsub("(;metagenome$)", "", labels)
labels <- gsub("(__.$)", "", labels)
labels <- gsub("(;__)", "", labels)
labels <- gsub("(;$)", "", labels)
labels <-
gsub("(;$)", "", labels) #This is not a duplicate -- leave it here
}
## Retrieves the last taxonomy entry (i.e., genus, species) and preserves the link between featureID and taxonomy:
feature_taxonomy <- as.data.frame(data_tran$Consensus.Lineage, data_tran$Feature.ID)
full_lineage <-
as.data.frame(paste(data_tran$Consensus.Lineage, data_tran$rowID, sep = "_"))
lineage_OTU <-
as.data.frame(paste(gsub(".*;", "", labels), data_tran$rowID, sep = "_"))
colnames(lineage_OTU) <- "TaxaName"
colnames(full_lineage) <- "TaxaName"
rownames(data_tran) <- full_lineage$TaxaName
# This final table to track taxonomy and feature IDs. This way I can always map back data based on unique taxon IDs
feature_taxonomy_labels <- cbind(feature_taxonomy, labels, lineage_OTU)
feature_taxonomy_labels
})
data_long_react <- reactive({
data_tran <- data_tran_contam_filt_react()
feature_taxonomy_labels <- data_labels_react()
## Filter the table so that it will only have numerical data
cols_to_filter <-
c("OTU.ID",
"Consensus.Lineage",
"ReprSequence",
"rowID",
"Feature.ID")
filtered_table <-
data_tran[,!(names(data_tran) %in% cols_to_filter)]
# Replace any NAs with zeros
filtered_table[is.na(filtered_table)] <- 0
if (input$remove_low_reads == TRUE){
filtered_table[filtered_table < input$read_threshold] <- 0
filtered_table
}
## Convert table into % abundance
data_prop <- filtered_table / colSums(filtered_table)
## Add full taxonomy into prop table and reassign row names to their truncated names:
data_prop <- as.data.frame(data_prop)
data_prop$Taxonomy <- rownames(data_prop)
rownames(data_prop) <- feature_taxonomy_labels$TaxaName
## Convert to data frame and change row names to ASVs/Taxa:
data_prop <- as.data.frame(data_prop)
data_prop_taxa <- data_prop
## Add TaxaName column for sorting in later graphs. Also formats the Taxonomy names:
data_prop$TaxaName <- feature_taxonomy_labels$TaxaName
data_prop$Taxonomy <- gsub("(D_.__)", "", data_prop$Taxonomy)
data_prop$Taxonomy <- gsub(";$", "", data_prop$Taxonomy)
## Convert to long form data frame
data_long <-
reshape2::melt(
data_prop,
id.vars = c("TaxaName", "Taxonomy"),
variable.name = as.character("SampleName"),
value.name = "Percentage"
)
data_long
})
output$proc_main_alt <- renderDataTable({
data_long <- data_long_react()
output$proc_alttext <- renderText("This is your processed data")
data_long
})
################# New Bubble table generation ###################
data_unfiltered_table <- reactive({
main_datafile <- main_datafile()
data_tran <- data_tran_contam_filt_react()
# req(input$main_file)
# req(input$meta_file)
if (input$is_main_collapsed == TRUE) {
labels <- data_tran$Consensus.Lineage
labels <- paste(labels, data_tran$Feature.ID, sep = "_")
} else {
labels <- data_tran$Consensus.Lineage
labels <- paste(labels, data_tran$rowID, sep = "_")
}
## Simple taxonomy corrections
data_tran$Consensus.Lineage <-
gsub(" ", "", data_tran$Consensus.Lineage)
## Cleans up labels and removes uncultured/ambiguous taxa
labels <- gsub("_[0-9]*$", "", labels)
labels <- gsub(" ", "", labels)
labels <- gsub("(;Ambiguous__taxa)", ";s__Ambiguous_taxa", labels)
labels <- gsub("(;Ambiguous_taxa)", ";s__Ambiguous_taxa", labels)
if (input$truncate_taxa == "Yes") {
labels <- paste(";", sep = "", labels)
labels <- gsub("(;\\s*Ambiguous_taxa)", "", labels)
labels <- gsub("(uncultured.*)", "", labels)
labels <- gsub("(__uncultured.*)", "", labels)
labels <- gsub("(unidenti.*)", "", labels)
labels <- gsub("(__unidenti.*)", "", labels)
labels <- gsub("(;.__Ambiguous_taxa)", "", labels)
labels <- gsub("(;._Ambiguous_taxa)", "", labels)
labels <- gsub("(;s__$)", "", labels)
labels <- gsub("(;g__$)", "", labels)
}
## Remove taxa pre-fixes associated with SILVA classifier (old):
if (input$remove_prefix == "Yes") {
labels <- gsub("(D_.__)", "", labels)
labels <- gsub(";$", "", labels)
}
labels <- gsub("(D_.__$)", "", labels)
## Remove taxa-prefixed associated with SILVA classifier (new):
if (input$remove_prefix == "Yes") {
labels <- gsub("(d__)", "", labels)
labels <- gsub("(p__)", "", labels)
labels <- gsub("(c__)", "", labels)
labels <- gsub("(o__)", "", labels)
labels <- gsub("(f__)", "", labels)
labels <- gsub("(g__)", "", labels)
labels <- gsub("(s__)", "", labels)
labels <- gsub(" ", "", labels)
labels <- gsub("(;metagenome$)", "", labels)
labels <- gsub("(__.$)", "", labels)
labels <- gsub("(;__)", "", labels)
labels <- gsub("(;$)", "", labels)
labels <-
gsub("(;$)", "", labels) #This is not a duplicate -- leave it here
}
## ## Retrieves the last taxonomy entry (i.e., genus, species):
full_lineage <-
as.data.frame(paste(data_tran$Consensus.Lineage, data_tran$rowID, sep = "_"))
lineage_OTU <-
as.data.frame(paste(gsub(".*;", "", labels), data_tran$rowID, sep = "_"))
colnames(lineage_OTU) <- "TaxaName"
colnames(full_lineage) <- "TaxaName"
rownames(data_tran) <- full_lineage$TaxaName
## Keep this unfiltered
unfiltered_table <- data_tran
unfiltered_table
})
## This filters the table to prepare it for prop table functions. This must be a separate reactive so that the reprsequences can be maintained
data_filtered_table <- reactive({
unfiltered_table <- data_unfiltered_table()
cols_to_filter <-
c("OTU.ID",
"rowID",
"Consensus.Lineage",
"ReprSequence",
"rowID",
"Feature.ID")
filtered_table <-
unfiltered_table[,!(names(unfiltered_table) %in% cols_to_filter)]
if (input$remove_low_reads == TRUE){
filtered_table[filtered_table < input$read_threshold] <- 0
filtered_table
}
filtered_table
})
data_filtered_table_abridged_names_re <- reactive({
unfiltered_table <- data_unfiltered_table()
})
############ Total read plot ############
## Read table metadata options
observeEvent(input$meta_file,{
req(input$meta_file)
## Incorporate the meta file column names into every appropriate input
meta_datafile <- meta_datafile_og()
meta_colnames <- colnames(meta_datafile)
# meta_colnames <- c(colnames(meta_datafile), "TaxaName")
meta_colnames <- meta_colnames[meta_colnames != "SampleName"]
## Update the selections - read plot
# isolate({
updateSelectInput(session, "read_sortby_axis", choices = sort(meta_colnames))
updateSelectInput(session, "read_meta_group", choices = sort(meta_colnames))
# updateSelectInput(session,"read_meta_key",choices = read_meta_list)
updateSliderInput(session, "read_plot_out_w")
# updateSelectInput(session,"read_colour",choices = colnames(meta_datafile()))
updateSelectInput(session, "read_sortby_axis", choices = sort(meta_colnames))
updateSelectInput(session, "read_meta_group", choices = sort(meta_colnames))
updateSliderInput(session, "read_plot_out_w")
# })
})
#Transform the raw ASV data into counts per sample
data_read_table <- reactive({
req(input$read_start)
# if (read_start()) {
# isolate({
filtered_table <- data_filtered_table()
meta_data_table <- meta_datafile()
data_read <- filtered_table
data_read <- rbind(data_read, Total = colSums(data_read))
data_read_total <- data_read["Total", ]
data_read_total <-
reshape2::melt(
data_read_total,
variable.name = as.character("SampleName"),
value.name = "Total"
)
## Add metadata columns
data_read_total <-
left_join(data_read_total,
meta_data_table,
by = "SampleName",
copy = FALSE)
## Reorder x-axis to follow metadata category in data frame
data_read_total$SampleName <-
as.character(data_read_total$SampleName)
data_read_total$SampleName <-
factor(data_read_total$SampleName,
levels = unique(data_read_total$SampleName))
## Make a list of unique metadata
read_meta_list <- unique(data_read_total$input$read_meta_group)
read_meta_list
data_read_total
# })
})
#Generate the read plot
data_read_plot <- reactive({
req(input$read_start)
# if (read_start()) {
# isolate({
data_read_total <- data_read_table()
if (isolate(input$box_select) == "Bar") {
read_plot <- ggplot(data = data_read_total,
aes(
x = SampleName,
y = Total,
width = isolate(input$read_width)
))
read_plot <-
read_plot + geom_bar(
# aes(fill = as.factor(
# eval(
# parse(text = paste(
# "data_read_total$", isolate(input$read_sortby_axis)
# ))))
# ),
fill = "#D3D3D3",
colour = "black",
size = 0.4,
alpha = 0.8,
stat = "identity",
position = "stack"
)
read_plot
}
if (isolate(input$box_select) == "Box") {
read_plot <- ggplot(data = data_read_total,
#aes(x=as.factor(input$read_sortby_axis), y=Total, width = input$read_width))
aes(
x = as.factor(isolate(input$read_sortby_axis)),
y = Total,
width = 2
))
read_plot <- read_plot + geom_boxplot(
# aes(fill = as.factor(eval(
# parse(text = paste(
# "data_read_total$", isolate(input$read_sortby_axis)
# ))))),
fill = "#D3D3D3",
position = "dodge2",
alpha = 0.8,
# size = input$read_width,
size = 0.4,
outlier.shape = NA
)
read_plot <- read_plot + stat_summary(
fun = mean,
geom = "point",
shape = 15,
size = 1,
colour = "red"
)
read_plot <- read_plot + geom_jitter()
read_plot
}
## setting the graph so that it begins at the x-axis and there is no gap. Also sets the limits of the y-axis.
read_plot <- read_plot + scale_y_continuous(expand = c(0, 0),
limit = (c(0, isolate(input$read_yaxis_limit))))
## Add faceting for sorting
read_plot <- read_plot +
facet_grid(
~ eval(parse(text = isolate(input$read_sortby_axis))),
space = "free",
scales = "free",
switch = "both"
)
if (isolate(input$read_panel) == "Yes") {
read_plot <- read_plot + theme_bw() +
theme(
panel.grid = element_blank(),
text = element_text(colour = "black"),
#axis.line = element_line(colour = "black"),
axis.line = element_blank(),
axis.text = element_text(colour = "black", size = 12),
axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 0.5,
size = 12
),
legend.text = element_text(face = "italic", size = 12),
legend.title = element_text(size = 14),
panel.spacing = isolate(unit(as.numeric(input$read_panel_spacing), "points")),
#legend.position = "none",
axis.title = element_text(size = 14, face = NULL),
axis.text.y = element_text(size = 16),
strip.text.x = element_text(size = 10, face = "bold"),
strip.background = element_rect(fill = "white"),
)
}
if (isolate(input$read_panel) == "No") {
read_plot <- read_plot + theme_bw() +
theme(
panel.grid = element_blank(),
text = element_text(colour = "black"),
axis.line = element_line(colour = "black"),
axis.text = element_text(colour = "black", size = 12),
axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 0.5,
size = 12
),
legend.text = element_text(face = "italic", size = 12),
legend.title = element_text(size = 14),
panel.border = element_blank(),
panel.spacing = isolate(unit(as.numeric(input$read_panel_spacing), "points")),
#legend.position = "none",
axis.title = element_text(size = 14, face = NULL),
axis.text.y = element_text(size = 16),
strip.background = element_rect(fill = "white"),
strip.text.x = element_text(size = 10, face = "bold")
)
}
read_plot <- read_plot + labs(fill = "Sample category")
read_plot <- read_plot + xlab("Samples")
read_plot <- read_plot + ylab("Total reads following DADA2")
read_plot
})
output$read_table_out <- renderDataTable({
req(input$read_start)
read_table <- data_read_table()
read_table
})
## You must define the input for width/height within a reactive context, then call it in the output
read_plot_height = reactive(input$read_plot_out_h)
read_plot_width = reactive(input$read_plot_out_w)
output$read_plot = renderPlot({
read_plot = data_read_plot()
read_plot
},
width = read_plot_width,
height = read_plot_height)
# Download output for read plot
output$read_download = downloadHandler(
filename = "read_plot.pdf",
contentType = ".pdf",
content = function(read_file) {
ggsave(
read_file,
plot = data_read_plot(),
device = "pdf",
height = as.numeric(input$read_plot_out_h),
width = as.numeric(input$read_plot_out_w),
units = "px",
scale = 4
)
}
)
# Download the table for the read plot
output$read_table_download <- downloadHandler(
filename = "read_table.csv",
content = function(bubble_table) {
write.csv(data_read_table(), bubble_table)
})
#### Taxonomic Bar Plot ####
## Update selection - Bar plot
observeEvent(input$meta_file,{
req(input$meta_file)
## Incorporate the meta file column names into every appropriate input
meta_datafile <- meta_datafile_og()
meta_colnames <- colnames(meta_datafile)
# meta_colnames <- c(colnames(meta_datafile), "TaxaName")
meta_colnames <- meta_colnames[meta_colnames != "SampleName"]
## Update the selections - read plot
# isolate({
updateSelectInput(session, "bar_sortby_xaxis", choices = sort(meta_colnames))
updateSelectInput(session, "bar_taxon_level")
updateTextInput(session, "bar_plotheight")
updateTextInput(session, "bar_plotwidth")
updateSelectInput(session, "bar_second_facet", choices = sort(meta_colnames))
})
#
data_long_bar_filt_re <- reactive({
filtered_table <- data_filtered_table()
bar_data_prop <- data_filtered_table()
meta_data_table <- meta_datafile()
unfiltered_table <- data_unfiltered_table()
req(input$bar_start)
## Initial transformations:
## Produce a prop table and filter it to a specific threshold
bar_data_prop <- prop.table(as.matrix(bar_data_prop), 2) * 100
bar_data_prop <- as.data.frame(bar_data_prop)
## If the table contains any columns with zero reads, the column reports NA, and the script fails after this point.
## These columns must be removed first.
bar_data_prop <- bar_data_prop[, colSums(is.na(bar_data_prop)) == 0]
bar_data_prop <- bar_data_prop %>% filter_all(any_vars(. >= as.numeric(isolate(input$bar_cutoff))))
## Reassigns taxonomy to the filtered table
bar_data_prop$Taxonomy <- rownames(bar_data_prop)
bar_data_prop$TaxaName <- rownames(bar_data_prop)
## Collect the ASV IDs before mutating labels:
bar_rowIDs <-
as.data.frame(gsub(".*_", "", bar_data_prop$Taxonomy))
colnames(bar_rowIDs) <- "rowIDs"
## Right now I have to redo the labels since I haven't figured out a way to exclude them from filter step. Until then:
## Cleans up labels and removes uncultured/ambiguous taxa
bar_labels <- bar_data_prop$Taxonomy
bar_labels <- gsub("_[0-9]*$", "", bar_labels, )
bar_labels <-
gsub("(;Ambiguous__taxa)", ";s__Ambiguous_taxa", bar_labels)
bar_labels <-
gsub("(;Ambiguous_taxa)", ";s__Ambiguous_taxa", bar_labels)
bar_labels <- gsub(" ", "", bar_labels)
if (input$truncate_taxa == "Yes") {
bar_labels <- paste(";", sep = "", bar_labels)
bar_labels <- gsub("(;\\s*Ambiguous_taxa)", "", bar_labels)
bar_labels <- gsub("(uncultured.*)", "", bar_labels)
bar_labels <- gsub("(__uncultured.*)", "", bar_labels)
bar_labels <- gsub("(unidenti.*)", "", bar_labels)
bar_labels <- gsub("(__unidenti.*)", "", bar_labels)
bar_labels <- gsub("(;.__Ambiguous_taxa)", "", bar_labels)
bar_labels <- gsub("(;._Ambiguous_taxa)", "", bar_labels)
bar_labels <- gsub("(;s__$)", "", bar_labels)
bar_labels <- gsub("(;g__$)", "", bar_labels)
}
## Remove taxa pre-fixes associated with SILVA classifier (old):
if (input$remove_prefix == "Yes") {
bar_labels <- gsub("(D_.__)", "", bar_labels)
bar_labels <- gsub(";$", "", bar_labels)
}
bar_labels <- gsub("(;D_.__$)", "", bar_labels)
## Remove taxa-prefixed associated with SILVA classifier (new):
if (input$remove_prefix == "Yes") {
bar_labels <- gsub("(d__)", "", bar_labels)
bar_labels <- gsub("(p__)", "", bar_labels)
bar_labels <- gsub("(c__)", "", bar_labels)
bar_labels <- gsub("(o__)", "", bar_labels)
bar_labels <- gsub("(f__)", "", bar_labels)
bar_labels <- gsub("(g__)", "", bar_labels)
bar_labels <- gsub("(s__)", "", bar_labels)
bar_labels <- gsub(" ", "", bar_labels)
bar_labels <- gsub("(;metagenome$)", "", bar_labels)
bar_labels <- gsub("(__.$)", "", bar_labels)
bar_labels <- gsub("(;__)", "", bar_labels)
bar_labels <- gsub("(;$)", "", bar_labels)
bar_labels <-
gsub("(;$)", "", bar_labels) #This is not a duplicate -- leave it here
}
## Further refining labels.
bar_labels <- gsub(" ", "", bar_labels)
bar_labels <- gsub("(__.$)", "", bar_labels)
bar_labels <- gsub("(;$)", "", bar_labels)
bar_labels <- gsub("(;__)", "", bar_labels)
#bar_labels<-gsub("(_)","",bar_labels)
bar_labels <- gsub(".*;", "", bar_labels)
bar_labels <-
paste(gsub(".*;$", "", bar_labels), bar_rowIDs$rowIDs, sep = "_")
bar_labels <- as.data.frame(bar_labels, colnames("labels"))
colnames(bar_labels) <- "labels"
#bar_labels<-gsub("([:digit:]$)","_",bar_labels)
## Apply the labels to the new proportion table:
bar_data_prop$TaxaName <- bar_labels$labels
rownames(bar_data_prop) <- bar_data_prop$TaxaName
## Transform into long form:
bar_data_long <-
reshape2::melt(
bar_data_prop,
id.vars = c("TaxaName", "Taxonomy"),
variable.name = as.character("SampleName"),
value.name = "Percentage"
)
## Add representative sequences back into the data by merging tables based on taxonomy. If collapsed, skips.
unfiltered_table$Taxonomy <- rownames(unfiltered_table)
if (input$is_main_collapsed == FALSE) {
unfiltered_table <- unfiltered_table[, c("Taxonomy", "ReprSequence")]
}
bar_data_long <- left_join(bar_data_long,
unfiltered_table,
by = "Taxonomy",
copy = TRUE)
## Modify taxonomy names
bar_data_long$Taxonomy <-
gsub("(D_.__)", "", bar_data_long$Taxonomy)
bar_data_long$Taxonomy <- gsub(";$", "", bar_data_long$Taxonomy)
## Filter above a threshold, append metadata, and round decimals -- I think this needs to be moved after the filtering
data_long_bar <- bar_data_long
data_long_bar <- dplyr::filter(data_long_bar, Percentage > 0)
data_long_bar <-
left_join(data_long_bar,
meta_data_table,
by = "SampleName",
copy = TRUE)
## Attach full taxonomic lineage and separate into classifications, and fix labelling issues:
data_long_bar$Sep <- data_long_bar$Taxonomy