Skip to content

Commit

Permalink
deprecate longFormat and use longForm from BiocGenerics
Browse files Browse the repository at this point in the history
  • Loading branch information
LiNk-NY committed Jan 22, 2025
1 parent 19c1d27 commit a42ee78
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 105 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Depends:
Imports:
Biobase,
BiocBaseUtils,
BiocGenerics,
BiocGenerics (>= 0.53.4),
DelayedArray,
GenomicRanges,
IRanges,
Expand Down Expand Up @@ -70,6 +70,7 @@ Collate:
'data.R'
'hasAssay.R'
'listToMap.R'
'longFormat-deprecated.R'
'mapToList.R'
'prepMultiAssay.R'
'reexports.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ exportMethods(exportClass)
exportMethods(hasRowRanges)
exportMethods(isEmpty)
exportMethods(length)
exportMethods(longForm)
exportMethods(longFormat)
exportMethods(mergeReplicates)
exportMethods(metadata)
Expand All @@ -85,7 +86,9 @@ import(IRanges)
import(S4Vectors)
import(SummarizedExperiment)
import(methods)
importFrom(BiocBaseUtils,lifeCycle)
importFrom(BiocGenerics,colnames)
importFrom(BiocGenerics,longForm)
importFrom(BiocGenerics,rownames)
importFrom(S4Vectors,"metadata<-")
importFrom(S4Vectors,metadata)
Expand Down
132 changes: 61 additions & 71 deletions R/MultiAssayExperiment-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ NULL
#' * mergeReplicates: A function that combines replicated / repeated
#' measurements across all experiments and is guided by the replicated
#' return value
#' * longFormat: A `MultiAssayExperiment` method that
#' returns a small and skinny [`DataFrame`][S4Vectors::DataFrame-class]. The
#' `colDataCols` arguments allows the user to append `colData` columns to
#' the data.
#' * longForm: A `MultiAssayExperiment` method that returns a small and
#' skinny [`DataFrame`][S4Vectors::DataFrame-class]. The `colDataCols`
#' arguments allows the user to append `colData` columns to the data.
#' * wideFormat: A function to reshape the data in a
#' `MultiAssayExperiment` to a "wide" format
#' [`DataFrame`][S4Vectors::DataFrame-class]. Each row in the `DataFrame`
Expand Down Expand Up @@ -340,65 +339,6 @@ setMethod("mergeReplicates", "ANY",
return(object)
})


# longFormat generic and methods ------------------------------------------

#' @rdname MultiAssayExperiment-helpers
#' @aliases longFormat
#' @export
setGeneric(
"longFormat",
function(object, colDataCols = NULL, i = 1L, ...)
standardGeneric("longFormat")
)

#' @rdname MultiAssayExperiment-helpers
#' @exportMethod longFormat
setMethod("longFormat", "ANY", function(object, colDataCols, i = 1L, ...) {
rowNAMES <- rownames(object)
if (is.null(rowNAMES))
rowNames <- as.character(seq_len(nrow(object)))

if (is(object, "ExpressionSet"))
object <- Biobase::exprs(object)
if (is(object, "SummarizedExperiment") || is(object, "RaggedExperiment"))
object <- assay(object, i = i)

BiocBaseUtils::checkInstalled("reshape2")

res <- reshape2::melt(
object, varnames = c("rowname", "colname"), value.name = "value"
)
if (!is.character(res[["rowname"]]))
res[["rowname"]] <- as.character(res[["rowname"]])
res
})

#' @rdname MultiAssayExperiment-helpers
#' @exportMethod longFormat
setMethod(
"longFormat", "ExperimentList",
function(object, colDataCols, i = 1L, ...) {
samelength <- identical(length(object), length(i))
if (!samelength && identical(length(i), 1L))
i <- rep(i, length(object))
res <- mapply(
function(obj, obname, idx) {
data.frame(
assay = obname,
longFormat(obj, i = idx),
stringsAsFactors = FALSE
)
}, obj = object, obname = names(object), idx = i, SIMPLIFY = FALSE
)

do.call(
function(...) rbind(..., make.row.names = FALSE),
res
)
}
)

