In [1]:
from __future__ import print_function

from collections import defaultdict
import json
import requests

from IPython.display import Image
import networkx as nx

from py2cytoscape.util import from_networkx

In [2]:
def get_psiquic_uniprot(query, **kwargs):
    kwargs['format'] = kwargs.get('format', 'tab27')
    server = 'http://www.ebi.ac.uk/Tools/webservices/psicquic/uniprot/webservices/current/search/query'
    req = requests.get('%s/%s' % (server, query), params=kwargs)
    return req.content

In [3]:
genes_species = defaultdict(set)
interactions = {}

def get_gene_name(my_id, alt_names):
    toks = alt_names.split('|')
    for tok in toks:
        if tok.endswith('(gene name)'):
            return tok[tok.find(':') + 1: tok.find('(')]
    return my_id + '?'  # no name...

def get_vernacular_tax(tax):
    return tax.split('|')[0][tax.find('(') + 1:-1]

def add_interactions(species):
    for rec in species.split('\n'):
        toks = rec.rstrip().split('\t')
        if len(toks) < 15:
            continue  # empty line at the end
        id1 = toks[0][toks[0].find(':') + 1:]
        id2 = toks[1][toks[1].find(':') + 1:]
        gene1, gene2 = get_gene_name(id1, toks[4]), get_gene_name(id2, toks[5])
        #print(toks[6])

        tax1, tax2 = get_vernacular_tax(toks[9]), get_vernacular_tax(toks[10])
        inter_type = toks[11][toks[11].find('(') + 1:-1]
        miscore = float(toks[14].split(':')[1])
        genes_species[tax1].add(gene1)
        genes_species[tax2].add(gene2)
        interactions[((tax1, gene1), (tax2, gene2))] = {'score': miscore, 'type': inter_type}

In [4]:
human = get_psiquic_uniprot('uniprotkb:P04637')
add_interactions(human)
rat = get_psiquic_uniprot('uniprotkb:P10361')
add_interactions(rat)
mouse = get_psiquic_uniprot('uniprotkb:P02340')
add_interactions(mouse)

In [5]:
def get_node_id(species, gene):
    if species == 'human':
        return gene
    elif species in ['mouse', 'rat']:
        return '%s (%s)' % (gene, species[0])
    else:
        return  '%s (%s)' % (gene, species)

graph = nx.Graph()
for species, genes in genes_species.items():
    #print(species)
    for gene in genes:
        name = get_node_id(species, gene)
        #print(gene, name)
        graph.add_node(get_node_id(species, gene),
                       species=species, gene=gene)

for (i1, i2), attribs in interactions.items():
    tax1, gene1 = i1
    tax2, gene2 = i2
    graph.add_edge(get_node_id(tax1, gene1),
                   get_node_id(tax2, gene2),
                   interaction=attribs['type'],
                   score=attribs['score'])

In [6]:
server = 'http://localhost:1234/v1'

p53_interactions = from_networkx(graph)
p53_net = requests.post(server + '/networks', data=json.dumps(p53_interactions),
                        headers={'Content-Type': 'application/json'})
net_id = p53_net.json()['networkSUID']
requests.get('%s/apply/layouts/circular/%d' % (server, net_id))
Image('%s/networks/%d/views/first.png' % (server, net_id))


Out[6]:

In [7]:
res = requests.get(server + '/networks',
                     headers={'Content-Type': 'application/json'})
print(res.content)


[52]

In [8]:
res = requests.get(server + '/apply/styles',
                     headers={'Content-Type': 'application/json'})
print(res.json())


[u'Sample1', u'Minimal', u'Ripple', u'BioPAX_SIF', u'default black', u'Big Labels', u'Solid', u'Universe', u'BioPAX', u'default', u'Directed', u'Nested Network Style']

In [9]:
res = requests.get(server + '/styles',
                     headers={'Content-Type': 'application/json'})
res.content
res = requests.get(server + '/styles/default',
                     headers={'Content-Type': 'application/json'})
print(json.dumps(json.loads(res.content), indent=4))


