実行に必要なもの
Cytoscape 3.1以上
cyREST(CytoscapeのApp)
実行前の準備
cyRESTを追加したCytoscapeを起動しておくこと
# 1. graphオブジェクト準備
library(igraph)
g <- static.power.law.game(no.of.nodes=10000, no.of.edges=30000, 2.3, 2.4)
V(g)$degree <- degree(g) # 次数(ノードに接するエッジの数)
# 2. graph → JSON 変換
library(RJSONIO)
library(httr)
devtools::source_url("https://raw.githubusercontent.com/idekerlab/cy-rest-R/develop/toCytoscape.R")
cygraph <- toCytoscape(g)
# 3. JSONデータ送信
res <- POST(url="http://localhost:1234/v1/networks", body=cygraph, encode="json")
network.suid <- unname(fromJSON(rawToChar(res$content)))
この時点でCytoscaopeに何も表示されていない場合は下記コードを実行する
POST(paste("http://localhost:1234/v1/networks/", toString(network.suid), "/views", sep=""))
続き
# 4. レイアウト変更
GET(paste("http://localhost:1234/v1/apply/layouts/force-directed/", toString(network.suid), sep=""))
この処理の後、レイアウトが変わらない時はCytoscapeで時点でレイアウトOrganicを行う
続き
# 6. スタイル作成
style.name = "midorinokedama"
# デフォルト
def.background.color <- list(
visualProperty = "NETWORK_BACKGROUND_PAINT",
value = "#000000"
)
# Node
def.node.color <- list(
visualProperty = "NODE_FILL_COLOR",
value = "#00FF00"
)
def.node.label <- list(
visualProperty = "NODE_LABEL",
value = ""
)
def.node.borderwidth <- list(
visualProperty = "NODE_BORDER_WIDTH",
value = 0.0
)
def.node.transparency <- list(
visualProperty = "NODE_TRANSPARENCY",
value = 150
)
def.node.shape <- list(
visualProperty = "NODE_SHAPE",
value = "ELLIPSE"
)
# Edge
def.edge.color <- list(
visualProperty = "EDGE_STROKE_UNSELECTED_PAINT",
value = "#00FF00"
)
def.edge.transparency <- list(
visualProperty = "EDGE_TRANSPARENCY",
value = 80
)
def.edge.width <- list(
visualProperty = "EDGE_WIDTH",
value = 2.0
)
defaults <- list(
def.background.color,
def.node.color,
def.node.label,
def.node.borderwidth,
def.node.transparency,
def.edge.color,
def.edge.transparency,
def.edge.width
)
# マッピング
min.degree = min(V(g)$degree)
max.degree = max(V(g)$degree)
# ノードの大きさ
ns.point1 = list(
value=min.degree,
lesser= "1.0",
equal="2.0",
greater="2.0"
)
ns.point2 = list(
value=max.degree,
lesser="150.0",
equal="150.0",
greater="1.0"
)
node.size.continuous.points = list(ns.point1, ns.point2)
node.size = list(
mappingType="continuous",
mappingColumn="degree",
mappingColumnType="Double",
visualProperty="NODE_SIZE",
points = node.size.continuous.points
)
# ノードの色
nc.point1 = list(
value=5.0,
lesser= "#00FF00",
equal="#00FF00",
greater="#00FF00"
)
nc.point2 = list(
value=10,
lesser="#FFFFFF",
equal="#FFFFFF",
greater="#FFFFFF"
)
node.color.continuous.points = list(nc.point1, nc.point2)
node.color = list(
mappingType="continuous",
mappingColumn="degree",
mappingColumnType="Double",
visualProperty="NODE_FILL_COLOR",
points = node.color.continuous.points
)
mappings = list(node.size, node.color)
style <- list(title = style.name, defaults = defaults, mappings = mappings)
style.JSON <- toJSON(style)
POST(url="http://localhost:1234/v1/styles", body=style.JSON, encode = "json")
GET(paste("http://localhost:1234/v1/apply/styles/midorinokedama/", toString(network.suid), sep=""))
Enjoy!