Identify (Filter) Edges by Attribute

(Note: Please review the companion recipe, Identify (Filter) Nodes by Attribute to gain a basic acquaintance with node attribute display and its use in selecting (identifying, filtering) a subset of all nodes. This recipe presumes you understand that recipe, and adds edge attribute specific information.)

A frequent need in graph exploration is to identify all the edges 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 edges whose score attribute is 0 or greater. There are two such edges in the simple graph.

We have to accomodate the difference in edge naming found in Cytoscape and R. A typical edge name in Cytoscape (using our simple graph as an example) is

"A (phosphorylates) B"
In R, the edge is less revealingly named:
To further confuse the issue, the Bioc graph packages sometimes use the name
RCytoscape and Cytoscape use the longer edge name by privileging one of the attributes: this attribute is so fundamental, and so broadly used, that its value becomes part of the edge name. However, the complexity goes a little further. In Cytoscape, the priviledged edge attribute is "interaction". In RCytoscape, it is "edgeType". This difference is managed by the conversion functions in RCytoscape, and should not concern most users.

If you identify edges in the R graph, and wish to use those edge names in the Cytoscape network, you must convert the R edge names to the Cytoscape edge names.

Select Edges in the Cytoscape Network by Attribute Values in the R Graph

The basic procedure:
  1. Identify edges based upon their attribute values in the R graph
  2. Convert the edge names to the Cytoscape form
  3. Select those edges in Cytoscape (or perhaps control their rendering, or get their target nodes, or ....)
  g = makeSimpleGraph ()
  cw = new.CytoscapeWindow ('Edge Selection Demo', g)
  displayGraph (cw)
  layoutNetwork (cw)
  redraw (cw)
  eda.names (g)
      # [1] "edgeType" "score"    "misc"

  eda (g, 'score')
      #  A|B B|C C|A
      #  35 -12   0

  which (eda (g, 'score') >= 0)
      #  A|B C|A
      #   1   3

  names (which (eda (g, 'score') >= 0))   
      # [1] "A|B" "C|A"

  cy2.edge.names (g, names (which (eda (g, 'score') >= 0)))
      #                   A~B                    C~A
      # "A (phosphorylates) B"      "C (undefined) A"

  as.character (cy2.edge.names (g, names (which (eda (g, 'score') >= 0))))
      # [1] "A (phosphorylates) B" "C (undefined) A"

  selectEdges (cw, as.character (cy2.edge.names (g, names (which (eda (g, 'score') >= 0)))))

  getSelectedEdges (cw)
      # [1] "A (phosphorylates) B" "C (undefined) A"