fas <- system.file("extdata/HA.fas", package="seqmagick")
phy <- system.file("extdata/HA.phy", package="seqmagick")

test_that("fa_read returns expected XStringSet", {
    x <- fa_read(fas)
    expect_s4_class(x, "XStringSet")
    expect_equal(length(x), 100)
})

test_that("fa_read type='auto' vs explicit type agree on DNA file", {
    a <- fa_read(fas)
    b <- fa_read(fas, type="DNA")
    expect_identical(as.character(a), as.character(b))
})

## compression formats x writing styles
test_that("fa_write/fa_read round-trip across compressed formats", {
    x <- fa_read(fas)[1:10]
    for (type in c("interleaved", "sequential")) {
        for (ext in c(".fas", ".fas.gz", ".fasta.bz2", ".fasta.xz")) {
            f <- tempfile(fileext=ext)
            fa_write(x, f, type=type)
            y <- fa_read(f)
            info <- paste(type, ext)
            expect_identical(names(y), names(x), info=info)
            expect_identical(as.character(y), as.character(x), info=info)
            unlink(f)
        }
    }
})

test_that("phylip round-trip (sequential)", {
    p <- phy_read(phy)
    f <- tempfile(fileext=".phy")
    phy_write(p, f, type="sequential")
    q <- phy_read(f)
    expect_identical(names(q), names(p))
    expect_identical(unname(as.character(q)), unname(as.character(p)))
    unlink(f)
})

test_that("phylip round-trip (interleaved)", {
    p <- phy_read(phy)
    f <- tempfile(fileext=".phy")
    phy_write(p, f, type="interleaved")
    q <- phy_read(f)
    expect_identical(names(q), names(p))
    expect_identical(gsub("-", "", unname(as.character(q))),
                     gsub("-", "", unname(as.character(p))))
    unlink(f)
})

test_that("phylip -> fasta conversion keeps sequences", {
    p <- phy_read(phy)[1:5]
    f <- tempfile(fileext=".fas")
    fa_write(p, f)
    x <- fa_read(f)
    expect_identical(names(x), names(p))
    expect_identical(unname(as.character(x)), unname(as.character(p)))
    unlink(f)
})
