-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
1007 lines (868 loc) · 31.8 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
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
# get number of generations from trace files
# read all tracefiles
get.ngen <- function(tracefile) {
tracefilelist <- lapply(tracefile$datapath, data.table::fread,
sep = "\t",
header = TRUE
)
# determine shortest trace file
ngen <- min(unlist(lapply(tracefilelist, nrow)))
return(ngen)
}
# read and re-format the trace files
read.trace <- function(tracefile, chain.names) {
({
tracefilelist <- lapply(tracefile$datapath, data.table::fread,
sep = "\t",
header = TRUE,
)
# add chain name as parameter
tracefilelist <- Map(cbind, tracefilelist, trace = chain.names)
lapply(tracefilelist, as.data.frame)
})
}
# trace thinning
thin.trace <- function(trace, trace.thin) {
trace <- trace[seq(from = 0, to = nrow(trace), by = trace.thin), ]
trace <- select(trace, -matches("time|topo"))
return(trace)
}
# remove burnin from trace
burn.trace <- function(trace, burnin) {
trace <- trace[burnin + 1:nrow(trace), ]
return(trace)
}
# merge all trace files into dataframe
tracelist.as.df <- function(tracelist) {
traceDF <- do.call("rbind", tracelist)
traceDF <- tidyr::gather(traceDF, variable, value, -trace, -iter, na.rm = TRUE, factor_key = TRUE)
return(traceDF)
}
# chose only the traces that are currently selected
choose.trace <- function(traceDF, which.chain) {
if (length(tracefile$datapath) == 1) {
traceDF1 <- traceDF
}
if (length(tracefile$datapath) > 1) {
traceDF1 <- filter(traceDF, trace %in% which.chain)
}
# This adds prettier error message in case no trace file is selected
validate(
need(nrow(traceDF1) > 0, "Please select at least one trace file!")
)
return(traceDF1)
}
# set colors for trace file plotting
set.trace.colors <- function(tracedata) {
traces <- unique(tracedata$trace)
colorvector <- c("#377eb8", "#ff7f00", "#4daf4a", "#984ea3")
colorvector <- colorvector[1:length(traces)]
names(colorvector) <- traces
return(colorvector)
}
# plotting functions
# Trace Plot
xyplot <- function(traceDF, trace.colors, trace.theme, trace.style, facet.col, cex) {
tP1 <- ggplot(traceDF, aes(y = value, x = iter, fill = trace)) +
facet_wrap(~variable, scales = "free", ncol = facet.col) +
scale_color_manual(values = trace.colors)
# This adds points to XY plots only if this option was chosen in check box
if ("points" %in% trace.style) {
tP1 <- tP1 + geom_point(data = traceDF, aes(y = value, x = iter, color = trace), size = cex)
}
# This adds lines to XY plots only if this option was chosen in check box
if ("lines" %in% trace.style) {
tP1 <- tP1 + geom_line(data = traceDF, aes(y = value, x = iter, color = trace), size = cex / 2)
}
return(tP1 + trace.theme)
}
# Violin plot
violinplot <- function(traceDF, trace.colors, trace.theme, facet.col, cex, violinplot.style) {
vP1 <- ggplot(traceDF, aes(y = value, x = trace, fill = trace)) +
facet_wrap(~variable, scales = "free", ncol = facet.col) +
scale_fill_manual(values = trace.colors) +
guides(scale_color_manual())
# Users can add box plots and/or data points to violin plot
# Data points must always be first layer, so each combination of violin/box plot/points is iterated below
# No points, no box plots
if (!"boxplot" %in% violinplot.style & !"points" %in% violinplot.style) {
vP1 <- vP1 +
geom_violin(trim = TRUE, alpha = 0.5, color = NA)
}
# With points, no box plots
if (!"boxplot" %in% violinplot.style & "points" %in% violinplot.style) {
vP1 <- vP1 +
geom_jitter(height = 0, width = 0.1, alpha = 0.2, color = "gray", show.legend = FALSE) +
geom_violin(trim = TRUE, alpha = 0.5, color = NA)
}
# No points, with box plots
if ("boxplot" %in% violinplot.style & !"points" %in% violinplot.style) {
vP1 <- vP1 +
geom_violin(trim = TRUE, alpha = 0.5, color = NA) +
geom_boxplot(fill = NA, width = 0.2, color = "darkgray", outlier.shape = NA, size = cex / 2)
}
# With points and box plots
if ("points" %in% violinplot.style & "boxplot" %in% violinplot.style) {
vP1 <- vP1 +
geom_jitter(height = 0, width = 0.1, alpha = 0.2, color = "gray", show.legend = FALSE) +
geom_violin(trim = TRUE, alpha = 0.5, color = NA) +
geom_boxplot(fill = NA, width = 0.2, color = "darkgray", outlier.shape = NA, size = cex / 2)
}
return(vP1 + trace.theme + theme(axis.text.x = element_blank()))
}
# density plot
densityplot <- function(traceDF, trace.colors, trace.theme, facet.col) {
dP1 <- ggplot(traceDF) +
geom_density(aes(x = value, fill = trace), alpha = 0.5, size = 0) +
facet_wrap(~variable, scales = "free", ncol = facet.col) +
scale_fill_manual(values = trace.colors) +
trace.theme +
theme(axis.text.y = element_blank())
return(dP1)
}
# Big function to calculate all of the summary statistics
calc.sum.stats <- function(trace.table) {
# spread the trace file
traceDF <- spread(trace.table, variable, value)
tracenames <- unique(traceDF$trace)
# calculate means, sds, and ess for all numeric columns
Mean <- summarise_if(traceDF, is.numeric, mean)
SD <- summarise_if(traceDF, is.numeric, sd)
# same for ess (here, calculate ESS for each trace separately and then add values)
ess <- group_by(traceDF, trace) %>%
summarize_if(is.numeric, effectiveSize) %>%
select(-trace) %>%
summarise_all(sum)
# calculate 95% HPD intervals
hpd <- HPDinterval(mcmc(select(traceDF, -trace)), prob = 0.95)
hpd <- as.data.frame(hpd)
hpd <- hpd %>%
mutate(lower = round(lower, 2)) %>%
mutate(upper = round(upper, 2))
names(hpd) <- c("95% HPD lower", "95% HPD upper")
# calculate geweke
geweke_res <- list()
chainlengths <- vector()
for (i in 1:length(tracenames)) {
traceDF1 <- traceDF %>%
filter(trace == tracenames[i]) %>%
select(-trace)
chainlengths[i] <- nrow(traceDF1)
geweke <- coda::geweke.diag(coda::as.mcmc(traceDF1))[[1]] # first item in geweke.diag result are the actual values
geweke <- data.frame(abs(geweke))
names(geweke) <- paste("Geweke", tracenames[i], sep = " ")
geweke_res[[i]] <- geweke
}
geweke_all <- do.call("cbind", geweke_res)
min_chainlength <- min(chainlengths)
# merge means, sd, ess, and geweke into results data frame
results <- data.frame(t(rbind(Mean, SD, ess)))
names(results) <- c("Mean", "SD", "ESS")
results <- cbind(results, hpd)
results <- dplyr::select(results, -SD, -ESS, SD, ESS)
results <- cbind(results, geweke_all)
# remove 'iter' & 'time' variables
results <- results[2:nrow(results), ]
# calculate discrepancy according to phylobayes manual
if (length(tracenames) > 1) {
# first, get means and sd for each variable and each chain separately
means <- group_by(traceDF, trace) %>%
summarize_if(is.numeric, mean) %>%
select(-trace)
sds <- group_by(traceDF, trace) %>%
summarize_if(is.numeric, sd) %>%
select(-trace)
# then calculate discrepancy for 2 chains
if (length(tracenames) == 2) {
Discrepancy <- 2 * abs(means[1, ] - means[2, ]) / (sds[1, ] + sds[2, ])
Discrepancy <- data.frame(t(Discrepancy[2:length(Discrepancy)]))
}
# for 3 & 4 chains, calculate discrepancy between any 2 chains, use average of these values
if (length(tracenames) == 3) {
Discrepancy <- 2 * abs(means[1, ] - means[2, ]) / (sds[1, ] + sds[2, ])
Discrepancy[2, ] <- 2 * abs(means[1, ] - means[2, ]) / (sds[1, ] + sds[2, ])
Discrepancy[3, ] <- 2 * abs(means[2, ] - means[3, ]) / (sds[2, ] + sds[3, ])
Discrepancy <- dplyr::summarize_all(Discrepancy, mean)
Discrepancy <- data.frame(t(Discrepancy[2:length(Discrepancy)]))
}
if (length(tracenames) == 4) {
Discrepancy <- 2 * abs(means[1, ] - means[2, ]) / (sds[1, ] + sds[2, ])
Discrepancy[2, ] <- 2 * abs(means[1, ] - means[3, ]) / (sds[1, ] + sds[3, ])
Discrepancy[3, ] <- 2 * abs(means[1, ] - means[4, ]) / (sds[1, ] + sds[4, ])
Discrepancy[4, ] <- 2 * abs(means[2, ] - means[3, ]) / (sds[2, ] + sds[3, ])
Discrepancy[5, ] <- 2 * abs(means[2, ] - means[4, ]) / (sds[2, ] + sds[4, ])
Discrepancy[6, ] <- 2 * abs(means[3, ] - means[4, ]) / (sds[3, ] + sds[4, ])
Discrepancy <- dplyr::summarize_all(Discrepancy, mean)
Discrepancy <- data.frame(t(Discrepancy[2:length(Discrepancy)]))
}
# add row name and merge with results vector
names(Discrepancy) <- "Discrepancy"
results <- cbind(results, Discrepancy)
# create list of mcmc objects, 1 for each trace file
tracelist <- list()
for (j in 1:length(tracenames)) {
tracex <- traceDF %>%
filter(trace == tracenames[j]) %>%
select(-trace)
tracelist[[j]] <- coda::mcmc(tracex[1:min_chainlength, 2:ncol(tracex)])
}
# Calculate Gelman & Rubin, extract point estimates and ci, rename, and combine with results data frame
gel.res <- coda::gelman.diag(tracelist, autoburnin = FALSE, multivariate = FALSE)
gel.point <- data.frame(gel.res$psrf[, 1])
gel.ci <- data.frame(gel.res$psrf[, 2])
names(gel.point) <- "GR point estimate"
names(gel.ci) <- "GR 95% CI"
gel.point <- tibble::rownames_to_column(gel.point)
gel.ci <- tibble::rownames_to_column(gel.ci)
results <- tibble::rownames_to_column(results)
results <- dplyr::full_join(results, gel.point, by = "rowname")
results <- dplyr::full_join(results, gel.ci, by = "rowname")
row.names(results) <- results$rowname
results <- results[, 2:ncol(results)]
}
# for the numeric values in data frame, round using 2 decimals
is.num <- sapply(results, is.numeric)
results[is.num] <- lapply(results[is.num], round, 2)
return(results)
}
# style data table
# call data frame with DT::data table to enable nice formatting
style.table <- function(table, trace.table) {
styled_table <- DT::datatable(table,
selection = "none",
extensions = "Buttons",
options = list(
searching = FALSE,
ordering = FALSE,
orientation = "landscape",
pageLength = nrow(table),
dom = "Bt",
buttons = c("copy", "csv", "print")
)
) %>%
DT::formatStyle("ESS", color = styleInterval(99.99, c("red", "black"))) %>%
DT::formatStyle(names(table)[grep("Geweke", names(table))], color = styleInterval(2, c("black", "red"))) %>%
DT::formatStyle(0, fontWeight = "bold")
if (length(unique(trace.table$trace)) > 1) {
styled_table <- styled_table %>%
DT::formatStyle(c("GR point estimate", "GR 95% CI"), color = styleInterval(1.2, c("black", "red"))) %>%
DT::formatStyle("Discrepancy", color = styleInterval(0.3, c("black", "red")))
}
return(styled_table)
}
# set tree display options
set.tree.opts <- function(tree.opts) {
treeopts <- vector()
if ("align" %in% tree.opts) {
treeopts[1] <- TRUE
}
if ("ignore" %in% tree.opts) {
treeopts[2] <- FALSE
}
if (!("ignore" %in% tree.opts)) {
treeopts[2] <- TRUE
}
if (!("align" %in% tree.opts)) {
treeopts[1] <- FALSE
}
return(treeopts)
}
# set tree label font
set.tree.font <- function(tree.font) {
treefont <- vector(mode = "numeric")
if (is.null(tree.font)) {
treefont <- 1
} else {
if (length(tree.font) == 1) {
if (tree.font == "bold") {
treefont <- 2
}
if (tree.font == "italic") {
treefont <- 3
}
}
if (length(tree.font) == 2) {
treefont <- 4
}
}
return(treefont)
}
# create a dataframe of tip labels
# first get the tip labels and order them according to their appearance in plot (1= bottom taxon, length(tiplabels)=top taxon)
# get this info from tree$edge[,2] all numbers < ntaxa(tree) correspond to tip labels, order is as plotted
get.tip.df <- function(tree) {
# which edges belong to tips?
is_tip <- tree$edge[, 2] <= length(tree$tip.label)
# order according
ordered_tips <- tree$edge[is_tip, 2]
# now just reorder the tip labels, and add consecutive numbering as 2nd row in that dataframe
tips <- as.data.frame(tree$tip.label[ordered_tips])
tiporder <- as.data.frame(1:length(tree$tip.label))
tipDF <- as.data.frame(cbind(tips, tiporder))
names(tipDF) <- c("tips", "tiporder")
# call dataframe
return(tipDF)
}
# read trees
# only update tree format when new files are uploaded
read.treefiles <- function(treefile) {
if (example$click == 1) {
treeformat <- "Newick (e.g., Phylobayes)"
}
if (example$click == 2) {
treeformat <- "Nexus (e.g., MrBayes)"
}
if (example$click == 0) {
treeformat <- input$treefiletype
}
treelist <- list()
for (i in 1:length(treefile$datapath)) {
treepath <- treefile$datapath[i]
if (treeformat == "Newick (e.g., Phylobayes)") {
firstline <- readLines(treepath, n = 1)
validate(
# add very simple check to make sure file IS NOT nexus format
need(!is.element("#Nexus", firstline), "Error reading file(s). Please check format."),
need(!is.element("#NEXUS", firstline), "Error reading file(s). Please check format."),
need(!is.element("#nexus", firstline), "Error reading file(s). Please check format.")
)
treelist[[i]] <- ape::read.tree(treepath)
}
if (treeformat == "Nexus (e.g., MrBayes)") {
# add very simple check to make sure file IS nexus format
firstline <- readLines(treepath, n = 1)
validate(
need(any(firstline == c("#Nexus", "#NEXUS", "#nexus")), "Error reading file(s). Please check format.")
)
treelist[[i]] <- ape::read.nexus(treepath)
}
}
return(treelist)
}
# tree thinning
thin.trees <- function(treelist, treethin) {
thinlist <- list()
for (i in 1:length(treelist)) {
thinlist[[i]] <- treelist[[i]][seq(from = 1, to = length(treelist[[i]]), by = treethin)]
}
return(thinlist)
}
# combine all trees into single multiphylo object
combine.trees <- function(alltrees, burnin) {
# req(treefile$datapath)
req(length(alltrees) >= 1)
treesall <- list()
class(treesall) <- "multiPhylo"
for (i in 1:length(alltrees)) {
# get trees
trees <- alltrees[[i]]
trees <- trees[(burnin + 1):length(trees)]
if (length(treesall) >= 1) {
treesall <- c(treesall, trees)
}
if (length(treesall) == 0) {
treesall <- trees
}
}
return(treesall)
}
# Collapse nodes below a posterior probability (inspired by http://evoslav.blogspot.com/2015/01/how-to-collapse-unsupported-branches-of.html)
collapse.nodes <- function(con.tree, pp) {
contree <- con.tree
# get position of nodes
collapse_nodes <- which(contree$node.label < pp) + length(contree$tip.label)
# get index of edges from these nodes
collapse_indexes <- which(contree$edge[, 2] %in% collapse_nodes)
# assign 0 branch length
contree$edge.length[collapse_indexes] <- 0
# use di2multi to collpase 0 branch lengths
# important: use tiny number for tol in order for short branches with high pp not to be collapsed
contree <- di2multi(contree, tol = 1e-10000)
# remove support values for collapsed nodes
contree$node.label[contree$node.label < pp] <- ""
return(contree)
}
# calculate consensus tree
calc.cons <- function(treelist) {
# get consensus branch lengths
contree <- phytools::consensus.edges(treelist,
method = "least.squares"
)
# and count how often the nodes are present in all trees (=pp) and writes this as node labels to the tree
sv <- prop.clades(contree, treelist)
contree$node.label <- sv / length(treelist)
contree$node.label <- formatC(contree$node.label, digits = 2, format = "f") # 2 decimals for pp values
# adjust node labels: 1) remove "root" label
contree$node.label[contree$node.label == "Root"] <- ""
# convert to numeric
contree$node.label <- as.numeric(contree$node.label)
return(contree)
}
# Root the tree
root.tree <- function(con.tree, og) {
# unroot
if ("<None>" %in% og) {
con.tree <- unroot(con.tree)
}
# midpoint root
if ("<Midpoint>" %in% og) {
con.tree <- phytools::midpoint.root(con.tree)
}
# root with outgroup
if (!("<Midpoint>" %in% og) & !("<None>" %in% og)) {
validate(
need(is.monophyletic(con.tree, og), "The specified outgroup is not monophyletic!")
)
# determine the node number of the outgroup clade
root.node <- getMRCA(con.tree, tip = og)
# root in the middle of the edge
edge.position <- 0.5 * con.tree$edge.length[which(con.tree$edge[, 2] == root.node)]
con.tree <- reroot(con.tree, root.node, edge.position)
}
# remove 'root label'
con.tree$node.label[con.tree$node.label == "Root"] <- ""
con.tree <- ladderize(con.tree)
return(con.tree)
}
# render plot
render.contree <- function(root.tree, high.col, thin.trees, tree.cex, tree.opts, tree.font, tree.annot) {
# colorvector for tips
col.df <- high.col %>%
arrange(factor(V1, levels = root.tree$tip.label))
concolvec <- as.vector(col.df$V2)
treetot <- sum(unlist(lapply(thin.trees, length)))
# plot
plot(root.tree,
main = paste0(
"Consensus of ", treetot - (input$conburnin * length(thin.trees)), " trees", # number of trees
" (", length(thin.trees), " chains)"
), # chains
cex.main = tree.cex * 1.1,
cex = tree.cex,
align.tip.label = tree.opts[1],
use.edge.length = tree.opts[2],
edge.width = tree.cex,
label.offset = 0.01,
font = tree.font,
tip.color = concolvec
)
nodelabels(
text = root.tree$node.label, # some formatting for the pp values
frame = "none",
adj = 0,
cex = tree.cex * 0.8
)
add.scale.bar(lwd = tree.cex)
# add custom plot annotations
title(
sub = tree.annot,
adj = 0,
line = 1,
font = 2,
cex.sub = tree.cex * 0.85
)
}
# count unique trees with progress indicator
uniq.trees <- function(all.trees) {
progress <- shiny::Progress$new()
on.exit(progress$close())
progress$set(message = "Determining unique topologies, please be patient.", value = 0.5)
uniqtrees <- ape::unique.multiPhylo(all.trees)
progress$set(detail = "DONE!", value = 1)
return(uniqtrees)
}
# determine frequencies of unique trees
topology.freqs <- function(all.trees, uniq.trees) {
progress <- shiny::Progress$new()
on.exit(progress$close())
# Create results vector
treefreq <- vector(mode = "numeric", length = length(all.trees))
# Count occurrences of unique trees
progress$set(message = paste(length(uniq.trees), "unique topologies found!"), value = 0)
foreach(i = 1:length(uniq.trees)) %do% {
treefreq[which(sapply(all.trees, all.equal.phylo, uniq.trees[[i]], use.edge.length = FALSE))] <- i
progress$inc(1 / length(uniq.trees), detail = paste("Calculating frequency for topology no", i))
}
# Prepare results df
treefreq <- as.data.frame(table(treefreq))
treefreq <- treefreq[order(-treefreq$Freq), ]
treefreq$order <- 1:nrow(treefreq)
return(treefreq)
}
# plot frequencies as bar plot
plot.topo.freq <- function(freqdf) {
ggplot(freqdf, aes(x = order, y = Freq)) +
geom_bar(stat = "identity") +
theme_minimal() +
ggtitle(paste(nrow(topfreq()), "unique topologies in", sum(topfreq()$Freq), "trees.")) +
xlab("Unique topology #") +
ylab("Frequency") +
theme(aspect.ratio = 0.3)
}
# plot unique topology with informative titles
render.uniq.topo <- function(currtop, topfreq, tn, high.col, tree.cex, tree.opts, tree.font) {
# colorvector for tips
col.df <- high.col %>%
arrange(factor(V1, levels = currtop$tip.label))
concolvec <- as.vector(col.df$V2)
# Create title
treetitle <- paste0("Topology ", topfreq$order[tn], ": ", topfreq$Freq[tn], "/", sum(topfreq$Freq), " trees")
# plot
plot(currtop,
main = treetitle,
cex.main = tree.cex * 1.1,
cex = tree.cex,
align.tip.label = tree.opts[1],
use.edge.length = tree.opts[2],
edge.width = tree.cex,
label.offset = 0.01,
font = tree.font,
tip.color = concolvec
)
add.scale.bar(lwd = tree.cex)
}
render.singletrees <- function(thin.trees, og, tree.generation, high.col, tree.cex, tree.font, tree.opts, which.tree) {
validate(
need(length(which.tree) > 1,
message = "Provide tree files from at least 2 chains to display differences between consensus trees."
)
)
# Adjusting plot layout
# change plot layout to 2 columns if 2 treefiles are present
if (length(thin.trees) == 2) {
par(mfrow = c(1, 2))
}
# 3 columns if 3 treefiles are present
if (length(thin.trees) == 3) {
par(mfrow = c(1, 3))
}
# and 2x2 for 4 treefiles
if (length(thin.trees) == 4) {
par(mfrow = c(2, 2))
}
# plot all trees in loop
for (i in 1:length(thin.trees)) {
# get trees
trees <- thin.trees[[i]]
# unroot tree if no root was chosen
if ("<None>" %in% og) {
currtree <- unroot(trees[[tree.generation]])
}
# midpoint root if chosen
if ("<Midpoint>" %in% og) {
currtree <- phytools::midpoint.root(trees[[tree.generation]])
}
# root with outgroup if chosen
if (!("<Midpoint>" %in% og) & !("<None>" %in% og)) {
validate(
need(is.monophyletic(trees[[tree.generation]], og), "The specified outgroup is not monophyletic!")
)
root.node <- getMRCA(trees[[tree.generation]], tip = og)
edge.position <- 0.5 * trees[[tree.generation]]$edge.length[which(trees[[tree.generation]]$edge[, 2] == root.node)]
currtree <- reroot(trees[[tree.generation]], root.node, edge.position)
}
# Create tip label color vector from highlight picker options (need to do this after rooting, as this impacts tip label order)
col.df <- high.col %>%
arrange(factor(V1, levels = currtree$tip.label))
concolvec <- as.vector(col.df$V2)
# Plot
plot(ladderize(currtree),
main = paste(which.tree[i], "iteration", tree.generation),
cex = tree.cex,
cex.main = tree.cex * 1.1,
edge.width = tree.cex,
align.tip.label = tree.opts[1],
use.edge.length = tree.opts[2],
label.offset = 0.01,
font = tree.font,
tip.color = concolvec
)
add.scale.bar(lwd = tree.cex)
}
}
render.treediff <- function(thin.trees, all.trees, og, burnin, high.col, tree.cex, tree.font, which.tree) {
validate(
need(!("<Midpoint>" %in% og),
message = "Midpoint rooting not possible for consensus cladograms.\nPlease choose different root!"
)
)
# loop through all trees and create tree list as well as color list
contrees <- list()
colvecs <- list()
for (i in 1:length(thin.trees)) {
tree <- thin.trees[[i]]
tree <- tree[(burnin + 1):length(tree)]
contree <- consensus(tree, p = 0.5)
# reroot outgroup
if (!("<Midpoint>" %in% og) & !("<None>" %in% og)) {
contree <- root(contree, outgroup = og)
}
# unroot tree
if ("<None>" %in% og) {
contree <- unroot(contree)
}
contrees[[i]] <- contree
# colorvector
col.df <- high.col %>%
arrange(factor(V1, levels = contree$tip.label))
colvecs[[i]] <- as.vector(col.df$V2)
}
# change plot layout according to number of chains analysed
if (length(all.trees) == 2) {
par(mfrow = c(1, 2))
}
if (length(all.trees) == 3) {
par(mfrow = c(3, 2))
}
if (length(all.trees) == 4) {
par(mfrow = c(3, 4))
}
# this nested loop plots consensus trees side by side, for all combinations in 2, 3, or 4 trees
for (i in 1:length(thin.trees)) {
for (j in i:length(thin.trees)) {
if (i != j) {
phylo.diff.new(contrees[[i]], contrees[[j]],
cex = tree.cex,
cex.main = tree.cex * 1.1,
label.offset = 0.01,
font = tree.font,
main1 = which.tree[i],
main2 = which.tree[j],
coltip1 = colvecs[[i]],
coltip2 = colvecs[[j]]
)
}
}
}
}
# function that plots 2 trees next to each other and highlights differences
# modified from http://blog.phytools.org/
phylo.diff.new <- function(x, y, main1, main2, coltip1, coltip2, ...) {
uniqT1 <- distinct.edges(x, y)
uniqT2 <- distinct.edges(y, x)
treeA.cs <- rep("black", dim(x$edge)[1])
treeA.cs[uniqT1] <- "#FF1493"
treeA.lw <- rep(input$treecex, dim(x$edge)[1])
treeA.lw[uniqT1] <- input$treecex * 2
treeB.cs <- rep("black", dim(y$edge)[1])
treeB.cs[uniqT2] <- "#FF1493"
treeB.lw <- rep(input$treecex, dim(x$edge)[1])
treeB.lw[uniqT2] <- input$treecex * 2
plot(x, edge.color = treeA.cs, main = main1, tip.color = coltip1, edge.width = treeA.lw, align.tip.label = TRUE, ...)
plot(y, edge.color = treeB.cs, main = main2, tip.color = coltip2, edge.width = treeB.lw, align.tip.label = TRUE, direction = "leftwards", ...)
invisible()
}
# Plot to display RF distances within and between chains
render.rfplot <- function(thin.trees, burnin, which.tree, tree.cex, tree.scalefactor) {
rflist <- list()
for (i in 1:length(thin.trees)) {
tree <- thin.trees[[i]]
tree <- tree[(burnin + 1):length(tree)]
rf <- vector(mode = "numeric")
rf <- foreach(j = 2:length(tree), .packages = "ape", .combine = c) %dopar% {
rf[j - 1] <- dist.topo(tree[j - 1], tree[j])
}
x <- (2 + burnin):(length(tree) + burnin)
RFdf <- as.data.frame(cbind(x, rf))
RFdf$chain <- paste("Iteration n vs. Iteration (n-1),", which.tree[i])
rflist[[i]] <- RFdf
}
RFdf <- do.call("rbind", rflist)
RFdf$difference <- "Tree distance within chain"
# results vector
if (length(thin.trees) > 1) {
rflist3 <- list()
counter <- 0
for (h in 1:length(thin.trees)) {
tree1 <- thin.trees[[h]]
tree1 <- tree1[(burnin + 1):length(tree1)]
for (j in h:length(thin.trees)) {
if (h != j) {
counter <- counter + 1
tree2 <- thin.trees[[j]]
tree2 <- tree2[(burnin + 1):length(tree2)]
minlength <- min(c(length(tree1), length(tree2)))
rf <- foreach(k = 1:minlength, .packages = "ape", combine = c) %dopar% {
dist.topo(tree1[[k]], tree2[[k]])
}
x <- (1 + burnin):(length(rf) + burnin)
RFdf3 <- as.data.frame(cbind(x, rf))
RFdf3$chain <- paste(which.tree[h], which.tree[j], sep = " vs. ")
rflist3[[counter]] <- RFdf3
}
}
}
RFdf3 <- do.call("rbind", rflist3)
RFdf3 <- as.data.frame(lapply(RFdf3, unlist))
RFdf3$difference <- "Tree distance between chains"
RFdf <- rbind(RFdf, RFdf3)
}
RFggplot <- ggplot(data = RFdf, aes(x = x, y = rf, color = chain)) +
geom_line(size = tree.cex / 2) +
theme_light() +
facet_wrap(~difference, nrow = 2, scales = "free_y") +
xlab("Tree generation") +
ylab("Robinson-Foulds distance") +
theme(
axis.title = element_text(size = 12 * tree.scalefactor),
legend.title = element_blank(),
legend.spacing.x = unit(0.2, "cm"),
axis.text = element_text(size = 11 * tree.scalefactor),
legend.position = "bottom",
legend.direction = "vertical",
legend.text = element_text(size = 12 * tree.scalefactor),
strip.text = element_text(size = 14 * tree.scalefactor, face = "bold")
) +
scale_color_manual(values = c(
"#4E79A7", "#F28E2B", "#E15759", "#76B7B2", "#59A14F",
"#EDC948", "#B07AA1", "#FF9DA7", "#9C755F", "#BAB0AC"
))
return(RFggplot)
}
# calculate bipartition support for any clade
calc.bpsupport <- function(selected.tax, tip.names, thin.trees, which.tree, burnin) {
validate(
need(length(selected.tax) > 1,
message = "Please chose at least two taxa! Note that entering taxon names into the field will overwrite any selection from the drop-down menu."
),
need(length(intersect(selected.tax, tip.names)) == length(selected.tax),
message = "Not all taxon name(s) are present in tree. Please check."
)
)
if (length(thin.trees) > 1) {
req(length(which.tree) > 0)
}
# read in trees in loop
treesall <- list()
for (i in 1:length(thin.trees)) {
# get trees
trees <- thin.trees[[i]]
trees <- trees[(burnin + 1):length(trees)]
if (i == 1) {
treesall <- trees
} else {
treesall <- c(treesall, trees)
}
}
selectedtax <- selected.tax
# check if taxa from the list are monophyletic (for whole list of trees)
bpcount <- vector()
bpcount <- foreach(
i = 1:length(treesall),
.packages = "ape",
combine = c,
.inorder = FALSE
) %dopar% {
is.monophyletic(treesall[[i]], selected.tax)
}
# count number of "TRUE"
monotrue <- sum(unlist(bpcount))
monotrue <- as.numeric(monotrue)
# summarize results in list
bpsupport <- list(
absolute = monotrue, # in how many trees is the group monophyletic
relative = formatC(monotrue / length(treesall) * 100,
digits = 2, format = "f"
), # in percent
total = length(bpcount) # how many trees were analysed
)
return(bpsupport)
}
# simple plot of relationships tested
render.bipartplot <- function(tip.names, selected.tax) {
# get all tipnams, the ones that were selected and extract the non selected ones
alltips <- tip.names
selecttips <- selected.tax
inverttips <- alltips[!alltips %in% selecttips]
# this adds parentheses and commas to all selected and non-selected tip names s
str1 <- paste("(", selecttips, "),", sep = "", collapse = "")
str1 <- substr(str1, 1, nchar(str1) - 1)
str2 <- paste("(", inverttips, "),", sep = "", collapse = "")
str2 <- substr(str2, 1, nchar(str2) - 1)
# to create newick tree, add paretheses around each monophyletic group and final semicolon
bipartition <- paste0("(", str2, "),", "(", str1, ");")
# read tree string
bipartition_plot <- read.newick(text = bipartition)
# make all edge lengths equal (improves display)
bipartition_plot$edge.length <- rep(1, length(bipartition_plot$edge / 2))
# Highlight the edges connecting selected trees in pink
edge.highlight <- which.edge(bipartition_plot, selecttips)
edgecolor <- rep("black", Nedge(bipartition_plot))
edgecolor[edge.highlight] <- "#FF1493"
# Also, increase edge width
edgewidth <- rep(1, Nedge(bipartition_plot))
edgewidth[edge.highlight] <- 2
# and plot
p <- plot(bipartition_plot,
type = "phylogram",
cex = input$treecex,
edge.color = edgecolor,
edge.width = edgewidth * input$treecex
)
return(p)
}
# reformat trees for rwty
prepare.rwty.trees <- function(thin.trees, treethin) {
rwtytrees <- list()
for (i in 1:length(thin.trees)) {
trees <- list()
trees[[1]] <- thin.trees[[i]]
trees[[2]] <- NULL
trees[[3]] <- treethin
names(trees) <- c("trees", "ptable", "gens.per.tree")
class(trees) <- "rwty.chain"
rwtytrees[[i]] <- trees
}
return(rwtytrees)
}
# wrapper for all rwty plots
rwty.wrapper <- function(ncores, tree.scalefactor, rwty.trees) {
# install if not present
validate(
need("rwty" %in% rownames(installed.packages()),
message = "\nPackage rwty not found! Please install."
)
)
library(rwty)
# set number of processors fo rwty calculations
rwty.processors <- ncores
# set theme for all rwty plots
theme_rwty <-
theme_light() +
theme(
axis.title = element_text(size = 12 * tree.scalefactor),
legend.title = element_blank(),
axis.text = element_text(size = 11 * tree.scalefactor),
legend.text = element_text(size = 12 * tree.scalefactor),
title = element_text(size = 14 * tree.scalefactor),
strip.text = element_text(size = 12 * tree.scalefactor, face = "bold")
)
# Autocorrelation
if (input$rwtytype == "Autocorrelation") {
autocorplot <- makeplot.autocorr(rwty.trees)
autocorplot$autocorr.plot + theme_rwty
}
# Split frequencies
else if (input$rwtytype == "Split frequencies") {
cumsplitfreq <- makeplot.splitfreqs.cumulative(rwty.trees)
slidesplitfreq <- makeplot.splitfreqs.sliding(rwty.trees)
grid.arrange(cumsplitfreq$splitfreqs.cumulative.plot + theme_rwty,
slidesplitfreq$splitfreqs.sliding.plot + theme_rwty,
ncol = 1
)
}
# Topology traces
else if (input$rwtytype == "Topology trace") {
topologyplot <- makeplot.topology(rwty.trees)
trace <- topologyplot$trace.plot + theme_rwty
dense <- topologyplot$density.plot + theme_rwty
grid.arrange(trace, dense, ncol = 1)
}
# Tree space