vignettes/Upgrading-existing-scripts.Rmd
Upgrading-existing-scripts.Rmd
Cytoscape is a well-known bioinformatics tool for displaying and exploring biological networks. R is a powerful programming language and environment for statistical and exploratory data analysis. RCy3 uses CyREST to communicate between R and Cytoscape, allowing any graphs (e.g., iGraph, graphNEL or dataframes) to be viewed, explored and manipulated with the Cytoscape point-and-click visual interface. Thus, via RCy3, these two quite different, quite useful bioinformatics software environments are connected, mutually enhancing each other, providing new possibilities for exploring biological data.
if(!"RCy3" %in% installed.packages()){
install.packages("BiocManager")
BiocManager::install("RCy3")
}
library(RCy3)
In addition to this package (RCy3), you will need:
Welcome to RCy3 v2.0! If you have been using prior versions of RCy3, then this is a big step. The fundamental change is that CytoscapeConnection and CytoscapeWindow classes are no longer used. These were pervasive arguments throughout the package in prior versions, so practically every function signature has changed. However, this change simplifies functions and in 99% of cases the old cw and cc args can simply be deleted.
Some summary stats regarding the changes:
Going forward, this realignment will help us maintain the package in sync with changes to critical upstream resources such as Cytoscape’s CyREST API. It will also greatly simply script writing for users. We hope you’ll stay onboard.
We documented this upgrade in a few different ways to help you update existing scripts you don’t want to leave behind and to adopt the new conceptual model for RCy3 going forward.
Here’s a wiki page from the main RCy3 repo that we will keep updated with helpful tips. I’d recommend starting there:
It is always helpful to see examples. Here are a few of the most common chunks of RCy3 usage, provided in BEFORE and AFTER versions. If you have code similar to these in your pre-v2.0 scripts, then try these suggested swaps.
Note: don’t run the BEFORE chunks with the latest RCy3 package. These chunks are NOT meant to be run as a sequence, but rather as isolated examples.
BEFORE
g <- new ('graphNEL', edgemode='directed')
g <- graph::addNode ('A', g)
g <- graph::addNode ('B', g)
g <- graph::addNode ('C', g)
g <- graph::addEdge ('A', 'B', g)
g <- graph::addEdge ('B', 'C', g)
cw <- CytoscapeWindow ('vignette', graph=g, overwrite=TRUE)
displayGraph (cw)
layoutNetwork (cw, layout.name='grid')
AFTER
g <- new ('graphNEL', edgemode='directed')
g <- graph::addNode ('A', g)
g <- graph::addNode ('B', g)
g <- graph::addNode ('C', g)
g <- graph::addEdge ('A', 'B', g)
g <- graph::addEdge ('B', 'C', g)
net.suid <- createNetworkFromGraph (g, 'vignette')
BEFORE
node.df <- data.frame(id=c("A","B","C","D"),
stringsAsFactors=FALSE)
edge.df <- data.frame(source=c("A","A","A","C"),
target=c("B","C","D","D"),
interaction=c("inhibits","interacts","activates","interacts"), # optional
stringsAsFactors=FALSE)
g <- cyPlot(node.df, edge.df)
cw <- CytoscapeWindow("vignette2", g)
displayGraph(cw)
layoutNetwork(cw, "force-directed")
AFTER
node.df <- data.frame(id=c("A","B","C","D"),
stringsAsFactors=FALSE)
edge.df <- data.frame(source=c("A","A","A","C"),
target=c("B","C","D","D"),
interaction=c("inhibits","interacts","activates","interacts"), # optional
stringsAsFactors=FALSE)
net.suid <- createNetworkFromDataFrames (node.df, edge.df, "vignette2")
BEFORE
g <- initNodeAttribute (graph=g, attribute.name='moleculeType',
attribute.type='char',
default.value='undefined')
g <- initNodeAttribute (graph=g, 'lfc', 'numeric', 0.0)
nodeData (g, 'A', 'moleculeType') <- 'kinase'
nodeData (g, 'B', 'moleculeType') <- 'TF'
nodeData (g, 'C', 'moleculeType') <- 'cytokine'
nodeData (g, 'D', 'moleculeType') <- 'cytokine'
nodeData (g, 'A', 'lfc') <- -1.2
nodeData (g, 'B', 'lfc') <- 1.8
nodeData (g, 'C', 'lfc') <- 3.2
nodeData (g, 'D', 'lfc') <- 2.2
cw = setGraph (cw, g)
displayGraph (cw)
AFTER
node.data <- data.frame(id=c('A','B','C','D'),
moleculeType=c('kinase','TF','cytokine','cytokine'),
lfc=c(-1.2, 1.8, 3.2, 2.2),
stringsAsFactors = FALSE)
loadTableData(node.data, 'id')
BEFORE
setDefaultNodeShape (cw, 'OCTAGON')
setDefaultNodeColor (cw, '#AAFF88')
setDefaultNodeSize (cw, 80)
setDefaultNodeFontSize (cw, 40)
AFTER
setNodeShapeDefault ('OCTAGON')
setNodeColorDefault ('#AAFF88')
setNodeSizeDefault (80)
setNodeFontSizeDefault (40)
BEFORE
attribute.values <- c ('kinase', 'TF', 'cytokine')
node.shapes <- c ('DIAMOND', 'TRIANGLE', 'RECTANGLE')
setNodeShapeRule (cw, 'moleculeType', attribute.values, node.shapes)
setNodeColorRule (cw, 'lfc', c (-3.0, 0.0, 3.0),
c ('#00AA00', '#00FF00', '#FFFFFF', '#FF0000', '#AA0000'),
mode='interpolate')
control.points = c (-1.2, 2.0, 4.0)
node.sizes = c (30, 40, 60, 80, 90)
setNodeSizeRule (cw, 'lfc', control.points, node.sizes, mode='interpolate')
AFTER
attribute.values <- c ('kinase', 'TF', 'cytokine')
node.shapes <- c ('DIAMOND', 'TRIANGLE', 'RECTANGLE')
setNodeShapeMapping('moleculeType', attribute.values, node.shapes)
setNodeColorMapping('lfc', c(-3.0, 0.0, 3.0),
c('#00AA00', '#00FF00', '#FFFFFF', '#FF0000', '#AA0000'))
control.points = c (-1.2, 2.0, 4.0)
node.sizes = c (30, 40, 60, 80, 90)
setNodeSizeMapping('lfc',control.points, node.sizes)
BEFORE
selectNodes(cw, 'B')
nodes <- getSelectedNodes (cw)
selectFirstNeighborsOfSelectedNodes (cw)
AFTER
selectNodes('B', 'name') #or 'id'
nodes <- getSelectedNodes()
selectFirstNeighbors()
nodes <- selectNodes('B', 'name')$nodes
BEFORE
saveImage(cw, "sample_image", "png", h = 800)
saveNetwork(cw, "sample_session", format = "cys")
AFTER
exportImage("sample_image", "png", h= 800)
saveSession("sample_session")
And the next time you start a script from scratch, consider reviewing the vignettes and other RCy3 scripts available online.
browseVignettes('RCy3')
Repository of R scripts for Cytoscape, many using RCy3: