]> git.donarmstrong.com Git - ape.git/blobdiff - R/read.tree.R
new option 'rotate.tree' in plot.phylo()
[ape.git] / R / read.tree.R
index aa508b0eab5d37879162a9a0a16746cbc4e68041..87ae296e8f7d9fb33ff7843cb60c27fc15a062f6 100644 (file)
@@ -1,8 +1,8 @@
-## read.tree.R (2009-03-09)
+## read.tree.R (2010-09-27)
 
 ##   Read Tree Files in Parenthetic Format
 
-## Copyright 2002-2009 Emmanuel Paradis and Daniel Lawson
+## Copyright 2002-2010 Emmanuel Paradis, Daniel Lawson and Klaus Schliep
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
@@ -12,11 +12,13 @@ tree.build <- function(tp)
     add.internal <- function() {
         edge[j, 1] <<- current.node
         edge[j, 2] <<- current.node <<- node <<- node + 1L
+        index[node] <<- j # set index
         j <<- j + 1L
     }
     add.terminal <- function() {
         edge[j, 1] <<- current.node
         edge[j, 2] <<- tip
+        index[tip] <<- j # set index
         X <- unlist(strsplit(tpc[k], ":"))
         tip.label[tip] <<- X[1]
         edge.length[j] <<- as.numeric(X[2])
@@ -25,7 +27,7 @@ tree.build <- function(tp)
         j <<- j + 1L
     }
     go.down <- function() {
-        l <- which(edge[, 2] == current.node)
+        l <- index[current.node]
         X <- unlist(strsplit(tpc[k], ":"))
         node.label[current.node - nb.tip] <<- X[1]
         edge.length[l] <<- as.numeric(X[2])
@@ -38,7 +40,7 @@ tree.build <- function(tp)
         obj$edge.length <- as.numeric(tp[3])
         obj$Nnode <- 1L
         obj$tip.label <- tp[2]
-        if (length(tp) == 4) obj$node.label <- tp[4]
+        if (tp[4] != "") obj$node.label <- tp[4]
         class(obj) <- "phylo"
         return(obj)
     }
@@ -60,7 +62,9 @@ tree.build <- function(tp)
     edge.length <- numeric(nb.edge)
     edge <- matrix(0L, nb.edge, 2)
     current.node <- node <- as.integer(nb.tip + 1) # node number
-    edge[nb.edge, 2] <- node #
+    edge[nb.edge, 2] <- node
+    index <- numeric(nb.edge + 1) # hash index to avoid which
+    index[node] <- nb.edge
 
     ## j: index of the line number of edge
     ## k: index of the line number of tpc
@@ -98,12 +102,13 @@ read.tree <- function(file = "", text = NULL, tree.names = NULL, skip = 0,
     comment.char = "#", keep.multi = FALSE, ...)
 {
     unname <- function(treetext) {
+        nc <- nchar(treetext)
        tstart <- 1
-       while (substr(treetext, tstart, tstart) != "(" && tstart <= nchar(treetext))
+       while (substr(treetext, tstart, tstart) != "(" && tstart <= nc)
             tstart <- tstart + 1
        if (tstart > 1)
             return(c(substr(treetext, 1, tstart - 1),
-                     substr(treetext, tstart, nchar(treetext))))
+                     substr(treetext, tstart, nc)))
        return(c("", treetext))
     }
     if (!is.null(text)) {
@@ -114,10 +119,6 @@ read.tree <- function(file = "", text = NULL, tree.names = NULL, skip = 0,
         tree <- scan(file = file, what = "", sep = "\n", quiet = TRUE,
                      skip = skip, comment.char = comment.char, ...)
     }
-    tmp <- lapply(tree, unname)
-    tmpnames <- sapply(tmp, function(x) x[1])
-    tree <- sapply(tmp, function(x) x[2])
-    if (is.null(tree.names) && any(nzchar(tmpnames))) tree.names <- tmpnames
     ## Suggestion from Eric Durand and Nicolas Bortolussi (added 2005-08-17):
     if (identical(tree, character(0))) {
         warning("empty character string.")
@@ -133,6 +134,13 @@ read.tree <- function(file = "", text = NULL, tree.names = NULL, skip = 0,
     STRING <- character(Ntree)
     for (i in 1:Ntree)
         STRING[i] <- paste(tree[x[i]:y[i]], sep = "", collapse = "")
+
+    tmp <- unlist(lapply(STRING, unname))
+    tmpnames <- tmp[c(TRUE, FALSE)]
+    STRING <- tmp[c(FALSE, TRUE)]
+    if (is.null(tree.names) && any(nzchar(tmpnames)))
+        tree.names <- tmpnames
+
     colon <- grep(":", STRING)
     if (!length(colon)) {
         obj <- lapply(STRING, clado.build)