]> git.donarmstrong.com Git - ape.git/blobdiff - R/read.dna.R
fixing drop.tip and bionj
[ape.git] / R / read.dna.R
index 8117906a5bdc54cdc9901f78b21f7b2f8b8e386b..88f3d61abd7a17757dae0a8d85e80cae07f08d48 100644 (file)
@@ -1,15 +1,15 @@
-## read.dna.R (2008-07-03)
+## read.dna.R (2011-02-01)
 
 ##   Read DNA Sequences in a File
 
-## Copyright 2003-2008 Emmanuel Paradis
+## Copyright 2003-2011 Emmanuel Paradis
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
 
 read.dna <- function(file, format = "interleaved", skip = 0,
                      nlines = 0, comment.char = "#", seq.names = NULL,
-                     as.character = FALSE)
+                     as.character = FALSE, as.matrix = NULL)
 {
     getTaxaNames <- function(x) {
         x <- sub("^['\" ]+", "", x) # remove the leading quotes and spaces
@@ -105,6 +105,18 @@ read.dna <- function(file, format = "interleaved", skip = 0,
     } else {
         names(obj) <- seq.names
         obj <- lapply(obj, tolower)
+        LENGTHS <- unique(unlist(lapply(obj, length)))
+        allSameLength <- length(LENGTHS) == 1
+        if (is.logical(as.matrix)) {
+            if (as.matrix && !allSameLength)
+                stop("sequences in FASTA file not of the same length")
+        } else {
+            as.matrix <- allSameLength
+        }
+        if (as.matrix) {
+            obj <- matrix(unlist(obj), ncol = LENGTHS, byrow = TRUE)
+            rownames(obj) <- seq.names
+        }
     }
     if (!as.character) obj <- as.DNAbin(obj)
     obj