-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtanglegram.Rmd
110 lines (87 loc) · 2.57 KB
/
tanglegram.Rmd
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
---
title: "tanglegram"
author: "Le Viet Thanh"
date: "July 24, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#Library from CRAN
library(ape) #nj tree
library(dendextend) #tanglegram
library(dplyr) #pipe
library(randomcoloR) #random distinct colors
#Library from Bioconductor
library(DECIPHER) #load dendrogram tree
```
## 1. Generating some trees
```{r}
den <- read.nexus.data("dengue.nex") %>% as.DNAbin()
#Estimate distance
den.dist <- dist.dna(den, as.matrix = TRUE)
#bionj
den.tree.bionj <- bionj(den.dist)
#Remove neg branch length
den.tree.bionj$edge.length[den.tree.bionj$edge.length < 0] <- 0
den.tree.bionj <- chronos(den.tree.bionj)
write.tree(den.tree.bionj, "den_bionj.nwk")
#nj
den.tree.nj <- nj(den.dist)
#Remove neg branch length
den.tree.nj$edge.length[den.tree.nj$edge.length < 0] <- 0
den.tree.nj <- chronos(den.tree.nj)
write.tree(den.tree.nj, "den_nj.nwk")
```
## 2. Tanglegram
### Preparing data
```{r}
den_nj <- ReadDendrogram("den_nj.nwk") %>% as.dendrogram()
den_bionj <- ReadDendrogram("den_bionj.nwk") %>% as.dendrogram()
labels <- den_nj %>% set("labels_to_char") %>% labels
labels <- as.data.frame(labels)
labels$country <- gsub("D4|[0-9]{2}","",labels$labels)
unique_country <- data.frame(country = unique(labels$country))
unique_country$color <- distinctColorPalette(nrow(unique_country))
labels_color <- left_join(labels, unique_country)
den_list <- dendlist(den_bionj, den_nj)
```
### Fig 1
- hang = FALSE
```{r}
tanglegram(den_nj, den_bionj,
highlight_branches_lwd = FALSE,
common_subtrees_color_lines = FALSE,
highlight_distinct_edges = FALSE,
hang = F,
lwd = 1.5,
edge.lwd = 2,
color_lines = labels_color$color,
sort = FALSE,
columns_width = c(4,4,4),
intersecting = FALSE,
margin_outer = 1.6,
margin_inner = 6,
dLeaf = 0.00000000005,
main = "Colored by Country",
cex_main = 1)
```
### Fig 2
- hang = TRUE
```{r}
tanglegram(den_nj, den_bionj,
highlight_branches_lwd = FALSE,
common_subtrees_color_lines = FALSE,
highlight_distinct_edges = FALSE,
hang = T,
lwd = 1.5,
edge.lwd = 2,
color_lines = labels_color$color,
sort = FALSE,
columns_width = c(4,4,4),
intersecting = FALSE,
margin_outer = 1.6,
margin_inner = 6,
dLeaf = 0.00000000005,
main = "Colored by Country",
cex_main = 1)
```