-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentanglement.qmd
488 lines (419 loc) · 15.2 KB
/
entanglement.qmd
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
---
title: "Entanglement"
description: "Assessing recombination history through comparison of subgenomic region phylogenies"
format:
html:
df-print: kable
code-fold: true
code-summary: "Show code"
code-overflow: wrap
toc-title: Page Contents
toc: true
toc-depth: 3
toc-location: right
number-sections: false
html-math-method: katex
smooth-scroll: true
editor: source
editor_options:
chunk_output_type: console
---
```{=html}
<style type="text/css">
body, td {
font-size: 13pt;
}
code.r{
font-size: 9pt;
}
pre {
font-size: 11pt
}
</style>
```
```{r echo=FALSE, warning=FALSE, message=FALSE}
library(knitr)
knit_print.gt <- function(x, ...) {
stringr::str_c(
"<div style='all:initial';>\n",
gt::as_raw_html(x),
"\n</div>"
) |>
knitr::asis_output()
}
registerS3method(
"knit_print", 'gt_tbl', knit_print.gt,
envir = asNamespace("gt")
)
```
```{r warning=FALSE, message=FALSE, echo=FALSE}
library(here)
library(tidyverse)
options(dplyr.summarise.inform = FALSE)
library(ape)
library(ggtree)
library(dendextend)
library(pals)
library(gt)
## Custom Functions
source(here("R/utilities.R"))
source_dir(here("R"))
```
## Read Gene Trees
Segment specific phylogenies. Note that the`_rev` subfix indicates that the original Nexus files were revised to swap tip labels for names in the description block (see, preprocessing)
```{r}
# phylogeny list from directory
tree_files <- list.files(here("local/paktrees/original"), pattern="_rev\\.nex$")
# read ML trees & convert
IRES_Lpro.tree <- read.nexus(here("local/paktrees/original", tree_files[8]))
IRES_Lpro.den <- get_dendro(
extract_subtree(IRES_Lpro.tree))
seg_2C.tree <- read.nexus(here("local/paktrees/original", tree_files[1]))
seg_2C.den <- get_dendro(
extract_subtree(seg_2C.tree))
seg_3A.tree <- read.nexus(here("local/paktrees/original", tree_files[2]))
seg_3A.den <- get_dendro(
extract_subtree(seg_3A.tree))
seg_3C.tree <- read.nexus(here("local/paktrees/original", tree_files[3]))
seg_3C.den <- get_dendro(
extract_subtree(seg_3C.tree))
seg_3D.tree <- read.nexus(here("local/paktrees/original", tree_files[4]))
seg_3D.den <- get_dendro(
extract_subtree(seg_3D.tree))
```
## Entanglement
Entanglement scores range from 0.00 - 1.00 with a values of 0.00 indicating the trees are exactly the same and a score of 1.00 indicating they are opposites. Essentially, the score can be interpreted as a proportion or percent change.
Table to summarize comparisons
```{r}
entangle_tab <- as.data.frame(matrix(nrow = 10, ncol = 2))
names(entangle_tab) <- c("Compared", "Score")
```
#### Select tabs to view comparisons
::: panel-tabset
## IRES_Lpro vs. 2C
```{r fig.width=8, fig.height=10}
dends <- dendlist(IRES_Lpro.den, seg_2C.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[1] <- "IRES_Lpro vs. 2C"
entangle_tab$Score[1] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "IRES_Lpro",
cex_main_left = 4,
main_right = "2C",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## IRES_Lpro vs. 3A
```{r fig.width=8, fig.height=10}
dends <- dendlist(IRES_Lpro.den, seg_3A.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[2] <- "IRES_Lpro vs. 3A"
entangle_tab$Score[2] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "IRES_Lpro",
cex_main_left = 4,
main_right = "3A",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## IRES_Lpro vs. 3C
```{r fig.width=8, fig.height=10}
dends <- dendlist(IRES_Lpro.den, seg_3C.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[3] <- "IRES_Lpro vs. 3C"
entangle_tab$Score[3] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "IRES_Lpro",
cex_main_left = 4,
main_right = "3C",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## IRES_Lpro vs. 3D
```{r fig.width=8, fig.height=10}
dends <- dendlist(IRES_Lpro.den, seg_3D.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[4] <- "IRES_Lpro vs. 3D"
entangle_tab$Score[4] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "IRES_Lpro",
cex_main_left = 4,
main_right = "3D",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## 2C vs. 3A
```{r fig.width=8, fig.height=10}
dends <- dendlist(seg_2C.den, seg_3A.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[5] <- "2C vs. 3A"
entangle_tab$Score[5] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "2C",
cex_main_left = 4,
main_right = "3A",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## 2C vs. 3C
```{r fig.width=8, fig.height=10}
dends <- dendlist(seg_2C.den, seg_3C.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[6] <- "2C vs. 3C"
entangle_tab$Score[6] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "2C",
cex_main_left = 4,
main_right = "3C",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## 2C vs. 3D
```{r fig.width=8, fig.height=10}
dends <- dendlist(seg_2C.den, seg_3D.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[7] <- "2C vs. 3D"
entangle_tab$Score[7] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "2C",
cex_main_left = 4,
main_right = "3D",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## 3A vs. 3C
```{r fig.width=8, fig.height=10}
dends <- dendlist(seg_3A.den, seg_3C.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[8] <- "3A vs. 3C"
entangle_tab$Score[8] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "3A",
cex_main_left = 4,
main_right = "3C",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## 3A vs. 3D
```{r fig.width=8, fig.height=10}
dends <- dendlist(seg_3A.den, seg_3D.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[9] <- "3A vs. 3D"
entangle_tab$Score[9] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "3A",
cex_main_left = 4,
main_right = "3D",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
## 3C vs. 3D
```{r fig.width=8, fig.height=10}
dends <- dendlist(seg_3C.den, seg_3D.den)
entang_score <- round(entanglement(dends), 2)
entangle_tab$Compared[10] <- "3C vs. 3D"
entangle_tab$Score[10] <- entang_score
x <- dends %>% dendextend::untangle(method = "step2side")
x %>% set("rank_branches") %>%
set("branches_lwd", 1.5) %>%
set("branches_col", value = c("gray40")) %>%
tanglegram(main_left = "3C",
cex_main_left = 4,
main_right = "3D",
cex_main_right = 4,
columns_width = c(5,0.5,5),
lwd = 1.8,
lab.cex = 0.8,
dLeaf_left = -0.15,
dLeaf_right = 0.15,
margin_outer = 4,
margin_inner = 10,
color_lines = tol(12),
axes=FALSE,
sub = paste("Entanglement:", entang_score),
common_subtrees_color_branches = FALSE)
```
:::
## Entanglement Scores
```{r}
entangle_tab %>%
gt() %>%
tab_header(
title = md("Score Summary")) %>%
cols_width(everything() ~ px(150)) %>%
tab_options(table.font.size = "small",
row_group.font.size = "small",
stub.font.size = "small",
column_labels.font.size = "medium",
heading.title.font.size = "large",
data_row.padding = px(2),
heading.title.font.weight = "bold",
column_labels.font.weight = "bold") %>%
opt_stylize(style = 6, color = 'gray')
```
## Matrix View
```{r fig.height=6, fig.width=10}
#| label: fig-entangle_scores
#| fig-cap: "Compare entanglement scores."
#|
mat_view <- entangle_tab %>%
separate(Compared, into = c("Var1", "Var2"), sep = " vs. ", remove = FALSE)
mat_view <- mat_view %>%
select(Var1, Var2, Score)
unique_vars <- unique(c(mat_view$Var1, mat_view$Var2))
self_comparisons <- data.frame(
Var1 = unique_vars,
Var2 = unique_vars,
Score = 0 # self-comparisons
)
mat_view <- bind_rows(mat_view, self_comparisons)
mat_view <- bind_rows(
mat_view,
mat_view %>% rename(Var1 = Var2, Var2 = Var1)
)
mat_view <- mat_view %>%
distinct()
mat_view$Var1 <- gsub("_", " ", mat_view$Var1)
mat_view$Var2 <- gsub("_", " ", mat_view$Var2)
ggplot(mat_view, aes(Var1, Var2, fill = Score)) +
geom_tile() +
scale_fill_gradientn(
colors = (pals::ocean.ice(100)[20:100])
) +
theme_classic() +
theme(
plot.margin = unit(c(2, 2, 2, 2), "mm"),
axis.title.x = element_text(size = 24, face = "bold"),
axis.title.y = element_text(size = 24, face = "bold"),
axis.text.x = element_text(size = 23, face = "bold"),
axis.text.y = element_text(size = 23, face = "bold"),
legend.direction = "vertical",
legend.position = "right",
strip.text = element_blank(),
strip.background = element_blank(),
legend.key.size = unit(2, "line"),
legend.key.width = unit(1, "line"),
legend.text = element_text(size = 20, face = "bold"),
legend.title = element_text(size = 25, face = "bold"),
plot.title = element_text(size = 28, face = "bold")
) +
labs(title = "Entanglement Scores",
x = " ", y = " ", fill = "Entanglement")
```