{ "metadata": { "name": "", "signature": "sha256:d6a3efd7903943b5456e9a2eaee13132dae0cec1b7b0edb74c51396fa893f386" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Workflow 2: NetworkX and Cytoscape\n", "\n", "\n", "by [Keiichiro Ono](http://keiono.github.io/)\n", "\n", "\n", "----\n", "\n", "![](http://cytoscape.org/images/logo/cy3logoOrange.svg)\n", "\n", "\n", "![](http://ipython.org/_static/IPy_header.png)\n", "\n", "\n", "## Introduction\n", "Welcome to the part 2 of basic tutorial. In this example, you will learn how to use Cytoscape with NetworkX, a very poweful network analysis toolkit.\n", "\n", "### Prerequisites\n", "* Basic knowledge of RESTful API\n", " * [This is a good introduction to REST](http://www.restapitutorial.com/)\n", "* Basic Python skill\n", "* Basic knowledge of Cytoscape \n", "\n", "### System Requirments\n", "* [Java 7+](http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html)\n", "* [Cytoscape 3.1.1 or later](http://cytoscape.org/download.html)\n", "* Latest version of [cy-rest app](https://github.com/keiono/cy-rest/releases/latest)\n", "\n", "----\n", "## Questions or Feature Requests?\n", "Please send them to our [mailing list](https://groups.google.com/forum/#!forum/cytoscape-discuss)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import requests\n", "import json\n", "import networkx as nx\n", "\n", "# Basic Setup\n", "PORT_NUMBER = 1234\n", "BASE = 'http://localhost:' + str(PORT_NUMBER) + '/v1/'\n", "\n", "# Header for posting data to the server as JSON\n", "HEADERS = {'Content-Type': 'application/json'}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate Networks with NetworkX\n", "\n", "### Generate scale-free networks" ] }, { "cell_type": "code", "collapsed": false, "input": [ "graphs = []\n", "\n", "# Create 10 small randome networks\n", "for i in range(10):\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": 6 }, { "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": 7 }, { "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(BASE + '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(BASE + 'networks/' + str(suid) + \"/views/first\")\n", " network_views.append(json.loads(response2.content))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[64700, 66370, 66720, 64008, 66054, 64362, 65726, 65028, 67068, 65340]\n" ] } ], "prompt_number": 9 }, { "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(BASE + \"styles\", data=json.dumps(my_style), headers=HEADERS)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }