In [1]:
import requests
import json
from IPython.display import display
from IPython.display import Image
# Basic Setup
PORT_NUMBER = 1234
BASE = 'http://localhost:' + str(PORT_NUMBER) + '/v1/'
HEADERS = {'Content-Type': 'application/json'}
# Utility function to print result (JSON Printer)
def jp(data):
print(json.dumps(data, indent=4))
In [2]:
res = requests.delete(BASE + 'session')
jp(res.json())
In [3]:
# URL Parameters
url_params = {
'source': 'url',
'collection': 'Your Collection Name'
}
# Array of data source. URL of the file (remote or local)
network_files = [
'http://chianti.ucsd.edu/cytoscape-data/galFiltered.sif'
# And of course, you can add as many files as you need...
]
# Load network from URLs
res = requests.post(BASE + 'networks', params=url_params, data=json.dumps(network_files), headers=HEADERS)
jp(res.json())
suid = res.json()[0]['networkSUID'][0]
# Make a utility to get first SUID
def get_suid(response):
return res.json()[0]['networkSUID'][0]
In [4]:
# Let's make a utility function
def show(network_id):
url = BASE+'networks/' + str(network_id) + '/views/first.png'
print('Your image is available here: ' + url)
display(Image(url=url, embed=True))
# Call it!
show(suid)
In [5]:
res = requests.get(BASE + 'apply/layouts')
jp(res.json())
In [6]:
res = requests.get(BASE + 'apply/layouts/force-directed/' + str(suid))
show(suid)
In [7]:
res = requests.get(BASE + 'styles')
jp(res.json())
In [8]:
res = requests.get(BASE + 'apply/styles/Directed/' + str(suid))
show(suid)
In [9]:
res = requests.get(BASE + 'styles/Directed')
#jp(res.json())
In [10]:
res = requests.get(BASE + 'styles/Directed/defaults')
#jp(res.json())
In [11]:
res = requests.get(BASE + 'styles/Directed/mappings')
jp(res.json())
In [12]:
# Simply define a key-value pairs of visual properties
new_defaults = [
{
'visualProperty': 'EDGE_WIDTH',
'value': 12
},
{
'visualProperty': 'EDGE_STROKE_UNSELECTED_PAINT',
'value': '#00abff'
}
]
res = requests.put(BASE + 'styles/Directed/defaults', data=json.dumps(new_defaults), headers=HEADERS)
show(suid)
In [13]:
res = requests.get(BASE + 'networks/' + str(suid) + '/views')
jp(res.json())
view_id = res.json()[0]
In [14]:
view_url = BASE + 'networks/' + str(suid) + '/views/' + str(view_id)
res = requests.get(view_url)
cyjs_network = res.json()
# jp(cyjs_network)
In [15]:
# Pick a node from the network
node_suid = cyjs_network['elements']['nodes'][0]['data']['SUID']
new_values = [
{
'visualProperty': 'NODE_WIDTH',
'value': 300
},
{
'visualProperty': 'NODE_HEIGHT',
'value': 300
},
{
'visualProperty': 'NODE_FILL_COLOR',
'value': 'orange'
},
{
'visualProperty': 'NODE_SHAPE',
'value': 'diamond'
}
]
res = requests.put(view_url + '/nodes/' + str(node_suid), data=json.dumps(new_values), headers=HEADERS)
show(suid)
In [16]:
# Update network view values
scale_url = view_url + '/network/NETWORK_SCALE_FACTOR'
res = requests.get(scale_url)
print(scale_url)
jp(res.json())
new_values = [
{
'visualProperty': 'NETWORK_BACKGROUND_PAINT',
'value': '#aaaaaa'
},
{
'visualProperty': 'NETWORK_SCALE_FACTOR',
'value': 2.7
}
]
res = requests.put(view_url + '/network', data=json.dumps(new_values), headers=HEADERS)
In [17]:
# Essentially, this is just a new value in selection
new_values = [
{
'SUID': node_suid,
'value': True
}
]
res = requests.put(BASE + 'networks/' + str(suid) + '/tables/defaultnode/columns/selected', data=json.dumps(new_values), headers=HEADERS)
show(suid)
In [18]:
# Deselect
new_values = [
{
'SUID': node_suid,
'value': False
}
]
res = requests.put(BASE + 'networks/' + str(suid) + '/tables/defaultnode/columns/selected', data=json.dumps(new_values), headers=HEADERS)
show(suid)
In [19]:
res = requests.put(BASE + 'networks/' + str(suid) + '/tables/defaultnode/columns/selected?default=true', data={}, headers=HEADERS)
show(suid)
In [20]:
# TODO
In [21]:
matched_url = BASE + 'networks/' +str(suid) + '/nodes?column=selected&query=true'
res = requests.get(matched_url)
print(matched_url)
# This is an array
result = res.json()
print('Number of selected nodes = ' + str(len(result)))
In [22]:
# Prepare new table data. This format is a bit redundant, but is a standard JSON way to store values.
new_table_data = {
'key': 'name', # Key in the existing table. In this case, "name" column in default node table.
'dataKey': 'uniprot_id', # Mapping key ih the new data. If this value maches to the "name" value, data will be assigned to the row.
'data': [
{
'uniprot_id': 'YBR190W',
'sgd':'S000000394',
'description': 'Dubious open reading frame',
'molecular_weight': 11036.8
}, {
'uniprot_id': 'YOL059W',
'sgd':'S000005420',
'description': 'NAD-dependent glycerol 3-phosphate dehydrogenase',
'molecular_weight': 49418.2
}
]
}
# PUT the new data to default node table
res = requests.put(BASE + 'networks/' + str(suid) + '/tables/defaultnode', data=json.dumps(new_table_data), headers=HEADERS)
In [23]:
res = requests.get(BASE + 'apply/layouts/force-directed')
jp(res.json())
params= [
{
'name': 'defaultNodeMass',
'value': 10
},
{
'name': 'defaultSpringLength',
'value': 100
},
{
'name': 'isDeterministic',
'value': True
}
]
res = requests.put(BASE + 'apply/layouts/force-directed', data=json.dumps(params), headers=HEADERS)
In [24]:
res = requests.get(BASE + 'ui/panels')
jp(res.json())
panel_state = [
{
"state": "DOCK",
"name": "SOUTH"
},
{
"state": "FLOAT",
"name": "SOUTH_WEST"
}
]
res = requests.put(BASE + 'ui/panels', data=json.dumps(panel_state), headers=HEADERS)