Skip to content

Commit 6d7414d

Browse files
authored
Merge pull request #98 from lambdamoses/master
Allow y as S4 DataFrame in *_join()
2 parents cdad9bf + 92ebb1d commit 6d7414d

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

R/dplyr_methods.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,14 @@ rowwise.SingleCellExperiment <- function(data, ...) {
336336
#' tt |> left_join(tt |>
337337
#' distinct(groups) |>
338338
#' mutate(new_column=1:2))
339-
#'
339+
#'
340+
#' library(S4Vectors)
341+
#' # y can be S4 DataFrame for _*join, though not tested on list columns
342+
#' DF <- tt |>
343+
#' distinct(groups) |>
344+
#' mutate(new_column=1:2) |> DataFrame()
345+
#' tt |> left_join(DF)
346+
#'
340347
#' @importFrom SummarizedExperiment colData
341348
#' @importFrom dplyr left_join
342349
#' @importFrom dplyr count
@@ -349,7 +356,7 @@ left_join.SingleCellExperiment <- function(x, y,
349356
if (is_sample_feature_deprecated_used(x, .cols)) {
350357
x <- ping_old_special_column_into_metadata(x)
351358
}
352-
359+
if (is(y, "DataFrame")) y <- as.data.frame(y)
353360
z <- x |>
354361
as_tibble() |>
355362
dplyr::left_join(y, by=by, copy=copy, suffix=suffix, ...)
@@ -389,7 +396,7 @@ inner_join.SingleCellExperiment <- function(x, y,
389396
if (is_sample_feature_deprecated_used(x, .cols)) {
390397
x <- ping_old_special_column_into_metadata(x)
391398
}
392-
399+
if (is(y, "DataFrame")) y <- as.data.frame(y)
393400
z <- x |>
394401
as_tibble() |>
395402
dplyr::inner_join(y, by=by, copy=copy, suffix=suffix, ...)
@@ -430,7 +437,7 @@ right_join.SingleCellExperiment <- function(x, y,
430437
if (is_sample_feature_deprecated_used(x, .cols)) {
431438
x <- ping_old_special_column_into_metadata(x)
432439
}
433-
440+
if (is(y, "DataFrame")) y <- as.data.frame(y)
434441
z <- x |>
435442
as_tibble() |>
436443
dplyr::right_join(y, by=by, copy=copy, suffix=suffix, ...)
@@ -467,7 +474,7 @@ full_join.SingleCellExperiment <- function(x, y,
467474
if (is_sample_feature_deprecated_used(x, .cols)) {
468475
x <- ping_old_special_column_into_metadata(x)
469476
}
470-
477+
if (is(y, "DataFrame")) y <- as.data.frame(y)
471478
z <- x |>
472479
as_tibble() |>
473480
dplyr::full_join(y, by=by, copy=copy, suffix=suffix, ...)

man/left_join.Rd

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

tests/testthat/test-dplyr_methods.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
library(S4Vectors)
12
data(pbmc_small)
23
df <- pbmc_small
34
df$number <- sample(seq(ncol(df)))
@@ -130,6 +131,17 @@ test_that("left_join()", {
130131
expect_identical(colData(fd)[-n], colData(df))
131132
})
132133

134+
test_that("left_join(), with DataFrame y", {
135+
y <- df |>
136+
distinct(factor) |>
137+
mutate(string=letters[seq(nlevels(df$factor))]) |>
138+
DataFrame()
139+
fd <- left_join(df, y, by="factor")
140+
expect_s4_class(fd, "SingleCellExperiment")
141+
expect_equal(n <- ncol(colData(fd)), ncol(colData(df))+1)
142+
expect_identical(colData(fd)[-n], colData(df))
143+
})
144+
133145
test_that("inner_join()", {
134146
y <- df |>
135147
distinct(factor) |>
@@ -141,6 +153,17 @@ test_that("inner_join()", {
141153
expect_equal(ncol(fd), sum(df$factor == fd$factor[1]))
142154
})
143155

156+
test_that("inner_join(), with DataFrame y", {
157+
y <- df |>
158+
distinct(factor) |>
159+
mutate(string=letters[seq(nlevels(df$factor))]) |>
160+
slice(1) |> DataFrame()
161+
fd <- inner_join(df, y, by="factor")
162+
expect_s4_class(fd, "SingleCellExperiment")
163+
expect_equal(n <- ncol(colData(fd)), ncol(colData(df))+1)
164+
expect_equal(ncol(fd), sum(df$factor == fd$factor[1]))
165+
})
166+
144167
test_that("right_join()", {
145168
y <- df |>
146169
distinct(factor) |>
@@ -152,6 +175,17 @@ test_that("right_join()", {
152175
expect_equal(ncol(fd), sum(df$factor == fd$factor[1]))
153176
})
154177

178+
test_that("right_join(), with DataFrame y", {
179+
y <- df |>
180+
distinct(factor) |>
181+
mutate(string=letters[seq(nlevels(df$factor))]) |>
182+
slice(1) |> DataFrame()
183+
fd <- right_join(df, y, by="factor")
184+
expect_s4_class(fd, "SingleCellExperiment")
185+
expect_equal(n <- ncol(colData(fd)), ncol(colData(df))+1)
186+
expect_equal(ncol(fd), sum(df$factor == fd$factor[1]))
187+
})
188+
155189
test_that("full_join()", {
156190
# w/ duplicated cell names
157191
y <- tibble(factor="g2", other=1:3)
@@ -169,6 +203,23 @@ test_that("full_join()", {
169203
mutate(df, factor=paste(factor)))
170204
})
171205

206+
test_that("full_join(), with DataFrame y", {
207+
# w/ duplicated cell names
208+
y <- tibble(factor="g2", other=1:3) |> DataFrame()
209+
fd <- expect_message(full_join(df, y, by="factor", relationship="many-to-many"))
210+
expect_s3_class(fd, "tbl_df")
211+
expect_true(all(is.na(fd$other[fd$factor != "g2"])))
212+
expect_true(all(!is.na(fd$other[fd$factor == "g2"])))
213+
expect_equal(nrow(fd), ncol(df)+2*sum(df$factor == "g2"))
214+
# w/o duplicates
215+
y <- tibble(factor="g2", other=1) |> DataFrame()
216+
fd <- expect_silent(full_join(df, y, by="factor"))
217+
expect_s4_class(fd, "SingleCellExperiment")
218+
expect_identical(
219+
select(fd, -other),
220+
mutate(df, factor=paste(factor)))
221+
})
222+
172223
test_that("slice()", {
173224
expect_identical(slice(df), df[, 0])
174225
expect_identical(slice(df, ncol(df)+1), df[, 0])

0 commit comments

Comments
 (0)