Style


In this section, you can learn how to apply default style or your original style. And you can also understand how to apply descrete or continuous mapping.

Table of contents


  • Get the default Style and apply it
  • Get visual propeties
  • Create original Style and apply it
  • Create passthrough mapping
  • Create continuous mapping
  • Create descrete mapping

Prepare network data


To execute this cookbook's example, first you prepare network data.


In [75]:
from py2cytoscape.data.cynetwork import CyNetwork
from py2cytoscape.data.cyrest_client import CyRestClient
from py2cytoscape.data.style import StyleUtil
import py2cytoscape.util.cytoscapejs as cyjs
import py2cytoscape.cytoscapejs as renderer

import networkx as nx
import pandas as pd
import json
from IPython.display import Image

# Create py2cytoscape client
cy = CyRestClient()

# Reset
cy.session.delete()

# Load network from somewhere
yeast_net = cy.network.create_from('../sampleData/galFiltered.sif')

# Load table as pandas' DataFrame
table_data = pd.read_csv('../sampleData/sample_data_table.csv', index_col=0)
table_data.head()


Out[75]:
alias annotation.DB_Object_Name annotation.GO BIOLOGICAL_PROCESS annotation.GO CELLULAR_COMPONENT annotation.GO MOLECULAR_FUNCTION label kegg uniprot sgd entrez
YAL003W translation elongation factor EF-1beta, EF-1 b... EF-1 beta, translation elongation factor EF-1beta translational elongation eukaryotic translation elongation factor 1 com... translation elongation factor activity EF1B sce:YAL003W EF1B_YEAST S000000003 851260
YAL030W S000000028, SNC1 NaN Golgi to plasma membrane transport, endocytosi... Golgi trans face, SNARE complex, endosome, tra... v-SNARE activity SNC1 sce:YAL030W SNC1_YEAST S000000028 851203
YAL038W CDC19, PYK1, S000000036, pyruvate kinase pyruvate kinase glycolysis, pyruvate metabolic process cytosol pyruvate kinase activity KPYK1 sce:YAL038W KPYK1_YEAST S000000036 851193
YAL040C CLN3, DAF1, FUN10, G1 cyclin, S000000038, WHI1 G1 cyclin G1/S transition of mitotic cell cycle, regulat... nucleus cyclin-dependent protein kinase regulator acti... CG13 sce:YAL040C CG13_YEAST S000000038 851191
YAR007C heterotrimeric RPA (RF-A) single-stranded DNA ... RF-A, heterotrimeric RPA (RF-A) single-strande... DNA recombination, DNA replication, synthesis ... DNA replication factor A complex, chromosome, ... damaged DNA binding, single-stranded DNA binding RFA1 sce:YAR007C RFA1_YEAST S000000065 851266

In [76]:
# Merge them in Cytoscape
yeast_net.update_node_table(df=table_data, network_key_col='name')

# Apply layout
cy.layout.apply(name='degree-circle', network=yeast_net)

# Show it
yeast_net_png = yeast_net.get_png()
Image(yeast_net_png)


Out[76]:

Get the default Style and apply it


In Cytoscape, there are several useful default styles. You can get the list of the style and apply the style that you want.

Get the default Style


First, to know the available default style, let's get all of the default style in Cytoscape. If you add some original style, you can get that style by this method. However, now we don't make any style, so we can only get the default style here.


In [63]:
# Get the all Style.
cy.style.get_all()


Out[63]:
['Curved',
 'Sample2',
 'Nested Network Style',
 'Ripple',
 'default',
 'Marquee',
 'Minimal',
 'Sample1',
 'Big Labels',
 'Sample3',
 'Gradient1',
 'Solid',
 'default black',
 'Directed',
 'Universe']

Apply default style network


Now, we know the available style, so let's apply the default style network.


In [78]:
# Apply 'default black' Style.
cy.style.apply(style = cy.style.create('default black'), network = yeast_net)

# Show it
yeast_net_png = yeast_net.get_png()
Image(yeast_net_png)


Out[78]:

In [77]:
# Apply 'Ripple' Style.
cy.style.apply(style = cy.style.create('Nested Network Style'), network = yeast_net)

# Show it
yeast_net_png = yeast_net.get_png()
Image(yeast_net_png)


Out[77]:

Get visual propeties


To create your original style, we have to set value to visual propeties. First, we should know the kind of visual propeties and there are several visual propeties in each node, edge and network.

Get all visual propeties


We can get all visual propeties.


In [66]:
# Get all available Visual Properties
vps = cy.style.vps.get_all()

