Skip to content

Commit

Permalink
document drops generic, method, and replacement
Browse files Browse the repository at this point in the history
- remove from metadata
- access with drops function
- update unit tests
  • Loading branch information
LiNk-NY committed Aug 18, 2023
1 parent 7291e52 commit 4ca8ca3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -48,6 +48,7 @@ exportMethods("[[")
exportMethods("[[<-")
exportMethods("colData<-")
exportMethods("colnames<-")
exportMethods("drops<-")
exportMethods("experiments<-")
exportMethods("names<-")
exportMethods("sampleMap<-")
Expand Down
29 changes: 17 additions & 12 deletions R/MultiAssayExperiment-class.R
Expand Up @@ -512,6 +512,7 @@ setMethod("show", "MultiAssayExperiment", function(object) {
#' \item experiments: Access the \link{ExperimentList} slot
#' \item `[[`: Access the \link{ExperimentList} slot
#' \item `$`: Access a column in \code{colData}
#' \item `drops`: Get a vector of dropped \link{ExperimentList} names
#' }
#'
#' @section Setters:
Expand All @@ -527,6 +528,8 @@ setMethod("show", "MultiAssayExperiment", function(object) {
#' \item `[[<-`: Equivalent to the \code{experiments<-} setter method for
#' convenience
#' \item `$<-`: A vector to replace the indicated column in \code{colData}
#' \item `drops<-`: Trace \link{ExperimentList} names that have been
#' removed
#' }
#'
#' @param object,x A \code{MultiAssayExperiment} object
Expand All @@ -543,7 +546,7 @@ setMethod("show", "MultiAssayExperiment", function(object) {
#'
#' @example inst/scripts/MultiAssayExperiment-methods-Ex.R
#'
#' @aliases experiments sampleMap experiments<- sampleMap<-
#' @aliases experiments sampleMap experiments<- sampleMap<- drops drops<-
NULL

### - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -573,6 +576,15 @@ setMethod("colData", "MultiAssayExperiment", function(x, ...) {
getElement(x, "colData")
})

#' @export
setGeneric("drops", function(x) standardGeneric("drops"))

#' @exportMethod drops
#' @rdname MultiAssayExperiment-methods
setMethod("drops", "MultiAssayExperiment", function(x) {
getElement(x, "drops")
})

### - - - - - - - - - - - - - - - - - - - - - - - -
### Getters
###
Expand All @@ -589,15 +601,6 @@ setMethod("names", "MultiAssayExperiment", function(x)
names(experiments(x))
)

#' @export
setGeneric("drops", function(x) standardGeneric("drops"))

#' @exportMethod drops
#' @describeIn MultiAssayExperiment Get information about dropped experiments
setMethod("drops", "MultiAssayExperiment", function(x) {
getElement(x, "drops")
})

### - - - - - - - - - - - - - - - - - - - - - - - -
### Replacers
###
Expand Down Expand Up @@ -640,14 +643,15 @@ setGeneric("experiments<-", function(object, value)
standardGeneric("experiments<-"))

#' @export
#' @rdname MultiAssayExperiment-methods
setGeneric("drops<-", function(x, ..., value) standardGeneric("drops<-"))

#' @exportMethod experiments<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("experiments", c("MultiAssayExperiment", "ExperimentList"),
function(object, value) {
if (!any(names(object) %in% names(value)) && !isEmpty(object)) {
if (isEmpty(drops(x)))
if (isEmpty(drops(object)))
warning("'experiments' dropped; see 'drops()'", call. = FALSE)
drops(object) <-
list(experiments = setdiff(names(object), names(value)))
Expand Down Expand Up @@ -737,7 +741,8 @@ setReplaceMethod("colData", c("MultiAssayExperiment", "ANY"),
}
)


#' @exportMethod drops<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("drops", c("MultiAssayExperiment", "ANY"),
function(x, ..., value) {
anydrops <- getElement(x, "drops")[["experiments"]]
Expand Down
6 changes: 4 additions & 2 deletions R/MultiAssayExperiment-subset.R
Expand Up @@ -17,13 +17,15 @@ NULL
isEmptyAssay <- .emptyAssays(experiments(object))
if (all(isEmptyAssay)) {
drops(object) <- list(experiments = names(object))
if (warn)
warning("'experiments' dropped; see 'drops()'", call. = FALSE)
experiments(object) <- ExperimentList()
} else if (any(isEmptyAssay)) {
empties <- vapply(isEmptyAssay, isTRUE, logical(1L))
keeps <- names(isEmptyAssay)[!empties]
drops(object) <- list(experiments = names(isEmptyAssay)[empties])
if (warn)
warning("'experiments' dropped; see 'metadata'", call. = FALSE)
warning("'experiments' dropped; see 'drops()'", call. = FALSE)
FUN <- if (warn) force else suppressWarnings
object <- FUN(subsetByAssay(object, keeps))
}
Expand All @@ -47,7 +49,7 @@ NULL
x <- subsetByRow(x, i, ...)
}
if (drop) {
x <- .dropEmpty(x)
x <- .dropEmpty(x, warn = TRUE)
}
return(x)
}
Expand Down
5 changes: 0 additions & 5 deletions man/MultiAssayExperiment-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions man/MultiAssayExperiment-methods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/test-MultiAssayExperiment.R
Expand Up @@ -124,23 +124,23 @@ test_that("dropping experiments is noisy and traced", {
mae <- MultiAssayExperiment(list(m=m, m2=m2, m3=m3))
## check warning about experiments
expect_warning( dp <- mae[, list(m = 1:3, m2 = 0, m3 = 1), , drop = TRUE] )
## check drops in metadata
expect_identical(metadata(dp)[["drops.experiments"]], "m2")
## check drops in mae
expect_identical(drops(dp)[["experiments"]], "m2")

## check warning about experiments
expect_warning( dp <- mae[character(0L), , drop = TRUE] )
## check drops in metadata
expect_identical(metadata(dp)[["drops.experiments"]], names(mae))
expect_identical(drops(dp)[["experiments"]], names(mae))

## check warning about experiments
expect_warning( dp <- mae[, , 0, drop = TRUE] )
## check drops in metadata
expect_identical(metadata(dp)[["drops.experiments"]], names(mae))
expect_identical(drops(dp)[["experiments"]], names(mae))

## check warning about experiments
expect_warning( dp <- mae[, , c(TRUE, FALSE), drop = TRUE] )
## check drops in metadata
expect_identical(metadata(dp)[["drops.experiments"]], "m2")
expect_identical(drops(dp)[["experiments"]], "m2")
})

test_that("MultiAssayExperiment replacements work", {
Expand Down

0 comments on commit 4ca8ca3

Please sign in to comment.