1 ## read.nexus.R (2008-02-28)
3 ## Read Tree File in Nexus Format
5 ## Copyright 2003-2008 Emmanuel Paradis
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 = "ape")
13 dim(phy[[1]]) <- c(length(phy[[1]])/2, 2)
14 nms <- c("edge", "edge.length", "Nnode", "node.label")
15 if (length(phy) == 5) nms <- c(nms, "root.edge")
17 if (!sum(phy[[4]])) phy[[4]] <- NULL
22 clado.build <- function(tp)
24 add.internal <- function() {
25 edge[j, 1] <<- current.node
27 edge[j, 2] <<- current.node <<- node
30 add.terminal <- function() {
31 edge[j, 1] <<- current.node
33 tip.label[tip] <<- tpc[k]
38 go.down <- function() {
39 l <- which(edge[, 2] == current.node)
40 node.label[current.node - nb.tip] <<- tpc[k]
42 current.node <<- edge[l, 1]
44 if (!length(grep(",", tp))) {
45 obj <- list(edge = matrix(c(2, 1), 1, 2), Nnode = 1)
46 tp <- unlist(strsplit(tp, "[\\(\\);]"))
47 obj$tip.label <- tp[2]
48 if (length(tp) == 3) obj$node.label <- tp[3]
52 tsp <- unlist(strsplit(tp, NULL))
53 tp <- gsub(")", ")NA", tp)
54 tp <- gsub(" ", "", tp)
55 tpc <- unlist(strsplit(tp, "[\\(\\),;]"))
57 skeleton <- tsp[tsp == "(" | tsp == ")" | tsp == "," | tsp == ";"]
58 nsk <- length(skeleton)
59 nb.node <- length(skeleton[skeleton == ")"])
60 nb.tip <- length(skeleton[skeleton == ","]) + 1
61 ## We will assume there is an edge at the root;
62 ## if so, it will be removed and put in a vector
63 nb.edge <- nb.node + nb.tip
64 node.label <- character(nb.node)
65 tip.label <- character(nb.tip)
67 edge <- matrix(NA, nb.edge, 2)
68 current.node <- node <- nb.tip + 1 # node number
69 edge[nb.edge, 1] <- 0 # see comment above
70 edge[nb.edge, 2] <- node #
72 ## j: index of the line number of edge
73 ## k: index of the line number of tpc
78 if (skeleton[i] == "(") add.internal() # add an internal branch (on top)
79 if (skeleton[i] == ",") {
80 if (skeleton[i - 1] != ")") add.terminal() # add a terminal branch
82 if (skeleton[i] == ")") {
83 if (skeleton[i - 1] == ",") { # add a terminal branch and go down one level
87 if (skeleton[i - 1] == ")") go.down() # go down one level
90 # if(node.label[1] == "NA") node.label[1] <- ""
91 edge <- edge[-nb.edge, ]
92 obj <- list(edge = edge, tip.label = tip.label,
93 Nnode = nb.node, node.label = node.label)
95 if (all(obj$node.label == "NA")) NULL
96 else gsub("^NA", "", obj$node.label)
101 read.nexus <- function(file, tree.names = NULL)
103 X <- scan(file = file, what = "", sep = "\n", quiet = TRUE)
104 ## remove all comments
105 ## (this might not work if there are square brackets within the comments)
106 LEFT <- grep("\\[", X)
107 RIGHT <- grep("\\]", X)
108 if (length(LEFT)) { # in case there are no comments at all
110 if (any(w)) { # in case all comments use at least 2 lines
112 X[s] <- gsub("\\[.*\\]", "", X[s])
117 X[s] <- gsub("\\[.*", "", X[s])
119 X[sb] <- gsub(".*\\]", "", X[sb])
121 X <- X[-unlist(mapply(":", (s + 1), (sb - 1)))]
124 endblock <- grep("END;|ENDBLOCK;", X, ignore.case = TRUE)
125 semico <- grep(";", X)
126 i1 <- grep("BEGIN TREES;", X, ignore.case = TRUE)
127 i2 <- grep("TRANSLATE", X, ignore.case = TRUE)
128 translation <- if (length(i2) == 1 && i2 > i1) TRUE else FALSE
130 end <- semico[semico > i2][1]
131 x <- X[(i2 + 1):end] # assumes there's a 'new line' after "TRANSLATE"
132 ## x <- gsub("TRANSLATE", "", x, ignore.case = TRUE)
133 x <- unlist(strsplit(x, "[,; \t]"))
135 TRANS <- matrix(x, ncol = 2, byrow = TRUE)
136 TRANS[, 2] <- gsub("['\"]", "", TRANS[, 2])
140 if (translation) semico[semico > i2][1] + 1
141 else semico[semico > i1][1]
142 end <- endblock[endblock > i1][1] - 1
145 tree <- gsub("^.*= *", "", tree)
146 semico <- grep(";", tree)
147 Ntree <- length(semico)
148 ## are some trees on several lines?
149 if (any(diff(semico) != 1)) {
150 STRING <- character(Ntree)
151 s <- c(1, semico[-Ntree] + 1)
152 j <- mapply(":", s, semico)
154 STRING[i] <- paste(tree[j[, i]], collapse = "")
155 } else STRING <- tree
157 STRING <- gsub(" ", "", STRING)
158 colon <- grep(":", STRING)
159 if (!length(colon)) {
160 trees <- lapply(STRING, clado.build)
161 } else if (length(colon) == Ntree) {
163 if (translation) lapply(STRING, .treeBuildWithTokens)
164 else lapply(STRING, tree.build)
166 trees <- vector("list", Ntree)
167 trees[colon] <- lapply(STRING[colon], tree.build)
168 nocolon <- (1:Ntree)[!1:Ntree %in% colon]
169 trees[nocolon] <- lapply(STRING[nocolon], clado.build)
174 ind <- which(tr$tip.label[j] == TRANS[, 1])
175 tr$tip.label[j] <- TRANS[ind, 2]
177 if (!is.null(tr$node.label)) {
178 for (j in 1:length(tr$node.label)) {
179 ind <- which(tr$node.label[j] == TRANS[, 1])
180 tr$node.label[j] <- TRANS[ind, 2]
190 ## Check here that the root edge is not incorrectly represented
191 ## in the object of class "phylo" by simply checking that there
192 ## is a bifurcation at the root
193 if (!translation) n <- length(tr$tip.label)
195 if (sum(tr$edge[, 1] == ROOT) == 1 && dim(tr$edge)[1] > 1) {
196 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 = ""))
199 if (Ntree == 1) trees <- trees[[1]] else {
200 if (!is.null(tree.names)) names(trees) <- tree.names
201 if (translation) attr(trees, "TipLabel") <- TRANS[, 2]
202 class(trees) <- "multiPhylo"
204 if (length(grep("[\\/]", file)) == 1)
205 file <- paste(getwd(), file, sep = "/")
206 attr(trees, "origin") <- file