Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper for agglomerateByRank/mergeRows #389

Merged
merged 6 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,21 @@

taxa_tab
}

################################################################################
# internal wrappers for agglomerateByRank/mergeRows
.merge_features <- function(x, merge.by, ...) {
# Check if merge.by parameter belongs to taxonomyRanks
if (is.character(merge.by) && length(merge.by) < 2 && merge.by %in% taxonomyRanks(x)) {
Daenarys8 marked this conversation as resolved.
Show resolved Hide resolved
#Merge using agglomerateByRank
x <- agglomerateByRank(x, rank = merge.by, ...)
} else {
# Merge using mergeRows
x <- mergeRows(x, f = merge.by, ...)
}

return(x)

Daenarys8 marked this conversation as resolved.
Show resolved Hide resolved
}


Daenarys8 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions tests/testthat/test-2merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ test_that("merge", {
merged <- mergeRows(tse, f=rowData(tse)$group2, mergeTree=TRUE)
merged2 <- mergeRows(tse, f = rowData(tse)$group2, mergeTree = FALSE)
merged3 <- mergeRows(esophagus, f = rowData(esophagus)$group2, mergeTree = TRUE)
merged4 <- .merge_features(tse, merge.by = rowData(tse)$group2)
merged5 <- mergeRows(tse, f = rowData(tse)$group2)
Daenarys8 marked this conversation as resolved.
Show resolved Hide resolved
expect_equal( rowLinks(merged)$whichTree,
rowLinks(merged2)$whichTree )
expect_false( all(rowLinks(merged) == rowLinks(merged2)) )
expect_equal(rowTree(tse), rowTree(merged2))
expect_equal(rowTree(merged), rowTree(merged3))
expect_equal(merged4, merged5)

# Both datasets have group variable
merged <- mergeRows(tse, f = rowData(tse)$group, mergeTree = TRUE)
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-3agglomerate.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ test_that("agglomerate", {
expect_equal(rowData(actual)$Family,c("c",NA,"d","e","f","g","h",NA))
actual <- agglomerateByRank(xtse,"Phylum")
expect_equivalent(rowData(actual),rowData(actual_phylum))

#
Daenarys8 marked this conversation as resolved.
Show resolved Hide resolved
actual1 <- agglomerateByRank(xtse,"Family")
actual2 <- .merge_features(xtse, merge.by = "Family")
expect_equal(actual1, actual2)

# Only one rank available in the object -
# the same dimensionality is retained
Expand Down