-
Notifications
You must be signed in to change notification settings - Fork 27
/
new_plotTreeHighlightBranches.R
82 lines (72 loc) · 2.25 KB
/
new_plotTreeHighlightBranches.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
require(ggtree)
require(ape)
#' @param hlspecies the species to highlight
#' @param hlcols the colors to highlight those species with
#' @param main the name to give the plot
plotTreeHighlightBranches = function(tree, outgroup=NULL, hlspecies, hlcols=NULL, main="")
{
if (!is.null(outgroup)) {
outgroup <- outgroup[outgroup %in% tree$tip.label]
if (length(outgroup) > 0) {
#root the tree
rooted <- root(tree, outgroup)
} else {
print("No members of requested outgroup found in tree; keeping unrooted.")
rooted <- tree
outgroup <- NULL
}
} else {
rooted <- tree
}
#if hlcols is null make branches blue
if (is.null(hlcols)) {
hlcols <- rep_len("#0000ff", length(hlspecies)) #if there are fewer colors than species to highlight, repeat colors
}else if (length(hlcols) < length(hlspecies)) {
hlcols <- rep_len(hlcols, length(hlspecies))
}
#create hlspecies_named
hlspecies_named <- vector(mode="character")
if(is.numeric(hlspecies)){
for(i in 1: length(hlspecies))
{
hlspecies_named[i] <- tree$tip.label[hlspecies[i]]
}
}else
hlspecies_named <- hlspecies
#Make branches of length 0 just *slightly* larger values to visualize tree
rooted2 <- rooted
mm <- min(rooted2$edge.length[rooted2$edge.length>0])
rooted2$edge.length[rooted2$edge.length==0] <- max(0.02,mm/20)
#vector of tip label colors
tipCols <- vector(mode = "character")
x <- 1
for(i in 1: length(tree$tip.label))
{
if(tree$tip.label[i] %in% hlspecies_named)
{
tipCols[i] <- hlcols[x]
x <- x+1
}
else tipCols[i] <- "black"
}
#number of labels
nlabel <- rooted2$Nnode + length(rooted2$tip.label)
edgeCols <- vector(mode="character", length=nlabel)
x <- 1
for(i in 1: nlabel)
{
if(i < length(rooted2$tip.label))
{
if(rooted2$tip.label[i] %in% hlspecies_named)
{
edgeCols[i] <- hlcols[x]
x <- x+1
}else
edgeCols[i] <- "Black"
}else
edgeCols[i] <- "Black"
}
plotobj = ggtree(rooted2, color = edgeCols)
plotobj = plotobj + geom_tiplab(color= tipCols, geom="text", cex = 3) + labs(title = main)
return(plotobj)
}