<jupyter_start><jupyter_text>Basic Workflow for R---------------------------------------This jupyter-notebook contains basic R workflow for network visualizatin.You can understand the basic workflow by reading this, but the RCy3's documentation is really useful and you can know much more about this.The documentation : https://www.bioconductor.org/packages/release/bioc/html/RCy3.html  Setup---------------------------------------To execute this, please sutisfy following items.- Java SE 8- Cytoscape version 3.3+- CyREST- Use docker file to do this. The docker file provide you the environment below.    - RCy3    - igraph    - ...  Start a new session---------------------------------------To start a new session, you should do the following step. Steps1. Please execute cytoscape1. Import libraries1. Load Data1. Shaping data IMPORTANT NOTEIf you are running this notebook from a Docker container, you need to specify ***IP address of your machine running Cytoscape***.  Usually, it is the IP address of your laptop/workstation.  Please edit the following line before you run this notebook!```ip_address <- '192.168.1.1'```<jupyter_code># !!!!!!!!!!!! EDIT  THIS VALUE FIRST!!!!!!!!!!!!!!
ip_address <- 'IP_OF_YOUR_MACHINE'
ip_address <- '172.16.58.3'

library(RCy3)
library(igraph)
library(RJSONIO)
library(httr)
library(data.table)


# Test Connection to Cytoscape
conn <- CytoscapeConnection(host = ip_address)
print(conn)

