Browse code

bugfix: exprs<- validates dim, dimnames

- https://support.bioconductor.org/p/86260/
- some unit tests also created invalid objects


git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/Biobase@120255 bc3139a8-67e5-0310-9ffc-ced21a209358

Martin Morgan authored on 18/08/2016 19:23:14
Showing 5 changed files

... ...
@@ -1,6 +1,6 @@
1 1
 Package: Biobase
2 2
 Title: Biobase: Base functions for Bioconductor
3
-Version: 2.33.0
3
+Version: 2.33.1
4 4
 Author: R. Gentleman, V. Carey, M. Morgan, S. Falcon
5 5
 Description: Functions that are needed by many other packages or which
6 6
         replace R functions.
... ...
@@ -1,3 +1,10 @@
1
+CHANGES IN VERSION 2.33
2
+-----------------------
3
+
4
+BUG FIXES
5
+
6
+    o exprs<- enforces value with correct dim, dimnames.
7
+
1 8
 CHANGES IN VERSION 2.31
2 9
 -----------------------
3 10
 
... ...
@@ -443,6 +443,26 @@ assayDataElementNames <- function(object) {
443 443
 assayDataElement <- function(object, elt) assayData(object)[[elt]]
444 444
 
445 445
 assayDataElementReplace <- function(obj, elt, value) {
446
+    if (!is.null(value) && !identical(unname(dim(obj)), unname(dim(value))))
447
+        stop("object and replacement value have different dimensions")
448
+    
449
+    if (!is.null(value)) {
450
+        if (!is.null(dimnames(value))) {
451
+            ## validate and harmonize dimnames
452
+            vd <- Map(function(od, vd) {
453
+                if (is.null(vd))
454
+                    ## update vd to contain indexes into matrix
455
+                    od <- seq_along(od)
456
+                else if (!setequal(od, vd))
457
+                    stop("object and replacement value dimnames differ")
458
+                od
459
+            }, dimnames(obj), dimnames(value))
460
+            ## re-arrange value to have dimnames in same order as obj
461
+            value <- do.call(`[`, c(list(value), vd, drop=FALSE))
462
+        }
463
+        dimnames(value) <- dimnames(obj)
464
+    }
465
+
446 466
     storage.mode <- storageMode(obj)
447 467
     switch(storageMode(obj),
448 468
            "lockedEnvironment" = {
... ...
@@ -209,10 +209,9 @@ testAssayDataReplacement <- function() {
209 209
 
210 210
 testAssayDataElement <- function() {
211 211
     checkObj <- function(obj) {
212
+        m <- new("matrix",0, nr=0, nc=0, dimnames=list(list(),list()))
212 213
         checkTrue(identical(assayDataElementNames(obj), "exprs"))
213
-        checkTrue(identical(assayDataElement(obj, "exprs"),
214
-                            new("matrix",0,dimnames=list(list(),list()))[FALSE,FALSE,drop=FALSE]))
215
-        m <- matrix(1:10, nrow=2)
214
+        checkTrue(identical(assayDataElement(obj, "exprs"), m))
216 215
         obj <- assayDataElementReplace(obj, "exprs", m)
217 216
         checkTrue(identical(assayDataElement(obj, "exprs"), m))
218 217
     }
... ...
@@ -295,7 +294,7 @@ testExprs <- function() {
295 294
     sNames <- sampleNames(obj)
296 295
     oldExprs <- exprs(obj)
297 296
     exprs(obj) <- newExprs
298
-    checkTrue( identical(exprs(obj), newExprs))
297
+    checkTrue( identical(unname(exprs(obj)), newExprs))
299 298
     if (storageMode(obj)!="environment")
300 299
       checkTrue(!identical(exprs(obj), oldExprs))
301 300
     sampleNames(assayData(obj)) <- sNames
... ...
@@ -173,3 +173,17 @@ testHarmonizeAssayDataDimnames <- function() {
173 173
     dimnames(se.exprs) <- list(letters[1:5], LETTERS[1:2])
174 174
     checkException(checkCreation(exprs, se.exprs), silent=TRUE)
175 175
 }
176
+
177
+testExprsReplacement <- function() {
178
+    exprs <- se.exprs <- matrix(1:50, 10, 5)
179
+    eset <- ExpressionSet(list2env(list(exprs=exprs, se.exprs=se.exprs)))
180
+    exprs(eset) <- exprs(eset)
181
+    checkTrue(validObject(eset))
182
+
183
+    ## shuffled names ok
184
+    exprs(eset) <- exprs(eset)[sample(rownames(eset)), sample(colnames(eset))]
185
+    checkTrue(validObject(eset))
186
+
187
+    checkException({ exprs(eset) <- exprs(eset)[, 1:3] }, silent=TRUE)
188
+    checkException({ exprs(eset) <- exprs(eset)[, c(1:4, 1)] }, silent=TRUE)
189
+}