Skip to content

Commit 4625ba2

Browse files
committed
fix bugs
1 parent 1e08dca commit 4625ba2

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

R/dplyr_methods.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bind_rows.SummarizedExperiment <- function(..., .id=NULL, add.cell.ids=NULL) {
6262
bind_cols_internal <- function(..., .id=NULL, column_belonging=NULL) {
6363
tts <- tts <- flatten_if(dots_values(...), is_spliced)
6464

65-
<<<<<<< HEAD
65+
6666
# If I have column corresponding bind directly
6767
# Without tranformation to tibble
6868
if(!is.null(column_belonging)){
@@ -683,19 +683,28 @@ select.SummarizedExperiment <- function(.data, ...) {
683683
data.frame(row.names=pull(., !!f_(.data)$symbol)) |>
684684
select(-!!f_(.data)$symbol) |>
685685
DataFrame()
686-
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+
687692
col_data_tibble <-
688693
colData(.data) |>
689694
as_tibble(rownames = s_(.data)$name)
690695

691696
col_data_DF <-
692697
col_data_tibble |>
693-
select(one_of(columns_query), !!s_(.data)$symbol) |>
694-
suppressWarnings() %>%
695-
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)) |>
696700
select(-!!s_(.data)$symbol) |>
697701
DataFrame()
698702

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+
699708
count_data <-
700709
assays(.data)@listData %>%
701710
.[names(assays(.data)@listData) %in% columns_query]

R/tidyr_methods.R

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)

R/utilities.R

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,8 @@ get_count_datasets <- function(se) {
749749
se <- check_se_dimnames(se)
750750

751751
# Join assays
752-
map2(
752+
list_assays =
753+
map2(
753754
assays(se, withDimnames = FALSE) %>% as.list(),
754755
names(assays(se)),
755756
~ {
@@ -807,21 +808,27 @@ get_count_datasets <- function(se) {
807808
f_(se)$name %in% colnames(.x) %>% not ~ mutate(.x, !!f_(se)$symbol := as.character(NA)),
808809
s_(se)$name %in% colnames(.x) %>% not ~ mutate(.x, !!s_(se)$symbol := as.character(NA)),
809810
~ .x
810-
)) |>
811-
812-
when(
813-
length(.)>0 ~
814-
815-
reduce(., full_join, by = c(f_(se)$name, s_(se)$name)),
816-
# reduce(., left_join, by = c(f_(se)$name, s_(se)$name)),
817-
# bind_cols(., .name_repair = c("minimal")) %>% .[!duplicated(colnames(.))],
818-
~ expand.grid(
819-
rownames(se), colnames(se)
820-
) %>%
821-
setNames(c(f_(se)$name, s_(se)$name)) %>%
822-
tibble::as_tibble()
823-
)
811+
))
812+
813+
# If assays is non empty
814+
if(list_assays |> length() > 0)
815+
list_assays |>
816+
reduce(full_join, by = c(f_(se)$name, s_(se)$name))
824817

818+
# If assays is empty
819+
else {
820+
821+
# If I don't have row column names
822+
if(se |> rownames() |> is.null()) rn = nrow(se) |> seq_len() |> as.character()
823+
else rn = rownames(se)
824+
if(se |> colnames() |> is.null()) cn = ncol(se) |> seq_len() |> as.character()
825+
else cn = colnames(se)
826+
827+
expand.grid( rn, cn ) |>
828+
setNames(c(f_(se)$name, s_(se)$name)) |>
829+
tibble::as_tibble()
830+
}
831+
825832

826833
}
827834

0 commit comments

Comments
 (0)