forked from idekerlab/cyREST
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
8,226 additions
and
0 deletions.
There are no files selected for viewing
367 changes: 367 additions & 0 deletions
367
examples/python/basic/.ipynb_checkpoints/CytoscapeREST_Basic1-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,367 @@ | ||
{ | ||
"metadata": { | ||
"name": "", | ||
"signature": "sha256:547a7fc519ef97dbedfd1d1a0f31dda26a0310e09a4939a9a5ac0fd2aae81cbc" | ||
}, | ||
"nbformat": 3, | ||
"nbformat_minor": 0, | ||
"worksheets": [ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Basic Workflow Part 1: Fundamentals of Cytoscape RESTful API\n", | ||
"\n", | ||
"## Introduction\n", | ||
"This is a basic workflow how to access Cytoscape via RESTful API.\n", | ||
"\n", | ||
"## System Requirments\n", | ||
"* Java 7+\n", | ||
"* Cytoscape 3.1.1 or later\n", | ||
"* Latest version of cy-rest app\n", | ||
"\n", | ||
"## Questions?\n", | ||
"Send them to our [mailing list]()." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 1. Import Python Libraries and Basic Setup\n", | ||
"\n", | ||
"### Dependent Libraries\n", | ||
"In this tutorial, we use several popular Python libraries to make this workflow more realistic. Since you need to access Cytoscape via RESTful API, HTTP client library is very important tool. In this example, we use [Requests](http://docs.python-requests.org/en/latest/) library.\n", | ||
"\n", | ||
"Other libraries:\n", | ||
"\n", | ||
"* json - for JSON processing\n", | ||
"* NetworkX - network generation and analysis\n", | ||
"* pandas - very powerful data analysis framework for Python\n", | ||
"\n", | ||
"### Basic Setup for the API\n", | ||
"At this point, there is only one option for the cy-rest module: port number. By default, port number used by cy-rest module is __1234__. To change this, you need set a global Cytoscape property from _Edit-->Preserences-->Properties..._ and add a new property __port.number__." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"# Import libraries\n", | ||
"import requests\n", | ||
"import json\n", | ||
"import networkx as nx\n", | ||
"import pandas as pd\n", | ||
"\n", | ||
"# Basic Setup\n", | ||
"PORT_NUMBER = 8080\n", | ||
"BASE = 'http://localhost:' + str(PORT_NUMBER) + '/v1/'\n", | ||
"\n", | ||
"# Header for posting data to the server\n", | ||
"HEADERS = {'Content-Type': 'application/json'}" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [], | ||
"prompt_number": 31 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 2. Test Cytoscape REST API\n", | ||
"First, run a simple REST call and make sure cy-rest module is running." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"# Get version number of Cytoscape and REST API version\n", | ||
"response1 = requests.get(BASE + 'version')\n", | ||
"print(response1.content)" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"{ \"apiVersion\":\"1.0.0\",\"cytoscapeVersion\": \"3.1.1\"}\n" | ||
] | ||
} | ||
], | ||
"prompt_number": 32 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"If the method call above works fine, you are ready to go!\n", | ||
"\n", | ||
"## 3. Import Networks from various data sources\n", | ||
"There are several ways to load networks into Cytoscape:\n", | ||
"\n", | ||
"* From files\n", | ||
"* From web services\n", | ||
"* From Cytoscape style json\n", | ||
"* From edge list" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"# Delete all existing networks\n", | ||
"requests.delete(BASE + 'networks')\n", | ||
"\n", | ||
"# Array of data source. \n", | ||
"network_files = [\n", | ||
" # SIF file on a web server\n", | ||
" 'http://chianti.ucsd.edu/cytoscape-data/galFiltered.sif',\n", | ||
" # PSICQUIC web service query\n", | ||
" 'http://www.ebi.ac.uk/Tools/webservices/psicquic/intact/webservices/current/search/interactor/brca1_human?format=xml25'\n", | ||
"]\n", | ||
"\n", | ||
"response2 = requests.post(BASE + 'networks?source=url', data=json.dumps(network_files), headers=HEADERS)\n", | ||
"new_networks = json.loads(response2.content)\n", | ||
"print(json.dumps(new_networks, indent=2))" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"[\n", | ||
" {\n", | ||
" \"source\": \"http://www.ebi.ac.uk/Tools/webservices/psicquic/intact/webservices/current/search/interactor/brca1_human?format=xml25\", \n", | ||
" \"networkSUID\": [\n", | ||
" 74052\n", | ||
" ]\n", | ||
" }, \n", | ||
" {\n", | ||
" \"source\": \"http://chianti.ucsd.edu/cytoscape-data/galFiltered.sif\", \n", | ||
" \"networkSUID\": [\n", | ||
" 72638\n", | ||
" ]\n", | ||
" }\n", | ||
"]\n" | ||
] | ||
} | ||
], | ||
"prompt_number": 58 | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"# From Cytoscape.js JSON\n", | ||
"\n", | ||
"# This is the basic structure of network JSON object:\n", | ||
"small_network = {\n", | ||
" 'data': {\n", | ||
" 'name': 'manually generated empty network'\n", | ||
" },\n", | ||
" 'elements': {\n", | ||
" 'nodes':[],\n", | ||
" 'edges':[]\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"response3 = requests.post(BASE + 'networks?collection=My%20Collection', data=json.dumps(small_network), headers=HEADERS)\n", | ||
"\n", | ||
"# From edgelist\n", | ||
"edge_list = '1 2\\n' + '2 3\\n' + '1 3'\n", | ||
"response3 = requests.post(BASE + 'networks?format=edgelist', data=edge_list, headers=HEADERS)" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [], | ||
"prompt_number": 57 | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"# Get the node table as JSON\n", | ||
"\n", | ||
"# Or CSV\n", | ||
"\n", | ||
"# Convert it as DataFrame" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Import & Generate Data in Python\n", | ||
"\n", | ||
"### Generate scale-free graphs with NetworkX" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"graphs = []\n", | ||
"\n", | ||
"# Create 20 small randome networks\n", | ||
"for i in range(20):\n", | ||
" # Generate scale-free graph\n", | ||
" g = nx.scale_free_graph(50);\n", | ||
" \n", | ||
" # Perform simple graph analysis\n", | ||
" \n", | ||
" # Node statistics\n", | ||
" bc = nx.betweenness_centrality(g)\n", | ||
" degree = nx.degree(g)\n", | ||
" cc = nx.closeness_centrality(g)\n", | ||
" nx.set_node_attributes(g, 'betweenness', bc)\n", | ||
" nx.set_node_attributes(g, 'closeness', cc)\n", | ||
" nx.set_node_attributes(g, 'degree', degree)\n", | ||
" \n", | ||
" # Network statistics\n", | ||
" g.graph[\"avg_shortest_path_len\"] = nx.average_shortest_path_length(g)\n", | ||
" g.graph[\"density\"] = nx.density(g)\n", | ||
" graphs.append(g)" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [], | ||
"prompt_number": 50 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Send all network models to Cytoscape" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"# Remove all networks\n", | ||
"requests.delete(BASE + 'networks')\n", | ||
"\n", | ||
"import cytoscape.viewer as cy\n", | ||
"\n", | ||
"for graph in graphs:\n", | ||
" cyjs_network = cy.from_networkx(graph)\n", | ||
" res1 = requests.post(BASE + 'networks', data=json.dumps(cyjs_network), headers=HEADERS)" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [], | ||
"prompt_number": 52 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## (Now graphs are in Cytoscape. Do analysis, visualization, etc...)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Get the visualization back to this notebook." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"response = requests.get(\"http://localhost:8080/v1/networks?format=SUID\")\n", | ||
"network_list = json.loads(response.content)\n", | ||
"print(network_list)\n", | ||
"\n", | ||
"network_views = []\n", | ||
"for suid in network_list:\n", | ||
" response2 = requests.get(\"http://localhost:8080/v1/networks/\" + str(suid) + \"/views/first\")\n", | ||
" network_views.append(json.loads(response2.content))" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"[67314, 68616, 68922, 67654, 68334, 72226, 66680, 69952, 71912, 69292, 67984, 71240, 65946, 70886, 66996, 69640, 66328, 70578, 71576, 70274]\n" | ||
] | ||
} | ||
], | ||
"prompt_number": 55 | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"# Visual Style can be a simple Python object!\n", | ||
"\n", | ||
"my_style = {\n", | ||
" \"title\" : \"My Style 10\",\n", | ||
" \"defaults\" : [ {\n", | ||
" \"visualProperty\" : \"EDGE_WIDTH\",\n", | ||
" \"value\" : 11.0\n", | ||
" }, {\n", | ||
" \"visualProperty\" : \"EDGE_STROKE_UNSELECTED_PAINT\",\n", | ||
" \"value\" : \"#00ddff\"\n", | ||
" }, {\n", | ||
" \"visualProperty\" : \"NODE_WIDTH\",\n", | ||
" \"value\" : 20\n", | ||
" }, {\n", | ||
" \"visualProperty\" : \"NODE_HEIGHT\",\n", | ||
" \"value\" : 20\n", | ||
" }],\n", | ||
" \"mappings\" : [ {\n", | ||
" \"mappingType\" : \"discrete\",\n", | ||
" \"mappingColumn\" : \"degree\",\n", | ||
" \"mappingColumnType\" : \"Integer\",\n", | ||
" \"visualProperty\" : \"NODE_FILL_COLOR\",\n", | ||
" \"map\" : [ {\n", | ||
" \"key\" : \"1\",\n", | ||
" \"value\" : \"#440055\"\n", | ||
" }, {\n", | ||
" \"key\" : \"4\",\n", | ||
" \"value\" : \"#00FF11\"\n", | ||
" } ]\n", | ||
" }, {\n", | ||
" \"mappingType\" : \"passthrough\",\n", | ||
" \"mappingColumn\" : \"name\",\n", | ||
" \"mappingColumnType\" : \"String\",\n", | ||
" \"visualProperty\" : \"NODE_LABEL\"\n", | ||
" } ]\n", | ||
"}\n", | ||
"\n", | ||
"requests.post(\"http://localhost:8080/v1/styles\", data=json.dumps(my_style), headers=HEADERS)" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"metadata": {}, | ||
"output_type": "pyout", | ||
"prompt_number": 53, | ||
"text": [ | ||
"<Response [200]>" | ||
] | ||
} | ||
], | ||
"prompt_number": 53 | ||
} | ||
], | ||
"metadata": {} | ||
} | ||
] | ||
} |
Oops, something went wrong.