Skip to content

Commit 640ef9d

Browse files
authored
Merge pull request #116 from stemangiola/append_samples
Update version to 1.19.2, introduce 'append_samples()' method for com…
2 parents 63bb13a + 2c9526a commit 640ef9d

File tree

10 files changed

+109
-90
lines changed

10 files changed

+109
-90
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: tidySingleCellExperiment
33
Title: Brings SingleCellExperiment to the Tidyverse
4-
Version: 1.19.1
4+
Version: 1.19.2
55
Authors@R: c(person("Stefano", "Mangiola",
66
comment=c(ORCID="0000-0001-7474-836X"),
77

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
S3method(add_count,SingleCellExperiment)
44
S3method(anti_join,SingleCellExperiment)
5+
S3method(append_samples,SingleCellExperiment)
56
S3method(arrange,SingleCellExperiment)
67
S3method(as_tibble,SingleCellExperiment)
78
S3method(bind_cols,SingleCellExperiment)
@@ -158,6 +159,7 @@ importFrom(tidyr,unnest)
158159
importFrom(tidyselect,all_of)
159160
importFrom(tidyselect,eval_select)
160161
importFrom(ttservice,aggregate_cells)
162+
importFrom(ttservice,append_samples)
161163
importFrom(ttservice,bind_cols)
162164
importFrom(ttservice,bind_rows)
163165
importFrom(ttservice,join_features)

R/dplyr_methods.R

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ arrange.SingleCellExperiment <- function(.data, ..., .by_group=FALSE) {
2929
#' @name bind_rows
3030
#' @rdname bind_rows
3131
#' @inherit ttservice::bind_rows
32-
#'
33-
#' @examples
34-
#' data(pbmc_small)
35-
#' tt <- pbmc_small
36-
#' bind_rows(tt, tt)
37-
#'
38-
#' tt_bind <- tt |> select(nCount_RNA, nFeature_RNA)
39-
#' tt |> bind_cols(tt_bind)
32+
#' @noRd
4033
#'
4134
#' @references
4235
#' Hutchison, W.J., Keyes, T.J., The tidyomics Consortium. et al. The tidyomics ecosystem: enhancing omic data analyses. Nat Methods 21, 1166–1170 (2024). https://doi.org/10.1038/s41592-024-02299-2
@@ -48,6 +41,13 @@ arrange.SingleCellExperiment <- function(.data, ..., .by_group=FALSE) {
4841
#' @importFrom SingleCellExperiment cbind
4942
#' @export
5043
bind_rows.SingleCellExperiment <- function(..., .id=NULL, add.cell.ids=NULL) {
44+
lifecycle::deprecate_warn(
45+
when = "1.19.2",
46+
what = "bind_rows()",
47+
with = "append_samples()",
48+
details = "bind_rows is not a generic method in dplyr and may cause conflicts. Use append_samples."
49+
)
50+
5151
tts <- flatten_if(dots_values(...), is_spliced)
5252

5353
new_obj <- SingleCellExperiment::cbind(tts[[1]], tts[[2]])
@@ -62,6 +62,45 @@ bind_rows.SingleCellExperiment <- function(..., .id=NULL, add.cell.ids=NULL) {
6262
new_obj
6363
}
6464

65+
#' @name append_samples
66+
#' @rdname append_samples
67+
#' @title Append samples from multiple SingleCellExperiment objects
68+
#'
69+
#' @description
70+
#' Append samples from multiple SingleCellExperiment objects by column-binding them.
71+
#' This function is equivalent to `cbind` but provides a tidyverse-like interface.
72+
#'
73+
#' @param x First SingleCellExperiment object to combine
74+
#' @param ... Additional SingleCellExperiment objects to combine by samples
75+
#' @param .id Object identifier (currently not used)
76+
#'
77+
#' @return A combined SingleCellExperiment object
78+
#'
79+
#' @examples
80+
#' data(pbmc_small)
81+
#' append_samples(pbmc_small, pbmc_small)
82+
#'
83+
#' @importFrom ttservice append_samples
84+
#' @importFrom rlang flatten_if
85+
#' @importFrom rlang is_spliced
86+
#' @importFrom SingleCellExperiment cbind
87+
#' @export
88+
append_samples.SingleCellExperiment <- function(x, ..., .id = NULL) {
89+
# Combine all arguments into a list
90+
tts <- flatten_if(list(x, ...), is_spliced)
91+
new_obj <- do.call(cbind, tts)
92+
93+
# If duplicated cell names
94+
if (any(duplicated(colnames(new_obj)))) {
95+
warning("tidySingleCellExperiment says:",
96+
" you have duplicated cell names, they will be made unique.")
97+
unique_colnames <- make.unique(colnames(new_obj), sep = "_")
98+
colnames(new_obj) <- unique_colnames
99+
}
100+
101+
new_obj
102+
}
103+
65104
#' @importFrom rlang flatten_if
66105
#' @importFrom rlang is_spliced
67106
#' @importFrom rlang dots_values

README.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ pbmc_small_nested_interactions <-
461461
cell_signaling(genes=rownames(data), cluster=cluster) |>
462462
inter_network(data=data, signal=_, genes=rownames(data), cluster=cluster) %$%
463463
`individual-networks` |>
464-
map_dfr(~ bind_rows(as_tibble(.x)))
464+
map_dfr(~ append_samples(as_tibble(.x)))
465465
}))
466466
467467
pbmc_small_nested_interactions |>

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,6 @@ pbmc_small_pca <-
314314
## TRUE, : You're computing too large a percentage of total singular values, use a
315315
## standard svd instead.
316316

317-
## Warning in (function (A, nv = 5, nu = nv, maxit = 1000, work = nv + 7, reorth =
318-
## TRUE, : did not converge--results might be invalid!; try increasing work or
319-
## maxit
320-
321317
``` r
322318
pbmc_small_pca
323319
```
@@ -742,7 +738,7 @@ pbmc_small_nested_interactions <-
742738
cell_signaling(genes=rownames(data), cluster=cluster) |>
743739
inter_network(data=data, signal=_, genes=rownames(data), cluster=cluster) %$%
744740
`individual-networks` |>
745-
map_dfr(~ bind_rows(as_tibble(.x)))
741+
map_dfr(~ append_samples(as_tibble(.x)))
746742
}))
747743