# Show it
print(json.dumps(vps, indent=4))


{
    "NODE_LABEL_POSITION": "Node Label Position",
    "NETWORK_TITLE": "Network Title",
    "NODE_CUSTOMPAINT_4": "Node Custom Paint 4",
    "NETWORK_BACKGROUND_PAINT": "Network Background Paint",
    "NODE_CUSTOMGRAPHICS_9": "Node Image/Chart 9",
    "NODE_HEIGHT": "Node Height",
    "NODE_CUSTOMPAINT_9": "Node Custom Paint 9",
    "NODE_VISIBLE": "Node Visible",
    "EDGE_CURVED": "Edge Curved",
    "EDGE_SELECTED": "Edge Selected",
    "NODE_Z_LOCATION": "Node Z Location",
    "EDGE_TOOLTIP": "Edge Tooltip",
    "NODE_X_LOCATION": "Node X Location",
    "NODE_CUSTOMPAINT_7": "Node Custom Paint 7",
    "NODE_CUSTOMGRAPHICS_5": "Node Image/Chart 5",
    "NODE_FILL_COLOR": "Node Fill Color",
    "NETWORK_SIZE": "Network Size",
    "COMPOUND_NODE_PADDING": "Padding (Compound Node)",
    "NODE_CUSTOMPAINT_3": "Node Custom Paint 3",
    "NETWORK_NODE_SELECTION": "Network Node Selection",
    "NODE_CUSTOMGRAPHICS_2": "Node Image/Chart 2",
    "NETWORK_SCALE_FACTOR": "Network Scale Factor",
    "NETWORK_DEPTH": "Network Depth",
    "EDGE_SOURCE_ARROW_SELECTED_PAINT": "Edge Source Arrow Selected Paint",
    "NODE_CUSTOMGRAPHICS_3": "Node Image/Chart 3",
    "NODE_DEPTH": "Node Depth",
    "EDGE_WIDTH": "Edge Width",
    "NODE_LABEL_COLOR": "Node Label Color",
    "NODE_TOOLTIP": "Node Tooltip",
    "NODE_CUSTOMGRAPHICS_POSITION_4": "Node Image/Chart Position 4",
    "NETWORK_EDGE_SELECTION": "Network Edge Selection",
    "NODE_PAINT": "Node Paint",
    "NODE_NESTED_NETWORK_IMAGE_VISIBLE": "Nested Network Image Visible",
    "EDGE_SOURCE_ARROW_SHAPE": "Edge Source Arrow Shape",
    "NODE_BORDER_STROKE": "Node Border Line Type",
    "NODE_BORDER_TRANSPARENCY": "Node Border Transparency",
    "NETWORK_CENTER_Z_LOCATION": "Network Center Z Location",
    "EDGE_TARGET_ARROW_SELECTED_PAINT": "Edge Target Arrow Selected Paint",
    "NODE_CUSTOMGRAPHICS_POSITION_9": "Node Image/Chart Position 9",
    "EDGE_PAINT": "Edge Paint",
    "NETWORK": "Network Visual Property",
    "NODE_SELECTED_PAINT": "Node Selected Paint",
    "NODE_CUSTOMGRAPHICS_8": "Node Image/Chart 8",
    "EDGE_SOURCE_ARROW_UNSELECTED_PAINT": "Edge Source Arrow Unselected Paint",
    "EDGE_TARGET_ARROW_UNSELECTED_PAINT": "Edge Target Arrow Unselected Paint",
    "NODE_CUSTOMGRAPHICS_SIZE_9": "Node Image/Chart Size 9",
    "EDGE_LABEL_FONT_SIZE": "Edge Label Font Size",
    "EDGE_LABEL_WIDTH": "Edge Label Width",
    "EDGE_VISIBLE": "Edge Visible",
    "NODE_CUSTOMGRAPHICS_4": "Node Image/Chart 4",
    "NODE_CUSTOMGRAPHICS_SIZE_1": "Node Image/Chart Size 1",
    "NODE_CUSTOMGRAPHICS_1": "Node Image/Chart 1",
    "COMPOUND_NODE_SHAPE": "Shape (Compound Node)",
    "NODE_LABEL_WIDTH": "Node Label Width",
    "EDGE_LABEL_TRANSPARENCY": "Edge Label Transparency",
    "NODE_CUSTOMGRAPHICS_POSITION_5": "Node Image/Chart Position 5",
    "NODE_CUSTOMGRAPHICS_SIZE_3": "Node Image/Chart Size 3",
    "NETWORK_WIDTH": "Network Width",
    "NODE_CUSTOMPAINT_1": "Node Custom Paint 1",
    "NODE": "Node Visual Property",
    "EDGE_LABEL_COLOR": "Edge Label Color",
    "NODE_CUSTOMGRAPHICS_SIZE_8": "Node Image/Chart Size 8",
    "EDGE_STROKE_UNSELECTED_PAINT": "Edge Stroke Color (Unselected)",
    "NODE_SHAPE": "Node Shape",
    "EDGE_LABEL": "Edge Label",
    "EDGE_LINE_TYPE": "Edge Line Type",
    "DING_RENDERING_ENGINE_ROOT": "Ding Rndering Engine Root Visual Property",
    "EDGE_STROKE_SELECTED_PAINT": "Edge Stroke Color (Selected)",
    "NODE_BORDER_PAINT": "Node Border Paint",
    "NODE_CUSTOMGRAPHICS_SIZE_6": "Node Image/Chart Size 6",
    "NODE_LABEL": "Node Label",
    "NODE_BORDER_WIDTH": "Node Border Width",
    "NODE_CUSTOMGRAPHICS_POSITION_6": "Node Image/Chart Position 6",
    "NODE_SELECTED": "Node Selected",
    "NODE_LABEL_FONT_FACE": "Node Label Font Face",
    "EDGE_UNSELECTED_PAINT": "Edge Color (Unselected)",
    "EDGE": "Edge Visual Property",
    "NODE_Y_LOCATION": "Node Y Location",
    "NODE_CUSTOMGRAPHICS_7": "Node Image/Chart 7",
    "NODE_CUSTOMGRAPHICS_SIZE_5": "Node Image/Chart Size 5",
    "NODE_CUSTOMGRAPHICS_POSITION_2": "Node Image/Chart Position 2",
    "NODE_CUSTOMPAINT_2": "Node Custom Paint 2",
    "NODE_LABEL_TRANSPARENCY": "Node Label Transparency",
    "NODE_CUSTOMGRAPHICS_SIZE_4": "Node Image/Chart Size 4",
    "NODE_CUSTOMGRAPHICS_POSITION_3": "Node Image/Chart Position 3",
    "EDGE_LABEL_FONT_FACE": "Edge Label Font Face",
    "NETWORK_CENTER_X_LOCATION": "Network Center X Location",
    "NODE_WIDTH": "Node Width",
    "NODE_CUSTOMGRAPHICS_POSITION_7": "Node Image/Chart Position 7",
    "NODE_CUSTOMGRAPHICS_SIZE_7": "Node Image/Chart Size 7",
    "NODE_CUSTOMPAINT_8": "Node Custom Paint 8",
    "NODE_CUSTOMPAINT_6": "Node Custom Paint 6",
    "NETWORK_CENTER_Y_LOCATION": "Network Center Y Location",
    "EDGE_TARGET_ARROW_SHAPE": "Edge Target Arrow Shape",
    "NODE_CUSTOMGRAPHICS_6": "Node Image/Chart 6",
    "NODE_LABEL_FONT_SIZE": "Node Label Font Size",
    "EDGE_BEND": "Edge Bend",
    "EDGE_SELECTED_PAINT": "Edge Color (Selected)",
    "EDGE_TRANSPARENCY": "Edge Transparency",
    "NODE_CUSTOMGRAPHICS_SIZE_2": "Node Image/Chart Size 2",
    "NODE_CUSTOMGRAPHICS_POSITION_8": "Node Image/Chart Position 8",
    "NODE_TRANSPARENCY": "Node Transparency",
    "NODE_CUSTOMPAINT_5": "Node Custom Paint 5",
    "NODE_SIZE": "Node Size",
    "NODE_CUSTOMGRAPHICS_POSITION_1": "Node Image/Chart Position 1",
    "NETWORK_HEIGHT": "Network Height"
}

