Repository 'plot_force'
hg clone https://toolshed.g2.bx.psu.edu/repos/bornea/plot_force

Changeset 0:4324d3f925fe (2017-10-18)
Next changeset 1:70c42d02d54e (2017-10-18)
Commit message:
Uploaded
added:
plotForce.py
b
diff -r 000000000000 -r 4324d3f925fe plotForce.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plotForce.py Wed Oct 18 15:34:12 2017 -0400
[
b'@@ -0,0 +1,409 @@\n+import sys\n+def readTab(infile): # read in txt file\n+    with open(infile, \'r\') as input_file:\n+    # read in tab-delim text\n+        output = []\n+        for input_line in input_file:\n+            input_line = input_line.strip()\n+            temp = input_line.split(\'\\t\')\n+            output.append(temp)\n+    return output\n+def network2JSON(nodes_att_file, edge_file):\n+\tnodes = readTab(nodes_att_file)\n+\tedges = readTab(edge_file)\n+\n+\tstart = """{"nodes":  ["""\n+  \tnode_numbers = {}\n+  \tcnt=0\n+  \tfor i in nodes[1:]:\n+  \t\tnode_numbers[i[0]] = cnt\n+  \t\tcnt+=1\n+  \t\tstart = start + \'{"id":\'+\'"\'+i[0]+\'", "size":\'+str(float(i[2])*1)+\', "score":\'+i[1]+\',"type":"circle"},\'\n+  \tstart = start[:-1]\n+  \tstart = start+"""], "links":  ["""\n+  \tfor i in edges:\n+  \t\t\tstart = start + """{"source":"""+str(node_numbers[i[0]])+""","target":"""+str(node_numbers[i[1]])+"""},"""\n+  \tstart = start[:-1]\n+  \tstart = start + """]}"""\n+  \treturn start\n+\n+\n+output = """<!DOCTYPE html>\n+<meta charset="utf-8">\n+<style>\n+body {\n+  overflow:hidden;\n+   margin:0;\n+}\n+\n+text {\n+  font-family: sans-serif;\n+  pointer-events: none;\n+}\n+\n+</style>\n+<body>\n+<script src="http://d3js.org/d3.v3.min.js"></script>\n+<script>\n+jsondata=\'"""\n+\n+output = output + network2JSON(sys.argv[1],sys.argv[2]) + "\'\\n"\n+\n+output = output + """var w = window.innerWidth;\n+var h = window.innerHeight;\n+\n+var keyc = true, keys = true, keyt = true, keyr = true, keyx = true, keyd = true, keyl = true, keym = true, keyh = true, key1 = true, key2 = true, key3 = true, key0 = true\n+\n+var focus_node = null, highlight_node = null;\n+\n+var text_center = false;\n+var outline = false;\n+\n+var min_score = 0;\n+var max_score = 1;\n+\n+var color = d3.scale.category20()\n+  .range(["green", "orange", "red","black","steelblue","purple","gray","blue","yellow","lime"]);\n+\n+var highlight_color = "red";\n+var highlight_trans = 0.1;\n+\n+var size = d3.scale.pow().exponent(1)\n+  .domain([1,100])\n+  .range([8,24]);\n+\n+var force = d3.layout.force()\n+  .linkDistance(60)\n+  .charge(-500)\n+  .size([w,h]);\n+\n+var default_node_color = "#000";\n+//var default_node_color = "rgb(3,190,100)";\n+var default_link_color = "#888";\n+var nominal_base_node_size = 8;\n+var nominal_text_size = 10;\n+var max_text_size = 24;\n+var nominal_stroke = 1.5;\n+var max_stroke = 4.5;\n+var max_base_node_size = 36;\n+var min_zoom = 0.1;\n+var max_zoom = 7;\n+var svg = d3.select("body").append("svg");\n+var zoom = d3.behavior.zoom().scaleExtent([min_zoom,max_zoom])\n+var g = svg.append("g");\n+svg.style("cursor","move");\n+\n+var data = JSON.parse(jsondata);\n+function plot_graph(graph) {\n+\n+var linkedByIndex = {};\n+    graph.links.forEach(function(d) {\n+\tlinkedByIndex[d.source + "," + d.target] = true;\n+    });\n+\n+\tfunction isConnected(a, b) {\n+        return linkedByIndex[a.index + "," + b.index] || linkedByIndex[b.index + "," + a.index] || a.index == b.index;\n+    }\n+\n+\tfunction hasConnections(a) {\n+\t\tfor (var property in linkedByIndex) {\n+\t\t\t\ts = property.split(",");\n+\t\t\t\tif ((s[0] == a.index || s[1] == a.index) && linkedByIndex[property]) \t\t\t\t\treturn true;\n+\t\t}\n+\treturn false;\n+\t}\n+\n+  force\n+    .nodes(graph.nodes)\n+    .links(graph.links)\n+    .start();\n+\n+  var link = g.selectAll(".link")\n+    .data(graph.links)\n+    .enter().append("line")\n+    .attr("class", "link")\n+\t.style("stroke-width",nominal_stroke)\n+\t.style("stroke", function(d) {\n+\tif (isNumber(d.score) && d.score>=0) return color(d.score);\n+\telse return default_link_color; })\n+\n+\n+  var node = g.selectAll(".node")\n+    .data(graph.nodes)\n+    .enter().append("g")\n+    .attr("class", "node")\n+\n+    .call(force.drag)\n+\n+\n+\tnode.on("dblclick.zoom", function(d) { d3.event.stopPropagation();\n+\tvar dcx = (window.innerWidth/2-d.x*zoom.scale());\n+\tvar dcy = (window.innerHeight/2-d.y*zoom.scale());\n+\tzoom.translate([dcx,dcy]);\n+\t g.attr("transform", "translate("+ dcx + "," + dcy  + ")scale(" + zoom.scale() + ")");\n+\n+\n+\t});\n+\n+\n+\n+\n+\tvar tocolor = "fill";\n+\tvar towhite = "stroke";\n+\tif (outl'..b'})\n+        .type(function(d) { return d.type; }))\n+\n+\t//circle.attr("r", function(d) { return (size(d.size)*base_radius/nominal_base_node_size||base_radius); })\n+\tif (!text_center) text.attr("dx", function(d) { return (size(d.size)*base_radius/nominal_base_node_size||base_radius); });\n+\n+\tvar text_size = nominal_text_size;\n+    if (nominal_text_size*zoom.scale()>max_text_size) text_size = max_text_size/zoom.scale();\n+    text.style("font-size",text_size + "px");\n+\n+\tg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");\n+\t});\n+\n+  svg.call(zoom);\n+\n+  resize();\n+  //window.focus();\n+  d3.select(window).on("resize", resize).on("keydown", keydown);\n+\n+  force.on("tick", function() {\n+\n+    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });\n+    text.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });\n+\n+    link.attr("x1", function(d) { return d.source.x; })\n+      .attr("y1", function(d) { return d.source.y; })\n+      .attr("x2", function(d) { return d.target.x; })\n+      .attr("y2", function(d) { return d.target.y; });\n+\n+    node.attr("cx", function(d) { return d.x; })\n+      .attr("cy", function(d) { return d.y; });\n+\t});\n+\n+  function resize() {\n+    var width = window.innerWidth, height = window.innerHeight;\n+\tsvg.attr("width", width).attr("height", height);\n+\n+\tforce.size([force.size()[0]+(width-w)/zoom.scale(),force.size()[1]+(height-h)/zoom.scale()]).resume();\n+    w = width;\n+\th = height;\n+\t}\n+\n+\tfunction keydown() {\n+\tif (d3.event.keyCode==32) {  force.stop();}\n+\telse if (d3.event.keyCode>=48 && d3.event.keyCode<=90 && !d3.event.ctrlKey && !d3.event.altKey && !d3.event.metaKey)\n+\t{\n+  switch (String.fromCharCode(d3.event.keyCode)) {\n+    case "C": keyc = !keyc; break;\n+    case "S": keys = !keys; break;\n+\tcase "T": keyt = !keyt; break;\n+\tcase "R": keyr = !keyr; break;\n+    case "X": keyx = !keyx; break;\n+\tcase "D": keyd = !keyd; break;\n+\tcase "L": keyl = !keyl; break;\n+\tcase "M": keym = !keym; break;\n+\tcase "H": keyh = !keyh; break;\n+\tcase "1": key1 = !key1; break;\n+\tcase "2": key2 = !key2; break;\n+\tcase "3": key3 = !key3; break;\n+\tcase "0": key0 = !key0; break;\n+  }\n+\n+  link.style("display", function(d) {\n+\t\t\t\tvar flag  = vis_by_type(d.source.type)&&vis_by_type(d.target.type)&&vis_by_node_score(d.source.score)&&vis_by_node_score(d.target.score)&&vis_by_link_score(d.score);\n+\t\t\t\tlinkedByIndex[d.source.index + "," + d.target.index] = flag;\n+              return flag?"inline":"none";});\n+  node.style("display", function(d) {\n+\t\t\t\treturn (key0||hasConnections(d))&&vis_by_type(d.type)&&vis_by_node_score(d.score)?"inline":"none";});\n+  text.style("display", function(d) {\n+                return (key0||hasConnections(d))&&vis_by_type(d.type)&&vis_by_node_score(d.score)?"inline":"none";});\n+\n+\t\t\t\tif (highlight_node !== null)\n+\t\t\t\t{\n+\t\t\t\t\tif ((key0||hasConnections(highlight_node))&&vis_by_type(highlight_node.type)&&vis_by_node_score(highlight_node.score)) {\n+\t\t\t\t\tif (focus_node!==null) set_focus(focus_node);\n+\t\t\t\t\tset_highlight(highlight_node);\n+\t\t\t\t\t}\n+\t\t\t\t\telse {exit_highlight();}\n+\t\t\t\t}\n+\n+}\n+}\n+\n+};\n+\n+function vis_by_type(type)\n+{\n+\tswitch (type) {\n+\t  case "circle": return keyc;\n+\t  case "circle": return keys;\n+\t  case "triangle-up": return keyt;\n+\t  case "diamond": return keyr;\n+\t  case "cross": return keyx;\n+\t  case "triangle-down": return keyd;\n+\t  default: return true;\n+}\n+}\n+function vis_by_node_score(score)\n+{\n+\tif (isNumber(score))\n+\t{\n+\tif (score>=0.666) return keyh;\n+\telse if (score>=0.333) return keym;\n+\telse if (score>=0) return keyl;\n+\t}\n+\treturn true;\n+}\n+\n+function vis_by_link_score(score)\n+{\n+\tif (isNumber(score))\n+\t{\n+\tif (score>=0.666) return key3;\n+\telse if (score>=0.333) return key2;\n+\telse if (score>=0) return key1;\n+}\n+\treturn true;\n+}\n+\n+function isNumber(n) {\n+  return !isNaN(parseFloat(n)) && isFinite(n);\n+}\n+plot_graph(data)\n+\n+</script>"""\n+\n+with open(sys.argv[3],"w") as x:\n+\tx.write(output)\n'