]> git.donarmstrong.com Git - ape.git/blob - R/write.nexus.R
some bug fixes and '...' in rTrait*()
[ape.git] / R / write.nexus.R
1 ## write.nexus.R (2009-10-27)
2
3 ##   Write Tree File in Nexus Format
4
5 ## Copyright 2003-2009 Emmanuel Paradis
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 write.nexus <- function(..., file = "", translate = TRUE, original.data = TRUE)
11 {
12     obj <- list(...)
13     ## We insure that all trees are in a list, even if there is a single one:
14     if (length(obj) == 1) {
15         if (class(obj[[1]]) == "phylo") ntree <- 1
16         else {
17             obj <- obj[[1]] # NOT use unlist()
18             ntree <- length(obj)
19         }
20     } else ntree <- length(obj)
21     cat("#NEXUS\n", file = file)
22     cat(paste("[R-package APE, ", date(), "]\n\n", sep = ""),
23         file = file, append = TRUE)
24     if (original.data) {
25         if (!is.null(attr(obj[[1]], "origin"))) {
26             if (!file.exists(attr(obj[[1]], "origin"))) {
27                 warning(paste("the file", attr(obj[[1]], "origin"),
28                               "cannot be found,
29 the original data won't be written with the tree."))
30                 original.data <- FALSE
31             }
32             else {
33                 ORI <- scan(file = attr(obj[[1]], "origin"), what = character(),
34                             sep = "\n", skip = 1)
35                 start <- grep("BEGIN TAXA;", ORI)
36                 ORI <- ORI[-(1:(start - 1))]
37                 ORI <- gsub("ENDBLOCK;", "END;", ORI)
38                 endblock <- grep("END;", ORI)
39                 start <- grep("BEGIN TREES;", ORI)
40                 end <- endblock[endblock > start][1]
41                 cat(ORI[1:(start - 1)], file = file, append = TRUE, sep = "\n")
42                 ORI <- ORI[-(1:end)]
43             }
44         }
45         else original.data <- FALSE
46     }
47     N <- length(obj[[1]]$tip.label)
48     if (!original.data) {
49         cat("BEGIN TAXA;\n", file = file, append = TRUE)
50         cat(paste("\tDIMENSIONS NTAX = ", N, ";\n", sep = ""),
51             file = file, append = TRUE)
52         cat("\tTAXLABELS\n", file = file, append = TRUE)
53         cat(paste("\t\t", obj[[1]]$tip.label, sep = ""),
54             sep = "\n", file = file, append = TRUE)
55         cat("\t;\n", file = file, append = TRUE)
56         cat("END;\n", file = file, append = TRUE)
57     }
58     cat("BEGIN TREES;\n", file = file, append = TRUE)
59     if (translate) {
60         ## We take arbitrarily the labels of the first tree, and
61         ## translate them as "1", "2", "3", ...
62         cat("\tTRANSLATE\n", file = file, append = TRUE)
63         tmp <- checkLabel(obj[[1]]$tip.label)
64         X <- paste("\t\t", 1:N, "\t", tmp, ",", sep = "")
65         ## We remove the last comma:
66         X[length(X)] <- gsub(",", "", X[length(X)])
67         cat(X, file = file, append = TRUE, sep = "\n")
68         cat("\t;\n", file = file, append = TRUE)
69         token <- as.character(1:N)
70         names(token) <- obj[[1]]$tip.label
71         obj[[1]]$tip.label <- token
72         if (ntree > 1) {
73             for (i in 2:ntree)
74                 obj[[i]]$tip.label <- token[obj[[i]]$tip.label]
75             class(obj) <- NULL
76         }
77     } else {
78         for (i in 1:ntree)
79           obj[[i]]$tip.label <- checkLabel(obj[[i]]$tip.label)
80     }
81     for (i in 1:ntree) {
82         if (class(obj[[i]]) != "phylo") next
83         if (is.rooted(obj[[i]]))
84           cat("\tTREE * UNTITLED = [&R] ", file = file, append = TRUE)
85         else cat("\tTREE * UNTITLED = [&U] ", file = file, append = TRUE)
86         cat(write.tree(obj[[i]], file = ""),
87             "\n", sep = "", file = file, append = TRUE)
88     }
89     cat("END;\n", file = file, append = TRUE)
90     if(original.data) cat(ORI, file = file, append = TRUE, sep = "\n")
91 }