Get node visual propeties



In [67]:
# Get node Visual Properties
node_vps = cy.style.vps.get_node_visual_props()

# Show it
print(json.dumps(node_vps, indent=4))


[
    "COMPOUND_NODE_PADDING",
    "COMPOUND_NODE_SHAPE",
    "NODE",
    "NODE_BORDER_PAINT",
    "NODE_BORDER_STROKE",
    "NODE_BORDER_TRANSPARENCY",
    "NODE_BORDER_WIDTH",
    "NODE_CUSTOMGRAPHICS_1",
    "NODE_CUSTOMGRAPHICS_2",
    "NODE_CUSTOMGRAPHICS_3",
    "NODE_CUSTOMGRAPHICS_4",
    "NODE_CUSTOMGRAPHICS_5",
    "NODE_CUSTOMGRAPHICS_6",
    "NODE_CUSTOMGRAPHICS_7",
    "NODE_CUSTOMGRAPHICS_8",
    "NODE_CUSTOMGRAPHICS_9",
    "NODE_CUSTOMGRAPHICS_POSITION_1",
    "NODE_CUSTOMGRAPHICS_POSITION_2",
    "NODE_CUSTOMGRAPHICS_POSITION_3",
    "NODE_CUSTOMGRAPHICS_POSITION_4",
    "NODE_CUSTOMGRAPHICS_POSITION_5",
    "NODE_CUSTOMGRAPHICS_POSITION_6",
    "NODE_CUSTOMGRAPHICS_POSITION_7",
    "NODE_CUSTOMGRAPHICS_POSITION_8",
    "NODE_CUSTOMGRAPHICS_POSITION_9",
    "NODE_CUSTOMGRAPHICS_SIZE_1",
    "NODE_CUSTOMGRAPHICS_SIZE_2",
    "NODE_CUSTOMGRAPHICS_SIZE_3",
    "NODE_CUSTOMGRAPHICS_SIZE_4",
    "NODE_CUSTOMGRAPHICS_SIZE_5",
    "NODE_CUSTOMGRAPHICS_SIZE_6",
    "NODE_CUSTOMGRAPHICS_SIZE_7",
    "NODE_CUSTOMGRAPHICS_SIZE_8",
    "NODE_CUSTOMGRAPHICS_SIZE_9",
    "NODE_CUSTOMPAINT_1",
    "NODE_CUSTOMPAINT_2",
    "NODE_CUSTOMPAINT_3",
    "NODE_CUSTOMPAINT_4",
    "NODE_CUSTOMPAINT_5",
    "NODE_CUSTOMPAINT_6",
    "NODE_CUSTOMPAINT_7",
    "NODE_CUSTOMPAINT_8",
    "NODE_CUSTOMPAINT_9",
    "NODE_DEPTH",
    "NODE_FILL_COLOR",
    "NODE_HEIGHT",
    "NODE_LABEL",
    "NODE_LABEL_COLOR",
    "NODE_LABEL_FONT_FACE",
    "NODE_LABEL_FONT_SIZE",
    "NODE_LABEL_POSITION",
    "NODE_LABEL_TRANSPARENCY",
    "NODE_LABEL_WIDTH",
    "NODE_NESTED_NETWORK_IMAGE_VISIBLE",
    "NODE_PAINT",
    "NODE_SELECTED",
    "NODE_SELECTED_PAINT",
    "NODE_SHAPE",
    "NODE_SIZE",
    "NODE_TOOLTIP",
    "NODE_TRANSPARENCY",
    "NODE_VISIBLE",
    "NODE_WIDTH",
    "NODE_X_LOCATION",
    "NODE_Y_LOCATION",
    "NODE_Z_LOCATION"
]

