Search code examples
rgraphcytoscape

How to add node size as legend in Cytoscape 3?


From an R function (cnetplot) I've obtained the following image which does not look very nice. Plot_R_Package Therefore, I extracted the data from the R object and wrote a script to create an equivalent network data file that is readable by Cytoscape. The following equivalent plot from Cytoscape looks much better but the problem is that I am not able to add legends based on node size in Cytoscape as the R function did. I tried with Legend Creator app in cytoscspe but couldn't do it. Plot_Cytoscape The original data and R code to reproduce the plots can be found in the following link. ftp://ftp.lrz.de/transfer/Data_For_Plot/StackOverflow/

I looked into this Mapping nodes sizes and adding legends in cytoscape network, but in that case questioner already was able to load the node sizes as legends in cytoscape and moreover, he/she used a python package.

Any suggestions will highly be appreciated


Solution

  • Here's a little R script that will generate a min/max node size legend. You'll need to set the first variable to the name of the Visual Style in your network. This one works with the sample session file, "Yeast Perturbation.cys" if you want to test it there first.

    If you are familiar with RCy3, then it should be self-explanatory. You can customize the positioning of the nodes, labels and label font size, etc. You can even adapt it to generate intermediate values (like in your example above) if you want.

    NOTE: This adds nodes to your network. If you run a layout after adding these, then they will be moved! If you rely on node counts or connectivity measures, then these will affect those counts! Etc.

    If you find this uesful, I might try to add it as helper function in the RCy3 package. Let me know if you have feedback or questions.

    # https://bioconductor.org/packages/release/bioc/html/RCy3.html
    library(RCy3)
    
    # Set your current style name
    style.name <- "galFiltered Style"
    
    # Extract min and max node size
    res<-cyrestGET(paste0("styles/",style.name,"/mappings/NODE_SIZE"))
    size.col <- res$mappingColumn
    min.size <- res$points[[1]]$equal
    min.value <- res$points[[1]]$value
    max.size <- res$points[[length(res$points)]]$equal
    max.value <- res$points[[length(res$points)]]$value
    
    # Prepare as data.frame
    legend.df <-data.frame(c(min.size, max.size), c(min.value, max.value))
    colnames(legend.df) <- c("legend.label",size.col)
    rownames(legend.df) <- c("legend.size.min", "legend.size.max")
    
    # Add legend nodes and data
    addCyNodes(c("legend.size.min", "legend.size.max"))
    loadTableData(legend.df)
    
    # Style and position
    setNodeColorBypass(c("legend.size.min", "legend.size.max"),"#000000")
    setNodePropertyBypass(c("legend.size.min", "legend.size.max"), 
                          c("E,W,l,5,0", "E,W,l,5,0"),  # node_anchor, label_anchor, justification, x-offset, y-offset
                          "NODE_LABEL_POSITION")
    setNodeLabelBypass(c("legend.size.min", "legend.size.max"), legend.df$legend.label)
    setNodePropertyBypass("legend.size.max", 
                          as.numeric(max.size)/2 + as.numeric(min.size)/2 + 10, # vertical spacing
                          "NODE_Y_LOCATION")
    setNodeFontSizeBypass(c("legend.size.min", "legend.size.max"), c(20,20))