diff --git a/R/utils.R b/R/utils.R index ec9485804..9d2989ac9 100644 --- a/R/utils.R +++ b/R/utils.R @@ -283,3 +283,17 @@ 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) == 1 && merge.by %in% taxonomyRanks(x)) { + #Merge using agglomerateByRank + x <- agglomerateByRank(x, rank = merge.by, ...) + } else { + # Merge using mergeRows + x <- mergeRows(x, f = merge.by, ...) + } + return(x) +} diff --git a/tests/testthat/test-2merge.R b/tests/testthat/test-2merge.R index 90a0c5a3b..c13b82a22 100644 --- a/tests/testthat/test-2merge.R +++ b/tests/testthat/test-2merge.R @@ -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, mergeTree = TRUE) + merged5 <- mergeRows(tse, f = rowData(tse)$group2, mergeTree = TRUE) 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) diff --git a/tests/testthat/test-3agglomerate.R b/tests/testthat/test-3agglomerate.R index df1437ce3..e3710a8e1 100644 --- a/tests/testthat/test-3agglomerate.R +++ b/tests/testthat/test-3agglomerate.R @@ -51,6 +51,10 @@ 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)) + # + 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