First export your Cytoscape file to an XML file. This can be done in Cytoscape under file> export> network. XML is a superset of HTML, this file contains the positional information for each node in your network along with the edges, color and shape of each edge and node and any other graphical information you can think of.
With the Cytoscape file in XML format we can then parse this file in python using Beautiful Soup!
Beautiful Soup is an XML parser written for python. First import Beautiful Soup and tell it the format.
from bs4 import BeautifulSoup
soup = BeautifulSoup(xgmml)
You can then select all the nodes or edges from you XML file using soup.find_all
edges = soup.find_all("edge")
print edges[0]
<edge cy:directed="1" id="412" label="MYB46 (PD) C4H" source="145" target="123">
<att name="interaction" type="string" value="PD"></att>
<att name="shared name" type="string" value="MYB46 (PD) C4H"></att>
<att name="selected" type="boolean" value="0"></att>
<att name="name" type="string" value="MYB46 (PD) C4H"></att>
<att name="shared interaction" type="string" value="PD"></att>
And Search for nodes or edges that match using with by getting the nodes label
edge = edge[0]
print edge.get("label")
MYB46 (PD) C4H
You can then change the color of a node for example by grabbing the graphics tag and
graphics_tag = edges.graphics
graphics_tag["fill"] = "#eb2200"
The graphics tag also contain other information inducing arrow types and others shown below:
<graphics fill="#999999" width="2.0">
<att name="EDGE_TARGET_ARROW_SHAPE" type="string" value="NONE"></att>
<att name="EDGE_LABEL_FONT_SIZE" type="string" value="10"></att>
<att name="EDGE_LABEL_COLOR" type="string" value="#000000"></att>
<att name="EDGE_LABEL_TRANSPARENCY" type="string" value="255"></att>
<att name="EDGE_SELECTED" type="string" value="false"></att>
<att name="EDGE_TOOLTIP" type="string" value=""></att>
<att name="EDGE_SOURCE_ARROW_UNSELECTED_PAINT" type="string" value="#000000"></att>
<att name="EDGE_TARGET_ARROW_UNSELECTED_PAINT" type="string" value="#999999"></att>
<att name="EDGE_LABEL_FONT_FACE" type="string" value="Dialog,plain,10"></att>
<att name="EDGE_LINE_TYPE" type="string" value="SOLID"></att>
<att name="EDGE_VISIBLE" type="string" value="true"></att>
<att name="EDGE_BEND" type="string" value=""></att>
<att name="EDGE_SOURCE_ARROW_SELECTED_PAINT" type="string" value="#ffff00"></att>
<att name="EDGE_CURVED" type="string" value="true"></att>
<att name="EDGE_SOURCE_ARROW_SHAPE" type="string" value="NONE"></att>
<att name="EDGE_TARGET_ARROW_SELECTED_PAINT" type="string" value="#ffff00"></att>
<att name="EDGE_STROKE_SELECTED_PAINT" type="string" value="#ff0000"></att>
<att name="EDGE_TRANSPARENCY" type="string" value="255"></att>
<att name="EDGE_LABEL" type="string" value=""></att>
</graphics>
I have used this code for many things and made if available on github here. I used this code to color feed forward loops and stress expression response in our Xylem Secondary Cell wall Network recently published in Nature.
Here is our Xylem Secondary Cell wall Network before changes:
The feed forward loops in our network are highlighted in Red here:
Here is the same network with salt stress microarray data overlaid where green shows positive correlation between transcription factors/ nodes and red negative.
An Arabidopsis gene regulatory network for secondary cell wall synthesis M Taylor-Teeples, L Lin, M de Lucas, G Turco, TW Toal… - Nature, 2014