Skip to content

Commit

Permalink
update: sankey plotting
Browse files Browse the repository at this point in the history
- param reorganization
- allow sankey plotting directly from a data.frame of relationships
  • Loading branch information
jiajic committed Oct 20, 2023
1 parent b2f50f4 commit d2e027e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
56 changes: 43 additions & 13 deletions R/plot_sankey.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ####
Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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',
...
)

})



15 changes: 11 additions & 4 deletions man/sankeyPlot.Rd

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

0 comments on commit d2e027e

Please sign in to comment.