-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions_v3.js
278 lines (225 loc) · 7.6 KB
/
interactions_v3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
function interactions(input,interactionTerms,referenceTerms,originalGenes) {
console.log(input);
var dataset = input;
var nodes=dataset.nodes;
var w=2000;
var h=2000;
if (nodes.length>500) {
w=4*nodes.length;
h=4*nodes.length;
}
var size = 100;
//Initialize a default force layout, using the nodes and edges in dataset
var force = d3.layout.force()
.gravity(0.8)
.size([w, h]);
var maxEdgeCount = 0;
for (var i=0;i<nodes.length;i++) {
if (nodes[i].class==="targetNode") {
if (nodes[i].edgeCount>maxEdgeCount) {
maxEdgeCount=nodes[i].edgeCount;
}
}
}
// determine how many foci we need and evenly distribute them throughout the force graph
var numberOfGroups=0;
for (var i=0;i<originalGenes.length;i++) {
if (originalGenes[i].groupID>numberOfGroups) {
numberOfGroups=originalGenes[i].groupID;
}
}
numberOfGroups=numberOfGroups+1;
var foci=createFoci(numberOfGroups,w,h);
// if maxEdgeCount<6, then there may be errors here. I could implement a different color scale for that case or just turn it off.
console.log(maxEdgeCount);
var colors = d3.scale.linear()
.domain([1,maxEdgeCount])
.range(["#ffffff","#000000"]);
var groupColors = d3.scale.category20();
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var windowHeight = window.innerHeight;
var windowWidth = window.innerWidth;
var halfWidth = w/2;
var halfHeight = h/2;
var halfWindowHeight = windowHeight/2;
var halfWindowWidth= windowWidth/2;
window.scrollTo((halfWidth-halfWindowWidth),(halfHeight-halfWindowHeight));
force
.nodes(nodes)
.links(dataset.edges)
.charge([-1000])
.start();
var rectangle = d3.superformula()
.type("longRectangle")
.size(3000)
.segments(360);
var circle = d3.superformula()
.type("circle")
.size(150)
.segments(360);
//Create edges as lines
var edges = svg.selectAll("line")
.data(dataset.edges)
.enter()
.append("line")
.style("stroke", "#ccc")
.style("stroke-width", 2);
var nodeGroup = svg.selectAll("g")
.data(nodes)
.enter()
.append("g")
.on("contextmenu", contextClickNode)
.on("click", clickNode);
nodeGroup
.append("path")
.attr("class", function(d) {return d.class;})
.attr("d", function(d) {
if(d.shape=="rectangle")
{return rectangle(d)}
else if (d.shape=="circle")
{return circle(d)}
});
// label the nodes: source nodes get the text written on them directly, target nodes only show their label on hover
nodeGroup
.append("svg:title")
.text(function(d) {
return d.name;
});
var sourceNodes = nodeGroup.filter(function(d,i) {return d.class == 'sourceNode'});
sourceNodes.append("text")
.text(function(d) {
return d.name;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return 0; //doesn't need to be shifted because we used text-anchor middle
})
.attr("y", function(d) {
return 5; //this needs to be half of the size of font-size specified below
})
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.attr("fill", "black");
//Adjust rectangular source nodes
sourceNodes.selectAll("path")
.style("stroke","steelblue")
.attr("stroke-width","3px")
.attr("groupID",function(d) { return d.groupID;})
.style("fill",function(d) { return groupColors(d.groupID+2);})
.call(force.drag);
//Adjust circular target nodes
var targetNodes = nodeGroup.selectAll("[class=targetNode]")
.style("fill", function(d) {return colors(d.edgeCount);})
.style("stroke", "steelblue")
.attr("stroke-width","2px")
.call(force.drag);
//Every time the simulation "ticks", this will be called
force.on("tick", function(e) {
if (numberOfGroups>1) {
// this block is to implement the multiple foci
var k=1*e.alpha;
sourceNodes.each(function(o,i) {
o.y += (foci[o.groupID].y-o.y)*k;
o.x += (foci[o.groupID].x-o.x)*k;
});
}
// this block is to implement the collide function so that all the nodes don't cover each other up
var q = d3.geom.quadtree(nodes),
i = 0,
n = nodes.length;
while (++i < n) {
q.visit(collide(nodes[i]));
}
//Update positions of all edges
edges.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
//Update positions of all nodes and their labels
nodeGroup.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; });
});
function collide(node) {
var r = node.radius + 16,
nx1 = node.x - r,
nx2 = node.x + r,
ny1 = node.y - r,
ny2 = node.y + r;
return function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== node)) {
var x = node.x - quad.point.x,
y = node.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = node.radius + quad.point.radius;
if (l < r) {
l = (l - r) / l * .5;
node.x -= x *= l;
node.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2
|| x2 < nx1
|| y1 > ny2
|| y2 < ny1;
};
}
// On node hover, examine the links to see if their
// source or target properties match the hovered node.
nodeGroup.on('mouseover', function(d) {
edges.style('stroke-width', function(l) {
if (d === l.source || d === l.target)
return 4;
else
return 2;
});
edges.style('stroke', function(l) {
if (d === l.source || d === l.target)
return 'steelblue';
else
return '#ccc';
});
});
// Set the stroke width back to normal when mouse leaves the node.
nodeGroup.on('mouseout', function() {
edges.style('stroke-width', 2);
edges.style('stroke', '#ccc');
});
// this function ends the current viewer and launches an instance of singleNodeView with the selected node as the root
function clickNode() {
// first grab this node
var thisNode=d3.select(this).select("title").text();
// initialize a json object with this node as the root
var tempData = { "name":thisNode , "children": [] };
// next, grab each node that this node is connected with and add them to the json object as children
// now pass the tempData object to the fillOutData function which will add in missing children and grandchildren that were not present in the
// force-directed graph view
var completedData={};
completedData=fillOutDataGene(tempData,referenceTerms);
var totalPotentialNodes=completedData.children.length;
for (var i=0; i<completedData.children.length;i++) {
totalPotentialNodes=totalPotentialNodes + completedData.children[i].children.length;
}
// finally, delete the svg and pass the json object to the single node view
if (completedData.children.length>160) {
var newDiameter = completedData.children.length*15; // here the magic number is just an optimizing value -- feel free to change it
d3.selectAll("svg").remove().call(function() {singleNodeView(completedData, interactionTerms, referenceTerms, originalGenes, newDiameter);});
}
else if (totalPotentialNodes>200) {
var newDiameter= totalPotentialNodes*9;
d3.selectAll("svg").remove().call(function() {singleNodeView(completedData, interactionTerms, referenceTerms, originalGenes, newDiameter);});
}
else {
d3.selectAll("svg").remove().call(function() {singleNodeView(completedData, interactionTerms, referenceTerms, originalGenes);});
}
}
function contextClickNode() {
console.log("right click registered");
d3.selectAll("svg").remove().call(function() {bubbles(originalGenes, interactionTerms, referenceTerms);});
}
}