Skip to content

Commit

Permalink
update longFormat to use reshape2
Browse files Browse the repository at this point in the history
  • Loading branch information
LiNk-NY committed May 1, 2024
1 parent 9189d8c commit 0062790
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Suggests:
maftools (>= 2.7.10),
R.rsp,
RaggedExperiment,
reshape2,
rmarkdown,
survival,
survminer,
Expand Down
31 changes: 8 additions & 23 deletions R/MultiAssayExperiment-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -340,35 +340,20 @@ setMethod("mergeReplicates", "ANY",

.longFormatANY <- function(object, i) {
rowNAMES <- rownames(object)
nullROWS <- is.null(rowNAMES)
if (nullROWS)
rowNAMES <- as.character(seq_len(nrow(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)

## use stats::reshape instead of reshape2::melt
if (nullROWS)
rownames(object) <- rowNAMES
# if (requireNamespace("reshape2", quietly = TRUE)) {
if (FALSE) {
object <- reshape2::melt(
object, varnames = c("rowname", "colname"), value.name = "value"
)
} else {
if (is.matrix(object))
object <- as.data.frame(object)
object <- stats::reshape(object, idvar = "rowname",
ids = rownames(object), times = names(object),
timevar = "colname", varying = list(names(object)),
direction = "long", v.names = "value")
## Reshape leaves rownames even if new.row.names = NULL
rownames(object) <- NULL
object[, c("rowname", "colname", "value")]
}
object
if (!requireNamespace("reshape2", quietly = TRUE))
stop("Package 'reshape2' is required for 'longFormat()' conversion")

reshape2::melt(
object, varnames = c("rowname", "colname"), value.name = "value"
)
}

.longFormatElist <- function(object, i) {
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-MultiAssayExperiment-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,13 @@ test_that(".longFormatANY works", {
data("miniACC", package="MultiAssayExperiment", envir = denv)
miniACC <- denv[["miniACC"]]

expect_silent(
longdf <- longFormat(miniACC)
)
expect_true(
all(
as.character(longdf[["rowname"]]) %in%
Reduce(union, rownames(miniACC))
)
)
})

0 comments on commit 0062790

Please sign in to comment.