cytoscape / cyjShiny

An R/shiny widget for cytoscape.js
Other
92 stars 28 forks source link

support data.frame (and other) graph data structures; convert to cyjs JSON #12

Closed paul-shannon closed 3 years ago

paul-shannon commented 5 years ago

@erickfvelasquez requests support for data.frame graph representations. This will appear in R/graphsToJSON.R which now only handles graphNELs. igraph support will come.

erickfvelasquez commented 5 years ago

@paul-shannon

Convert R dataframes objects to cytoscape.js JSON

Takes in nodeData and edgeData dataframes with ("id", "name") and ("source", "target") columns respectively

nodeShepe, nodeHeight, nodeWidth, nodeLabelColor, edgeColor, edgeSourceShape, edgeTargetgetShape, and nodeHref are preset but can be changed if desired

returns a list of lists with nodes and edge information

createcyjNetwork <- function (nodeData, edgeData, nodeColor = "#888888", nodeShape = "ellipse", nodeHeight = "70", nodeWidth = "70", nodeLabelColor = "#FFFFFF", edgeColor = "#888888", edgeSourceShape = "none", edgeTargetShape = "triangle", nodeHref = "") { if (nrow(nodeData) == 0 || !(all(c("id", "name") %in% names(nodeData)))) { stop("ERROR: nodeData must have 'id' and 'name' columns") } if (nrow(edgeData) == 0 || !(all(c("source", "target") %in% names(edgeData)))) { stop("ERROR: edgeData must have 'source' and 'target' columns") } if (!("color" %in% colnames(nodeData))) { nodeData$color <- rep(nodeColor, nrow(nodeData)) } if (!("shape" %in% colnames(nodeData))) { nodeData$shape <- rep(nodeShape, nrow(nodeData)) } if (!("href" %in% colnames(nodeData))) { nodeData$href <- rep(nodeHref, nrow(nodeData)) } if (!("height" %in% colnames(nodeData))) { nodeData$height <- rep(nodeHeight, nrow(nodeData)) } if (!("width" %in% colnames(nodeData))) { nodeData$width <- rep(nodeWidth, nrow(nodeData)) } if (!("nodeLabelColor" %in% colnames(nodeData))) { nodeData$nodeLabelColor <- rep(nodeLabelColor, nrow(nodeData)) } rownames(nodeData) <- NULL nodeEntries <- apply(nodeData, 1, function(x) { list(data = as.list(x)) }) if (!("color" %in% colnames(edgeData))) { edgeData$color <- rep(edgeColor, nrow(edgeData)) } if (!("sourceShape" %in% colnames(edgeData))) { edgeData$edgeSourceShape <- rep(edgeSourceShape, nrow(edgeData)) } if (!("targetShape" %in% colnames(edgeData))) { edgeData$edgeTargetShape <- rep(edgeTargetShape, nrow(edgeData)) } rownames(edgeData) <- NULL edgeEntries <- apply(edgeData, 1, function(x) { list(data = as.list(x)) }) network <- list(nodes = nodeEntries, edges = edgeEntries) return(network) }

cyjShinyRender <- function (nodeEntries, edgeEntries, layout = "cose", width = NULL, height = NULL, showPanzoom = TRUE, highlightConnectedNodes = TRUE, boxSelectionEnabled = TRUE){ x = list() x$nodeEntries <- nodeEntries x$edgeEntries <- edgeEntries x$layout <- layout x$showPanzoom <- showPanzoom x$highlightConnectedNodes <- highlightConnectedNodes x$boxSelectionEnabled <- boxSelectionEnabled htmlwidgets::createWidget(name = 'cyjShiny', x, width = width, height = height, package = 'cyjShiny') }

paul-shannon commented 5 years ago

I think I see. (An introduction to this comment would have been helpful!).

I propose a different strategy:

1) multiple representations of graphs (networks) in R are supported a) graphNEL from the bioc graph package b) data.frames in your preferred style c) (soon) the igraph data structure

2) cyjShiny, in the server side R code converts each of these to a proper cytoscape.js JSON form

3) styling of the displayed graph, both nodes and edges, is accomplished via the traditional cyjs method: a separate style file, in which colors, shapes, sizes, labels, borders & etc are all controlled by node & edge data attributes.

So the code will soon include, in graphsToJSON.R

grapToJSON data.framesToJSON igraphToJSON

Where the data.frames to support might be 2:

1) nodes and their attributes 2) edges and their attributes

A minimal graph can be specified with only an edge data.frame; that will be supported - your example protein-protein interaction is one of those I believe. For more detailed graphs, both data.frames are needed.

We want, whenever possible, to find consistency and inter-operability across the cytoscape ecosystem. In that spirit, see:

http://bioconductor.org/packages/release/bioc/vignettes/RCy3/inst/doc/Overview-of-RCy3.html#4_my_first_network

Make sense? I hope I do not seem dictatorial!

On Sep 12, 2018, at 2:21 PM, erickfvelasquez notifications@github.com wrote:

@paul-shannon

Convert R dataframes objects to cytoscape.js JSON

Takes in nodeData and edgeData dataframes with ("id", "name") and ("source", "target") columns respectively

nodeShepe, nodeHeight, nodeWidth, nodeLabelColor, edgeColor, edgeSourceShape, edgeTargetgetShape, and nodeHref are preset but can be changed if desired

returns a list of lists with nodes and edge information

createcyjNetwork <- function (nodeData, edgeData, nodeColor = "#888888", nodeShape = "ellipse", nodeHeight = "70", nodeWidth = "70", nodeLabelColor = "#FFFFFF", edgeColor = "#888888", edgeSourceShape = "none", edgeTargetShape = "triangle", nodeHref = "") { if (nrow(nodeData) == 0 || !(all(c("id", "name") %in% names(nodeData)))) { stop("ERROR: nodeData must have 'id' and 'name' columns") } if (nrow(edgeData) == 0 || !(all(c("source", "target") %in% names(edgeData)))) { stop("ERROR: edgeData must have 'source' and 'target' columns") } if (!("color" %in% colnames(nodeData))) { nodeData$color <- rep(nodeColor, nrow(nodeData)) } if (!("shape" %in% colnames(nodeData))) { nodeData$shape <- rep(nodeShape, nrow(nodeData)) } if (!("href" %in% colnames(nodeData))) { nodeData$href <- rep(nodeHref, nrow(nodeData)) } if (!("height" %in% colnames(nodeData))) { nodeData$height <- rep(nodeHeight, nrow(nodeData)) } if (!("width" %in% colnames(nodeData))) { nodeData$width <- rep(nodeWidth, nrow(nodeData)) } if (!("nodeLabelColor" %in% colnames(nodeData))) { nodeData$nodeLabelColor <- rep(nodeLabelColor, nrow(nodeData)) } rownames(nodeData) <- NULL nodeEntries <- apply(nodeData, 1, function(x) { list(data = as.list(x)) }) if (!("color" %in% colnames(edgeData))) { edgeData$color <- rep(edgeColor, nrow(edgeData)) } if (!("sourceShape" %in% colnames(edgeData))) { edgeData$edgeSourceShape <- rep(edgeSourceShape, nrow(edgeData)) } if (!("targetShape" %in% colnames(edgeData))) { edgeData$edgeTargetShape <- rep(edgeTargetShape, nrow(edgeData)) } rownames(edgeData) <- NULL edgeEntries <- apply(edgeData, 1, function(x) { list(data = as.list(x)) }) network <- list(nodes = nodeEntries, edges = edgeEntries) return(network) }

cyjShinyRender <- function (nodeEntries, edgeEntries, layout = "cose", width = NULL, height = NULL, showPanzoom = TRUE, highlightConnectedNodes = TRUE, boxSelectionEnabled = TRUE){ x = list() x$nodeEntries <- nodeEntries x$edgeEntries <- edgeEntries x$layout <- layout x$showPanzoom <- showPanzoom x$highlightConnectedNodes <- highlightConnectedNodes x$boxSelectionEnabled <- boxSelectionEnabled htmlwidgets::createWidget(name = 'cyjShiny', x, width = width, height = height, package = 'cyjShiny') }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

paul-shannon commented 3 years ago

solved long ago by R/ graphsToJSON.R, tested in inst/unitTests/test_graphsToJSON.R