# Start from scratch: Delete all networks
deleteAllWindows(conn)<jupyter_output><empty_output><jupyter_text>Import network table from Data Frame objectIn this example, we'll use [HumanNet v1]() data sets as the base network.  Also, for convenience, we'll use [data.table](https://github.com/Rdatatable/data.table/wiki) library to import data.<jupyter_code># Read as an igraph object
humannet.url <- "http://www.functionalnet.org/humannet/HumanNet.v1.benchmark.txt"
humannet.ig <- read.graph(humannet.url)
summary(humannet.ig)
cw <- CytoscapeWindow('vignette', host = ip_address , graph=g, overwrite=TRUE)
# Load Data
gal.table <- read.table('sampleData/galFiltered.sif',stringsAsFactors=FALSE)
head(gal.table)
# create graph class
g <- new ('graphNEL', edgemode='directed')

# Get NodesVec
gal.table.nodevec <- unique(c(gal.table[[1]], gal.table[[3]]))

# add nodes to graph
for(node in gal.table.nodevec){
    g <- graph::addNode(node, g)
}

# get EdgeList
gal.table.fromvec = gal.table[[1]]
gal.table.tovec = gal.table[[3]]

for (index in 1:length(gal.table.fromvec)){
    g <- graph::addEdge (gal.table.fromvec[[index]] ,gal.table.tovec[[index]], g)
}
# show it in cytescape
cw <- CytoscapeWindow('vignette', host = ip_address , graph=g, overwrite=TRUE)
displayGraph (cw)
layoutNetwork (cw, layout.name='degree-circle')<jupyter_output><empty_output><jupyter_text>Option 2: Load from list of URLsInstead of passing actual graph data, you can pass list of file locations to load files into Cytoscape. Note:This feature is not implemented in RCy3, so you need to use the following code snippet to do this...<jupyter_code>port.number = 1234
base.url = paste("http://", ip_address, ":", toString(port.number), "/v1/", sep="")
res <-GET(base.url)
rawToChar(res$content)
network.url = paste(base.url, "networks?source=url&collection=FromR", sep="")

urls <-c(
    'file:///Users/kono/git/cyrest-examples/notebooks/cookbook/sampleData/galFiltered.sif',
    'file:///Users/kono/git/cyrest-examples/notebooks/cookbook/sampleData/galFiltered.gml'
)
res <- POST(url=network.url, body=toJSON(urls), encode="json")
network.suid = unname(fromJSON(rawToChar(res$content)))
print(network.suid)<jupyter_output><empty_output><jupyter_text>Then, you can get this image in cytoscape.![cytoscape image](graph_image.jpeg)  Upload  a node attribute table.--------------------------------------- Import Annotation Data from bioconductor and merge it into Network data---------------------------------------In this example of workflow, by executing the following steps, you can load annotation data and merge it.1. First, import network data from local file/URL to Cytoscape.1. Second, import annotation data from database(in R, we can use bioconductor as database).  - Human database in bioconductor:  - https://www.bioconductor.org/packages/release/data/annotation/html/org.Hs.eg.db.html  - Yeast database in bioconductor :  - https://www.bioconductor.org/packages/release/data/annotation/html/org.Sc.sgd.db.html1. Finally, merge the above two data table and push it to Cytoscape.Now, we use the data file 'galFiltered.sif' that is the yeast data. So, we will try to add annotation data to this.<jupyter_code># Second, import annotation data from database(in R, we can use bioconductor as database).
# import library to access database
library(org.Sc.sgd.db)

# import Data
# DESCRIPTION data
descriptions <- select(org.Sc.sgd.db, keys=getAllNodes(cw), columns="DESCRIPTION")
df.descriptions <- data.frame(descriptions)

# GENENAME data
gene.names <- select(org.Sc.sgd.db, keys=getAllNodes(cw), columns="GENENAME")
df.gene.names <- data.frame(gene.names)

# GO data
go <- select(org.Sc.sgd.db, keys=getAllNodes(cw), columns="GO")
df.go <- data.frame(go)

# Finally, merge the above two data table and push it to Cytoscape.
# set DESCRIPTION as attribute data
nodeDataDefaults(g, attr = "DESCRIPTION") <- "undefined"
attr(nodeDataDefaults(g, attr = "DESCRIPTION"), "class") <- "STRING"

# set GENENAME as attribute data
nodeDataDefaults(g, attr = "name") <- "undefined"
attr(nodeDataDefaults(g, attr = "name"), "class") <- "STRING"

# set GO as attribute data
#nodeDataDefaults(g, attr = "go") <- "undefined"
#attr(nodeDataDefaults(g, attr = "go"), "class") <- "LIST"

# marge DESCRIPTION attribute.
for (index in 1:length(df.descriptions$DESCRIPTION)){
    nodeData(g, df.descriptions$ORF[[index]], "DESCRIPTION") <- df.descriptions$DESCRIPTION[[index]]
}

# marge GENENAME attribute.
for (index in 1:length(df.gene.names$GENENAME)){
    nodeData(g, df.gene.names$ORF[[index]], "name") <- df.gene.names$GENENAME[[index]]
}

# marge GO attribute.
#for (index in 1:length(df.gene.names$GENENAME)){
#    nodeData(g, df.gene.names$ORF[[index]], "name") <- df.gene.names$GENENAME[[index]]
#}

# show it in cytescape
cw <- CytoscapeWindow('vignette', graph=g, overwrite=TRUE)
displayGraph (cw)
layoutNetwork (cw, layout.name='degree-circle')<jupyter_output><empty_output><jupyter_text>Upload an edge attribute table<jupyter_code># add attribute:"edgeType" and the type:"STRING"
edgeDataDefaults(g, attr = "edgeType") <- "undefined"
attr(edgeDataDefaults(g, attr = "edgeType"), "class") <- "STRING"

# get EdgeList and type
gal.table.fromvec <- gal.table[,1]
gal.table.type <- gal.table[,2]
gal.table.tovec <-  gal.table[,3]

# add attribute value to the network
for (index in 1:length(gal.table.fromvec)){
    edgeData(g, gal.table.fromvec[[index]] ,gal.table.tovec[[index]], "edgeType") <- gal.table.type[[index]]
}
cw <- CytoscapeWindow('vignette', graph=g, overwrite=TRUE)
displayGraph (cw)
layoutNetwork (cw, layout.name='degree-circle')<jupyter_output><empty_output><jupyter_text>Select edges based on some edge attributes--------------------------------------- How to select edges based on their attributes?First, we have to set attributes to edges. Then, we can select edges based on attributes. Method : selectEdges UsageselectEdges(obj, edge.names, preserve.current.selection=TRUE) Arguments- **obj** : a CytoscapeWindowClass object.  - **edge.names** : a list of strings, the names of edges to select.  - **preserve.current.selection** : a logical object.<jupyter_code># clear selection
clearSelection (cw)
# selection Edges
vec = c()
for (index in 1:length(gal.table.type)){
    if(gal.table.type[[index]] == 'pp'){
        vec = c(vec, paste(gal.table.fromvec[[index]], '(pp)', gal.table.tovec[[index]]))
    }
}
selectEdges (cw, vec)<jupyter_output><empty_output><jupyter_text>Then, you can get the output,like the follwoing image, in cytoscape.![cytoscape image](select_edge_image.jpeg)  Hide these edges---------------------------------------If you want to hide selected edges, you can use this method. Method : hideSelectedEdges UsagehideSelectedEdges(obj) Argumentsobj a CytoscapeWindowClass object.<jupyter_code># The code is only this!!
# TODO : this methods have some error. I can't do anything with this method.
hideSelectedEdges (cw)<jupyter_output><empty_output><jupyter_text>Select nodes based on some node attributes--------------------------------------- method : selectNodes UsageselectNodes(obj, node.names, preserve.current.selection=TRUE) Arguments- **obj** : a CytoscapeWindowClass object.- **node.names** : a list of strings, the names of nodes to select.- **preserve.current.selection** : a logical object.<jupyter_code># get attributes data
gal.node.attrs <- read.table('sampleData/galFiltered_node.attrs',stringsAsFactors=FALSE, skip=1)
head(gal.node.attrs)
g <- initNodeAttribute (graph=g, attribute.name='moleculeType', attribute.type='char', default.value='undefined')

# get EdgeAttrs
gal.node = gal.node.attrs[[1]]
gal.attrs = gal.node.attrs[[3]]

for (index in 1:length(gal.node)){
    nodeData (g, gal.node, 'moleculeType') <- as.character(gal.attrs[[index]])
}

cw <- setGraph (cw, g)
displayGraph (cw)
# clear selection
clearSelection (cw)
vac = c()
for (index in 1:length(gal.node)){
    if(gal.attrs[[index]] == '1'){
        vec = c(vec, gal.node[[index]])
    }
}

selectNodes(cw, vec)<jupyter_output><empty_output><jupyter_text>Then, you can get the output,like the follwoing image, in cytoscape.![cytoscape image](select_nodes_image.jpeg)  Hide these nodes--------------------------------------- Method : hideSelectedNodes UsagehideSelectedNodes(obj) Argumentsobj : a CytoscapeWindowClass object.<jupyter_code># TODO : There are some error in this method. I can't do anything by this.
hideSelectedNodes(cw)<jupyter_output><empty_output><jupyter_text>Apply a layout the the network that is not hidden--------------------------------------- Method : getLayoutNames, layoutNetworkWe use two method in this section. 'getLayoutNames' let us know available layoutNames and we can apply one of them by 'layoutNetwork' method. 1. UsagegetLayoutNames(obj) 1. Argumentsobj : a CytoscapeConnectionClass object. 2. UsagelayoutNetwork(obj, layout.name= grid ) 2. Argumentsobj a CytoscapeWindowClass object.layout.name a string, one of the values returned by getLayoutNames, ’grid’ by default.<jupyter_code># get available layout names
getLayoutNames(cw)
# execute layout that you want
layoutNetwork(cw, layout.name= 'isom')<jupyter_output><empty_output><jupyter_text>Then, you can get the output,like the follwoing image, in cytoscape.![cytoscape image](isom_graph_image.jpeg)<jupyter_code># execute layout that you want
layoutNetwork(cw, layout.name= 'kamada-kawai')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16174570954050563, 'fraction_numerical': 0.005720612659162207, 'mean_word_length': 4.457703927492447, 'pattern_counts': {'":': 1, '<': 88, '<?xml version=': 0, '>': 45, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '463576', 'license': 'MIT', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3361, 'n_words': 972, 'path': 'notebooks/Realistic workflow/The workflow of Anne/The R workflow of Anne v2.ipynb', 'repo_name': 'idekerlab/cyrest-examples', 'size': '10842'}