Skip to content

Instantly share code, notes, and snippets.

@cannin
Last active August 29, 2015 14:02
Show Gist options
  • Save cannin/bcc2d73e1e345b1540d1 to your computer and use it in GitHub Desktop.
Save cannin/bcc2d73e1e345b1540d1 to your computer and use it in GitHub Desktop.
An example of programmatically coloring nodes/edges of an existing Cytoscape network with R
# An example of programmatically coloring nodes/edges of an existing Cytoscape network with R
library(RCytoscape)
library(RColorBrewer)
# Generate demo Cytoscape network
cy <- CytoscapeConnection()
cw <- new.CytoscapeWindow("demo", graph=makeSimpleGraph(), overwriteWindow=TRUE)
displayGraph(cw)
layoutNetwork(cw)
redraw(cw)
# Get existing Cytoscape network
cw <- existing.CytoscapeWindow("demo", copy.graph.from.cytoscape.to.R=TRUE)
# Get nodes/edges and their attributes
edges <- getAllEdgeAttributes(cw, onlySelectedEdges=FALSE)
nodes <- getAllNodeAttributes (cw, onlySelectedNodes=FALSE)
# Color Edges
edgeValues = unique(edges$edgeType)
# Generate color palette
colors <- brewer.pal(length(edgeValues), "Set3")
# NOTE: This function exists for nodes and edges
setEdgeColorRule(cw, "edgeType", edgeValues, colors, mode="lookup")
# Color Nodes
nodeValues = unique(nodes$lfc)
colors <- brewer.pal(length(nodeValues), "YlOrRd")
for(i in seq_along(nodeValues)) {
# NOTE: This function exists for nodes and edges
setNodeColorDirect (cw, rownames(nodes)[i], colors[i])
}
redraw(cw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment