#!/usr/bin/env python # coding: utf-8 # # Visualizing time series metabolome profile # # by Kozo Nishida (Riken, Japan) # # # ## Software Requirments # # Please install the following software packages to run this workflow: # # * [KEGGscape](http://apps.cytoscape.org/apps/keggscape) # * [enhancedGraphics](http://apps.cytoscape.org/apps/enhancedgraphics) # # # ### Background # This is a sample workflow to automate complex Cytoscape data integaration/visualization process. Please read the following document for more background: # # * https://github.com/idekerlab/KEGGscape/wiki/How-to-visualize-time-series-metabolome-profile # # In[10]: import json import requests import pandas as pd PORT_NUMBER = 1234 BASE_URL = "http://localhost:" + str(PORT_NUMBER) + "/v1/" HEADERS = {'Content-Type': 'application/json'} # ## Load a KGML pathway data file from KEGG REST API # In[11]: pathway_location = "http://rest.kegg.jp/get/ath00020/kgml" res1 = requests.post(BASE_URL + "networks?source=url", data=json.dumps([pathway_location]), headers=HEADERS) result = json.loads(res1.content) pathway_suid = result[0]["networkSUID"][0] print("Pathway SUID = " + str(pathway_suid)) # ## Load table data file as Pandas DataFrame # In[12]: profile_csv = "https://raw.githubusercontent.com/idekerlab/KEGGscape/develop/wiki/data/light-dark-20.csv" profile_df = pd.read_csv(profile_csv) profile_df.head() # ## Convert the DataFrame to JSON and send it to Cytoscape # In[13]: profile = json.loads(profile_df.to_json(orient="records")) # print(json.dumps(profile, indent=4)) # In[14]: new_table_data = { "key": "KEGG_NODE_LABEL", "dataKey": "KEGG", "data": profile } update_table_url = BASE_URL + "networks/" + str(pathway_suid) + "/tables/defaultnode" requests.put(update_table_url, data=json.dumps(new_table_data), headers=HEADERS) # ## Set values to the chart column # In[15]: 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"' target_row_url = BASE_URL + "networks/" + str(pathway_suid) + "/tables/defaultnode/columns/KEGG" res2 = requests.get(target_row_url) matched = json.loads(res2.content)["values"] df2 = pd.DataFrame(columns=["id", "chart"]); df2["id"] = matched df2["chart"] = chart_entry data = json.loads(df2.to_json(orient="records")) chart_data = { "key": "KEGG", "dataKey": "id", "data": data } requests.put(update_table_url, data=json.dumps(chart_data), headers=HEADERS) # ## Create Visual Style for Custom Mapping # In[16]: custom_graphics_mapping = { "mappingType" : "passthrough", "mappingColumn" : "chart", "mappingColumnType" : "String", "visualProperty" : "NODE_CUSTOMGRAPHICS_1" } style_url = BASE_URL + "styles/KEGG Style/mappings" requests.post(style_url, data=json.dumps([custom_graphics_mapping]), headers=HEADERS) # Uncheck the **Fit Custom Graphics to node** box, and bundle edges from menubar. # ![](https://raw.githubusercontent.com/idekerlab/cy-rest-python/develop/advanced/uncheck-checkbox.png) # ![](https://raw.githubusercontent.com/idekerlab/cy-rest-python/develop/advanced/bundle-edges.png) # # You will see # ![](https://raw.githubusercontent.com/idekerlab/cy-rest-python/develop/advanced/metabolome-profile.png)