This is great! This can also read other types of connection. I made a few modifications so that the function can read remote files through HTTPS or FTPS and this fixes the crash when reading empty files (or compressed files without specifying a connection). For this fix, it is needed to recompile ape, but the modified read.FASTA (pasted below) can be used without recompilation.



read.FASTA <- function(file)
    if (length(grep("^(ht|f)tp(s|):", file))) {
        url <- file
        file <- tempfile()
        download.file(url, file)
    if (is(file, "connection")) {
        if (!isOpen(file, "rt")) {
            open(file, "rt")
        x <- scan(file, what = character(), sep = "\n", quiet = TRUE)
        x <- charToRaw(paste(x, collapse = "\n"))
        sz <- length(x)
    } else {
        sz <- file.size(file)
        x <- readBin(file, "raw", sz)
    ## if the file is larger than 1 Gb we assume that it is
    ## UNIX-encoded and skip the search-replace of carriage returns
    if (sz < 1e9) {
        icr <- which(x == as.raw(0x0d)) # CR
        if (length(icr)) x <- x[-icr]
    res <- .Call("rawStreamToDNAbin", x)
    if (identical(res, 0L)) {
        warning("failed to read sequences, returns NULL")
    names(res) <- sub("^ +", "", names(res)) # to permit phylosim
    class(res) <- "DNAbin"

Le 04/01/2017 à 00:37, Rj Ewing a écrit :

Thanks, I've attached a patch containing the changes.

I'm not very familiar with R, so maybe there is a better way to do it.
Let me know if you want me to make any changes.


On Tue, Jan 3, 2017 at 12:53 AM, Emmanuel Paradis
< <>> wrote:


    You can send your contribution directly to me. See this post for a
    recent discussion on how to contribute to ape: 

    Sure your contribution will be useful. I've just found out that
    read.FASTA crashes R if the FASTA file is gz'ed. It will be the
    opportunity to fix this too.



    Le 02/01/2017 à 19:26, Rj Ewing a écrit :

        Is there a way to contribute to the project? I would like to add the
        ability for read.FASTA to accept a connection or a file.

        I have a zip file that I would rather not unzip before passing into
        read.FASTA. I can obtain a connection using unz("",
        "myFasta.fasta"), however read.FASTA does not support this.


                [[alternative HTML version deleted]]

        R-sig-phylo mailing list -
        Searchable archive at

        Pour nous remonter une erreur de filtrage, veuillez vous rendre
        ici :

R-sig-phylo mailing list -
Searchable archive at

Reply via email to