.matchAddColData <- function(reshaped, colData, colDataCols) {
extraColumns <- as.data.frame(colData[, colDataCols, drop = FALSE])
rowNameValues <- rownames(extraColumns)
Expand All @@ -418,16 +358,18 @@ setMethod(
reshaped[, c("assay", "primary", "rowname", "colname", "value")]
}

# longFrom method ---------------------------------------------------------

#' @rdname MultiAssayExperiment-helpers
#'
#' @details The `longFormat` "ANY" class method, works with classes such as
#' @details The `longForm` "ANY" class method, works with classes such as
#' [`ExpressionSet`][Biobase::ExpressionSet] and
#' [`SummarizedExperiment`][SummarizedExperiment::SummarizedExperiment-class] as
#' well as `matrix` to provide a consistent long and skinny
#' [`DataFrame`][S4Vectors::DataFrame-class].
#'
#' @section longFormat:
#' The 'longFormat' method takes data from the [`ExperimentList`]
#' @section longForm:
#' The 'longForm' method takes data from the [`ExperimentList`]
#' in a `MultiAssayExperiment` and returns a uniform
#' `DataFrame`. The resulting DataFrame has columns indicating
#' primary, rowname, colname and value. This method can optionally include
Expand All @@ -441,21 +383,24 @@ setMethod(
#' @param colDataCols A `character`, `logical`, or `numeric`
#' index for `colData` columns to be included
#'
#' @param i longFormat: The i-th assay in
#' @param i longForm: The i-th assay in
#' `SummarizedExperiment`-like objects. A vector input is
#' supported in the case that the `SummarizedExperiment` object(s) has more
#' than one assay (default 1L),
#' renameColname: Either a `numeric` or `character` index
#' indicating the assay whose colnames are to be renamed
#'
#' @exportMethod longFormat
#' @importFrom BiocBaseUtils lifeCycle
#' @importFrom BiocGenerics longForm
#'
#' @exportMethod longForm
setMethod(
"longFormat", "MultiAssayExperiment",
"longForm", "MultiAssayExperiment",
function(object, colDataCols = NULL, i = 1L, ...) {
if (any(.emptyAssays(experiments(object))))
object <- .dropEmpty(object, warn = FALSE)

longDataFrame <- longFormat(experiments(object), i = i)
longDataFrame <- longForm(experiments(object), i = i)

longDataFrame <- .mapOrderPrimary(longDataFrame, sampleMap(object))

Expand All @@ -467,6 +412,51 @@ setMethod(
}
)

#' @rdname MultiAssayExperiment-helpers
#' @exportMethod longForm
setMethod(
"longForm", "ExperimentList",
function(object, colDataCols, i = 1L, ...) {
samelength <- identical(length(object), length(i))
if (!samelength && identical(length(i), 1L))
i <- rep(i, length(object))
res <- mapply(
function(obj, obname, idx) {
data.frame(
assay = obname,
longForm(obj, i = idx),
stringsAsFactors = FALSE
)
}, obj = object, obname = names(object), idx = i, SIMPLIFY = FALSE
)
do.call(
function(...) rbind(..., make.row.names = FALSE),
res
)
}
)

#' @rdname MultiAssayExperiment-helpers
#' @exportMethod longForm
setMethod("longForm", "ANY", function(object, colDataCols, i = 1L, ...) {
rowNAMES <- rownames(object)
if (is.null(rowNAMES)) rowNames <- as.character(seq_len(nrow(object)))

if (is(object, "ExpressionSet"))
object <- Biobase::exprs(object)
if (is(object, "SummarizedExperiment") || is(object, "RaggedExperiment"))
object <- assay(object, i = i)

BiocBaseUtils::checkInstalled("reshape2")

res <- reshape2::melt(
object, varnames = c("rowname", "colname"), value.name = "value"
)
if (!is.character(res[["rowname"]]))
res[["rowname"]] <- as.character(res[["rowname"]])
res
})

# wideformat function -----------------------------------------------------

.wideFormatANY <- function(object, i, check.names) {
Expand Down Expand Up @@ -551,7 +541,7 @@ wideFormat <- function(object, colDataCols = NULL, check.names = TRUE,
if (is.null(colDataCols)) colDataCols <- character(0L)
nameFUN <- if (check.names) make.names else I
cnames <- colnames(object)
longDataFrame <- longFormat(experiments(object), i = i)
longDataFrame <- longForm(experiments(object), i = i)
longList <- split(longDataFrame, longDataFrame[["assay"]])
longList <- lapply(longList, .mapOrderPrimary, sampleMap(object))
colsofinterest <- c("assay", "rowname")
Expand Down
4 changes: 2 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
#' @source https://github.com/waldronlab/multiassayexperiment-tcga
#'
#' @usage data("miniACC")
#'
#'
#' @examples
#'
#' data("miniACC")
#' metadata(miniACC)
#' colnames(colData(miniACC))
#' table(miniACC$vital_status)
#' longFormat(
#' longForm(
#' miniACC["MAPK3", , ],
#' colDataCols = c("vital_status", "days_to_death")
#' )
Expand Down
115 changes: 115 additions & 0 deletions R/longFormat-deprecated.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#' @name longFormat-deprecated
#'
#' @title Deprecated longFormat method
#'
#' @description The `longFormat` method is deprecated and will be removed in a
#' future release. Please use the `longForm` method instead.
#'
#' @details The `longFormat` "ANY" class method, works with classes such as
#' [`ExpressionSet`][Biobase::ExpressionSet] and
#' [`SummarizedExperiment`][SummarizedExperiment::SummarizedExperiment-class] as
#' well as `matrix` to provide a consistent long and skinny
#' [`DataFrame`][S4Vectors::DataFrame-class].
#'
#' @section longFormat:
#' The 'longFormat' method takes data from the [`ExperimentList`]
#' in a `MultiAssayExperiment` and returns a uniform
#' `DataFrame`. The resulting DataFrame has columns indicating
#' primary, rowname, colname and value. This method can optionally include
#' columns of the MultiAssayExperiment colData named by `colDataCols` character
#' vector argument. (`MultiAssayExperiment` method only). The `i` argument
#' allows the user to specify the assay value for the
#' `SummarizedExperiment` assay function's `i` argument.
#'
#' @param object Any supported class object
#'
#' @param colDataCols A `character`, `logical`, or `numeric`
#' index for `colData` columns to be included
#'
#' @param i longFormat: The i-th assay in
#' `SummarizedExperiment`-like objects. A vector input is
#' supported in the case that the `SummarizedExperiment` object(s) has more
#' than one assay (default 1L),
#' renameColname: Either a `numeric` or `character` index
#' indicating the assay whose colnames are to be renamed
#'
#' @param ... Additional arguments. See details for more information.
#'
#' @importFrom BiocBaseUtils lifeCycle
#'
#' @aliases longFormat
#' @export
setGeneric(
"longFormat",
function(object, colDataCols = NULL, i = 1L, ...)
standardGeneric("longFormat")
)

#' @rdname longFormat-deprecated
#' @exportMethod longFormat
setMethod(
"longFormat", "MultiAssayExperiment",
function(object, colDataCols = NULL, i = 1L, ...) {
if (any(.emptyAssays(experiments(object))))
object <- .dropEmpty(object, warn = FALSE)

longDataFrame <- longFormat(experiments(object), i = i)

longDataFrame <- .mapOrderPrimary(longDataFrame, sampleMap(object))

if (!is.null(colDataCols))
longDataFrame <-
.matchAddColData(longDataFrame, colData(object), colDataCols)

as(longDataFrame, "DataFrame")
}
)

#' @rdname longFormat-deprecated
#' @exportMethod longFormat
setMethod("longFormat", "ANY", function(object, colDataCols, i = 1L, ...) {
rowNAMES <- rownames(object)
if (is.null(rowNAMES)) rowNames <- as.character(seq_len(nrow(object)))

if (is(object, "ExpressionSet"))
object <- Biobase::exprs(object)
if (is(object, "SummarizedExperiment") || is(object, "RaggedExperiment"))
object <- assay(object, i = i)

BiocBaseUtils::checkInstalled("reshape2")

res <- reshape2::melt(
object, varnames = c("rowname", "colname"), value.name = "value"
)
if (!is.character(res[["rowname"]]))
res[["rowname"]] <- as.character(res[["rowname"]])
res
})

#' @rdname longFormat-deprecated
#' @exportMethod longFormat
setMethod(
"longFormat", "ExperimentList",
function(object, colDataCols, i = 1L, ...) {
lifeCycle(
"longForm", package = "MultiAssayExperiment", title = "longFormat"
)
samelength <- identical(length(object), length(i))
if (!samelength && identical(length(i), 1L))
i <- rep(i, length(object))
res <- mapply(
function(obj, obname, idx) {
data.frame(
assay = obname,
longFormat(obj, i = idx),
stringsAsFactors = FALSE
)
}, obj = object, obname = names(object), idx = i, SIMPLIFY = FALSE
)

do.call(
function(...) rbind(..., make.row.names = FALSE),
res
)
}
)
Loading

0 comments on commit a42ee78

Please sign in to comment.