cytoscape / cyREST

Core App: REST API module for Cytoscape
https://github.com/cytoscape/cyREST/wiki
MIT License
30 stars 13 forks source link

Pause needed after network creation #106

Open AlexanderPico opened 3 years ago

AlexanderPico commented 3 years ago

I think we have a synchronicity issue (see https://github.com/cytoscape/RCy3/issues/98).

Can you help me diagnose if the issue is with CyREST or Cytoscape? Ideas for how to fix this?

AlexanderPico commented 3 years ago

Additional details. This line throws an error sometimes...

https://github.com/cytoscape/RCy3/blob/master/R/Networks.R#L992

The prior call to CyREST occurs on this line:

https://github.com/cytoscape/RCy3/blob/master/R/Networks.R#L956

I suspect that CyREST is returning before the network view is actually ready.

AlexanderPico commented 3 years ago

And here's a little troubleshooting script in R. Run this with RCy3 loaded and it should throw an error, usually in the first or second attempt.

https://www.dropbox.com/s/q61hg6bqk15d085/cyrest.R?dl=0

Note that adding a sleep function before attempting to apply the visual style will completely "fix" the problem.

AlexanderPico commented 3 years ago

Confirmed error in 3.8.0 and 3.8.1.

AlexanderPico commented 3 years ago

@dotasek Any ideas on this one? Would be great to include a simple fix in 3.8.2.

dotasek commented 3 years ago

I added a tunable to set the network for the apply style endpoint. This goes around the issue of selectedNetworkViews. The committed code is on this branch of cytoscape-impl: https://github.com/cytoscape/cytoscape-impl/tree/cyrest-106

Your original code needs a slight modification to work without a pause:

## Troubleshooting for https://github.com/cytoscape/cyREST/issues/106

# This script will create a network and then attempt to apply a visual style.
# If the network view is not ready yet, then it will throw an error. This
# behavior is inconsistent to repeated attempts may be needed to throw error.

library(RCy3)

## parameters
base.url <- 'http://127.0.0.1:1234/v1'
title <- "test network"
collection <- "test collection"
nodes <- data.frame(id=c("node 0","node 1","node 2","node 3"),
                    group=c("A","A","B","B"), 
                    score=as.integer(c(20,10,15,5)))

.nodeSet2JSON <- function(node.set, node.id.list = 'id') {
  counter <- 0
  size <- 1
  json_set <- c()

  for (i in seq_len(dim(node.set)[1])) {
    rest <- list()
    for (j in seq_len(dim(node.set)[2])) {
      rest[[colnames(node.set)[j]]] = node.set[i, j]
    }
    current_node = list("data" = rest)
    counter <- counter + 1
    json_set[[counter]] <- current_node
  }
  return(json_set[seq_len(counter)])
}

json_nodes <- .nodeSet2JSON(nodes["id"])

json_edges <- "[]"
json_network <- list(data = list(name = title),
                     elements = c(
                       nodes = list(json_nodes),
                       edges = list(json_edges)
                     ))

for(n in 1:5){
  ## create network
  network.suid <- cyrestPOST('networks',
                             parameters = list(
                               title = title, 
                               collection = collection),
                             body = json_network,
                             base.url = base.url)

  ## NOTE: A LITTLE SLEEP WILL "FIX" THE PROBLEM
  #commandSleep(2)

  ## apply style
  commandsPOST('vizmap apply network="CURRENT" styles="default"', base.url = base.url)
}
closeSession(F, base.url = base.url)