1 ## read.dna.R (2012-12-27)
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.FASTA <- function(file)
12 sz <- file.info(file)$size
13 x <- readBin(file, "raw", sz)
14 if (Sys.info()[1] == "Windows") {
15 icr <- which(x == as.raw(0x0d)) # CR
18 res <- .Call("rawStreamToDNAbin", x, PACKAGE = "ape")
19 class(res) <- "DNAbin"
23 read.dna <- function(file, format = "interleaved", skip = 0,
24 nlines = 0, comment.char = "#",
25 as.character = FALSE, as.matrix = NULL)
27 findFirstNucleotide <- function(x) {
28 ## actually find the 1st non-blank character
29 ## just in case: pat.base <- "[-AaCcGgTtUuMmRrWwSsYyKkVvHhDdBbNn?]{10}"
30 tmp <- regexpr("[[:blank:]]+", x[1]) # consider only a single string
31 tmp[1] + attr(tmp, "match.length")
33 getTaxaNames <- function(x) {
34 x <- sub("^['\" ]+", "", x) # remove the leading quotes and spaces
35 x <- sub("['\" ]+$", "", x) # " " trailing " " "
38 getNucleotide <- function(x) {
40 x <- strsplit(x, NULL)
43 formats <- c("interleaved", "sequential", "fasta", "clustal")
44 format <- match.arg(format, formats)
45 if (format == "fasta") {
46 obj <- read.FASTA(file)
48 X <- scan(file = file, what = "", sep = "\n", quiet = TRUE,
49 skip = skip, nlines = nlines, comment.char = comment.char)
51 if (format %in% formats[1:2]) {
52 ## need to remove the possible leading spaces and/or tabs in the first line
53 fl <- gsub("^[[:blank:]]+", "", X[1])
54 fl <- as.numeric(unlist(strsplit(fl, "[[:blank:]]+")))
55 if (length(fl) != 2 || any(is.na(fl)))
56 stop("the first line of the file must contain the dimensions of the data")
59 obj <- matrix("", n, s)
64 start.seq <- findFirstNucleotide(X[1])
66 taxa <- getTaxaNames(substr(X[one2n], 1, start.seq - 1))
67 X[one2n] <- substr(X[one2n], start.seq, nchar(X[one2n]))
70 obj[i, ] <- getNucleotide(X[seq(i, nl, n)])
76 start.seq <- findFirstNucleotide(X[j])
77 taxa[i] <- getTaxaNames(substr(X[j], 1, start.seq - 1))
78 sequ <- getNucleotide(substr(X[j], start.seq, nchar(X[j])))
80 while (length(sequ) < s) {
81 sequ <- c(sequ, getNucleotide(X[j]))
86 taxa <- getTaxaNames(taxa)
89 X <- X[-1] # drop the line with "Clustal bla bla..."
90 ## find where the 1st sequence starts
91 start.seq <- findFirstNucleotide(X[1])
92 ## find the lines with *********....
93 nspaces <- paste("^ {", start.seq - 1, "}", sep = "", collapse = "")
94 stars <- grep(nspaces, X)
95 ## we now know how many sequences in the file:
97 taxa <- getTaxaNames(substr(X[1:n], 1, start.seq - 1))
98 ## need to remove the sequence names before getting the sequences:
99 X <- substr(X, start.seq, nchar(X))
101 ## find the length of the 1st sequence:
102 tmp <- getNucleotide(X[seq(1, nl, n + 1)])
104 obj <- matrix("", n, s)
107 obj[i, ] <- getNucleotide(X[seq(i, nl, n + 1)])
110 if (format != "fasta") {
111 rownames(obj) <- taxa
113 LENGTHS <- unique(unlist(lapply(obj, length)))
114 allSameLength <- length(LENGTHS) == 1
115 if (is.logical(as.matrix)) {
116 if (as.matrix && !allSameLength)
117 stop("sequences in FASTA file not of the same length")
119 as.matrix <- allSameLength
122 obj <- matrix(unlist(obj), ncol = LENGTHS, byrow = TRUE)
123 rownames(obj) <- taxa
126 if (!as.character) obj <- as.DNAbin(obj)