Skip to content

Commit d05b3fd

Browse files
authored
Merge pull request #79 from HelenaLC/unit_tests
revise unit tests
2 parents e092048 + 02dcbf3 commit d05b3fd

File tree

6 files changed

+430
-343
lines changed

6 files changed

+430
-343
lines changed

R/dplyr_methods.R

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ mutate.SingleCellExperiment <- function(.data, ...) {
229229

230230
tst <-
231231
intersect(
232-
cols %>%
233-
names(),
232+
cols,
234233
get_special_columns(.data) %>%
235234
c(get_needed_columns(.data))
236235
) %>%
@@ -240,13 +239,17 @@ mutate.SingleCellExperiment <- function(.data, ...) {
240239
if (tst) {
241240
columns =
242241
get_special_columns(.data) %>%
243-
c(get_needed_columns()) %>%
242+
c(get_needed_columns(.data)) %>%
244243
paste(collapse=", ")
245244
stop(
246-
"tidySingleCellExperiment says: you are trying to rename a column that is view only",
247-
columns, " ",
248-
"(it is not present in the colData). If you want to mutate a view-only column, make a copy and mutate that one."
245+
"tidySingleCellExperiment says: you are trying to mutate a column that is view only `",
246+
cols,
247+
"` ",
248+
"(it is not present in the colData). If you want to mutate a view-only column, make a copy (e.g. mutate(new_column = ",
249+
cols[1],
250+
")) and mutate that one."
249251
)
252+
250253
}
251254

252255
colData(.data) <-
@@ -275,30 +278,38 @@ mutate.SingleCellExperiment <- function(.data, ...) {
275278
rename.SingleCellExperiment <- function(.data, ...) {
276279

277280
# Check that we are not modifying a key column
278-
cols <- tidyselect::eval_select(expr(c(...)), colData(.data) %>% as.data.frame())
279-
280-
tst <-
281-
intersect(
282-
cols %>%
283-
names(),
284-
get_special_columns(.data) %>%
285-
c(get_needed_columns(.data))
286-
) %>%
287-
length() %>%
288-
gt(0)
289-
290-
if (tst) {
291-
columns =
292-
get_special_columns(.data) %>%
293-
c(get_needed_columns(.data)) %>%
294-
paste(collapse=", ")
295-
stop(
296-
"tidySingleCellExperiment says: you are trying to rename a column that is view only",
297-
columns, " ",
298-
"(it is not present in the colData). If you want to mutate a view-only column, make a copy and mutate that one."
299-
)
300-
}
301-
281+
read_only_columns <- c(
282+
get_needed_columns(.data),
283+
get_special_columns(.data)
284+
)
285+
286+
# Small df to be more efficient
287+
df <- .data[1,1] |> as_tibble()
288+
289+
# What columns we are going to create
290+
cols_from <- tidyselect::eval_select(expr(c(...)), df) |> names()
291+
292+
# What are the columns before renaming
293+
original_columns = df |> colnames()
294+
295+
# What the column after renaming would be
296+
new_colums = df |> rename(...) |> colnames()
297+
298+
# What column you are impacting
299+
changed_columns = original_columns |> setdiff(new_colums)
300+
301+
# Check that you are not impacting any read-only columns
302+
if(any(changed_columns %in% read_only_columns))
303+
stop(
304+
"tidySingleCellExperiment says: you are trying to rename a column that is view only `",
305+
changed_columns,
306+
"` ",
307+
"(it is not present in the colData). If you want to rename a view-only column, make a copy (e.g. mutate(",
308+
cols_from[1],
309+
" = ",
310+
changed_columns[1],
311+
"))."
312+
)
302313

303314
colData(.data) <- dplyr::rename(colData(.data) %>% as.data.frame(), ...) %>% DataFrame()
304315

@@ -387,7 +398,7 @@ inner_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c(
387398
if(is_sample_feature_deprecated_used( x, when(by, !is.null(.) ~ by, ~ colnames(y)))){
388399
x= ping_old_special_column_into_metadata(x)
389400
}
390-
401+
391402
x %>%
392403
as_tibble() %>%
393404
dplyr::inner_join(y, by=by, copy=copy, suffix=suffix, ...) %>%

data/pbmc_small.rda

8.22 KB
Binary file not shown.

0 commit comments

Comments
 (0)