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) {
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 (nobj == 1) stop("only one matrix to bind.")
93 if(ncol(obj[[i]]) != NC)
94 stop("matrices do not have the same number of columns.")
95 for (i in 1:nobj) class(obj[[i]]) <- NULL
97 for (i in 2:nobj) ans <- rbind(ans, obj[[i]])
98 structure(ans, class = "DNAbin")
101 cbind.DNAbin <- function(..., check.names = TRUE)
102 ### works only with matrices for the moment
106 if (nobj == 1) stop("only one matrix to bind.")
109 if(nrow(obj[[i]]) != NR)
110 stop("matrices do not have the same number of rows.")
111 for (i in 1:nobj) class(obj[[i]]) <- NULL
112 nms <- rownames(obj[[1]])
115 if (all(rownames(obj[[i]]) %in% nms))
116 obj[[i]] <- obj[[i]][nms, ]
117 else stop("rownames do not match among matrices.")
119 ans <- matrix(unlist(obj), NR)
121 structure(ans, class = "DNAbin")
124 print.DNAbin <- function(x, ...)
126 n <- 1 # <- if is.vector(x)
127 if (is.list(x)) n <- length(x)
128 else if (is.matrix(x)) n <- dim(x)[1]
129 if (n > 1) cat(n, "DNA sequences in binary format.\n")
130 else cat("1 DNA sequence in binary format.\n")
133 summary.DNAbin <- function(object, printlen = 6, digits = 3, ...)
135 if (is.list(object)) {
139 cat("1 DNA sequence in binary format stored in a list.\n\n")
140 cat("Sequence length:", length(object[[1]]), "\n\n")
141 cat("Label:", nms, "\n\n")
143 cat(n, "DNA sequences in binary format stored in a list.\n\n")
144 cat("Summary of sequence lengths:\n")
145 print(summary(unlist(lapply(object, length))))
148 nms <- nms[1:printlen]
151 cat("\nLabels:", paste(nms, collapse = " "), TAIL)
153 } else if (is.matrix(object)) {
155 nms <- rownames(object)
156 cat(nd[1], "DNA sequences in binary format stored in a matrix.\n\n")
157 cat("All sequences of same length:", nd[2], "\n")
159 if (printlen < nd[1]) {
160 nms <- nms[1:printlen]
163 cat("\nLabels:", paste(nms, collapse = " "), TAIL)
165 cat("1 DNA sequence in binary format stored in a vector.\n\n")
166 cat("Sequence length:", length(object), "\n\n")
168 cat("Base composition:\n")
169 print(round(base.freq(object), digits))
172 as.DNAbin <- function(x, ...) UseMethod("as.DNAbin")
174 ._cs_<- letters[c(1, 7, 3, 20, 18, 13, 23, 19, 11, 25, 22, 8, 4, 2, 14)]
176 ._bs_<- c(136, 72, 40, 24, 192, 160, 144, 96, 80, 48, 224, 176, 208, 112, 240)
178 as.DNAbin.character <- function(x, ...)
183 ans[which(x == ._cs_[i])] <- as.raw(._bs_[i])
184 ans[which(x == "-")] <- as.raw(4)
185 ans[which(x == "?")] <- as.raw(2)
188 dimnames(ans) <- dimnames(x)
190 class(ans) <- "DNAbin"
194 as.DNAbin.alignment <- function(x, ...)
197 x$seq <- tolower(x$seq)
198 ans <- matrix("", n, nchar(x$seq[1]))
200 ans[i, ] <- strsplit(x$seq[i], "")[[1]]
201 rownames(ans) <- gsub(" +$", "", gsub("^ +", "", x$nam))
202 as.DNAbin.character(ans)
205 as.DNAbin.list <- function(x, ...)
207 obj <- lapply(x, as.DNAbin)
208 class(obj) <- "DNAbin"
212 as.character.DNAbin <- function(x, ...)
215 ans <- character(length(xx))
217 ans[which(xx == ._bs_[i])] <- ._cs_[i]
218 ans[which(xx == 4)] <- "-"
219 ans[which(xx == 2)] <- "?"
222 dimnames(ans) <- dimnames(xx)
226 if (is.list(x)) lapply(x, f) else f(x)
229 base.freq <- function(x)
231 if (is.list(x)) x <- unlist(x)
233 BF <- .C("BaseProportion", x, n, double(4),
234 DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")[[3]]
235 names(BF) <- letters[c(1, 3, 7, 20)]
239 GC.content <- function(x) sum(base.freq(x)[2:3])
241 seg.sites <- function(x)
243 if (is.list(x)) x <- as.matrix(x)
247 ans <- .C("SegSites", x, n, s, integer(s),
248 DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
249 which(as.logical(ans[[4]]))
252 nuc.div <- function(x, variance = FALSE, pairwise.deletion = FALSE)
254 if (pairwise.deletion && variance)
255 warning("cannot compute the variance of nucleotidic diversity\nwith pairwise deletion: try 'pairwise.deletion = FALSE' instead.")
256 if (is.list(x)) x <- as.matrix(x)
258 ans <- sum(dist.dna(x, "raw", pairwise.deletion = pairwise.deletion))/
261 var <- (n + 1)*ans/(3*(n + 1)*dim(x)[2]) + 2*(n^2 + n + 3)*ans/(9*n*(n - 1))
267 dist.dna <- function(x, model = "K80", variance = FALSE, gamma = FALSE,
268 pairwise.deletion = FALSE, base.freq = NULL,
271 MODELS <- c("RAW", "JC69", "K80", "F81", "K81", "F84", "T92", "TN93",
272 "GG95", "LOGDET", "BH87", "PARALIN")
273 imod <- which(MODELS == toupper(model))
274 if (imod == 11 && variance) {
275 warning("computing variance temporarily not available for model BH87.")
278 if (gamma && imod %in% c(1, 5:7, 9:12)) {
279 warning(paste("gamma-correction not available for model", model))
282 if (is.list(x)) x <- as.matrix(x)
283 nms <- dimnames(x)[[1]]
287 BF <- if (is.null(base.freq)) base.freq(x) else base.freq
288 if (!pairwise.deletion) {
289 keep <- .C("GlobalDeletionDNA", x, n, s,
290 rep(1L, s), PACKAGE = "ape")[[4]]
291 x <- x[, as.logical(keep)]
294 Ndist <- if (imod == 11) n*n else n*(n - 1)/2
295 var <- if (variance) double(Ndist) else 0
296 if (!gamma) gamma <- alpha <- 0
297 else alpha <- gamma <- 1
298 d <- .C("dist_dna", x, n, s, imod, double(Ndist), BF,
299 as.integer(pairwise.deletion), as.integer(variance),
300 var, as.integer(gamma), alpha, DUP = FALSE, NAOK = TRUE,
302 if (variance) var <- d[[9]]
306 dimnames(d) <- list(nms, nms)
309 attr(d, "Labels") <- nms
310 attr(d, "Diag") <- attr(d, "Upper") <- FALSE
311 attr(d, "call") <- match.call()
312 attr(d, "method") <- model
314 if (as.matrix) d <- as.matrix(d)
316 if (variance) attr(d, "variance") <- var