1 ## read.dna.R (2012-05-03)
3 ## Read DNA Sequences in a File
5 ## Copyright 2003-2012 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 read.dna <- function(file, format = "interleaved", skip = 0,
11 nlines = 0, comment.char = "#",
12 as.character = FALSE, as.matrix = NULL)
14 findFirstNucleotide <- function(x) {
15 ## actually find the 1st non-blank character
16 ## just in case: pat.base <- "[-AaCcGgTtUuMmRrWwSsYyKkVvHhDdBbNn?]{10}"
17 tmp <- regexpr("[[:blank:]]+", x[1]) # consider only a single string
18 tmp[1] + attr(tmp, "match.length")
20 getTaxaNames <- function(x) {
21 x <- sub("^['\" ]+", "", x) # remove the leading quotes and spaces
22 x <- sub("['\" ]+$", "", x) # " " trailing " " "
25 getNucleotide <- function(x) {
27 x <- strsplit(x, NULL)
30 formats <- c("interleaved", "sequential", "fasta", "clustal")
31 format <- match.arg(format, formats)
32 phylip <- if (format %in% formats[1:2]) TRUE else FALSE
33 X <- scan(file = file, what = "", sep = "\n", quiet = TRUE,
34 skip = skip, nlines = nlines, comment.char = comment.char)
37 ## need to remove the possible leading spaces and/or tabs in the first line
38 fl <- gsub("^[[:blank:]]+", "", X[1])
39 fl <- as.numeric(unlist(strsplit(fl, "[[:blank:]]+")))
40 if (length(fl) != 2 || any(is.na(fl)))
41 stop("the first line of the file must contain the dimensions of the data")
44 obj <- matrix("", n, s)
49 start.seq <- findFirstNucleotide(X[1])
51 taxa <- getTaxaNames(substr(X[one2n], 1, start.seq - 1))
52 X[one2n] <- substr(X[one2n], start.seq, nchar(X[one2n]))
55 obj[i, ] <- getNucleotide(X[seq(i, nl, n)])
61 start.seq <- findFirstNucleotide(X[j])
62 taxa[i] <- getTaxaNames(substr(X[j], 1, start.seq - 1))
63 sequ <- getNucleotide(substr(X[j], start.seq, nchar(X[j])))
65 while (length(sequ) < s) {
66 sequ <- c(sequ, getNucleotide(X[j]))
71 taxa <- getTaxaNames(taxa)
74 start <- grep("^ {0,}>", X)
76 taxa <- sub("^ {0,}>", "", taxa) # remove the hook and the spaces before
77 taxa <- getTaxaNames(taxa)
79 obj <- vector("list", n)
80 start <- c(start, length(X) + 1) # this avoids the following to crash when `i = n'
82 obj[[i]] <- getNucleotide(X[(start[i] + 1):(start[i + 1] - 1)])
85 X <- X[-1] # drop the line with "Clustal bla bla..."
86 ## find where the 1st sequence starts
87 start.seq <- findFirstNucleotide(X[1])
88 ## find the lines with *********....
89 nspaces <- paste("^ {", start.seq - 1, "}", sep = "", collapse = "")
90 stars <- grep(nspaces, X)
91 ## we now know how many sequences in the file:
93 taxa <- getTaxaNames(substr(X[1:n], 1, start.seq - 1))
94 ## need to remove the sequence names before getting the sequences:
95 X <- substr(X, start.seq, nchar(X))
97 ## find the length of the 1st sequence:
98 tmp <- getNucleotide(X[seq(1, nl, n + 1)])
100 obj <- matrix("", n, s)
103 obj[i, ] <- getNucleotide(X[seq(i, nl, n + 1)])
106 if (format != "fasta") {
107 rownames(obj) <- taxa
110 LENGTHS <- unique(unlist(lapply(obj, length)))
111 allSameLength <- length(LENGTHS) == 1
112 if (is.logical(as.matrix)) {
113 if (as.matrix && !allSameLength)
114 stop("sequences in FASTA file not of the same length")
116 as.matrix <- allSameLength
119 obj <- matrix(unlist(obj), ncol = LENGTHS, byrow = TRUE)
120 rownames(obj) <- taxa
123 if (!as.character) obj <- as.DNAbin(obj)