Browse code

Tweak collapseBSseq()

Peter Hickey authored on 17/09/2018 11:07:48
Showing1 changed files

... ...
@@ -30,6 +30,7 @@
30 30
 # TODO: Make optional the collapsing of colData?
31 31
 # TODO: Document (and warn) that coef and se.coef aren't collapsed?
32 32
 collapseBSseq <- function(BSseq, group, BPPARAM = bpparam(),
33
+                          BACKEND = getRealizationBackend(),
33 34
                           dir = tempfile("BSseq"), replace = FALSE,
34 35
                           chunkdim = NULL, level = NULL,
35 36
                           type = c("double", "integer")) {
... ...
@@ -49,32 +50,50 @@ collapseBSseq <- function(BSseq, group, BPPARAM = bpparam(),
49 50
     if (anyNA(group)) {
50 51
         warning("missing values for 'group'")
51 52
     }
52
-    # Set appropriate BACKEND and check compatability with BPPARAM.
53
-    BACKEND <- .getBSseqBackends(BSseq)
53
+    # Register 'BACKEND' and return to current value on exit.
54
+    # TODO: Is this strictly necessary?
55
+    # current_BACKEND <- getRealizationBackend()
56
+    # on.exit(setRealizationBackend(current_BACKEND), add = TRUE)
57
+    # setRealizationBackend(BACKEND)
58
+    # Check compatability of 'BPPARAM' with 'BACKEND'.
54 59
     if (!.areBackendsInMemory(BACKEND)) {
55 60
         if (!.isSingleMachineBackend(BPPARAM)) {
56 61
             stop("The parallelisation strategy must use a single machine ",
57 62
                  "when using an on-disk realization backend.\n",
58
-                 "See help(\"BSmooth\") for details.",
63
+                 "See help(\"read.bismark\") for details.",
59 64
                  call. = FALSE)
60 65
         }
61 66
     } else {
62 67
         if (!is.null(BACKEND)) {
63 68
             # NOTE: Currently do not support any in-memory realization
64
-            #       backends. If 'BACKEND' is NULL then an ordinary matrix
65
-            #       is returned rather than a matrix-backed DelayedMatrix.
66
-            stop("The '", BACKEND, "' realization backend is not ",
67
-                 "supported.\n",
68
-                 "See help(\"BSmooth\") for details.",
69
+            #       backends. If the realization backend is NULL then an
70
+            #       ordinary matrix is returned rather than a matrix-backed
71
+            #       DelayedMatrix.
72
+            stop("The '", BACKEND, "' realization backend is not supported.",
73
+                 "\n  See help(\"read.bismark\") for details.",
69 74
                  call. = FALSE)
70 75
         }
71 76
     }
72
-    # TODO: Additional argument checks (e.g., `replace`,
73
-    #       HDF5Array:::.create_dir)
77
+    # If using HDF5Array as BACKEND, check remaining options are sensible.
78
+    if (identical(BACKEND, "HDF5Array")) {
79
+        # NOTE: Most of this copied from
80
+        #       HDF5Array::saveHDF5SummarizedExperiment().
81
+        if (!isSingleString(dir)) {
82
+            stop(wmsg("'dir' must be a single string specifying the path to ",
83
+                      "the directory where to save the BSseq object (the ",
84
+                      "directory will be created)."))
85
+        }
86
+        if (!isTRUEorFALSE(replace)) {
87
+            stop("'replace' must be TRUE or FALSE")
88
+        }
89
+        HDF5Array:::.create_dir(dir = dir, replace = replace)
90
+        h5_path <- file.path(dir, "assays.h5")
91
+    } else if (identical(BACKEND, NULL)) {
92
+        h5_path <- NULL
93
+    }
74 94
 
75 95
     # Collapse 'M' and 'Cov' matrices ------------------------------------------
76 96
 
77
-    h5_path <- file.path(dir, "assays.h5")
78 97
     M <- colsum(
79 98
         x = getCoverage(BSseq, type = "M", withDimnames = FALSE),
80 99
         group = group,
... ...
@@ -109,6 +128,7 @@ collapseBSseq <- function(BSseq, group, BPPARAM = bpparam(),
109 128
         assays = SimpleList(M = unname(M), Cov = unname(Cov)),
110 129
         rowRanges = rowRanges(BSseq),
111 130
         colData = colData)
131
+
112 132
     # TODO: Is there a way to use the internal constructor with `check = FALSE`?
113 133
     #       Don't need to check M and Cov because this has already happened
114 134
     #       when files were parsed.