Browse code

Tweak and update unit tests

Peter Hickey authored on 17/09/2018 11:08:34
Showing4 changed files

... ...
@@ -1,4 +1,3 @@
1 1
 library(testthat)
2 2
 library(bsseq)
3
-
4 3
 test_check("bsseq")
... ...
@@ -14,7 +14,7 @@ expect_equivalent_SE <- function(SE1, SE2) {
14 14
     assays(SE1) <- endoapply(assays(SE1), as.matrix)
15 15
     assays(SE2) <- endoapply(assays(SE2), as.matrix)
16 16
     if (isTRUE(all.equal(SE1, SE2))) {
17
-        return(all.equal(assays(SE1), assays(SE2)))
17
+        return(invisible(all.equal(assays(SE1), assays(SE2))))
18 18
     }
19
-    FALSE
19
+    invisible(FALSE)
20 20
 }
... ...
@@ -2,19 +2,27 @@ context("BSmooth")
2 2
 
3 3
 test_that("Errors on bad input", {
4 4
     expect_error(
5
-        BSmooth(as(bsseq_test, "SummarizedExperiment")),
6
-        "'BSseq' must be a BSseq object")
7
-    expect_error(BSmooth(rev(bsseq_test)), "'BSseq' must be sorted")
5
+        object = BSmooth(
6
+            BSseq = as(bsseq_test, "SummarizedExperiment"),
7
+            BPPARAM = SerialParam()),
8
+        regexp = "'BSseq' must be a BSseq object")
8 9
     expect_error(
9
-        BSmooth(resize(bsseq_test, 2)),
10
-        "All loci in 'BSseq' must have width == 1")
10
+        object =  BSmooth(rev(bsseq_test), BPPARAM = SerialParam()),
11
+        regexp = "'BSseq' must be sorted")
12
+    expect_error(
13
+        object = BSmooth(resize(bsseq_test, 2), BPPARAM = SerialParam()),
14
+        regexp = "All loci in 'BSseq' must have width == 1")
11 15
 })
12 16
 