{
    "mappings": [
        {
            "mappingType": "passthrough", 
            "visualProperty": "NODE_LABEL", 
            "mappingColumnType": "String", 
            "mappingColumn": "name"
        }
    ], 
    "defaults": [
        {
            "visualProperty": "EDGE_BEND", 
            "value": ""
        }, 
        {
            "visualProperty": "EDGE_CURVED", 
            "value": true
        }, 
        {
            "visualProperty": "EDGE_LABEL", 
            "value": ""
        }, 
        {
            "visualProperty": "EDGE_LABEL_COLOR", 
            "value": "#000000"
        }, 
        {
            "visualProperty": "EDGE_LABEL_FONT_FACE", 
            "value": "Dialog.plain,plain,10"
        }, 
        {
            "visualProperty": "EDGE_LABEL_FONT_SIZE", 
            "value": 10
        }, 
        {
            "visualProperty": "EDGE_LABEL_TRANSPARENCY", 
            "value": 255
        }, 
        {
            "visualProperty": "EDGE_LINE_TYPE", 
            "value": "SOLID"
        }, 
        {
            "visualProperty": "EDGE_PAINT", 
            "value": "#323232"
        }, 
        {
            "visualProperty": "EDGE_SELECTED", 
            "value": false
        }, 
        {
            "visualProperty": "EDGE_SELECTED_PAINT", 
            "value": "#FF0000"
        }, 
        {
            "visualProperty": "EDGE_SOURCE_ARROW_SELECTED_PAINT", 
            "value": "#FFFF00"
        }, 
        {
            "visualProperty": "EDGE_SOURCE_ARROW_SHAPE", 
            "value": "NONE"
        }, 
        {
            "visualProperty": "EDGE_SOURCE_ARROW_UNSELECTED_PAINT", 
            "value": "#000000"
        }, 
        {
            "visualProperty": "EDGE_STROKE_SELECTED_PAINT", 
            "value": "#FF0000"
        }, 
        {
            "visualProperty": "EDGE_STROKE_UNSELECTED_PAINT", 
            "value": "#666666"
        }, 
        {
            "visualProperty": "EDGE_TARGET_ARROW_SELECTED_PAINT", 
            "value": "#FFFF00"
        }, 
        {
            "visualProperty": "EDGE_TARGET_ARROW_SHAPE", 
            "value": "NONE"
        }, 
        {
            "visualProperty": "EDGE_TARGET_ARROW_UNSELECTED_PAINT", 
            "value": "#000000"
        }, 
        {
            "visualProperty": "EDGE_TOOLTIP", 
            "value": ""
        }, 
        {
            "visualProperty": "EDGE_TRANSPARENCY", 
            "value": 255
        }, 
        {
            "visualProperty": "EDGE_UNSELECTED_PAINT", 
            "value": "#404040"
        }, 
        {
            "visualProperty": "EDGE_VISIBLE", 
            "value": true
        }, 
        {
            "visualProperty": "EDGE_WIDTH", 
            "value": 3.0
        }, 
        {
            "visualProperty": "NETWORK_BACKGROUND_PAINT", 
            "value": "#FFFFFF"
        }, 
        {
            "visualProperty": "NETWORK_CENTER_X_LOCATION", 
            "value": 0.0
        }, 
        {
            "visualProperty": "NETWORK_CENTER_Y_LOCATION", 
            "value": 0.0
        }, 
        {
            "visualProperty": "NETWORK_CENTER_Z_LOCATION", 
            "value": 0.0
        }, 
        {
            "visualProperty": "NETWORK_DEPTH", 
            "value": 0.0
        }, 
        {
            "visualProperty": "NETWORK_EDGE_SELECTION", 
            "value": true
        }, 
        {
            "visualProperty": "NETWORK_HEIGHT", 
            "value": 400.0
        }, 
        {
            "visualProperty": "NETWORK_NODE_SELECTION", 
            "value": true
        }, 
        {
            "visualProperty": "NETWORK_SCALE_FACTOR", 
            "value": 1.0
        }, 
        {
            "visualProperty": "NETWORK_SIZE", 
            "value": 550.0
        }, 
        {
            "visualProperty": "NETWORK_TITLE", 
            "value": ""
        }, 
        {
            "visualProperty": "NETWORK_WIDTH", 
            "value": 550.0
        }, 
        {
            "visualProperty": "NODE_BORDER_PAINT", 
            "value": "#006699"
        }, 
        {
            "visualProperty": "NODE_BORDER_STROKE", 
            "value": "SOLID"
        }, 
        {
            "visualProperty": "NODE_BORDER_TRANSPARENCY", 
            "value": 255
        }, 
        {
            "visualProperty": "NODE_BORDER_WIDTH", 
            "value": 4.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_1", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_2", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_3", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_4", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_5", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_6", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_7", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_8", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_9", 
            "value": "org.cytoscape.ding.customgraphics.NullCustomGraphics,0,[ Remove Graphics ],"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_1", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_2", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_3", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_4", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_5", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_6", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_7", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_8", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_POSITION_9", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_1", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_2", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_3", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_4", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_5", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_6", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_7", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_8", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMGRAPHICS_SIZE_9", 
            "value": 50.0
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_1", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_1, name=Node Custom Paint 1)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_2", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_2, name=Node Custom Paint 2)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_3", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_3, name=Node Custom Paint 3)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_4", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_4, name=Node Custom Paint 4)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_5", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_5, name=Node Custom Paint 5)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_6", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_6, name=Node Custom Paint 6)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_7", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_7, name=Node Custom Paint 7)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_8", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_8, name=Node Custom Paint 8)"
        }, 
        {
            "visualProperty": "NODE_CUSTOMPAINT_9", 
            "value": "DefaultVisualizableVisualProperty(id=NODE_CUSTOMPAINT_9, name=Node Custom Paint 9)"
        }, 
        {
            "visualProperty": "NODE_DEPTH", 
            "value": 0.0
        }, 
        {
            "visualProperty": "NODE_FILL_COLOR", 
            "value": "#0099CC"
        }, 
        {
            "visualProperty": "NODE_HEIGHT", 
            "value": 30.0
        }, 
        {
            "visualProperty": "NODE_LABEL", 
            "value": ""
        }, 
        {
            "visualProperty": "NODE_LABEL_COLOR", 
            "value": "#FFFFFF"
        }, 
        {
            "visualProperty": "NODE_LABEL_FONT_FACE", 
            "value": "Dialog.plain,plain,12"
        }, 
        {
            "visualProperty": "NODE_LABEL_FONT_SIZE", 
            "value": 12
        }, 
        {
            "visualProperty": "NODE_LABEL_POSITION", 
            "value": "C,C,c,0.00,0.00"
        }, 
        {
            "visualProperty": "NODE_LABEL_TRANSPARENCY", 
            "value": 255
        }, 
        {
            "visualProperty": "NODE_LABEL_WIDTH", 
            "value": 200.0
        }, 
        {
            "visualProperty": "NODE_NESTED_NETWORK_IMAGE_VISIBLE", 
            "value": true
        }, 
        {
            "visualProperty": "NODE_PAINT", 
            "value": "#1E90FF"
        }, 
        {
            "visualProperty": "NODE_SELECTED", 
            "value": false
        }, 
        {
            "visualProperty": "NODE_SELECTED_PAINT", 
            "value": "#FFFF00"
        }, 
        {
            "visualProperty": "NODE_SHAPE", 
            "value": "ROUND_RECTANGLE"
        }, 
        {
            "visualProperty": "NODE_SIZE", 
            "value": 35.0
        }, 
        {
            "visualProperty": "NODE_TOOLTIP", 
            "value": ""
        }, 
        {
            "visualProperty": "NODE_TRANSPARENCY", 
            "value": 255
        }, 
        {
            "visualProperty": "NODE_VISIBLE", 
            "value": true
        }, 
        {
            "visualProperty": "NODE_WIDTH", 
            "value": 70.0
        }, 
        {
            "visualProperty": "NODE_X_LOCATION", 
            "value": 0.0
        }, 
        {
            "visualProperty": "NODE_Y_LOCATION", 
            "value": 0.0
        }, 
        {
            "visualProperty": "NODE_Z_LOCATION", 
            "value": 0.0
        }
    ], 
    "title": "default"
}

