Author: mkirby
Date: 2012-07-11 15:15:01 -0700 (Wed, 11 Jul 2012)
New Revision: 29846

Modified:
   csplugins/trunk/soc/mkirby/R/cytobridge.R
Log:
mkirby: R Updated.

Modified: csplugins/trunk/soc/mkirby/R/cytobridge.R
===================================================================
--- csplugins/trunk/soc/mkirby/R/cytobridge.R   2012-07-11 22:13:33 UTC (rev 
29845)
+++ csplugins/trunk/soc/mkirby/R/cytobridge.R   2012-07-11 22:15:01 UTC (rev 
29846)
@@ -1,51 +1,84 @@
-#make your igraph
-g1 <- graph.ring(100000)
+#------------------------------
+#CytoBridge -------------------
+#Author: Michael Kirby --------
+#Depends On: iGraph, XMLRPC ---
+#------------------------------
 
-#The Cytobridge class
-setClass(Class="cytobridge", 
-          representation=representation(a="ANY", s="ANY"),
-          prototype=prototype(a=graph(c(0,1))),
-          validity=function(object) {
-              if(class(object@a)!="igraph") {
-                    return(paste("Expected iGraph but got ", class(object@a)))
-                } else {
-                    return(TRUE)
-                }
-          })
+#Pushes the given iGraph to Cytoscape and returns the iGraph with extra 
CytoBridge
+#Consistency attributes.
+#name: The name to link this graph to with CytoBridge.
+#g: The iGraph to push.
+#tables: TRUE if push tables also, FALSE otherwise.
+pushNetwork <- function (name,g, tables=TRUE) {
+       if (!('cytobid' %in% list.graph.attributes(g))) {
+               cytob.suid <- 0
+               V(g)$cytobid <- seq(cytob.suid,cytob.suid+length(V(g))-1)
+               cytob.suid <- cytob.suid + length(V(g))-1
+               E(g)$cytobid <- seq(cytob.suid,cytob.suid+length(E(g))-1)
+               cytob.suid <- cytob.suid + length(E(g))-1
+               g <- set.graph.attribute(g, "cytobid", as.integer(cytob.suid))
+       } else {
+               cytob.suid <- get.graph.attribute(g, "cytobid")
+               for(n in 1:length(V(g))) {
+                       if (is.na(get.vertex.attribute(g,'cytobid',n))) {
+                               cytob.suid <- cytob.suid +  1
+                               g <- 
set.vertex.attribute(g,'cytobid',n,as.integer(cytob.suid))
+                       }
+               }
 
-#Returns the graph of this CytoBridge
-setGeneric(name="getGraph", def=function(x) standardGeneric("getGraph"))
-        setMethod(f="getGraph", signature="cytobridge", definition=function(x) 
{
-                return(x@a)
-        }) 
+               for(e in 1:length(E(g))) {
+                       if (is.na(get.edge.attribute(g,'cytobid',e))) {
+                               cytob.suid <- cytob.suid +  1
+                               g <- 
set.edge.attribute(g,'cytobid',e,as.integer(cytob.suid))
+                       }
+               }
+               g <- set.graph.attribute(g, "cytobid", as.integer(cytob.suid))
+       }
+       xml.rpc('localhost:9000', 'Cytoscape.pushNetwork', name, 
get.vertex.attribute(g, 'cytobid'), get.edge.attribute(g, 'cytobid'),  
get.vertex.attribute(g,"cytobid",get.edges(g,E(g))[,1]), 
get.vertex.attribute(g,"cytobid",get.edges(g,E(g))[,2]))
+       if (tables) { pushTables(name, g) }
+       g
+}
 
-#Send the graph data to the communication Layer
-setGeneric(name="update", def=function(x) standardGeneric("update"))
-        setMethod(f="update", signature="cytobridge",
-            definition=function(x) {
-                   socket <- make.socket("localhost", "4444")
-                   on.exit(close.socket(socket))
-                   write.socket(socket, 
paste(as.character(x@s),toString(x@a),"endRSend"))
-                   close.socket(socket)
-        })
+#Pushes the given iGraphs attributes to Cytoscape.
+#name: The name this graph is linked to with CytoBridge.
+#g: The iGraph with the attributes to push.
+#net: TRUE to push network table.
+#node: TRUE to push node table.
+#edge: TRUE to push edge table.
+pushTables <- function (name,g, net=FALSE, node=FALSE, edge=FALSE) {
+       if (!net && !node && !edge) {
+               net = TRUE
+               node = TRUE
+               edge = TRUE
+       }
+       if (net) {
+               gdata <- c()
+               for(i in 1:length(list.graph.attributes(g))) {
+                       gdata <- append(gdata, 
get.graph.attribute(g,list.graph.attributes(g)[i]))
+               }
+               xml.rpc('localhost:9000', 'Cytoscape.pushNetTable', name, 
as.vector(list.graph.attributes(g)), as.character(gdata))
+       }
+       if (node) {
+               vdata <- c()
+               for(v in 1:length(list.vertex.attributes(g))) {
+                       vdata <- append(vdata, 
get.vertex.attribute(g,list.vertex.attributes(g)[v]))
+               }
+               xml.rpc('localhost:9000', 'Cytoscape.pushNodeTable', name, 
as.vector(list.vertex.attributes(g)), get.vertex.attribute(g, 'cytobid'), 
as.character(vdata))
+       }
+       if (edge) {
+               edata <- c()
+               for(e in 1:length(list.edge.attributes(g))) {
+                       edata <- append(edata, 
get.edge.attribute(g,list.edge.attributes(g)[e]))
+               }
+               xml.rpc('localhost:9000', 'Cytoscape.pushEdgeTable', name, 
as.vector(list.edge.attributes(g)), get.edge.attribute(g, 'cytobid'), 
as.character(edata))
+       }
+}
 
-setGeneric(name="connect", def=function(x) standardGeneric("connect"))
-        setMethod(f="connect", signature="cytobridge",
-            definition=function(x) {
-                   x@s <- 1234 #some unique id
-                   return(x)
-        })
-
-setGeneric(name="close", def=function(x) standardGeneric("close"))
-        setMethod(f="close", signature="cytobridge",
-            definition=function(x) {
-                   socket <- make.socket("localhost", "4444")
-                   on.exit(close.socket(socket))
-                   write.socket(socket, "die")
-                   close.socket(socket)
-                x@s <- 0 #resets id
-        })
-
-test <- new("cytobridge",a=g1)
-test <- connect(test)
-update(test)
\ No newline at end of file
+#Pushes the specified dataframe as a table to Cytoscape.
+pushTable <- function (name, df) {     
+       data <- c()
+       for(i in 1:length(names(df))) {
+               data <- append(data, as.vector(t(df[i])))
+       }
+       xml.rpc('localhost:9000', 'Cytoscape.pushTable', name, 
as.vector(names(df)), as.character(data))
+}
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to