From 425911936c2a78008bf39439b77610df20be8ccf Mon Sep 17 00:00:00 2001 From: stemangiola Date: Tue, 12 Oct 2021 16:43:22 +1100 Subject: [PATCH 01/17] fix filter for objects with missing cell names --- NAMESPACE | 1 + R/dplyr_methods.R | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 249bbf8..56f3a05 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -77,6 +77,7 @@ export(summarise) export(tidy) export(unite) export(unnest) +export(unnest_single_cell_experiment) importFrom(S4Vectors,DataFrame) importFrom(SingleCellExperiment,cbind) importFrom(SingleCellExperiment,counts) diff --git a/R/dplyr_methods.R b/R/dplyr_methods.R index ac49cc5..eca5238 100755 --- a/R/dplyr_methods.R +++ b/R/dplyr_methods.R @@ -297,10 +297,17 @@ filter.SingleCellExperiment <- function(.data, ..., .preserve=FALSE) { new_meta <- .data %>% as_tibble() %>% dplyr::filter(..., .preserve=.preserve) # %>% as_meta_data(.data) - new_obj <- .data[, new_meta$cell] - # colData(new_obj)=new_meta - new_obj + # Try to solve missing colnames + if(colnames(.data) %>% is.null()){ + message("tidySingleCellExperiment says: the input object does not have cell names (colnames(...)). \n Therefore, the cell column in the filtered tibble abstraction will still include an incremental integer vector.") + new_meta$cell = as.integer(new_meta$cell) + + } + + + .data[, new_meta$cell] + } From 465c1813fa3c01af7928df866c1e6093eb121e99 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Sat, 23 Oct 2021 19:35:20 +1100 Subject: [PATCH 02/17] use ttservice --- DESCRIPTION | 1 + NAMESPACE | 57 +++++++++++----------- R/methods.R | 113 ++++++++++++++++++++----------------------- man/join_features.Rd | 26 +++++----- 4 files changed, 95 insertions(+), 102 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c26f942..7024be9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Description: tidySingleCellExperiment is an adapter that abstracts the 'SingleCe License: GPL-3 Depends: R (>= 4.0.0), + ttservice, SingleCellExperiment Imports: SummarizedExperiment, diff --git a/NAMESPACE b/NAMESPACE index 56f3a05..20ba739 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,81 +2,79 @@ S3method(add_count,SingleCellExperiment) S3method(add_count,default) -S3method(arrange,SingleCellExperiment) -S3method(as_tibble,SingleCellExperiment) S3method(bind_cols,SingleCellExperiment) S3method(bind_cols,default) S3method(bind_rows,SingleCellExperiment) S3method(bind_rows,default) S3method(count,SingleCellExperiment) S3method(count,default) -S3method(distinct,SingleCellExperiment) -S3method(extract,SingleCellExperiment) -S3method(filter,SingleCellExperiment) -S3method(full_join,SingleCellExperiment) -S3method(ggplot,SingleCellExperiment) -S3method(glimpse,tidySingleCellExperiment) -S3method(group_by,SingleCellExperiment) -S3method(inner_join,SingleCellExperiment) -S3method(join_features,SingleCellExperiment) -S3method(join_features,default) S3method(join_transcripts,Seurat) S3method(join_transcripts,default) -S3method(left_join,SingleCellExperiment) -S3method(mutate,SingleCellExperiment) -S3method(nest,SingleCellExperiment) -S3method(pivot_longer,SingleCellExperiment) S3method(plot_ly,SingleCellExperiment) S3method(plot_ly,default) S3method(print,SingleCellExperiment) -S3method(pull,SingleCellExperiment) -S3method(rename,SingleCellExperiment) -S3method(right_join,SingleCellExperiment) -S3method(rowwise,SingleCellExperiment) -S3method(sample_frac,SingleCellExperiment) -S3method(sample_n,SingleCellExperiment) -S3method(select,SingleCellExperiment) -S3method(separate,SingleCellExperiment) -S3method(slice,SingleCellExperiment) -S3method(summarise,SingleCellExperiment) S3method(tidy,SingleCellExperiment) -S3method(unite,SingleCellExperiment) -S3method(unnest,tidySingleCellExperiment_nested) export("%>%") export(add_count) export(arrange) +export(arrange.SingleCellExperiment) export(as_tibble) +export(as_tibble.SingleCellExperiment) export(bind_cols) export(bind_rows) export(count) export(distinct) +export(distinct.SingleCellExperiment) export(extract) +export(extract.SingleCellExperiment) export(filter) +export(filter.SingleCellExperiment) export(full_join) +export(full_join.SingleCellExperiment) export(ggplot) +export(ggplot.SingleCellExperiment) export(glimpse) +export(glimpse.tidySingleCellExperiment) export(group_by) +export(group_by.SingleCellExperiment) export(inner_join) +export(inner_join.SingleCellExperiment) export(join_features) export(join_transcripts) export(left_join) +export(left_join.SingleCellExperiment) export(mutate) +export(mutate.SingleCellExperiment) export(nest) +export(nest.SingleCellExperiment) export(pivot_longer) +export(pivot_longer.SingleCellExperiment) export(plot_ly) export(pull) +export(pull.SingleCellExperiment) export(rename) +export(rename.SingleCellExperiment) export(right_join) +export(right_join.SingleCellExperiment) export(rowwise) +export(rowwise.SingleCellExperiment) export(sample_frac) +export(sample_frac.SingleCellExperiment) export(sample_n) +export(sample_n.SingleCellExperiment) export(select) +export(select.SingleCellExperiment) export(separate) +export(separate.SingleCellExperiment) export(slice) +export(slice.SingleCellExperiment) export(summarise) +export(summarise.SingleCellExperiment) export(tidy) export(unite) +export(unite.SingleCellExperiment) export(unnest) +export(unnest.tidySingleCellExperiment_nested) export(unnest_single_cell_experiment) importFrom(S4Vectors,DataFrame) importFrom(SingleCellExperiment,cbind) @@ -147,7 +145,6 @@ importFrom(tidyr,separate) importFrom(tidyr,spread) importFrom(tidyr,unite) importFrom(tidyr,unnest) -importFrom(tidyselect,contains) importFrom(tidyselect,eval_select) -importFrom(tidyselect,everything) +importFrom(ttservice,join_features) importFrom(utils,tail) diff --git a/R/methods.R b/R/methods.R index b1d35c1..c0ecfb8 100755 --- a/R/methods.R +++ b/R/methods.R @@ -52,89 +52,82 @@ setMethod( } ) -#' Add differential featureion information to a tbl using edgeR. +#' Extract and join information for features. #' -#' \lifecycle{experimental} #' -#' @description join_features() extracts and joins information for specific -#' features +#' @description join_features() extracts and joins information for specified features #' #' @importFrom rlang enquo #' @importFrom magrittr "%>%" +#' @importFrom ttservice join_features #' #' @name join_features #' @rdname join_features #' -#' @param .data A tidy SingleCellExperiment object +#' @param .data A SingleCellExperiment object #' @param features A vector of feature identifiers to join #' @param all If TRUE return all #' @param exclude_zeros If TRUE exclude zero values #' @param shape Format of the returned table "long" or "wide" #' @param ... Parameters to pass to join wide, i.e. assay name to extract feature abundance from and gene prefix, for shape="wide" #' -#' @details This function extracts information for specified features and -#' returns the information in either long or wide format. +#' @details This function extracts information for specified features and returns the information in either long or wide format. #' -#' @return A `tbl` containing the information.for the specified features +#' @return An object containing the information.for the specified features #' #' @examples #' -#' tidySingleCellExperiment::pbmc_small %>% +#' data("pbmc_small") +#' pbmc_small %>% +#' join_features(features = c("HLA-DRA", "LYZ")) #' -#' join_features(features=c("HLA-DRA", "LYZ")) -#' @export #' -join_features <- function(.data, - features = NULL, - all = FALSE, - exclude_zeros = FALSE, - shape = "long", ...) { - UseMethod("join_features", .data) -} -#' @export -join_features.default <- - function(.data, - features = NULL, - all = FALSE, - exclude_zeros = FALSE, - shape = "long", ...) { - print("This function cannot be applied to this object") - } -#' @importFrom tidyselect contains -#' @importFrom tidyselect everything #' @export -join_features.SingleCellExperiment <- - function(.data, - features = NULL, - all = FALSE, - exclude_zeros = FALSE, - shape = "long", ...) { +#' +NULL + +#' join_features +#' +#' @docType methods +#' @rdname join_features +#' +#' @return An object containing the information.for the specified features +#' +setMethod("join_features", "SingleCellExperiment", function(.data, + features = NULL, + all = FALSE, + exclude_zeros = FALSE, + shape = "long", ...) +{ - # CRAN Note - cell = NULL - feature= NULL + # CRAN Note + cell = NULL + feature= NULL + + # Shape is long + if (shape == "long") + .data %>% + left_join( + get_abundance_sc_long( + .data = .data, + features = features, + all = all, + exclude_zeros = exclude_zeros + ), + by = "cell" + ) %>% + select(cell, feature, contains("abundance"), everything()) + + # Shape if wide + else + .data %>% left_join(get_abundance_sc_wide( + .data = .data, + features = features, + all = all, ... + ), + by = "cell") + +}) - # Shape is long - if (shape == "long") - .data %>% - left_join( - get_abundance_sc_long( - .data = .data, - features = features, - all = all, - exclude_zeros = exclude_zeros - ), - by = "cell" - ) %>% - select(cell, feature, contains("abundance"), everything()) - # Shape if wide - else - .data %>% left_join(get_abundance_sc_wide( - .data = .data, - features = features, - all = all, ... - ), - by = "cell") - } diff --git a/man/join_features.Rd b/man/join_features.Rd index c1c4fb3..3463178 100644 --- a/man/join_features.Rd +++ b/man/join_features.Rd @@ -1,10 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/methods.R +\docType{methods} \name{join_features} \alias{join_features} -\title{Add differential featureion information to a tbl using edgeR.} +\alias{join_features,SingleCellExperiment-method} +\title{Extract and join information for features.} \usage{ -join_features( +\S4method{join_features}{SingleCellExperiment}( .data, features = NULL, all = FALSE, @@ -14,7 +16,7 @@ join_features( ) } \arguments{ -\item{.data}{A tidy SingleCellExperiment object} +\item{.data}{A SingleCellExperiment object} \item{features}{A vector of feature identifiers to join} @@ -27,21 +29,21 @@ join_features( \item{...}{Parameters to pass to join wide, i.e. assay name to extract feature abundance from and gene prefix, for shape="wide"} } \value{ -A \code{tbl} containing the information.for the specified features +An object containing the information.for the specified features + +An object containing the information.for the specified features } \description{ -join_features() extracts and joins information for specific -features +join_features() extracts and joins information for specified features } \details{ -\lifecycle{experimental} - -This function extracts information for specified features and -returns the information in either long or wide format. +This function extracts information for specified features and returns the information in either long or wide format. } \examples{ -tidySingleCellExperiment::pbmc_small \%>\% +data("pbmc_small") +pbmc_small \%>\% +join_features(features = c("HLA-DRA", "LYZ")) + - join_features(features=c("HLA-DRA", "LYZ")) } From 762784ddcc5b607af4a0c05bafde3f6ae8b55ec3 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Sat, 23 Oct 2021 20:00:36 +1100 Subject: [PATCH 03/17] update NAMESPACE --- NAMESPACE | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 20ba739..b65a854 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,79 +2,79 @@ S3method(add_count,SingleCellExperiment) S3method(add_count,default) +S3method(arrange,SingleCellExperiment) +S3method(as_tibble,SingleCellExperiment) S3method(bind_cols,SingleCellExperiment) S3method(bind_cols,default) S3method(bind_rows,SingleCellExperiment) S3method(bind_rows,default) S3method(count,SingleCellExperiment) S3method(count,default) +S3method(distinct,SingleCellExperiment) +S3method(extract,SingleCellExperiment) +S3method(filter,SingleCellExperiment) +S3method(full_join,SingleCellExperiment) +S3method(ggplot,SingleCellExperiment) +S3method(glimpse,tidySingleCellExperiment) +S3method(group_by,SingleCellExperiment) +S3method(inner_join,SingleCellExperiment) S3method(join_transcripts,Seurat) S3method(join_transcripts,default) +S3method(left_join,SingleCellExperiment) +S3method(mutate,SingleCellExperiment) +S3method(nest,SingleCellExperiment) +S3method(pivot_longer,SingleCellExperiment) S3method(plot_ly,SingleCellExperiment) S3method(plot_ly,default) S3method(print,SingleCellExperiment) +S3method(pull,SingleCellExperiment) +S3method(rename,SingleCellExperiment) +S3method(right_join,SingleCellExperiment) +S3method(rowwise,SingleCellExperiment) +S3method(sample_frac,SingleCellExperiment) +S3method(sample_n,SingleCellExperiment) +S3method(select,SingleCellExperiment) +S3method(separate,SingleCellExperiment) +S3method(slice,SingleCellExperiment) +S3method(summarise,SingleCellExperiment) S3method(tidy,SingleCellExperiment) +S3method(unite,SingleCellExperiment) +S3method(unnest,tidySingleCellExperiment_nested) export("%>%") export(add_count) export(arrange) -export(arrange.SingleCellExperiment) export(as_tibble) -export(as_tibble.SingleCellExperiment) export(bind_cols) export(bind_rows) export(count) export(distinct) -export(distinct.SingleCellExperiment) export(extract) -export(extract.SingleCellExperiment) export(filter) -export(filter.SingleCellExperiment) export(full_join) -export(full_join.SingleCellExperiment) export(ggplot) -export(ggplot.SingleCellExperiment) export(glimpse) -export(glimpse.tidySingleCellExperiment) export(group_by) -export(group_by.SingleCellExperiment) export(inner_join) -export(inner_join.SingleCellExperiment) export(join_features) export(join_transcripts) export(left_join) -export(left_join.SingleCellExperiment) export(mutate) -export(mutate.SingleCellExperiment) export(nest) -export(nest.SingleCellExperiment) export(pivot_longer) -export(pivot_longer.SingleCellExperiment) export(plot_ly) export(pull) -export(pull.SingleCellExperiment) export(rename) -export(rename.SingleCellExperiment) export(right_join) -export(right_join.SingleCellExperiment) export(rowwise) -export(rowwise.SingleCellExperiment) export(sample_frac) -export(sample_frac.SingleCellExperiment) export(sample_n) -export(sample_n.SingleCellExperiment) export(select) -export(select.SingleCellExperiment) export(separate) -export(separate.SingleCellExperiment) export(slice) -export(slice.SingleCellExperiment) export(summarise) -export(summarise.SingleCellExperiment) export(tidy) export(unite) -export(unite.SingleCellExperiment) export(unnest) -export(unnest.tidySingleCellExperiment_nested) export(unnest_single_cell_experiment) importFrom(S4Vectors,DataFrame) importFrom(SingleCellExperiment,cbind) From 42ee280bdfeb0c4b90ac5714c8d5b816dd29e0d2 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Tue, 12 Oct 2021 16:43:22 +1100 Subject: [PATCH 04/17] fix filter for objects with missing cell names --- R/dplyr_methods.R | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/R/dplyr_methods.R b/R/dplyr_methods.R index ac49cc5..eca5238 100755 --- a/R/dplyr_methods.R +++ b/R/dplyr_methods.R @@ -297,10 +297,17 @@ filter.SingleCellExperiment <- function(.data, ..., .preserve=FALSE) { new_meta <- .data %>% as_tibble() %>% dplyr::filter(..., .preserve=.preserve) # %>% as_meta_data(.data) - new_obj <- .data[, new_meta$cell] - # colData(new_obj)=new_meta - new_obj + # Try to solve missing colnames + if(colnames(.data) %>% is.null()){ + message("tidySingleCellExperiment says: the input object does not have cell names (colnames(...)). \n Therefore, the cell column in the filtered tibble abstraction will still include an incremental integer vector.") + new_meta$cell = as.integer(new_meta$cell) + + } + + + .data[, new_meta$cell] + } From 0de9a1f7be112c1cedc7d34330ac8a57a13bddf4 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Wed, 24 Nov 2021 10:28:17 +1100 Subject: [PATCH 05/17] deprecate cell column --- NAMESPACE | 4 ++ R/dplyr_methods.R | 143 +++++++++++++++++++++++++++++++++------- R/ggplot2_methods.R | 11 +++- R/methods.R | 6 +- R/methods_DEPRECATED.R | 6 +- R/tibble_methods.R | 2 +- R/tidyr_methods.R | 53 +++++++++++++-- R/utilities.R | 68 ++++++++++++++++--- man/ggplot2-methods.Rd | 2 +- man/join_transcripts.Rd | 2 +- man/print.Rd | 27 -------- 11 files changed, 249 insertions(+), 75 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 56f3a05..9848486 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -78,7 +78,9 @@ export(tidy) export(unite) export(unnest) export(unnest_single_cell_experiment) +importFrom(S4Vectors,"metadata<-") importFrom(S4Vectors,DataFrame) +importFrom(S4Vectors,metadata) importFrom(SingleCellExperiment,cbind) importFrom(SingleCellExperiment,counts) importFrom(SummarizedExperiment,"colData<-") @@ -135,6 +137,8 @@ importFrom(rlang,is_spliced) importFrom(rlang,names2) importFrom(rlang,quo_name) importFrom(rlang,quo_squash) +importFrom(stringr,regex) +importFrom(stringr,str_detect) importFrom(stringr,str_replace) importFrom(tibble,as_tibble) importFrom(tibble,enframe) diff --git a/R/dplyr_methods.R b/R/dplyr_methods.R index eca5238..e74a4fb 100755 --- a/R/dplyr_methods.R +++ b/R/dplyr_methods.R @@ -63,7 +63,7 @@ arrange.SingleCellExperiment <- function(.data, ..., .by_group=FALSE) { as_tibble() %>% dplyr::arrange(..., .by_group=.by_group) - .data[, new_metadata$cell] + .data[, pull(new_metadata, !!c_(.data)$symbol)] } @@ -215,6 +215,14 @@ NULL distinct.SingleCellExperiment <- function(.data, ..., .keep_all=FALSE) { message(data_frame_returned_message) + distinct_columns = + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + + # Deprecation of special column names + if(is_sample_feature_deprecated_used(.data, distinct_columns)){ + .data= ping_old_special_column_into_metadata(.data) + } + .data %>% as_tibble() %>% dplyr::distinct(..., .keep_all=.keep_all) @@ -294,19 +302,28 @@ NULL #' #' @export filter.SingleCellExperiment <- function(.data, ..., .preserve=FALSE) { - new_meta <- .data %>% + + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + .data, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + .data= ping_old_special_column_into_metadata(.data) + } + + new_meta <- .data %>% as_tibble() %>% dplyr::filter(..., .preserve=.preserve) # %>% as_meta_data(.data) # Try to solve missing colnames if(colnames(.data) %>% is.null()){ message("tidySingleCellExperiment says: the input object does not have cell names (colnames(...)). \n Therefore, the cell column in the filtered tibble abstraction will still include an incremental integer vector.") - new_meta$cell = as.integer(new_meta$cell) + new_meta = mew_meta %>% mutate(!!c_(.data)$symbol := as.integer(!!c_(.data)$symbol)) } - .data[, new_meta$cell] + .data[, pull(new_meta, !!c_(.data)$symbol)] } @@ -361,6 +378,14 @@ NULL group_by.SingleCellExperiment <- function(.data, ..., .add=FALSE, .drop=group_by_drop_default(.data)) { message(data_frame_returned_message) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + .data, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + .data= ping_old_special_column_into_metadata(.data) + } + .data %>% as_tibble() %>% dplyr::group_by(..., .add=.add, .drop=.drop) @@ -447,6 +472,13 @@ NULL summarise.SingleCellExperiment <- function(.data, ...) { message(data_frame_returned_message) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + .data, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + .data= ping_old_special_column_into_metadata(.data) + } .data %>% as_tibble() %>% @@ -554,12 +586,20 @@ mutate.SingleCellExperiment <- function(.data, ...) { # Check that we are not modifying a key column cols <- enquos(...) %>% names() + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + .data, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + .data= ping_old_special_column_into_metadata(.data) + } + tst <- intersect( cols %>% names(), get_special_columns(.data) %>% - c(get_needed_columns()) + c(get_needed_columns(.data)) ) %>% length() %>% gt(0) @@ -642,7 +682,7 @@ rename.SingleCellExperiment <- function(.data, ...) { cols %>% names(), get_special_columns(.data) %>% - c(get_needed_columns()) + c(get_needed_columns(.data)) ) %>% length() %>% gt(0) @@ -650,7 +690,7 @@ rename.SingleCellExperiment <- function(.data, ...) { if (tst) { columns = get_special_columns(.data) %>% - c(get_needed_columns()) %>% + c(get_needed_columns(.data)) %>% paste(collapse=", ") stop( "tidySingleCellExperiment says: you are trying to rename a column that is view only", @@ -742,13 +782,19 @@ NULL #' @export left_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c(".x", ".y"), ...) { + + # Deprecation of special column names + if(is_sample_feature_deprecated_used( x, when(by, !is.null(.) ~ by, ~ colnames(y)))){ + x= ping_old_special_column_into_metadata(x) + } + x %>% as_tibble() %>% dplyr::left_join(y, by=by, copy=copy, suffix=suffix, ...) %>% when( # If duplicated cells returns tibble - dplyr::count(., cell) %>% + dplyr::count(., !!c_(x)$symbol) %>% filter(n > 1) %>% nrow() %>% gt(0) ~ { @@ -793,13 +839,19 @@ NULL #' @importFrom SummarizedExperiment colData #' @export inner_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c(".x", ".y"), ...) { + + # Deprecation of special column names + if(is_sample_feature_deprecated_used( x, when(by, !is.null(.) ~ by, ~ colnames(y)))){ + x= ping_old_special_column_into_metadata(x) + } + x %>% as_tibble() %>% dplyr::inner_join(y, by=by, copy=copy, suffix=suffix, ...) %>% when( # If duplicated cells returns tibble - count(., cell) %>% + count(., !!c_(x)$symbol) %>% filter(n > 1) %>% nrow() %>% gt(0) ~ { @@ -809,7 +861,7 @@ inner_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c( # Otherwise return updated tidySingleCellExperiment ~ { - new_obj <- x[, .$cell] + new_obj <- x[, pull(., c_(x)$name)] colData(new_obj) <- (.) %>% as_meta_data(new_obj) new_obj } @@ -849,13 +901,19 @@ NULL #' @export right_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c(".x", ".y"), ...) { + + # Deprecation of special column names + if(is_sample_feature_deprecated_used( x, when(by, !is.null(.) ~ by, ~ colnames(y)))){ + x= ping_old_special_column_into_metadata(x) + } + x %>% as_tibble() %>% dplyr::right_join(y, by=by, copy=copy, suffix=suffix, ...) %>% when( # If duplicated cells returns tibble - count(., cell) %>% + count(., !!c_(x)$symbol) %>% filter(n > 1) %>% nrow() %>% gt(0) ~ { @@ -865,7 +923,7 @@ right_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c( # Otherwise return updated tidySingleCellExperiment ~ { - new_obj <- x[, .$cell] + new_obj <- x[, pull(., c_(x)$name)] colData(new_obj) <- (.) %>% as_meta_data(new_obj) new_obj } @@ -905,13 +963,19 @@ NULL #' @export full_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c(".x", ".y"), ...) { + + # Deprecation of special column names + if(is_sample_feature_deprecated_used( x, when(by, !is.null(.) ~ by, ~ colnames(y)))){ + x= ping_old_special_column_into_metadata(x) + } + x %>% as_tibble() %>% dplyr::full_join(y, by=by, copy=copy, suffix=suffix, ...) %>% when( # If duplicated cells returns tibble - count(., cell) %>% + count(., !!c_(x)$symbol) %>% filter(n > 1) %>% nrow() %>% gt(0) ~ { @@ -921,7 +985,7 @@ full_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c(" # Otherwise return updated tidySingleCellExperiment ~ { - new_obj <- x[, .$cell] + new_obj <- x[, pull(., c_(x)$name)] colData(new_obj) <- (.) %>% as_meta_data(x) new_obj } @@ -1065,13 +1129,22 @@ NULL #' @importFrom SummarizedExperiment colData #' @export select.SingleCellExperiment <- function(.data, ...) { + + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + .data, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + .data= ping_old_special_column_into_metadata(.data) + } + .data %>% as_tibble() %>% select_helper(...) %>% when( # If key columns are missing - (get_needed_columns() %in% colnames(.)) %>% + (get_needed_columns(.data) %in% colnames(.)) %>% all() %>% `!`() ~ { message("tidySingleCellExperiment says: Key columns are missing. A data frame is returned for independent data analysis.") @@ -1148,19 +1221,19 @@ sample_n.SingleCellExperiment <- function(tbl, size, replace=FALSE, new_meta = colData(tbl) %>% as.data.frame() %>% - as_tibble(rownames = "cell") %>% + as_tibble(rownames = c_(tbl)$name) %>% dplyr::sample_n( size, replace = replace, weight = weight, .env = .env, ...) - count_cells = new_meta %>% select(cell) %>% count(cell) + count_cells = new_meta %>% select(!!c_(tbl)$symbol) %>% count(!!c_(tbl)$symbol) # If repeted cells if(count_cells$n %>% max() %>% gt(1)){ message("tidySingleCellExperiment says: When sampling with replacement a data frame is returned for independent data analysis.") tbl %>% as_tibble() %>% - right_join(new_meta %>% select(cell), by = "cell") + right_join(new_meta %>% select(!!c_(tbl)$symbol), by = c_(tbl)$name) } else{ - new_obj = tbl[, new_meta %>% pull(cell)] + new_obj = tbl[, new_meta %>% pull(!!c_(tbl)$symbol)] new_obj } } @@ -1185,19 +1258,19 @@ sample_frac.SingleCellExperiment <- function(tbl, size=1, replace=FALSE, new_meta = colData(tbl) %>% as.data.frame() %>% - as_tibble(rownames = "cell") %>% + as_tibble(rownames = c_(tbl)$name) %>% dplyr::sample_frac( size, replace = replace, weight = weight, .env = .env, ...) - count_cells = new_meta %>% select(cell) %>% count(cell) + count_cells = new_meta %>% select(!!c_(tbl)$symbol) %>% count(!!c_(tbl)$symbol) # If repeted cells if(count_cells$n %>% max() %>% gt(1)){ message("tidySingleCellExperiment says: When sampling with replacement a data frame is returned for independent data analysis.") tbl %>% as_tibble() %>% - right_join(new_meta %>% select(cell), by = "cell") + right_join(new_meta %>% select(!!c_(tbl)$symbol), by = c_(tbl)$name) } else{ - new_obj = tbl[, new_meta %>% pull(cell)] + new_obj = tbl[, new_meta %>% pull(!!c_(tbl)$symbol)] new_obj } } @@ -1266,6 +1339,14 @@ count.default <- function(x, ..., wt=NULL, sort=FALSE, name=NULL, .drop=group_by count.SingleCellExperiment <- function(x, ..., wt=NULL, sort=FALSE, name=NULL, .drop=group_by_drop_default(x)) { message(data_frame_returned_message) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + x, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + x= ping_old_special_column_into_metadata(x) + } + x %>% as_tibble() %>% dplyr::count(..., wt=!!enquo(wt), sort=sort, name=name, .drop=.drop) @@ -1289,6 +1370,14 @@ add_count.default <- function(x, ..., wt = NULL, sort = FALSE, name = NULL, .dro #' @rdname count add_count.SingleCellExperiment <- function(x, ..., wt = NULL, sort = FALSE, name = NULL, .drop = group_by_drop_default(x)) { + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + x, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + x= ping_old_special_column_into_metadata(x) + } + colData(x) = x %>% as_tibble %>% @@ -1342,6 +1431,14 @@ pull.SingleCellExperiment <- function(.data, var=-1, name=NULL, ...) { var <- enquo(var) name <- enquo(name) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + .data, + quo_name(var) + )){ + .data= ping_old_special_column_into_metadata(.data) + } + .data %>% as_tibble() %>% dplyr::pull(var=!!var, name=!!name, ...) diff --git a/R/ggplot2_methods.R b/R/ggplot2_methods.R index 217a4d5..c6bb225 100755 --- a/R/ggplot2_methods.R +++ b/R/ggplot2_methods.R @@ -47,13 +47,22 @@ #' library(ggplot2) #' #' tidySingleCellExperiment::pbmc_small %>% -#' +#' #' tidySingleCellExperiment::ggplot(aes(groups, nCount_RNA)) + #' geom_boxplot() NULL #' @export ggplot.SingleCellExperiment <- function(data=NULL, mapping=aes(), ..., environment=parent.frame()) { + + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + data, + mapping %>% unlist() %>% map(~ quo_name(.x)) %>% unlist() %>% as.character() + )){ + data= ping_old_special_column_into_metadata(data) + } + data %>% as_tibble() %>% ggplot2::ggplot(mapping=mapping) diff --git a/R/methods.R b/R/methods.R index b1d35c1..e08e812 100755 --- a/R/methods.R +++ b/R/methods.R @@ -124,9 +124,9 @@ join_features.SingleCellExperiment <- all = all, exclude_zeros = exclude_zeros ), - by = "cell" + by = c_(.data)$name ) %>% - select(cell, feature, contains("abundance"), everything()) + select(!!c_(.data)$symbol, feature, contains("abundance"), everything()) # Shape if wide else @@ -135,6 +135,6 @@ join_features.SingleCellExperiment <- features = features, all = all, ... ), - by = "cell") + by = c_(.data)$name) } diff --git a/R/methods_DEPRECATED.R b/R/methods_DEPRECATED.R index d53a983..7fd8d6e 100644 --- a/R/methods_DEPRECATED.R +++ b/R/methods_DEPRECATED.R @@ -9,7 +9,7 @@ #' @name join_transcripts #' @rdname join_transcripts #' -#' @param .data A tidyseurat object +#' @param .data A tidySingleCellExperiment object #' @param transcripts A vector of transcript identifiers to join #' @param all If TRUE return all #' @param exclude_zeros If TRUE exclude zero values @@ -42,7 +42,7 @@ join_transcripts.default <- exclude_zeros = FALSE, shape = "long", ...) { - print("tidyseurat says: This function cannot be applied to this object") + print("tidySingleCellExperiment says: This function cannot be applied to this object") } #' @export join_transcripts.Seurat <- @@ -53,7 +53,7 @@ join_transcripts.Seurat <- shape = "long", ...) { - deprecate_warn("1.1.2", "join_transcripts()", "tidyseurat::join_features()") + deprecate_warn("1.1.2", "join_transcripts()", "tidySingleCellExperiment::join_features()") .data %>% diff --git a/R/tibble_methods.R b/R/tibble_methods.R index 2dcb500..d8fbafb 100755 --- a/R/tibble_methods.R +++ b/R/tibble_methods.R @@ -74,7 +74,7 @@ as_tibble.SingleCellExperiment <- function(x, ..., rownames=pkgconfig::get_config("tibble::rownames", NULL)) { colData(x) %>% as.data.frame() %>% - tibble::as_tibble(rownames="cell") %>% + tibble::as_tibble(rownames=c_(x)$name) %>% # Attach reduced dimensions diff --git a/R/tidyr_methods.R b/R/tidyr_methods.R index c4db48d..b3024c5 100755 --- a/R/tidyr_methods.R +++ b/R/tidyr_methods.R @@ -185,10 +185,19 @@ NULL #' #' @export nest.SingleCellExperiment <- function(.data, ..., .names_sep = NULL) { - my_data__ <- .data cols <- enquos(...) col_name_data <- names(cols) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + .data, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + .data= ping_old_special_column_into_metadata(.data) + } + + my_data__ = .data + my_data__ %>% # This is needed otherwise nest goes into loop and fails @@ -200,7 +209,7 @@ nest.SingleCellExperiment <- function(.data, ..., .names_sep = NULL) { ~ my_data__ %>% # Subset cells - filter(cell %in% .x$cell) %>% + filter(!!c_(my_data__)$symbol %in% pull(.x, !!c_(my_data__)$symbol)) %>% # Subset columns select(colnames(.x)) @@ -266,6 +275,14 @@ extract.SingleCellExperiment <- function(data, col, into, regex="([[:alnum:]]+)" convert=FALSE, ...) { col <- enquo(col) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + data, + c(quo_name(col), into) + )){ + data= ping_old_special_column_into_metadata(data) + } + colData(data) <- data %>% as_tibble() %>% @@ -387,6 +404,14 @@ pivot_longer.SingleCellExperiment <- function(data, message(data_frame_returned_message) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + data, + c(quo_names(cols)) + )){ + data= ping_old_special_column_into_metadata(data) + } + data %>% as_tibble() %>% tidyr::pivot_longer(!!cols, @@ -449,10 +474,18 @@ unite.SingleCellExperiment <- function(data, col, ..., sep="_", remove=TRUE, na. # Check that we are not modifying a key column cols <- enquo(col) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + data, + (enquos(..., .ignore_empty = "all") %>% map(~ quo_name(.x)) %>% unlist) + )){ + data= ping_old_special_column_into_metadata(data) + } + tst <- intersect( cols %>% quo_names(), - get_special_columns(data) %>% c(get_needed_columns()) + get_special_columns(data) %>% c(get_needed_columns(data)) ) %>% length() %>% gt(0) & @@ -461,7 +494,7 @@ unite.SingleCellExperiment <- function(data, col, ..., sep="_", remove=TRUE, na. if (tst) { columns = get_special_columns(data) %>% - c(get_needed_columns()) %>% + c(get_needed_columns(data)) %>% paste(collapse=", ") stop( "tidySingleCellExperiment says: you are trying to rename a column that is view only", @@ -538,10 +571,18 @@ separate.SingleCellExperiment <- function(data, col, into, sep="[^[:alnum:]]+", # Check that we are not modifying a key column cols <- enquo(col) + # Deprecation of special column names + if(is_sample_feature_deprecated_used( + data, + c(quo_names(cols)) + )){ + data= ping_old_special_column_into_metadata(data) + } + tst <- intersect( cols %>% quo_names(), - get_special_columns(data) %>% c(get_needed_columns()) + get_special_columns(data) %>% c(get_needed_columns(data)) ) %>% length() %>% gt(0) & @@ -550,7 +591,7 @@ separate.SingleCellExperiment <- function(data, col, into, sep="[^[:alnum:]]+", if (tst) { columns = get_special_columns(data) %>% - c(get_needed_columns()) %>% + c(get_needed_columns(data)) %>% paste(collapse=", ") stop( "tidySingleCellExperiment says: you are trying to rename a column that is view only", diff --git a/R/utilities.R b/R/utilities.R index b8f2616..496e0fe 100755 --- a/R/utilities.R +++ b/R/utilities.R @@ -9,7 +9,7 @@ to_tib <- function(.data) { colData(.data) %>% as.data.frame() %>% - as_tibble(rownames="cell") + as_tibble(rownames=c_(.data)$name) } # Greater than @@ -142,10 +142,10 @@ get_abundance_sc_wide <- function(.data, features=NULL, all=FALSE, assay = assay ) %>% as.matrix() %>% t() %>% - as_tibble(rownames="cell") %>% + as_tibble(rownames=c_(.data)$name) %>% # Add prefix - setNames(c("cell", sprintf("%s%s", prefix, colnames(.)[-1]))) + setNames(c(c_(.data)$name, sprintf("%s%s", prefix, colnames(.)[-1]))) } #' get abundance long @@ -237,14 +237,14 @@ get_abundance_sc_long <- function(.data, features=NULL, all=FALSE, exclude_zeros as_tibble(rownames="feature") %>% tidyr::pivot_longer( cols=-feature, - names_to="cell", + names_to=c_(.data)$name, values_to="abundance" %>% paste(.y, sep="_"), values_drop_na=TRUE ) # %>% # mutate_if(is.character, as.factor) %>% ) %>% - Reduce(function(...) full_join(..., by=c("feature", "cell")), .) + Reduce(function(...) full_join(..., by=c("feature", c_(.data)$name)), .) } #' @importFrom dplyr select_if @@ -266,7 +266,7 @@ as_meta_data <- function(.data, SingleCellExperiment_object) { .data %>% select_if(!colnames(.) %in% col_to_exclude) %>% # select(-one_of(col_to_exclude)) %>% - data.frame(row.names="cell") %>% + data.frame(row.names=c_(SingleCellExperiment_object)$name) %>% DataFrame() } @@ -298,9 +298,9 @@ get_special_datasets <- function(SingleCellExperiment_object, n_dimensions_to_re }) } -get_needed_columns <- function() { - # c("cell", "orig.ident", "nCount_RNA", "nFeature_RNA") - c("cell") +get_needed_columns <- function(.data) { + + c(c_(.data)$name) } #' Convert array of quosure (e.g. c(col_a, col_b)) into character vector @@ -333,3 +333,53 @@ select_helper <- function(.data, ...) { data_frame_returned_message = "tidySingleCellExperiment says: A data frame is returned for independent data analysis." duplicated_cell_names = "tidySingleCellExperiment says: This operation lead to duplicated cell names. A data frame is returned for independent data analysis." +# This function is used for the change of special sample column to .sample +# Check if "sample" is included in the query and is not part of any other existing annotation +#' @importFrom stringr str_detect +#' @importFrom stringr regex +is_sample_feature_deprecated_used = function(.data, user_columns, use_old_special_names = FALSE){ + + old_standard_is_used_for_cell = + ( + ( any(str_detect(user_columns , regex("\\bcell\\b"))) & !any(str_detect(user_columns , regex("\\W*(\\.cell)\\W*"))) ) | + "cell" %in% user_columns + ) & + !"cell" %in% colnames(colData(.data)) + + old_standard_is_used = old_standard_is_used_for_cell + + if(old_standard_is_used){ + warning("tidySingleCellExperiment says: from version 1.3.1, the special columns including cell id (colnames(se)) has changed to \".cell\". This dataset is returned with the old-style vocabulary (feature), however we suggest to update your workflow to reflect the new vocabulary (.cell)") + + use_old_special_names = TRUE + } + + use_old_special_names +} + +get_special_column_name_symbol = function(name){ + list(name = name, symbol = as.symbol(name)) +} + +# Key column names +#' @importFrom S4Vectors metadata +#' @importFrom S4Vectors metadata<- +ping_old_special_column_into_metadata = function(.data){ + + metadata(.data)$cell__ = get_special_column_name_symbol("cell") + + .data +} + +get_special_column_name_cell = function(name){ + list(name = name, symbol = as.symbol(name)) +} + +cell__ = get_special_column_name_symbol(".cell") + +#' @importFrom S4Vectors metadata +c_ = function(x){ + # Check if old deprecated columns are used + if("cell__" %in% names(metadata(x))) cell__ = metadata(x)$cell__ + return(cell__) +} diff --git a/man/ggplot2-methods.Rd b/man/ggplot2-methods.Rd index 05328f6..171dd34 100644 --- a/man/ggplot2-methods.Rd +++ b/man/ggplot2-methods.Rd @@ -47,7 +47,7 @@ is often the case in complex graphics. library(ggplot2) tidySingleCellExperiment::pbmc_small \%>\% - + tidySingleCellExperiment::ggplot(aes(groups, nCount_RNA)) + geom_boxplot() } diff --git a/man/join_transcripts.Rd b/man/join_transcripts.Rd index 498dd40..2954e36 100644 --- a/man/join_transcripts.Rd +++ b/man/join_transcripts.Rd @@ -14,7 +14,7 @@ join_transcripts( ) } \arguments{ -\item{.data}{A tidyseurat object} +\item{.data}{A tidySingleCellExperiment object} \item{transcripts}{A vector of transcript identifiers to join} diff --git a/man/print.Rd b/man/print.Rd index 85ea70f..53d4817 100644 --- a/man/print.Rd +++ b/man/print.Rd @@ -62,33 +62,6 @@ exceeded. Default: 10. \item \code{tibble.max_extra_cols}: Number of extra columns printed in reduced form. Default: 100. } - - - -\itemize{ -\item \code{pillar.bold}: Use bold font, e.g. for column headers? This currently -defaults to \code{FALSE}, because many terminal fonts have poor support for -bold fonts. -\item \code{pillar.subtle}: Use subtle style, e.g. for row numbers and data types? -Default: \code{TRUE}. -\item \code{pillar.subtle_num}: Use subtle style for insignificant digits? Default: -\code{FALSE}, is also affected by the \code{pillar.subtle} option. -\item \code{pillar.neg}: Highlight negative numbers? Default: \code{TRUE}. -\item \code{pillar.sigfig}: The number of significant digits that will be printed and -highlighted, default: \code{3}. Set the \code{pillar.subtle} option to \code{FALSE} to -turn off highlighting of significant digits. -\item \code{pillar.min_title_chars}: The minimum number of characters for the column -title, default: \code{15}. Column titles may be truncated up to that width to -save horizontal space. Set to \code{Inf} to turn off truncation of column -titles. -\item \code{pillar.min_chars}: The minimum number of characters wide to -display character columns, default: \code{0}. Character columns may be -truncated up to that width to save horizontal space. Set to \code{Inf} to -turn off truncation of character columns. -\item \code{pillar.max_dec_width}: The maximum allowed width for decimal notation, -default 13. -} - } \examples{ From 3c4deb807b951438303c5083fc7ef26a94ad29a9 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Wed, 24 Nov 2021 13:05:32 +1100 Subject: [PATCH 06/17] fix vignette --- vignettes/introduction.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index 6f6cb46..73413d1 100755 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -250,7 +250,7 @@ marker_genes <- pbmc_small_cluster %>% join_features(features=marker_genes) %>% group_by(label) %>% - heatmap(feature, cell, abundance_counts, .scale="column") + heatmap(feature, .cell, abundance_counts, .scale="column") ``` # Reduce dimensions From 80732516fd9a0e33fad87ac26556a6a0872b0d1c Mon Sep 17 00:00:00 2001 From: stemangiola Date: Wed, 24 Nov 2021 13:39:19 +1100 Subject: [PATCH 07/17] change abundance and fix docs --- R/methods.R | 6 +++--- R/tidyr_methods.R | 2 +- R/utilities.R | 10 +++++----- README.Rmd | 4 ++-- README.md | 4 ++-- man/tidyr-methods.Rd | 2 ++ vignettes/introduction.Rmd | 4 ++-- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/R/methods.R b/R/methods.R index e08e812..a12a8ca 100755 --- a/R/methods.R +++ b/R/methods.R @@ -111,8 +111,8 @@ join_features.SingleCellExperiment <- shape = "long", ...) { # CRAN Note - cell = NULL - feature= NULL + .cell = NULL + .feature= NULL # Shape is long if (shape == "long") @@ -126,7 +126,7 @@ join_features.SingleCellExperiment <- ), by = c_(.data)$name ) %>% - select(!!c_(.data)$symbol, feature, contains("abundance"), everything()) + select(!!c_(.data)$symbol, .feature, contains(".abundance"), everything()) # Shape if wide else diff --git a/R/tidyr_methods.R b/R/tidyr_methods.R index b3024c5..d3f5499 100755 --- a/R/tidyr_methods.R +++ b/R/tidyr_methods.R @@ -92,7 +92,7 @@ NULL #' @param ptype See tidyr::unnest #' @param .drop See tidyr::unnest #' @param .id tidyr::unnest -#' @param sep tidyr::unnest +#' @param .sep tidyr::unnest #' @param .preserve See tidyr::unnest #' #' @return A tidySingleCellExperiment objector a tibble depending on input diff --git a/R/utilities.R b/R/utilities.R index 496e0fe..64e717a 100755 --- a/R/utilities.R +++ b/R/utilities.R @@ -234,17 +234,17 @@ get_abundance_sc_long <- function(.data, features=NULL, all=FALSE, exclude_zeros }, ~ (.)) %>% as.matrix() %>% data.frame(check.names = FALSE) %>% - as_tibble(rownames="feature") %>% + as_tibble(rownames=".feature") %>% tidyr::pivot_longer( - cols=-feature, + cols=- .feature, names_to=c_(.data)$name, - values_to="abundance" %>% paste(.y, sep="_"), + values_to=".abundance" %>% paste(.y, sep="_"), values_drop_na=TRUE ) # %>% # mutate_if(is.character, as.factor) %>% ) %>% - Reduce(function(...) full_join(..., by=c("feature", c_(.data)$name)), .) + Reduce(function(...) full_join(..., by=c(".feature", c_(.data)$name)), .) } #' @importFrom dplyr select_if @@ -349,7 +349,7 @@ is_sample_feature_deprecated_used = function(.data, user_columns, use_old_specia old_standard_is_used = old_standard_is_used_for_cell if(old_standard_is_used){ - warning("tidySingleCellExperiment says: from version 1.3.1, the special columns including cell id (colnames(se)) has changed to \".cell\". This dataset is returned with the old-style vocabulary (feature), however we suggest to update your workflow to reflect the new vocabulary (.cell)") + warning("tidySingleCellExperiment says: from version 1.3.1, the special columns including cell id (colnames(se)) has changed to \".cell\". This dataset is returned with the old-style vocabulary (cell), however we suggest to update your workflow to reflect the new vocabulary (.cell)") use_old_special_names = TRUE } diff --git a/README.Rmd b/README.Rmd index 392cb05..481d775 100755 --- a/README.Rmd +++ b/README.Rmd @@ -180,7 +180,7 @@ Here we plot abundance of two features for each group. ```{r} pbmc_small_polished %>% join_features(features=c("HLA-DRA", "LYZ")) %>% - ggplot(aes(groups, abundance_counts + 1, fill=groups)) + + ggplot(aes(groups, .abundance_counts + 1, fill=groups)) + geom_boxplot(outlier.shape=NA) + geom_jitter(aes(size=nCount_RNA), alpha=0.5, width=0.2) + scale_y_log10() + @@ -317,7 +317,7 @@ marker_genes <- pbmc_small_cluster %>% join_features(features=marker_genes) %>% group_by(label) %>% - heatmap(feature, cell, abundance_counts, .scale="column") + heatmap(.feature, .cell, .abundance_counts, .scale="column") ``` # Reduce dimensions diff --git a/README.md b/README.md index 6896b6f..f8b517f 100755 --- a/README.md +++ b/README.md @@ -224,7 +224,7 @@ Here we plot abundance of two features for each group. pbmc_small_polished %>% join_features(features=c("HLA-DRA", "LYZ")) %>% - ggplot(aes(groups, abundance_counts + 1, fill=groups)) + + ggplot(aes(groups, .abundance_counts + 1, fill=groups)) + geom_boxplot(outlier.shape=NA) + geom_jitter(aes(size=nCount_RNA), alpha=0.5, width=0.2) + scale_y_log10() + @@ -371,7 +371,7 @@ SingleCellExperiment, tidyverse functions and tidyHeatmap pbmc_small_cluster %>% join_features(features=marker_genes) %>% group_by(label) %>% - heatmap(feature, cell, abundance_counts, .scale="column") + heatmap(.feature, .cell, .abundance_counts, .scale="column") ## tidySingleCellExperiment says: A data frame is returned for independent data analysis. diff --git a/man/tidyr-methods.Rd b/man/tidyr-methods.Rd index 35c5a89..e167177 100644 --- a/man/tidyr-methods.Rd +++ b/man/tidyr-methods.Rd @@ -51,6 +51,8 @@ stripped. This makes \code{names_sep} roughly symmetric between nesting and unne \item{.id}{tidyr::unnest} +\item{.sep}{tidyr::unnest} + \item{.preserve}{See tidyr::unnest} \item{sep}{tidyr::unnest} diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index 73413d1..5bb9d53 100755 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -169,7 +169,7 @@ Here we plot abundance of two features for each group. ```{r} pbmc_small_polished %>% join_features(features=c("HLA-DRA", "LYZ")) %>% - ggplot(aes(groups, abundance_counts + 1, fill=groups)) + + ggplot(aes(groups, .abundance_counts + 1, fill=groups)) + geom_boxplot(outlier.shape=NA) + geom_jitter(aes(size=nCount_RNA), alpha=0.5, width=0.2) + scale_y_log10() + @@ -250,7 +250,7 @@ marker_genes <- pbmc_small_cluster %>% join_features(features=marker_genes) %>% group_by(label) %>% - heatmap(feature, .cell, abundance_counts, .scale="column") + heatmap(.feature, .cell, .abundance_counts, .scale="column") ``` # Reduce dimensions From a8357cb90695d69a6a19136a26427f6cc1ddc323 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Wed, 24 Nov 2021 14:36:16 +1100 Subject: [PATCH 08/17] fix docs --- R/tidyr_methods.R | 19 +++++++++++++------ man/tidyr-methods.Rd | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/R/tidyr_methods.R b/R/tidyr_methods.R index d3f5499..1d86b87 100755 --- a/R/tidyr_methods.R +++ b/R/tidyr_methods.R @@ -54,6 +54,19 @@ #' @export NULL + +#' @rdname tidyr-methods +#' @name unnest +#' +#' @export +unnest.tidySingleCellExperiment_nested <- function(data, cols, ..., keep_empty=FALSE, ptype=NULL, + names_sep=NULL, names_repair="check_unique", .drop, .id, .sep, .preserve) { + unnest_single_cell_experiment(data, cols, ..., keep_empty=keep_empty, ptype=ptype, + names_sep=names_sep, names_repair=names_repair, .drop, .id, .sep, .preserve) + } + + + #' unnest_single_cell_experiment #' #' @importFrom tidyr unnest @@ -148,12 +161,6 @@ unnest_single_cell_experiment <- function(data, cols, ..., keep_empty=FALSE, p ) } -#' @importFrom rlang quo_name -#' @importFrom purrr imap -#' -#' -#' @export -unnest.tidySingleCellExperiment_nested <- unnest_single_cell_experiment diff --git a/man/tidyr-methods.Rd b/man/tidyr-methods.Rd index e167177..65448eb 100644 --- a/man/tidyr-methods.Rd +++ b/man/tidyr-methods.Rd @@ -2,11 +2,26 @@ % Please edit documentation in R/tidyr_methods.R \name{unnest} \alias{unnest} +\alias{unnest.tidySingleCellExperiment_nested} \alias{unnest_single_cell_experiment} \alias{nest} \alias{extract} \title{unnest} \usage{ +\method{unnest}{tidySingleCellExperiment_nested}( + data, + cols, + ..., + keep_empty = FALSE, + ptype = NULL, + names_sep = NULL, + names_repair = "check_unique", + .drop, + .id, + .sep, + .preserve +) + unnest_single_cell_experiment( data, cols, From d58a6873272b3d078e23ae11f20b69b5af42ded9 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Wed, 24 Nov 2021 16:13:37 +1100 Subject: [PATCH 09/17] fix unnest --- R/tidyr_methods.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/tidyr_methods.R b/R/tidyr_methods.R index 1d86b87..57acdcb 100755 --- a/R/tidyr_methods.R +++ b/R/tidyr_methods.R @@ -61,7 +61,10 @@ NULL #' @export unnest.tidySingleCellExperiment_nested <- function(data, cols, ..., keep_empty=FALSE, ptype=NULL, names_sep=NULL, names_repair="check_unique", .drop, .id, .sep, .preserve) { - unnest_single_cell_experiment(data, cols, ..., keep_empty=keep_empty, ptype=ptype, + + cols <- enquo(cols) + + unnest_single_cell_experiment(data, !!cols, ..., keep_empty=keep_empty, ptype=ptype, names_sep=names_sep, names_repair=names_repair, .drop, .id, .sep, .preserve) } From 9bc512630faf83cb4cc658b3c9b6a3dafb17499b Mon Sep 17 00:00:00 2001 From: stemangiola Date: Wed, 24 Nov 2021 16:21:01 +1100 Subject: [PATCH 10/17] fix arguments unnest --- R/tidyr_methods.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tidyr_methods.R b/R/tidyr_methods.R index 57acdcb..354e4a1 100755 --- a/R/tidyr_methods.R +++ b/R/tidyr_methods.R @@ -65,7 +65,7 @@ unnest.tidySingleCellExperiment_nested <- function(data, cols, ..., keep_empty=F cols <- enquo(cols) unnest_single_cell_experiment(data, !!cols, ..., keep_empty=keep_empty, ptype=ptype, - names_sep=names_sep, names_repair=names_repair, .drop, .id, .sep, .preserve) + names_sep=names_sep, names_repair=names_repair) } From 507753e2ebba53582e373962994b308677ab1583 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Mon, 20 Dec 2021 19:16:35 +1100 Subject: [PATCH 11/17] update code and fixes --- R/dplyr_methods.R | 2 +- R/tidyr_methods.R | 12 ++-- man/extract-methods.Rd | 61 ++++++++++++++++++++ man/nest-methods.Rd | 26 +++++++++ man/separate-methods.Rd | 52 +++++++++++++++++ man/unite-methods.Rd | 43 +------------- man/{tidyr-methods.Rd => unnest-methods.Rd} | 64 +++++---------------- 7 files changed, 163 insertions(+), 97 deletions(-) create mode 100644 man/extract-methods.Rd create mode 100644 man/nest-methods.Rd create mode 100644 man/separate-methods.Rd rename man/{tidyr-methods.Rd => unnest-methods.Rd} (56%) diff --git a/R/dplyr_methods.R b/R/dplyr_methods.R index e74a4fb..162d1ed 100755 --- a/R/dplyr_methods.R +++ b/R/dplyr_methods.R @@ -318,7 +318,7 @@ filter.SingleCellExperiment <- function(.data, ..., .preserve=FALSE) { # Try to solve missing colnames if(colnames(.data) %>% is.null()){ message("tidySingleCellExperiment says: the input object does not have cell names (colnames(...)). \n Therefore, the cell column in the filtered tibble abstraction will still include an incremental integer vector.") - new_meta = mew_meta %>% mutate(!!c_(.data)$symbol := as.integer(!!c_(.data)$symbol)) + new_meta = new_meta %>% mutate(!!c_(.data)$symbol := as.integer(!!c_(.data)$symbol)) } diff --git a/R/tidyr_methods.R b/R/tidyr_methods.R index 354e4a1..e210215 100755 --- a/R/tidyr_methods.R +++ b/R/tidyr_methods.R @@ -48,14 +48,14 @@ #' nest(data=-groups) %>% #' unnest(data) #' -#' @rdname tidyr-methods +#' @rdname unnest-methods #' @name unnest #' #' @export NULL -#' @rdname tidyr-methods +#' @rdname unnest-methods #' @name unnest #' #' @export @@ -121,7 +121,7 @@ unnest.tidySingleCellExperiment_nested <- function(data, cols, ..., keep_empty=F #' nest(data=-groups) %>% #' unnest_single_cell_experiment(data) #' -#' @rdname tidyr-methods +#' @rdname unnest-methods #' @name unnest_single_cell_experiment #' #' @@ -184,7 +184,7 @@ unnest_single_cell_experiment <- function(data, cols, ..., keep_empty=FALSE, p #' #' nest(data=-groups) %>% #' unnest(data) -#' @rdname tidyr-methods +#' @rdname nest-methods #' @name nest #' #' @export @@ -268,7 +268,7 @@ nest.SingleCellExperiment <- function(.data, ..., .names_sep = NULL) { #' #' @importFrom tidyr extract #' -#' @rdname tidyr-methods +#' @rdname extract-methods #' @name extract #' #' @export @@ -559,7 +559,7 @@ unite.SingleCellExperiment <- function(data, col, ..., sep="_", remove=TRUE, na. #' #' @return A tidySingleCellExperiment objector a tibble depending on input #' -#' @rdname unite-methods +#' @rdname separate-methods #' @name separate #' #' @export diff --git a/man/extract-methods.Rd b/man/extract-methods.Rd new file mode 100644 index 0000000..e496a0a --- /dev/null +++ b/man/extract-methods.Rd @@ -0,0 +1,61 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tidyr_methods.R +\name{extract} +\alias{extract} +\alias{extract.SingleCellExperiment} +\title{Extract a character column into multiple columns using regular +expression groups} +\usage{ +\method{extract}{SingleCellExperiment}( + data, + col, + into, + regex = "([[:alnum:]]+)", + remove = TRUE, + convert = FALSE, + ... +) +} +\arguments{ +\item{data}{A tidySingleCellExperiment object} + +\item{col}{Column name or position. This is passed to +\code{\link[tidyselect:vars_pull]{tidyselect::vars_pull()}}. + +This argument is passed by expression and supports +\link[rlang:nse-force]{quasiquotation} (you can unquote column +names or column positions).} + +\item{into}{Names of new variables to create as character vector. +Use \code{NA} to omit the variable in the output.} + +\item{regex}{a regular expression used to extract the desired values. +There should be one group (defined by \verb{()}) for each element of \code{into}.} + +\item{remove}{If \code{TRUE}, remove input column from output data frame.} + +\item{convert}{If \code{TRUE}, will run \code{\link[=type.convert]{type.convert()}} with +\code{as.is=TRUE} on new columns. This is useful if the component +columns are integer, numeric or logical. + +NB: this will cause string \code{"NA"}s to be converted to \code{NA}s.} + +\item{...}{Additional arguments passed on to methods.} +} +\value{ +A tidySingleCellExperiment objector a tibble depending on input +} +\description{ +Given a regular expression with capturing groups, \code{extract()} turns +each group into a new column. If the groups don't match, or the input +is NA, the output will be NA. +} +\examples{ + +pbmc_small \%>\% + + extract(groups, into="g", regex="g([0-9])", convert=TRUE) +} +\seealso{ +\code{\link[=separate]{separate()}} to split up by a separator. +} diff --git a/man/nest-methods.Rd b/man/nest-methods.Rd new file mode 100644 index 0000000..fb257f9 --- /dev/null +++ b/man/nest-methods.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tidyr_methods.R +\name{nest} +\alias{nest} +\title{nest} +\arguments{ +\item{.data}{A tbl. (See tidyr)} + +\item{...}{Name-variable pairs of the form new_col=c(col1, col2, col3) (See tidyr)} + +\item{.names_sep}{See ?tidyr::nest} +} +\value{ +A tidySingleCellExperiment objector a tibble depending on input +} +\description{ +nest +} +\examples{ + +library(dplyr) +pbmc_small \%>\% + + nest(data=-groups) \%>\% + unnest(data) +} diff --git a/man/separate-methods.Rd b/man/separate-methods.Rd new file mode 100644 index 0000000..24fd354 --- /dev/null +++ b/man/separate-methods.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tidyr_methods.R +\name{separate} +\alias{separate} +\title{Separate a character column into multiple columns with a regular +expression or numeric locations} +\arguments{ +\item{sep}{Separator between columns. + +If character, \code{sep} is interpreted as a regular expression. The default +value is a regular expression that matches any sequence of +non-alphanumeric values. + +If numeric, \code{sep} is interpreted as character positions to split at. Positive +values start at 1 at the far-left of the string; negative value start at -1 at +the far-right of the string. The length of \code{sep} should be one less than +\code{into}.} + +\item{extra}{If \code{sep} is a character vector, this controls what +happens when there are too many pieces. There are three valid options: +\itemize{ +\item "warn" (the default): emit a warning and drop extra values. +\item "drop": drop any extra values without a warning. +\item "merge": only splits at most \code{length(into)} times +}} + +\item{fill}{If \code{sep} is a character vector, this controls what +happens when there are not enough pieces. There are three valid options: +\itemize{ +\item "warn" (the default): emit a warning and fill from the right +\item "right": fill with missing values on the right +\item "left": fill with missing values on the left +}} +} +\value{ +A tidySingleCellExperiment objector a tibble depending on input +} +\description{ +Given either a regular expression or a vector of character positions, +\code{separate()} turns a single character column into multiple columns. +} +\examples{ + +un <- pbmc_small \%>\% + + unite("new_col", c(orig.ident, groups)) +un \%>\% separate(col=new_col, into=c("orig.ident", "groups")) +} +\seealso{ +\code{\link[=unite]{unite()}}, the complement, \code{\link[=extract]{extract()}} which uses regular +expression capturing groups. +} diff --git a/man/unite-methods.Rd b/man/unite-methods.Rd index 31e9db6..fb19305 100644 --- a/man/unite-methods.Rd +++ b/man/unite-methods.Rd @@ -2,7 +2,6 @@ % Please edit documentation in R/tidyr_methods.R \name{unite} \alias{unite} -\alias{separate} \title{Unite multiple columns into one by pasting strings together} \arguments{ \item{data}{A data frame.} @@ -18,63 +17,25 @@ tidyverse; we support it here for backward compatibility).} \item{...}{<\code{\link[=tidyr_tidy_select]{tidy-select}}> Columns to unite} +\item{sep}{Separator to use between values.} + \item{na.rm}{If \code{TRUE}, missing values will be remove prior to uniting each value.} \item{remove}{If \code{TRUE}, remove input columns from output data frame.} - -\item{sep}{Separator between columns. - -If character, \code{sep} is interpreted as a regular expression. The default -value is a regular expression that matches any sequence of -non-alphanumeric values. - -If numeric, \code{sep} is interpreted as character positions to split at. Positive -values start at 1 at the far-left of the string; negative value start at -1 at -the far-right of the string. The length of \code{sep} should be one less than -\code{into}.} - -\item{extra}{If \code{sep} is a character vector, this controls what -happens when there are too many pieces. There are three valid options: -\itemize{ -\item "warn" (the default): emit a warning and drop extra values. -\item "drop": drop any extra values without a warning. -\item "merge": only splits at most \code{length(into)} times -}} - -\item{fill}{If \code{sep} is a character vector, this controls what -happens when there are not enough pieces. There are three valid options: -\itemize{ -\item "warn" (the default): emit a warning and fill from the right -\item "right": fill with missing values on the right -\item "left": fill with missing values on the left -}} } \value{ -A tidySingleCellExperiment objector a tibble depending on input - A tidySingleCellExperiment objector a tibble depending on input } \description{ Convenience function to paste together multiple columns into one. - -Given either a regular expression or a vector of character positions, -\code{separate()} turns a single character column into multiple columns. } \examples{ pbmc_small \%>\% unite("new_col", c(orig.ident, groups)) - -un <- pbmc_small \%>\% - - unite("new_col", c(orig.ident, groups)) -un \%>\% separate(col=new_col, into=c("orig.ident", "groups")) } \seealso{ \code{\link[=separate]{separate()}}, the complement. - -\code{\link[=unite]{unite()}}, the complement, \code{\link[=extract]{extract()}} which uses regular -expression capturing groups. } diff --git a/man/tidyr-methods.Rd b/man/unnest-methods.Rd similarity index 56% rename from man/tidyr-methods.Rd rename to man/unnest-methods.Rd index 65448eb..7787fa8 100644 --- a/man/tidyr-methods.Rd +++ b/man/unnest-methods.Rd @@ -4,8 +4,6 @@ \alias{unnest} \alias{unnest.tidySingleCellExperiment_nested} \alias{unnest_single_cell_experiment} -\alias{nest} -\alias{extract} \title{unnest} \usage{ \method{unnest}{tidySingleCellExperiment_nested}( @@ -37,14 +35,24 @@ unnest_single_cell_experiment( ) } \arguments{ -\item{data}{A tidySingleCellExperiment object} +\item{data}{A tbl. (See tidyr)} \item{cols}{<\code{\link[=tidyr_tidy_select]{tidy-select}}> Columns to unnest. If you \code{unnest()} multiple columns, parallel entries must be of compatible sizes, i.e. they're either equal or length 1 (following the standard tidyverse recycling rules).} -\item{...}{Additional arguments passed on to methods.} +\item{...}{<\code{\link[=tidyr_tidy_select]{tidy-select}}> Columns to nest, specified +using name-variable pairs of the form \code{new_col=c(col1, col2, col3)}. +The right hand side can be any valid tidy select expression. + +\Sexpr[results=rd, stage=render]{lifecycle::badge("deprecated")}: +previously you could write \code{df \%>\% nest(x, y, z)} and \code{df \%>\% unnest(x, y, z)}. Convert to \code{df \%>\% nest(data=c(x, y, z))}. +and \code{df \%>\% unnest(c(x, y, z))}. + +If you previously created new variable in \code{unnest()} you'll now need to +do it explicitly with \code{mutate()}. Convert \code{df \%>\% unnest(y=fun(x, y, z))} +to \code{df \%>\% mutate(y=fun(x, y, z)) \%>\% unnest(y)}.} \item{keep_empty}{See tidyr::unnest} @@ -71,45 +79,16 @@ stripped. This makes \code{names_sep} roughly symmetric between nesting and unne \item{.preserve}{See tidyr::unnest} \item{sep}{tidyr::unnest} - -\item{.data}{A tbl. (See tidyr)} - -\item{.names_sep}{See ?tidyr::nest} - -\item{col}{Column name or position. This is passed to -\code{\link[tidyselect:vars_pull]{tidyselect::vars_pull()}}. - -This argument is passed by expression and supports -\link[rlang:nse-force]{quasiquotation} (you can unquote column -names or column positions).} - -\item{into}{Names of new variables to create as character vector. -Use \code{NA} to omit the variable in the output.} - -\item{regex}{a regular expression used to extract the desired values. -There should be one group (defined by \verb{()}) for each element of \code{into}.} - -\item{remove}{If \code{TRUE}, remove input column from output data frame.} - -\item{convert}{If \code{TRUE}, will run \code{\link[=type.convert]{type.convert()}} with -\code{as.is=TRUE} on new columns. This is useful if the component -columns are integer, numeric or logical. - -NB: this will cause string \code{"NA"}s to be converted to \code{NA}s.} } \value{ A tidySingleCellExperiment objector a tibble depending on input -A tidySingleCellExperiment objector a tibble depending on input - -A tidySingleCellExperiment objector a tibble depending on input - A tidySingleCellExperiment objector a tibble depending on input } \description{ -Given a regular expression with capturing groups, \code{extract()} turns -each group into a new column. If the groups don't match, or the input -is NA, the output will be NA. +unnest + +unnest_single_cell_experiment } \examples{ @@ -126,17 +105,4 @@ pbmc_small \%>\% nest(data=-groups) \%>\% unnest_single_cell_experiment(data) - -library(dplyr) -pbmc_small \%>\% - - nest(data=-groups) \%>\% - unnest(data) - -pbmc_small \%>\% - - extract(groups, into="g", regex="g([0-9])", convert=TRUE) -} -\seealso{ -\code{\link[=separate]{separate()}} to split up by a separator. } From 4d2a6488108d1617dba44c470ce94bfc6f781780 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Fri, 28 Jan 2022 23:48:38 +1100 Subject: [PATCH 12/17] test --- DESCRIPTION | 2 +- man/print.Rd | 27 --------------------------- tests/testthat/test-dplyr_methods.R | 2 -- tests/testthat/test-methods.R | 15 +++++++++++++++ 4 files changed, 16 insertions(+), 30 deletions(-) create mode 100644 tests/testthat/test-methods.R diff --git a/DESCRIPTION b/DESCRIPTION index 7024be9..0b88998 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -56,7 +56,7 @@ Biarch: true biocViews: AssayDomain, Infrastructure, RNASeq, DifferentialExpression, GeneExpression, Normalization, Clustering, QualityControl, Sequencing, featureion, featureomics Encoding: UTF-8 LazyData: true -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.2 Roxygen: list(markdown = TRUE) URL: https://github.com/stemangiola/tidySingleCellExperiment BugReports: https://github.com/stemangiola/tidySingleCellExperiment/issues diff --git a/man/print.Rd b/man/print.Rd index 85ea70f..53d4817 100644 --- a/man/print.Rd +++ b/man/print.Rd @@ -62,33 +62,6 @@ exceeded. Default: 10. \item \code{tibble.max_extra_cols}: Number of extra columns printed in reduced form. Default: 100. } - - - -\itemize{ -\item \code{pillar.bold}: Use bold font, e.g. for column headers? This currently -defaults to \code{FALSE}, because many terminal fonts have poor support for -bold fonts. -\item \code{pillar.subtle}: Use subtle style, e.g. for row numbers and data types? -Default: \code{TRUE}. -\item \code{pillar.subtle_num}: Use subtle style for insignificant digits? Default: -\code{FALSE}, is also affected by the \code{pillar.subtle} option. -\item \code{pillar.neg}: Highlight negative numbers? Default: \code{TRUE}. -\item \code{pillar.sigfig}: The number of significant digits that will be printed and -highlighted, default: \code{3}. Set the \code{pillar.subtle} option to \code{FALSE} to -turn off highlighting of significant digits. -\item \code{pillar.min_title_chars}: The minimum number of characters for the column -title, default: \code{15}. Column titles may be truncated up to that width to -save horizontal space. Set to \code{Inf} to turn off truncation of column -titles. -\item \code{pillar.min_chars}: The minimum number of characters wide to -display character columns, default: \code{0}. Character columns may be -truncated up to that width to save horizontal space. Set to \code{Inf} to -turn off truncation of character columns. -\item \code{pillar.max_dec_width}: The maximum allowed width for decimal notation, -default 13. -} - } \examples{ diff --git a/tests/testthat/test-dplyr_methods.R b/tests/testthat/test-dplyr_methods.R index afdf184..db3204e 100755 --- a/tests/testthat/test-dplyr_methods.R +++ b/tests/testthat/test-dplyr_methods.R @@ -2,8 +2,6 @@ context("dplyr test") library(magrittr) -pbmc_small <- pbmc_small - test_that("arrange", { tt_pca_aranged <- pbmc_small %>% diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R new file mode 100644 index 0000000..dcc255a --- /dev/null +++ b/tests/testthat/test-methods.R @@ -0,0 +1,15 @@ +context('methods test') + +data("pbmc_small") + +test_that("join_features",{ + + + pbmc_small |> + join_features("CD3D") |> + slice(1) |> + tidySingleCellExperiment::pull(abundance_counts) |> + expect_equal(4, tolerance=0.1) + + +}) From ab6f3d8bbb91dff0f6380b1e8b47ac8355c8b605 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Sat, 23 Oct 2021 19:35:20 +1100 Subject: [PATCH 13/17] use ttservice --- DESCRIPTION | 1 + NAMESPACE | 57 +++++++++++----------- R/methods.R | 113 ++++++++++++++++++++----------------------- man/join_features.Rd | 26 +++++----- 4 files changed, 95 insertions(+), 102 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8223300..febd58f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Description: tidySingleCellExperiment is an adapter that abstracts the 'SingleCe License: GPL-3 Depends: R (>= 4.0.0), + ttservice, SingleCellExperiment Imports: SummarizedExperiment, diff --git a/NAMESPACE b/NAMESPACE index 56f3a05..20ba739 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,81 +2,79 @@ S3method(add_count,SingleCellExperiment) S3method(add_count,default) -S3method(arrange,SingleCellExperiment) -S3method(as_tibble,SingleCellExperiment) S3method(bind_cols,SingleCellExperiment) S3method(bind_cols,default) S3method(bind_rows,SingleCellExperiment) S3method(bind_rows,default) S3method(count,SingleCellExperiment) S3method(count,default) -S3method(distinct,SingleCellExperiment) -S3method(extract,SingleCellExperiment) -S3method(filter,SingleCellExperiment) -S3method(full_join,SingleCellExperiment) -S3method(ggplot,SingleCellExperiment) -S3method(glimpse,tidySingleCellExperiment) -S3method(group_by,SingleCellExperiment) -S3method(inner_join,SingleCellExperiment) -S3method(join_features,SingleCellExperiment) -S3method(join_features,default) S3method(join_transcripts,Seurat) S3method(join_transcripts,default) -S3method(left_join,SingleCellExperiment) -S3method(mutate,SingleCellExperiment) -S3method(nest,SingleCellExperiment) -S3method(pivot_longer,SingleCellExperiment) S3method(plot_ly,SingleCellExperiment) S3method(plot_ly,default) S3method(print,SingleCellExperiment) -S3method(pull,SingleCellExperiment) -S3method(rename,SingleCellExperiment) -S3method(right_join,SingleCellExperiment) -S3method(rowwise,SingleCellExperiment) -S3method(sample_frac,SingleCellExperiment) -S3method(sample_n,SingleCellExperiment) -S3method(select,SingleCellExperiment) -S3method(separate,SingleCellExperiment) -S3method(slice,SingleCellExperiment) -S3method(summarise,SingleCellExperiment) S3method(tidy,SingleCellExperiment) -S3method(unite,SingleCellExperiment) -S3method(unnest,tidySingleCellExperiment_nested) export("%>%") export(add_count) export(arrange) +export(arrange.SingleCellExperiment) export(as_tibble) +export(as_tibble.SingleCellExperiment) export(bind_cols) export(bind_rows) export(count) export(distinct) +export(distinct.SingleCellExperiment) export(extract) +export(extract.SingleCellExperiment) export(filter) +export(filter.SingleCellExperiment) export(full_join) +export(full_join.SingleCellExperiment) export(ggplot) +export(ggplot.SingleCellExperiment) export(glimpse) +export(glimpse.tidySingleCellExperiment) export(group_by) +export(group_by.SingleCellExperiment) export(inner_join) +export(inner_join.SingleCellExperiment) export(join_features) export(join_transcripts) export(left_join) +export(left_join.SingleCellExperiment) export(mutate) +export(mutate.SingleCellExperiment) export(nest) +export(nest.SingleCellExperiment) export(pivot_longer) +export(pivot_longer.SingleCellExperiment) export(plot_ly) export(pull) +export(pull.SingleCellExperiment) export(rename) +export(rename.SingleCellExperiment) export(right_join) +export(right_join.SingleCellExperiment) export(rowwise) +export(rowwise.SingleCellExperiment) export(sample_frac) +export(sample_frac.SingleCellExperiment) export(sample_n) +export(sample_n.SingleCellExperiment) export(select) +export(select.SingleCellExperiment) export(separate) +export(separate.SingleCellExperiment) export(slice) +export(slice.SingleCellExperiment) export(summarise) +export(summarise.SingleCellExperiment) export(tidy) export(unite) +export(unite.SingleCellExperiment) export(unnest) +export(unnest.tidySingleCellExperiment_nested) export(unnest_single_cell_experiment) importFrom(S4Vectors,DataFrame) importFrom(SingleCellExperiment,cbind) @@ -147,7 +145,6 @@ importFrom(tidyr,separate) importFrom(tidyr,spread) importFrom(tidyr,unite) importFrom(tidyr,unnest) -importFrom(tidyselect,contains) importFrom(tidyselect,eval_select) -importFrom(tidyselect,everything) +importFrom(ttservice,join_features) importFrom(utils,tail) diff --git a/R/methods.R b/R/methods.R index b1d35c1..c0ecfb8 100755 --- a/R/methods.R +++ b/R/methods.R @@ -52,89 +52,82 @@ setMethod( } ) -#' Add differential featureion information to a tbl using edgeR. +#' Extract and join information for features. #' -#' \lifecycle{experimental} #' -#' @description join_features() extracts and joins information for specific -#' features +#' @description join_features() extracts and joins information for specified features #' #' @importFrom rlang enquo #' @importFrom magrittr "%>%" +#' @importFrom ttservice join_features #' #' @name join_features #' @rdname join_features #' -#' @param .data A tidy SingleCellExperiment object +#' @param .data A SingleCellExperiment object #' @param features A vector of feature identifiers to join #' @param all If TRUE return all #' @param exclude_zeros If TRUE exclude zero values #' @param shape Format of the returned table "long" or "wide" #' @param ... Parameters to pass to join wide, i.e. assay name to extract feature abundance from and gene prefix, for shape="wide" #' -#' @details This function extracts information for specified features and -#' returns the information in either long or wide format. +#' @details This function extracts information for specified features and returns the information in either long or wide format. #' -#' @return A `tbl` containing the information.for the specified features +#' @return An object containing the information.for the specified features #' #' @examples #' -#' tidySingleCellExperiment::pbmc_small %>% +#' data("pbmc_small") +#' pbmc_small %>% +#' join_features(features = c("HLA-DRA", "LYZ")) #' -#' join_features(features=c("HLA-DRA", "LYZ")) -#' @export #' -join_features <- function(.data, - features = NULL, - all = FALSE, - exclude_zeros = FALSE, - shape = "long", ...) { - UseMethod("join_features", .data) -} -#' @export -join_features.default <- - function(.data, - features = NULL, - all = FALSE, - exclude_zeros = FALSE, - shape = "long", ...) { - print("This function cannot be applied to this object") - } -#' @importFrom tidyselect contains -#' @importFrom tidyselect everything #' @export -join_features.SingleCellExperiment <- - function(.data, - features = NULL, - all = FALSE, - exclude_zeros = FALSE, - shape = "long", ...) { +#' +NULL + +#' join_features +#' +#' @docType methods +#' @rdname join_features +#' +#' @return An object containing the information.for the specified features +#' +setMethod("join_features", "SingleCellExperiment", function(.data, + features = NULL, + all = FALSE, + exclude_zeros = FALSE, + shape = "long", ...) +{ - # CRAN Note - cell = NULL - feature= NULL + # CRAN Note + cell = NULL + feature= NULL + + # Shape is long + if (shape == "long") + .data %>% + left_join( + get_abundance_sc_long( + .data = .data, + features = features, + all = all, + exclude_zeros = exclude_zeros + ), + by = "cell" + ) %>% + select(cell, feature, contains("abundance"), everything()) + + # Shape if wide + else + .data %>% left_join(get_abundance_sc_wide( + .data = .data, + features = features, + all = all, ... + ), + by = "cell") + +}) - # Shape is long - if (shape == "long") - .data %>% - left_join( - get_abundance_sc_long( - .data = .data, - features = features, - all = all, - exclude_zeros = exclude_zeros - ), - by = "cell" - ) %>% - select(cell, feature, contains("abundance"), everything()) - # Shape if wide - else - .data %>% left_join(get_abundance_sc_wide( - .data = .data, - features = features, - all = all, ... - ), - by = "cell") - } diff --git a/man/join_features.Rd b/man/join_features.Rd index c1c4fb3..3463178 100644 --- a/man/join_features.Rd +++ b/man/join_features.Rd @@ -1,10 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/methods.R +\docType{methods} \name{join_features} \alias{join_features} -\title{Add differential featureion information to a tbl using edgeR.} +\alias{join_features,SingleCellExperiment-method} +\title{Extract and join information for features.} \usage{ -join_features( +\S4method{join_features}{SingleCellExperiment}( .data, features = NULL, all = FALSE, @@ -14,7 +16,7 @@ join_features( ) } \arguments{ -\item{.data}{A tidy SingleCellExperiment object} +\item{.data}{A SingleCellExperiment object} \item{features}{A vector of feature identifiers to join} @@ -27,21 +29,21 @@ join_features( \item{...}{Parameters to pass to join wide, i.e. assay name to extract feature abundance from and gene prefix, for shape="wide"} } \value{ -A \code{tbl} containing the information.for the specified features +An object containing the information.for the specified features + +An object containing the information.for the specified features } \description{ -join_features() extracts and joins information for specific -features +join_features() extracts and joins information for specified features } \details{ -\lifecycle{experimental} - -This function extracts information for specified features and -returns the information in either long or wide format. +This function extracts information for specified features and returns the information in either long or wide format. } \examples{ -tidySingleCellExperiment::pbmc_small \%>\% +data("pbmc_small") +pbmc_small \%>\% +join_features(features = c("HLA-DRA", "LYZ")) + - join_features(features=c("HLA-DRA", "LYZ")) } From cc53d23f50ef16f2dd00e885a34407264495f6c1 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Sat, 23 Oct 2021 20:00:36 +1100 Subject: [PATCH 14/17] update NAMESPACE --- NAMESPACE | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 20ba739..b65a854 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,79 +2,79 @@ S3method(add_count,SingleCellExperiment) S3method(add_count,default) +S3method(arrange,SingleCellExperiment) +S3method(as_tibble,SingleCellExperiment) S3method(bind_cols,SingleCellExperiment) S3method(bind_cols,default) S3method(bind_rows,SingleCellExperiment) S3method(bind_rows,default) S3method(count,SingleCellExperiment) S3method(count,default) +S3method(distinct,SingleCellExperiment) +S3method(extract,SingleCellExperiment) +S3method(filter,SingleCellExperiment) +S3method(full_join,SingleCellExperiment) +S3method(ggplot,SingleCellExperiment) +S3method(glimpse,tidySingleCellExperiment) +S3method(group_by,SingleCellExperiment) +S3method(inner_join,SingleCellExperiment) S3method(join_transcripts,Seurat) S3method(join_transcripts,default) +S3method(left_join,SingleCellExperiment) +S3method(mutate,SingleCellExperiment) +S3method(nest,SingleCellExperiment) +S3method(pivot_longer,SingleCellExperiment) S3method(plot_ly,SingleCellExperiment) S3method(plot_ly,default) S3method(print,SingleCellExperiment) +S3method(pull,SingleCellExperiment) +S3method(rename,SingleCellExperiment) +S3method(right_join,SingleCellExperiment) +S3method(rowwise,SingleCellExperiment) +S3method(sample_frac,SingleCellExperiment) +S3method(sample_n,SingleCellExperiment) +S3method(select,SingleCellExperiment) +S3method(separate,SingleCellExperiment) +S3method(slice,SingleCellExperiment) +S3method(summarise,SingleCellExperiment) S3method(tidy,SingleCellExperiment) +S3method(unite,SingleCellExperiment) +S3method(unnest,tidySingleCellExperiment_nested) export("%>%") export(add_count) export(arrange) -export(arrange.SingleCellExperiment) export(as_tibble) -export(as_tibble.SingleCellExperiment) export(bind_cols) export(bind_rows) export(count) export(distinct) -export(distinct.SingleCellExperiment) export(extract) -export(extract.SingleCellExperiment) export(filter) -export(filter.SingleCellExperiment) export(full_join) -export(full_join.SingleCellExperiment) export(ggplot) -export(ggplot.SingleCellExperiment) export(glimpse) -export(glimpse.tidySingleCellExperiment) export(group_by) -export(group_by.SingleCellExperiment) export(inner_join) -export(inner_join.SingleCellExperiment) export(join_features) export(join_transcripts) export(left_join) -export(left_join.SingleCellExperiment) export(mutate) -export(mutate.SingleCellExperiment) export(nest) -export(nest.SingleCellExperiment) export(pivot_longer) -export(pivot_longer.SingleCellExperiment) export(plot_ly) export(pull) -export(pull.SingleCellExperiment) export(rename) -export(rename.SingleCellExperiment) export(right_join) -export(right_join.SingleCellExperiment) export(rowwise) -export(rowwise.SingleCellExperiment) export(sample_frac) -export(sample_frac.SingleCellExperiment) export(sample_n) -export(sample_n.SingleCellExperiment) export(select) -export(select.SingleCellExperiment) export(separate) -export(separate.SingleCellExperiment) export(slice) -export(slice.SingleCellExperiment) export(summarise) -export(summarise.SingleCellExperiment) export(tidy) export(unite) -export(unite.SingleCellExperiment) export(unnest) -export(unnest.tidySingleCellExperiment_nested) export(unnest_single_cell_experiment) importFrom(S4Vectors,DataFrame) importFrom(SingleCellExperiment,cbind) From 47df266699ffd4d9ca4df709f7fe0f481fabf4cb Mon Sep 17 00:00:00 2001 From: stemangiola Date: Fri, 28 Jan 2022 23:48:38 +1100 Subject: [PATCH 15/17] test --- man/print.Rd | 27 --------------------------- tests/testthat/test-dplyr_methods.R | 2 -- tests/testthat/test-methods.R | 15 +++++++++++++++ 3 files changed, 15 insertions(+), 29 deletions(-) create mode 100644 tests/testthat/test-methods.R diff --git a/man/print.Rd b/man/print.Rd index 85ea70f..53d4817 100644 --- a/man/print.Rd +++ b/man/print.Rd @@ -62,33 +62,6 @@ exceeded. Default: 10. \item \code{tibble.max_extra_cols}: Number of extra columns printed in reduced form. Default: 100. } - - - -\itemize{ -\item \code{pillar.bold}: Use bold font, e.g. for column headers? This currently -defaults to \code{FALSE}, because many terminal fonts have poor support for -bold fonts. -\item \code{pillar.subtle}: Use subtle style, e.g. for row numbers and data types? -Default: \code{TRUE}. -\item \code{pillar.subtle_num}: Use subtle style for insignificant digits? Default: -\code{FALSE}, is also affected by the \code{pillar.subtle} option. -\item \code{pillar.neg}: Highlight negative numbers? Default: \code{TRUE}. -\item \code{pillar.sigfig}: The number of significant digits that will be printed and -highlighted, default: \code{3}. Set the \code{pillar.subtle} option to \code{FALSE} to -turn off highlighting of significant digits. -\item \code{pillar.min_title_chars}: The minimum number of characters for the column -title, default: \code{15}. Column titles may be truncated up to that width to -save horizontal space. Set to \code{Inf} to turn off truncation of column -titles. -\item \code{pillar.min_chars}: The minimum number of characters wide to -display character columns, default: \code{0}. Character columns may be -truncated up to that width to save horizontal space. Set to \code{Inf} to -turn off truncation of character columns. -\item \code{pillar.max_dec_width}: The maximum allowed width for decimal notation, -default 13. -} - } \examples{ diff --git a/tests/testthat/test-dplyr_methods.R b/tests/testthat/test-dplyr_methods.R index afdf184..db3204e 100755 --- a/tests/testthat/test-dplyr_methods.R +++ b/tests/testthat/test-dplyr_methods.R @@ -2,8 +2,6 @@ context("dplyr test") library(magrittr) -pbmc_small <- pbmc_small - test_that("arrange", { tt_pca_aranged <- pbmc_small %>% diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R new file mode 100644 index 0000000..dcc255a --- /dev/null +++ b/tests/testthat/test-methods.R @@ -0,0 +1,15 @@ +context('methods test') + +data("pbmc_small") + +test_that("join_features",{ + + + pbmc_small |> + join_features("CD3D") |> + slice(1) |> + tidySingleCellExperiment::pull(abundance_counts) |> + expect_equal(4, tolerance=0.1) + + +}) From 0f816a255a2b357c03606cd50c1849037ac6e4d6 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Sat, 29 Jan 2022 00:09:30 +1100 Subject: [PATCH 16/17] fix |> bug --- tests/testthat/test-methods.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index dcc255a..9149341 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -1,14 +1,14 @@ context('methods test') data("pbmc_small") - +library(dplyr) test_that("join_features",{ - pbmc_small |> - join_features("CD3D") |> - slice(1) |> - tidySingleCellExperiment::pull(abundance_counts) |> + pbmc_small %>% + join_features("CD3D") %>% + slice(1) %>% + tidySingleCellExperiment::pull(abundance_counts) %>% expect_equal(4, tolerance=0.1) From 5190f6243800d9975e62311b16a27509ea780b73 Mon Sep 17 00:00:00 2001 From: stemangiola Date: Sat, 29 Jan 2022 04:52:23 +1100 Subject: [PATCH 17/17] fix test --- tests/testthat/test-methods.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index 9149341..c7fb84a 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -8,7 +8,7 @@ test_that("join_features",{ pbmc_small %>% join_features("CD3D") %>% slice(1) %>% - tidySingleCellExperiment::pull(abundance_counts) %>% + tidySingleCellExperiment::pull(.abundance_counts) %>% expect_equal(4, tolerance=0.1)