3 ## Manipulations and Comparisons of DNA Sequences
5 ## Copyright 2002-2008 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 del.gaps <- function(x)
12 deleteGaps <- function(x) {
14 if (length(i)) x[-i] else x
17 if (class(x) != "DNAbin") x <- as.DNAbin(x)
20 y <- vector("list", n)
21 for (i in 1:n) y[[i]] <- x[i, ]
25 if (!is.list(x)) return(deleteGaps(x))
26 x <- lapply(x, deleteGaps)
31 as.alignment <- function(x)
33 if (is.list(x)) n <- length(x)
34 if (is.matrix(x)) n <- dim(x)[1]
39 seq[i] <- paste(x[[i]], collapse = "")
42 nam <- dimnames(x)[[1]]
44 seq[i] <- paste(x[i, ], collapse = "")
46 obj <- list(nb = n, seq = seq, nam = nam, com = NA)
47 class(obj) <- "alignment"
51 "[.DNAbin" <- function(x, i, j, drop = TRUE)
55 if (nargs() == 2 && !missing(i)) ans <- x[i]
58 if (missing(i)) i <- 1:nd[1]
59 if (missing(j)) j <- 1:nd[2]
60 ans <- x[i, j, drop = drop]
63 if (missing(i)) i <- 1:length(x)
66 structure(ans, class = "DNAbin")
69 as.matrix.DNAbin <- function(x, ...)
71 if (is.matrix(x)) return(x)
73 if (length(unique(unlist(lapply(x, length)))) != 1)
74 stop("DNA sequences in list not of the same length.")
78 x <- matrix(unlist(x), n, s, byrow = TRUE)
85 rbind.DNAbin <- function(...)
86 ### works only with matrices for the moment
90 if (n == 1) return(obj[[1]])
91 NC <- unlist(lapply(obj, ncol))
92 if (length(unique(NC)) > 1)
93 stop("matrices do not have the same number of columns.")
94 for (i in 1:n) class(obj[[i]]) <- NULL
95 for (i in 2:n) obj[[1]] <- rbind(obj[[1]], obj[[i]])
96 structure(obj[[1]], class = "DNAbin")
99 cbind.DNAbin <- function(..., check.names = TRUE, fill.with.gaps = FALSE,
101 ### works only with matrices for the moment
105 if (n == 1) return(obj[[1]])
106 NR <- unlist(lapply(obj, nrow))
107 if (length(unique(NR)) > 1)
108 stop("matrices do not have the same number of rows.")
109 for (i in 1:n) class(obj[[i]]) <- NULL
110 nms <- rownames(obj[[1]])
113 if (all(rownames(obj[[i]]) %in% nms))
114 obj[[i]] <- obj[[i]][nms, ]
115 else stop("rownames do not match among matrices.")
117 ans <- matrix(unlist(obj), NR)
119 structure(ans, class = "DNAbin")
122 print.DNAbin <- function(x, ...)
124 n <- 1 # <- if is.vector(x)
125 if (is.list(x)) n <- length(x)
126 else if (is.matrix(x)) n <- dim(x)[1]
127 if (n > 1) cat(n, "DNA sequences in binary format.\n")
128 else cat("1 DNA sequence in binary format.\n")
131 summary.DNAbin <- function(object, printlen = 6, digits = 3, ...)
133 if (is.list(object)) {
137 cat("1 DNA sequence in binary format stored in a list.\n\n")
138 cat("Sequence length:", length(object[[1]]), "\n\n")
139 cat("Label:", nms, "\n\n")
141 cat(n, "DNA sequences in binary format stored in a list.\n\n")
142 tmp <- unlist(lapply(object, length))
146 cat("All sequences of same length:", maxi, "\n")
148 cat("Mean sequence length:", round(mean(tmp), 3), "\n")
149 cat(" Shortest sequence:", mini, "\n")
150 cat(" Longest sequence:", maxi, "\n")
154 nms <- nms[1:printlen]
157 cat("\nLabels:", paste(nms, collapse = " "), TAIL)
159 } else if (is.matrix(object)) {
161 nms <- rownames(object)
162 cat(nd[1], "DNA sequences in binary format stored in a matrix.\n\n")
163 cat("All sequences of same length:", nd[2], "\n")
165 if (printlen < nd[1]) {
166 nms <- nms[1:printlen]
169 cat("\nLabels:", paste(nms, collapse = " "), TAIL)
171 cat("1 DNA sequence in binary format stored in a vector.\n\n")
172 cat("Sequence length:", length(object), "\n\n")
174 cat("Base composition:\n")
175 print(round(base.freq(object), digits))
178 as.DNAbin <- function(x, ...) UseMethod("as.DNAbin")
180 ._cs_<- letters[c(1, 7, 3, 20, 18, 13, 23, 19, 11, 25, 22, 8, 4, 2, 14)]
182 ._bs_<- c(136, 72, 40, 24, 192, 160, 144, 96, 80, 48, 224, 176, 208, 112, 240)
184 as.DNAbin.character <- function(x, ...)
189 ans[which(x == ._cs_[i])] <- as.raw(._bs_[i])
190 ans[which(x == "-")] <- as.raw(4)
191 ans[which(x == "?")] <- as.raw(2)
194 dimnames(ans) <- dimnames(x)
196 class(ans) <- "DNAbin"
200 as.DNAbin.alignment <- function(x, ...)
203 x$seq <- tolower(x$seq)
204 ans <- matrix("", n, nchar(x$seq[1]))
206 ans[i, ] <- strsplit(x$seq[i], "")[[1]]
207 rownames(ans) <- gsub(" +$", "", gsub("^ +", "", x$nam))
208 as.DNAbin.character(ans)
211 as.DNAbin.list <- function(x, ...)
213 obj <- lapply(x, as.DNAbin)
214 class(obj) <- "DNAbin"
218 as.character.DNAbin <- function(x, ...)
221 ans <- character(length(xx))
223 ans[which(xx == ._bs_[i])] <- ._cs_[i]
224 ans[which(xx == 4)] <- "-"
225 ans[which(xx == 2)] <- "?"
228 dimnames(ans) <- dimnames(xx)
232 if (is.list(x)) lapply(x, f) else f(x)
235 base.freq <- function(x)
237 if (is.list(x)) x <- unlist(x)
239 BF <- .C("BaseProportion", x, n, double(4),
240 DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")[[3]]
241 names(BF) <- letters[c(1, 3, 7, 20)]
245 GC.content <- function(x) sum(base.freq(x)[2:3])
247 seg.sites <- function(x)
249 if (is.list(x)) x <- as.matrix(x)
253 ans <- .C("SegSites", x, n, s, integer(s),
254 DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
255 which(as.logical(ans[[4]]))
258 nuc.div <- function(x, variance = FALSE, pairwise.deletion = FALSE)
260 if (pairwise.deletion && variance)
261 warning("cannot compute the variance of nucleotidic diversity\nwith pairwise deletion: try 'pairwise.deletion = FALSE' instead.")
262 if (is.list(x)) x <- as.matrix(x)
264 ans <- sum(dist.dna(x, "raw", pairwise.deletion = pairwise.deletion))/
267 var <- (n + 1)*ans/(3*(n + 1)*dim(x)[2]) + 2*(n^2 + n + 3)*ans/(9*n*(n - 1))
273 dist.dna <- function(x, model = "K80", variance = FALSE, gamma = FALSE,
274 pairwise.deletion = FALSE, base.freq = NULL,
277 MODELS <- c("RAW", "JC69", "K80", "F81", "K81", "F84", "T92", "TN93",
278 "GG95", "LOGDET", "BH87", "PARALIN")
279 imod <- which(MODELS == toupper(model))
280 if (imod == 11 && variance) {
281 warning("computing variance temporarily not available for model BH87.")
284 if (gamma && imod %in% c(1, 5:7, 9:12)) {
285 warning(paste("gamma-correction not available for model", model))
288 if (is.list(x)) x <- as.matrix(x)
289 nms <- dimnames(x)[[1]]
293 BF <- if (is.null(base.freq)) base.freq(x) else base.freq
294 if (!pairwise.deletion) {
295 keep <- .C("GlobalDeletionDNA", x, n, s,
296 rep(1L, s), PACKAGE = "ape")[[4]]
297 x <- x[, as.logical(keep)]
300 Ndist <- if (imod == 11) n*n else n*(n - 1)/2
301 var <- if (variance) double(Ndist) else 0
302 if (!gamma) gamma <- alpha <- 0
303 else alpha <- gamma <- 1
304 d <- .C("dist_dna", x, n, s, imod, double(Ndist), BF,
305 as.integer(pairwise.deletion), as.integer(variance),
306 var, as.integer(gamma), alpha, DUP = FALSE, NAOK = TRUE,
308 if (variance) var <- d[[9]]
312 dimnames(d) <- list(nms, nms)
315 attr(d, "Labels") <- nms
316 attr(d, "Diag") <- attr(d, "Upper") <- FALSE
317 attr(d, "call") <- match.call()
318 attr(d, "method") <- model
320 if (as.matrix) d <- as.matrix(d)
322 if (variance) attr(d, "variance") <- var