Last active
April 8, 2018 00:13
-
-
Save Vessy/6c9567f26f397320ae849445ac92a981 to your computer and use it in GitHub Desktop.
Network visualization and R - An example how to plot a network and customize its appearance in Cytoscape directly from R using the RCy3 package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cyPlot_mod <- function (node.df, edge.df) | |
{ | |
edge.nodes <- unique(c(as.character(edge.df[, 1]), as.character(edge.df[, | |
2]))) | |
mydata <- new("graphNEL", edgemode = "directed", nodes = unique(c(as.character(node.df[, | |
1]), edge.nodes))) | |
node.df[, 1] <- as.character(node.df[, 1]) | |
edge.df[, 1:2] <- sapply(edge.df[, 1:2], as.character) | |
node.class <- sapply(node.df, class) | |
if (any(grep("factor", node.class))) { | |
node.df[, grep("factor", node.class)] <- sapply(node.df[, | |
grep("factor", node.class)], as.character) | |
} | |
if (any(grep("integer", node.class))) { | |
node.df[, grep("integer", node.class)] <- sapply(node.df[, | |
grep("integer", node.class)], as.numeric) | |
} | |
node.class <- sapply(node.df, class) | |
edge.class <- sapply(edge.df, class) | |
if (any(grep("factor", edge.class))) { | |
edge.df[, grep("factor", edge.class)] <- sapply(edge.df[, | |
grep("factor", edge.class)], as.character) | |
} | |
if (any(grep("integer", edge.class))) { | |
edge.df[, grep("integer", edge.class)] <- sapply(edge.df[, | |
grep("integer", edge.class)], as.numeric) | |
} | |
edge.class <- sapply(edge.df, class) | |
if (length(grep("character", node.class)) > 1) | |
for (i in 2:length(grep("character", node.class))) { | |
mydata <- RCy3::initNodeAttribute(graph = mydata, attribute.name = names(node.class[grep("character", | |
node.class)])[i], attribute.type = "char", default.value = "undefined") | |
graph::nodeData(mydata, n = as.character(node.df[, 1]), attr = names(node.class[grep("character", | |
node.class)])[i]) <- as.character(node.df[, grep("character", | |
node.class)[i]]) | |
} | |
if (length(grep("numeric", node.class)) > 0) | |
for (i in 1:length(grep("numeric", node.class))) { | |
mydata <- RCy3::initNodeAttribute(graph = mydata, attribute.name = names(node.class[grep("numeric", | |
node.class)])[i], attribute.type = "numeric", default.value = 0) | |
graph::nodeData(mydata, n = as.character(node.df[, 1]), attr = names(node.class[grep("numeric", | |
node.class)])[i]) <- as.numeric(node.df[, grep("numeric", | |
node.class)[i]]) | |
} | |
mydata = graph::addEdge(as.vector(edge.df[, 1], mode = "character"), | |
as.vector(edge.df[, 2], mode = "character"), mydata) | |
if (length(grep("character", edge.class)) > 2) | |
for (i in 3:length(grep("character", edge.class))) { | |
mydata <- RCy3::initEdgeAttribute(graph = mydata, attribute.name = names(edge.df[, | |
grep("character", edge.class)])[i], attribute.type = "char", | |
default.value = "undefined") | |
graph::edgeData(mydata, as.vector(edge.df[, 1], mode = "character"), | |
as.vector(edge.df[, 2], mode = "character"), attr = names(edge.df[, | |
grep("character", edge.class)])[i]) <- as.character(edge.df[, | |
grep("character", edge.class)[i]]) | |
} | |
if (length(grep("numeric", edge.class)) > 0) | |
for (i in 1:length(grep("numeric", edge.class))) { | |
mydata <- RCy3::initEdgeAttribute(mydata, attribute.name = names(edge.class[grep("numeric", | |
edge.class)])[i], attribute.type = "numeric", default.value = 0) | |
graph::edgeData(mydata, as.vector(edge.df[, 1], mode = "character"), | |
as.vector(edge.df[, 2], mode = "character"), attr = names(edge.class[grep("numeric", | |
edge.class)])[i]) <- as.numeric(edge.df[, grep("numeric", | |
edge.class)[i]]) | |
} | |
return(mydata) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gillenormand JeanValjean 2 | |
Zephine Listolier 3 | |
Joly Feuilly 5 | |
Brevet Judge 2 | |
Bamatabois JeanValjean 2 | |
Gavroche JeanValjean 1 | |
MadameHucheloup Courfeyrac 1 | |
Gavroche Javert 1 | |
Count BishopCharles-Francois-BienvenuMyriel 2 | |
Dahlia Listolier 3 | |
Fantine JeanValjean 9 | |
Marius Cosette 21 | |
MadameHucheloup Joly 1 | |
Blacheville Listolier 4 | |
Scaufflaire JeanValjean 1 | |
MadameHucheloup Gavroche 1 | |
Montparnasse Claquesous 2 | |
Fantine Zephine 4 | |
MadameHucheloup Grantaire 1 | |
Pontmercy Thenardier 1 | |
Combeferre Marius 5 | |
Bahorel Feuilly 3 | |
Courfeyrac JeanProuvaire 3 | |
Combeferre Mabeuf 2 | |
Bahorel Mabeuf 2 | |
Gueulemer JeanValjean 1 | |
Brevet JeanValjean 2 | |
Joly JeanProuvaire 2 | |
Dahlia FelixTholomyes 3 | |
OldMan BishopCharles-Francois-BienvenuMyriel 1 | |
Brujon Babet 3 | |
Toussaint Javert 1 | |
Fantine Dahlia 4 | |
Fameuil Listolier 4 | |
Grantaire Combeferre 1 | |
LAigleDeMeauxBossuet Marius 5 | |
Child2 Gavroche 2 | |
Mabeuf Marius 1 | |
Gueulemer Thenardier 5 | |
Courfeyrac Marius 9 | |
SisterSimplice Fantine 2 | |
Cosette FelixTholomyes 1 | |
Judge JeanValjean 3 | |
Gillenormand Cosette 3 | |
Fantine Marguerite 2 | |
MaubertIsabeau JeanValjean 1 | |
Fantine Fameuil 3 | |
Fauchelevent Javert 1 | |
Mabeuf Gavroche 1 | |
Gervais JeanValjean 1 | |
MademoiselleGillenormand Cosette 2 | |
LieutenantTheoduleGillenormand Gillenormand 1 | |
Gavroche MadameBurgon 2 | |
LAigleDeMeauxBossuet JeanValjean 1 | |
Gueulemer Javert 1 | |
Cochepaille Bamatabois 1 | |
LieutenantTheoduleGillenormand Cosette 1 | |
Javert JeanValjean 17 | |
Marius JeanValjean 19 | |
Babet JeanValjean 1 | |
Grantaire Joly 2 | |
Thenardier JeanValjean 12 | |
Feuilly JeanProuvaire 2 | |
Courfeyrac Feuilly 6 | |
Brujon Eponine 1 | |
Grantaire Enjolras 3 | |
Cochepaille Champmathieu 2 | |
Montparnasse Gavroche 1 | |
Babet Thenardier 6 | |
Javert Cosette 1 | |
Joly Marius 2 | |
SisterSimplice Javert 1 | |
Chenildieu Bamatabois 1 | |
Fantine Listolier 3 | |
Zephine Dahlia 4 | |
Joly Mabeuf 1 | |
JeanProuvaire Enjolras 4 | |
Grantaire Feuilly 1 | |
Brujon Thenardier 3 | |
Marius LieutenantTheoduleGillenormand 1 | |
Claquesous MadameThenardier 1 | |
Courfeyrac Eponine 1 | |
Bahorel JeanProuvaire 2 | |
Marius Gillenormand 12 | |
Brujon Montparnasse 1 | |
Toussaint Cosette 2 | |
Eponine Thenardier 3 | |
Enjolras Marius 7 | |
Favourite Blacheville 4 | |
Zephine Favourite 4 | |
Brujon Gueulemer 3 | |
MotherInnocent JeanValjean 1 | |
Grantaire LAigleDeMeauxBossuet 3 | |
LAigleDeMeauxBossuet Bahorel 4 | |
Marius Pontmercy 1 | |
Bahorel Enjolras 4 | |
Grantaire JeanProuvaire 1 | |
Dahlia Blacheville 3 | |
Marius MademoiselleGillenormand 6 | |
Bahorel Courfeyrac 6 | |
Brujon Claquesous 1 | |
BaronessDeThenard Marius 1 | |
Bahorel Combeferre 5 | |
MadameThenardier JeanValjean 7 | |
Gueulemer MadameThenardier 1 | |
Enjolras JeanValjean 4 | |
Zephine Blacheville 3 | |
Joly LAigleDeMeauxBossuet 7 | |
Enjolras Javert 6 | |
Claquesous JeanValjean 1 | |
MotherPlutarch Mabeuf 3 | |
LAigleDeMeauxBossuet Courfeyrac 12 | |
Anzelma Thenardier 2 | |
Javert Thenardier 5 | |
Javert Fantine 5 | |
LieutenantTheoduleGillenormand MademoiselleGillenormand 2 | |
Claquesous Eponine 1 | |
CountessDeLo BishopCharles-Francois-BienvenuMyriel 1 | |
Napoleon BishopCharles-Francois-BienvenuMyriel 1 | |
Listolier FelixTholomyes 4 | |
JeanProuvaire Gavroche 1 | |
Joly Gavroche 3 | |
Courfeyrac Mabeuf 2 | |
Dahlia Fameuil 3 | |
Cochepaille Chenildieu 2 | |
Eponine MadameThenardier 2 | |
Favourite Fameuil 3 | |
Babet Javert 2 | |
BaronessDeThenard Gillenormand 1 | |
Combeferre Enjolras 15 | |
Gueulemer Gavroche 1 | |
JeanValjean MadameMagloire 3 | |
Cosette MadameThenardier 4 | |
Marius FelixTholomyes 1 | |
Feuilly Combeferre 5 | |
Cosette Thenardier 1 | |
Thenardier MadameThenardier 13 | |
Child1 Gavroche 2 | |
Cochepaille Judge 2 | |
JeanProuvaire Combeferre 2 | |
Grantaire Bahorel 1 | |
Montparnasse Gueulemer 2 | |
Marius Thenardier 2 | |
Courfeyrac Combeferre 13 | |
Enjolras Gavroche 7 | |
Montparnasse Thenardier 1 | |
Joly Bahorel 5 | |
Woman2 Cosette 1 | |
Cochepaille Brevet 2 | |
JeanValjean BaptistineMyrie 3 | |
MadamePontmercy MademoiselleGillenormand 1 | |
Babet MadameThenardier 1 | |
Woman2 JeanValjean 3 | |
Montparnasse Javert 1 | |
LAigleDeMeauxBossuet JeanProuvaire 2 | |
Favourite Listolier 3 | |
Combeferre Gavroche 6 | |
Feuilly Mabeuf 1 | |
Woman1 JeanValjean 2 | |
Montparnasse Babet 2 | |
Fameuil FelixTholomyes 4 | |
Courfeyrac Enjolras 17 | |
Marius Eponine 5 | |
Zephine FelixTholomyes 3 | |
Chenildieu Champmathieu 2 | |
MadamePontmercy Pontmercy 1 | |
MadameBurgon Jondrette 1 | |
Feuilly Gavroche 2 | |
Mabeuf Eponine 1 | |
Thenardier Fantine 1 | |
Monsieur Geborand BishopCharles-Francois-BienvenuMyriel 1 | |
Champmathieu JeanValjean 3 | |
Fantine Blacheville 3 | |
Feuilly Enjolras 6 | |
BaptistineMyrie BishopCharles-Francois-BienvenuMyriel 8 | |
Champtercier BishopCharles-Francois-BienvenuMyriel 1 | |
Bahorel Gavroche 5 | |
Babet Gavroche 1 | |
MadameHucheloup Enjolras 1 | |
Enjolras Mabeuf 1 | |
Montparnasse JeanValjean 1 | |
Javert MadameThenardier 1 | |
Claquesous Enjolras 1 | |
Claquesous Thenardier 4 | |
Claquesous Gueulemer 4 | |
Toussaint JeanValjean 1 | |
Bamatabois Javert 1 | |
LAigleDeMeauxBossuet Mabeuf 1 | |
Courfeyrac Gavroche 7 | |
LAigleDeMeauxBossuet Enjolras 10 | |
Joly Combeferre 5 | |
Magnon MadameThenardier 1 | |
Grantaire Courfeyrac 2 | |
MotherInnocent Fauchelevent 3 | |
Grantaire Gavroche 1 | |
Brevet Champmathieu 2 | |
Gribier Fauchelevent 2 | |
Cosette JeanValjean 31 | |
Anzelma Eponine 2 | |
Brevet Bamatabois 1 | |
Marius Gavroche 4 | |
LAigleDeMeauxBossuet Feuilly 6 | |
Feuilly Marius 1 | |
MademoiselleGillenormand Gillenormand 9 | |
Cravatte BishopCharles-Francois-BienvenuMyriel 1 | |
Claquesous Javert 1 | |
Child2 Child1 3 | |
SisterPerpetue Fantine 1 | |
Chenildieu JeanValjean 2 | |
Chenildieu Brevet 2 | |
Blacheville FelixTholomyes 4 | |
Fauchelevent JeanValjean 8 | |
Brujon Gavroche 1 | |
Gueulemer Eponine 1 | |
Boulatruelle Thenardier 1 | |
Bamatabois Fantine 1 | |
Zephine Fameuil 3 | |
Claquesous Babet 4 | |
Montparnasse Eponine 1 | |
SisterSimplice JeanValjean 3 | |
Champmathieu Judge 3 | |
Favourite FelixTholomyes 3 | |
MadameThenardier Fantine 2 | |
JeanValjean BishopCharles-Francois-BienvenuMyriel 5 | |
Magnon Gillenormand 1 | |
Blacheville Fameuil 4 | |
Gavroche Thenardier 1 | |
Babet Eponine 1 | |
Champmathieu Bamatabois 2 | |
Fantine Favourite 4 | |
Cochepaille JeanValjean 2 | |
MadameMagloire BaptistineMyrie 6 | |
Woman2 Javert 1 | |
Dahlia Favourite 5 | |
MadameMagloire BishopCharles-Francois-BienvenuMyriel 10 | |
Fantine FelixTholomyes 3 | |
Woman1 Javert 1 | |
MadameHucheloup LAigleDeMeauxBossuet 1 | |
MadameHucheloup Bahorel 1 | |
LAigleDeMeauxBossuet Combeferre 9 | |
Judge Bamatabois 2 | |
Bahorel Marius 1 | |
Joly Courfeyrac 5 | |
Marguerite JeanValjean 1 | |
JeanValjean JacquinLabarre 1 | |
MadameDeR JeanValjean 1 | |
SisterSimplice SisterPerpetue 2 | |
LAigleDeMeauxBossuet Gavroche 5 | |
Joly Enjolras 5 | |
MademoiselleGillenormand JeanValjean 2 | |
MademoiselleVaubois MademoiselleGillenormand 1 | |
Anzelma MadameThenardier 1 | |
Babet Gueulemer 6 | |
Chenildieu Judge 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################ | |
############################################################################################ | |
# Plotting networks in R - an example how to plot a network and | |
# customize its appearance in Cytoscape directly from R using | |
# the RCy3 package | |
############################################################################################ | |
############################################################################################ | |
# Clear workspace | |
# rm(list = ls()) | |
############################################################################################ | |
# Read a data set. | |
# Data format: dataframe with 3 variables; variables 1 & 2 correspond to interactions; variable 3 is weight of interaction | |
dataSet <- read.table("lesmis.txt", header = FALSE, sep = "\t") | |
# Create a graph. Use simplyfy to ensure that there are no duplicated edges or self loops | |
gD <- igraph::simplify(igraph::graph.data.frame(dataSet, directed=FALSE)) | |
# Print number of nodes and edges | |
# igraph::vcount(gD) | |
# igraph::ecount(gD) | |
############################################################################################ | |
# Calculate some node properties and node similarities that will be used to illustrate | |
# different plotting abilities | |
# Calculate degree for all nodes | |
degAll <- igraph::degree(gD, v = igraph::V(gD), mode = "all") | |
# Calculate betweenness for all nodes | |
betAll <- igraph::betweenness(gD, v = igraph::V(gD), directed = FALSE) / (((igraph::vcount(gD) - 1) * (igraph::vcount(gD)-2)) / 2) | |
betAll.norm <- (betAll - min(betAll))/(max(betAll) - min(betAll)) | |
rm(betAll) | |
#Calculate Dice similarities between all pairs of nodes | |
dsAll <- igraph::similarity.dice(gD, vids = igraph::V(gD), mode = "all") | |
############################################################################################ | |
# Add new node and edge attributes based on the calculated node properties/similarities | |
gD <- igraph::set.vertex.attribute(gD, "degree", index = igraph::V(gD), value = degAll) | |
gD <- igraph::set.vertex.attribute(gD, "betweenness", index = igraph::V(gD), value = betAll.norm) | |
# Check the attributes | |
# summary(gD) | |
F1 <- function(x) {data.frame(V4 = dsAll[which(igraph::V(gD)$name == as.character(x$V1)), which(igraph::V(gD)$name == as.character(x$V2))])} | |
dataSet.ext <- plyr::ddply(dataSet, .variables=c("V1", "V2", "V3"), function(x) data.frame(F1(x))) | |
gD <- igraph::set.edge.attribute(gD, "weight", index = igraph::E(gD), value = 0) | |
gD <- igraph::set.edge.attribute(gD, "similarity", index = igraph::E(gD), value = 0) | |
# The order of interactions in dataSet.ext is not the same as it is in dataSet or as it is in the edge list | |
# and for that reason these values cannot be assigned directly | |
for (i in 1:nrow(dataSet.ext)) | |
{ | |
igraph::E(gD)[as.character(dataSet.ext$V1) %--% as.character(dataSet.ext$V2)]$weight <- as.numeric(dataSet.ext$V3) | |
igraph::E(gD)[as.character(dataSet.ext$V1) %--% as.character(dataSet.ext$V2)]$similarity <- as.numeric(dataSet.ext$V4) | |
} | |
# Check the attributes | |
# summary(gD) | |
rm(dataSet,dsAll, i, F1) | |
############################################################################################ | |
# Now, let's do Cytoscape plots | |
# First, we need to transform our network from the igraph to graphnel format | |
gD.cyt <- igraph::as_graphnel(gD) | |
# Check if attributes have been passed | |
# graph::nodeData(gD.cyt, igraph::V(gD)$name, 'degree') | |
# graph::nodeData(gD.cyt, igraph::V(gD)$name, 'betweenness') | |
# graph::edgeData(gD.cyt, as.character(dataSet.ext$V1), as.character(dataSet.ext$V2), 'weight') | |
# graph::edgeData(gD.cyt, as.character(dataSet.ext$V1), as.character(dataSet.ext$V2), 'similarity') | |
# We have to create attributes for graphNEL | |
# We'll keep the same names as before | |
# In RCytoscape, this would ensure that the values of attributes are passed directly from igraph. | |
# However, this does not work with RCy3 right now (not sure if it is a bug or a feature has changed). | |
# Thus, we need to do send attributes to Cytoscape | |
gD.cyt <- RCy3::initNodeAttribute(gD.cyt, 'degree', 'numeric', 0) | |
gD.cyt <- RCy3::initNodeAttribute(gD.cyt, 'betweenness', 'numeric', 0) | |
gD.cyt <- RCy3::initEdgeAttribute (gD.cyt, "weight", 'integer', 0) | |
gD.cyt <- RCy3::initEdgeAttribute (gD.cyt, "similarity", 'numeric', 0) | |
# Next, we will create a new graph window in cytoscape | |
gDCW <- RCy3::CytoscapeWindow("Les Miserables", graph = gD.cyt, overwriteWindow = TRUE) | |
# We can display graph, with defaults color/size scheme | |
RCy3::displayGraph(gDCW) | |
# Now let's send/load node and edge attributes into Cytoscape | |
########## | |
# This should theoretically work, but there are some problems with attributes when networks | |
# are created from data frames (see https://github.com/tmuetze/Bioconductor_RCy3_the_new_RCytoscape/issues/25) | |
# I'll keep this code uncommented, but right now, it doesn't do anything | |
# setNodeAttributes should transfer the specified node attributes, for all nodes, the named node attribute | |
# from the R graph (found in obj@graph) to Cytoscape. | |
attribute.names <- RCy3::noa.names(gDCW@graph) | |
# Print list of attribute names to see if they are ok | |
# attribute.names | |
# All nodes should already be in | |
RCy3::sendNodes(gDCW) | |
for (attribute.name in attribute.names){ | |
RCy3::setNodeAttributes(gDCW, attribute.name) | |
} | |
attribute.names <- RCy3::eda.names(gDCW@graph) | |
# All edges should already be in | |
RCy3::sendEdges(gDCW) | |
for (attribute.name in attribute.names){ | |
RCy3::setEdgeAttributes(gDCW, attribute.name) | |
} | |
RCy3::displayGraph(gDCW) | |
########## | |
# Thealternative, when we set attributes directly, works fine, | |
# so we will use it now (although, it seems kind of repetative) | |
RCy3::setNodeAttributesDirect(gDCW, 'degree', 'numeric', igraph::V(gD)$name, igraph::V(gD)$degree) | |
RCy3::setNodeAttributesDirect(gDCW, 'betweenness', 'numeric', igraph::V(gD)$name, igraph::V(gD)$betweenness) | |
RCy3::setEdgeAttributesDirect(gDCW, 'weight', 'integer', as.character (RCy3::cy2.edge.names (gDCW@graph)), graph::edgeData(gD.cyt, as.character(dataSet.ext$V1), as.character(dataSet.ext$V2), 'weight')) | |
RCy3::setEdgeAttributesDirect(gDCW, 'similarity', 'numeric', as.character (RCy3::cy2.edge.names (gDCW@graph)), graph::edgeData(gD.cyt, as.character(dataSet.ext$V1), as.character(dataSet.ext$V2), 'similarity')) | |
########## | |
# Now let's decide on a layout | |
# If you also want to choose a layout from R, a list of available layouts can be accessed as follow: | |
cy <- RCy3::CytoscapeConnection() | |
hlp <-RCy3::getLayoutNames(cy) | |
# We'll select the "fruchterman-rheingold" layout. This layout is the layout number 10 | |
# To see properties for the given layout, use: | |
# RCy3::getLayoutPropertyNames(cy, hlp[10]) | |
# We can choose any property we want and provide them as a list | |
RCy3::setLayoutProperties (gDCW, hlp[10], list (gravity_multiplier = 'similarity', nIterations = 1000)) | |
RCy3::layoutNetwork(gDCW, hlp[10]) | |
# I've noticed that if I change property to attraction_multiplier | |
# RCy3::setLayoutProperties (gDCW, hlp[10], list (attraction_multiplier = 'similarity', nIterations = 1000)) | |
# RCy3::layoutNetwork(gDCW, hlp[10]) | |
# And then go back to the original one | |
# RCy3::setLayoutProperties (gDCW, hlp[10], list (gravity_multiplier = 'similarity', nIterations = 1000)) | |
# RCy3::layoutNetwork(gDCW, hlp[10]) | |
# The layout won't go back to the original one. I am not sure if this is a bug or not | |
########## | |
# Finally, we can define rules for nodes: | |
RCy3::setNodeColorRule(gDCW, 'degree', c(min(degAll), mean(degAll), max(degAll)), c('#F5DEB3', '#FFA500', '#FF7F50', '#FF4500', '#FF0000'), mode = 'interpolate') | |
RCy3::setNodeSizeRule(gDCW, 'betweenness', c(min(betAll.norm), mean(betAll.norm), max(betAll.norm)), c(30, 45, 60, 80, 100), mode = 'interpolate') | |
# And edges: | |
RCy3::setEdgeLineWidthRule(gDCW, 'weight', dataSet.ext$V3, dataSet.ext$V3) | |
RCy3::setEdgeColorRule(gDCW, 'weight', c(min(as.numeric(dataSet.ext$V3)), mean(as.numeric(dataSet.ext$V3)), max(as.numeric(dataSet.ext$V3))), c('#FFFF00', '#00FFFF', '#00FF7F', '#228B22', '#006400'), mode='interpolate') | |
# While I get the "Successfully set rule" for both of the Edge rules, the view in the Cytoscape did not | |
# change accordning the rules - setEdgeLineWidthRule command did not make any changes and the | |
# setEdgeColorRule command made all edges white. | |
# One of the GitHub solved issues suggests to first set all rule-based functions and then the direct ones, but | |
# but it didn't work here (https://github.com/tmuetze/Bioconductor_RCy3_the_new_RCytoscape/issues/21 and | |
# https://github.com/tmuetze/Bioconductor_RCy3_the_new_RCytoscape/issues/20) | |
# We will define our own default color/size schema after we defined node and edge rules, due to | |
# possible issues when using rules | |
RCy3::setDefaultBackgroundColor(gDCW, '#D3D3D3') | |
RCy3::setDefaultEdgeColor(gDCW, '#CDC9C9') | |
RCy3::setDefaultEdgeLineWidth(gDCW, 4) | |
RCy3::setDefaultNodeBorderColor(gDCW, '#000000') | |
RCy3::setDefaultNodeBorderWidth(gDCW, 3) | |
RCy3::setDefaultNodeShape(gDCW, 'ellipse') | |
RCy3::setDefaultNodeColor(gDCW, '#87CEFA') | |
RCy3::setDefaultNodeSize(gDCW, 60) | |
RCy3::setDefaultNodeFontSize(gDCW, 20) | |
RCy3::setDefaultNodeLabelColor(gDCW, '#000000') | |
# Running these commands will set color to all edges back to black and set their width to 4, | |
# ignoring the rules specified above | |
############################################################################################ | |
sessionInfo() | |
# R version 3.3.1 (2016-06-21) | |
# Platform: x86_64-redhat-linux-gnu (64-bit) | |
# Running under: Fedora 23 (Workstation Edition) | |
# | |
# locale: | |
# [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C | |
# [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 | |
# [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 | |
# [7] LC_PAPER=en_US.UTF-8 LC_NAME=C | |
# [9] LC_ADDRESS=C LC_TELEPHONE=C | |
# [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C | |
# | |
# attached base packages: | |
# [1] stats graphics grDevices utils datasets methods | |
# [7] base | |
# | |
# loaded via a namespace (and not attached): | |
# [1] httr_1.2.1 R6_2.1.2 plyr_1.8.4 | |
# [4] magrittr_1.5 parallel_3.3.1 tools_3.3.1 | |
# [7] igraph_1.0.1 RCurl_1.95-4.8 curl_1.1 | |
# [10] Rcpp_0.12.6 RJSONIO_1.3-0 BiocGenerics_0.18.0 | |
# [13] RCy3_1.2.0 bitops_1.0-6 stats4_3.3.1 | |
# [16] graph_1.50.0 | |
# | |
##### | |
# Cytoscape version: 3.4.0 | |
# Java version: 1.8.0_101 | |
# cyREST version: 3.3.4 | |
############################################################################################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################ | |
############################################################################################ | |
# Plotting networks in R - an example how to plot a network and | |
# customize its appearance in Cytoscape directly from R using | |
# the RCy3 package | |
############################################################################################ | |
############################################################################################ | |
# Clear workspace | |
# rm(list = ls()) | |
############################################################################################ | |
# Read a data set. | |
# Data format: dataframe with 3 variables; variables 1 & 2 correspond to interactions; variable 3 is weight of interaction | |
dataSet <- read.table("lesmis.txt", header = FALSE, sep = "\t") | |
# Create a graph. Use simplyfy to ensure that there are no duplicated edges or self loops | |
gD <- igraph::simplify(igraph::graph.data.frame(dataSet, directed=FALSE)) | |
# Print number of nodes and edges | |
# igraph::vcount(gD) | |
# igraph::ecount(gD) | |
############################################################################################ | |
# Calculate some node properties and node similarities that will be used to illustrate | |
# different plotting abilities | |
# Calculate degree for all nodes | |
degAll <- igraph::degree(gD, v = igraph::V(gD), mode = "all") | |
# Calculate betweenness for all nodes | |
betAll <- igraph::betweenness(gD, v = igraph::V(gD), directed = FALSE) / (((igraph::vcount(gD) - 1) * (igraph::vcount(gD)-2)) / 2) | |
betAll.norm <- (betAll - min(betAll))/(max(betAll) - min(betAll)) | |
rm(betAll) | |
#Calculate Dice similarities between all pairs of nodes | |
dsAll <- igraph::similarity.dice(gD, vids = igraph::V(gD), mode = "all") | |
############################################################################################ | |
# Add new node and edge attributes based on the calculated node properties/similarities | |
gD <- igraph::set.vertex.attribute(gD, "degree", index = igraph::V(gD), value = degAll) | |
gD <- igraph::set.vertex.attribute(gD, "betweenness", index = igraph::V(gD), value = betAll.norm) | |
# Check the attributes | |
# summary(gD) | |
F1 <- function(x) {data.frame(V4 = dsAll[which(igraph::V(gD)$name == as.character(x$V1)), which(igraph::V(gD)$name == as.character(x$V2))])} | |
dataSet.ext <- plyr::ddply(dataSet, .variables=c("V1", "V2", "V3"), function(x) data.frame(F1(x))) | |
gD <- igraph::set.edge.attribute(gD, "weight", index = igraph::E(gD), value = 0) | |
gD <- igraph::set.edge.attribute(gD, "similarity", index = igraph::E(gD), value = 0) | |
# The order of interactions in dataSet.ext is not the same as it is in dataSet or as it is in the edge list | |
# and for that reason these values cannot be assigned directly | |
for (i in 1:nrow(dataSet.ext)) | |
{ | |
igraph::E(gD)[as.character(dataSet.ext$V1) %--% as.character(dataSet.ext$V2)]$weight <- as.numeric(dataSet.ext$V3) | |
igraph::E(gD)[as.character(dataSet.ext$V1) %--% as.character(dataSet.ext$V2)]$similarity <- as.numeric(dataSet.ext$V4) | |
} | |
# Check the attributes | |
# summary(gD) | |
rm(dataSet,dsAll, i, F1) | |
############################################################################################ | |
# Now, let's do Cytoscape plots | |
# Here, we will use the cyPlot function to create a graphNEL object with nodes, edges, and their | |
# attributes from the corresponding node and edge data frames. | |
node.tbl <- data.frame(person = igraph::V(gD)$name, degree = igraph::V(gD)$degree, betweenness = igraph::V(gD)$betweenness) | |
edge.tbl <- dataSet.ext | |
colnames(edge.tbl) <- c("person_1", "person_2", "weight", "similarity") | |
# gD.cyt <- RCy3::cyPlot(node.tbl, edge.tbl) | |
# There were some issues with the original cyPlot function - it was assumed that there will be more than one | |
# data frame column of the character type or at least one numeric type column, | |
# and the lack of integer to numeric type conversion for edges, so I made changes and called a new function | |
# cyPlot_mod function | |
gD.cyt <- cyPlot_mod(node.tbl, edge.tbl) | |
# Next, we will create a new graph window in cytoscape | |
gDCW <- RCy3::CytoscapeWindow("Les Miserables using cyPlot", graph = gD.cyt, overwriteWindow = TRUE) | |
# We can display graph, with defaults color/size scheme | |
RCy3::displayGraph(gDCW) | |
# Now let's send/load node and edge attributes into Cytoscape | |
# Using the cyPlot function, all attributes are loaded into Cytoscape directly and we don't | |
# have to use additional functions to initialize or set node attributes | |
########## | |
# Now let's decide on a layout | |
# If you also want to choose a layout from R, a list of available layouts can be accessed as follow: | |
cy <- RCy3::CytoscapeConnection() | |
hlp <-RCy3::getLayoutNames(cy) | |
# We'll select the "fruchterman-rheingold" layout. This layout is the layout number 10 | |
# To see properties for the given layout, use: | |
# RCy3::getLayoutPropertyNames(cy, hlp[10]) | |
# We can choose any property we want and provide them as a list | |
RCy3::setLayoutProperties (gDCW, hlp[10], list (gravity_multiplier = 'similarity', nIterations = 1000)) | |
RCy3::layoutNetwork(gDCW, hlp[10]) | |
# RCy3::setLayoutProperties (gDCW, hlp[10], list (attraction_multiplier = 'similarity', nIterations = 1000)) | |
# RCy3::layoutNetwork(gDCW, hlp[10]) | |
# The layout looks different than before and does not change significalty based on whether we select gravity | |
# or attraction multiplier as its property | |
########## | |
# Finally, we can define rules for nodes: | |
RCy3::setNodeColorRule(gDCW, 'degree', c(min(degAll), mean(degAll), max(degAll)), c('#F5DEB3', '#FFA500', '#FF7F50', '#FF4500', '#FF0000'), mode = 'interpolate') | |
RCy3::setNodeSizeRule(gDCW, 'betweenness', c(min(betAll.norm), mean(betAll.norm), max(betAll.norm)), c(30, 45, 60, 80, 100), mode = 'interpolate') | |
# And edges: | |
RCy3::setEdgeLineWidthRule(gDCW, 'weight', dataSet.ext$V3, dataSet.ext$V3) | |
RCy3::setEdgeColorRule(gDCW, 'weight', c(min(as.numeric(dataSet.ext$V3)), mean(as.numeric(dataSet.ext$V3)), max(as.numeric(dataSet.ext$V3))), c('#FFFF00', '#00FFFF', '#00FF7F', '#228B22', '#006400'), mode='interpolate') | |
# While I get the "Successfully set rule" for both of the Edge rules, the view in the Cytoscape did not | |
# change accordning the rules - setEdgeLineWidthRule command did not make any changes and the | |
# setEdgeColorRule command made all edges white. | |
# Thus, this approach does not solve this issue | |
# We will define our own default color/size schema after we defined node and edge rules, due to | |
# possible issues when using rules | |
RCy3::setDefaultBackgroundColor(gDCW, '#D3D3D3') | |
RCy3::setDefaultEdgeColor(gDCW, '#CDC9C9') | |
RCy3::setDefaultEdgeLineWidth(gDCW, 4) | |
RCy3::setDefaultNodeBorderColor(gDCW, '#000000') | |
RCy3::setDefaultNodeBorderWidth(gDCW, 3) | |
RCy3::setDefaultNodeShape(gDCW, 'ellipse') | |
RCy3::setDefaultNodeColor(gDCW, '#87CEFA') | |
RCy3::setDefaultNodeSize(gDCW, 60) | |
RCy3::setDefaultNodeFontSize(gDCW, 20) | |
RCy3::setDefaultNodeLabelColor(gDCW, '#000000') | |
# Running these commands will set color to all edges back to black and set their width to 4, | |
# ignoring the rules specified above | |
############################################################################################ | |
sessionInfo() | |
# R version 3.3.1 (2016-06-21) | |
# Platform: x86_64-redhat-linux-gnu (64-bit) | |
# Running under: Fedora 23 (Workstation Edition) | |
# | |
# locale: | |
# [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C | |
# [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 | |
# [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 | |
# [7] LC_PAPER=en_US.UTF-8 LC_NAME=C | |
# [9] LC_ADDRESS=C LC_TELEPHONE=C | |
# [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C | |
# | |
# attached base packages: | |
# [1] stats graphics grDevices utils datasets methods | |
# [7] base | |
# | |
# loaded via a namespace (and not attached): | |
# [1] httr_1.2.1 R6_2.1.2 plyr_1.8.4 | |
# [4] magrittr_1.5 parallel_3.3.1 tools_3.3.1 | |
# [7] igraph_1.0.1 RCurl_1.95-4.8 curl_1.1 | |
# [10] Rcpp_0.12.6 RJSONIO_1.3-0 BiocGenerics_0.18.0 | |
# [13] RCy3_1.2.0 bitops_1.0-6 stats4_3.3.1 | |
# [16] graph_1.50.0 | |
# | |
##### | |
# Cytoscape version: 3.4.0 | |
# Java version: 1.8.0_101 | |
# cyREST version: 3.3.4 | |
############################################################################################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################ | |
############################################################################################ | |
# Plotting networks in R - an example how to plot a network and | |
# customize its appearance in Cytoscape directly from R using | |
# the RCy3 package | |
############################################################################################ | |
############################################################################################ | |
# Clear workspace | |
# rm(list = ls()) | |
############################################################################################ | |
# Read a data set. | |
# Data format: dataframe with 3 variables; variables 1 & 2 correspond to interactions; variable 3 is weight of interaction | |
dataSet <- read.table("lesmis.txt", header = FALSE, sep = "\t") | |
# Create a graph. Use simplyfy to ensure that there are no duplicated edges or self loops | |
gD <- igraph::simplify(igraph::graph.data.frame(dataSet, directed=FALSE)) | |
# Print number of nodes and edges | |
# igraph::vcount(gD) | |
# igraph::ecount(gD) | |
############################################################################################ | |
# Calculate some node properties and node similarities that will be used to illustrate | |
# different plotting abilities | |
# Calculate degree for all nodes | |
degAll <- igraph::degree(gD, v = igraph::V(gD), mode = "all") | |
# Calculate betweenness for all nodes | |
betAll <- igraph::betweenness(gD, v = igraph::V(gD), directed = FALSE) / (((igraph::vcount(gD) - 1) * (igraph::vcount(gD)-2)) / 2) | |
betAll.norm <- (betAll - min(betAll))/(max(betAll) - min(betAll)) | |
rm(betAll) | |
#Calculate Dice similarities between all pairs of nodes | |
dsAll <- igraph::similarity.dice(gD, vids = igraph::V(gD), mode = "all") | |
############################################################################################ | |
# Add new node and edge attributes based on the calculated node properties/similarities | |
gD <- igraph::set.vertex.attribute(gD, "degree", index = igraph::V(gD), value = degAll) | |
gD <- igraph::set.vertex.attribute(gD, "betweenness", index = igraph::V(gD), value = betAll.norm) | |
# Check the attributes | |
# summary(gD) | |
F1 <- function(x) {data.frame(V4 = dsAll[which(igraph::V(gD)$name == as.character(x$V1)), which(igraph::V(gD)$name == as.character(x$V2))])} | |
dataSet.ext <- plyr::ddply(dataSet, .variables=c("V1", "V2", "V3"), function(x) data.frame(F1(x))) | |
gD <- igraph::set.edge.attribute(gD, "weight", index = igraph::E(gD), value = 0) | |
gD <- igraph::set.edge.attribute(gD, "similarity", index = igraph::E(gD), value = 0) | |
# The order of interactions in dataSet.ext is not the same as it is in dataSet or as it is in the edge list | |
# and for that reason these values cannot be assigned directly | |
for (i in 1:nrow(dataSet.ext)) | |
{ | |
igraph::E(gD)[as.character(dataSet.ext$V1) %--% as.character(dataSet.ext$V2)]$weight <- as.numeric(dataSet.ext$V3) | |
igraph::E(gD)[as.character(dataSet.ext$V1) %--% as.character(dataSet.ext$V2)]$similarity <- as.numeric(dataSet.ext$V4) | |
} | |
# Check the attributes | |
# summary(gD) | |
rm(dataSet,dsAll, i, F1) | |
############################################################################################ | |
# Now, let's do Cytoscape plots | |
# Here, we will use the cyPlot function to create a graphNEL object with nodes, edges, and their | |
# attributes from the corresponding node and edge data frames. | |
devtools::install_github("tmuetze/Bioconductor_RCy3_the_new_RCytoscape") | |
library(RCy3) | |
node.tbl <- data.frame(person = igraph::V(gD)$name, degree = igraph::V(gD)$degree, betweenness = igraph::V(gD)$betweenness) | |
edge.tbl <- dataSet.ext | |
colnames(edge.tbl) <- c("person_1", "person_2", "weight", "similarity") | |
# gD.cyt <- cyPlot(node.tbl, edge.tbl) | |
# Some of the issues with the original cyPlot function are fixed, but it still lacks | |
# the integer to numeric type conversion for edges. So we will call the cyPlot_mod function again | |
gD.cyt <- cyPlot_mod(node.tbl, edge.tbl) | |
# Next, we will create a new graph window in cytoscape | |
gDCW <- CytoscapeWindow("Les Miserables using cyPlot from GitHub", graph = gD.cyt, overwriteWindow = TRUE) | |
# We can display graph, with defaults color/size scheme | |
displayGraph(gDCW) | |
# Now let's send/load node and edge attributes into Cytoscape | |
# Using the cyPlot function, all attributes are loaded into Cytoscape directly and we don't | |
# have to use additional functions to initialize or set node attributes | |
########## | |
# Now let's decide on a layout | |
# If you also want to choose a layout from R, a list of available layouts can be accessed as follow: | |
cy <- CytoscapeConnection() | |
hlp <-getLayoutNames(cy) | |
# We'll select the "fruchterman-rheingold" layout. This layout is the layout number 10 | |
# To see properties for the given layout, use: | |
# getLayoutPropertyNames(cy, hlp[10]) | |
# We can choose any property we want and provide them as a list | |
setLayoutProperties (gDCW, hlp[10], list (gravity_multiplier = 'similarity', nIterations = 1000)) | |
layoutNetwork(gDCW, hlp[10]) | |
# setLayoutProperties (gDCW, hlp[10], list (attraction_multiplier = 'similarity', nIterations = 1000)) | |
# layoutNetwork(gDCW, hlp[10]) | |
# The layout looks different than before and does not change significalty based on whether we select gravity | |
# or attraction multiplier as its property | |
########## | |
# Finally, we can define rules for nodes: | |
setNodeColorRule(gDCW, 'degree', c(min(degAll), mean(degAll), max(degAll)), c('#F5DEB3', '#FFA500', '#FF7F50', '#FF4500', '#FF0000'), mode = 'interpolate') | |
setNodeSizeRule(gDCW, 'betweenness', c(min(betAll.norm), mean(betAll.norm), max(betAll.norm)), c(30, 45, 60, 80, 100), mode = 'interpolate') | |
# And edges: | |
setEdgeLineWidthRule(gDCW, 'weight', dataSet.ext$V3, dataSet.ext$V3) | |
setEdgeColorRule(gDCW, 'weight', c(min(as.numeric(dataSet.ext$V3)), mean(as.numeric(dataSet.ext$V3)), max(as.numeric(dataSet.ext$V3))), c('#FFFF00', '#00FFFF', '#00FF7F', '#228B22', '#006400'), mode='interpolate') | |
# While I get the "Successfully set rule" for both of the Edge rules, the view in the Cytoscape did not | |
# change accordning the rules - setEdgeLineWidthRule command did not make any changes and the | |
# setEdgeColorRule command made all edges white. | |
# Thus, this approach does not solve this issue | |
# We will define our own default color/size schema after we defined node and edge rules, due to | |
# possible issues when using rules | |
setDefaultBackgroundColor(gDCW, '#D3D3D3') | |
setDefaultEdgeColor(gDCW, '#CDC9C9') | |
setDefaultEdgeLineWidth(gDCW, 4) | |
setDefaultNodeBorderColor(gDCW, '#000000') | |
setDefaultNodeBorderWidth(gDCW, 3) | |
setDefaultNodeShape(gDCW, 'ellipse') | |
setDefaultNodeColor(gDCW, '#87CEFA') | |
setDefaultNodeSize(gDCW, 60) | |
setDefaultNodeFontSize(gDCW, 20) | |
setDefaultNodeLabelColor(gDCW, '#000000') | |
# Running these commands will set color to all edges back to black and set their width to 4, | |
# ignoring the rules specified above | |
############################################################################################ | |
sessionInfo() | |
# R version 3.3.1 (2016-06-21) | |
# Platform: x86_64-redhat-linux-gnu (64-bit) | |
# Running under: Fedora 23 (Workstation Edition) | |
# | |
# locale: | |
# [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 | |
# [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 | |
# [7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C | |
# [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C | |
# | |
# attached base packages: | |
# [1] parallel stats graphics grDevices utils datasets methods base | |
# | |
# other attached packages: | |
# [1] RCy3_1.2.1 graph_1.50.0 BiocGenerics_0.18.0 | |
# | |
# loaded via a namespace (and not attached): | |
# [1] Rcpp_0.12.6 withr_1.0.2 digest_0.6.10 bitops_1.0-6 plyr_1.8.4 | |
# [6] R6_2.1.2 git2r_0.15.0 stats4_3.3.1 magrittr_1.5 BiocInstaller_1.22.3 | |
# [11] httr_1.2.1 curl_1.1 devtools_1.12.0 RJSONIO_1.3-0 tools_3.3.1 | |
# [16] RCurl_1.95-4.8 igraph_1.0.1 memoise_1.0.0 | |
# | |
##### | |
# Cytoscape version: 3.4.0 | |
# Java version: 1.8.0_101 | |
# cyREST version: 3.3.4 | |
############################################################################################ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment