From e824414281bd71ce54eb353c504ffa532b6ed0c8 Mon Sep 17 00:00:00 2001 From: Qile0317 Date: Tue, 29 Oct 2024 20:25:01 -0700 Subject: [PATCH] fix test --- R/clonalCompare.R | 3 ++- tests/testthat/test-clonalCompare.R | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/R/clonalCompare.R b/R/clonalCompare.R index 4459146..a980b65 100644 --- a/R/clonalCompare.R +++ b/R/clonalCompare.R @@ -102,12 +102,13 @@ clonalCompare <- function(input.data, } compareColname <- ifelse(proportion, "Proportion", "Count") + normalizer <- ifelse(proportion, sum, length) Con.df <- input.data %>% purrr::imap(function(df, columnNames) { tbl <- as.data.frame(table(df[, cloneCall])) if (proportion) { - tbl[, 2] <- tbl[, 2] / sum(tbl[, 2]) + tbl[, 2] <- tbl[, 2] / normalizer(tbl[, 2]) } colnames(tbl) <- c("clones", compareColname) tbl$Sample <- columnNames diff --git a/tests/testthat/test-clonalCompare.R b/tests/testthat/test-clonalCompare.R index 20667b3..0812000 100644 --- a/tests/testthat/test-clonalCompare.R +++ b/tests/testthat/test-clonalCompare.R @@ -68,12 +68,28 @@ test_that("clonalCompare works with exportTable and prop FALSE", { ) } - clonalCompareCountConvertedToProp <- getClonalCompareRes(FALSE) %>% - dplyr::mutate(Count = Count / length(Count)) %>% #FIXME this is wrong - dplyr::rename(Proportion = Count) + countCompareRes <- getClonalCompareRes(prop = FALSE) + propCompareRes <- getClonalCompareRes(prop = TRUE) expect_identical( - getClonalCompareRes(TRUE), - clonalCompareCountConvertedToProp + countCompareRes %>% dplyr::select(-Count), + propCompareRes %>% dplyr::select(-Proportion) + ) + + fullJoined <- getClonalCompareRes(FALSE) %>% + dplyr::full_join( + getClonalCompareRes(TRUE) + ) + + expect_setequal( + colnames(fullJoined), + c("clones", "Count", "original.clones", "Proportion", "Sample") + ) + + countPropFactor <- fullJoined$Count / fullJoined$Proportion + + expect_identical( + as.integer(fullJoined$Count - fullJoined$Proportion * countPropFactor), + integer(nrow(fullJoined)) ) })