]> git.donarmstrong.com Git - ape.git/blobdiff - R/read.dna.R
new chronos files and a bunch of various improvements
[ape.git] / R / read.dna.R
index 9e46cba67bbc81b5eed7624232e5c8185519e5c2..4b2ddd8e2a65dd7b664071251fd17824320a160e 100644 (file)
@@ -1,4 +1,4 @@
-## read.dna.R (2012-12-27)
+## read.dna.R (2013-01-04)
 
 ##   Read DNA Sequences in a File
 
@@ -47,7 +47,6 @@ read.dna <- function(file, format = "interleaved", skip = 0,
     } else {
         X <- scan(file = file, what = "", sep = "\n", quiet = TRUE,
                   skip = skip, nlines = nlines, comment.char = comment.char)
-
         if (format %in% formats[1:2]) {
             ## need to remove the possible leading spaces and/or tabs in the first line
             fl <- gsub("^[[:blank:]]+", "", X[1])
@@ -106,9 +105,10 @@ read.dna <- function(file, format = "interleaved", skip = 0,
                    for (i in 2:n)
                        obj[i, ] <- getNucleotide(X[seq(i, nl, n + 1)])
                })
-
+    }
     if (format != "fasta") {
         rownames(obj) <- taxa
+        if (!as.character) obj <- as.DNAbin(obj)
     } else {
         LENGTHS <- unique(unlist(lapply(obj, length)))
         allSameLength <- length(LENGTHS) == 1
@@ -119,10 +119,15 @@ read.dna <- function(file, format = "interleaved", skip = 0,
             as.matrix <- allSameLength
         }
         if (as.matrix) {
-            obj <- matrix(unlist(obj), ncol = LENGTHS, byrow = TRUE)
+            taxa <- names(obj)
+            n <- length(obj)
+            y <- matrix(as.raw(0), n, LENGTHS)
+            for (i in seq_len(n)) y[i, ] <- obj[[i]]
+            obj <- y
             rownames(obj) <- taxa
+            class(obj) <- "DNAbin"
         }
+        if (as.character) obj <- as.character(obj)
     }
-    if (!as.character) obj <- as.DNAbin(obj)
     obj
 }