Mercurial > repos > bornea > plot_adjacency
comparison plot_adjacency.py @ 1:65551439a6bb draft default tip
Uploaded
author | bornea |
---|---|
date | Wed, 18 Oct 2017 15:29:22 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:8adf6ccd1077 | 1:65551439a6bb |
---|---|
1 import sys | |
2 def readTab(infile): # read in txt file | |
3 with open(infile, 'r') as input_file: | |
4 # read in tab-delim text | |
5 output = [] | |
6 for input_line in input_file: | |
7 input_line = input_line.strip() | |
8 temp = input_line.split('\t') | |
9 output.append(temp) | |
10 return output | |
11 def network2JSON(nodes_att_file, edge_file): | |
12 nodes = readTab(nodes_att_file) | |
13 edges = readTab(edge_file) | |
14 | |
15 start = """{"nodes": [""" | |
16 node_numbers = {} | |
17 cnt=0 | |
18 for i in nodes[1:]: | |
19 node_numbers[i[0]] = cnt | |
20 cnt+=1 | |
21 start = start + '{"name":'+'"'+i[0]+'", "EV":'+i[2]+', "group":'+i[1]+"}," | |
22 start = start[:-1] | |
23 start = start+"""], "links": [""" | |
24 for i in edges: | |
25 start = start + """{"source":"""+str(node_numbers[i[0]])+""","target":"""+str(node_numbers[i[1]])+""","value":1},""" | |
26 start = start[:-1] | |
27 start = start + """]}""" | |
28 return start | |
29 def labelCentrality(nodes_att_file): | |
30 nodes = readTab(nodes_att_file) | |
31 if nodes[0][2] == "eigen": | |
32 return "Eigenvector Centrality" | |
33 if nodes[0][2] == "closeness": | |
34 return "Closeness Centrality" | |
35 if nodes[0][2] == "betweenness": | |
36 return "Betweenness Centrality" | |
37 if nodes[0][2] == "page": | |
38 return "Page Rank" | |
39 | |
40 | |
41 | |
42 adjacency_start = """ | |
43 <!DOCTYPE html> | |
44 <html><head> | |
45 <meta charset="utf-8"> | |
46 <style> | |
47 .background {fill: #fff;} | |
48 line {stroke: #000;} | |
49 text.active {fill: red;font-weight:bold;} | |
50 </style> | |
51 <script src="http://d3js.org/d3.v2.min.js" charset="utf-8"></script> | |
52 <script src="https://cdn.rawgit.com/eligrey/Blob.js/0cef2746414269b16834878a8abc52eb9d53e6bd/Blob.js""></script> | |
53 <script src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script> | |
54 </head> | |
55 <aside style = "margin-top: 80px"> | |
56 <p>Order: <select id="order"> | |
57 <option value="name">Name</option> | |
58 <option value="EV">""" + labelCentrality(sys.argv[1]) + """</option> | |
59 <option value="group">Modularity</option> | |
60 </select> | |
61 <button id="generate">Save as SVG</button> | |
62 </aside> | |
63 | |
64 <script> | |
65 var margin = {top: 200, right: 0, bottom: 10, left: 200}, | |
66 width = 720, | |
67 height = 720; | |
68 | |
69 var x = d3.scale.ordinal().rangeBands([0, width]), | |
70 z = d3.scale.linear().domain([0, 4]).clamp(true), | |
71 c = d3.scale.category10().domain(d3.range(10)); | |
72 | |
73 var svg = d3.select("body").append("svg") | |
74 .attr("id","adjmatrix") | |
75 .attr("width", width + margin.left + margin.right) | |
76 .attr("height", height + margin.top + margin.bottom) | |
77 //.style("margin-left", -margin.left + "px") | |
78 .append("g") | |
79 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");""" | |
80 | |
81 | |
82 ################# Create JSON formatted Network ################ | |
83 | |
84 jsondata = """\njsondata ='""" + network2JSON(sys.argv[1],sys.argv[2])+"'" | |
85 | |
86 ################################################################ | |
87 adjacency_end = """; | |
88 var data = JSON.parse(jsondata); | |
89 | |
90 function plot_adjacency(data) { | |
91 var matrix = [], | |
92 nodes = data.nodes, | |
93 n = nodes.length; | |
94 | |
95 // Compute index per node. | |
96 nodes.forEach(function(node, i) { | |
97 node.index = i; | |
98 node.count = 0; | |
99 matrix[i] = d3.range(n).map(function(j) { return {x: j, y: i, z: 0}; }); | |
100 }); | |
101 | |
102 // Convert edges to matrix | |
103 data.links.forEach(function(link) { | |
104 matrix[link.source][link.target].z += link.value; | |
105 matrix[link.target][link.source].z += link.value; | |
106 matrix[link.source][link.source].z += link.value; // self loop | |
107 matrix[link.target][link.target].z += link.value; // self loop | |
108 | |
109 }); | |
110 | |
111 // order based on modularity, name and eigenvector centrality | |
112 var orders = { | |
113 name: d3.range(n).sort(function(a, b) { return d3.ascending(nodes[a].name, nodes[b].name); }), | |
114 EV: d3.range(n).sort(function(a, b) { return nodes[b].EV - nodes[a].EV; }), | |
115 group: d3.range(n).sort(function(a, b) { return nodes[b].group - nodes[a].group; }) | |
116 }; | |
117 | |
118 // The default sort order. | |
119 x.domain(orders.name); | |
120 | |
121 svg.append("rect") | |
122 .attr("class", "background") | |
123 .attr("width", width) | |
124 .attr("height", height) | |
125 .attr("fill-opacity",0); | |
126 | |
127 var row = svg.selectAll(".row") | |
128 .data(matrix) | |
129 .enter().append("g") | |
130 .attr("class", "row") | |
131 .attr("transform", function(d, i) { return "translate(0," + x(i) + ")"; }) | |
132 .each(row); | |
133 | |
134 row.append("line") | |
135 .attr("x2", width) | |
136 .attr("stroke","black"); | |
137 | |
138 row.append("text") | |
139 .attr("x", -6) | |
140 .attr("y", x.rangeBand() / 2) | |
141 .attr("dy", ".32em") | |
142 .attr("text-anchor", "end") | |
143 .attr("font-size", 850 * (1/nodes.length)) | |
144 .text(function(d, i) { return nodes[i].name; }); | |
145 | |
146 var column = svg.selectAll(".column") | |
147 .data(matrix) | |
148 .enter().append("g") | |
149 .attr("class", "column") | |
150 .attr("transform", function(d, i) { return "translate(" + x(i) + ")rotate(-90)"; }); | |
151 | |
152 column.append("line") | |
153 .attr("x1", -width) | |
154 .attr("stroke","black"); | |
155 | |
156 column.append("text") | |
157 .attr("x", 6) | |
158 .attr("y", x.rangeBand() / 2) | |
159 .attr("dy", ".32em") | |
160 .attr("text-anchor", "start") | |
161 .style("font-style","15px") | |
162 .attr("font-size", 850 * (1/nodes.length)) | |
163 .text(function(d, i) { return nodes[i].name; }); | |
164 | |
165 function row(row) { | |
166 var cell = d3.select(this).selectAll(".cell") | |
167 .data(row.filter(function(d) { return d.z; })) | |
168 .enter().append("rect") | |
169 .attr("class", "cell") | |
170 .attr("x", function(d) { return x(d.x); }) | |
171 .attr("width", x.rangeBand()) | |
172 .attr("height", x.rangeBand()) | |
173 .style("fill-opacity", function(d) { return 0.4 * d.z; }) | |
174 .style("fill", function(d) { return nodes[d.x].group == nodes[d.y].group ? c(nodes[d.x].group) : null; }) | |
175 .on("mouseover", mouseover) | |
176 .on("mouseout", mouseout); | |
177 } | |
178 | |
179 function mouseover(p) { | |
180 d3.selectAll(".row text").classed("active", function(d, i) { return i == p.y; }); | |
181 d3.selectAll(".column text").classed("active", function(d, i) { return i == p.x; }); | |
182 } | |
183 | |
184 function mouseout() { | |
185 d3.selectAll("text").classed("active", false); | |
186 } | |
187 | |
188 d3.select("#order").on("change", function() { | |
189 clearTimeout(timeout); | |
190 order(this.value); | |
191 }); | |
192 | |
193 | |
194 function order(value) { | |
195 x.domain(orders[value]); | |
196 | |
197 var t = svg.transition().duration(2500); | |
198 | |
199 t.selectAll(".row") | |
200 .delay(function(d, i) { return x(i) * 4; }) | |
201 .attr("transform", function(d, i) { return "translate(0," + x(i) + ")"; }) | |
202 .selectAll(".cell") | |
203 .delay(function(d) { return x(d.x) * 4; }) | |
204 .attr("x", function(d) { return x(d.x); }); | |
205 | |
206 t.selectAll(".column") | |
207 .delay(function(d, i) { return x(i) * 4; }) | |
208 .attr("transform", function(d, i) { return "translate(" + x(i) + ")rotate(-90)"; }); | |
209 } | |
210 | |
211 var timeout = setTimeout(function() { | |
212 order("group"); | |
213 d3.select("#order").property("selectedIndex", 2).node().focus(); | |
214 }, 5000); | |
215 | |
216 | |
217 }; | |
218 plot_adjacency(data); | |
219 d3.select("#generate") | |
220 .on("click", writeDownloadLink); | |
221 | |
222 function writeDownloadLink(){ //saving entire file instead of just SVG element | |
223 try { | |
224 var isFileSaverSupported = !!new Blob(); | |
225 } catch (e) { | |
226 alert("blob not supported"); | |
227 } | |
228 | |
229 var html = document.getElementById("adjmatrix").outerHTML | |
230 | |
231 var blob = new Blob([html], {type: "image/svg+xml"}); | |
232 saveAs(blob, "myProfile.svg"); | |
233 }; | |
234 </script> | |
235 | |
236 | |
237 </body></html> | |
238 """ | |
239 with open(sys.argv[3],"w") as x: | |
240 x.write(adjacency_start+jsondata+adjacency_end) |