@@ -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
764782renameColname <- function (x , i , value ) {
765783 stopifnot(length(i ) == 1L , ! is.na(i ), ! missing(i ))
0 commit comments