Skip to content

Append samples #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: tidySummarizedExperiment
Title: Brings SummarizedExperiment to the Tidyverse
Version: 1.19.4
Version: 1.19.5
Authors@R: c(person("Stefano", "Mangiola", email = "mangiolastefano@gmail.com",
role = c("aut", "cre")) )
Author: Stefano Mangiola [aut, cre] <mangiolastefano@gmail.com>
Expand All @@ -19,7 +19,7 @@ License: GPL-3
Depends:
R (>= 4.3.0),
SummarizedExperiment,
ttservice (>= 0.4.0)
ttservice (>= 0.5.0)
Imports:
dplyr,
tibble (>= 3.0.4),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(append_samples,SummarizedExperiment)
S3method(as_tibble,SummarizedExperiment)
S3method(bind_cols,RangedSummarizedExperiment)
S3method(bind_cols,SummarizedExperiment)
Expand Down Expand Up @@ -132,6 +133,7 @@ importFrom(tidyr,unite)
importFrom(tidyr,unnest)
importFrom(tidyselect,eval_select)
importFrom(tidyselect,one_of)
importFrom(ttservice,append_samples)
importFrom(ttservice,bind_cols)
importFrom(ttservice,bind_rows)
importFrom(ttservice,plot_ly)
Expand Down
66 changes: 59 additions & 7 deletions R/dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
#' @rdname bind_rows
#' @inherit ttservice::bind_rows
#' @param add.cell.ids Appends the corresponding values to
#' @examples
#' data(se)
#' ttservice::bind_rows(se, se)
#'
#' se_bind <- se |> select(dex, albut)
#' se |> ttservice::bind_cols(se_bind)
#'
#' @noRd
#' @importFrom rlang dots_values
#' @importFrom rlang flatten_if
#' @importFrom rlang is_spliced
Expand All @@ -17,8 +11,15 @@
#' @importFrom SummarizedExperiment assays<-
#' @importFrom S4Vectors SimpleList
#' @importFrom ttservice bind_rows
#' @importFrom lifecycle deprecate_warn
#' @export
bind_rows.SummarizedExperiment <- function(..., .id=NULL, add.cell.ids=NULL) {
lifecycle::deprecate_warn(
when = "1.19.5",
what = "bind_rows()",
with = "append_samples()",
details = "bind_rows is not a generic method in dplyr and may cause conflicts. Use append_samples from ttservice instead."
)
tts <- flatten_if(dots_values(...), is_spliced)

new_obj <-
Expand Down Expand Up @@ -55,6 +56,55 @@ bind_rows.SummarizedExperiment <- function(..., .id=NULL, add.cell.ids=NULL) {
new_obj
}

#' @name append_samples
#' @rdname append_samples
#' @title Append samples from multiple SummarizedExperiment objects
#'
#' @description
#' Append samples from multiple SummarizedExperiment objects by column-binding them.
#' This function is equivalent to `cbind` but provides a tidyverse-like interface.
#'
#' @param x First SummarizedExperiment object to combine
#' @param ... Additional SummarizedExperiment objects to combine by samples
#' @param .id Object identifier (currently not used)
#'
#' @return A combined SummarizedExperiment object
#'
#' @examples
#' data(se)
#' append_samples(se, se)
#'
#' @importFrom ttservice append_samples
#' @importFrom rlang dots_values
#' @importFrom rlang flatten_if
#' @importFrom rlang is_spliced
#' @importFrom SummarizedExperiment cbind
#' @importFrom SummarizedExperiment assays
#' @importFrom SummarizedExperiment assays<-
#' @importFrom S4Vectors SimpleList
#' @export
append_samples.SummarizedExperiment <- function(x, ..., .id = NULL) {
# Combine all arguments into a list
tts <- flatten_if(list(x, ...), is_spliced)
new_obj <- do.call(cbind, tts)

# If duplicated sample names
if (any(duplicated(colnames(new_obj)))) {
warning("tidySummarizedExperiment says:",
" you have duplicated sample names, they will be made unique.")
unique_colnames <- make.unique(colnames(new_obj), sep = "_")
colnames(new_obj) <- unique_colnames

# Change also all assays colnames
assays(new_obj) <- assays(new_obj)@listData |> lapply(function(.x) {
Copy link
Preview

Copilot AI Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly accessing the @listData slot is discouraged. Instead, consider using assays(new_obj) as a SimpleList and applying lapply() over its elements, then reassigning via SimpleList() to avoid relying on internal slots.

Suggested change
assays(new_obj) <- assays(new_obj)@listData |> lapply(function(.x) {
assays(new_obj) <- lapply(assays(new_obj), function(.x) {

Copilot uses AI. Check for mistakes.

colnames(.x) <- unique_colnames
.x
}) |> SimpleList()
}

new_obj
}

#' @importFrom rlang flatten_if
#' @importFrom rlang is_spliced
#' @importFrom rlang dots_values
Expand Down Expand Up @@ -1089,3 +1139,5 @@ group_split.SummarizedExperiment <- function(.tbl, ..., .keep = TRUE) {
}

}


8 changes: 8 additions & 0 deletions inst/NEWS.rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
\name{NEWS}
\title{News for Package \pkg{tidySummarizedExperiment}}

\section{Changes in version 1.19.5}{
\itemize{
\item Soft deprecated \code{bind_rows()} in favor of \code{append_samples()} from ttservice.
\item Added \code{append_samples()} method for SummarizedExperiment objects.
\item \code{bind_rows()} is not a generic method in dplyr and may cause conflicts.
\item Users are encouraged to use \code{append_samples()} instead.
}}

\section{Changes in version 1.19.2, Bioconductor 3.22 Release}{
\itemize{
\item Updated documentation to properly reflect S3 method structure.
Expand Down
28 changes: 28 additions & 0 deletions man/append_samples.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 0 additions & 71 deletions man/bind_rows.Rd

This file was deleted.

17 changes: 8 additions & 9 deletions tests/testthat/test-dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ context("dplyr test")
library(tidySummarizedExperiment)


test_that("bind_rows", {
pasilla_bind <- bind_rows(pasilla, pasilla)

pasilla_bind %>%
count(.sample, .feature) %>%
dplyr::count(n) %>%
filter(n > 1) %>%
nrow() %>%
expect_equal(0)
test_that("append_samples", {
pasilla_bind <- append_samples(pasilla, pasilla)

# Check that the combined object has the expected number of samples
expect_equal(ncol(pasilla_bind), ncol(pasilla) * 2)

# Check that it's still a SummarizedExperiment
expect_true(inherits(pasilla_bind, "SummarizedExperiment"))
})

test_that("distinct", {
Expand Down
15 changes: 15 additions & 0 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ pasilla_tidy %>%
unite("group", c(condition, type))
```

We can use `append_samples` to combine multiple SummarizedExperiment objects by samples. It is equivalent to `cbind` but it is a tidyverse-like function.

```{r}
# Create two subsets of the data
pasilla_subset1 <- pasilla_tidy %>%
filter(condition == "untreated")

pasilla_subset2 <- pasilla_tidy %>%
filter(condition == "treated")

# Combine them using append_samples
combined_data <- append_samples(pasilla_subset1, pasilla_subset2)
combined_data
```

We can also combine commands with the tidyverse pipe `%>%`.

For example, we could combine `group_by` and `summarise` to get the total counts for each sample.
Expand Down
Loading