Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,56 @@ setGeneric("as_SummarizedExperiment", function(.data,
feature_cols = col_direction$vertical_cols
counts_cols = col_direction$counts_cols

colData = .data %>% select(!!.sample, sample_cols) %>% distinct %>% arrange(!!.sample) %>% {
S4Vectors::DataFrame((.) %>% select(-!!.sample),
row.names = (.) %>% pull(!!.sample))
colData =
.data %>%
select(!!.sample, sample_cols) %>%
distinct() %>%

# Unite if multiple sample columns
tidyr::unite(!!sample__$name, !!.sample, remove = FALSE, sep = "___") |>

arrange(!!sample__$symbol) %>% {
S4Vectors::DataFrame(
(.) %>% select(-!!sample__$symbol),
row.names = (.) %>% pull(!!sample__$symbol)
)
}

rowData = .data %>% select(!!.transcript, feature_cols) %>% distinct %>% arrange(!!.transcript) %>% {
S4Vectors::DataFrame((.) %>% select(-!!.transcript),
row.names = (.) %>% pull(!!.transcript))
rowData =
.data %>%
select(!!.transcript, feature_cols) %>%
distinct() %>%

# Unite if multiple sample columns
tidyr::unite(!!feature__$name, !!.transcript, remove = FALSE, sep = "___") |>

arrange(!!feature__$symbol) %>% {
S4Vectors::DataFrame(
(.) %>% select(-!!feature__$symbol),
row.names = (.) %>% pull(!!feature__$symbol)
)
}

my_assays =
.data %>%
select(!!.sample,
!!.transcript,
!!.abundance,
!!.abundance_scaled,
counts_cols) %>%
distinct() %>%
pivot_longer( cols=-c(!!.transcript,!!.sample), names_to="assay", values_to= ".a") %>%

# Unite if multiple sample columns
tidyr::unite(!!sample__$name, !!.sample, remove = FALSE, sep = "___") |>

# Unite if multiple sample columns
tidyr::unite(!!feature__$name, !!.transcript, remove = FALSE, sep = "___") |>

select(!!sample__$symbol,
!!feature__$symbol,
!!.abundance,
!!.abundance_scaled,
counts_cols) %>%
distinct() %>%

pivot_longer( cols=-c(!!feature__$symbol,!!sample__$symbol), names_to="assay", values_to= ".a") %>%
nest(`data` = -`assay`) %>%
mutate(`data` = `data` %>% map(
~ .x %>% spread(!!.sample, .a) %>% as_matrix(rownames = quo_name(.transcript))
~ .x %>% spread(!!sample__$symbol, .a) %>% as_matrix(rownames = feature__$name)
))

# Build the object
Expand Down
25 changes: 14 additions & 11 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,17 @@ add_class = function(var, name) {
#' @return A list of column enquo or error
get_sample_transcript_counts = function(.data, .sample, .transcript, .abundance){

if( .sample %>% quo_is_symbol() ) .sample = .sample
if( quo_is_symbolic(.sample) ) .sample = .sample
else if(".sample" %in% (.data %>% get_tt_columns() %>% names))
.sample = get_tt_columns(.data)$.sample
else my_stop()

if( .transcript %>% quo_is_symbol() ) .transcript = .transcript
if( quo_is_symbolic(.transcript) ) .transcript = .transcript
else if(".transcript" %in% (.data %>% get_tt_columns() %>% names))
.transcript = get_tt_columns(.data)$.transcript
else my_stop()

if( .abundance %>% quo_is_symbolic() ) .abundance = .abundance
if( quo_is_symbolic(.abundance) ) .abundance = .abundance
else if(".abundance" %in% (.data %>% get_tt_columns() %>% names))
.abundance = get_tt_columns(.data)$.abundance
else my_stop()
Expand Down Expand Up @@ -894,8 +894,8 @@ get_x_y_annotation_columns = function(.data, .horizontal, .vertical, .abundance,
.abundance_scaled = enquo(.abundance_scaled)

# x-annotation df
n_x = .data %>% distinct(!!.horizontal) %>% nrow
n_y = .data %>% distinct(!!.vertical) %>% nrow
n_x = .data %>% select(!!.horizontal) |> distinct() |> nrow()
n_y = .data %>% select(!!.vertical) |> distinct() |> nrow()

# Sample wise columns
horizontal_cols=
Expand All @@ -907,8 +907,9 @@ get_x_y_annotation_columns = function(.data, .horizontal, .vertical, .abundance,
.x %>%
when(
.data %>%
distinct(!!.horizontal, !!as.symbol(.x)) %>%
nrow %>%
select(!!.horizontal, !!as.symbol(.x)) %>%
distinct() |>
nrow() %>%
equals(n_x) ~ .x,
~ NULL
)
Expand All @@ -928,8 +929,9 @@ get_x_y_annotation_columns = function(.data, .horizontal, .vertical, .abundance,
.x %>%
ifelse_pipe(
.data %>%
distinct(!!.vertical, !!as.symbol(.x)) %>%
nrow %>%
select(!!.vertical, !!as.symbol(.x)) |>
distinct() |>
nrow() %>%
equals(n_y),
~ .x,
~ NULL
Expand Down Expand Up @@ -963,8 +965,9 @@ get_x_y_annotation_columns = function(.data, .horizontal, .vertical, .abundance,
.x %>%
ifelse_pipe(
.data %>%
distinct(!!.vertical, !!.horizontal, !!as.symbol(.x)) %>%
nrow %>%
select(!!.vertical, !!.horizontal, !!as.symbol(.x)) %>%
distinct() |>
nrow() %>%
equals(n_x * n_y),
~ .x,
~ NULL
Expand Down
21 changes: 12 additions & 9 deletions tests/testthat/test-bulk_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,17 @@ test_that("gene over representation",{

})

test_that("as_SummarizedExperiment",{
input_df |>
as_SummarizedExperiment(
.sample = c(a, condition),
.transcript = c(b, entrez),
.abundance = c
) |>
nrow() |>
expect_equal(527)

})

# test_that("bibliography",{
#
Expand All @@ -1954,12 +1965,4 @@ test_that("gene over representation",{
#
# })
#
# test_that("as_SummarizedExperiment",{
# input_df |>
# as_SummarizedExperiment(
# .sample = a,
# .transcript = b,
# .abundance = c
# )
#
# })