Browse code

Improved the error message for checkBatchConsistency. Closes #22.

LTLA authored on 26/11/2020 05:02:40
Showing 3 changed files

... ...
@@ -1,6 +1,6 @@
1 1
 Package: batchelor
2
-Version: 1.6.1
3
-Date: 2020-11-16
2
+Version: 1.6.2
3
+Date: 2020-11-25
4 4
 Title: Single-Cell Batch Correction Methods
5 5
 Authors@R: c(person("Aaron", "Lun", role = c("aut", "cre"), email = "infinite.monkeys.with.keyboards@gmail.com"),
6 6
     person("Laleh", "Haghverdi", role="ctb"))
... ...
@@ -39,11 +39,7 @@
39 39
 #' @export
40 40
 #' @importMethodsFrom BiocGenerics nrow ncol
41 41
 #' @importFrom BiocGenerics colnames rownames
42
-checkBatchConsistency <- function(batches, cells.in.columns=TRUE)
43
-# Checking for identical number of rows (and rownames).
44
-# It can also do the same for columns, if we're dealing with PC results.
45
-# It then returns a list of dimnames for renaming the output.
46
-{
42
+checkBatchConsistency <- function(batches, cells.in.columns=TRUE) {
47 43
     if (length(batches)==0L) {
48 44
         return(invisible(NULL))
49 45
     }
... ...
@@ -65,18 +61,28 @@ checkBatchConsistency <- function(batches, cells.in.columns=TRUE)
65 61
     for (b in seq_along(batches)[-1]) { 
66 62
         current <- batches[[b]]
67 63
         if (!identical(DIMFUN(current), ref.n)) {
68
-            stop(sprintf("number of %ss is not the same across batches", DIM))
64
+            stop(sprintf("number of %ss is not the same across batches (see batch %s)", 
65
+                DIM, .identify_failed_batch(b, names(batches))))
69 66
         }
70 67
 
71 68
         cur.names <- DIMNAMEFUN(current)
72 69
         if (!identical(cur.names, ref.names)) {
73
-            stop(sprintf("%s names are not the same across batches", DIM))
70
+            stop(sprintf("%s names are not the same across batches (see batch %s)",
71
+                DIM, .identify_failed_batch(b, names(batches))))
74 72
         }
75 73
     }
76 74
 
77 75
     invisible(NULL)
78 76
 }
79 77
 
78
+.identify_failed_batch <- function(bx, names) {
79
+    if (is.null(names) || names[bx]=="") {
80
+        bx
81
+    } else {
82
+        deparse(names[bx])
83
+    }
84
+}
85
+
80 86
 #' @rdname checkInputs
81 87
 #' @export
82 88
 #' @importFrom methods is
... ...
@@ -50,6 +50,17 @@ test_that("checkBatchConsistency handles corner cases", {
50 50
     expect_error(checkBatchConsistency(list(B[,0], B[,0]), cells.in.columns=FALSE), NA)
51 51
 })
52 52
 
53
+set.seed(10000012)
54
+test_that("checkBatchConsistency reports names correctly", {
55
+    expect_error(checkBatchConsistency(list(cbind(1), cbind(1:2))), "batch 2")
56
+    expect_error(checkBatchConsistency(list(cbind(1), X=cbind(1:2))), "batch \"X\"")
57
+    expect_error(checkBatchConsistency(list(X=cbind(1), cbind(1:2))), "batch 2")
58
+
59
+    expect_error(checkBatchConsistency(list(rbind(A=1), rbind(B=2))), "batch 2")
60
+    expect_error(checkBatchConsistency(list(rbind(A=1), X=rbind(B=2))), "batch \"X\"")
61
+    expect_error(checkBatchConsistency(list(X=rbind(A=1), rbind(B=2))), "batch 2")
62
+})
63
+
53 64
 set.seed(1000003)
54 65
 test_that("checkIfSCE works correctly", {
55 66
     sce1 <- SingleCellExperiment(list(logcounts=matrix(runif(5000), nrow=10)))