Skip to content

Commit 6d3ba57

Browse files
committed
Don't throw an error in the Z-score calculation when just one feature has variation. Update tests accordingly. Partially addresses #83
1 parent 05e7886 commit 6d3ba57

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

R/multi_gene_z_score.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
#' @family functions for summarizing expression of multiple continuous variables simultaneously
1515
#' @keywords internal
1616
multi_gene_z_score <- function(cont_mat) {
17-
# Z-score calculation requires at least 2 features with nonzero variance.
17+
# Z-score calculation requires at least 1 feature with nonzero variance.
1818
# Verify this and drop any zero-variance features
1919
good_indices <- which(colSds(cont_mat, na.rm = TRUE) != 0)
20-
if (length(good_indices) < 2) {
21-
stop("After dropping features with no expression variation, less than 2 features were left. This error can occur when using data from only 1 spot.", call. = FALSE)
20+
if (length(good_indices) < 1) {
21+
stop("After dropping features with no expression variation, no features were left. This error can occur when using data from only 1 spot.", call. = FALSE)
2222
}
2323
if (ncol(cont_mat) - length(good_indices) > 0) {
2424
warning(

tests/testthat/test-multi_gene_z_score.R

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,32 @@ test_that(
1111
)
1212

1313
# NAs should be correctly removed from columns (as long as 2 non-NAs remain
14-
# in at least 2 columns), and the result should have no NAs
14+
# in at least 1 column), and the result should have no NAs
1515
cont_mat <- matrix(c(1, NA, 3, NA, 2, 0), ncol = 2)
1616
colnames(cont_mat) <- c("good1", "good2")
1717
expect_equal(any(is.na(multi_gene_z_score(cont_mat))), FALSE)
1818

19-
# With only one good column, an error should be thrown
19+
# With only one good column, the result should simply be the
20+
# Z-score-normalized good column. A warning should indicate which
21+
# columns were dropped
2022
cont_mat <- matrix(c(1, NA, 3, 4, 2, 2), ncol = 3)
2123
colnames(cont_mat) <- c("bad1", "good", "bad2")
24+
25+
temp = c(3, 4)
26+
expected_result = (temp - mean(temp)) / sd(temp)
27+
28+
expect_warning(
29+
{ actual_result = multi_gene_z_score(cont_mat) },
30+
"Dropping features\\(s\\) 'bad1', 'bad2' which have no expression variation"
31+
)
32+
expect_equal(actual_result, expected_result)
33+
34+
# An error should be thrown if no columns have variation
35+
cont_mat <- matrix(c(1, 1, 0, 0, 2, 2), ncol = 3)
36+
colnames(cont_mat) <- c("bad1", "bad2", "bad3")
2237
expect_error(
2338
multi_gene_z_score(cont_mat),
24-
"After dropping features with no expression variation, less than 2 features were left"
39+
"^After dropping features with no expression variation"
2540
)
2641
}
2742
)

0 commit comments

Comments
 (0)