Skip to content

Instantly share code, notes, and snippets.

@keiono
Created July 31, 2014 02:24
Show Gist options
  • Save keiono/ca9f043c7037228fe387 to your computer and use it in GitHub Desktop.
Save keiono/ca9f043c7037228fe387 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:16f5dce6087ce370545bf44e4a6ced11e83ab8378c6feecd36db8c986a46f9f0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualize time series metabolome profile\n",
"\n",
"## Background:\n",
"This is a sample workflow to automate complex Cytoscape data integaration/visualization process. \n",
"\n",
"* https://github.com/idekerlab/KEGGscape/wiki/How-to-visualize-time-series-metabolome-profile\n",
"\n",
"----"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import json\n",
"import requests\n",
"import pandas as pd\n",
"\n",
"PORT_NUMBER = 8080\n",
"BASE_URL = \"http://localhost:\" + str(PORT_NUMBER) + \"/v1/\"\n",
"HEADERS = {'Content-Type': 'application/json'}\n",
"\n",
"# Make sure Cytoscape RESTful API App is running!\n",
"cytoscape_version = requests.get(BASE_URL + 'version')\n",
"print(\"Cytoscape RESTful API module is ready: version \" + json.loads(cytoscape_version.text)[\"cytoscapeVersion\"])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Cytoscape RESTful API module is ready: version 3.1.0\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load a KGML pathway data file from KEGG REST API"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pathway_location = \"http://rest.kegg.jp/get/ath00020/kgml\"\n",
"res1 = requests.post(BASE_URL + \"networks?source=url\", data=json.dumps([pathway_location]), headers=HEADERS)\n",
"result = json.loads(res1.content)\n",
"pathway_suid = result[0][\"networkSUID\"][0]\n",
"print(\"Pathway SUID = \" + str(pathway_suid))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Pathway SUID = 52\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load table data file as Pandas DataFrame"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"profile_csv = \"https://raw.githubusercontent.com/idekerlab/KEGGscape/develop/wiki/data/light-dark-20.csv\"\n",
"profile_df = pd.read_csv(profile_csv)\n",
"profile_df.head()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Metabolite</th>\n",
" <th>KEGG</th>\n",
" <th>ld20t14</th>\n",
" <th>ld20t16</th>\n",
" <th>ld20t20</th>\n",
" <th>ld20t24</th>\n",
" <th>ld20t28</th>\n",
" <th>ld20t32</th>\n",
" <th>ld20t36</th>\n",
" <th>ld20t40</th>\n",
" <th>ld20t44</th>\n",
" <th>ld20t48</th>\n",
" <th>ld20t52</th>\n",
" <th>ld20t56</th>\n",
" <th>ld20t60</th>\n",
" <th>ld20t64</th>\n",
" <th>ld20t68</th>\n",
" <th>ld20t72</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td> Alanine</td>\n",
" <td> C00041</td>\n",
" <td>-0.051007</td>\n",
" <td> 0.040631</td>\n",
" <td>-0.165335</td>\n",
" <td>-0.126382</td>\n",
" <td>-0.093879</td>\n",
" <td>-0.102625</td>\n",
" <td> 0.009509</td>\n",
" <td> 0.051824</td>\n",
" <td>-0.137530</td>\n",
" <td>-0.140400</td>\n",
" <td>-0.116827</td>\n",
" <td>-0.060302</td>\n",
" <td> 0.027001</td>\n",
" <td> 0.081365</td>\n",
" <td>-0.060153</td>\n",
" <td>-0.097988</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td> Valine</td>\n",
" <td> C00183</td>\n",
" <td>-0.025403</td>\n",
" <td>-0.003585</td>\n",
" <td>-0.058986</td>\n",
" <td>-0.069280</td>\n",
" <td>-0.129784</td>\n",
" <td>-0.092210</td>\n",
" <td> 0.009129</td>\n",
" <td>-0.068003</td>\n",
" <td>-0.105905</td>\n",
" <td>-0.097984</td>\n",
" <td>-0.095181</td>\n",
" <td>-0.021911</td>\n",
" <td>-0.021224</td>\n",
" <td>-0.028314</td>\n",
" <td>-0.032834</td>\n",
" <td>-0.025435</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td> Leucine</td>\n",
" <td> C00123</td>\n",
" <td>-0.005918</td>\n",
" <td> 0.058287</td>\n",
" <td>-0.049900</td>\n",
" <td>-0.075853</td>\n",
" <td>-0.098339</td>\n",
" <td>-0.003847</td>\n",
" <td> 0.056037</td>\n",
" <td>-0.104399</td>\n",
" <td>-0.122298</td>\n",
" <td>-0.098051</td>\n",
" <td>-0.082529</td>\n",
" <td> 0.027760</td>\n",
" <td>-0.006289</td>\n",
" <td>-0.048610</td>\n",
" <td>-0.042620</td>\n",
" <td>-0.000897</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td> Isoleucine</td>\n",
" <td> C00407</td>\n",
" <td> 0.079870</td>\n",
" <td> 0.101324</td>\n",
" <td>-0.103993</td>\n",
" <td>-0.041534</td>\n",
" <td>-0.020148</td>\n",
" <td> 0.066486</td>\n",
" <td> 0.091006</td>\n",
" <td>-0.027547</td>\n",
" <td>-0.193494</td>\n",
" <td>-0.144676</td>\n",
" <td>-0.015375</td>\n",
" <td> 0.070540</td>\n",
" <td> 0.118164</td>\n",
" <td> 0.013795</td>\n",
" <td>-0.060418</td>\n",
" <td>-0.042314</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td> Glycine</td>\n",
" <td> C00037</td>\n",
" <td>-0.056155</td>\n",
" <td>-0.185757</td>\n",
" <td>-0.857195</td>\n",
" <td>-0.725952</td>\n",
" <td>-0.051028</td>\n",
" <td> 0.069038</td>\n",
" <td> 0.042867</td>\n",
" <td> 0.098185</td>\n",
" <td>-0.445921</td>\n",
" <td>-0.893788</td>\n",
" <td>-0.201299</td>\n",
" <td> 0.000000</td>\n",
" <td>-0.064322</td>\n",
" <td>-0.090597</td>\n",
" <td>-0.637518</td>\n",
" <td>-0.874438</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
" Metabolite KEGG ld20t14 ld20t16 ld20t20 ld20t24 ld20t28 \\\n",
"0 Alanine C00041 -0.051007 0.040631 -0.165335 -0.126382 -0.093879 \n",
"1 Valine C00183 -0.025403 -0.003585 -0.058986 -0.069280 -0.129784 \n",
"2 Leucine C00123 -0.005918 0.058287 -0.049900 -0.075853 -0.098339 \n",
"3 Isoleucine C00407 0.079870 0.101324 -0.103993 -0.041534 -0.020148 \n",
"4 Glycine C00037 -0.056155 -0.185757 -0.857195 -0.725952 -0.051028 \n",
"\n",
" ld20t32 ld20t36 ld20t40 ld20t44 ld20t48 ld20t52 ld20t56 \\\n",
"0 -0.102625 0.009509 0.051824 -0.137530 -0.140400 -0.116827 -0.060302 \n",
"1 -0.092210 0.009129 -0.068003 -0.105905 -0.097984 -0.095181 -0.021911 \n",
"2 -0.003847 0.056037 -0.104399 -0.122298 -0.098051 -0.082529 0.027760 \n",
"3 0.066486 0.091006 -0.027547 -0.193494 -0.144676 -0.015375 0.070540 \n",
"4 0.069038 0.042867 0.098185 -0.445921 -0.893788 -0.201299 0.000000 \n",
"\n",
" ld20t60 ld20t64 ld20t68 ld20t72 \n",
"0 0.027001 0.081365 -0.060153 -0.097988 \n",
"1 -0.021224 -0.028314 -0.032834 -0.025435 \n",
"2 -0.006289 -0.048610 -0.042620 -0.000897 \n",
"3 0.118164 0.013795 -0.060418 -0.042314 \n",
"4 -0.064322 -0.090597 -0.637518 -0.874438 "
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert the DataFrame to JSON and send it to Cytoscape"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"profile = json.loads(profile_df.to_json(orient=\"records\"))\n",
"\n",
"new_table_data = {\n",
" \"key\": \"KEGG_NODE_LABEL\",\n",
" \"dataKey\": \"KEGG\",\n",
" \"data\": profile\n",
"}\n",
"\n",
"update_table_url = BASE_URL + \"networks/\" + str(pathway_suid) + \"/tables/defaultnode\"\n",
"requests.put(update_table_url, data=json.dumps(new_table_data), headers=HEADERS)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"<Response [204]>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create new column for chart mapping"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set values to the chart column"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"chart_entry = 'barchart: attributelist=\"ld20t14,ld20t16,ld20t20,ld20t24,ld20t28,ld20t32,ld20t36,ld20t40,ld20t44,ld20t48,ld20t52,ld20t56,ld20t60,ld20t64,ld20t68,ld20t72\" colorlist=\"up:red,zero:red,down:red\" showlabels=\"false\"'\n",
"\n",
"target_row_url = BASE_URL + \"networks/\" + str(pathway_suid) + \"/tables/defaultnode/columns/KEGG\"\n",
"res2 = requests.get(target_row_url)\n",
"matched = json.loads(res2.content)[\"values\"]\n",
"\n",
"df2 = pd.DataFrame(columns=[\"id\", \"chart\"]);\n",
"df2[\"id\"] = matched\n",
"df2[\"chart\"] = chart_entry\n",
"\n",
"data = json.loads(df2.to_json(orient=\"records\"))\n",
" \n",
"chart_data = {\n",
" \"key\": \"KEGG\",\n",
" \"dataKey\": \"id\",\n",
" \"data\": data\n",
"}\n",
"\n",
"requests.put(update_table_url, data=json.dumps(chart_data), headers=HEADERS)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"<Response [204]>"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create Visual Style for Custom Mapping"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"custom_graphics_mapping = {\n",
" \"mappingType\" : \"passthrough\",\n",
" \"mappingColumn\" : \"chart\",\n",
" \"mappingColumnType\" : \"String\",\n",
" \"visualProperty\" : \"NODE_CUSTOMGRAPHICS_1\"\n",
"}\n",
"\n",
"style_url = BASE_URL + \"styles/KEGG Style/mappings\"\n",
"requests.post(style_url, data=json.dumps([custom_graphics_mapping]), headers=HEADERS)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"<Response [204]>"
]
}
],
"prompt_number": 6
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment