Skip to content

Commit dde1d58

Browse files
authored
pass value for cutoff (#754)
Signed-off-by: Daena Rys <rysdaena8@gmail.com>
1 parent 5f07bf5 commit dde1d58

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

R/transformCounts.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@
4848
#' specifies relative difference threshold and determines the first point
4949
#' where the relative change in differences between consecutive quantiles
5050
#' exceeds this threshold. (Default: \code{0.1}) For \code{"cutoff"},
51-
#' values less than or equal to the threshold are replaced with \code{NA}.
51+
#' values less than or equal to the threshold are replaced with \code{value}.
5252
#' (Default: \code{0})
53+
#' \item \code{value}: \code{Numeric scalar}. For \code{"cutoff"}, specifies
54+
#' the replacement value for counts less than or equal to the threshold.
55+
#' (Default: \code{NA})
5356
#' \item \code{tree}: \code{phylo}. Phylogeny used in PhILR transformation.
5457
#' If \code{NULL}, the tree is retrieved from \code{x}.
5558
#' (Default: \code{NULL}).
@@ -102,7 +105,7 @@
102105
#'
103106
#' \item 'cutoff': In some ecological studies, only strictly positive values
104107
#' are taken into account. This method keeps only values greater than
105-
#' \code{threshold} and replaces all other values with \code{NA}.
108+
#' \code{threshold} and replaces all other values with \code{value}.
106109
#'
107110
#' }
108111
#'
@@ -813,10 +816,13 @@ setMethod("transformAssay", signature = c(x = "SingleCellExperiment"),
813816
}
814817

815818
# This function replaces values under or equal to threshold with NA
816-
.apply_cutoff <- function(mat, threshold = 0, ...){
819+
.apply_cutoff <- function(mat, threshold = 0, value = NA, ...){
817820
if( !.is_a_numeric(threshold) ){
818-
stop("'threshold' must be a single numierc value.", call. = FALSE)
821+
stop("'threshold' must be a single numeric value.", call. = FALSE)
822+
}
823+
if( length(value) != 1L || (!is.numeric(value) && !is.na(value)) ){
824+
stop("'value' must be a single numeric value or NA.", call. = FALSE)
819825
}
820-
mat[ mat <= threshold ] <- NA
826+
mat[ mat <= threshold ] <- value
821827
return(mat)
822828
}

man/transformAssay.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-5transformCounts.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,23 @@ test_that("transformAssay", {
405405
check.attributes = FALSE
406406
)
407407
expect_equal(colData(tse), colData(altExp(tse, "philr")))
408+
################################# CUTOFF ###############################
409+
# Basic cutoff with NA value
410+
expect_identical(
411+
.apply_cutoff(c(0, 1, 2, 3, 0.5), 1),
412+
c(NA, NA, 2, 3, NA)
413+
)
414+
# Basic cutoff with numeric replacement
415+
expect_identical(
416+
.apply_cutoff(c(0, 1, 2, 3, 0.5), 2, value = 999),
417+
c(999, 999, 999, 3, 999)
418+
)
419+
# Error on non-length-1 value
420+
expect_error(
421+
.apply_cutoff(c(1, 2, 3), 1, value = c(1, 2)),
422+
"'value' must be a single numeric value or NA"
423+
)
424+
408425
}
409426

410427
# TSE object

0 commit comments

Comments
 (0)