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.

Cheers,

Emmanuel


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")
            on.exit(close(file))
        }
        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")
        return(NULL)
    }
    names(res) <- sub("^ +", "", names(res)) # to permit phylosim
    class(res) <- "DNAbin"
    res
}

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

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.

Thanks,
RJ

On Tue, Jan 3, 2017 at 12:53 AM, Emmanuel Paradis
<emmanuel.para...@ird.fr <mailto:emmanuel.para...@ird.fr>> wrote:

    Hi,

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

    http://www.mail-archive.com/r-sig-phylo@r-project.org/msg04580.htmlra 
<http://www.mail-archive.com/r-sig-phylo@r-project.org/msg04580.htmlra>

    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.

    Best,

    Emmanuel


    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("file.zip",
        "myFasta.fasta"), however read.FASTA does not support this.

        Thanks,
        RJ

                [[alternative HTML version deleted]]

        _______________________________________________
        R-sig-phylo mailing list - R-sig-phylo@r-project.org
        <mailto:R-sig-phylo@r-project.org>
        https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
        <https://stat.ethz.ch/mailman/listinfo/r-sig-phylo>
        Searchable archive at
        http://www.mail-archive.com/r-sig-phylo@r-project.org/
        <http://www.mail-archive.com/r-sig-phylo@r-project.org/>


        Pour nous remonter une erreur de filtrage, veuillez vous rendre
        ici : http://f.security-mail.net/302UOvTyxTd
        <http://f.security-mail.net/302UOvTyxTd>






_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

Reply via email to