-
Notifications
You must be signed in to change notification settings - Fork 1
/
d3-rose-chart_genesort.js
348 lines (287 loc) · 9.11 KB
/
d3-rose-chart_genesort.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Created by jmcmicha on 2/11/14.
*/
var width = 950,
height = 950,
radius = Math.min(width, height) / 2 - 50,
ageInnerRadius = 155,
vafInnerRadius = 50,
vafOuterRadius = 145,
numTicksAge = 5,
numTicksVaf = 5,
avgAge = Number(),
axisGap = .15,
startAngle = axisGap/2,
endAngle = 2*Math.PI - axisGap/2,
axisStrokeColor = "#666",
sdat = [],
sdatVaf = [];
var conservedSingletons = ["ASXL2", "AXL", "CBL", "CDKN2A", "CREBBP", "IDH2", "MYLK", "SETBP1", "SF1", "SH2B3", "ZRSR2"];
var vafRange = d3.scale.linear()
.domain([0,100])
.range([vafInnerRadius, vafOuterRadius]);
var vafArc = d3.svg.arc()
.outerRadius(function(d) { return vafRange(d.data.NORMAL_VAF); })
.innerRadius(vafInnerRadius);
var vafArcBg = d3.svg.arc()
.outerRadius(vafOuterRadius)
.innerRadius(vafInnerRadius);
var labelArc = d3.svg.arc()
.outerRadius(radius + 10)
.innerRadius(radius);
var ageRange = d3.scale.linear()
.domain([0, 100])
.range([ageInnerRadius, radius]);
var ageArc = d3.svg.arc()
.outerRadius(function(d) {
var age = d.data.AGE == "null" ? avgAge : d.data.AGE;
return ageRange(age);
})
.innerRadius(ageInnerRadius);
var ageAxisArc = d3.svg.arc()
.outerRadius(function(d) {
return d + .5;
})
.innerRadius(function(d) {
return d - .5;
})
.startAngle(startAngle)
.endAngle(endAngle);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return 1
})
.startAngle(startAngle)
.endAngle(endAngle)
var pieGroups = d3.layout.pie()
.sort(null)
.value(function(d) {
return d.length;
})
.startAngle(startAngle)
.endAngle(endAngle);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// create crosshatch pattern
svg.append('defs')
.append('pattern')
.attr('id', 'diagonalHatch')
.attr('patternUnits', 'userSpaceOnUse')
.attr('width', 4)
.attr('height', 4)
.append('path')
.attr('d', 'M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2')
.attr('stroke', '#000000')
.attr('stroke-width', 1);
// set up tooltips
var tipAge = d3.tip()
.attr('class', 'd3-tip')
.direction("center")
.html(function(d) {
return "<strong>CASE:</strong> <span style='color:red'>" + d.data.CASE + "</span><br/>" +
"<strong>AGE:</strong> <span style='color: red;'>" + d.data.AGE + "</span><br/>" +
"<strong>NORMAL VAF:</strong> <span style='color: red;'>" + d.data.NORMAL_VAF + "</span><br/>" +
"<strong>GENE:</strong> <span style='color: red;'>" + d.data.GENE + "</span>";
});
svg.call(tipAge);
// set up tick positions (this can probably be done more elegantly...)
for (i=0; i<=numTicksAge; i++) {
sdat[i] = ageInnerRadius + (((radius - ageInnerRadius)/numTicksAge) * i);
}
for (i=0; i<=numTicksVaf; i++) {
sdatVaf[i] = vafInnerRadius + (((vafOuterRadius- vafInnerRadius)/numTicksVaf) * i);
}
d3.csv("GP-77_samples-data-14.csv", function(error, data) {
var dataGroups;
// groupBy gene name (using notAML as the gene name for genes in the notAML group)
dataGroups = _.groupBy(data, function (sample) {
if (sample.GROUP == "AML") {
return sample.GENE;
} else {
return "notAML";
}
});
// sort dataGroups by sample count in each group, descending
dataGroups = _.chain(dataGroups)
//.sortBy(function(group) { return group[0]['GENE']; })
.sortBy(function(group) { return group.length; })
.reverse()
.value();
// move singletons to their own group
var singletonGroupArray = _.remove(dataGroups, function(group) {
return group.length == 1 && _.contains(conservedSingletons, group[0].GENE);
});
console.log("dataGroups before:");
console.dir(dataGroups);
var notAMLg = dataGroups[_.findIndex(dataGroups, function(group) {
return _.every(group, function(sample) {
return sample.GROUP == "notAML";
});
})];
// tag the remainder of the singletons as notAML and move to the notAML group
_.forEach(dataGroups, function(group) {
if (group.length == 1) {
group[0].GROUP = "notAML";
notAMLg.push(group[0]);
}
});
// convert from array of arrays of objects to array of objects
var singletonGroup = [];
_.forEach(singletonGroupArray, function(array) {
singletonGroup.push(array[0]);
});
singletonGroup = _.sortBy(singletonGroup, "AGE");
dataGroups.push(singletonGroup);
// move notAML group to the end
var notAMLgroup = _.remove(dataGroups, function(group) {
if (_.every(group, function(sample) { return sample.GROUP == "notAML"; })) {
return true;
}
});
dataGroups.push(notAMLgroup[0]);
// get a list of non-aml genes for use in constructing the key
console.log("notAML group:" );
console.log(_.map(notAMLgroup[0], function(s) { return s.GENE }).sort().join(" "));
avgAge = Math.round(d3.mean(_.pluck(data, "AGE")));
console.log("avgAge: " + avgAge);
// sort each group by age, replacing null AGE values with
dataGroups = _.map(dataGroups, function(group){
return _.sortBy(group, function(sample) {
if (sample.AGE == "null") {
return avgAge;
} else {
return sample.AGE;
}
});
});
// concat all groups into one data array
data = data.concat.apply([], dataGroups);
var legendItems = _.uniq(_.pluck(data, "GENE")).sort();
// set up color palettes
var ageColor = d3.scale.category20();
var notAMLcolor = d3.scale.linear().domain([0, notAMLg.length]).range(["#000000","#EFEFEF"]);
// draw age rose chart
var gAge = svg.append("g")
.attr("id", "gAge");
var ga = gAge.selectAll(".arc")
.data(pie(data))
.enter().append("g");
ga.append("path")
.attr("d", ageArc)
.attr("data-legend", function(d) {
if (d.data.GROUP == "notAML") {
return "notAML"
} else {
return d.data.GENE
}
})
.attr("data-legend-pos", function(d) { return legendItems.indexOf(d.data.GENE)})
.style("fill", function(d) {
if (d.data.GROUP == "notAML") {
return notAMLcolor(_.indexOf(notAMLg, d.data));
} else {
return ageColor(d.data.GENE);
}
})
.style("stroke", function(d) {
if (d.data.CASE.substr(-3) == "dbl") {
return "#0F0";
} else if (d.data.AGE == "null") {
return "#00F";
} else {
return "#FFF";
}
})
.style("stroke-width", function(d) {
return d.data.AGE == "null" ? 1 : 1;
})
.on('mouseover', tipAge.show)
.on('mouseout', tipAge.hide);
// draw VAF rose chart
var gVaf = svg.append("g")
.attr("id", "gVaf");
var gVaf1 = svg.append("g")
.attr("id", "gVaf1");
var gVaf2 = svg.append("g")
.attr("id", "gVaf2");
// vaf pie bg
var gvbg = gVaf1.selectAll(".arc")
.data(pie(data))
.enter().append("g");
gvbg.append("path")
.attr("d", vafArcBg)
.style("fill", "#EEE")
.style("stroke", "#FFF")
.style("stroke-width", 1);
// vaf pie
var gv = gVaf2.selectAll(".arc")
.data(pie(data))
.enter().append("g");
gv.append("path")
.attr("d", vafArc)
.style("fill", "#AAA")
.style("stroke", "#FFF")
.style("stroke-width", 1);
// draw gene group label arcs
var gLabels= svg.append("g")
.attr("id", "gLabels");
var gl = gLabels.selectAll(".arc")
.data(pieGroups(dataGroups))
.enter().append("g");
gl.append("path")
.attr("d", labelArc)
.style("fill", function(d) {
return ageColor(d.data[0].GENE)
})
.style("stroke", "#FFF")
.style("stroke-width", 1);
addAgeCircleAxes();
addVafCircleAxes();
addLegend();
});
addAgeCircleAxes = function() {
var circleAxes, i;
svg.selectAll('.circle-ticks').remove();
circleAxes = svg.selectAll('.circle-ticks')
.data(sdat)
.enter().append('svg:g')
.attr("class", "circle-ticks");
circleAxes.append("path")
.attr("d", ageAxisArc)
.style("fill", axisStrokeColor);
circleAxes.append("svg:text")
.attr("text-anchor", "center")
.attr("font-size", "14px")
.attr("font-weight", "bold")
.attr("dy", function(d) { return d + 15 })
.style("fill", "#333")
.text(function(d,i) { return i * (100/numTicksAge) });
};
addVafCircleAxes = function() {
var circleAxes, i;
svg.selectAll('.circle-ticks-vaf').remove();
circleAxes = svg.selectAll('.circle-ticks-vaf')
.data(sdatVaf)
.enter().append('svg:g')
.attr("class", "circle-ticks-vaf");
circleAxes.append("path")
.attr("d", ageAxisArc)
.style("fill", axisStrokeColor);
circleAxes.append("svg:text")
.attr("text-anchor", "start")
.attr("font-size", "8px")
.attr("font-weight", "bold")
.attr("dy", function(d) { return d + 15 })
.style("fill", "#333")
.text(function(d,i) { return i * (100/numTicksVaf) });
};
addLegend = function() {
svg.append("g")
.attr("class","legend")
.attr("transform","translate(400,225)")
.style("font-size","12px")
.call(d3.legend);
};