1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
# Tests for various data input methods
# library(scuttle); library(testthat); source("test-load-data.R")
a <- matrix(rpois(10000, lambda=1), ncol=50)
rownames(a) <- paste0("Gene", seq_len(nrow(a)))
colnames(a) <- paste0("Cell", seq_len(ncol(a)))
ofile <- tempfile()
write.table(a, file=ofile, sep="\t", quote=FALSE, col.names=NA)
ref <- as(a, "dgCMatrix")
library(Matrix)
test_that("readSparseCounts works as expected in basic cases", {
out <- readSparseCounts(ofile)
expect_identical(ref, out)
# Unaffected by chunk size.
out2 <- readSparseCounts(ofile, chunk=23L)
expect_identical(ref, out2)
out2 <- readSparseCounts(ofile, chunk=51L)
expect_identical(ref, out2)
})
test_that("readSparseCounts avoids row/column names if requested", {
# Avoids row names if requested.
out <- readSparseCounts(ofile, row.names=FALSE, ignore.col=1L)
ref2 <- ref
rownames(ref2) <- NULL
expect_identical(ref2, out)
expect_error(suppressWarnings(readSparseCounts(ofile, row.names=FALSE)), "expected")
# Avoids column names if requested.
out <- readSparseCounts(ofile, col.names=FALSE, ignore.row=1L)
ref2 <- ref
colnames(ref2) <- NULL
expect_identical(ref2, out)
expect_warning(out <- readSparseCounts(ofile, col.names=FALSE), "coercion")
expect_true(all(is.na(out[1,])))
})
test_that("Skipping works correctly", {
out <- readSparseCounts(ofile, skip.row=10L)
expect_identical(ref[11:nrow(ref),], out)
out <- readSparseCounts(ofile, skip.col=10L)
expect_identical(ref[,11:ncol(ref)], out)
})
test_that("Behaves correctly with wonky quotes and comments in the row/column names", {
a2 <- a
rownames(a2) <- paste0('"', rownames(a), "#")
colnames(a2) <- paste0('#"', colnames(a), "'")
ref2 <- as(a2, "dgCMatrix")
ofile2 <- tempfile()
write.table(a2, file=ofile2, sep="\t", quote=FALSE, col.names=NA)
out2 <- readSparseCounts(ofile2)
expect_identical(ref2, out2)
})
test_that("Behaves properly with file handle input", {
ofile3 <- tempfile(fileext=".gz")
XHANDLE <- gzfile(ofile3, open='wb')
write.table(a, file=XHANDLE, sep="\t", quote=FALSE, col.names=NA)
close(XHANDLE)
fhandle <- file(ofile3)
expect_error(readSparseCounts(fhandle), "read mode")
close(fhandle)
expect_error(readSparseCounts(DataFrame(ofile3)), "connection")
outX <- readSparseCounts(ofile3)
fhandle <- file(ofile3, open='rt')
outY <- readSparseCounts(fhandle)
close(fhandle)
expect_identical(outX, ref)
expect_identical(outY, ref)
})
|