Skip to content

Commit 3c8d054

Browse files
authored
filter bad clusters with missing/empty levels (#39)
1 parent 526ebdf commit 3c8d054

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

R/util.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ select_clusters <- function(obj, dedup=FALSE) {
112112
}
113113
}
114114

115+
# remove clusters that are missing factor levels or have a single empty level name
116+
good_clusters <- sapply(clusters, function(f) {
117+
if (length(levels(f)) == 0) {
118+
return(FALSE)
119+
}
120+
max(as.numeric(lapply(levels(f), nchar))) > 0
121+
})
122+
123+
clusters <- clusters[good_clusters]
124+
115125
if (dedup) deduplicate_clusters(clusters) else clusters
116126
}
117127

R/validate.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ validate_clusters <- function(clusters, barcode_count) {
127127
if (any(sapply(clusters, nlevels) > 32768)) {
128128
return(err("cluster cannot have more than 32768 groupings"))
129129
}
130+
if (any(sapply(clusters, nlevels) == 0)) {
131+
return(err("cluster must have at least one grouping"))
132+
}
133+
for (clusterIdx in seq_along(clusters)) {
134+
l <- levels(clusters[[clusterIdx]])
135+
if (!all(sapply(l, nchar))) {
136+
return(err("cluster group names cannot be the empty string"))
137+
}
138+
}
130139

131140
SUCCESS
132141
}

tests/testthat/test-validate.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ test_that("validate clusters", {
141141
expect_false(resp$success)
142142
expect_match(resp$msg, "cluster cannot have more than")
143143

144+
# cluster with zero levels
145+
factors <- list("f1" = factor(seq(32769), levels = c()))
146+
resp <- validate_clusters(factors, length(factors[[1]]))
147+
expect_false(resp$success)
148+
expect_match(resp$msg, "cluster must have at least one grouping")
149+
150+
# cluster with one level, but it's empty
151+
factors <- list("f1" = factor(seq(32769), levels = c("")))
152+
resp <- validate_clusters(factors, length(factors[[1]]))
153+
expect_false(resp$success)
154+
expect_match(resp$msg, "cluster group names cannot be the empty string")
155+
144156
# good
145157
factors <- list("f1" = factor(c("one", "two", "three")))
146158
resp <- validate_clusters(factors, 3)

0 commit comments

Comments
 (0)