In [10]:
ustyle = {
    'title': 'UniProt style',
    'mappings': [
        {'mappingType': 'discrete',
         'map': [
                {'key': 'human', 'value': '#00FF00'},
                {'key': 'rat', 'value': '#FF00FF'},
                {'key': 'mouse', 'value': '#00FFFF'}],
         'visualProperty': 'NODE_FILL_COLOR',
         'mappingColumnType': 'String',
         'mappingColumn': 'species'},
        {
            'mappingType': 'passthrough', 
            'visualProperty': 'NODE_LABEL', 
            'mappingColumnType': 'String', 
            'mappingColumn': 'gene'}, 
        {
            'mappingType': 'passthrough', 
            'visualProperty': 'EDGE_TOOLTIP', 
            'mappingColumnType': 'String', 
            'mappingColumn': 'interaction'
        }],
    'defaults': [ {"visualProperty": "NODE_FILL_COLOR", 
            "value": "#FFFFFF"}]}

In [11]:
res = requests.post(server + "/styles", data=json.dumps(ustyle),
                   headers={'Content-Type': 'application/json'})

requests.get('%s/apply/layouts/force-directed/%d' % (server, net_id))
res = requests.get('%s/apply/styles/UniProt style/%d' % (server, net_id),
                     headers={'Content-Type': 'application/json'})
Image('%s/networks/%s/views/first.png' % (server, net_id))


Out[11]:

In [ ]: