In [1]:
!python --version


Python 3.5.2 :: Anaconda 4.2.0 (x86_64)

In [2]:
import pandas as pd
import json

In [23]:
net = pd.read_csv('./sample-simulation/testNetwork.txt', delimiter='\t')
net.head(20)


Out[23]:
Source Target
0 YJL166W GO:0006122
1 YEL024W GO:0006122
2 GO:0005739 GO:0005750
3 GO:0009167 GO:0009123
4 GO:0044281 GO:0044710
5 GO:0009161 GO:0009167
6 GO:1901360 GO:0071704
7 GO:0044710 GO:0044699
8 GO:0005750 GO:1902495
9 GO:0022900 GO:0044763
10 GO:0022904 GO:0022900
11 GO:1902495 GO:0044425
12 GO:0044425 GO:0005575
13 GO:0008150 GO:00SUPER
14 GO:0008152 GO:0044710
15 GO:0006753 GO:0055086
16 GO:0009123 GO:0006753
17 GO:0044699 GO:0008150
18 GO:0044763 GO:0044699
19 GO:0055086 GO:0044763

In [24]:
prop = pd.read_csv('./sample-simulation/testNetwork_style.txt', delimiter='\t')
prop.head()


Out[24]:
Source Property
0 YJL166W 1.000000
1 GO:0006122 0.444456
2 YEL024W 1.000000
3 GO:0005739 0.191416
4 GO:0005750 0.360780

In [13]:
prop[prop['Source'] =='YEL024W']


Out[13]:
Source Property
2 YEL024W 1.0

In [3]:
subnet = {
    'data': {
        'name': 'simulation1'
    },
    'elements': {
        'nodes': [],
        'edges': []
    }
}

def get_node(id, score):
    node = {
        'data': {
            'id': id,
            'score': score
        }
    }  
    return node

def get_edge(source, target):
    edge = {
        'data': {
            'source': source,
            'target': target
        }
    }
    
    return edge

In [26]:
for data in net.iterrows():
    row=data[1]
    edge = get_edge(row['Source'], row['Target'])
    subnet['elements']['edges'].append(edge)

for data in prop.iterrows():
    row=data[1]
    node = get_node(row['Source'], row['Property'])
    subnet['elements']['nodes'].append(node)

with open('./data/subnet.cyjs', 'w') as outfile:
    json.dump(subnet, outfile)

In [1]:
# Load from NetworkX JSON

import json
from pprint import pprint

with open('./data/YAL056W_YCL038C.json') as data_file:    
    result1 = json.load(data_file)
    
with open('./data/YBL098W_YPL113C.json') as data_file:    
    result2 = json.load(data_file)

pprint(result1)


{'directed': False,
 'edges': [{'source': 'GO:0016020', 'target': 'GO:0005575'},
           {'source': 'GO:0044464', 'target': 'GO:0005575'},
           {'source': 'GO:0044464', 'target': 'GO:0044424'},
           {'source': 'GO:0005575', 'target': 'GO:00SUPER'},
           {'source': 'GO:0044444', 'target': 'GO:0044424'},
           {'source': 'GO:0044444', 'target': 'YAL056W'},
           {'source': 'GO:0044444', 'target': 'YCL038C'},
           {'source': 'GO:0008150', 'target': 'GO:0044699'},
           {'source': 'GO:0008150', 'target': 'GO:00SUPER'}],
 'graph': {},
 'multigraph': False,
 'nodes': [{'id': 'GO:0016020',
            'importance': 0.0,
            'phenotype': 0.024179171770811},
           {'id': 'GO:0044464', 'importance': 0.0, 'phenotype': 'NA'},
           {'id': 'GO:0005575', 'importance': 0.0, 'phenotype': 'NA'},
           {'id': 'GO:0044444',
            'importance': 0.0,
            'phenotype': 0.01525542512536},
           {'id': 'YCL038C', 'importance': 0.0, 'phenotype': 0.0},
           {'id': 'GO:0008150', 'importance': 0.0, 'phenotype': 'NA'},
           {'id': 'GO:0044424', 'importance': 0.0, 'phenotype': 'NA'},
           {'id': 'YAL056W', 'importance': 0.0, 'phenotype': 0.0},
           {'id': 'GO:0044699',
            'importance': 0.0,
            'phenotype': 0.01524106413126},
           {'id': 'GO:00SUPER',
            'importance': 0.0,
            'phenotype': 0.024996409192681}]}

In [10]:
nodes1 = result1['nodes']
edges1 = result1['edges']
new_nodes1 = []
new_edges1 = []

for node in nodes1:
    new_nodes1.append(get_node(node['id'], node['phenotype']))
    
for edge in edges1:
    new_edges1.append(get_edge(edge['source'], edge['target']))
    
net1 = {
    'data': {'name': 'result1'},
    'elements': {
        'nodes': new_nodes1,
        'edges': new_edges1
    }
}

with open('./data/result1.cyjs', 'w') as outfile:
    json.dump(net1, outfile)