748744
pbmc_small_nested_interactions |>

inst/NEWS.rd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
\name{NEWS}
22
\title{News for Package \pkg{tidySingleCellExperiment}}
33

4+
\section{Changes in version 1.19.2, Bioconductor 3.22 Release}{
5+
\itemize{
6+
\item Soft deprecated \code{bind_rows()} in favor of \code{append_samples()} from ttservice.
7+
\item Added \code{append_samples()} method for SingleCellExperiment objects.
8+
\item \code{bind_rows()} is not a generic method in dplyr and may cause conflicts.
9+
\item Users are encouraged to use \code{append_samples()} instead.
10+
}}
11+
412
\section{Changes in version 1.4.0, Bioconductor 3.14 Release}{
513
\itemize{
614
\item Improved sample_n, and sample_frac functions.

man/append_samples.Rd

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

man/bind_rows.Rd

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/testthat/test-dplyr_methods.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ df$factor <- sample(
2424
# expect_identical(fd, df)
2525
# })
2626

27-
test_that("bind_rows()", {
27+
test_that("append_samples()", {
2828
# warn about duplicated cells names
29-
expect_warning(fd <- bind_rows(df, df))
29+
expect_warning(fd <- append_samples(df, df))
3030
# cell names should be unique after binding
3131
expect_true(!any(duplicated(pull(fd, .cell))))
3232
})

vignettes/introduction.Rmd

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ pbmc_small_cluster %>%
278278
.value=.abundance_counts, scale="column")
279279
```
280280

281+
# Combining datasets
282+
283+
We can use `append_samples()` to combine multiple SingleCellExperiment objects by samples.
284+
This is useful when you have multiple datasets that you want to analyze together.
285+
286+
```{r}
287+
# Create two subsets of the data
288+
pbmc_subset1 <- pbmc_small_cluster %>%
289+
filter(groups == "g1")
290+
291+
pbmc_subset2 <- pbmc_small_cluster %>%
292+
filter(groups == "g2")
293+
294+
# Combine them using append_samples
295+
combined_data <- append_samples(pbmc_subset1, pbmc_subset2)
296+
combined_data
297+
```
298+
281299
# Reduce dimensions
282300

283301
We can calculate the first 3 UMAP dimensions using `r BiocStyle::Biocpkg("scater")`.
@@ -459,7 +477,7 @@ pbmc_small_nested_interactions <-
459477
cell_signaling(genes=rownames(data), cluster=cluster) %>%
460478
inter_network(data=data, signal=., genes=rownames(data), cluster=cluster) %$%
461479
`individual-networks` %>%
462-
map_dfr(~ bind_rows(as_tibble(.x)))
480+
map_dfr(~ append_samples(as_tibble(.x)))
463481
}))
464482
465483
pbmc_small_nested_interactions %>%

0 commit comments

Comments
 (0)