rm(list = ls())
if(!"RCy3" %in% installed.packages()){
  install.packages("BiocManager")
  BiocManager::install("RCy3")
}
library(RCy3)
cytoscapePing () #You are connected to Cytoscape!
## You are connected to Cytoscape!
cytoscapeVersionInfo () #apiVersion cytoscapeVersion  "v1"          "3.9.0" 
##       apiVersion cytoscapeVersion 
##             "v1"          "3.9.0"
###################################___1. network
nodes <- data.frame(id=c("node 0","node 1","node 2","node 3"),
                    group=c("A","A","B","B"), # categorical strings
                    score=as.integer(c(20,10,15,5)), # integers
                    stringsAsFactors=FALSE)
nodes
##       id group score
## 1 node 0     A    20
## 2 node 1     A    10
## 3 node 2     B    15
## 4 node 3     B     5
edges <- data.frame(source=c("node 0","node 0","node 0","node 2"),
                    target=c("node 1","node 2","node 3","node 3"),
                    interaction=c("inhibits","interacts","activates","interacts"),  # optional
                    weight=c(5.1,3.0,5.2,9.9), # numeric
                    stringsAsFactors=FALSE)
edges
##   source target interaction weight
## 1 node 0 node 1    inhibits    5.1
## 2 node 0 node 2   interacts    3.0
## 3 node 0 node 3   activates    5.2
## 4 node 2 node 3   interacts    9.9
createNetworkFromDataFrames(nodes,edges, title="my first network", collection="DataFrame Example")
## Loading data...
## Applying default style...
## Applying preferred layout...
## networkSUID 
##         697
setVisualStyle('Marquee')
##                 message 
## "Visual Style applied."
##############################My own style
style.name = "myStyle"
defaults <- list(NODE_SHAPE="diamond",
                 NODE_SIZE=30,
                 EDGE_TRANSPARENCY=120,
                 NODE_LABEL_POSITION="W,E,c,0.00,0.00")
nodeLabels <- mapVisualProperty('node label','id','p')
nodeFills <- mapVisualProperty('node fill color','group','d',c("A","B"), c("#FF9900","#66AAAA"))
arrowShapes <- mapVisualProperty('Edge Target Arrow Shape','interaction','d',c("activates","inhibits","interacts"),c("Arrow","T","None"))
edgeWidth <- mapVisualProperty('edge width','weight','p')

createVisualStyle(style.name, defaults, list(nodeLabels,nodeFills,arrowShapes,edgeWidth))
setVisualStyle(style.name)
##                 message 
## "Visual Style applied."
##################################Bioconductor graph example
g = new ('graphNEL', edgemode='directed')
g = graph::addNode ('A', g)
g = graph::addNode ('D', g)
g = graph::addNode ('C', g, edges = list('D'))
g = graph::addNode ('B', g, edges = list(c('A','D','C')))
createNetworkFromGraph (g, title='simple network', collection='GraphNEL Example')
## Loading data...
## Applying default style...
## Applying preferred layout...
## networkSUID 
##         807
################################Add node attributes
df <- data.frame (moleculeType=c('kinase','TF','cytokine','cytokine'),
                  log2fc=c(1.8,3.0,-1.2,-2.5),
                  row.names = c('A','B','C','D'), # row.names = node names
                  stringsAsFactors = FALSE)       # important when loading strings!
