Skip to content

Commit

Permalink
Merge pull request stemangiola#68 from stemangiola/drop-bind-generics
Browse files Browse the repository at this point in the history
Drop bind generics
  • Loading branch information
stemangiola authored Jun 20, 2023
2 parents 1cce7e5 + e976f6b commit 4964d34
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 81 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description: tidySingleCellExperiment is an adapter that abstracts the 'SingleCe
License: GPL-3
Depends:
R (>= 4.1.0),
ttservice,
ttservice (>= 0.3.6),
SingleCellExperiment
Imports:
SummarizedExperiment,
Expand All @@ -32,7 +32,8 @@ Imports:
stringr,
cli,
fansi,
Matrix
Matrix,
stats
Suggests:
BiocStyle,
testthat,
Expand All @@ -47,8 +48,7 @@ Suggests:
GGally,
uwot,
celldex,
dittoSeq,
EnsDb.Hsapiens.v86
dittoSeq
VignetteBuilder:
knitr
RdMacros:
Expand Down
11 changes: 7 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ S3method(add_count,SingleCellExperiment)
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(distinct,SingleCellExperiment)
S3method(extract,SingleCellExperiment)
Expand Down Expand Up @@ -78,8 +76,6 @@ export(tidy)
export(unite)
export(unnest)
export(unnest_single_cell_experiment)
import(dplyr)
import(tidyr)
importFrom(Matrix,rowSums)
importFrom(S4Vectors,"metadata<-")
importFrom(S4Vectors,DataFrame)
Expand All @@ -92,9 +88,11 @@ importFrom(SummarizedExperiment,colData)
importFrom(cli,cat_line)
importFrom(dplyr,add_count)
importFrom(dplyr,arrange)
importFrom(dplyr,contains)
importFrom(dplyr,count)
importFrom(dplyr,distinct)
importFrom(dplyr,distinct_at)
importFrom(dplyr,everything)
importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by)
Expand All @@ -112,6 +110,7 @@ importFrom(dplyr,select)
importFrom(dplyr,select_if)
importFrom(dplyr,slice)
importFrom(dplyr,summarise)
importFrom(dplyr,vars)
importFrom(ellipsis,check_dots_unnamed)
importFrom(ellipsis,check_dots_used)
importFrom(fansi,strwrap_ctl)
Expand All @@ -123,6 +122,7 @@ importFrom(magrittr,"%>%")
importFrom(magrittr,equals)
importFrom(magrittr,set_rownames)
importFrom(methods,as)
importFrom(methods,getMethod)
importFrom(pillar,align)
importFrom(pillar,get_extent)
importFrom(pillar,style_subtle)
Expand All @@ -149,6 +149,7 @@ importFrom(rlang,quo_is_symbol)
importFrom(rlang,quo_is_symbolic)
importFrom(rlang,quo_name)
importFrom(rlang,quo_squash)
importFrom(stats,setNames)
importFrom(stringr,regex)
importFrom(stringr,str_detect)
importFrom(stringr,str_replace)
Expand All @@ -165,6 +166,8 @@ importFrom(tidyr,unite)
importFrom(tidyr,unnest)
importFrom(tidyselect,eval_select)
importFrom(ttservice,aggregate_cells)
importFrom(ttservice,bind_cols)
importFrom(ttservice,bind_rows)
importFrom(ttservice,join_features)
importFrom(utils,data)
importFrom(utils,tail)
Expand Down
50 changes: 17 additions & 33 deletions R/dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ arrange.SingleCellExperiment <- function(.data, ..., .by_group=FALSE) {
#'
#' When column-binding, rows are matched by position, so all data
#' frames must have the same number of rows. To match by value, not
#' position, see [mutate-joins].
#' position, see mutate-joins.
#' @param .id Data frame identifier.
#'
#' When `.id` is supplied, a new column of identifiers is
Expand All @@ -96,36 +96,24 @@ arrange.SingleCellExperiment <- function(.data, ..., .by_group=FALSE) {
#' list of data frames is supplied, the labels are taken from the
#' names of the list. If no names are found a numeric sequence is
#' used instead.
#' @param add.cell.ids from SingleCellExperiment 3.0 A character vector of
#' length(x=c(x, y)). Appends the corresponding values to the start of each
#' objects' cell names.
#' @param add.cell.ids from Seurat 3.0 A character vector of length(x = c(x, y)). Appends the corresponding values to the start of each objects' cell names.
#'
#' @importFrom ttservice bind_rows
#'
#' @return `bind_rows()` and `bind_cols()` return the same type as
#' the first input, either a data frame, `tbl_df`, or `grouped_df`.
#' @examples
#' `%>%` <- magrittr::`%>%`
#' tt <- pbmc_small
#' bind_rows(tt, tt)
#' `%>%` = magrittr::`%>%`
#' tt = pbmc_small
#' bind_rows( tt, tt )
#'
#' tt_bind <- tt %>% select(nCount_RNA, nFeature_RNA)
#' tt_bind = tt %>% select(nCount_RNA ,nFeature_RNA)
#' tt %>% bind_cols(tt_bind)
#' @name bind
NULL

#' @rdname dplyr-methods
#'
#' @inheritParams bind
#'
#' @export
#'
bind_rows <- function(..., .id=NULL, add.cell.ids=NULL) {
UseMethod("bind_rows")
}

#' @export
bind_rows.default <- function(..., .id=NULL, add.cell.ids=NULL) {
dplyr::bind_rows(..., .id=.id)
}
#'
#' @name bind_rows
NULL

#' @importFrom rlang dots_values
#' @importFrom rlang flatten_if
Expand Down Expand Up @@ -155,25 +143,21 @@ bind_rows.SingleCellExperiment <- function(..., .id=NULL, add.cell.ids=NULL) {
bind_cols_ = function(..., .id=NULL) {
tts <- tts <- flatten_if(dots_values(...), is_spliced)

colData(tts[[1]]) <- dplyr::bind_cols(colData(tts[[1]]) %>% as.data.frame(),
colData(tts[[1]]) <- bind_cols(colData(tts[[1]]) %>% as.data.frame(),
tts[[2]], .id=.id) %>% DataFrame()

tts[[1]]
}

#' @export
#'
#' @inheritParams bind
#' @importFrom ttservice bind_cols
#' @inheritParams bind_cols
#'
#' @name bind_cols
#'
#' @rdname dplyr-methods
bind_cols <- function(..., .id=NULL) {
UseMethod("bind_cols")
}

#' @export
bind_cols.default <- function(..., .id=NULL) {
dplyr::bind_cols(..., .id=.id)
}
NULL

#' @importFrom rlang dots_values
#' @importFrom rlang flatten_if
Expand Down
9 changes: 8 additions & 1 deletion R/methods.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#' @importFrom methods getMethod
setMethod(
f = "show",
signature = "SingleCellExperiment",
Expand Down Expand Up @@ -55,6 +56,9 @@ NULL

#' join_features
#'
#' @importFrom dplyr contains
#' @importFrom dplyr everything
#'
#' @docType methods
#' @rdname join_features
#'
Expand Down Expand Up @@ -173,6 +177,9 @@ setMethod("aggregate_cells", "SingleCellExperiment", function(.data,
assays = NULL,
aggregation_function = Matrix::rowSums){

# Fix NOTEs
feature = NULL

.sample = enquo(.sample)

# Subset only wanted assays
Expand All @@ -192,7 +199,7 @@ setMethod("aggregate_cells", "SingleCellExperiment", function(.data,

# Get counts
~ .x %>%
aggregation_function(na.rm = T) %>%
aggregation_function(na.rm = TRUE) %>%
enframe(
name = "feature",
value = sprintf("%s", .y)
Expand Down
2 changes: 1 addition & 1 deletion R/tibble_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ as_tibble.SingleCellExperiment <- function(x, ...,
when(

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

Expand Down
22 changes: 15 additions & 7 deletions R/tidybulk_utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#' @keywords internal
#' @noRd
#'
#' @import dplyr
#' @import tidyr
#' @importFrom purrr as_mapper
#' @importFrom magrittr equals
#'
Expand Down Expand Up @@ -47,6 +45,10 @@ as_SummarizedExperiment = function(.data,
.transcript = NULL,
.abundance = NULL) {

# Fix NOTES
. = NULL
assay = NULL

# Get column names
.sample = enquo(.sample)
.transcript = enquo(.transcript)
Expand Down Expand Up @@ -167,23 +169,27 @@ get_sample_transcript_counts = function(.data, .sample, .transcript, .abundance)
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()
else stop()

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()
else stop()

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()
else stop()

list(.sample = .sample, .transcript = .transcript, .abundance = .abundance)

}

get_tt_columns = function(.data){

# Fix NOTES
tt_columns = NULL

if(
.data %>% attr("internals") %>% is.list() &&
"tt_columns" %in% names(.data %>% attr("internals"))
Expand Down Expand Up @@ -313,8 +319,6 @@ get_x_y_annotation_columns = function(.data, .horizontal, .vertical, .abundance,
#' @keywords internal
#' @noRd
#'
#' @import dplyr
#' @import tidyr
#' @importFrom magrittr set_rownames
#' @importFrom rlang quo_is_null
#'
Expand All @@ -330,6 +334,10 @@ get_x_y_annotation_columns = function(.data, .horizontal, .vertical, .abundance,
as_matrix <- function(tbl,
rownames = NULL,
do_check = TRUE) {

# Fix NOTEs
variable = NULL

rownames = enquo(rownames)
tbl %>%

Expand Down
4 changes: 3 additions & 1 deletion R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ drop_class <- function(var, name) {
#' @importFrom magrittr "%$%"
#' @importFrom utils tail
#' @importFrom SummarizedExperiment assays
#' @importFrom stats setNames
#'
#' @param .data A tidySingleCellExperiment
#' @param features A character
Expand Down Expand Up @@ -434,7 +435,7 @@ special_datasets_to_tibble = function(.singleCellExperiment, ...){
# Otherwise continue normally
~ as_tibble(.)
)) %>%
reduce(dplyr::bind_cols)
reduce(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()
Expand All @@ -456,6 +457,7 @@ trick_to_avoid_renaming_of_already_unique_columns_by_dplyr = function(x){
#' @importFrom purrr map
#' @importFrom dplyr distinct_at
#' @importFrom magrittr equals
#' @importFrom dplyr vars
#'
#' @param .data A tibble
#' @param .col A vector of column names
Expand Down
19 changes: 9 additions & 10 deletions man/bind.Rd → man/bind_rows.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4964d34

Please sign in to comment.