Identify (Filter) Nodes by Attribute

A frequent need in graph exploration is to identify all the nodes which meet a certain criteria. After identifying them, you may wish to select them in the display, delete them, retrieve other attributes, or perhaps render them specially. Here we show how to examine and extract nodes whose count attribute exceeds 50. There is only one such node in the simple graph.

Node attributes are handled much the same in Cytoscape and the R graph classes. Arbitrary attributes of a few basic types (integer, double, character string) may be defined, added, and retrieved. Here we show how to identify nodes of a graph which meet criteria based upon those attributes. We use our standard demonstration, the 3-node, 3-edge graph returned by RCytoscape's makeSimpleGraph ().



  library (RCytoscape)
  g = makeSimpleGraph ()
  cw = new.CytoscapeWindow ('Filter Demo', graph=g)
  displayGraph (cw); 
  layoutNetwork (cw); 
  redraw (cw)
  noa.names (g)  # display the node attribute names 
 
  [1] "type"  "lfc"   "label" "count"
  
  noa (g, 'count')  # display the count attribute values

      A     B     C
      2    30   100


  noa (g, 'count') > 50
 
      A     B     C
  FALSE FALSE  TRUE
  
  which (noa (g, 'count') > 50)
 
    C
    3
  
  names (which (noa (g, 'count') > 50))
 
   [1] "C"
  
  selectNodes (cw, names (which (noa (g, 'count') > 50)))  

Some Other Random Examples

  1. A graph 'g.kegg' contains enzymes in metabolic order, with genes which code for the enzymes connected by edges. Extract the genes only:
    
      head (noquote (names (which (noa (g.kegg, 'moleculeType') == 'gene'))))
         [1] YGR192C YJL052W YJR009C YER073W YOR374W YPL061W