df
##   moleculeType log2fc
## A       kinase    1.8
## B           TF    3.0
## C     cytokine   -1.2
## D     cytokine   -2.5
loadTableData (df)
## [1] "Success: Data loaded in defaultnode table"
##############################Modifying the display: defaults and mappings
setNodeShapeDefault ('OCTAGON')
## style.name not specified, so updating "default" style.
setNodeColorDefault ('#AAFF88')
## style.name not specified, so updating "default" style.
setNodeSizeDefault  (60)
## style.name not specified, so updating "default" style.
## style.name not specified, so updating "default" style.
setNodeFontSizeDefault (30)
## style.name not specified, so updating "default" style.
#############
getNodeShapes ()   # diamond, ellipse, trapezoid, triangle, etc.
## [1] "RECTANGLE"       "PARALLELOGRAM"   "TRIANGLE"        "DIAMOND"        
## [5] "ELLIPSE"         "OCTAGON"         "ROUND_RECTANGLE" "VEE"            
## [9] "HEXAGON"
column <- 'moleculeType'
values <- c ('kinase',  'TF','cytokine')
shapes <- c ('DIAMOND', 'TRIANGLE', 'RECTANGLE')
setNodeShapeMapping (column, values, shapes)
## style.name not specified, so updating "default" style.
## NULL
##########
column <- 'log2fc'
control.points <- c (-3.0, 0.0, 3.0)
colors <-  c ('#5588DD', '#FFFFFF', '#DD8855')
setNodeColorMapping (column, control.points, colors)
## style.name not specified, so updating "default" style.
## NULL
##########
control.points <- c (-2.0, 0.0, 2.0)
colors <-  c ('#2255CC', '#5588DD', '#FFFFFF', '#DD8855','#CC5522')
setNodeColorMapping (column, control.points, colors)
## style.name not specified, so updating "default" style.
## NULL
#########
control.points = c (-3.0, 2.0, 3.0)
sizes     = c (20, 80, 90)
setNodeSizeMapping (column, control.points, sizes)
## style.name not specified, so updating "default" style.
## style.name not specified, so updating "default" style.
## NULL
##################################Selecting nodes
selectNodes ('C','name')
## $nodes
## [1] 847
## 
## $edges
## list()
getSelectedNodes ()
## [1] "C"
selectFirstNeighbors ()
## $nodes
## [1] 847 850 844
## 
## $edges
## list()
node.names <- getSelectedNodes ()
clearSelection()
?clearSelection
## starting httpd help server ...
##  done
################################Saving and export
getwd()
## [1] "C:/Users/liyix/OneDrive/Desktop"
setwd("C:/Users/liyix/OneDrive/Desktop/")
saveSession('vignette_session')
## Warning: This file has been overwritten.
###############################Saving high resolution image files
full.path=paste(getwd(),'vignette_image',sep='/')
exportImage(full.path, 'PNG', zoom=200) #.png scaled by 200%
##                                                      file 
## "C:\\Users\\liyix\\OneDrive\\Desktop\\vignette_image.png"
exportImage(full.path, 'PDF') #.pdf
##                                                      file 
## "C:\\Users\\liyix\\OneDrive\\Desktop\\vignette_image.pdf"
?exportImage
##################################Browse available functions, commands and arguments
help(package=RCy3)
cyrestAPI()  # CyREST API
commandsAPI()  # Commands API
commandsHelp("help")  
## [1] "Available namespaces:"
##  [1] "analyzer"         "annotation"       "apps"             "autoannotate"    
##  [5] "cluster"          "clusterdimreduce" "clusterrank"      "clusterviz"      
##  [9] "command"          "cybrowser"        "cychart"          "cytargetlinker"  
## [13] "diffusion"        "edge"             "enhancedGraphics" "enrichmentmap"   
## [17] "filetransfer"     "filter"           "gpml"             "group"           
## [21] "idmapper"         "layout"           "network"          "node"            
## [25] "session"          "string"           "table"            "view"            
## [29] "vizmap"           "wikipathways"     "wordcloud"
commandsHelp("help network")  
## [1] "Available commands for 'network':"
##  [1] "add"              "add edge"         "add node"         "clone"           
##  [5] "connect nodes"    "create"           "create attribute" "create empty"    
##  [9] "delete"           "deselect"         "destroy"          "export"          
## [13] "get"              "get attribute"    "get properties"   "hide"            
## [17] "import file"      "import url"       "list"             "list attributes" 
## [21] "list properties"  "load file"        "load url"         "merge"           
## [25] "rename"           "select"           "set attribute"    "set current"     
## [29] "set properties"   "show"
commandsHelp("help network select") 
## [1] "Available arguments for 'network select':"
## [1] "adjacentEdges"  "edgeList"       "extendEdges"    "firstNeighbors"
## [5] "invert"         "network"        "nodeList"
#################################More examples
browseVignettes("RCy3")
###############ref https://bioconductor.org/packages/release/bioc/vignettes/RCy3/inst/doc/Overview-of-RCy3.html