1 ## read.nexus.R (2011-02-28)
3 ## Read Tree File in Nexus Format
5 ## Copyright 2003-2011 Emmanuel Paradis and 2010 Klaus Schliep
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 .treeBuildWithTokens <- function(x)
12 phy <- .Call("treeBuildWithTokens", x, PACKAGE = "apex")
13 dim(phy[[1]]) <- c(length(phy[[1]])/2, 2)
14 nms <- c("edge", "edge.length", "Nnode", "node.label", "root.edge")
15 if (length(phy) == 4) nms <- nms[-5]
17 if (all(phy$node.label == "")) phy$node.label <- NULL
22 clado.build <- function(tp)
24 add.internal <- function() {
25 edge[j, 1] <<- current.node
27 edge[j, 2] <<- current.node <<- node
28 index[node] <<- j # set index
31 add.terminal <- function() {
32 edge[j, 1] <<- current.node
34 index[tip] <<- j # set index
35 tip.label[tip] <<- tpc[k]
40 go.down <- function() {
41 l <- index[current.node]
42 node.label[current.node - nb.tip] <<- tpc[k]
44 current.node <<- edge[l, 1]
46 if (!length(grep(",", tp))) {
47 obj <- list(edge = matrix(c(2, 1), 1, 2), Nnode = 1)
48 tp <- unlist(strsplit(tp, "[\\(\\);]"))
49 obj$tip.label <- tp[2]
50 if (tp[3] != "") obj$node.label <- tp[3]
54 tsp <- unlist(strsplit(tp, NULL))
55 tp <- gsub(")", ")NA", tp)
56 tp <- gsub(" ", "", tp)
57 tpc <- unlist(strsplit(tp, "[\\(\\),;]"))
59 skeleton <- tsp[tsp == "(" | tsp == ")" | tsp == "," | tsp == ";"]
60 nsk <- length(skeleton)
61 nb.node <- length(skeleton[skeleton == ")"])
62 nb.tip <- length(skeleton[skeleton == ","]) + 1
63 ## We will assume there is an edge at the root;
64 ## if so, it will be removed and put in a vector
65 nb.edge <- nb.node + nb.tip
66 node.label <- character(nb.node)
67 tip.label <- character(nb.tip)
69 edge <- matrix(NA, nb.edge, 2)
70 current.node <- node <- nb.tip + 1 # node number
71 edge[nb.edge, 1] <- 0 # see comment above
72 edge[nb.edge, 2] <- node #
74 index <- numeric(nb.edge + 1)
75 index[node] <- nb.edge
76 ## j: index of the line number of edge
77 ## k: index of the line number of tpc
81 if (skeleton[i] == "(") add.internal() # add an internal branch (on top)
82 if (skeleton[i] == ",") {
83 if (skeleton[i - 1] != ")") add.terminal() # add a terminal branch
85 if (skeleton[i] == ")") {
86 if (skeleton[i - 1] == ",") { # add a terminal branch and go down one level
90 if (skeleton[i - 1] == ")") go.down() # go down one level
93 edge <- edge[-nb.edge, ]
94 obj <- list(edge = edge, tip.label = tip.label,
95 Nnode = nb.node, node.label = node.label)
97 if (all(obj$node.label == "NA")) NULL
98 else gsub("^NA", "", obj$node.label)
103 read.nexus <- function(file, tree.names = NULL)
105 X <- scan(file = file, what = "", sep = "\n", quiet = TRUE)
106 ## remove all comments
107 ## (this might not work if there are square brackets within the comments)
108 LEFT <- grep("\\[", X)
109 RIGHT <- grep("\\]", X)
110 if (length(LEFT)) { # in case there are no comments at all
112 if (any(w)) { # in case all comments use at least 2 lines
114 X[s] <- gsub("\\[[^]]*\\]", "", X[s])
115 ## The above regexp was quite tough to find: it makes
116 ## possible to delete series of comments on the same line:
117 ## ...[...]xxx[...]...
118 ## without deleting the "xxx". This regexp is in three parts:
120 ## where [^]]* means "any character, except "]", repeated zero
121 ## or more times" (note that the ']' is not escaped here).
122 ## The previous version was:
123 ## X[s] <- gsub("\\[.*\\]", "", X[s])
124 ## which deleted the "xxx". (EP 2008-06-24)
129 X[s] <- gsub("\\[.*", "", X[s])
131 X[sb] <- gsub(".*\\]", "", X[sb])
133 X <- X[-unlist(mapply(":", (s + 1), (sb - 1)))]
136 endblock <- grep("END;|ENDBLOCK;", X, ignore.case = TRUE)
137 semico <- grep(";", X)
138 i1 <- grep("BEGIN TREES;", X, ignore.case = TRUE)
139 i2 <- grep("TRANSLATE", X, ignore.case = TRUE)
140 translation <- if (length(i2) == 1 && i2 > i1) TRUE else FALSE
142 end <- semico[semico > i2][1]
143 x <- X[(i2 + 1):end] # assumes there's a 'new line' after "TRANSLATE"
144 ## x <- gsub("TRANSLATE", "", x, ignore.case = TRUE)
145 x <- unlist(strsplit(x, "[,; \t]"))
147 TRANS <- matrix(x, ncol = 2, byrow = TRUE)
148 TRANS[, 2] <- gsub("['\"]", "", TRANS[, 2])
152 if (translation) semico[semico > i2][1] + 1
153 else semico[semico > i1][1]
154 end <- endblock[endblock > i1][1] - 1
157 tree <- gsub("^.*= *", "", tree)
158 ## check whether there are empty lines from the above manips:
159 tree <- tree[tree != ""]
160 semico <- grep(";", tree)
161 Ntree <- length(semico)
162 ## are some trees on several lines?
163 if (Ntree == 1 && length(tree) > 1) STRING <- paste(tree, collapse = "") else {
164 if (any(diff(semico) != 1)) {
165 STRING <- character(Ntree)
166 s <- c(1, semico[-Ntree] + 1)
167 j <- mapply(":", s, semico)
170 STRING[i] <- paste(tree[j[[i]]], collapse = "")
173 STRING[i] <- paste(tree[j[, i]], collapse = "")
175 } else STRING <- tree
178 STRING <- gsub(" ", "", STRING)
179 colon <- grep(":", STRING)
180 if (!length(colon)) {
181 trees <- lapply(STRING, clado.build)
182 } else if (length(colon) == Ntree) {
184 if (translation) lapply(STRING, .treeBuildWithTokens)
185 else lapply(STRING, tree.build)
187 trees <- vector("list", Ntree)
188 trees[colon] <- lapply(STRING[colon], tree.build)
189 nocolon <- (1:Ntree)[!1:Ntree %in% colon]
190 trees[nocolon] <- lapply(STRING[nocolon], clado.build)
195 ind <- which(tr$tip.label[j] == TRANS[, 1])
196 tr$tip.label[j] <- TRANS[ind, 2]
198 if (!is.null(tr$node.label)) {
199 for (j in 1:length(tr$node.label)) {
200 ind <- which(tr$node.label[j] == TRANS[, 1])
201 tr$node.label[j] <- TRANS[ind, 2]
211 ## Check here that the root edge is not incorrectly represented
212 ## in the object of class "phylo" by simply checking that there
213 ## is a bifurcation at the root
214 if (!translation) n <- length(tr$tip.label)
216 if (sum(tr$edge[, 1] == ROOT) == 1 && dim(tr$edge)[1] > 1) {
217 stop(paste("There is apparently two root edges in your file: cannot read tree file.\n Reading NEXUS file aborted at tree no.", i, sep = ""))
224 if (length(colon)) TRANS[, 2] else
225 TRANS[, 2][as.numeric(trees$tip.label)]
228 if (!is.null(tree.names)) names(trees) <- tree.names
230 if (length(colon) == Ntree) # .treeBuildWithTokens() was used
231 attr(trees, "TipLabel") <- TRANS[, 2]
232 else { # reassign the tip labels then compress
234 trees[[i]]$tip.label <-
235 TRANS[, 2][as.numeric(trees[[i]]$tip.label)]
236 trees <- .compressTipLabel(trees)
239 class(trees) <- "multiPhylo"
241 if (length(grep("[\\/]", file)) == 1)
242 if (!file.exists(file)) # suggestion by Francois Michonneau
243 file <- paste(getwd(), file, sep = "/")
244 attr(trees, "origin") <- file