Skip to content

Commit c2f5e31

Browse files
authored
Merge pull request #82 from stemangiola/generalise-to-0-dimensions
Generalise to 0 dimensions
2 parents 2f3b271 + af3c26f commit c2f5e31

File tree

5 files changed

+944
-872
lines changed

5 files changed

+944
-872
lines changed

R/print_method.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ print.SummarizedExperiment <- function(x, ..., n=NULL,
6868
# Fix NOTEs
6969
. <- NULL
7070

71+
7172
# Stop if any column or row names are duplicated
7273
if (check_if_any_dimnames_duplicated(x, dim = "cols")) {
7374
stop("tidySummarizedExperiment says: some column names are duplicated")
7475
}
7576
if (check_if_any_dimnames_duplicated(x, dim = "rows")) {
7677
stop("tidySummarizedExperiment says: some row names are duplicated")
7778
}
79+
7880
# Stop if column names of assays do not overlap
7981
if (check_if_assays_are_NOT_overlapped(x, dim = "cols")) {
8082
stop(
@@ -96,11 +98,12 @@ but they do not completely overlap."
9698
my_tibble <-
9799
x |>
98100

99-
# If I have more than 30 genes select first sample
100-
when(
101-
nrow(.) > 30 ~.[1:min(50, nrow(x)), min(1, ncol(x)), drop=FALSE],
102-
~ .[, 1:min(20, ncol(x)), drop=FALSE]
103-
) %>%
101+
# If I have more than 30 genes select first sample
102+
when(
103+
nrow(.) > 30 ~.[1:min(50, nrow(x)), min(1, ncol(x)), drop=FALSE] ,
104+
ncol(.) == 0 ~ .,
105+
~ .[, 1:min(20, ncol(x)), drop=FALSE]
106+
) %>%
104107

105108
as_tibble()
106109

R/tidyr_methods.R

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ nest.SummarizedExperiment <- function(.data, ..., .names_sep=NULL) {
225225
# Check if the nesting is too complicated
226226
# for the moment without optimisation
227227
my_test_nest <-
228-
my_data__[1,1] %>%
228+
my_data__[min(1, nrow(my_data__)),min(1, ncol(my_data__))] %>%
229229
as_tibble() %>%
230230
tidyr::nest(...)
231231

@@ -279,47 +279,44 @@ nest.SummarizedExperiment <- function(.data, ..., .names_sep=NULL) {
279279
as_tibble() %>%
280280
tidyr::nest(...) |>
281281

282-
mutate(
282+
mutate(
283283
!!as.symbol(col_name_data) := pmap(
284284

285-
# Add sample feature to map if nesting by those
285+
# Add sample feature to map if nesting by those
286286
list(!!as.symbol(col_name_data)) %>%
287+
288+
# Check if nested by sample
289+
when(sample_name %in% colnames(my_test_nest) ~ c(., list(!!sample_symbol)), ~ (.)) %>%
290+
291+
# Check if nested by feature
292+
when(feature_name %in% colnames(my_test_nest) ~ c(., list(!!feature_symbol)), ~ (.)) , ~ {
293+
294+
# VERY COMPLICATE WAY TO DO THIS. SIMPLIFY IN THE FUTURE
295+
287296
# Check if nested by sample
288-
when(sample_name %in% colnames(my_test_nest) ~
289-
c(., list(!!sample_symbol)), ~ (.)) %>%
297+
if(sample_name %in% colnames(my_test_nest)) { my_samples=..2 }
298+
299+
# Here I am filtering because if I have 0 samples this leads to failure
300+
else my_samples= ..1 |> filter(!is.na(!!sample_symbol)) |> pull(!!sample_symbol)
290301

291-
# Check if nested by feature
292-
when(feature_name %in% colnames(my_test_nest) ~
293-
c(., list(!!feature_symbol)), ~ (.)), ~ {
302+
# Check if nested by feature and sample
303+
if(sample_name %in% colnames(my_test_nest) & feature_name %in% colnames(my_test_nest)) {my_transcripts=..3}
304+
else if(feature_name %in% colnames(my_test_nest)) my_transcripts=..2
294305

295-
# VERY COMPLICATE WAY TO DO THIS.
296-
# SIMPLIFY IN THE FUTURE
306+
# Here I am filtering because if I have 0 features this leads to failure
307+
else my_transcripts= ..1 |> filter(!is.na(!!feature_symbol)) |> pull(!!feature_symbol)
297308

298-
# Check if nested by sample
299-
if (sample_name %in% colnames(my_test_nest)) {
300-
my_samples=..2
301-
} else {
302-
my_samples=pull(..1,!!sample_symbol)
303-
}
304-
305-
# Check if nested by feature and sample
306-
if (sample_name %in% colnames(my_test_nest) &
307-
feature_name %in% colnames(my_test_nest)) {
308-
my_transcripts=..3
309-
} else if (feature_name %in% colnames(my_test_nest)) {
310-
my_transcripts=..2
311-
} else {
312-
my_transcripts=pull(..1,!!feature_symbol)
313-
}
314-
###
315-
316-
my_data__[unique(my_transcripts), unique(my_samples)] |>
317-
select(-one_of(
318-
my_nesting_column |>
319-
setdiff(c(sample_name, feature_name))
320-
)) |>
321-
suppressWarnings()
322-
}
309+
###
310+
311+
my_data__[unique(my_transcripts),unique(my_samples)] |>
312+
select(-one_of(
313+
my_nesting_column |>
314+
setdiff(c(sample_name, feature_name))
315+
)) |>
316+
suppressWarnings()
317+
318+
319+
}
323320
)
324321
) %>%
325322
# Coerce to tidySummarizedExperiment_nested for unnesting

0 commit comments

Comments
 (0)