-
Notifications
You must be signed in to change notification settings - Fork 3
/
read.annot.beast.R
175 lines (174 loc) · 5.71 KB
/
read.annot.beast.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
#' read.annot.beast
#'
#' @description
#' This function reads a nexus formated beast file and
#' appends all meta-data and annotations.
#'
#' @details
#' Node ordering is the same as in the \code{read.tree}/\code{read.nexus}
#' functions from ape (cladewise). The metadata is stored as a
#' data.frame named 'metadata'. Only node posterior
#' probabilites are passed as an additional numeric vector
#' named 'posterior'. Note that 'metadata' includes tip
#' annotations as well. The code might be a bit slow for large trees.
#'
#' @param file The path to the MCC file from BEAST
#' @return annotated \code{phylo} object
#' @seealso \code{\link{read.nexus}}
#' @export
#' @examples
#' file <- system.file("data/mcc.tre", package="rBt")
#' tr <- read.annot.beast(file)
#' class(tr)
#' # [1] "phylo"
#' names(tr)
#' #[1] "edge" "edge.length" "Nnode" "root.edge" "tip.label"
#' #[6] "metadata" "posterior"
#' # for add pp to nodes you could try:
#' plot(tr)
#' nodelabels(text=round(tr$posterior,2), cex=0.5)
#' # for edges, try:
#' tr$ordered.edges <- order.edges(tr)
#' plot(tr)
#' edgelabels(edge=tr$ordered.edges, text=round(tr$posterior,2), cex=0.5)
read.annot.beast <- function(file){
TREE <- scan(file=file, what=character(), sep="\n", quiet=T)
TREE <- TREE[ grep("^[[:space:]]*tree", TREE) ]
if (length(TREE) > 1){
trs <- read.nexus(file)
for (itr in 1:length(TREE)){
tr <- TREE[[itr]]
tr <- sub("tree.* \\(","\\(", tr)
edges <- list()
annot <- list()
for (i in strsplit(tr, ":")[[1]]){
if (substr(i, nchar(i), nchar(i)) == "]" | substr(i, nchar(i)-1, nchar(i)-1) == "]"){
brack <- gregexpr(pattern ='\\[|\\]', i)[[1]]
start <- rev(brack)[2]
end <- rev(brack)[1]
annot <- c(annot, list(substr(i, start, end)))
end <- start-1
if (substr(i, 1, 1) == "["){
start <- brack[2]+1
} else {
start <- 1
}
edges <- c(edges, list(substr(i, start, end)))
}
}
nodes <- lapply(edges, function(x) {
if (substr(x, nchar(x), nchar(x)) == ")"){
return("node")
}
else {
start <- gregexpr(pattern ='[\\(|,][[:alnum:]]+', x)[[1]][1]
return(substr(x, start+1, nchar(x)))
}
})
backbone <- strsplit(tr, "")[[1]][ grep("\\(|\\)",strsplit(tr, "")[[1]]) ]
tipsidx <- grep("node",nodes, invert=T)
nodesidx <- grep("node",nodes)
Ntips <- length(tipsidx)
Nnodes <- length(nodesidx)
lst <- list(opened=vector(), closed=vector(), currnode=Ntips)
for (b in backbone){
lst <- rBt:::cldws(b, lst)
}
tips = vector()
tip_num = 1
for (i in 1:length(nodes)){
if (nodes[[i]] != "node"){
tips = c(tips, nodes[[i]])
nodes[[i]] = tip_num
tip_num = tip_num + 1
}
}
nodes[nodesidx] <- lst$closed
nodes <- sapply(nodes, as.numeric)
annot <- lapply(annot, rBt:::process_annot)
annot_names <- sort(unlist(lapply(annot, function(x) names(x) )))
annot_names <- annot_names[ !duplicated(annot_names) ]
annot_mat <- data.frame(matrix(NA, ncol=length(annot_names)+1, nrow=length(nodes)))
colnames(annot_mat) <- c('node', annot_names)
annot_mat$node <- nodes
for (i in annot_names){
tmp <- lapply(annot, function(x, y=i) x[[paste(y)]] )
tmp <- lapply(tmp, function(x) if (is.null(x)) return(NA) else return(x) )
annot_mat[,paste(i)] <- unlist(tmp)
}
annot_mat <- annot_mat[order(annot_mat[,1]),]
rownames(annot_mat) <- 1:dim(annot_mat)[1]
trs[[itr]]$metadata <- annot_mat
trs[[itr]]$posterior <- as.numeric(annot_mat$posterior[lst$currnode:length(nodes)])
cat("tree #",itr,"\r",sep='')
}
message(paste("\nRead ",length(trs)," trees"))
return(trs)
} else {
tr <- TREE[[1]]
tr <- sub("^[[:space:]]*tree.* \\(","\\(", tr)
edges <- list()
annot <- list()
for (i in strsplit(tr, ":")[[1]]){
if (substr(i, nchar(i), nchar(i)) == "]" | substr(i, nchar(i)-1, nchar(i)-1) == "]"){
brack <- gregexpr(pattern ='\\[|\\]', i)[[1]]
start <- rev(brack)[2]
end <- rev(brack)[1]
annot <- c(annot, list(substr(i, start, end)))
end <- start-1
if (substr(i, 1, 1) == "["){
start <- brack[2]+1
} else {
start <- 1
}
edges <- c(edges, list(substr(i, start, end)))
}
}
nodes <- lapply(edges, function(x) {
if (substr(x, nchar(x), nchar(x)) == ")"){
return("node")
}
else {
start <- gregexpr(pattern ='[\\(|,][[:alnum:]]+', x)[[1]][1]
return(substr(x, start+1, nchar(x)))
}
})
backbone <- strsplit(tr, "")[[1]][ grep("\\(|\\)",strsplit(tr, "")[[1]]) ]
tipsidx <- grep("node",nodes, invert=T)
nodesidx <- grep("node",nodes)
Ntips <- length(tipsidx)
Nnodes <- length(nodesidx)
lst <- list(opened=vector(), closed=vector(), currnode=Ntips)
for (b in backbone){
lst <- rBt:::cldws(b, lst)
}
tips = vector()
tip_num = 1
for (i in 1:length(nodes)){
if (nodes[[i]] != "node"){
tips = c(tips, nodes[[i]])
nodes[[i]] = tip_num
tip_num = tip_num + 1
}
}
nodes[nodesidx] <- lst$closed
nodes <- sapply(nodes, as.numeric)
annot <- lapply(annot, rBt:::process_annot)
annot_names <- sort(unlist(lapply(annot, function(x) names(x) )))
annot_names <- annot_names[ !duplicated(annot_names) ]
annot_mat <- data.frame(matrix(NA, ncol=length(annot_names)+1, nrow=length(nodes)))
colnames(annot_mat) <- c('node', annot_names)
annot_mat$node <- nodes
for (i in annot_names){
tmp <- lapply(annot, function(x, y=i) x[[paste(y)]] )
tmp <- lapply(tmp, function(x) if (is.null(x)) return(NA) else return(x) )
annot_mat[,paste(i)] <- unlist(tmp)
}
annot_mat <- annot_mat[order(annot_mat[,1]),]
rownames(annot_mat) <- 1:dim(annot_mat)[1]
tr <- read.nexus(file)
tr$metadata <- annot_mat
tr$posterior <- as.numeric(annot_mat$posterior[lst$currnode:length(nodes)])
return(tr)
}
}