Skip to content

Commit d348bed

Browse files
authored
Merge pull request #57 from william-hutchison/master
Added aggregate cells function and dependencies
2 parents 92cc7f7 + 6411bff commit d348bed

File tree

7 files changed

+585
-4
lines changed

7 files changed

+585
-4
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Imports:
3131
pillar,
3232
stringr,
3333
cli,
34-
fansi
34+
fansi,
35+
Matrix
3536
Suggests:
3637
BiocStyle,
3738
testthat,
@@ -44,7 +45,6 @@ Suggests:
4445
tidyHeatmap,
4546
igraph,
4647
GGally,
47-
Matrix,
4848
uwot,
4949
celldex,
5050
dittoSeq,

NAMESPACE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ S3method(unite,SingleCellExperiment)
4141
S3method(unnest,tidySingleCellExperiment_nested)
4242
export("%>%")
4343
export(add_count)
44+
export(aggregate_cells)
4445
export(arrange)
4546
export(as_tibble)
4647
export(bind_cols)
@@ -76,6 +77,9 @@ export(tidy)
7677
export(unite)
7778
export(unnest)
7879
export(unnest_single_cell_experiment)
80+
import(dplyr)
81+
import(tidyr)
82+
importFrom(Matrix,rowSums)
7983
importFrom(S4Vectors,"metadata<-")
8084
importFrom(S4Vectors,DataFrame)
8185
importFrom(S4Vectors,metadata)
@@ -89,6 +93,7 @@ importFrom(dplyr,add_count)
8993
importFrom(dplyr,arrange)
9094
importFrom(dplyr,count)
9195
importFrom(dplyr,distinct)
96+
importFrom(dplyr,distinct_at)
9297
importFrom(dplyr,filter)
9398
importFrom(dplyr,full_join)
9499
importFrom(dplyr,group_by)
@@ -114,12 +119,15 @@ importFrom(ggplot2,ggplot)
114119
importFrom(lifecycle,deprecate_warn)
115120
importFrom(magrittr,"%$%")
116121
importFrom(magrittr,"%>%")
122+
importFrom(magrittr,equals)
123+
importFrom(magrittr,set_rownames)
117124
importFrom(methods,as)
118125
importFrom(pillar,align)
119126
importFrom(pillar,get_extent)
120127
importFrom(pillar,style_subtle)
121128
importFrom(pillar,tbl_format_header)
122129
importFrom(plotly,plot_ly)
130+
importFrom(purrr,as_mapper)
123131
importFrom(purrr,imap)
124132
importFrom(purrr,map)
125133
importFrom(purrr,map2)
@@ -135,6 +143,9 @@ importFrom(rlang,flatten_if)
135143
importFrom(rlang,is_empty)
136144
importFrom(rlang,is_spliced)
137145
importFrom(rlang,names2)
146+
importFrom(rlang,quo_is_null)
147+
importFrom(rlang,quo_is_symbol)
148+
importFrom(rlang,quo_is_symbolic)
138149
importFrom(rlang,quo_name)
139150
importFrom(rlang,quo_squash)
140151
importFrom(stringr,regex)
@@ -153,5 +164,6 @@ importFrom(tidyr,unite)
153164
importFrom(tidyr,unnest)
154165
importFrom(tidyselect,eval_select)
155166
importFrom(ttservice,join_features)
167+
importFrom(utils,data)
156168
importFrom(utils,tail)
157169
importFrom(vctrs,new_data_frame)

R/methods.R

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,67 @@ tidy.SingleCellExperiment <- function(object) {
131131
object
132132
}
133133

134-
135-
134+
#' Aggregate cells
135+
#'
136+
#' @description Combine cells into groups based on shared variables and aggregate feature counts.
137+
#'
138+
#' @importFrom magrittr "%>%"
139+
#' @importFrom rlang enquo
140+
#' @importFrom tibble enframe
141+
#' @importFrom Matrix rowSums
142+
#'
143+
#' @name aggregate_cells
144+
#' @rdname aggregate_cells
145+
#'
146+
#' @param .data A tidySingleCellExperiment object
147+
#' @param .sample A vector of variables by which cells are aggregated
148+
#' @param slot The slot to which the function is applied
149+
#' @param assays The assay to which the function is applied
150+
#' @param aggregation_function The method of cell-feature value aggregation
151+
#'
152+
#' @return A SummarizedExperiment object
153+
#'
154+
#' @examples
155+
#' data("pbmc_small")
156+
#' pbmc_small_pseudo_bulk <- pbmc_small |>
157+
#' aggregate_cells(c(groups, ident), assays = "counts")
158+
#'
159+
#' @export
160+
aggregate_cells <- function(.data, .sample = NULL, slot = "data", assays = NULL, aggregation_function = rowSums) {
161+
162+
.sample = enquo(.sample)
163+
164+
# Subset only wanted assays
165+
if(!is.null(assays)){
166+
.data@assays@data = .data@assays@data[assays]
167+
}
168+
169+
.data %>%
170+
171+
nest(data = -!!.sample) %>%
172+
mutate(.aggregated_cells = as.integer(map(data, ~ ncol(.x)))) %>%
173+
mutate(data = map(data, ~
174+
175+
# loop over assays
176+
map2(
177+
as.list(assays(.x)), names(.x@assays),
178+
179+
# Get counts
180+
~ .x %>%
181+
aggregation_function(na.rm = T) %>%
182+
enframe(
183+
name = "feature",
184+
value = sprintf("%s", .y)
185+
) %>%
186+
mutate(feature = as.character(feature))
187+
) %>%
188+
Reduce(function(...) full_join(..., by=c("feature")), .)
189+
190+
)) %>%
191+
left_join(.data %>% as_tibble() %>% subset(!!.sample), by = quo_names(.sample)) %>%
192+
unnest(data) %>%
193+
194+
drop_class("tidySingleCellExperiment_nested") |>
195+
196+
as_SummarizedExperiment(.sample = !!.sample, .transcript = feature, .abundance = !!as.symbol(names(.data@assays)))
197+
}

0 commit comments

Comments
 (0)