From d2e027e48d1a4bec3e1a3e2831fe1a3c3746bc8c Mon Sep 17 00:00:00 2001 From: jiajic <72078254+jiajic@users.noreply.github.com> Date: Fri, 20 Oct 2023 12:50:52 -0400 Subject: [PATCH] update: sankey plotting - param reorganization - allow sankey plotting directly from a data.frame of relationships --- R/plot_sankey.R | 56 ++++++++++++++++++++++++++++++++++++----------- man/sankeyPlot.Rd | 15 +++++++++---- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/R/plot_sankey.R b/R/plot_sankey.R index 8d53320..ffbc2cc 100644 --- a/R/plot_sankey.R +++ b/R/plot_sankey.R @@ -50,7 +50,7 @@ setMethod('show', signature = 'giottoSankeyPlan', function(object) { # generics #### setGeneric('sankeyRelate', function(x, ...) standardGeneric('sankeyRelate')) setGeneric('sankeyRelate<-', function(x, add, value) standardGeneric('sankeyRelate<-')) -setGeneric('sankeyPlot', function(gobject, x, ...) standardGeneric('sankeyPlot')) +setGeneric('sankeyPlot', function(x, y, ...) standardGeneric('sankeyPlot')) # methods #### @@ -447,8 +447,9 @@ sankey_relation_pair = function(g, gsp, rel_idx, node_idx_start = 0) { #' and (optionally) `idx` params. More complex and cross spatial unit/feature #' type sankeys can be set up using the `sankey_plan` param which accepts a #' `giottoSankeyPlan` object. -#' @inheritParams data_access_params -#' @param x giottoSankeyPlan object or character vector referring to source and +#' @param x data source (gobject or data.frame-like object with relations +#' between the first two cols provided) +#' @param y giottoSankeyPlan object or character vector referring to source and #' target columns in metadata #' @param meta_type build sankey on cell or feature metadata #' @param spat_unit spatial unit of metadata @@ -458,6 +459,9 @@ sankey_relation_pair = function(g, gsp, rel_idx, node_idx_start = 0) { #' @inheritDotParams networkD3::sankeyNetwork -Links -Nodes -Source -Target -Value -NodeID #' @examples #' \dontrun{ +#' x = data.table::data.table(col1 = c('a', 'a', 'b'), +#' col2 = c('x', 'y', 'y')) +#' sankeyPlot(x) #' g = GiottoData::loadGiottoMini("vizgen") #' # with giottoSankeyPlan #' leiden = sankeySet(spat_unit = 'aggregate', @@ -483,13 +487,12 @@ sankey_relation_pair = function(g, gsp, rel_idx, node_idx_start = 0) { #' @export setMethod( 'sankeyPlot', - signature(gobject = 'giotto', - x = 'giottoSankeyPlan'), - function(gobject, - x, + signature(x = 'giotto', + y = 'giottoSankeyPlan'), + function(x, + y, meta_type = c('cell', 'feat'), ...) { - checkmate::assert_class(gobject, 'giotto') GiottoUtils::package_check("networkD3") meta_type = match.arg(meta_type, choices = c('cell', 'feat')) x@data_type = meta_type @@ -537,17 +540,18 @@ setMethod( #' @export setMethod( 'sankeyPlot', - signature(gobject = 'giotto', - x = 'character'), - function(gobject, - x, + signature(x = 'giotto', + y = 'character'), + function(x, + y, spat_unit = NULL, feat_type = NULL, meta_type = c('cell', 'feat'), idx = NULL, ...) { - checkmate::assert_character(x, len = 2L) + GiottoUtils::package_check("networkD3") + checkmate::assert_character(y, len = 2L) # Data type being compared. Either cell or feat meta_type = match.arg(meta_type, choices = c('cell', 'feat')) @@ -601,3 +605,29 @@ setMethod( } ) + +#' @rdname sankeyPlot +#' @export +setMethod('sankeyPlot', signature(x = 'data.frame', y = 'missing'), function(x, ...) { + GiottoUtils::package_check("networkD3") + + res = sankey_compare(data_dt = x) + links_dt = res$links + + # create nodes table + nodes = data.table::data.table(name = res$nodes) + + networkD3::sankeyNetwork( + Links = links_dt, + Nodes = nodes, + Source = 'source', + Target = 'target', + Value = 'value', + NodeID = 'name', + ... + ) + +}) + + + diff --git a/man/sankeyPlot.Rd b/man/sankeyPlot.Rd index 528679f..9dee432 100644 --- a/man/sankeyPlot.Rd +++ b/man/sankeyPlot.Rd @@ -3,24 +3,28 @@ \name{sankeyPlot} \alias{sankeyPlot} \alias{sankeyPlot,giotto,character-method} +\alias{sankeyPlot,data.frame,missing-method} \title{Create a sankey plot} \usage{ -\S4method{sankeyPlot}{giotto,giottoSankeyPlan}(gobject, x, meta_type = c("cell", "feat"), ...) +\S4method{sankeyPlot}{giotto,giottoSankeyPlan}(x, y, meta_type = c("cell", "feat"), ...) \S4method{sankeyPlot}{giotto,character}( - gobject, x, + y, spat_unit = NULL, feat_type = NULL, meta_type = c("cell", "feat"), idx = NULL, ... ) + +\S4method{sankeyPlot}{data.frame,missing}(x, y, ...) } \arguments{ -\item{gobject}{giotto object} +\item{x}{data source (gobject or data.frame-like object with relations +between the first two cols provided)} -\item{x}{giottoSankeyPlan object or character vector referring to source and +\item{y}{giottoSankeyPlan object or character vector referring to source and target columns in metadata} \item{meta_type}{whether to use 'cell' (cell) or 'feat' (feature) metadata} @@ -71,6 +75,9 @@ type sankeys can be set up using the \code{sankey_plan} param which accepts a } \examples{ \dontrun{ +x = data.table::data.table(col1 = c('a', 'a', 'b'), + col2 = c('x', 'y', 'y')) +sankeyPlot(x) g = GiottoData::loadGiottoMini("vizgen") # with giottoSankeyPlan leiden = sankeySet(spat_unit = 'aggregate',