-## read.tree.R (2008-02-18)
+## read.tree.R (2009-03-09)
## Read Tree Files in Parenthetic Format
-## Copyright 2002-2008 Emmanuel Paradis
+## Copyright 2002-2009 Emmanuel Paradis and Daniel Lawson
## This file is part of the R-package `ape'.
## See the file ../COPYING for licensing issues.
obj
}
-read.tree <- function(file = "", text = NULL, tree.names = NULL,
- skip = 0, comment.char = "#", ...)
+read.tree <- function(file = "", text = NULL, tree.names = NULL, skip = 0,
+ comment.char = "#", keep.multi = FALSE, ...)
{
+ unname <- function(treetext) {
+ tstart <- 1
+ while (substr(treetext, tstart, tstart) != "(" && tstart <= nchar(treetext))
+ tstart <- tstart + 1
+ if (tstart > 1)
+ return(c(substr(treetext, 1, tstart - 1),
+ substr(treetext, tstart, nchar(treetext))))
+ return(c("", treetext))
+ }
if (!is.null(text)) {
if (!is.character(text))
stop("argument `text' must be of mode character")
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.")
if(sum(obj[[i]]$edge[, 1] == ROOT) == 1 && dim(obj[[i]]$edge)[1] > 1)
stop(paste("There is apparently two root edges in your file: cannot read tree file.\n Reading Newick file aborted at tree no.", i, sep = ""))
}
- if (Ntree == 1) obj <- obj[[1]] else {
+ if (Ntree == 1 && !keep.multi) obj <- obj[[1]] else {
if (!is.null(tree.names)) names(obj) <- tree.names
class(obj) <- "multiPhylo"
}