@@ -41,7 +41,6 @@ unnest.tidySummarizedExperiment_nested <- function(data, cols, ...,
4141unnest_summarized_experiment <- function (data , cols , ... ,
4242 keep_empty = FALSE , ptype = NULL , names_sep = NULL ,
4343 names_repair = " check_unique" , .drop , .id , .sep , .preserve ) {
44-
4544 . <- NULL
4645
4746 # Need this otherwise crashes map
@@ -71,6 +70,7 @@ unnest_summarized_experiment <- function(data, cols, ...,
7170 )
7271 }
7372
73+
7474 # If both nested by transcript and sample
7575 if (s_(se )$ name %in% colnames(data ) &
7676 f_(se )$ name %in% colnames(data ) ) {
@@ -88,11 +88,11 @@ unnest_summarized_experiment <- function(data, cols, ...,
8888
8989
9090 # Mark if columns belong to feature or sample
91- my_unnested_tibble <-
92- mutate( data , !! cols : = map( !! cols , ~ as_tibble( .x ))) % > %
93- select( - suppressWarnings(one_of(s_( my_se ) $ name ,
94- f_(my_se )$ name ))) % > %
95- unnest(!! cols )
91+ my_unnested_tibble =
92+ data | >
93+ mutate( !! cols : = map( !! cols , ~ as_tibble( .x ))) | >
94+ select( - any_of(c(s_( my_se ) $ name , f_(my_se )$ name ))) | >
95+ unnest(!! cols )
9696
9797 # Get which column is relative to feature or sample
9898 sample_columns <- my_unnested_tibble %> %
@@ -130,14 +130,14 @@ unnest_summarized_experiment <- function(data, cols, ...,
130130
131131 # Attach back the columns used for nesting
132132 .data_ %> %
133- select(- !! cols , - any_of(c(s_(my_se )$ name , f_(my_se )$ name ))) %> %
133+ select(- !! cols , - any_of(c(s_(my_se )$ name , f_(my_se )$ name ))) %> %
134134 slice(rep(as.integer(.y ), ncol(.x ) * nrow(.x ))),
135135
136136 # Column sample-wise or feature-wise
137137 column_belonging =
138138 source_column [
139139 .data_ %> %
140- select(- !! cols , - any_of(c(s_(my_se )$ name , f_(my_se )$ name ))) %> %
140+ select(- !! cols , - any_of(c(s_(my_se )$ name , f_(my_se )$ name ))) %> %
141141 colnames()
142142 ]
143143 )
@@ -219,6 +219,14 @@ nest.SummarizedExperiment <- function(.data, ..., .names_sep=NULL) {
219219 .data <- ping_old_special_column_into_metadata(.data )
220220 }
221221
222+ # if the data does not have raw, names or clumn names
223+ # we have to add them, otherwise the nesting and a nesting will get confused with
224+ # the link between Sample wise, columns, and Sample IDs
225+ if (rownames(.data ) | > is.null() | colnames(.data ) | > is.null() )
226+ warning(" tidySummarizedExperiment says: the nesting and unnesting operations require row names and column names to avoid side-effects. Therefore, doors will be added as \" 1\" , \" 2\" , \" 3\" ." )
227+ if (rownames(.data ) | > is.null() ) rownames(.data ) = .data | > nrow() | > seq_len() | > as.character()
228+ if (colnames(.data ) | > is.null() ) colnames(.data ) = .data | > ncol() | > seq_len() | > as.character()
229+
222230 my_data__ <- .data
223231
224232 # Names
@@ -256,12 +264,6 @@ nest.SummarizedExperiment <- function(.data, ..., .names_sep=NULL) {
256264 " We are working for optimising a generalised solution of nest()." )
257265 }
258266
259- # my_data__nested <-
260- # my_data__ %>%
261- # # This is needed otherwise nest goes into loop and fails
262- # as_tibble() %>%
263- # tidyr::nest(...)
264-
265267 # If I nest only for .feature -> THIS WORKS ONLY WITH THE CHECK ABOVE
266268 if (feature_name %in% colnames(my_test_nest )) {
267269 return (
@@ -280,7 +282,7 @@ nest.SummarizedExperiment <- function(.data, ..., .names_sep=NULL) {
280282
281283
282284 my_data__ %> %
283- select(!! sample_symbol , !! feature_symbol , my_nesting_column ) | >
285+ select(!! sample_symbol , !! feature_symbol , all_of( my_nesting_column ) ) | >
284286 as_tibble() %> %
285287 tidyr :: nest(... ) | >
286288
@@ -311,15 +313,18 @@ nest.SummarizedExperiment <- function(.data, ..., .names_sep=NULL) {
311313 # Here I am filtering because if I have 0 features this leads to failure
312314 else my_transcripts = ..1 | > filter(! is.na(!! feature_symbol )) | > pull(!! feature_symbol )
313315
314- # ##
315-
316+ # if the summarised experiment does not have feature ID or Sample ID,
317+ # convert back those to integers
318+ # Note to self: this is convoluted, because if feature ID and sample ID
319+ # were to stay integers all along, these will not be needed
320+ if (rownames(my_data__ ) | > is.null()) my_transcripts = as.integer(my_transcripts )
321+ if (colnames(my_data__ ) | > is.null()) my_samples = as.integer(my_samples )
322+
316323 my_data__ [unique(my_transcripts ),unique(my_samples )] | >
317- select(- one_of (
324+ select(- any_of (
318325 my_nesting_column | >
319326 setdiff(c(sample_name , feature_name ))
320- )) | >
321- suppressWarnings()
322-
327+ ))
323328
324329 }
325330 )
0 commit comments