annotate plotForce.py @ 1:70c42d02d54e draft default tip

Uploaded
author bornea
date Wed, 18 Oct 2017 15:34:23 -0400
parents 4324d3f925fe
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4324d3f925fe Uploaded
bornea
parents:
diff changeset
1 import sys
4324d3f925fe Uploaded
bornea
parents:
diff changeset
2 def readTab(infile): # read in txt file
4324d3f925fe Uploaded
bornea
parents:
diff changeset
3 with open(infile, 'r') as input_file:
4324d3f925fe Uploaded
bornea
parents:
diff changeset
4 # read in tab-delim text
4324d3f925fe Uploaded
bornea
parents:
diff changeset
5 output = []
4324d3f925fe Uploaded
bornea
parents:
diff changeset
6 for input_line in input_file:
4324d3f925fe Uploaded
bornea
parents:
diff changeset
7 input_line = input_line.strip()
4324d3f925fe Uploaded
bornea
parents:
diff changeset
8 temp = input_line.split('\t')
4324d3f925fe Uploaded
bornea
parents:
diff changeset
9 output.append(temp)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
10 return output
4324d3f925fe Uploaded
bornea
parents:
diff changeset
11 def network2JSON(nodes_att_file, edge_file):
4324d3f925fe Uploaded
bornea
parents:
diff changeset
12 nodes = readTab(nodes_att_file)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
13 edges = readTab(edge_file)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
14
4324d3f925fe Uploaded
bornea
parents:
diff changeset
15 start = """{"nodes": ["""
4324d3f925fe Uploaded
bornea
parents:
diff changeset
16 node_numbers = {}
4324d3f925fe Uploaded
bornea
parents:
diff changeset
17 cnt=0
4324d3f925fe Uploaded
bornea
parents:
diff changeset
18 for i in nodes[1:]:
4324d3f925fe Uploaded
bornea
parents:
diff changeset
19 node_numbers[i[0]] = cnt
4324d3f925fe Uploaded
bornea
parents:
diff changeset
20 cnt+=1
4324d3f925fe Uploaded
bornea
parents:
diff changeset
21 start = start + '{"id":'+'"'+i[0]+'", "size":'+str(float(i[2])*1)+', "score":'+i[1]+',"type":"circle"},'
4324d3f925fe Uploaded
bornea
parents:
diff changeset
22 start = start[:-1]
4324d3f925fe Uploaded
bornea
parents:
diff changeset
23 start = start+"""], "links": ["""
4324d3f925fe Uploaded
bornea
parents:
diff changeset
24 for i in edges:
4324d3f925fe Uploaded
bornea
parents:
diff changeset
25 start = start + """{"source":"""+str(node_numbers[i[0]])+""","target":"""+str(node_numbers[i[1]])+"""},"""
4324d3f925fe Uploaded
bornea
parents:
diff changeset
26 start = start[:-1]
4324d3f925fe Uploaded
bornea
parents:
diff changeset
27 start = start + """]}"""
4324d3f925fe Uploaded
bornea
parents:
diff changeset
28 return start
4324d3f925fe Uploaded
bornea
parents:
diff changeset
29
4324d3f925fe Uploaded
bornea
parents:
diff changeset
30
4324d3f925fe Uploaded
bornea
parents:
diff changeset
31 output = """<!DOCTYPE html>
4324d3f925fe Uploaded
bornea
parents:
diff changeset
32 <meta charset="utf-8">
4324d3f925fe Uploaded
bornea
parents:
diff changeset
33 <style>
4324d3f925fe Uploaded
bornea
parents:
diff changeset
34 body {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
35 overflow:hidden;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
36 margin:0;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
37 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
38
4324d3f925fe Uploaded
bornea
parents:
diff changeset
39 text {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
40 font-family: sans-serif;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
41 pointer-events: none;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
42 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
43
4324d3f925fe Uploaded
bornea
parents:
diff changeset
44 </style>
4324d3f925fe Uploaded
bornea
parents:
diff changeset
45 <body>
4324d3f925fe Uploaded
bornea
parents:
diff changeset
46 <script src="http://d3js.org/d3.v3.min.js"></script>
4324d3f925fe Uploaded
bornea
parents:
diff changeset
47 <script>
4324d3f925fe Uploaded
bornea
parents:
diff changeset
48 jsondata='"""
4324d3f925fe Uploaded
bornea
parents:
diff changeset
49
4324d3f925fe Uploaded
bornea
parents:
diff changeset
50 output = output + network2JSON(sys.argv[1],sys.argv[2]) + "'\n"
4324d3f925fe Uploaded
bornea
parents:
diff changeset
51
4324d3f925fe Uploaded
bornea
parents:
diff changeset
52 output = output + """var w = window.innerWidth;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
53 var h = window.innerHeight;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
54
4324d3f925fe Uploaded
bornea
parents:
diff changeset
55 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
4324d3f925fe Uploaded
bornea
parents:
diff changeset
56
4324d3f925fe Uploaded
bornea
parents:
diff changeset
57 var focus_node = null, highlight_node = null;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
58
4324d3f925fe Uploaded
bornea
parents:
diff changeset
59 var text_center = false;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
60 var outline = false;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
61
4324d3f925fe Uploaded
bornea
parents:
diff changeset
62 var min_score = 0;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
63 var max_score = 1;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
64
4324d3f925fe Uploaded
bornea
parents:
diff changeset
65 var color = d3.scale.category20()
4324d3f925fe Uploaded
bornea
parents:
diff changeset
66 .range(["green", "orange", "red","black","steelblue","purple","gray","blue","yellow","lime"]);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
67
4324d3f925fe Uploaded
bornea
parents:
diff changeset
68 var highlight_color = "red";
4324d3f925fe Uploaded
bornea
parents:
diff changeset
69 var highlight_trans = 0.1;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
70
4324d3f925fe Uploaded
bornea
parents:
diff changeset
71 var size = d3.scale.pow().exponent(1)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
72 .domain([1,100])
4324d3f925fe Uploaded
bornea
parents:
diff changeset
73 .range([8,24]);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
74
4324d3f925fe Uploaded
bornea
parents:
diff changeset
75 var force = d3.layout.force()
4324d3f925fe Uploaded
bornea
parents:
diff changeset
76 .linkDistance(60)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
77 .charge(-500)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
78 .size([w,h]);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
79
4324d3f925fe Uploaded
bornea
parents:
diff changeset
80 var default_node_color = "#000";
4324d3f925fe Uploaded
bornea
parents:
diff changeset
81 //var default_node_color = "rgb(3,190,100)";
4324d3f925fe Uploaded
bornea
parents:
diff changeset
82 var default_link_color = "#888";
4324d3f925fe Uploaded
bornea
parents:
diff changeset
83 var nominal_base_node_size = 8;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
84 var nominal_text_size = 10;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
85 var max_text_size = 24;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
86 var nominal_stroke = 1.5;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
87 var max_stroke = 4.5;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
88 var max_base_node_size = 36;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
89 var min_zoom = 0.1;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
90 var max_zoom = 7;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
91 var svg = d3.select("body").append("svg");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
92 var zoom = d3.behavior.zoom().scaleExtent([min_zoom,max_zoom])
4324d3f925fe Uploaded
bornea
parents:
diff changeset
93 var g = svg.append("g");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
94 svg.style("cursor","move");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
95
4324d3f925fe Uploaded
bornea
parents:
diff changeset
96 var data = JSON.parse(jsondata);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
97 function plot_graph(graph) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
98
4324d3f925fe Uploaded
bornea
parents:
diff changeset
99 var linkedByIndex = {};
4324d3f925fe Uploaded
bornea
parents:
diff changeset
100 graph.links.forEach(function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
101 linkedByIndex[d.source + "," + d.target] = true;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
102 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
103
4324d3f925fe Uploaded
bornea
parents:
diff changeset
104 function isConnected(a, b) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
105 return linkedByIndex[a.index + "," + b.index] || linkedByIndex[b.index + "," + a.index] || a.index == b.index;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
106 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
107
4324d3f925fe Uploaded
bornea
parents:
diff changeset
108 function hasConnections(a) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
109 for (var property in linkedByIndex) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
110 s = property.split(",");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
111 if ((s[0] == a.index || s[1] == a.index) && linkedByIndex[property]) return true;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
112 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
113 return false;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
114 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
115
4324d3f925fe Uploaded
bornea
parents:
diff changeset
116 force
4324d3f925fe Uploaded
bornea
parents:
diff changeset
117 .nodes(graph.nodes)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
118 .links(graph.links)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
119 .start();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
120
4324d3f925fe Uploaded
bornea
parents:
diff changeset
121 var link = g.selectAll(".link")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
122 .data(graph.links)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
123 .enter().append("line")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
124 .attr("class", "link")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
125 .style("stroke-width",nominal_stroke)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
126 .style("stroke", function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
127 if (isNumber(d.score) && d.score>=0) return color(d.score);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
128 else return default_link_color; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
129
4324d3f925fe Uploaded
bornea
parents:
diff changeset
130
4324d3f925fe Uploaded
bornea
parents:
diff changeset
131 var node = g.selectAll(".node")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
132 .data(graph.nodes)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
133 .enter().append("g")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
134 .attr("class", "node")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
135
4324d3f925fe Uploaded
bornea
parents:
diff changeset
136 .call(force.drag)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
137
4324d3f925fe Uploaded
bornea
parents:
diff changeset
138
4324d3f925fe Uploaded
bornea
parents:
diff changeset
139 node.on("dblclick.zoom", function(d) { d3.event.stopPropagation();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
140 var dcx = (window.innerWidth/2-d.x*zoom.scale());
4324d3f925fe Uploaded
bornea
parents:
diff changeset
141 var dcy = (window.innerHeight/2-d.y*zoom.scale());
4324d3f925fe Uploaded
bornea
parents:
diff changeset
142 zoom.translate([dcx,dcy]);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
143 g.attr("transform", "translate("+ dcx + "," + dcy + ")scale(" + zoom.scale() + ")");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
144
4324d3f925fe Uploaded
bornea
parents:
diff changeset
145
4324d3f925fe Uploaded
bornea
parents:
diff changeset
146 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
147
4324d3f925fe Uploaded
bornea
parents:
diff changeset
148
4324d3f925fe Uploaded
bornea
parents:
diff changeset
149
4324d3f925fe Uploaded
bornea
parents:
diff changeset
150
4324d3f925fe Uploaded
bornea
parents:
diff changeset
151 var tocolor = "fill";
4324d3f925fe Uploaded
bornea
parents:
diff changeset
152 var towhite = "stroke";
4324d3f925fe Uploaded
bornea
parents:
diff changeset
153 if (outline) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
154 tocolor = "stroke"
4324d3f925fe Uploaded
bornea
parents:
diff changeset
155 towhite = "fill"
4324d3f925fe Uploaded
bornea
parents:
diff changeset
156 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
157
4324d3f925fe Uploaded
bornea
parents:
diff changeset
158
4324d3f925fe Uploaded
bornea
parents:
diff changeset
159
4324d3f925fe Uploaded
bornea
parents:
diff changeset
160 var circle = node.append("path")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
161
4324d3f925fe Uploaded
bornea
parents:
diff changeset
162
4324d3f925fe Uploaded
bornea
parents:
diff changeset
163 .attr("d", d3.svg.symbol()
4324d3f925fe Uploaded
bornea
parents:
diff changeset
164 .size(function(d) { return Math.PI*Math.pow(size(d.size)||nominal_base_node_size,2); })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
165 .type(function(d) { return d.type; }))
4324d3f925fe Uploaded
bornea
parents:
diff changeset
166
4324d3f925fe Uploaded
bornea
parents:
diff changeset
167 .style(tocolor, function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
168 if (isNumber(d.score) && d.score>=0) return color(d.score);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
169 else return default_node_color; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
170 //.attr("r", function(d) { return size(d.size)||nominal_base_node_size; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
171 .style("stroke-width", nominal_stroke)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
172 .style(towhite, "white");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
173
4324d3f925fe Uploaded
bornea
parents:
diff changeset
174
4324d3f925fe Uploaded
bornea
parents:
diff changeset
175 var text = g.selectAll(".text")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
176 .data(graph.nodes)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
177 .enter().append("text")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
178 .attr("dy", ".35em")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
179 .style("font-size", nominal_text_size + "px")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
180
4324d3f925fe Uploaded
bornea
parents:
diff changeset
181 if (text_center)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
182 text.text(function(d) { return d.id; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
183 .style("text-anchor", "middle");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
184 else
4324d3f925fe Uploaded
bornea
parents:
diff changeset
185 text.attr("dx", function(d) {return (size(d.size)||nominal_base_node_size);})
4324d3f925fe Uploaded
bornea
parents:
diff changeset
186 .text(function(d) { return '\u2002'+d.id; });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
187
4324d3f925fe Uploaded
bornea
parents:
diff changeset
188 node.on("mouseover", function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
189 set_highlight(d);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
190 })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
191 .on("mousedown", function(d) { d3.event.stopPropagation();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
192 focus_node = d;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
193 set_focus(d)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
194 if (highlight_node === null) set_highlight(d)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
195
4324d3f925fe Uploaded
bornea
parents:
diff changeset
196 } ).on("mouseout", function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
197 exit_highlight();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
198
4324d3f925fe Uploaded
bornea
parents:
diff changeset
199 } );
4324d3f925fe Uploaded
bornea
parents:
diff changeset
200
4324d3f925fe Uploaded
bornea
parents:
diff changeset
201 d3.select(window).on("mouseup",
4324d3f925fe Uploaded
bornea
parents:
diff changeset
202 function() {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
203 if (focus_node!==null)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
204 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
205 focus_node = null;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
206 if (highlight_trans<1)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
207 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
208
4324d3f925fe Uploaded
bornea
parents:
diff changeset
209 circle.style("opacity", 1);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
210 text.style("opacity", 1);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
211 link.style("opacity", 1);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
212 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
213 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
214
4324d3f925fe Uploaded
bornea
parents:
diff changeset
215 if (highlight_node === null) exit_highlight();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
216 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
217
4324d3f925fe Uploaded
bornea
parents:
diff changeset
218 function exit_highlight()
4324d3f925fe Uploaded
bornea
parents:
diff changeset
219 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
220 highlight_node = null;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
221 if (focus_node===null)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
222 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
223 svg.style("cursor","move");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
224 if (highlight_color!="white")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
225 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
226 circle.style(towhite, "white");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
227 text.style("font-weight", "normal");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
228 link.style("stroke", function(o) {return (isNumber(o.score) && o.score>=0)?color(o.score):default_link_color});
4324d3f925fe Uploaded
bornea
parents:
diff changeset
229 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
230
4324d3f925fe Uploaded
bornea
parents:
diff changeset
231 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
232 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
233
4324d3f925fe Uploaded
bornea
parents:
diff changeset
234 function set_focus(d)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
235 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
236 if (highlight_trans<1) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
237 circle.style("opacity", function(o) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
238 return isConnected(d, o) ? 1 : highlight_trans;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
239 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
240
4324d3f925fe Uploaded
bornea
parents:
diff changeset
241 text.style("opacity", function(o) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
242 return isConnected(d, o) ? 1 : highlight_trans;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
243 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
244
4324d3f925fe Uploaded
bornea
parents:
diff changeset
245 link.style("opacity", function(o) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
246 return o.source.index == d.index || o.target.index == d.index ? 1 : highlight_trans;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
247 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
248 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
249 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
250
4324d3f925fe Uploaded
bornea
parents:
diff changeset
251
4324d3f925fe Uploaded
bornea
parents:
diff changeset
252 function set_highlight(d)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
253 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
254 svg.style("cursor","pointer");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
255 if (focus_node!==null) d = focus_node;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
256 highlight_node = d;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
257
4324d3f925fe Uploaded
bornea
parents:
diff changeset
258 if (highlight_color!="white")
4324d3f925fe Uploaded
bornea
parents:
diff changeset
259 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
260 circle.style(towhite, function(o) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
261 return isConnected(d, o) ? highlight_color : "white";});
4324d3f925fe Uploaded
bornea
parents:
diff changeset
262 text.style("font-weight", function(o) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
263 return isConnected(d, o) ? "bold" : "normal";});
4324d3f925fe Uploaded
bornea
parents:
diff changeset
264 link.style("stroke", function(o) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
265 return o.source.index == d.index || o.target.index == d.index ? highlight_color : ((isNumber(o.score) && o.score>=0)?color(o.score):default_link_color);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
266
4324d3f925fe Uploaded
bornea
parents:
diff changeset
267 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
268 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
269 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
270
4324d3f925fe Uploaded
bornea
parents:
diff changeset
271
4324d3f925fe Uploaded
bornea
parents:
diff changeset
272 zoom.on("zoom", function() {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
273
4324d3f925fe Uploaded
bornea
parents:
diff changeset
274 var stroke = nominal_stroke;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
275 if (nominal_stroke*zoom.scale()>max_stroke) stroke = max_stroke/zoom.scale();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
276 link.style("stroke-width",stroke);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
277 circle.style("stroke-width",stroke);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
278
4324d3f925fe Uploaded
bornea
parents:
diff changeset
279 var base_radius = nominal_base_node_size;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
280 if (nominal_base_node_size*zoom.scale()>max_base_node_size) base_radius = max_base_node_size/zoom.scale();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
281 circle.attr("d", d3.svg.symbol()
4324d3f925fe Uploaded
bornea
parents:
diff changeset
282 .size(function(d) { return Math.PI*Math.pow(size(d.size)*base_radius/nominal_base_node_size||base_radius,2); })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
283 .type(function(d) { return d.type; }))
4324d3f925fe Uploaded
bornea
parents:
diff changeset
284
4324d3f925fe Uploaded
bornea
parents:
diff changeset
285 //circle.attr("r", function(d) { return (size(d.size)*base_radius/nominal_base_node_size||base_radius); })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
286 if (!text_center) text.attr("dx", function(d) { return (size(d.size)*base_radius/nominal_base_node_size||base_radius); });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
287
4324d3f925fe Uploaded
bornea
parents:
diff changeset
288 var text_size = nominal_text_size;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
289 if (nominal_text_size*zoom.scale()>max_text_size) text_size = max_text_size/zoom.scale();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
290 text.style("font-size",text_size + "px");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
291
4324d3f925fe Uploaded
bornea
parents:
diff changeset
292 g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
4324d3f925fe Uploaded
bornea
parents:
diff changeset
293 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
294
4324d3f925fe Uploaded
bornea
parents:
diff changeset
295 svg.call(zoom);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
296
4324d3f925fe Uploaded
bornea
parents:
diff changeset
297 resize();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
298 //window.focus();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
299 d3.select(window).on("resize", resize).on("keydown", keydown);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
300
4324d3f925fe Uploaded
bornea
parents:
diff changeset
301 force.on("tick", function() {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
302
4324d3f925fe Uploaded
bornea
parents:
diff changeset
303 node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
304 text.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
305
4324d3f925fe Uploaded
bornea
parents:
diff changeset
306 link.attr("x1", function(d) { return d.source.x; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
307 .attr("y1", function(d) { return d.source.y; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
308 .attr("x2", function(d) { return d.target.x; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
309 .attr("y2", function(d) { return d.target.y; });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
310
4324d3f925fe Uploaded
bornea
parents:
diff changeset
311 node.attr("cx", function(d) { return d.x; })
4324d3f925fe Uploaded
bornea
parents:
diff changeset
312 .attr("cy", function(d) { return d.y; });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
313 });
4324d3f925fe Uploaded
bornea
parents:
diff changeset
314
4324d3f925fe Uploaded
bornea
parents:
diff changeset
315 function resize() {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
316 var width = window.innerWidth, height = window.innerHeight;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
317 svg.attr("width", width).attr("height", height);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
318
4324d3f925fe Uploaded
bornea
parents:
diff changeset
319 force.size([force.size()[0]+(width-w)/zoom.scale(),force.size()[1]+(height-h)/zoom.scale()]).resume();
4324d3f925fe Uploaded
bornea
parents:
diff changeset
320 w = width;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
321 h = height;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
322 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
323
4324d3f925fe Uploaded
bornea
parents:
diff changeset
324 function keydown() {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
325 if (d3.event.keyCode==32) { force.stop();}
4324d3f925fe Uploaded
bornea
parents:
diff changeset
326 else if (d3.event.keyCode>=48 && d3.event.keyCode<=90 && !d3.event.ctrlKey && !d3.event.altKey && !d3.event.metaKey)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
327 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
328 switch (String.fromCharCode(d3.event.keyCode)) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
329 case "C": keyc = !keyc; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
330 case "S": keys = !keys; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
331 case "T": keyt = !keyt; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
332 case "R": keyr = !keyr; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
333 case "X": keyx = !keyx; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
334 case "D": keyd = !keyd; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
335 case "L": keyl = !keyl; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
336 case "M": keym = !keym; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
337 case "H": keyh = !keyh; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
338 case "1": key1 = !key1; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
339 case "2": key2 = !key2; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
340 case "3": key3 = !key3; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
341 case "0": key0 = !key0; break;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
342 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
343
4324d3f925fe Uploaded
bornea
parents:
diff changeset
344 link.style("display", function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
345 var 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);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
346 linkedByIndex[d.source.index + "," + d.target.index] = flag;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
347 return flag?"inline":"none";});
4324d3f925fe Uploaded
bornea
parents:
diff changeset
348 node.style("display", function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
349 return (key0||hasConnections(d))&&vis_by_type(d.type)&&vis_by_node_score(d.score)?"inline":"none";});
4324d3f925fe Uploaded
bornea
parents:
diff changeset
350 text.style("display", function(d) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
351 return (key0||hasConnections(d))&&vis_by_type(d.type)&&vis_by_node_score(d.score)?"inline":"none";});
4324d3f925fe Uploaded
bornea
parents:
diff changeset
352
4324d3f925fe Uploaded
bornea
parents:
diff changeset
353 if (highlight_node !== null)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
354 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
355 if ((key0||hasConnections(highlight_node))&&vis_by_type(highlight_node.type)&&vis_by_node_score(highlight_node.score)) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
356 if (focus_node!==null) set_focus(focus_node);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
357 set_highlight(highlight_node);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
358 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
359 else {exit_highlight();}
4324d3f925fe Uploaded
bornea
parents:
diff changeset
360 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
361
4324d3f925fe Uploaded
bornea
parents:
diff changeset
362 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
363 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
364
4324d3f925fe Uploaded
bornea
parents:
diff changeset
365 };
4324d3f925fe Uploaded
bornea
parents:
diff changeset
366
4324d3f925fe Uploaded
bornea
parents:
diff changeset
367 function vis_by_type(type)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
368 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
369 switch (type) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
370 case "circle": return keyc;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
371 case "circle": return keys;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
372 case "triangle-up": return keyt;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
373 case "diamond": return keyr;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
374 case "cross": return keyx;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
375 case "triangle-down": return keyd;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
376 default: return true;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
377 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
378 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
379 function vis_by_node_score(score)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
380 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
381 if (isNumber(score))
4324d3f925fe Uploaded
bornea
parents:
diff changeset
382 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
383 if (score>=0.666) return keyh;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
384 else if (score>=0.333) return keym;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
385 else if (score>=0) return keyl;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
386 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
387 return true;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
388 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
389
4324d3f925fe Uploaded
bornea
parents:
diff changeset
390 function vis_by_link_score(score)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
391 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
392 if (isNumber(score))
4324d3f925fe Uploaded
bornea
parents:
diff changeset
393 {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
394 if (score>=0.666) return key3;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
395 else if (score>=0.333) return key2;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
396 else if (score>=0) return key1;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
397 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
398 return true;
4324d3f925fe Uploaded
bornea
parents:
diff changeset
399 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
400
4324d3f925fe Uploaded
bornea
parents:
diff changeset
401 function isNumber(n) {
4324d3f925fe Uploaded
bornea
parents:
diff changeset
402 return !isNaN(parseFloat(n)) && isFinite(n);
4324d3f925fe Uploaded
bornea
parents:
diff changeset
403 }
4324d3f925fe Uploaded
bornea
parents:
diff changeset
404 plot_graph(data)
4324d3f925fe Uploaded
bornea
parents:
diff changeset
405
4324d3f925fe Uploaded
bornea
parents:
diff changeset
406 </script>"""
4324d3f925fe Uploaded
bornea
parents:
diff changeset
407
4324d3f925fe Uploaded
bornea
parents:
diff changeset
408 with open(sys.argv[3],"w") as x:
4324d3f925fe Uploaded
bornea
parents:
diff changeset
409 x.write(output)