3 ## Manipulations and Comparisons of DNA Sequences
5 ## Copyright 2002-2010 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 labels.DNAbin <- function(object, ...)
12 if (is.list(object)) return(names(object))
13 if (is.matrix(object)) return(rownames(object))
17 del.gaps <- function(x)
19 deleteGaps <- function(x) {
21 if (length(i)) x[-i] else x
24 if (!inherits(x, "DNAbin")) x <- as.DNAbin(x)
27 y <- vector("list", n)
28 for (i in 1:n) y[[i]] <- x[i, ]
29 names(y) <- rownames(x)
33 if (!is.list(x)) return(deleteGaps(x))
34 x <- lapply(x, deleteGaps)
39 as.alignment <- function(x)
41 if (is.list(x)) n <- length(x)
42 if (is.matrix(x)) n <- dim(x)[1]
47 seq[i] <- paste(x[[i]], collapse = "")
50 nam <- dimnames(x)[[1]]
52 seq[i] <- paste(x[i, ], collapse = "")
54 obj <- list(nb = n, seq = seq, nam = nam, com = NA)
55 class(obj) <- "alignment"
59 "[.DNAbin" <- function(x, i, j, drop = FALSE)
64 if (nargs() == 2 && !missing(i)) ans <- x[i]
67 if (missing(i)) i <- 1:nd[1]
68 if (missing(j)) j <- 1:nd[2]
69 ans <- x[i, j, drop = drop]
72 if (missing(i)) i <- 1:length(x)
79 as.matrix.DNAbin <- function(x, ...)
82 if (length(unique(unlist(lapply(x, length)))) != 1)
83 stop("DNA sequences in list not of the same length.")
87 x <- matrix(unlist(x), n, s, byrow = TRUE)
94 as.list.DNAbin <- function(x, ...)
96 if (is.list(x)) return(x)
97 if (is.vector(x)) obj <- list(x)
100 obj <- vector("list", n)
101 for (i in 1:n) obj[[i]] <- x[i, ]
102 names(obj) <- rownames(x)
104 class(obj) <- "DNAbin"
108 rbind.DNAbin <- function(...)
109 ### works only with matrices for the moment
113 if (n == 1) return(obj[[1]])
115 if (!is.matrix(obj[[1]]))
116 stop("the 'rbind' method for \"DNAbin\" accepts only matrices")
117 NC <- unlist(lapply(obj, ncol))
118 if (length(unique(NC)) > 1)
119 stop("matrices do not have the same number of columns.")
120 for (i in 1:n) class(obj[[i]]) <- NULL
121 for (i in 2:n) obj[[1]] <- rbind(obj[[1]], obj[[i]])
122 structure(obj[[1]], class = "DNAbin")
126 function(..., check.names = TRUE, fill.with.gaps = FALSE,
128 ### works only with matrices for the moment
132 if (n == 1) return(obj[[1]])
134 if (!is.matrix(obj[[1]]))
135 stop("the 'cbind' method for \"DNAbin\" accepts only matrices")
136 NR <- unlist(lapply(obj, nrow))
137 for (i in 1:n) class(obj[[i]]) <- NULL
139 nms <- unlist(lapply(obj, rownames))
140 if (fill.with.gaps) {
141 NC <- unlist(lapply(obj, ncol))
143 ans <- matrix(as.raw(4), length(nms), sum(NC))
147 to <- from + NC[i] - 1
148 tmp <- rownames(obj[[i]])
149 nmsi <- tmp[tmp %in% nms]
150 ans[nmsi, from:to] <- obj[[i]][nmsi, , drop = FALSE]
156 nms <- names(tab)[which(ubi)]
157 ans <- obj[[1]][nms, , drop = FALSE]
159 ans <- cbind(ans, obj[[i]][nms, , drop = FALSE])
160 if (!quiet && !all(ubi))
161 warning("some rows were dropped.")
164 if (length(unique(NR)) > 1)
165 stop("matrices do not have the same number of rows.")
166 ans <- matrix(unlist(obj), NR)
167 rownames(ans) <- rownames(obj[[1]])
169 class(ans) <- "DNAbin"
173 c.DNAbin <- function(..., recursive = FALSE)
175 if (!all(unlist(lapply(list(...), is.list))))
176 stop("the 'c' method for \"DNAbin\" accepts only lists")
177 structure(NextMethod("c"), class = "DNAbin")
180 print.DNAbin <- function(x, printlen = 6, digits = 3, ...)
186 cat("1 DNA sequence in binary format stored in a list.\n\n")
187 cat("Sequence length:", length(x[[1]]), "\n\n")
188 cat("Label:", nms, "\n\n")
190 cat(n, "DNA sequences in binary format stored in a list.\n\n")
191 tmp <- unlist(lapply(x, length))
195 cat("All sequences of same length:", maxi, "\n")
197 cat("Mean sequence length:", round(mean(tmp), 3), "\n")
198 cat(" Shortest sequence:", mini, "\n")
199 cat(" Longest sequence:", maxi, "\n")
203 nms <- nms[1:printlen]
206 cat("\nLabels:", paste(nms, collapse = " "), TAIL)
208 } else if (is.matrix(x)) {
211 cat(nd[1], "DNA sequences in binary format stored in a matrix.\n\n")
212 cat("All sequences of same length:", nd[2], "\n")
214 if (printlen < nd[1]) {
215 nms <- nms[1:printlen]
218 cat("\nLabels:", paste(nms, collapse = " "), TAIL)
220 cat("1 DNA sequence in binary format stored in a vector.\n\n")
221 cat("Sequence length:", length(x), "\n\n")
223 cat("Base composition:\n")
224 print(round(base.freq(x), digits))
227 as.DNAbin <- function(x, ...) UseMethod("as.DNAbin")
229 ._cs_<- letters[c(1, 7, 3, 20, 18, 13, 23, 19, 11, 25, 22, 8, 4, 2, 14)]
231 ._bs_<- c(136, 72, 40, 24, 192, 160, 144, 96, 80, 48, 224, 176, 208, 112, 240)
233 as.DNAbin.character <- function(x, ...)
238 ans[which(x == ._cs_[i])] <- as.raw(._bs_[i])
239 ans[which(x == "-")] <- as.raw(4)
240 ans[which(x == "?")] <- as.raw(2)
243 dimnames(ans) <- dimnames(x)
245 class(ans) <- "DNAbin"
249 as.DNAbin.alignment <- function(x, ...)
252 x$seq <- tolower(x$seq)
253 ans <- matrix("", n, nchar(x$seq[1]))
255 ans[i, ] <- strsplit(x$seq[i], "")[[1]]
256 rownames(ans) <- gsub(" +$", "", gsub("^ +", "", x$nam))
257 as.DNAbin.character(ans)
260 as.DNAbin.list <- function(x, ...)
262 obj <- lapply(x, as.DNAbin)
263 class(obj) <- "DNAbin"
267 as.character.DNAbin <- function(x, ...)
270 ans <- character(length(xx))
272 ans[which(xx == ._bs_[i])] <- ._cs_[i]
273 ans[which(xx == 4)] <- "-"
274 ans[which(xx == 2)] <- "?"
277 dimnames(ans) <- dimnames(xx)
281 if (is.list(x)) lapply(x, f) else f(x)
284 base.freq <- function(x, freq = FALSE)
286 if (is.list(x)) x <- unlist(x)
288 BF <- .C("BaseProportion", x, n, double(4), freq,
289 DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")[[3]]
290 names(BF) <- letters[c(1, 3, 7, 20)]
294 GC.content <- function(x) sum(base.freq(x)[2:3])
296 seg.sites <- function(x)
298 if (is.list(x)) x <- as.matrix(x)
299 if (is.vector(x)) n <- 1
300 else { # 'x' is a matrix
305 if (n == 1) return(integer(0))
306 ans <- .C("SegSites", x, n, s, integer(s),
307 DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
308 which(as.logical(ans[[4]]))
311 dist.dna <- function(x, model = "K80", variance = FALSE, gamma = FALSE,
312 pairwise.deletion = FALSE, base.freq = NULL,
315 MODELS <- c("RAW", "JC69", "K80", "F81", "K81", "F84", "T92", "TN93",
316 "GG95", "LOGDET", "BH87", "PARALIN", "N", "TS", "TV")
317 imod <- pmatch(toupper(model), MODELS)
319 stop(paste("'model' must be one of:",
320 paste("\"", MODELS, "\"", sep = "", collapse = " ")))
321 if (imod == 11 && variance) {
322 warning("computing variance temporarily not available for model BH87.")
325 if (gamma && imod %in% c(1, 5:7, 9:15)) {
326 warning(paste("gamma-correction not available for model", model))
329 if (is.list(x)) x <- as.matrix(x)
330 nms <- dimnames(x)[[1]]
334 if (imod %in% c(4, 6:8)) {
335 BF <- if (is.null(base.freq)) base.freq(x) else base.freq
337 if (!pairwise.deletion) {
338 keep <- .C("GlobalDeletionDNA", x, n, s,
339 rep(1L, s), PACKAGE = "ape")[[4]]
340 x <- x[, as.logical(keep)]
343 Ndist <- if (imod == 11) n*n else n*(n - 1)/2
344 var <- if (variance) double(Ndist) else 0
345 if (!gamma) gamma <- alpha <- 0
346 else alpha <- gamma <- 1
347 d <- .C("dist_dna", x, n, s, imod, double(Ndist), BF,
348 as.integer(pairwise.deletion), as.integer(variance),
349 var, as.integer(gamma), alpha, DUP = FALSE, NAOK = TRUE,
351 if (variance) var <- d[[9]]
355 dimnames(d) <- list(nms, nms)
358 attr(d, "Labels") <- nms
359 attr(d, "Diag") <- attr(d, "Upper") <- FALSE
360 attr(d, "call") <- match.call()
361 attr(d, "method") <- model
363 if (as.matrix) d <- as.matrix(d)
365 if (variance) attr(d, "variance") <- var