Skip to content

Commit 1af77ea

Browse files
committed
add longFormat generic and methods
- re-split longFormat output in wideFormat
1 parent 9cfd443 commit 1af77ea

File tree

5 files changed

+77
-46
lines changed

5 files changed

+77
-46
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export(intersectColumns)
2020
export(intersectRows)
2121
export(listToMap)
2222
export(loadHDF5MultiAssayExperiment)
23-
export(longFormat)
2423
export(makeHitList)
2524
export(mapToList)
2625
export(mergeReplicates)
@@ -67,6 +66,7 @@ exportMethods(exportClass)
6766
exportMethods(hasRowRanges)
6867
exportMethods(isEmpty)
6968
exportMethods(length)
69+
exportMethods(longFormat)
7070
exportMethods(mergeReplicates)
7171
exportMethods(metadata)
7272
exportMethods(names)

R/MultiAssayExperiment-helpers.R

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,20 @@ setMethod("mergeReplicates", "ANY",
340340
return(object)
341341
})
342342

343-
# longFormat function -----------------------------------------------------
344343

345-
.longFormatANY <- function(object, i) {
344+
# longFormat generic and methods ------------------------------------------
345+
346+
#' @rdname MultiAssayExperiment-helpers
347+
#' @aliases longFormat
348+
setGeneric(
349+
"longFormat",
350+
function(object, colDataCols = NULL, i = 1L, ...)
351+
standardGeneric("longFormat")
352+
)
353+
354+
#' @rdname MultiAssayExperiment-helpers
355+
#' @exportMethod longFormat
356+
setMethod("longFormat", "ANY", function(object, colDataCols, i = 1L, ...) {
346357
rowNAMES <- rownames(object)
347358
if (is.null(rowNAMES))
348359
rowNames <- as.character(seq_len(nrow(object)))
@@ -360,19 +371,32 @@ setMethod("mergeReplicates", "ANY",
360371
if (!is.character(res[["rowname"]]))
361372
res[["rowname"]] <- as.character(res[["rowname"]])
362373
res
363-
}
374+
})
364375

365-
.longFormatElist <- function(object, i) {
366-
if (!is(object, "ExperimentList"))
367-
stop("<internal> Not an 'ExperimentList' input")
368-
samelength <- identical(length(object), length(i))
369-
if (!samelength && identical(length(i), 1L))
370-
i <- rep(i, length(object))
371-
mapply(function(obj, obname, idx) {
372-
data.frame(assay = obname, .longFormatANY(obj, i = idx),
373-
stringsAsFactors = FALSE)
374-
}, obj = object, obname = names(object), idx = i, SIMPLIFY = FALSE)
375-
}
376+
#' @rdname MultiAssayExperiment-helpers
377+
#' @exportMethod longFormat
378+
setMethod(
379+
"longFormat", "ExperimentList",
380+
function(object, colDataCols, i = 1L, ...) {
381+
samelength <- identical(length(object), length(i))
382+
if (!samelength && identical(length(i), 1L))
383+
i <- rep(i, length(object))
384+
res <- mapply(
385+
function(obj, obname, idx) {
386+
data.frame(
387+
assay = obname,
388+
longFormat(obj, i = idx),
389+
stringsAsFactors = FALSE
390+
)
391+
}, obj = object, obname = names(object), idx = i, SIMPLIFY = FALSE
392+
)
393+
394+
do.call(
395+
function(...) rbind(..., make.row.names = FALSE),
396+
res
397+
)
398+
}
399+
)
376400

377401
.matchAddColData <- function(reshaped, colData, colDataCols) {
378402
extraColumns <- as.data.frame(colData[, colDataCols, drop = FALSE])
@@ -395,8 +419,6 @@ setMethod("mergeReplicates", "ANY",
395419

396420
#' @rdname MultiAssayExperiment-helpers
397421
#'
398-
#' @aliases longFormat
399-
#'
400422
#' @details The `longFormat` "ANY" class method, works with classes such as
401423
#' [`ExpressionSet`][Biobase::ExpressionSet] and
402424
#' [`SummarizedExperiment`][SummarizedExperiment::SummarizedExperiment-class] as
@@ -425,31 +447,24 @@ setMethod("mergeReplicates", "ANY",
425447
#' renameColname: Either a `numeric` or `character` index
426448
#' indicating the assay whose colnames are to be renamed
427449
#'
428-
#' @param mode String indicating how `MultiAssayExperiment`
429-
#' column-level metadata should be added to the
430-
#' `SummarizedExperiment` `colData`.
431-
#'
432-
#' @export longFormat
433-
longFormat <- function(object, colDataCols = NULL, i = 1L) {
434-
if (is(object, "ExperimentList"))
435-
return(do.call(rbind, .longFormatElist(object, i = i)))
436-
else if (!is(object, "MultiAssayExperiment"))
437-
return(.longFormatANY(object, i = i))
438-
439-
if (any(.emptyAssays(experiments(object))))
440-
object <- .dropEmpty(object, warn = FALSE)
450+
#' @exportMethod longFormat
451+
setMethod(
452+
"longFormat", "MultiAssayExperiment",
453+
function(object, colDataCols = NULL, i = 1L, ...) {
454+
if (any(.emptyAssays(experiments(object))))
455+
object <- .dropEmpty(object, warn = FALSE)
441456

442-
longDataFrame <- do.call(function(...) rbind(..., make.row.names = FALSE),
443-
.longFormatElist(experiments(object), i = i))
457+
longDataFrame <- longFormat(experiments(object), i = i)
444458

445-
longDataFrame <- .mapOrderPrimary(longDataFrame, sampleMap(object))
459+
longDataFrame <- .mapOrderPrimary(longDataFrame, sampleMap(object))
446460

447-
if (!is.null(colDataCols))
448-
longDataFrame <-
449-
.matchAddColData(longDataFrame, colData(object), colDataCols)
461+
if (!is.null(colDataCols))
462+
longDataFrame <-
463+
.matchAddColData(longDataFrame, colData(object), colDataCols)
450464

451-
as(longDataFrame, "DataFrame")
452-
}
465+
as(longDataFrame, "DataFrame")
466+
}
467+
)
453468

454469
# wideformat function -----------------------------------------------------
455470

@@ -535,7 +550,8 @@ wideFormat <- function(object, colDataCols = NULL, check.names = TRUE,
535550
if (is.null(colDataCols)) colDataCols <- character(0L)
536551
nameFUN <- if (check.names) make.names else I
537552
cnames <- colnames(object)
538-
longList <- .longFormatElist(experiments(object), i = i)
553+
longDataFrame <- longFormat(experiments(object), i = i)
554+
longList <- split(longDataFrame, longDataFrame[["assay"]])
539555
longList <- lapply(longList, .mapOrderPrimary, sampleMap(object))
540556
colsofinterest <- c("assay", "rowname")
541557

@@ -630,6 +646,10 @@ setMethod("hasRowRanges", "ExperimentList", function(x) {
630646

631647
#' @rdname MultiAssayExperiment-helpers
632648
#'
649+
#' @param mode String indicating how `MultiAssayExperiment`
650+
#' column-level metadata should be added to the
651+
#' `SummarizedExperiment` `colData`.
652+
#'
633653
#' @param verbose `logical(1)` Whether to `suppressMessages` on subsetting
634654
#' operations in `getWithColData` (default FALSE)
635655
#'
@@ -758,7 +778,6 @@ renamePrimary <- function(x, value) {
758778
#' colnames(mae2)
759779
#' sampleMap(mae2)
760780
#'
761-
#'
762781
#' @export renameColname
763782
renameColname <- function(x, i, value) {
764783
stopifnot(length(i) == 1L, !is.na(i), !missing(i))

man/MultiAssayExperiment-helpers.Rd

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-MultiAssayExperiment-helpers.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ test_that("renaming helpers work", {
240240
})
241241

242242

243-
test_that(".longFormatANY works", {
243+
test_that("longFormat,ANY-method works", {
244244
denv <- new.env(parent = emptyenv())
245245
data("miniACC", package="MultiAssayExperiment", envir = denv)
246246
miniACC <- denv[["miniACC"]]

tests/testthat/test-saveHDF5MultiAssayExperiment.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ test_that("saveHDF5MultiAssayExperiment is working", {
2929
})
3030

3131
test_that("prefix argument works as intended", {
32+
env <- new.env(parent = emptyenv())
33+
data("miniACC", envir = env)
34+
miniACC <- env[["miniACC"]]
35+
3236
testDir <- file.path(tempdir(), "test_mae")
3337
saveHDF5MultiAssayExperiment(
3438
miniACC, dir = testDir, prefix = "", replace = TRUE
@@ -85,7 +89,7 @@ test_that("loadHDF5MultiAssayExperiment is working", {
8589

8690
testDir <- file.path(tempdir(), "test_mae")
8791
on.exit(unlink(testDir, recursive = TRUE))
88-
92+
8993
saveHDF5MultiAssayExperiment(
9094
miniACC, prefix = "", dir = testDir, replace = TRUE
9195
)
@@ -105,7 +109,7 @@ test_that("loadHDF5MultiAssayExperiment prefix input is consistent", {
105109

106110
testDir <- file.path(tempdir(), "test_mae")
107111
on.exit(unlink(testDir, recursive = TRUE))
108-
112+
109113
saveHDF5MultiAssayExperiment(
110114
miniACC, prefix = "test", dir = testDir, replace = TRUE
111115
)
@@ -125,4 +129,4 @@ test_that("loadHDF5MultiAssayExperiment prefix input is consistent", {
125129
loadHDF5MultiAssayExperiment(dir = testDir, prefix = "error")
126130
)
127131
)
128-
})
132+
})

0 commit comments

Comments
 (0)