@@ -62,29 +62,82 @@ bind_rows.SummarizedExperiment <- function(..., .id=NULL, add.cell.ids=NULL) {
6262bind_cols_internal <- function (... , .id = NULL , column_belonging = NULL ) {
6363 tts <- tts <- flatten_if(dots_values(... ), is_spliced )
6464
65- tts [[1 ]] | >
66- as_tibble(skip_GRanges = TRUE ) | >
67- dplyr :: bind_cols(tts [[2 ]], .id = .id ) %> %
68- when(
69-
70- # If the column added are not sample-wise
71- # or feature-wise, return tibble
72- (colnames(tts [[2 ]]) %in% c(
73- get_subset_columns(. , !! s_(tts [[1 ]])$ symbol ),
74- get_subset_columns(. , !! f_(tts [[1 ]])$ symbol )
75- )
76- ) | > all() ~ update_SE_from_tibble(. , tts [[1 ]],
77- column_belonging = column_belonging ),
7865
79- # Return tiblle
80- ~ {
66+ # If I have column corresponding bind directly
67+ # Without tranformation to tibble
68+ if (! is.null(column_belonging )){
69+
70+ # For colData
71+ colData_additions = column_belonging [column_belonging == s_(tts [[1 ]] )$ name ] | > names()
72+
73+ data_frame_to_attach =
74+ tts [[1 ]] | >
75+ select(!! s_(tts [[1 ]] )$ symbol ) | >
76+ suppressMessages() | >
77+ bind_cols(tts [[2 ]] | > select(all_of(colData_additions ))) | >
78+ distinct()
79+
80+ # Set row names
81+ data_frame_to_attach =
82+ data_frame_to_attach | >
83+ select(- 1 ) | >
84+ DataFrame(row.names = data_frame_to_attach | > pull(1 ))
85+
86+ # Reorder
87+ data_frame_to_attach = data_frame_to_attach [match(rownames(data_frame_to_attach ), colnames(tts [[1 ]])), , drop = FALSE ]
88+
89+ # Attach
90+ colData(tts [[1 ]]) = cbind(colData(tts [[1 ]]), data_frame_to_attach )
91+
92+ # For rowData
93+ rowData_additions = column_belonging [column_belonging == f_(tts [[1 ]] )$ name ] | > names()
94+
95+ data_frame_to_attach =
96+ tts [[1 ]] | >
97+ select(!! f_(tts [[1 ]] )$ symbol ) | >
98+ suppressMessages() | >
99+ bind_cols(tts [[2 ]] | > select(all_of(rowData_additions ))) | >
100+ distinct()
101+
102+ # Set row names
103+ data_frame_to_attach =
104+ data_frame_to_attach | >
105+ select(- 1 ) | >
106+ DataFrame(row.names = data_frame_to_attach | > pull(1 ))
107+
108+ # Reorder
109+ data_frame_to_attach = data_frame_to_attach [match(rownames(data_frame_to_attach ), rownames(tts [[1 ]])), , drop = FALSE ]
110+
111+ # Attach
112+ rowData(tts [[1 ]]) = cbind(rowData(tts [[1 ]]), data_frame_to_attach )
113+
114+ tts [[1 ]]
115+ }
116+
117+ # If I DON'T have column corresponding go through tibble
118+ else
119+ tts [[1 ]] | >
120+ as_tibble(skip_GRanges = TRUE ) | >
121+ dplyr :: bind_cols(tts [[2 ]], .id = .id ) %> %
122+ when(
123+
124+ # If the column added are not sample-wise or feature-wise return tibble
125+ (colnames(tts [[2 ]]) %in% c(
126+ get_subset_columns(. , !! s_(tts [[1 ]])$ symbol ),
127+ get_subset_columns(. , !! f_(tts [[1 ]])$ symbol )
128+ )
129+ ) | > all() ~ update_SE_from_tibble(. , tts [[1 ]], column_belonging = column_belonging ),
130+
131+ # Return tiblle
132+ ~ {
81133 warning(" tidySummarizedExperiment says:" ,
82- " The new columns do not include pure sample-wise" ,
83- " or feature-wise. A data frame is returned for" ,
84- " independent data analysis." )
134+ " The new columns do not include pure sample-wise" ,
135+ " or feature-wise. A data frame is returned for" ,
136+ " independent data analysis." )
85137 (. )
86- }
87- )
138+ }
139+ )
140+
88141}
89142
90143bind_cols_ <- function (... , .id = NULL ) { bind_cols_internal(... , .id = NULL ) }
@@ -630,19 +683,28 @@ select.SummarizedExperiment <- function(.data, ...) {
630683 data.frame (row.names = pull(. , !! f_(.data )$ symbol )) | >
631684 select(- !! f_(.data )$ symbol ) | >
632685 DataFrame()
633-
686+
687+ # If SE does not have rownames,
688+ # I have to take them our of here, otherwise count integration,
689+ # which is a matrix and behaved differently from DataFrame fails
690+ if (rownames(.data ) | > is.null()) rownames(row_data_DF ) = NULL
691+
634692 col_data_tibble <-
635693 colData(.data ) | >
636694 as_tibble(rownames = s_(.data )$ name )
637695
638696 col_data_DF <-
639697 col_data_tibble | >
640- select(one_of(columns_query ), !! s_(.data )$ symbol ) | >
641- suppressWarnings() %> %
642- data.frame (row.names = pull(. , !! s_(.data )$ symbol )) | >
698+ select(any_of(columns_query ), !! s_(.data )$ symbol ) | >
699+ data.frame (row.names = pull(col_data_tibble , !! s_(.data )$ symbol )) | >
643700 select(- !! s_(.data )$ symbol ) | >
644701 DataFrame()
645702
703+ # If SE does not have rownames,
704+ # I have to take them our of here, otherwise count integration,
705+ # which is a matrix and behaved differently from DataFrame fails
706+ if (colnames(.data ) | > is.null()) rownames(col_data_DF ) = NULL
707+
646708 count_data <-
647709 assays(.data )@ listData %> %
648710 . [names(assays(.data )@ listData ) %in% columns_query ]
@@ -862,3 +924,46 @@ pull.SummarizedExperiment <- function(.data, var=-1, name=NULL, ...) {
862924 as_tibble(skip_GRanges = skip_GRanges ) | >
863925 dplyr :: pull(var = !! var , name = !! name , ... )
864926}
927+
928+ # ' @name group_split
929+ # ' @rdname group_split
930+ # ' @inherit dplyr::group_split
931+ # '
932+ # ' @examples
933+ # ' data(pasilla, package = "tidySummarizedExperiment")
934+ # ' pasilla |> group_split(condition)
935+ # ' pasilla |> group_split(counts > 0)
936+ # ' pasilla |> group_split(condition, counts > 0)
937+ # '
938+ # ' @importFrom ellipsis check_dots_used
939+ # ' @importFrom dplyr group_split
940+ # ' @export
941+ group_split.SummarizedExperiment <- function (.tbl , ... , .keep = TRUE ) {
942+
943+ var_list <- enquos(... )
944+ data_nested <- NULL
945+
946+ nested <- .tbl | >
947+ mutate(!!! var_list ) | >
948+ nest(data_nested = - (substring(as.character(var_list ), 2 )))
949+
950+ if (.keep ) {
951+ grouped_data <- nested | >
952+ pull(data_nested )
953+
954+ grouping_cols <- nested | >
955+ select(substring(as.character(var_list ), 2 ))
956+
957+ for (i in 1 : length(grouped_data )) {
958+ grouped_data [[i ]] <- grouped_data [[i ]] | >
959+ mutate(grouping_cols [i ,])
960+ }
961+
962+ grouped_data
963+
964+ } else {
965+ nested | >
966+ pull(data_nested )
967+ }
968+
969+ }
0 commit comments