Skip to content

Instantly share code, notes, and snippets.

@keiono
Created February 23, 2016 21:14
Show Gist options
  • Save keiono/9f55fd46660d2be20797 to your computer and use it in GitHub Desktop.
Save keiono/9f55fd46660d2be20797 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import json\n",
"import matplotlib.pyplot as plt\n",
"\n",
"JSON_EXT = '.json'\n",
"CYTOSCAPE_JS_EXT = '.cyjs'\n",
"\n",
"Blues = plt.get_cmap('Blues')\n",
"\n",
"def find_min_max(nodes):\n",
" \n",
" scores = []\n",
" \n",
" for node in nodes:\n",
" if \"score\" in node[\"data\"]:\n",
" scores.append(node[\"data\"][\"score\"])\n",
" \n",
" min_score = min(scores)\n",
" max_score = max(scores)\n",
" \n",
" return (min_score, max_score)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def convert2sigma(in_file_name, out_file_name):\n",
" print('Source file: ' + in_file_name)\n",
"\n",
" # Load Original Cyjs file\n",
" cyjs_file = open(in_file_name)\n",
" source_json = json.load(cyjs_file)\n",
" cyjs_file.close()\n",
" \n",
" \n",
"\n",
" # Output files\n",
" out = open(out_file_name, \"w\")\n",
"\n",
" nodeMap = {}\n",
"\n",
" sourceNodes = source_json[\"elements\"][\"nodes\"]\n",
" sourceEdges = source_json[\"elements\"][\"edges\"]\n",
"\n",
" min_max = find_min_max(sourceNodes)\n",
" score_range = min_max[1] - min_max[0]\n",
" \n",
" nodeList = []\n",
" edgeList = []\n",
"\n",
" for node in sourceNodes:\n",
" termID = node[\"data\"][\"Manual_Name\"]\n",
" x = node[\"position\"][\"x\"]\n",
" y = node[\"position\"][\"y\"]\n",
" suid = node[\"data\"][\"SUID\"]\n",
" if \"Size\" in node[\"data\"]:\n",
" size = node[\"data\"][\"Size\"] * 2\n",
" else:\n",
" size = 6.0\n",
" \n",
" if \"score\" in node[\"data\"]:\n",
" ratio = (node[\"data\"][\"score\"] - min_max[0]) / score_range\n",
" col1 = Blues(ratio)\n",
" color = (\n",
" int(col1[0] * 255),\n",
" int(col1[1] * 255),\n",
" int(col1[2]* 255)\n",
" )\n",
" print(str(color))\n",
" else:\n",
" color = (200,200,200)\n",
" \n",
" \n",
" \n",
" newNode = {\n",
" 'id':termID, \n",
" 'label':termID,\n",
" 'size':size,\n",
" 'x':x, 'y':y, \n",
" 'color':\"rgb(58,150,43)\"\n",
" }\n",
" \n",
" nodeMap[str(suid)] = termID\n",
" nodeList.append(newNode)\n",
"\n",
" for edge in sourceEdges:\n",
" source = nodeMap[edge['data']['source']]\n",
" target = nodeMap[edge['data']['target']]\n",
" interaction = edge['data']['interaction']\n",
" edgeList.append({'source':source, 'target':target, 'relationship':interaction, 'weight':1.0})\n",
"\n",
" graph = {}\n",
"\n",
" graph[\"nodes\"] = nodeList\n",
" graph[\"edges\"] = edgeList\n",
"\n",
" out.write(json.dumps(graph, sort_keys=True, indent=4))\n",
" out.close()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Source file: /Users/kono/git/nexo-data-prep/atgo.cyjs\n",
"(115, 178, 215)\n",
"(8, 54, 116)\n",
"(8, 52, 113)\n",
"(8, 52, 113)\n",
"(8, 48, 107)\n",
"(11, 85, 159)\n",
"(10, 84, 158)\n",
"(18, 94, 166)\n",
"(41, 121, 185)\n",
"(12, 86, 160)\n",
"(8, 81, 156)\n",
"(8, 54, 116)\n",
"(14, 89, 162)\n",
"(236, 243, 251)\n",
"(8, 64, 131)\n",
"(8, 82, 156)\n",
"(8, 80, 154)\n",
"(8, 53, 114)\n",
"(8, 76, 150)\n",
"(247, 251, 255)\n",
"(8, 70, 140)\n",
"(8, 71, 142)\n",
"(12, 86, 160)\n",
"(9, 83, 157)\n",
"(199, 219, 239)\n",
"(8, 48, 107)\n",
"(8, 69, 139)\n",
"(8, 53, 114)\n",
"(9, 83, 157)\n",
"(96, 166, 209)\n",
"(8, 49, 108)\n",
"(8, 69, 139)\n",
"(8, 75, 148)\n",
"(8, 54, 116)\n",
"(8, 52, 113)\n",
"(9, 83, 157)\n",
"(8, 76, 150)\n",
"(26, 105, 174)\n",
"(12, 86, 160)\n",
"(202, 221, 240)\n",
"(8, 70, 140)\n",
"(12, 86, 160)\n",
"(12, 86, 160)\n",
"(52, 132, 191)\n",
"(12, 87, 160)\n",
"(8, 80, 154)\n",
"(8, 70, 140)\n",
"(8, 79, 153)\n",
"(29, 108, 177)\n",
"(8, 53, 114)\n",
"(185, 213, 234)\n",
"(198, 219, 239)\n",
"(12, 86, 160)\n",
"(11, 85, 159)\n",
"(8, 54, 116)\n",
"(40, 120, 184)\n",
"(8, 80, 154)\n",
"(8, 69, 139)\n",
"(244, 249, 254)\n",
"(12, 86, 160)\n",
"(8, 81, 156)\n",
"(10, 84, 158)\n",
"(8, 58, 122)\n",
"(172, 208, 230)\n",
"(8, 62, 128)\n",
"(8, 69, 139)\n",
"(8, 72, 143)\n",
"(8, 71, 142)\n",
"(8, 79, 153)\n",
"(15, 91, 163)\n",
"(8, 69, 139)\n",
"(9, 83, 157)\n",
"(8, 67, 136)\n",
"(12, 87, 160)\n",
"(11, 85, 159)\n",
"(8, 49, 108)\n",
"(239, 246, 252)\n",
"(12, 86, 160)\n",
"(8, 60, 125)\n",
"(13, 88, 161)\n",
"(8, 55, 117)\n",
"(12, 86, 160)\n",
"(8, 79, 153)\n",
"(13, 88, 161)\n",
"(230, 240, 249)\n",
"(14, 89, 162)\n",
"(9, 83, 157)\n",
"(8, 80, 154)\n",
"(8, 68, 137)\n",
"(8, 55, 117)\n",
"(11, 85, 159)\n",
"(201, 221, 240)\n",
"(10, 84, 158)\n",
"(10, 84, 158)\n",
"(14, 89, 162)\n",
"(8, 76, 150)\n",
"(9, 83, 157)\n",
"(9, 83, 157)\n",
"(78, 154, 202)\n",
"(12, 87, 160)\n",
"(8, 79, 153)\n",
"(130, 186, 219)\n",
"(8, 49, 108)\n",
"(8, 73, 145)\n",
"(8, 79, 153)\n",
"(8, 79, 153)\n",
"(12, 86, 160)\n",
"(16, 92, 164)\n",
"(13, 88, 161)\n",
"(8, 54, 116)\n",
"(12, 86, 160)\n",
"(12, 87, 160)\n",
"(8, 51, 111)\n",
"(8, 76, 150)\n",
"(10, 84, 158)\n",
"(12, 86, 160)\n",
"(12, 86, 160)\n",
"(8, 48, 107)\n",
"(8, 54, 116)\n",
"(12, 87, 160)\n",
"(8, 60, 125)\n",
"(10, 84, 158)\n",
"(8, 54, 116)\n",
"(12, 86, 160)\n",
"(11, 85, 159)\n",
"(11, 85, 159)\n",
"(8, 79, 153)\n",
"(10, 84, 158)\n",
"(11, 85, 159)\n",
"(8, 52, 113)\n",
"(9, 83, 157)\n",
"(8, 71, 142)\n",
"(8, 80, 154)\n",
"(8, 79, 153)\n",
"(123, 183, 217)\n",
"(12, 87, 160)\n",
"(11, 85, 159)\n",
"(11, 85, 159)\n",
"(13, 88, 161)\n",
"(8, 56, 119)\n",
"(12, 86, 160)\n",
"(8, 55, 117)\n",
"(11, 85, 159)\n",
"(12, 87, 160)\n",
"(8, 79, 153)\n",
"(9, 83, 157)\n",
"(8, 82, 156)\n",
"(8, 55, 117)\n",
"(12, 87, 160)\n",
"(8, 79, 153)\n",
"(200, 220, 239)\n",
"(8, 79, 153)\n",
"(8, 79, 153)\n",
"(238, 245, 252)\n",
"(12, 87, 160)\n",
"(8, 69, 139)\n",
"(193, 217, 237)\n",
"(85, 159, 205)\n",
"(10, 84, 158)\n",
"(8, 82, 156)\n",
"(8, 60, 125)\n",
"(8, 71, 142)\n",
"(13, 88, 161)\n",
"(8, 80, 154)\n",
"(8, 66, 134)\n",
"(10, 84, 158)\n",
"(8, 48, 107)\n",
"(8, 79, 153)\n",
"(8, 73, 145)\n",
"(9, 83, 157)\n",
"(8, 49, 108)\n",
"(138, 191, 220)\n",
"(8, 54, 116)\n",
"(8, 54, 116)\n",
"(13, 88, 161)\n",
"(8, 61, 126)\n",
"(146, 195, 222)\n",
"(8, 54, 116)\n",
"(8, 59, 123)\n",
"(101, 170, 211)\n",
"(11, 85, 159)\n",
"(8, 79, 153)\n",
"(224, 236, 247)\n",
"(12, 86, 160)\n",
"(8, 79, 153)\n",
"(15, 90, 163)\n",
"(8, 60, 125)\n"
]
}
],
"source": [
"# Test run\n",
"convert2sigma('/Users/kono/git/nexo-data-prep/atgo.cyjs', 'atgo.json')"
]
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment