-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdensiTree_WE.R
178 lines (175 loc) · 6.46 KB
/
densiTree_WE.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
# Modified from phangorn
getAges <- function(x) {
fun <- function(x) max(node.depth.edgelength(x))
height <- NULL
if (inherits(x, "phylo")) height <- fun(x)
if (inherits(x, "multiPhylo")) {
if (!is.null(attr(x, "TipLabel"))) {
x <- .uncompressTipLabel(x)
x <- unclass(x)
height <- vapply(x, fun, 0)
}
else {
x <- unclass(x)
height <- vapply(x, fun, 0)
}
}
height
}
add_tiplabels <- function(xy, tip.label, direction, adj, font, srt = 0, cex = 1,
col = 1, label.offset = 0) {
direction <- match.arg(direction, c("rightwards", "leftwards", "upwards",
"downwards"))
horizontal <- direction %in% c("rightwards", "leftwards")
nTips <- length(tip.label)
xx <- rep(1, nrow(xy))
yy <- xy[, 2 ]
if (direction == "leftwards" | direction == "downwards") xx <- xx * 0
if (!horizontal) {
# tmp <- yy
yy <- xx
xx <- xy[, 1]
}
MAXSTRING <- max(strwidth(tip.label, cex = cex))
loy <- 0
if (direction == "rightwards") lox <- label.offset + MAXSTRING * 1.05 * adj
if (direction == "leftwards")
lox <- -label.offset - MAXSTRING * 1.05 * (1 - adj)
if (!horizontal) {
psr <- par("usr")
MAXSTRING <- MAXSTRING * 1.09 * (psr[4] - psr[3]) / (psr[2] - psr[1])
loy <- label.offset + MAXSTRING * 1.05 * adj
lox <- 0
srt <- 90 + srt
if (direction == "downwards") {
loy <- -loy
srt <- 180 + srt
}
}
text(xx[1:nTips] + lox, yy[1:nTips] + loy, tip.label, adj = adj,
font = font, srt = srt, cex = cex, col = col)
}
densiTree_WE <- function (x, type = "cladogram", alpha = 1/length(x), consensus = NULL,
direction = "rightwards", optim = FALSE, scaleX = FALSE,
col = 1, width = 1, lty = 1, cex = 0.8, font = 3, tip.color = 1,
adj = 0, srt = 0, underscore = FALSE, label.offset = 0, scale.bar = TRUE,
jitter = list(amount = 0, random = TRUE), plotwidth = 1.1, ...)
{
if (!inherits(x, "multiPhylo"))
stop("x must be of class multiPhylo")
if (is.character(consensus)) {
consensus <- stree(length(consensus), tip.label = consensus)
consensus$edge.length <- rep(1, nrow(consensus$edge))
}
if (is.null(consensus)) {
consensus <- tryCatch(consensus(x, p = 0.5), error = function(e) unroot(midpoint(superTree(x))))
}
if (inherits(consensus, "multiPhylo"))
consensus <- consensus[[1]]
sort_tips <- function(x) {
x <- reorder(x)
nTip <- as.integer(length(x$tip.label))
e2 <- x$edge[, 2]
x$tip.label <- x$tip.label[e2[e2 <= nTip]]
x$edge[e2 <= nTip, 2] <- as.integer(1L:nTip)
x
}
type <- match.arg(type, c("phylogram", "cladogram"))
direction <- match.arg(direction, c("rightwards", "leftwards",
"upwards", "downwards"))
horizontal <- direction %in% c("rightwards", "leftwards")
consensus <- reorder(consensus)
nTip <- as.integer(length(consensus$tip.label))
consensus <- sort_tips(consensus)
consensus <- reorder(consensus, "postorder")
maxBT <- max(getAges(x))
if (scaleX)
maxBT <- 1
label <- rev(pretty(c(maxBT, 0)))
maxBT <- max(label)
xy <- plotPhyloCoor(consensus, direction = direction, ...)
yy <- xy[, 2]
plot.new()
tl <- which.max(nchar(consensus$tip.label))
sw <- strwidth(consensus$tip.label[tl], cex = cex) * plotwidth # modified this line /WE 22/9/2021
if (direction == "rightwards") {
plot.default(0, type = "n", xlim = c(0, 1 + sw), ylim = c(0,
nTip + 1), xlab = "", ylab = "", axes = FALSE, ...)
if (scale.bar)
axis(side = 1, at = seq(0, 1, length.out = length(label)),
labels = label)
}
if (direction == "leftwards") {
plot.default(0, type = "n", xlim = c(0 - sw, 1), ylim = c(0,
nTip + 1), xlab = "", ylab = "", axes = FALSE, ...)
if (scale.bar)
axis(side = 1, at = seq(0, 1, length.out = length(label)),
labels = rev(label))
}
if (direction == "downwards") {
plot.default(0, type = "n", xlim = c(0, nTip + 1), ylim = c(0 -
sw, 1), xlab = "", ylab = "", axes = FALSE, ...)
if (scale.bar)
axis(side = 2, at = seq(0, 1, length.out = length(label)),
labels = rev(label))
}
if (direction == "upwards") {
plot.default(0, type = "n", xlim = c(0, nTip + 1), ylim = c(0,
1 + sw), xlab = "", ylab = "", axes = FALSE, ...)
if (scale.bar)
axis(side = 2, at = seq(0, 1, length.out = length(label)),
labels = label)
}
tip_labels <- consensus$tip.label
if (is.expression(consensus$tip.label))
underscore <- TRUE
if (!underscore)
tip_labels <- gsub("_", " ", tip_labels)
add_tiplabels(xy, tip_labels, direction, adj = adj, font = font,
srt = srt, cex = cex, col = tip.color, label.offset = label.offset)
col <- rep(col, length.out = length(x))
tiporder <- 1:nTip
names(tiporder) <- consensus$tip.label
if (jitter$amount > 0) {
if (jitter$random)
jit <- runif(length(x), -jitter$amount, jitter$amount)
else jit <- seq(-jitter$amount, jitter$amount, length = length(x))
}
for (treeindex in seq_along(x)) {
tmp <- reorder(x[[treeindex]])
tmp <- sort_tips(tmp)
xy <- plotPhyloCoor(tmp, tip.height = tiporder, direction = direction,
...)
xx <- xy[, 1]
yy <- xy[, 2]
if (horizontal) {
if (scaleX)
xx <- xx/max(xx)
else xx <- xx/maxBT
if (direction == "rightwards")
xx <- xx + (1 - max(xx))
if (jitter$amount > 0)
yy <- yy + jit[treeindex]
}
else {
if (scaleX)
yy <- yy/max(yy)
else yy <- yy/maxBT
if (direction == "upwards")
yy <- yy + (1 - max(yy))
if (jitter$amount > 0)
xx <- xx + jit[treeindex]
}
e1 <- tmp$edge[, 1]
if (type == "cladogram")
cladogram.plot(tmp$edge, xx, yy, edge.color = adjustcolor(col[treeindex],
alpha.f = alpha), edge.width = width, edge.lty = lty)
if (type == "phylogram") {
Ntip <- min(e1) - 1L
Nnode <- tmp$Nnode
phylogram.plot(tmp$edge, Ntip, Nnode, xx, yy, horizontal,
edge.color = adjustcolor(col[treeindex], alpha.f = alpha),
edge.width = width, edge.lty = lty)
}
}
}