Get edge visual propeties



In [68]:
# Get edge Visual Properties for each data type
edge_vps = cy.style.vps.get_edge_visual_props()

# Show it
print(json.dumps(edge_vps, indent=4))


[
    "EDGE",
    "EDGE_BEND",
    "EDGE_CURVED",
    "EDGE_LABEL",
    "EDGE_LABEL_COLOR",
    "EDGE_LABEL_FONT_FACE",
    "EDGE_LABEL_FONT_SIZE",
    "EDGE_LABEL_TRANSPARENCY",
    "EDGE_LABEL_WIDTH",
    "EDGE_LINE_TYPE",
    "EDGE_PAINT",
    "EDGE_SELECTED",
    "EDGE_SELECTED_PAINT",
    "EDGE_SOURCE_ARROW_SELECTED_PAINT",
    "EDGE_SOURCE_ARROW_SHAPE",
    "EDGE_SOURCE_ARROW_UNSELECTED_PAINT",
    "EDGE_STROKE_SELECTED_PAINT",
    "EDGE_STROKE_UNSELECTED_PAINT",
    "EDGE_TARGET_ARROW_SELECTED_PAINT",
    "EDGE_TARGET_ARROW_SHAPE",
    "EDGE_TARGET_ARROW_UNSELECTED_PAINT",
    "EDGE_TOOLTIP",
    "EDGE_TRANSPARENCY",
    "EDGE_UNSELECTED_PAINT",
    "EDGE_VISIBLE",
    "EDGE_WIDTH"
]

Get network visual propeties



In [69]:
# Get network Visual Properties for each data type
network_vps = cy.style.vps.get_network_visual_props()

# Show it
print(json.dumps(network_vps, indent=4))


[
    "NETWORK",
    "NETWORK_BACKGROUND_PAINT",
    "NETWORK_CENTER_X_LOCATION",
    "NETWORK_CENTER_Y_LOCATION",
    "NETWORK_CENTER_Z_LOCATION",
    "NETWORK_DEPTH",
    "NETWORK_EDGE_SELECTION",
    "NETWORK_HEIGHT",
    "NETWORK_NODE_SELECTION",
    "NETWORK_SCALE_FACTOR",
    "NETWORK_SIZE",
    "NETWORK_TITLE",
    "NETWORK_WIDTH"
]

Create original Style and apply it


Now, we know the all of the visual propeties, so let's create your original style and apply it.

Create original Style


To create original style, the first step is to create style object. Then, add the value to each visual propeties that you want to change from default.


In [79]:
# Create Visual Style as code (or by hand if you prefer)
my_yeast_style = cy.style.create('GAL Style')

# You can set default values as key-value pairs.
basic_settings = {
    
    'NODE_FILL_COLOR': '#6AACB8',
    'NODE_SIZE': 100,
    'NODE_BORDER_WIDTH': 0,
    'NODE_LABEL_COLOR': '#555555',
    
    'EDGE_WIDTH': 2,
    'EDGE_TRANSPARENCY': 100,
    'EDGE_STROKE_UNSELECTED_PAINT': '#333333',
    
    'NETWORK_BACKGROUND_PAINT': '#FFFFEA'
}

# Set basic style
my_yeast_style.update_defaults(basic_settings)

Apply original style



In [80]:
# let's apply that style
cy.style.apply(style=my_yeast_style, network=yeast_net)

# Show it
yeast_net_png = yeast_net.get_png()
Image(yeast_net_png)


Out[80]:

Create passthrough mapping


The "passthrough" means that vizual propetiy connect with the table column. So, if you set the 'NODE_LABEL' in visual propety as 'lable' data of the table's column, you can bind these data and will appear it as graph in Cytoscape.


In [82]:
# Create passthrough mapping
my_yeast_style.create_passthrough_mapping(column='label', vp='NODE_LABEL', col_type='String')

# apply it
cy.style.apply(style=my_yeast_style, network=yeast_net)

# Show it
yeast_net_png = yeast_net.get_png()
Image(yeast_net_png)


Out[82]:

Create continuous mapping


The continuous mapping is to map the network value to visual style. For example, you can map the color gradient from the node value.


In [83]:
# Get the value for continuous mapping
degrees = yeast_net.get_node_column('degree.layout')

# Map the color gradient from that value
color_gradient = StyleUtil.create_2_color_gradient(min=degrees.min(), max=degrees.max(), colors=('white', '#6AACB8'))

# Map the size from value
degree_to_size = StyleUtil.create_slope(min=degrees.min(), max=degrees.max(), values=(10, 100))

# Crate continuous mapping
my_yeast_style.create_continuous_mapping(column='degree.layout', vp='NODE_FILL_COLOR', col_type='Integer', points=color_gradient)
my_yeast_style.create_continuous_mapping(column='degree.layout', vp='NODE_SIZE', col_type='Integer', points=degree_to_size)
my_yeast_style.create_continuous_mapping(column='degree.layout', vp='NODE_WIDTH', col_type='Integer', points=degree_to_size)
my_yeast_style.create_continuous_mapping(column='degree.layout', vp='NODE_HEIGHT', col_type='Integer', points=degree_to_size)
my_yeast_style.create_continuous_mapping(column='degree.layout', vp='NODE_LABEL_FONT_SIZE', col_type='Integer', points=degree_to_size)

# apply it
cy.style.apply(my_yeast_style, yeast_net)

# Show it
yeast_net_png = yeast_net.get_png()
Image(yeast_net_png)


Out[83]:

Create descrete mapping


The descrete mapping is to map the network value to visual style. For example, you can map the color from the edge value and change the edge color by the edge's value.


In [84]:
# Discrete mapping: Simply prepare key-value pairs and send it
kv_pair = {
    'pp': 'pink',
    'pd': 'green'
}
my_yeast_style.create_discrete_mapping(column='interaction', 
                               col_type='String', vp='EDGE_STROKE_UNSELECTED_PAINT', mappings=kv_pair)

# apply it
cy.style.apply(my_yeast_style, yeast_net)

# Show it
yeast_net_png = yeast_net.get_png()
Image(yeast_net_png)


Out[84]: