utils::globalVariables(".")

## open a file connection, transparently handling compressed files (.gz/.bz2/.xz)
.seq_connection <- function(filename, mode="r") {
    if (grepl("\\.gz$", filename, ignore.case=TRUE)) {
        gzfile(filename, mode)
    } else if (grepl("\\.bz2$", filename, ignore.case=TRUE)) {
        bzfile(filename, mode)
    } else if (grepl("\\.xz$", filename, ignore.case=TRUE)) {
        xzfile(filename, mode)
    } else {
        file(filename, mode)
    }
}

## return a plain text file path, decompressing .bz2/.xz to a temp file
## (Biostrings only reads gzip-compressed files natively)
.plain_text_file <- function(filename) {
    ext <- tolower(sub(".*\\.", "", filename))
    if (!ext %in% c("bz2", "xz")) {
        return(filename)
    }
    con <- .seq_connection(filename, "r")
    on.exit(close(con))
    txt <- tempfile()
    out <- file(txt, "w")
    on.exit(close(out), add=TRUE)
    writeLines(readLines(con), out)
    return(txt)
}
