{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualizing time series metabolome profile\n", "\n", "by Kozo Nishida (Riken, Japan)\n", "\n", "\n", "## Software Requirments\n", "\n", "Please install the following software packages to run this workflow:\n", "\n", "* [KEGGscape](http://apps.cytoscape.org/apps/keggscape)\n", "* [enhancedGraphics](http://apps.cytoscape.org/apps/enhancedgraphics)\n", "\n", "\n", "### Background\n", "This is a sample workflow to automate complex Cytoscape data integaration/visualization process. Please read the following document for more background: \n", "\n", "* https://github.com/idekerlab/KEGGscape/wiki/How-to-visualize-time-series-metabolome-profile\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import json\n", "import requests\n", "import pandas as pd\n", "\n", "PORT_NUMBER = 1234\n", "BASE_URL = \"http://localhost:\" + str(PORT_NUMBER) + \"/v1/\"\n", "HEADERS = {'Content-Type': 'application/json'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load a KGML pathway data file from KEGG REST API" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pathway SUID = 71718\n" ] } ], "source": [ "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", "\n", "print(\"Pathway SUID = \" + str(pathway_suid))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load table data file as Pandas DataFrame" ] }, { "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", "
MetaboliteKEGGld20t14ld20t16ld20t20ld20t24ld20t28ld20t32ld20t36ld20t40ld20t44ld20t48ld20t52ld20t56ld20t60ld20t64ld20t68ld20t72
0AlanineC00041-0.0510070.040631-0.165335-0.126382-0.093879-0.1026250.0095090.051824-0.137530-0.140400-0.116827-0.0603020.0270010.081365-0.060153-0.097988
1ValineC00183-0.025403-0.003585-0.058986-0.069280-0.129784-0.0922100.009129-0.068003-0.105905-0.097984-0.095181-0.021911-0.021224-0.028314-0.032834-0.025435
2LeucineC00123-0.0059180.058287-0.049900-0.075853-0.098339-0.0038470.056037-0.104399-0.122298-0.098051-0.0825290.027760-0.006289-0.048610-0.042620-0.000897
3IsoleucineC004070.0798700.101324-0.103993-0.041534-0.0201480.0664860.091006-0.027547-0.193494-0.144676-0.0153750.0705400.1181640.013795-0.060418-0.042314
4GlycineC00037-0.056155-0.185757-0.857195-0.725952-0.0510280.0690380.0428670.098185-0.445921-0.893788-0.2012990.000000-0.064322-0.090597-0.637518-0.874438
\n", "
" ], "text/plain": [ " 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 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert the DataFrame to JSON and send it to Cytoscape" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "profile = json.loads(profile_df.to_json(orient=\"records\"))\n", "# print(json.dumps(profile, indent=4))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set values to the chart column" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Visual Style for Custom Mapping" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uncheck the **Fit Custom Graphics to node** box, and bundle edges from menubar.\n", "![](https://raw.githubusercontent.com/idekerlab/cy-rest-python/develop/advanced/uncheck-checkbox.png)\n", "![](https://raw.githubusercontent.com/idekerlab/cy-rest-python/develop/advanced/bundle-edges.png)\n", "\n", "You will see\n", "![](https://raw.githubusercontent.com/idekerlab/cy-rest-python/develop/advanced/metabolome-profile.png)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }