]> git.donarmstrong.com Git - ape.git/blobdiff - R/read.nexus.R
last changes for ape 2.4-1
[ape.git] / R / read.nexus.R
index 8c9abc24002e57a27d36e064e92d5317d6f91fc5..969d42c0e403c23c28bb459ecdc41dc1d2a3305c 100644 (file)
@@ -1,4 +1,4 @@
-## read.nexus.R (2009-10-27)
+## read.nexus.R (2009-11-21)
 
 ##   Read Tree File in Nexus Format
 
@@ -9,12 +9,20 @@
 
 .treeBuildWithTokens <- function(x)
 {
+    ## remove potential node labels; see ?read.nexus for justification
+    node.label <- gsub("[:;].*$", "", strsplit(x, ")")[[1]][-1])
+    has.node.labels <- FALSE
+    if (any(node.label != "")) {
+        x <- gsub(")[^:]*:", "):", x)
+        x <- gsub(")[^:]*;", ");", x) # if there's no root edge
+        has.node.labels <- TRUE
+    }
     phy <- .Call("treeBuildWithTokens", x, PACKAGE = "ape")
     dim(phy[[1]]) <- c(length(phy[[1]])/2, 2)
-    nms <- c("edge", "edge.length", "Nnode", "node.label")
-    if (length(phy) == 5) nms <- c(nms, "root.edge")
+    nms <- c("edge", "edge.length", "Nnode", "root.edge")
+    if (length(phy) == 3) nms <- nms[-4]
     names(phy) <- nms
-    if (!sum(phy[[4]])) phy[[4]] <- NULL
+    if (has.node.labels) phy$node.label <- node.label
     class(phy) <- "phylo"
     phy
 }