13 17
 test_that("BSmooth properly inherits 'dir'", {
14
-    infile <- system.file("extdata", "test_data.fastq_bismark.bismark.cov.gz",
15
-                          package = "bsseq")
16
-    bsseq <- read.bismark(files = infile, BACKEND = "HDF5Array")
17
-    bsseq <- BSmooth(bsseq)
18
+    infile <- system.file(
19
+        "extdata", "test_data.fastq_bismark.bismark.cov.gz",
20
+        package = "bsseq")
21
+    bsseq <- read.bismark(
22
+        files = infile,
23
+        BACKEND = "HDF5Array",
24
+        BPPARAM = SerialParam())
25
+    bsseq <- BSmooth(bsseq, BPPARAM = SerialParam())
18 26
     expect_is(bsseq, "BSseq")
19 27
     expect_true(hasBeenSmoothed(bsseq))
20 28
     expect_error(
21 29
new file mode 100644
... ...
@@ -0,0 +1,104 @@
1
+context("BSseq-class")
2
+
3
+test_that("BSseq() allows loci of width != 1", {
4
+    bsseq <- BSseq(
5
+        M = matrix(1:10),
6
+        Cov = matrix(1:10),
7
+        gr = GRanges("chr1", IRanges(1:10, width = 10), strand = "*"))
8
+    expect_true(validObject(bsseq))
9
+})
10
+
11
+test_that("strandCollapse() does nothing for unstranded data", {
12
+    bsseq <- BSseq(
13
+        M = matrix(1:10),
14
+        Cov = matrix(1:10),
15
+        gr = GRanges("chr1", IRanges(1:10, width = 1), strand = "*"))
16
+    expect_warning(strandCollapse(bsseq))
17
+})
18
+
19
+test_that("strandCollapse() works on good input", {
20
+    nrow <- 20
21
+    ncol <- 5
22
+    M <- matrix(sample(0:10, size = nrow * ncol, replace = TRUE), ncol = ncol)
23
+    Cov <- M + sample(0:10, size = nrow * ncol, replace = TRUE)
24
+    gr_pos <- GRanges(
25
+        seqnames = c(rep(1, nrow / 2), rep(2, nrow / 2)),
26
+        ranges = seq(1, 2 * nrow, by = 2),
27
+        strand = "+")
28
+    gr_neg <- invertStrand(shift(gr_pos, 1L))
29
+    bsseq_pos <- BSseq(M = M, Cov = Cov, gr = gr_pos)
30
+    bsseq_neg <- BSseq(M = M, Cov = Cov, gr = gr_neg)
31
+    bsseq <- rbind(bsseq_pos, bsseq_neg)
32
+
33
+    expect_equivalent_SE(
34
+        SE1 = strandCollapse(bsseq_pos, type = "integer"),
35
+        SE2 = unstrand(bsseq_pos))
36
+    expect_equivalent_SE(
37
+        SE1 = strandCollapse(bsseq_neg, shift = FALSE, type = "integer"),
38
+        SE2 = unstrand(bsseq_neg))
39
+    expect_equivalent_SE(
40
+        SE1 = strandCollapse(bsseq_neg, type = "integer"),
41
+        SE2 = unstrand(shift(bsseq_neg, -1L)))
42
+    bsseq_strand_collapsed <- strandCollapse(bsseq, type = "integer")
43
+    expect_identical(
44
+        object = rowRanges(bsseq_strand_collapsed),
45
+        expected = unstrand(rowRanges(bsseq_pos)))
46
+    expect_identical(
47
+        object = assay(bsseq_strand_collapsed, "M"),
48
+        expected = assay(bsseq_pos, "M") + assay(bsseq_neg, "M"))
49
+    expect_identical(
50
+        object = assay(bsseq_strand_collapsed, "Cov"),
51
+        expected = assay(bsseq_pos, "Cov") + assay(bsseq_neg, "Cov"))
52
+
53
+    bsseq_strand_collapsed2 <- strandCollapse(bsseq[sample(nrow(bsseq))])
54
+    expect_equivalent_SE(bsseq_strand_collapsed, bsseq_strand_collapsed2)
55
+
56
+    bsseq_pos <- realize(bsseq_pos, "HDF5Array")
57
+    bsseq_neg <- realize(bsseq_neg, "HDF5Array")
58
+    bsseq <- realize(bsseq, "HDF5Array")
59
+    expect_equivalent_SE(
60
+        SE1 = strandCollapse(bsseq_pos, type = "integer"),
61
+        SE2 = unstrand(bsseq_pos))
62
+    expect_equivalent_SE(
63
+        SE1 = strandCollapse(bsseq_neg, shift = FALSE),
64
+        SE2 = unstrand(bsseq_neg))
65
+    expect_equivalent_SE(
66
+        SE1 = strandCollapse(bsseq_neg),
67
+        SE2 = unstrand(shift(bsseq_neg, -1L)))
68
+    bsseq_strand_collapsed <- strandCollapse(bsseq, type = "integer")
69
+    expect_identical(
70
+        object = rowRanges(bsseq_strand_collapsed),
71
+        expected = unstrand(rowRanges(bsseq_pos)))
72
+    expect_identical(
73
+        object = as.matrix(assay(bsseq_strand_collapsed, "M")),
74
+        expected = as.matrix(assay(bsseq_pos, "M") + assay(bsseq_neg, "M")))
75
+    expect_identical(
76
+        object = as.matrix(assay(bsseq_strand_collapsed, "Cov")),
77
+        expected = as.matrix(assay(bsseq_pos, "Cov") + assay(bsseq_neg, "Cov")))
78
+
79
+    bsseq_strand_collapsed2 <- strandCollapse(
80
+        BSseq = bsseq[sample(nrow(bsseq))],
81
+        type = "integer")
82
+    expect_equivalent_SE(bsseq_strand_collapsed, bsseq_strand_collapsed2)
83
+})
84
+
85
+test_that("strandCollapse() errors on bad input", {
86
+    bsseq <- BSseq(
87
+        M = matrix(1:3),
88
+        Cov = matrix(1:3),
89
+        gr = GRanges(
90
+            seqnames = "chr1",
91
+            ranges = IRanges(1:3, width = 1),
92
+            strand = c("+", "-", "*")))
93
+    expect_error(strandCollapse(bsseq))
94
+})
95
+
96
+test_that("strandCollapse() will unstrand loci and may re-order them", {
97
+    bsseq <- BSseq(
98
+        M = matrix(1:10),
99
+        Cov = matrix(1:10),
100
+        gr = GRanges("chr1", IRanges(10:1, width = 1), strand = "+"))
101
+    expect_true(all(strand(strandCollapse(bsseq)) == "*"))
102
+    expect_false(expect_equivalent_SE(strandCollapse(bsseq), bsseq))
103
+    expect_equivalent_SE(strandCollapse(bsseq), bsseq[10:1])
104
+})