@@ -39,7 +39,7 @@ setMethod("join_features", "SingleCellExperiment", function(.data,
3939 # CRAN Note
4040 .cell <- NULL
4141 .feature <- NULL
42-
42+
4343 # Shape is long
4444 if (shape == " long" ) {
4545 .data %> %
@@ -50,11 +50,11 @@ setMethod("join_features", "SingleCellExperiment", function(.data,
5050 features = features ,
5151 all = all ,
5252 exclude_zeros = exclude_zeros )) %> %
53- select(!! c_(.data )$ symbol , .feature ,
53+ select(!! c_(.data )$ symbol , .feature ,
5454 contains(" .abundance" ), everything())
5555 # Shape if wide
5656 } else {
57- .data %> %
57+ .data %> %
5858 left_join(
5959 by = c_(.data )$ name ,
6060 get_abundance_sc_wide(
@@ -84,7 +84,7 @@ tidy <- function(object) {
8484# ' @importFrom lifecycle deprecate_warn
8585# ' @export
8686tidy.SingleCellExperiment <- function (object ) {
87-
87+
8888 # DEPRECATE
8989 deprecate_warn(
9090 when = " 1.1.1" ,
@@ -98,8 +98,8 @@ tidy.SingleCellExperiment <- function(object) {
9898# ' @rdname aggregate_cells
9999# ' @inherit ttservice::aggregate_cells
100100# ' @aliases aggregate_cells,SingleCellExperiment-method
101- # '
102- # ' @examples
101+ # '
102+ # ' @examples
103103# ' data(pbmc_small)
104104# ' pbmc_small_pseudo_bulk <- pbmc_small |>
105105# ' aggregate_cells(c(groups, ident), assays="counts")
@@ -110,48 +110,88 @@ tidy.SingleCellExperiment <- function(object) {
110110# ' @importFrom Matrix rowSums
111111# ' @importFrom ttservice aggregate_cells
112112# ' @importFrom SummarizedExperiment assays assays<- assayNames
113+ # ' @importFrom S4Vectors split
114+ # ' @importFrom stringr str_remove
115+ # ' @importFrom dplyr group_split
116+ # '
117+ # '
113118# ' @export
114119setMethod ("aggregate_cells ", "SingleCellExperiment", function(.data,
115- .sample = NULL , slot = " data" , assays = NULL ,
120+ .sample = NULL , slot = " data" , assays = NULL ,
116121 aggregation_function = Matrix :: rowSums ,
117122 ... ) {
118-
123+
119124 # Fix NOTEs
120125 feature <- NULL
121126 .sample <- enquo(.sample )
122-
127+
123128 # Subset only wanted assays
124129 if (! is.null(assays )) {
125130 assays(.data ) <- assays(.data )[assays ]
126131 }
127-
128- .data %> %
129- nest(data = - !! .sample ) %> %
130- mutate(.aggregated_cells = as.integer(map(data , ~ ncol(.x )))) %> %
131- mutate(
132- data = map(data , ~ {
133- # Loop over assays
134- map2(as.list(assays(.x )), assayNames(.x ), ~ {
135- # Get counts
136- .x %> %
137- aggregation_function(na.rm = TRUE ) %> %
138- enframe(
139- name = " feature" ,
140- value = sprintf(" %s" , .y )) %> %
141- mutate(feature = as.character(feature ))
142- }) %> %
143- Reduce(function (... ) full_join(... , by = " feature" ), . )
144- })
145- ) %> %
132+
133+
134+ grouping_factor =
135+ .data | >
136+ colData() | >
137+ as_tibble() | >
138+ select(!! .sample ) | >
139+ suppressMessages() | >
140+ unite(" my_id_to_split_by___" , !! .sample , sep = " ___" ) | >
141+ pull(my_id_to_split_by___ ) | >
142+ as.factor()
143+
144+ list_count_cells = table(grouping_factor ) | > as.list()
145+
146+ # New method
147+ list_assays =
148+ .data | >
149+ assays() | >
150+ as.list() | >
151+ map(~ .x | > splitColData(grouping_factor )) | >
152+ unlist(recursive = FALSE )
153+
154+ list_assays =
155+ list_assays | >
156+ map2(names(list_assays ), ~ {
157+ # Get counts
158+ .x %> %
159+ aggregation_function(na.rm = TRUE ) %> %
160+ enframe(
161+ name = " .feature" ,
162+ value = " x" ) %> % # sprintf("%s", .y)) %>%
163+
164+ # In case we don't have rownames
165+ mutate(.feature = as.character(.feature ))
166+ }) | >
167+ enframe(name = " .sample" ) | >
168+
169+ # Clean groups
170+ mutate(assay_name = assayNames(!! .data ) | > rep(each = length(levels(grouping_factor )))) | >
171+ mutate(.sample = .sample | > str_remove(assay_name ) | > str_remove(" \\ ." )) | >
172+ group_split(.sample ) | >
173+ map(~ .x | > unnest(value ) | > pivot_wider(names_from = assay_name , values_from = x ) ) | >
174+
175+ # Add cell count
176+ map2(
177+ list_count_cells ,
178+ ~ .x | > mutate(.aggregated_cells = .y )
179+ )
180+
181+
182+ do.call(rbind , list_assays ) | >
183+
146184 left_join(
147- .data %> %
148- as_tibble() %> %
149- subset(!! .sample ),
150- by = quo_names(.sample )) %> %
151- unnest(data ) %> %
152- drop_class(" tidySingleCellExperiment_nested" ) %> %
185+ .data | >
186+ colData() | >
187+ as_tibble() | >
188+ subset(!! .sample ) | >
189+ unite(" my_id_to_split_by___" , !! .sample , remove = FALSE , sep = " ___" ),
190+ by = join_by(" .sample" == " my_id_to_split_by___" )
191+ ) | >
192+
153193 as_SummarizedExperiment(
154- .sample = !! .sample ,
155- .transcript = feature ,
194+ .sample = .sample ,
195+ .transcript = . feature ,
156196 .abundance = !! as.symbol(names(.data @ assays )))
157197})
0 commit comments