1 "write.nexus.data" <- function (x, file, format = "dna", datablock = TRUE,
2 interleaved = TRUE, charsperline = NULL,
3 gap = NULL, missing = NULL)
7 # Version: 09/13/2006 09:06:33 AM CEST
9 # By: Johan Nylander, nylander @ scs.fsu.edu
11 # TODO: Standard data, mixed data, nice indent
12 #------------------------------------------------------------------
14 indent <- " " # Two blanks
15 maxtax <- 5 # Max nr of taxon names to be printed on a line
16 defcharsperline <- 80 # Default nr of characters per line if interleaved
17 defgap <- "-" # Default gap character
18 defmissing <- "?" # Default missing data character
21 nchars <- length(x[[1]])
24 if (is.null(names(x))) {
25 names(x) <- as.character(1:ntax)
28 "fcat" <- function (..., file = zz)
30 cat(..., file = file, sep = "", append = TRUE)
33 "find.max.length" <- function (x)
36 for (i in 1:length(x)) {
37 val <- length((strsplit(x[i], split = NULL))[[1]])
45 "print.matrix" <- function(x, dindent = " ")
48 printlength <- find.max.length(Names) + 2
49 if (interleaved == FALSE) {
50 for (i in 1:length(x)) {
51 sequence <- paste(x[[i]], collapse = "")
53 thestring <- sprintf("%-*s%s%s", printlength, taxon, dindent, sequence)
54 fcat(indent, indent, thestring, "\n")
58 ntimes <- ceiling(nchars/charsperline)
62 for (i in 1:length(x)) {
63 sequence <- paste(x[[i]][start:end], collapse = "")
65 thestring <- sprintf("%-*s%s%s", printlength, taxon, dindent, sequence)
66 fcat(indent, indent, thestring, "\n")
71 start <- start + charsperline
72 end <- end + charsperline
80 fcat("#NEXUS\n[Data written by write.nexus.data.R,", " ", date(),"]\n")
82 NCHAR <- paste("NCHAR=", nchars, sep = "")
83 NTAX <- paste("NTAX=", ntax, sep = "")
85 if (format == "dna") {
86 DATATYPE <- "DATATYPE=DNA"
88 if (format == "protein") {
89 DATATYPE <- "DATATYPE=PROTEIN"
92 if (is.null(charsperline)) {
93 if (nchars < defcharsperline) {
94 charsperline <- nchars
98 if (nchars > defcharsperline) {
99 charsperline <- defcharsperline
104 if (is.null(missing)) {
105 MISSING <- paste("MISSING=", defmissing, sep = "")
108 MISSING <- paste("MISSING=", missing, sep = "")
112 GAP <- paste("GAP=", defgap, sep = "")
115 GAP <- paste("GAP=", gap, sep = "")
118 if (interleaved == TRUE) {
119 INTERLEAVE <- "INTERLEAVE=YES"
121 if (interleaved == FALSE) {
122 INTERLEAVE <- "INTERLEAVE=NO"
125 if (datablock == TRUE) {
126 fcat("BEGIN DATA;\n")
127 fcat(indent,"DIMENSIONS", " ", NTAX, " ", NCHAR, ";\n")
128 if (format %in% c("dna", "protein")) {
129 fcat(indent, "FORMAT", " ", DATATYPE, " ", MISSING, " ", GAP, " ", INTERLEAVE, ";\n") # from François Michonneau (2009-10-02)
131 fcat(indent,"MATRIX\n")
137 fcat("BEGIN TAXA;\n")
138 fcat(indent, "DIMENSIONS", " ", NTAX, ";\n")
139 fcat(indent, "TAXLABELS\n")
143 fcat(names(x[i]), " ")
146 fcat("\n", indent, ";\n")
150 fcat("\n", indent, indent)
156 fcat("BEGIN CHARACTERS;\n")
157 fcat(indent, "DIMENSIONS", " ", NCHAR, ";\n")
158 if (format %in% c("dna", "protein")) {
159 fcat(indent, "FORMAT", " ", MISSING, " ", GAP, " ", DATATYPE, " ", INTERLEAVE, ";\n")
161 fcat(indent,"MATRIX\n")