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