1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
## write.nexus.data.R (2018-06-23)
## Write Character Data in NEXUS Format
## Copyright 2006-2015 Johan Nylander, Emmanuel Paradis, 2018 Thomas Guillerme
## This file is part of the R-package `ape'.
## See the file ../COPYING for licensing issues.
write.nexus.data <-
function(x, file, format = "dna", datablock = TRUE,
interleaved = TRUE, charsperline = NULL,
gap = NULL, missing = NULL)
{
### TODO: Standard data, mixed data, nice indent
format <- match.arg(toupper(format), c("DNA", "PROTEIN", "STANDARD", "CONTINUOUS"))
if (inherits(x, "DNAbin") && format != "DNA") {
format <- "DNA"
warning("object 'x' is of class DNAbin: format forced to DNA")
}
if (inherits(x, "AAbin") && format != "PROTEIN") {
format <- "PROTEIN"
warning("object 'x' is of class AAbin: format forced to PROTEIN")
}
indent <- " " # Two blanks
maxtax <- 5 # Max nr of taxon names to be printed on a line
defcharsperline <- 80 # Default nr of characters per line if interleaved
defgap <- "-" # Default gap character
defmissing <- "?" # Default missing data character
if (is.matrix(x)) {
if (inherits(x, "DNAbin")) x <- as.list(x) else {
xbak <- x
x <- vector("list", nrow(xbak))
for (i in seq_along(x)) x[[i]] <- xbak[i, ]
names(x) <- rownames(xbak)
rm(xbak)
}
}
ntax <- length(x)
nchars <- length(x[[1]])
zz <- file(file, "w")
if (is.null(names(x))) names(x) <- as.character(1:ntax)
fcat <- function(..., file = zz)
cat(..., file = file, sep = "", append = TRUE)
find.max.length <- function(x) max(nchar(x))
print.matrix <- function(x, dindent = " ", collapse = "") {
Names <- names(x)
printlength <- find.max.length(Names) + 2
if (!interleaved) {
for (i in seq_along(x)) {
sequence <- paste(x[[i]], collapse = collapse)
taxon <- Names[i]
thestring <- sprintf("%-*s%s%s", printlength, taxon, dindent, sequence)
fcat(indent, indent, thestring, "\n")
}
} else {
ntimes <- ceiling(nchars/charsperline)
start <- 1
end <- charsperline
for (j in seq_len(ntimes)) {
for (i in seq_along(x)) {
sequence <- paste(x[[i]][start:end], collapse = collapse)
taxon <- Names[i]
thestring <- sprintf("%-*s%s%s", printlength, taxon, dindent, sequence)
fcat(indent, indent, thestring, "\n")
}
if (j < ntimes) fcat("\n")
start <- start + charsperline
end <- end + charsperline
if (end > nchars) end <- nchars
}
}
}
if (inherits(x, "DNAbin") || inherits(x, "AAbin")) x <- as.character(x)
fcat("#NEXUS\n[Data written by write.nexus.data.R, ", date(), "]\n")
NCHAR <- paste("NCHAR=", nchars, sep = "")
NTAX <- paste0("NTAX=", ntax)
DATATYPE <- paste0("DATATYPE=", format) # fix by Robin Cristofari (2015-02-04)
if (is.null(charsperline)) {
if (nchars <= defcharsperline) {
charsperline <- nchars
interleaved <- FALSE
} else charsperline <- defcharsperline
}
if (is.null(missing)) missing <- defmissing
MISSING <- paste0("MISSING=", missing)
if (is.null(gap)) gap <- defgap
GAP <- paste0("GAP=", gap)
INTERLEAVE <- if (interleaved) "INTERLEAVE=YES" else "INTERLEAVE=NO"
if (datablock) {
fcat("BEGIN DATA;\n")
fcat(indent, "DIMENSIONS ", NTAX, " ", NCHAR, ";\n")
## <FIXME> only DNA and PROTEIN is supported for the moment, so the
## following 'if' is not needed
## if (format %in% c("DNA", "PROTEIN")) # from Francois Michonneau (2009-10-02)
if(format != "STANDARD") {
fcat(indent, "FORMAT", " ", DATATYPE, " ", MISSING, " ", GAP, " ", INTERLEAVE, ";\n")
} else {
fcat(indent, "FORMAT", " ", DATATYPE, " ", MISSING, " ", GAP, " ", INTERLEAVE, " symbols=\"0123456789\";\n")
}
## </FIXME>
fcat(indent, "MATRIX\n")
if(format != "CONTINUOUS") {
print.matrix(x)
} else {
print.matrix(x, collapse = "\t")
}
fcat(indent, ";\nEND;\n\n")
} else {
fcat("BEGIN TAXA;\n")
fcat(indent, "DIMENSIONS", " ", NTAX, ";\n")
fcat(indent, "TAXLABELS\n")
fcat(indent, indent)
j <- 0
for (i in seq_len(ntax)) {
fcat(names(x[i]), " ")
j <- j + 1
if (j == maxtax) {
fcat("\n", indent, indent)
j <- 0
}
}
fcat("\n", indent, ";\n")
fcat("END;\n\nBEGIN CHARACTERS;\n")
fcat(indent, "DIMENSIONS", " ", NCHAR, ";\n")
## <FIXME> only DNA and PROTEIN is supported for the moment, so the
## following 'if' is not needed
## if (format %in% c("DNA", "PROTEIN"))
if(format != "STANDARD") {
fcat(indent, "FORMAT", " ", MISSING, " ", GAP, " ", DATATYPE, " ", INTERLEAVE, ";\n")
} else {
fcat(indent, "FORMAT", " ", MISSING, " ", GAP, " ", DATATYPE, " ", INTERLEAVE, " symbols=\"0123456789\";\n")
}
## </FIXME>
fcat(indent,"MATRIX\n")
if(format != "CONTINUOUS") {
print.matrix(x)
} else {
print.matrix(x, collapse = "\t")
}
fcat(indent, ";\nEND;\n\n")
}
close(zz)
}
|