{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to _py2cytoscape_: Pythonista-friendly wrapper for cyREST\n", "\n", "![](http://cl.ly/XohP/logo300.png)\n", "\n", "

For

\n", "\n", "![](https://www.python.org/static/community_logos/python-logo-master-v3-TM.png)\n", "\n", "by [Keiichiro Ono](http://keiono.github.io/) - University of California, San Diego [Trey Ideker Lab](http://healthsciences.ucsd.edu/som/medicine/research/labs/ideker/Pages/default.aspx)\n", "\n", "## Requirments\n", "* Java 8\n", "* Cytoscape 3.2.1+\n", "* cyREST 1.1.0+\n", "* py2cytoscape 0.4.2+\n", "\n", "----\n", "\n", "## Q. What is _py2cytoscape_?\n", "## A. A Python package to drive Cytoscape in pythonic way" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# In a Nutshell..." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processing /Users/kono/git/py2cytoscape\n", " Requirement already satisfied (use --upgrade to upgrade): py2cytoscape==0.4.3 from file:///Users/kono/git/py2cytoscape in /Users/kono/anaconda/lib/python3.4/site-packages\n", "Requirement already satisfied (use --upgrade to upgrade): pandas in /Users/kono/anaconda/lib/python3.4/site-packages (from py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): networkx in /Users/kono/anaconda/lib/python3.4/site-packages (from py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): requests in /Users/kono/anaconda/lib/python3.4/site-packages (from py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): python-igraph in /Users/kono/anaconda/lib/python3.4/site-packages (from py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): pyparsing in /Users/kono/anaconda/lib/python3.4/site-packages (from py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): scipy in /Users/kono/anaconda/lib/python3.4/site-packages (from py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): python-dateutil>=2 in /Users/kono/anaconda/lib/python3.4/site-packages (from pandas->py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): pytz>=2011k in /Users/kono/anaconda/lib/python3.4/site-packages (from pandas->py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.0 in /Users/kono/anaconda/lib/python3.4/site-packages (from pandas->py2cytoscape==0.4.3)\n", "Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /Users/kono/anaconda/lib/python3.4/site-packages (from python-dateutil>=2->pandas->py2cytoscape==0.4.3)\n" ] } ], "source": [ "!pip install ../." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "if (window['cytoscape'] === undefined) {\n", " var paths = {\n", " cytoscape: 'http://cytoscape.github.io/cytoscape.js/api/cytoscape.js-latest/cytoscape.min'\n", " };\n", "\n", " require.config({\n", " paths: paths\n", " });\n", "\n", " require(['cytoscape'], function (cytoscape) {\n", " console.log('Loading Cytoscape.js Module...');\n", " window['cytoscape'] = cytoscape;\n", "\n", " var event = document.createEvent(\"HTMLEvents\");\n", " event.initEvent(\"load_cytoscape\", true, false);\n", " window.dispatchEvent(event);\n", " });\n", "}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from py2cytoscape.data.cynetwork import CyNetwork\n", "from py2cytoscape.data.cyrest_client import CyRestClient\n", "from py2cytoscape.data.style import StyleUtil\n", "import py2cytoscape.util.cytoscapejs as cyjs\n", "import py2cytoscape.cytoscapejs as renderer\n", "\n", "import networkx as nx\n", "import pandas as pd\n", "import json" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
aliasannotation.DB_Object_Nameannotation.GO BIOLOGICAL_PROCESSannotation.GO CELLULAR_COMPONENTannotation.GO MOLECULAR_FUNCTIONlabelkegguniprotsgdentrez
YAL003Wtranslation elongation factor EF-1beta, EF-1 b...EF-1 beta, translation elongation factor EF-1betatranslational elongationeukaryotic translation elongation factor 1 com...translation elongation factor activityEF1Bsce:YAL003WEF1B_YEASTS000000003851260
YAL030WS000000028, SNC1NaNGolgi to plasma membrane transport, endocytosi...Golgi trans face, SNARE complex, endosome, tra...v-SNARE activitySNC1sce:YAL030WSNC1_YEASTS000000028851203
YAL038WCDC19, PYK1, S000000036, pyruvate kinasepyruvate kinaseglycolysis, pyruvate metabolic processcytosolpyruvate kinase activityKPYK1sce:YAL038WKPYK1_YEASTS000000036851193
YAL040CCLN3, DAF1, FUN10, G1 cyclin, S000000038, WHI1G1 cyclinG1/S transition of mitotic cell cycle, regulat...nucleuscyclin-dependent protein kinase regulator acti...CG13sce:YAL040CCG13_YEASTS000000038851191
YAR007Cheterotrimeric RPA (RF-A) single-stranded DNA ...RF-A, heterotrimeric RPA (RF-A) single-strande...DNA recombination, DNA replication, synthesis ...DNA replication factor A complex, chromosome, ...damaged DNA binding, single-stranded DNA bindingRFA1sce:YAR007CRFA1_YEASTS000000065851266
\n", "
" ], "text/plain": [ " alias \\\n", "YAL003W translation elongation factor EF-1beta, EF-1 b... \n", "YAL030W S000000028, SNC1 \n", "YAL038W CDC19, PYK1, S000000036, pyruvate kinase \n", "YAL040C CLN3, DAF1, FUN10, G1 cyclin, S000000038, WHI1 \n", "YAR007C heterotrimeric RPA (RF-A) single-stranded DNA ... \n", "\n", " annotation.DB_Object_Name \\\n", "YAL003W EF-1 beta, translation elongation factor EF-1beta \n", "YAL030W NaN \n", "YAL038W pyruvate kinase \n", "YAL040C G1 cyclin \n", "YAR007C RF-A, heterotrimeric RPA (RF-A) single-strande... \n", "\n", " annotation.GO BIOLOGICAL_PROCESS \\\n", "YAL003W translational elongation \n", "YAL030W Golgi to plasma membrane transport, endocytosi... \n", "YAL038W glycolysis, pyruvate metabolic process \n", "YAL040C G1/S transition of mitotic cell cycle, regulat... \n", "YAR007C DNA recombination, DNA replication, synthesis ... \n", "\n", " annotation.GO CELLULAR_COMPONENT \\\n", "YAL003W eukaryotic translation elongation factor 1 com... \n", "YAL030W Golgi trans face, SNARE complex, endosome, tra... \n", "YAL038W cytosol \n", "YAL040C nucleus \n", "YAR007C DNA replication factor A complex, chromosome, ... \n", "\n", " annotation.GO MOLECULAR_FUNCTION label \\\n", "YAL003W translation elongation factor activity EF1B \n", "YAL030W v-SNARE activity SNC1 \n", "YAL038W pyruvate kinase activity KPYK1 \n", "YAL040C cyclin-dependent protein kinase regulator acti... CG13 \n", "YAR007C damaged DNA binding, single-stranded DNA binding RFA1 \n", "\n", " kegg uniprot sgd entrez \n", "YAL003W sce:YAL003W EF1B_YEAST S000000003 851260 \n", "YAL030W sce:YAL030W SNC1_YEAST S000000028 851203 \n", "YAL038W sce:YAL038W KPYK1_YEAST S000000036 851193 \n", "YAL040C sce:YAL040C CG13_YEAST S000000038 851191 \n", "YAR007C sce:YAR007C RFA1_YEAST S000000065 851266 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# !!!!!!!!!!!!!!!!! Step 0: Start Cytoscape 3 with cyREST App !!!!!!!!!!!!!!!!!!!!!!!!!!\n", "\n", "# Step 1: Create py2cytoscape client\n", "cy = CyRestClient()\n", "\n", "# Reset\n", "cy.session.delete()\n", "\n", "# Step 2: Load network from somewhere\n", "yeast_net = cy.network.create_from('../tests/data/galFiltered.json')\n", "\n", "# Step 3: Load table as pandas' DataFrame\n", "table_data = pd.read_csv('sample_data_table.csv', index_col=0)\n", "table_data.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", "\n", " \n", "\n", "\n", "\n", "
\n", " \n", "
\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Step 4: Merge them in Cytoscape\n", "yeast_net.update_node_table(df=table_data, network_key_col='name')\n", "\n", "# Step 5: Apply layout\n", "cy.layout.apply(name='force-directed', network=yeast_net)\n", "\n", "# Step 6: Create Visual Style as code (or by hand if you prefer)\n", "my_yeast_style = cy.style.create('GAL Style')\n", "\n", "\n", "basic_settings = {\n", " \n", " # You can set default values as key-value pairs.\n", " \n", " 'NODE_FILL_COLOR': '#6AACB8',\n", " 'NODE_SIZE': 55,\n", " 'NODE_BORDER_WIDTH': 0,\n", " 'NODE_LABEL_COLOR': '#555555',\n", " \n", " 'EDGE_WIDTH': 2,\n", " 'EDGE_TRANSPARENCY': 100,\n", " 'EDGE_STROKE_UNSELECTED_PAINT': '#333333',\n", " \n", " 'NETWORK_BACKGROUND_PAINT': '#FFFFEA'\n", "}\n", "\n", "my_yeast_style.update_defaults(basic_settings)\n", "\n", "# Create some mappings\n", "my_yeast_style.create_passthrough_mapping(column='label', vp='NODE_LABEL', col_type='String')\n", "\n", "degrees = yeast_net.get_node_column('Degree') \n", "color_gradient = StyleUtil.create_2_color_gradient(min=degrees.min(), max=degrees.max(), colors=('white', '#6AACB8'))\n", "degree_to_size = StyleUtil.create_slope(min=degrees.min(), max=degrees.max(), values=(10, 100))\n", "my_yeast_style.create_continuous_mapping(column='Degree', vp='NODE_FILL_COLOR', col_type='Integer', points=color_gradient)\n", "my_yeast_style.create_continuous_mapping(column='Degree', vp='NODE_SIZE', col_type='Integer', points=degree_to_size)\n", "my_yeast_style.create_continuous_mapping(column='Degree', vp='NODE_LABEL_FONT_SIZE', col_type='Integer', points=degree_to_size)\n", "\n", "cy.style.apply(my_yeast_style, yeast_net)\n", "\n", "# Step 7: (Optional) Embed as interactive Cytoscape.js widget\n", "yeast_net_view = yeast_net.get_first_view()\n", "style_for_widget = cy.style.get(my_yeast_style.get_name(), data_format='cytoscapejs')\n", "renderer.render(yeast_net_view, style=style_for_widget['style'], background='radial-gradient(#FFFFFF 15%, #DDDDDD 105%)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Long Description\n", "From version 0.4.0, ___py2cytoscape___ has wrapper modules for [cyREST](http://apps.cytoscape.org/apps/cyrest) RESTful API. This means you can access Cytoscape features in more _Pythonic_ way instead of calling raw REST API via HTTP. \n", "\n", "\n", "## Features\n", "\n", "### Pandas for basic data exchange\n", "Since [pandas](http://pandas.pydata.org/) is a standard library for data mangling/analysis in Python, this new version uses its [DataFrame](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html#pandas.DataFrame) as its basic data object.\n", "\n", "### Embedded Cytoscaep.js Widget\n", "You can use Cytoscape.js widget to embed your final result as a part of your notebook.\n", "\n", "### Simpler Code to access Cytoscape\n", "cyREST provides language-agnostic RESTful API, but you need to use a lot of template code to access raw API. Here is an example. Both of the following do the same task, which is creating an empty network in Cytoscape. You will notice it is significantly simpler if you use _py2cytoscape_ wrapper API.\n", "\n", "#### Raw cyREST" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "New network created with raw REST API. Its SUID is 239076\n" ] } ], "source": [ "# HTTP Client for Python\n", "import requests\n", "\n", "# Standard JSON library\n", "import json\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'}\n", "\n", "# Define dictionary of empty network\n", "empty_network = {\n", " 'data': {\n", " 'name': 'I\\'m empty!'\n", " },\n", " 'elements': {\n", " 'nodes':[],\n", " 'edges':[]\n", " }\n", "}\n", "\n", "res = requests.post(BASE + 'networks?collection=My%20Collection', data=json.dumps(empty_network), headers=HEADERS)\n", "new_network_id = res.json()['networkSUID']\n", "print('New network created with raw REST API. Its SUID is ' + str(new_network_id))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### With py2cytoscape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "New network created with py2cytoscape. Its SUID is 239104\n" ] } ], "source": [ "network = cy.network.create(name='My Network', collection='My network collection')\n", "print('New network created with py2cytoscape. Its SUID is ' + str(network.get_id()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Status\n", "\n", "As of 6/4/2015, this is still in alpha status and feature requests are always welcome. If youi have questions or feature requests, please send them to our Google Groups:\n", "\n", "* https://groups.google.com/forum/#!forum/cytoscape-discuss" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Quick Tour of py2cytoscape Features\n", "\n", "----\n", "\n", "## Create a client object to connect to Cytoscape" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create an instance of cyREST client. Default IP is 'localhost', and port number is 1234.\n", "# cy = CyRestClient() - This default constructor creates connection to http://localhost:1234/v1\n", "cy = CyRestClient(ip='127.0.0.1', port=1234)\n", "\n", "# Cleanup: Delete all existing networks and tables in current Cytoscape session\n", "cy.session.delete()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating empty networks" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Empty network\n", "empty1 = cy.network.create()\n", "\n", "# With name\n", "empty2 = cy.network.create(name='Created in Jupyter Notebook')\n", "\n", "# With name and collection name\n", "empty3 = cy.network.create(name='Also created in Jupyter', collection='New network collection')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load networks from files, URLs or web services" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CyNetwork
file:////Users/kono/git/py2cytoscape/examples/sample_yeast_network.xgmml<py2cytoscape.data.cynetwork.CyNetwork object ...
http://chianti.ucsd.edu/cytoscape-data/galFiltered.sif<py2cytoscape.data.cynetwork.CyNetwork object ...
http://www.ebi.ac.uk/Tools/webservices/psicquic/intact/webservices/current/search/query/brca1?format=xml25<py2cytoscape.data.cynetwork.CyNetwork object ...
\n", "
" ], "text/plain": [ " CyNetwork\n", "file:////Users/kono/git/py2cytoscape/examples/s... \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sourceinteractiontarget
0YKR026CppYGL122C
1YGR218WppYGL097W
2YGL097WppYOR204W
3YLR249WppYPR080W
4YLR249WppYBR118W
\n", "
" ], "text/plain": [ " source interaction target\n", "0 YKR026C pp YGL122C\n", "1 YGR218W pp YGL097W\n", "2 YGL097W pp YOR204W\n", "3 YLR249W pp YPR080W\n", "4 YLR249W pp YBR118W" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Pandas DataFrame\n", "\n", "# Example 1: From a simple text table\n", "df_from_sif = pd.read_csv('../tests/data/galFiltered.sif', names=['source', 'interaction', 'target'], sep=' ')\n", "df_from_sif.head()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
#ID(s) interactor AID(s) interactor BAlt. ID(s) interactor AAlt. ID(s) interactor BAlias(es) interactor AAlias(es) interactor BInteraction detection method(s)Publication 1st author(s)Publication Identifier(s)Taxid interactor A...Checksum(s) interactor AChecksum(s) interactor BInteraction Checksum(s)NegativeFeature(s) interactor AFeature(s) interactor BStoichiometry(s) interactor AStoichiometry(s) interactor BIdentification method participant AIdentification method participant B
0uniprotkb:Q8IZC7uniprotkb:P11802intact:EBI-5278328|uniprotkb:C9JU83|uniprotkb:...intact:EBI-295644|uniprotkb:B2R9A0|uniprotkb:B...psi-mi:zn101_human(display_long)|uniprotkb:ZNF...psi-mi:cdk4_human(display_long)|uniprotkb:CDK4...psi-mi:\"MI:0424\"(protein kinase assay)Anders et al. (2011)pubmed:22094256|imex:IM-16628taxid:9606(human)|taxid:9606(Homo sapiens)...rogid:mZLXs/PbWbfgLzkjwZfoE2c6jy09606rogid:pxIzF9EkX+gMz7zj/TDiytYtsOc9606intact-crc:0486DE729E5A8158|rigid:3gdrHm0coV8S...Falseglutathione s tranferase tag:?-?sufficient binding region:4-303|glutathione s ...--psi-mi:\"MI:0821\"(molecular weight estimation b...psi-mi:\"MI:0821\"(molecular weight estimation b...
1uniprotkb:P01106uniprotkb:Q00534intact:EBI-447544|intact:EBI-1058440|uniprotkb...intact:EBI-295663|uniprotkb:A4D1G0psi-mi:myc_human(display_long)|uniprotkb:BHLHE...psi-mi:cdk6_human(display_long)|uniprotkb:CDK6...psi-mi:\"MI:0424\"(protein kinase assay)Anders et al. (2011)pubmed:22094256|imex:IM-16628taxid:9606(human)|taxid:9606(Homo sapiens)...rogid:CCk+p9FN7JV6cTmJDlin2hRp1tg9606rogid:IlAJCZ9bvB5RaBdS1rlPnqDL9xg9606intact-crc:20A3DCC8419B269E|rigid:bfSR2lJKa/lY...Falseglutathione s tranferase tag:?-?his tag:n-n--psi-mi:\"MI:0821\"(molecular weight estimation b...psi-mi:\"MI:0821\"(molecular weight estimation b...
2uniprotkb:Q00534uniprotkb:P28698intact:EBI-295663|uniprotkb:A4D1G0intact:EBI-5278283|uniprotkb:M0QXU0|uniprotkb:...psi-mi:cdk6_human(display_long)|uniprotkb:CDK6...psi-mi:mzf1_human(display_long)|uniprotkb:MZF1...psi-mi:\"MI:0424\"(protein kinase assay)Anders et al. (2011)pubmed:22094256|imex:IM-16628taxid:9606(human)|taxid:9606(Homo sapiens)...rogid:IlAJCZ9bvB5RaBdS1rlPnqDL9xg9606rogid:1rq+ga/Uew705r63KlORpqBObbw9606intact-crc:578214FA683591D0|rigid:8AADqx3NNAXF...Falsehis tag:n-nglutathione s tranferase tag:?-?--psi-mi:\"MI:0821\"(molecular weight estimation b...psi-mi:\"MI:0821\"(molecular weight estimation b...
3uniprotkb:P11802uniprotkb:Q9H165intact:EBI-295644|uniprotkb:B2R9A0|uniprotkb:B...intact:EBI-765567|uniprotkb:Q86W14|uniprotkb:Q...psi-mi:cdk4_human(display_long)|uniprotkb:CDK4...psi-mi:bc11a_human(display_long)|uniprotkb:B-c...psi-mi:\"MI:0424\"(protein kinase assay)Anders et al. (2011)pubmed:22094256|imex:IM-16628taxid:9606(human)|taxid:9606(Homo sapiens)...rogid:pxIzF9EkX+gMz7zj/TDiytYtsOc9606rogid:xASsLQWykg/7ZpyjLsQP8GlQ/H09606intact-crc:F173ABFF34E61592|rigid:5u6F7IwnpEEf...Falsesufficient binding region:4-303|glutathione s ...glutathione s tranferase tag:?-?--psi-mi:\"MI:0821\"(molecular weight estimation b...psi-mi:\"MI:0821\"(molecular weight estimation b...
4uniprotkb:Q14980uniprotkb:Q00534intact:EBI-521611|uniprotkb:H0YH75|uniprotkb:Q...intact:EBI-295663|uniprotkb:A4D1G0psi-mi:numa1_human(display_long)|uniprotkb:SP-...psi-mi:cdk6_human(display_long)|uniprotkb:CDK6...psi-mi:\"MI:0424\"(protein kinase assay)Anders et al. (2011)pubmed:22094256|imex:IM-16628taxid:9606(human)|taxid:9606(Homo sapiens)...rogid:goAKbcTz6vzGy3T7RlpGPnTCbdk9606rogid:IlAJCZ9bvB5RaBdS1rlPnqDL9xg9606intact-crc:43D7B9AE4E55FAA0|rigid:mLhZxkGnjrpK...Falseglutathione s tranferase tag:?-?his tag:n-n--psi-mi:\"MI:0821\"(molecular weight estimation b...psi-mi:\"MI:0821\"(molecular weight estimation b...
\n", "

5 rows × 42 columns

\n", "
" ], "text/plain": [ " #ID(s) interactor A ID(s) interactor B \\\n", "0 uniprotkb:Q8IZC7 uniprotkb:P11802 \n", "1 uniprotkb:P01106 uniprotkb:Q00534 \n", "2 uniprotkb:Q00534 uniprotkb:P28698 \n", "3 uniprotkb:P11802 uniprotkb:Q9H165 \n", "4 uniprotkb:Q14980 uniprotkb:Q00534 \n", "\n", " Alt. ID(s) interactor A \\\n", "0 intact:EBI-5278328|uniprotkb:C9JU83|uniprotkb:... \n", "1 intact:EBI-447544|intact:EBI-1058440|uniprotkb... \n", "2 intact:EBI-295663|uniprotkb:A4D1G0 \n", "3 intact:EBI-295644|uniprotkb:B2R9A0|uniprotkb:B... \n", "4 intact:EBI-521611|uniprotkb:H0YH75|uniprotkb:Q... \n", "\n", " Alt. ID(s) interactor B \\\n", "0 intact:EBI-295644|uniprotkb:B2R9A0|uniprotkb:B... \n", "1 intact:EBI-295663|uniprotkb:A4D1G0 \n", "2 intact:EBI-5278283|uniprotkb:M0QXU0|uniprotkb:... \n", "3 intact:EBI-765567|uniprotkb:Q86W14|uniprotkb:Q... \n", "4 intact:EBI-295663|uniprotkb:A4D1G0 \n", "\n", " Alias(es) interactor A \\\n", "0 psi-mi:zn101_human(display_long)|uniprotkb:ZNF... \n", "1 psi-mi:myc_human(display_long)|uniprotkb:BHLHE... \n", "2 psi-mi:cdk6_human(display_long)|uniprotkb:CDK6... \n", "3 psi-mi:cdk4_human(display_long)|uniprotkb:CDK4... \n", "4 psi-mi:numa1_human(display_long)|uniprotkb:SP-... \n", "\n", " Alias(es) interactor B \\\n", "0 psi-mi:cdk4_human(display_long)|uniprotkb:CDK4... \n", "1 psi-mi:cdk6_human(display_long)|uniprotkb:CDK6... \n", "2 psi-mi:mzf1_human(display_long)|uniprotkb:MZF1... \n", "3 psi-mi:bc11a_human(display_long)|uniprotkb:B-c... \n", "4 psi-mi:cdk6_human(display_long)|uniprotkb:CDK6... \n", "\n", " Interaction detection method(s) Publication 1st author(s) \\\n", "0 psi-mi:\"MI:0424\"(protein kinase assay) Anders et al. (2011) \n", "1 psi-mi:\"MI:0424\"(protein kinase assay) Anders et al. (2011) \n", "2 psi-mi:\"MI:0424\"(protein kinase assay) Anders et al. (2011) \n", "3 psi-mi:\"MI:0424\"(protein kinase assay) Anders et al. (2011) \n", "4 psi-mi:\"MI:0424\"(protein kinase assay) Anders et al. (2011) \n", "\n", " Publication Identifier(s) Taxid interactor A \\\n", "0 pubmed:22094256|imex:IM-16628 taxid:9606(human)|taxid:9606(Homo sapiens) \n", "1 pubmed:22094256|imex:IM-16628 taxid:9606(human)|taxid:9606(Homo sapiens) \n", "2 pubmed:22094256|imex:IM-16628 taxid:9606(human)|taxid:9606(Homo sapiens) \n", "3 pubmed:22094256|imex:IM-16628 taxid:9606(human)|taxid:9606(Homo sapiens) \n", "4 pubmed:22094256|imex:IM-16628 taxid:9606(human)|taxid:9606(Homo sapiens) \n", "\n", " ... \\\n", "0 ... \n", "1 ... \n", "2 ... \n", "3 ... \n", "4 ... \n", "\n", " Checksum(s) interactor A \\\n", "0 rogid:mZLXs/PbWbfgLzkjwZfoE2c6jy09606 \n", "1 rogid:CCk+p9FN7JV6cTmJDlin2hRp1tg9606 \n", "2 rogid:IlAJCZ9bvB5RaBdS1rlPnqDL9xg9606 \n", "3 rogid:pxIzF9EkX+gMz7zj/TDiytYtsOc9606 \n", "4 rogid:goAKbcTz6vzGy3T7RlpGPnTCbdk9606 \n", "\n", " Checksum(s) interactor B \\\n", "0 rogid:pxIzF9EkX+gMz7zj/TDiytYtsOc9606 \n", "1 rogid:IlAJCZ9bvB5RaBdS1rlPnqDL9xg9606 \n", "2 rogid:1rq+ga/Uew705r63KlORpqBObbw9606 \n", "3 rogid:xASsLQWykg/7ZpyjLsQP8GlQ/H09606 \n", "4 rogid:IlAJCZ9bvB5RaBdS1rlPnqDL9xg9606 \n", "\n", " Interaction Checksum(s) Negative \\\n", "0 intact-crc:0486DE729E5A8158|rigid:3gdrHm0coV8S... False \n", "1 intact-crc:20A3DCC8419B269E|rigid:bfSR2lJKa/lY... False \n", "2 intact-crc:578214FA683591D0|rigid:8AADqx3NNAXF... False \n", "3 intact-crc:F173ABFF34E61592|rigid:5u6F7IwnpEEf... False \n", "4 intact-crc:43D7B9AE4E55FAA0|rigid:mLhZxkGnjrpK... False \n", "\n", " Feature(s) interactor A \\\n", "0 glutathione s tranferase tag:?-? \n", "1 glutathione s tranferase tag:?-? \n", "2 his tag:n-n \n", "3 sufficient binding region:4-303|glutathione s ... \n", "4 glutathione s tranferase tag:?-? \n", "\n", " Feature(s) interactor B \\\n", "0 sufficient binding region:4-303|glutathione s ... \n", "1 his tag:n-n \n", "2 glutathione s tranferase tag:?-? \n", "3 glutathione s tranferase tag:?-? \n", "4 his tag:n-n \n", "\n", " Stoichiometry(s) interactor A Stoichiometry(s) interactor B \\\n", "0 - - \n", "1 - - \n", "2 - - \n", "3 - - \n", "4 - - \n", "\n", " Identification method participant A \\\n", "0 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "1 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "2 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "3 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "4 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "\n", " Identification method participant B \n", "0 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "1 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "2 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "3 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "4 psi-mi:\"MI:0821\"(molecular weight estimation b... \n", "\n", "[5 rows x 42 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# By default, it uses 'source' for source node column, 'target' for target node column, and 'interaction' for interaction\n", "yeast1 = cy.network.create_from_dataframe(df_from_sif, name='Yeast network created from pandas DataFrame')\n", "\n", "# Example 2: from more complicated table\n", "df_from_mitab = pd.read_csv('intact_pubid_22094256.txt', sep='\\t')\n", "df_from_mitab.head()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "source = df_from_mitab.columns[0]\n", "target = df_from_mitab.columns[1]\n", "interaction = 'Interaction identifier(s)'\n", "title='A Systematic Screen for CDK4/6 Substrates Links FOXM1 Phosphorylation to Senescence Suppression in Cancer Cells.'\n", "\n", "human1 = cy.network.create_from_dataframe(df_from_mitab, source_col=source, target_col=target, interaction_col=interaction, name=title)\n", "\n", "\n", "# Import edge attributes and node attributes at the same time (TBD)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# NetworkX\n", "nx_graph = nx.scale_free_graph(100)\n", "nx.set_node_attributes(nx_graph, 'Degree', nx.degree(nx_graph))\n", "nx.set_node_attributes(nx_graph, 'Betweenness_Centrality', nx.betweenness_centrality(nx_graph))\n", "scale_free100 = cy.network.create_from_networkx(nx_graph, collection='Generated by NetworkX')\n", "\n", "# TODO: igraph\n", "# TODO: Numpy adj. martix\n", "# TODO: GraphX" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Network from Cytoscape\n", "\n", "You can get network data in the following forms:\n", "\n", "* Cytoscape.js\n", "* NetworkX\n", "* DataFrame" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# As Cytoscape.js (dict)\n", "yeast1_json = yeast1.to_json()\n", "\n", "# print(json.dumps(yeast1_json, indent=4))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of Nodes: 100\n", "Number of Edges: 197\n" ] } ], "source": [ "# As NetworkX graph object\n", "sf100 = scale_free100.to_networkx()\n", "\n", "num_nodes = sf100.number_of_nodes()\n", "num_edges = sf100.number_of_edges()\n", "\n", "print('Number of Nodes: ' + str(num_nodes))\n", "print('Number of Edges: ' + str(num_edges))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sourceinteractiontarget
0YDR277CppYDL194W
1YDR277CppYJR022W
2YPR145WppYMR117C
3YER054CppYBR045C
4YER054CppYER133W
\n", "
" ], "text/plain": [ " source interaction target\n", "0 YDR277C pp YDL194W\n", "1 YDR277C pp YJR022W\n", "2 YPR145W pp YMR117C\n", "3 YER054C pp YBR045C\n", "4 YER054C pp YER133W" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# As a simple, SIF-like DataFrame\n", "yeast1_df = yeast1.to_dataframe()\n", "yeast1_df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with CyNetwork API\n", "___CyNetwork___ class is a simple wrapper for network-related cyREST raw REST API. __It does not hold the actual network data. It's a reference to a network in current Cytoscape session__. With CyNetwork API, you can access Cytoscape data objects in more Pythonista-friendly way." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This object references to Cytoscape network with SUID 246460\n", "\n", "And its name is: b'Yeast network created from pandas DataFrame'\n", "\n", "* This network has 331 nodes and 362 edges\n", "\n", "SUID 246470\n", "id YKR026C\n", "name YKR026C\n", "selected False\n", "shared_name YKR026C\n", "dtype: object\n", "\n", "This node has name: b'YKR026C'\n" ] } ], "source": [ "network_suid = yeast1.get_id()\n", "print('This object references to Cytoscape network with SUID ' + str(network_suid) + '\\n')\n", "print('And its name is: ' + str(yeast1.get_network_value(column='name')) + '\\n')\n", "\n", "nodes = yeast1.get_nodes()\n", "edges = yeast1.get_edges()\n", "\n", "print('* This network has ' + str(len(nodes)) + ' nodes and ' + str(len(edges)) + ' edges\\n') \n", "\n", "# Get a row in the node table as pandas Series object\n", "node0 = nodes[0]\n", "row = yeast1.get_node_value(id=node0)\n", "print(row)\n", "\n", "# Or, pick one cell in the table\n", "cell = yeast1.get_node_value(id=node0, column='name')\n", "print('\\nThis node has name: ' + str(cell))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get references from existing networks\n", "And of course, you can grab references to existing Cytoscape networks:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "b'Yeast network created from pandas DataFrame'\n" ] } ], "source": [ "# Create a new CyNetwork object from existing network\n", "network_ref1 = cy.network.create(suid=yeast1.get_id())\n", "\n", "# And they are considered as same objects.\n", "print(network_ref1 == yeast1)\n", "print(network_ref1.get_network_value(column='name'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tables as DataFrame\n", "Cytoscape has two main data types: ___Network___ and ___Table___. Network is the graph topology, and Tables are properties for those graphs. For simplicity, this library has access to three basic table objects:\n", "\n", "* Node Table\n", "* Edge Table\n", "* Network Table\n", "\n", "For 99% of your use cases, you can use these three to store properties. Since [pandas](http://pandas.pydata.org/) is extremely useful to handle table data, default data type for tables is __DataFrame__. However, you can also use other data types including:\n", "\n", "* Cytoscape.js style JSON\n", "* CSV\n", "* TSV\n", "* CX (TBD)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
shared namenameselectedidDegreeBetweenness_Centrality
SUID
24821800False0630.032124
24821911False1940.085927
24822022False2240.013760
24822133False370.000000
24822244False4130.016595
\n", "
" ], "text/plain": [ " shared name name selected id Degree Betweenness_Centrality\n", "SUID \n", "248218 0 0 False 0 63 0.032124\n", "248219 1 1 False 1 94 0.085927\n", "248220 2 2 False 2 24 0.013760\n", "248221 3 3 False 3 7 0.000000\n", "248222 4 4 False 4 13 0.016595" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get table from Cytoscape\n", "node_table = scale_free100.get_node_table()\n", "edge_table = scale_free100.get_edge_table()\n", "network_table = scale_free100.get_network_table()\n", "node_table.head()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SUID248208
shared namedirected_scale_free_graph(100,alpha=0.41,beta=...
namedirected_scale_free_graph(100,alpha=0.41,beta=...
selectedTrue
__AnnotationsNaN
\n", "
" ], "text/plain": [ "SUID 248208\n", "shared name directed_scale_free_graph(100,alpha=0.41,beta=...\n", "name directed_scale_free_graph(100,alpha=0.41,beta=...\n", "selected True\n", "__Annotations NaN" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "network_table.transpose().head()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 94\n", "1 63\n", "2 7\n", "3 13\n", "4 7\n", "dtype: float64\n" ] }, { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
immutableprimaryKeytype
name
SUIDTrueTrueLong
shared nameTrueFalseString
nameTrueFalseString
selectedTrueFalseBoolean
idFalseFalseString
DegreeFalseFalseDouble
Betweenness_CentralityFalseFalseDouble
\n", "
" ], "text/plain": [ " immutable primaryKey type\n", "name \n", "SUID True True Long\n", "shared name True False String\n", "name True False String\n", "selected True False Boolean\n", "id False False String\n", "Degree False False Double\n", "Betweenness_Centrality False False Double" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names = scale_free100.get_node_column('Degree')\n", "print(names.head())\n", "\n", "# Node Column information. \"name\" is the unique Index\n", "scale_free100.get_node_columns()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Edit Network Topology\n", "\n", "### Adding and deleteing nodes/edges" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sourcetarget
SUID
248820248814248815
248821248814248816
248822248815248816
\n", "
" ], "text/plain": [ " source target\n", "SUID \n", "248820 248814 248815\n", "248821 248814 248816\n", "248822 248815 248816" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add new nodes: Simply send the list of node names. NAMES SHOULD BE UNIQUE!\n", "new_node_names = ['a', 'b', 'c']\n", "# Return value contains dictionary from name to SUID.\n", "new_nodes = scale_free100.add_nodes(new_node_names)\n", "\n", "# Add new edges\n", "# Send a list of tuples: (source node SUID, target node SUID, interaction type\n", "new_edges = []\n", "new_edges.append((new_nodes['a'], new_nodes['b'], 'type1'))\n", "new_edges.append((new_nodes['a'], new_nodes['c'], 'type2'))\n", "new_edges.append((new_nodes['b'], new_nodes['c'], 'type3'))\n", "\n", "new_edge_ids = scale_free100.add_edges(new_edges)\n", "new_edge_ids" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Delete node\n", "scale_free100.delete_node(new_nodes['a'])\n", "\n", "# Delete edge\n", "scale_free100.delete_edge(new_edge_ids.index[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update Table\n", "Let's do something a bit more realistic. You can update any Tables by using DataFrame objects.\n", "\n", "### 1. ID conversion with external service\n", "Let's use [ID Conversion web service by Uniprot](http://www.uniprot.org/help/programmatic_access) to add more information to existing yeast network in current session." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Small utility function to convert ID sets\n", "import requests\n", "\n", "def uniprot_id_mapping_service(query=None, from_id=None, to_id=None):\n", " # Uniprot ID Mapping service\n", " url = 'http://www.uniprot.org/mapping/'\n", " payload = {\n", " 'from': from_id,\n", " 'to': to_id,\n", " 'format':'tab',\n", " 'query': query\n", " }\n", " \n", " res = requests.get(url, params=payload)\n", " \n", " df = pd.read_csv(res.url, sep='\\t')\n", " res.close()\n", " return df" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
kegguniprotsgdentrezname
0sce:YKR026CEI2BA_YEASTS000001734853896YKR026C
1sce:YGL122CNAB2_YEASTS000003090852755YGL122C
2sce:YGR218WXPO1_YEASTS000003450853133YGR218W
3sce:YGL097WRCC1_YEASTS000003065852782YGL097W
4sce:YOR204WDED1_YEASTS000005730854379YOR204W
\n", "
" ], "text/plain": [ " kegg uniprot sgd entrez name\n", "0 sce:YKR026C EI2BA_YEAST S000001734 853896 YKR026C\n", "1 sce:YGL122C NAB2_YEAST S000003090 852755 YGL122C\n", "2 sce:YGR218W XPO1_YEAST S000003450 853133 YGR218W\n", "3 sce:YGL097W RCC1_YEAST S000003065 852782 YGL097W\n", "4 sce:YOR204W DED1_YEAST S000005730 854379 YOR204W" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get node table from Cytoscape\n", "yeast_node_table = yeast1.get_node_table()\n", "\n", "# From KEGG ID to UniprotKB ID\n", "query1 = ' '.join(yeast_node_table['name'].map(lambda gene_id: 'sce:' + gene_id).values)\n", "id_map_kegg2uniprot = uniprot_id_mapping_service(query1, from_id='KEGG_ID', to_id='ID')\n", "id_map_kegg2uniprot.columns = ['kegg', 'uniprot']\n", "\n", "# From UniprotKB to SGD\n", "query2 = ' '.join(id_map_kegg2uniprot['uniprot'].values)\n", "id_map_uniprot2sgd = uniprot_id_mapping_service(query2, from_id='ID', to_id='SGD_ID')\n", "id_map_uniprot2sgd.columns = ['uniprot', 'sgd']\n", "\n", "# From UniprotKB to Entrez Gene ID\n", "query3 = ' '.join(id_map_kegg2uniprot['uniprot'].values)\n", "id_map_uniprot2ncbi = uniprot_id_mapping_service(query3, from_id='ID', to_id='P_ENTREZGENEID')\n", "id_map_uniprot2ncbi.columns = ['uniprot', 'entrez']\n", "\n", "# Merge them\n", "merged = pd.merge(id_map_kegg2uniprot, id_map_uniprot2sgd, on='uniprot')\n", "merged = pd.merge(merged, id_map_uniprot2ncbi, on='uniprot')\n", "\n", "# Add key column by removing prefix\n", "merged['name'] = merged['kegg'].map(lambda kegg_id : kegg_id[4:])\n", "merged.head()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "http://localhost:1234/v1/networks/246460/tables/defaultnode\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "update_url = BASE + 'networks/' + str(yeast1.get_id()) + '/tables/defaultnode'\n", "print(update_url)\n", "\n", "ut = {\n", " 'key': 'name',\n", " 'dataKey': 'name',\n", " 'data': [\n", " {\n", " 'name': 'YBR112C',\n", " 'foo': 'aaaaaaaa'\n", " }\n", " ]\n", "}\n", "\n", "requests.put(update_url, json=ut, headers=HEADERS)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
shared namenameselectedidfookeggentrezuniprotsgd
SUID
246470YKR026CYKR026CFalseYKR026CNaNsce:YKR026C853896EI2BA_YEASTS000001734
246471YGL122CYGL122CFalseYGL122CNaNsce:YGL122C852755NAB2_YEASTS000003090
246472YGR218WYGR218WFalseYGR218WNaNsce:YGR218W853133XPO1_YEASTS000003450
246473YGL097WYGL097WFalseYGL097WNaNsce:YGL097W852782RCC1_YEASTS000003065
246474YOR204WYOR204WFalseYOR204WNaNsce:YOR204W854379DED1_YEASTS000005730
\n", "
" ], "text/plain": [ " shared name name selected id foo kegg entrez \\\n", "SUID \n", "246470 YKR026C YKR026C False YKR026C NaN sce:YKR026C 853896 \n", "246471 YGL122C YGL122C False YGL122C NaN sce:YGL122C 852755 \n", "246472 YGR218W YGR218W False YGR218W NaN sce:YGR218W 853133 \n", "246473 YGL097W YGL097W False YGL097W NaN sce:YGL097W 852782 \n", "246474 YOR204W YOR204W False YOR204W NaN sce:YOR204W 854379 \n", "\n", " uniprot sgd \n", "SUID \n", "246470 EI2BA_YEAST S000001734 \n", "246471 NAB2_YEAST S000003090 \n", "246472 XPO1_YEAST S000003450 \n", "246473 RCC1_YEAST S000003065 \n", "246474 DED1_YEAST S000005730 " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now update existing node table with the data frame above.\n", "yeast1.update_node_table(merged, network_key_col='name', data_key_col='name')\n", "\n", "# Check the table is actually updated\n", "yeast1.get_node_table().head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create / Delete Table Data\n", "Currently, ___you cannot delete the table or rows___ due to the Cytoscape data model design. However, it is easy to create / delete columns:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
shared namenameselectedidfooentrezuniprotsgdNew Empty Double ColumnEmpty String Col
SUID
246470YKR026CYKR026CFalseYKR026CNaN853896EI2BA_YEASTS000001734NaNNaN
246471YGL122CYGL122CFalseYGL122CNaN852755NAB2_YEASTS000003090NaNNaN
246472YGR218WYGR218WFalseYGR218WNaN853133XPO1_YEASTS000003450NaNNaN
246473YGL097WYGL097WFalseYGL097WNaN852782RCC1_YEASTS000003065NaNNaN
246474YOR204WYOR204WFalseYOR204WNaN854379DED1_YEASTS000005730NaNNaN
\n", "
" ], "text/plain": [ " shared name name selected id foo entrez uniprot \\\n", "SUID \n", "246470 YKR026C YKR026C False YKR026C NaN 853896 EI2BA_YEAST \n", "246471 YGL122C YGL122C False YGL122C NaN 852755 NAB2_YEAST \n", "246472 YGR218W YGR218W False YGR218W NaN 853133 XPO1_YEAST \n", "246473 YGL097W YGL097W False YGL097W NaN 852782 RCC1_YEAST \n", "246474 YOR204W YOR204W False YOR204W NaN 854379 DED1_YEAST \n", "\n", " sgd New Empty Double Column Empty String Col \n", "SUID \n", "246470 S000001734 NaN NaN \n", "246471 S000003090 NaN NaN \n", "246472 S000003450 NaN NaN \n", "246473 S000003065 NaN NaN \n", "246474 S000005730 NaN NaN " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Delete columns\n", "yeast1.delete_node_table_column('kegg')\n", "\n", "# Create columns\n", "yeast1.create_node_column(name='New Empty Double Column', data_type='Double', is_immutable=False, is_list=False)\n", "\n", "# Default is String, mutable column.\n", "yeast1.create_node_column(name='Empty String Col')\n", "\n", "yeast1.get_node_table().head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visual Styles\n", "You can also use wrapper API to access Visual Styles.\n", "\n", "Current limitations are:\n", "\n", "* You need to use unique name for the Styles\n", "* Need to know how to write serialized form of objects" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\n", " \"Universe\",\n", " \"Sample1\",\n", " \"Ripple\",\n", " \"Minimal\",\n", " \"default\",\n", " \"Solid\",\n", " \"Big Labels\",\n", " \"Nested Network Style\",\n", " \"Directed\",\n", " \"default black\"\n", "]\n", "sample_style1\n", "default\n", "103\n", "0 EDGE\n", "1 EDGE_BEND\n", "2 EDGE_CURVED\n", "3 EDGE_LABEL\n", "4 EDGE_LABEL_COLOR\n", "dtype: object\n" ] } ], "source": [ "# Get all existing Visual Styles\n", "import json\n", "styles = cy.style.get_all()\n", "print(json.dumps(styles, indent=4))\n", "\n", "# Create a new style\n", "style1 = cy.style.create('sample_style1')\n", "\n", "# Get a reference to the existing style\n", "default_style = cy.style.create('default')\n", "\n", "print(style1.get_name())\n", "print(default_style.get_name())\n", "\n", "# Get all available Visual Properties\n", "print(len(cy.style.vps.get_all()))\n", "\n", "# Get Visual Properties for each data type\n", "node_vps = cy.style.vps.get_node_visual_props()\n", "edge_vps = cy.style.vps.get_edge_visual_props()\n", "network_vps = cy.style.vps.get_network_visual_props()\n", "\n", "print(pd.Series(edge_vps).head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set default values\n", "To set default values for Visual Properties, simply pass key-value pairs as dictionary." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Prepare key-value pair for Style defaults\n", "\n", "new_defaults = {\n", " # Node defaults\n", " 'NODE_FILL_COLOR': '#eeeeff',\n", " 'NODE_SIZE': 20,\n", " 'NODE_BORDER_WIDTH': 0,\n", " 'NODE_TRANSPARENCY': 120,\n", " 'NODE_LABEL_COLOR': 'white',\n", " \n", " # Edge defaults\n", " 'EDGE_WIDTH': 3,\n", " 'EDGE_STROKE_UNSELECTED_PAINT': '#aaaaaa',\n", " 'EDGE_LINE_TYPE': 'LONG_DASH',\n", " 'EDGE_TRANSPARENCY': 120,\n", " \n", " # Network defaults\n", " 'NETWORK_BACKGROUND_PAINT': 'black'\n", "}\n", "\n", "# Update\n", "style1.update_defaults(new_defaults)\n", "\n", "# Apply the new style\n", "cy.style.apply(style1, yeast1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visual Mappings" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Passthrough mapping\n", "style1.create_passthrough_mapping(column='name', col_type='String', vp='NODE_LABEL')\n", "\n", "# Discrete mapping: Simply prepare key-value pairs and send it\n", "kv_pair = {\n", " 'pp': 'pink',\n", " 'pd': 'green'\n", "}\n", "style1.create_discrete_mapping(column='interaction', \n", " col_type='String', vp='EDGE_STROKE_UNSELECTED_PAINT', mappings=kv_pair)\n", "\n", "# Continuous mapping\n", "points = [\n", " {\n", " 'value': '1.0',\n", " 'lesser':'white',\n", " 'equal':'white',\n", " 'greater': 'white'\n", " },\n", " {\n", " 'value': '20.0',\n", " 'lesser':'green',\n", " 'equal':'green',\n", " 'greater': 'green'\n", " }\n", "]\n", "\n", "minimal_style = cy.style.create('Minimal')\n", "minimal_style.create_continuous_mapping(column='Degree', col_type='Double', vp='NODE_FILL_COLOR', points=points)\n", "\n", "# Or, use utility for simple mapping\n", "simple_slope = StyleUtil.create_slope(min=1, max=20, values=(10, 60))\n", "minimal_style.create_continuous_mapping(column='Degree', col_type='Double', vp='NODE_SIZE', points=simple_slope)\n", "\n", "# Apply the new style\n", "cy.style.apply(minimal_style, scale_free100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Layouts\n", "Currently, this supports automatic layouts with default parameters." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\n", " \"attribute-circle\",\n", " \"stacked-node-layout\",\n", " \"degree-circle\",\n", " \"circular\",\n", " \"attributes-layout\",\n", " \"kamada-kawai\",\n", " \"force-directed\",\n", " \"grid\",\n", " \"hierarchical\",\n", " \"fruchterman-rheingold\",\n", " \"isom\"\n", "]\n" ] } ], "source": [ "# Get list of available layout algorithms\n", "layouts = cy.layout.get_all()\n", "print(json.dumps(layouts, indent=4))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datapositionselected
0{'sgd': 'S000002353', 'name': 'YDL194W', 'sele...{'y': 337.3408049125783, 'x': -816.2080368805437}False
1{'sgd': 'S000002685', 'name': 'YDR277C', 'sele...{'y': 283.5470442679916, 'x': -768.351675592697}False
2{'sgd': 'S000000247', 'name': 'YBR043C', 'sele...{'y': 700.0122446801881, 'x': 166.44780103059588}False
3{'sgd': 'S000006349', 'name': 'YPR145W', 'sele...{'y': 549.1767770841686, 'x': -524.6571716355409}False
4{'sgd': 'S000000856', 'name': 'YER054C', 'sele...{'y': -104.59739351311714, 'x': 1446.504571276...False
\n", "
" ], "text/plain": [ " data \\\n", "0 {'sgd': 'S000002353', 'name': 'YDL194W', 'sele... \n", "1 {'sgd': 'S000002685', 'name': 'YDR277C', 'sele... \n", "2 {'sgd': 'S000000247', 'name': 'YBR043C', 'sele... \n", "3 {'sgd': 'S000006349', 'name': 'YPR145W', 'sele... \n", "4 {'sgd': 'S000000856', 'name': 'YER054C', 'sele... \n", "\n", " position selected \n", "0 {'y': 337.3408049125783, 'x': -816.2080368805437} False \n", "1 {'y': 283.5470442679916, 'x': -768.351675592697} False \n", "2 {'y': 700.0122446801881, 'x': 166.44780103059588} False \n", "3 {'y': 549.1767770841686, 'x': -524.6571716355409} False \n", "4 {'y': -104.59739351311714, 'x': 1446.504571276... False " ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Apply layout\n", "cy.layout.apply(name='circular', network=yeast1)\n", "\n", "yeast1.get_views()\n", "yeast_view1 = yeast1.get_first_view()\n", "node_views = yeast_view1['elements']['nodes']\n", "df3 = pd.DataFrame(node_views)\n", "df3.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Embed Interactive Widget" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", "\n", " \n", "\n", "\n", "\n", "
\n", " \n", "
\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from py2cytoscape.cytoscapejs import viewer as cyjs\n", "cy.layout.apply(network=scale_free100)\n", "view1 = scale_free100.get_first_view()\n", "view2 = yeast1.get_first_view()\n", "# print(view1)\n", "cyjs.render(view2, 'default2', background='#efefef')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", "\n", " \n", "\n", "\n", "\n", "
\n", " \n", "
\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Use Cytoscape.js style JSON\n", "cyjs_style = cy.style.get(minimal_style.get_name(), data_format='cytoscapejs')\n", "cyjs.render(view1, style=cyjs_style['style'], background='white')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }