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.
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]:
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]:
In [63]:
# Get the all Style.
cy.style.get_all()
Out[63]:
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]:
In [66]:
# Get all available Visual Properties
vps = cy.style.vps.get_all()
# Show it
print(json.dumps(vps, indent=4))
In [67]:
# Get node Visual Properties
node_vps = cy.style.vps.get_node_visual_props()
# Show it
print(json.dumps(node_vps, indent=4))
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))
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))
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)
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]:
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]:
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]:
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]: