In [6]:
# Tested on:
!python --version
You don't have to construct graph libraries in your local environment.
It is very easy to use python-igraph and graph-tools.
In [16]:
import requests
import json
In [17]:
url_community = 'http://localhost:80' # igraph's community detection service URL
url_layout = 'http://localhost:3000' # graph-tool's layout service URL
headers = {'Content-type': 'application/json'}
This DEMO uses yeastHQSubnet.cx
as original network.
In order to detect communities, igraph's community detection service can be used.
open()
requests.post()
In [20]:
data = open('./yeastHQSubnet.cx') # 1.
parameter = {'type': 'leading_eigenvector', 'clusters': 5, 'palette': 'husl'} # 2.
r = requests.post(url=url_community, headers=headers, data=data, params=parameter) # 3.
graph with community membership + color assignment for each group.
To save and look the output data, you can use r.json()['data']
Note
json.dumps(r.json()['data'])
In [21]:
import re
with open('output1.cx', 'w') as f:
# single quotation -> double quotation
output = re.sub(string=str(r.json()['data']), pattern="'", repl='"')
f.write(output)
In order to perform layout algorithm, graph-tool's layout algorithm service can be used.
You can use the community structure as a parameter for layout, and result reflects its structure.
You can use graph-tool's service in the same way as igraph's service.
Both input and output of cxMate service are CX, NOT igraph's object, graph-tool's object and so on.
So, you don't have to convert igraph object to graph-tools object.
json.dumps(r.json()['data'])
requests.post()
In [22]:
data2 = json.dumps(r.json()['data']) # 1.
parameter = {'only-layout': False, 'groups': 'community'} # 2.
r2 = requests.post(url=url_layout, headers=headers, data=data2, params=parameter) # 3.
In [23]:
import re
with open('output2.cx', 'w') as f:
# single quotation -> double quotation
output = re.sub(string=str(r2.json()['data']), pattern="'", repl='"')
f.write(output)
If you want to change color of communities, you can do it easily.
Many color palettes of seaborn can be used. (See http://seaborn.pydata.org/tutorial/color_palettes.html)
In [11]:
%matplotlib inline
import seaborn as sns, numpy as np
from ipywidgets import interact, FloatSlider
In [2]:
def show_husl(n):
sns.palplot(sns.color_palette('husl', n))
print('palette: husl')
interact(show_husl, n=10);
In [3]:
def show_pal0(palette):
sns.palplot(sns.color_palette(palette, 24))
interact(show_pal0, palette='deep muted pastel bright dark colorblind'.split());
In [4]:
sns.choose_colorbrewer_palette('qualitative');
In [5]:
sns.choose_colorbrewer_palette('sequential');
In [ ]: