Skip to content

Commit 01c0411

Browse files
committed
wip: longFormat generic and methods
1 parent 7d6b9c4 commit 01c0411

File tree

5 files changed

+76
-46
lines changed

5 files changed

+76
-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: 57 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)))
@@ -361,19 +372,32 @@ setMethod("mergeReplicates", "ANY",
361372
if (!is.character(res[["rowname"]]))
362373
res[["rowname"]] <- as.character(res[["rowname"]])
363374
res
364-
}
375+
})
365376

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

378402
.matchAddColData <- function(reshaped, colData, colDataCols) {
379403
extraColumns <- as.data.frame(colData[, colDataCols, drop = FALSE])
@@ -396,8 +420,6 @@ setMethod("mergeReplicates", "ANY",
396420

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

443-
longDataFrame <- do.call(function(...) rbind(..., make.row.names = FALSE),
444-
.longFormatElist(experiments(object), i = i))
458+
longDataFrame <- longFormat(experiments(object), i = i)
445459

446-
longDataFrame <- .mapOrderPrimary(longDataFrame, sampleMap(object))
460+
longDataFrame <- .mapOrderPrimary(longDataFrame, sampleMap(object))
447461

448-
if (!is.null(colDataCols))
449-
longDataFrame <-
450-
.matchAddColData(longDataFrame, colData(object), colDataCols)
462+
if (!is.null(colDataCols))
463+
longDataFrame <-
464+
.matchAddColData(longDataFrame, colData(object), colDataCols)
451465

452-
as(longDataFrame, "DataFrame")
453-
}
466+
as(longDataFrame, "DataFrame")
467+
}
468+
)
454469

455470
# wideformat function -----------------------------------------------------
456471

@@ -536,7 +551,7 @@ wideFormat <- function(object, colDataCols = NULL, check.names = TRUE,
536551
if (is.null(colDataCols)) colDataCols <- character(0L)
537552
nameFUN <- if (check.names) make.names else I
538553
cnames <- colnames(object)
539-
longList <- .longFormatElist(experiments(object), i = i)
554+
longList <- longFormat(experiments(object), i = i)
540555
longList <- lapply(longList, .mapOrderPrimary, sampleMap(object))
541556
colsofinterest <- c("assay", "rowname")
542557

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

632647
#' @rdname MultiAssayExperiment-helpers
633648
#'
649+
#' @param mode String indicating how `MultiAssayExperiment`
650+
#' column-level metadata should be added to the
651+
#' `SummarizedExperiment` `colData`.
652+
#'
634653
#' @param verbose `logical(1)` Whether to `suppressMessages` on subsetting
635654
#' operations in `getWithColData` (default FALSE)
636655
#'
@@ -759,7 +778,6 @@ renamePrimary <- function(x, value) {
759778
#' colnames(mae2)
760779
#' sampleMap(mae2)
761780
#'
762-
#'
763781
#' @export renameColname
764782
renameColname <- function(x, i, value) {
765783
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)