tmuetze / Bioconductor_RCy3_the_new_RCytoscape

Update RCytoscape to work for Cytoscape 3.0 and higher using CyREST
16 stars 10 forks source link

Is there a way to change the name of a network using Rcy3? #30

Open risserlin opened 8 years ago

risserlin commented 8 years ago

I am creating my networks from R using a command through cyrest. The App automatically generates a name for the network but I am creating a whole bunch of networks from R and i would like to rename the created network after it is created so I can keep track of them in R better.
I can't find a function that will rename the network. Is there a way to do this? Thanks, Ruth

jooolia commented 8 years ago

Hi Ruth,

Great question. Others (such as @tmuetze ) likely know better than I, but I haven't found an easy way to rename networks in RCy3. I think it would be something we could easily include in the next version. I'll make more robust versions of these functions for the release, but for now please see below for functions that should work with RCy3 and might be helpful.

library(RCy3)

selectAllNodes <- function(obj) {

  resource.uri <- paste(obj@uri,
                        pluginVersion(obj),
                        "networks",
                        obj@window.id,
                        "nodes",
                        sep = "/")

  request.res_nodes <- GET(resource.uri) ## returns all of the node suids
  all_node_SUIDs <- fromJSON(rawToChar(request.res_nodes$content))
  SUID.value.pairs <- lapply(all_node_SUIDs,
                             function(s) {list('SUID'=s, 'value'=TRUE)})
  SUID.value.pairs.JSON <- toJSON(SUID.value.pairs)

  resource.uri <- paste(obj@uri,
                        pluginVersion(obj),
                        "networks",
                        obj@window.id,
                        "tables/defaultnode/columns/selected",
                        sep = "/")
  request.res <- PUT(url=resource.uri,
                     body=SUID.value.pairs.JSON,
                     encode="json")
  invisible(request.res)
}

selectAllEdges <- function(obj) {

  resource.uri <- paste(obj@uri,
                        pluginVersion(obj),
                        "networks",
                        obj@window.id,
                        "edges",
                        sep = "/")

  request.res_edges <- GET(resource.uri) ## returns all of the edge suids
  all_edge_SUIDs <- fromJSON(rawToChar(request.res_edges$content))
  SUID.value.pairs <- lapply(all_edge_SUIDs,
                             function(s) {list('SUID'=s, 'value'=TRUE)})
  SUID.value.pairs.JSON <- toJSON(SUID.value.pairs)

  resource.uri <- paste(obj@uri,
                        pluginVersion(obj),
                        "networks",
                        obj@window.id,
                        "tables/defaultedge/columns/selected",
                        sep = "/")
  request.res <- PUT(url=resource.uri,
                     body=SUID.value.pairs.JSON,
                     encode="json")
  invisible(request.res)
}

copyCytoscapeNetwork <- function(obj,
                                 new_title) {
  selectAllNodes(obj)
  selectAllEdges(obj)
  ## makes a new network from all nodes and edges
  request.uri <- paste(obj@uri,
                       pluginVersion(obj),
                       "networks",
                       obj@window.id,
                       sep = "/")

  request.res <- POST(url = request.uri,
                      query = list(title = new_title))

  invisible(request.res)

   connect_window <- existing.CytoscapeWindow(new_title,
                                                       copy.graph.from.cytoscape.to.R = FALSE)
   return(connect_window)
}

renameCytoscapeNetwork <- function(obj, new_title){
  new_net <- copyCytoscapeNetwork(obj, new_title)
  deleteWindow(obj, obj@title)
  return(new_net)
}

copy_of_your_net <- copyCytoscapeNetwork(your_network, "new_copy")

renamed_net <- renameCytoscapeNetwork(your_network, "renamed_network")