]> git.donarmstrong.com Git - ape.git/blob - R/read.nexus.R
fix in birthdeath()
[ape.git] / R / read.nexus.R
1 ## read.nexus.R (2012-02-09)
2
3 ##   Read Tree File in Nexus Format
4
5 ## Copyright 2003-2012 Emmanuel Paradis and 2010 Klaus Schliep
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 .treeBuildWithTokens <- function(x)
11 {
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", "root.edge")
15     if (length(phy) == 4) nms <- nms[-5]
16     names(phy) <- nms
17     if (all(phy$node.label == "")) phy$node.label <- NULL
18     class(phy) <- "phylo"
19     phy
20 }
21
22 clado.build <- function(tp)
23 {
24     add.internal <- function() {
25         edge[j, 1] <<- current.node
26         node <<- node + 1
27         edge[j, 2] <<- current.node <<- node
28         index[node] <<- j # set index
29         j <<- j + 1
30     }
31     add.terminal <- function() {
32         edge[j, 1] <<- current.node
33         edge[j, 2] <<- tip
34         index[tip] <<- j # set index
35         tip.label[tip] <<- tpc[k]
36         k <<- k + 1
37         tip <<- tip + 1
38         j <<- j + 1
39     }
40     go.down <- function() {
41         l <- index[current.node]
42         node.label[current.node - nb.tip] <<- tpc[k]
43         k <<- k + 1
44         current.node <<- edge[l, 1]
45     }
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]
51         class(obj) <- "phylo"
52         return(obj)
53     }
54     tsp <- unlist(strsplit(tp, NULL))
55     tp <- gsub(")", ")NA", tp)
56     tp <- gsub(" ", "", tp)
57     tpc <- unlist(strsplit(tp, "[\\(\\),;]"))
58     tpc <- tpc[tpc != ""]
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)
68
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 #
73
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
78     ## tip: tip number
79     j <- k <- tip <- 1
80     for (i in 2:nsk) {
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
84         }
85         if (skeleton[i] == ")") {
86             if (skeleton[i - 1] == ",") {   # add a terminal branch and go down one level
87                 add.terminal()
88                 go.down()
89             }
90             if (skeleton[i - 1] == ")") go.down()   # go down one level
91         }
92     }
93     edge <- edge[-nb.edge, ]
94     obj <- list(edge = edge, tip.label = tip.label,
95                 Nnode = nb.node, node.label = node.label)
96     obj$node.label <-
97         if (all(obj$node.label == "NA")) NULL
98         else gsub("^NA", "", obj$node.label)
99     class(obj) <- "phylo"
100     obj
101 }
102
103 read.nexus <- function(file, tree.names = NULL)
104 {
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
111         w <- LEFT == RIGHT
112         if (any(w)) { # in case all comments use at least 2 lines
113             s <- LEFT[w]
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:
119             ##       \\[      [^]]*       \\]
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)
125         }
126         w <- !w
127         if (any(w)) {
128             s <- LEFT[w]
129             X[s] <- gsub("\\[.*", "", X[s])
130             sb <- RIGHT[w]
131             X[sb] <- gsub(".*\\]", "", X[sb])
132             if (any(s < sb - 1))
133                 X <- X[-unlist(mapply(":", (s + 1), (sb - 1)))]
134         }
135     }
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
141     if (translation) {
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]"))
146         x <- x[nzchar(x)]
147         TRANS <- matrix(x, ncol = 2, byrow = TRUE)
148         TRANS[, 2] <- gsub("['\"]", "", TRANS[, 2])
149         n <- dim(TRANS)[1]
150     }
151     start <-
152         if (translation) semico[semico > i2][1] + 1
153         else semico[semico > i1][1]
154     end <- endblock[endblock > i1][1] - 1
155     tree <- X[start:end]
156     rm(X)
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) # provisional -- some ";" may actually mark end of commands
162     ## are some trees on several lines?
163     ## -- this actually 'packs' all characters ending with a ";" in a single string --
164     if (Ntree == 1 && length(tree) > 1) STRING <- paste(tree, collapse = "") else {
165         if (any(diff(semico) != 1)) {
166             STRING <- character(Ntree)
167             s <- c(1, semico[-Ntree] + 1)
168             j <- mapply(":", s, semico)
169             if (is.list(j)) {
170                 for (i in 1:Ntree)
171                     STRING[i] <- paste(tree[j[[i]]], collapse = "")
172             } else {
173                 for (i in 1:Ntree)
174                     STRING[i] <- paste(tree[j[, i]], collapse = "")
175             }
176         } else STRING <- tree
177     }
178     rm(tree)
179     ## exclude the possible command lines ending with ";":
180     STRING <- STRING[grep("^[[:blank:]]*tree.*= *", STRING, ignore.case = TRUE)]
181     Ntree <- length(STRING) # update Ntree
182     ## get the tree names:
183     nms.trees <- sub(" *= *.*", "", STRING) # only the first occurence of "="
184     nms.trees <- sub("^ *tree *", "", nms.trees, ignore.case = TRUE)
185     STRING <- sub("^.*= *", "", STRING) # delete title and 'TREE' command with 'sub'
186     STRING <- gsub(" ", "", STRING) # delete all white spaces
187     colon <- grep(":", STRING)
188     if (!length(colon)) {
189         trees <- lapply(STRING, clado.build)
190     } else if (length(colon) == Ntree) {
191         trees <-
192             if (translation) lapply(STRING, .treeBuildWithTokens)
193             else lapply(STRING, tree.build)
194     } else {
195         trees <- vector("list", Ntree)
196         trees[colon] <- lapply(STRING[colon], tree.build)
197         nocolon <- (1:Ntree)[!1:Ntree %in% colon]
198         trees[nocolon] <- lapply(STRING[nocolon], clado.build)
199         if (translation) {
200             for (i in 1:Ntree) {
201                 tr <- trees[[i]]
202                 for (j in 1:n) {
203                     ind <- which(tr$tip.label[j] == TRANS[, 1])
204                     tr$tip.label[j] <- TRANS[ind, 2]
205                 }
206                 if (!is.null(tr$node.label)) {
207                     for (j in 1:length(tr$node.label)) {
208                         ind <- which(tr$node.label[j] == TRANS[, 1])
209                         tr$node.label[j] <- TRANS[ind, 2]
210                     }
211                 }
212                 trees[[i]] <- tr
213             }
214             translation <- FALSE
215         }
216     }
217     for (i in 1:Ntree) {
218         tr <- trees[[i]]
219         ## Check here that the root edge is not incorrectly represented
220         ## in the object of class "phylo" by simply checking that there
221         ## is a bifurcation at the root
222         if (!translation) n <- length(tr$tip.label)
223         ROOT <- n + 1
224         if (sum(tr$edge[, 1] == ROOT) == 1 && dim(tr$edge)[1] > 1) {
225             stop(paste("The tree has apparently singleton node(s): cannot read tree file.\n  Reading NEXUS file aborted at tree no.", i, sep = ""))
226         }
227     }
228     if (Ntree == 1) {
229         trees <- trees[[1]]
230         if (translation) {
231             trees$tip.label <-
232                 if (length(colon)) TRANS[, 2] else
233                 TRANS[, 2][as.numeric(trees$tip.label)]
234         }
235     } else {
236         if (!is.null(tree.names)) names(trees) <- tree.names
237         if (translation) {
238             if (length(colon) == Ntree) # .treeBuildWithTokens() was used
239                 attr(trees, "TipLabel") <- TRANS[, 2]
240             else { # reassign the tip labels then compress
241                 for (i in 1:Ntree)
242                     trees[[i]]$tip.label <-
243                         TRANS[, 2][as.numeric(trees[[i]]$tip.label)]
244                 trees <- .compressTipLabel(trees)
245             }
246         }
247         class(trees) <- "multiPhylo"
248         if (!all(nms.trees == "")) names(trees) <- nms.trees
249     }
250     trees
251 }