Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unique special columns #52

Merged
merged 3 commits into from
Aug 31, 2022
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Biarch: true
biocViews: AssayDomain, Infrastructure, RNASeq, DifferentialExpression, GeneExpression, Normalization, Clustering, QualityControl, Sequencing
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.0
RoxygenNote: 7.2.1
Roxygen: list(markdown = TRUE)
URL: https://github.com/stemangiola/tidySingleCellExperiment
BugReports: https://github.com/stemangiola/tidySingleCellExperiment/issues
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ importFrom(rlang,quo_squash)
importFrom(stringr,regex)
importFrom(stringr,str_detect)
importFrom(stringr,str_replace)
importFrom(stringr,str_replace_all)
importFrom(tibble,as_tibble)
importFrom(tibble,enframe)
importFrom(tibble,glimpse)
Expand Down
16 changes: 2 additions & 14 deletions R/tibble_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#' as_tibble()
NULL


#' @export
#' @importFrom purrr reduce
#' @importFrom purrr map
Expand All @@ -82,20 +83,7 @@ as_tibble.SingleCellExperiment <- function(x, ...,

# Only if I have reduced dimensions and special datasets
ncol(x@int_colData@listData$reducedDims) > 0 ~ (.) %>% dplyr::bind_cols(
get_special_datasets(x, ...) %>%
map(~ .x %>% when(

# If row == 1 do a trick
dim(.) %>% is.null() ~ {
(.) %>%
tibble::enframe() %>%
spread(name, value)
},

# Otherwise continue normally
~ as_tibble(.)
)) %>%
reduce(dplyr::bind_cols)
special_datasets_to_tibble(x, ...)
),

# Otherwise skip
Expand Down
43 changes: 42 additions & 1 deletion R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,19 @@ as_meta_data <- function(.data, SingleCellExperiment_object) {
# Solve CRAN warnings
. <- NULL

col_to_exclude <- get_special_columns(SingleCellExperiment_object)
col_to_exclude <-

# special_datasets_to_tibble(SingleCellExperiment_object) |>
# colnames()
get_special_columns(SingleCellExperiment_object) |>


# I need this in case we have multiple reduced dimension data frames with overlapping names of the columns.
# For example multiple PCA versions
vctrs::vec_as_names(repair = "unique") |>

# To avoid name change by the bind_cols of as_tibble
trick_to_avoid_renaming_of_already_unique_columns_by_dplyr()

.data_df =
.data %>%
Expand Down Expand Up @@ -405,3 +417,32 @@ add_attr = function(var, attribute, name) {
attr(var, name) <- attribute
var
}

special_datasets_to_tibble = function(.singleCellExperiment, ...){
x =
.singleCellExperiment |>
get_special_datasets(...) %>%
map(~ .x %>% when(

# If row == 1 do a trick
dim(.) %>% is.null() ~ {
(.) %>%
tibble::enframe() %>%
spread(name, value)
},

# Otherwise continue normally
~ as_tibble(.)
)) %>%
reduce(dplyr::bind_cols)

# To avoid name change by the bind_cols of as_tibble
colnames(x) = colnames(x) |> trick_to_avoid_renaming_of_already_unique_columns_by_dplyr()

x
}

#' @importFrom stringr str_replace_all
trick_to_avoid_renaming_of_already_unique_columns_by_dplyr = function(x){
x |> str_replace_all("\\.\\.\\.", "___")
}
16 changes: 16 additions & 0 deletions tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ test_that("join_features",{


})


test_that("duplicated PCA matrices",{

pbmc_small@int_colData@listData$reducedDims$PCA2 = pbmc_small@int_colData@listData$reducedDims$PCA

pbmc_small %>%
mutate(aa = 1) |>
as_tibble() |>
ncol() |>
expect_equal(
(pbmc_small |> as_tibble() |> ncol()) + 1
)


})