-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
464 lines (410 loc) · 16.8 KB
/
index.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
var d3 = require('d3');
var $ = require('jquery');
var gff = require('./gff3.js');
var Color = require('color');
// margin is space between genomes and container
var margin = {top: 30, right: 30, bottom: 30, left: 30},
width = 960 - margin.left - margin.right,
height = 660 - margin.top - margin.bottom,
genome_offset = 0,
genes_offset = 5, // from lcb
genome_height = 10,
lcb_overflow = 10,
longest = 0;
var gff3s = [];
var adjusted_genomes = [];
var genome_map = {};
var gene_rows = {};
var xmfas;
var genomeGroup;
var genesGroups = [];
var lcbGroups = [];
var textGroups = [];
var lcb_areaGroup = [];
// find length as a function of longest genome/canvas width
function convert(length) {
return length/longest * width;
};
var zoom = d3.zoom()
.scaleExtent([1, 1000])
.on("zoom", zoomed);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
//.style("display", "block")
//.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
// background
var rect = svg.append("rect")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("fill", "white")
.style("opacity", 0.4)
.style("pointer-events", "all");
// where genome name labels go
var border = d3.select("body")
.append("svg")
.attr("width", 100)
.attr("height", height + margin.top + margin.bottom)
var info = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", height + margin.top + margin.bottom)
container = svg.append("g");
//genome_elements = container.append("g");
function zoomed() {
tx = d3.event.transform;
txf = "translate(" + tx.x + ") scale(" + tx.k + ",1"+ ")";
container.attr("transform", txf);
if (tx.k == 1000) {
var x = tx.x
console.log(x);
draw_bars();
}
}
// draw genomes as rectangles
function draw_genomes() {
genomeGroup = container.selectAll("genomes")
.data(adjusted_genomes)
.enter()
.append("rect")
.attr("width", function(d) {return d.length;})
.attr("height", genome_height)
.attr("x", margin.left)
.attr("y", function(d,i) {return margin.top + i*genome_offset})
.attr("id", function(d, i){ return d.name; })
.style("fill", "green");
var text = border.selectAll("text")
.data(adjusted_genomes)
.enter()
.append("text")
.attr("y", function(d,i) {return margin.top + i*genome_offset + lcb_overflow;})
.text( function (d) { return d.name; })
.attr("font-family", "sans-serif")
.attr("font-size", "20px");
};
function gene_clicked(d){
colors = {
'%23FFFF00': 'DNA replication/recombination',
'%23FFA500': 'regulation',
'%2387CEFA': 'structural/morphogenesis',
'%23FF00FF': 'lysis',
'%2371BC78': 'packaging',
'black': 'other',
}
metadata = [d.seqid + ' ' + d.attributes.num, d.attributes.product, colors[d.attributes.color]];
console.log(metadata.join(' | '))
sizes = ['20px', '15px', '15px', '15px'];
if (textGroups.length) {
// if text already there, then change text
textGroups.map(function(old_text) {
old_text.text(function(d, i) {return metadata[i];})
});
}
else {
text = info.selectAll("text")
.data(metadata)
.enter()
.append("text")
.attr("x", '100')
.attr("y", function(d,i) {return i*20 + (height-80)/2;})
.text( function (d) {return d;})
.attr("font-family", "sans-serif")
.attr("font-size", function(d,i) {return sizes[i];});
//.attr("font-size", "20px");
textGroups.push(text);
}
};
function compute_height(genome_index, feat_index) {
for(var row in gene_rows[genome_index]) {
if (gene_rows[genome_index][row].indexOf(feat_index) > -1) {
return row*10;
}
}
};
function find_index(name) {
for (var genome in adjusted_genomes) {
if (name == adjusted_genomes[genome].name) {
return genome;
}
}
};
function gene_points(x_offset, gff3, i) {
var index = find_index(gff3[0].seqid);
var starting_x = x_offset + margin.left + convert(gff3[i].start);
var starting_y = genes_offset + margin.top + genome_height + lcb_overflow/2 + compute_height(index, i) + index*genome_offset;
var width = convert(gff3[i].end - gff3[i].start);
var height = 10;
if (width < 5) {
// draw tiny genes as just triangles
if (gff3[i].strand == '-') {
starting_x = starting_x + width;
return [
[starting_x, starting_y],
[starting_x - width, starting_y + height/2],
[starting_x, starting_y + height]
].join(' ');
}
return [
[starting_x, starting_y],
[starting_x + width, starting_y + height/2],
[starting_x, starting_y + height]
].join(' ');
}
if (gff3[i].strand == '-') {
starting_x = starting_x + width;
return [
[starting_x, starting_y],
[starting_x, starting_y + height],
[starting_x - width + 5, starting_y + height],
[starting_x - width, starting_y + height/2],
[starting_x - width + 5, starting_y]
].join(' ');
}
return [
[starting_x, starting_y],
[starting_x, starting_y + height],
[starting_x + width - 5, starting_y + height],
[starting_x + width, starting_y + height/2],
[starting_x + width - 5, starting_y]
].join(' ');
}
// draw genome features for each genome
function draw_features(gff3) {
var index = find_index(gff3[0].seqid);
var genes = container.selectAll('gene' + index)
.data(gff3)
.enter()
.append("polygon")
.attr("points", function(d,i) {return gene_points(0, gff3, i);})
//.append("rect")
//.attr("class", "gene")
//.attr("width", function(d, i) {return convert(d.end - d.start);})
//.attr("height", 10)
//.attr("x", function(d, i) {return margin.left + convert(d.start);})
//.attr("y", function(d, i) {
//return genes_offset + margin.top + genome_height + lcb_overflow/2 + compute_height(index, i) + index*genome_offset;
//})
.style("fill", function(d,i) {
if (d.attributes.color) {
return d.attributes.color.replace("%23", "#");
}
return "black";
})
.on("click", gene_clicked);
//.on("click", function(){
////PointColors = [PointColors[1], PointColors[0]]
//d3.selectAll(".gene").style("fill", "black");
//d3.select(this).style("fill", "blue");
//});
genesGroups.push(genes);
};
function calculate_offset(genome, lcb) {
xmfas[lcb].map(function(g) {
adjusted_genomes[g.id-1].x_offset = convert(genome.start - g.start);
});
};
function redraw() {
genomeGroup.attr("x", function(d) {return margin.left + d.x_offset;})
lcbGroups.map(function(lcb) {
lcb.attr("x", function(d, i) {return adjusted_genomes[d.id-1].x_offset + margin.left + convert(d.start);})
});
lcb_areaGroup.map(function(lcb_area, index) {
lcb_area.attr("points", function(d,i) {return configure_lcb_areas(xmfas[index], i);})
});
genesGroups.map(function(genes, index) {
genes.attr("points", function(d, i) {return gene_points(adjusted_genomes[genome_map[d.seqid]].x_offset, gff3s[index], i);})
//genes.attr("x", function(d, i) {return adjusted_genomes[genome_map[d.seqid]].x_offset + margin.left + convert(d.start);})
});
};
function configure_lcb_areas(lcb, i) {
var num_rows = function() {
var l = 1;
for (var i in gene_rows) {
if (Object.keys(gene_rows[i]).length - 1 > l) {
l = Object.keys(gene_rows[i]).length - 1;
}
}
return l;
}
var p1x = (adjusted_genomes[lcb[i].id-1].x_offset + margin.left + lcb[i].start/longest*width).toString();
var p1y = (margin.top+genome_height+(lcb_overflow/2) + (lcb[i].id - 1)*genome_offset).toString();
var p2x = (adjusted_genomes[lcb[i].id-1].x_offset + margin.left + lcb[i].end/longest*width).toString();
var p2y = (margin.top+genome_height+(lcb_overflow/2) + (lcb[i].id - 1)*genome_offset).toString();
var p3x = p2x;
var p3y = (margin.top+genome_height+(lcb_overflow/2) + (lcb[i].id - 1)*genome_offset + genes_offset*2 + 10*num_rows()).toString();
var p4x = (adjusted_genomes[lcb[i+1].id-1].x_offset + margin.left + lcb[i+1].end/longest*width).toString();
var p4y = (margin.top-(lcb_overflow/2) + (lcb[i+1].id - 1)*genome_offset).toString();
var p5x = (adjusted_genomes[lcb[i+1].id-1].x_offset + margin.left + lcb[i+1].start/longest*width).toString();
var p5y = (margin.top-(lcb_overflow/2) + (lcb[i+1].id - 1)*genome_offset).toString();
var p6x = p1x;
var p6y = p3y;
if (lcb[i].strand != lcb[i+1].strand) {
return [[p1x,p1y],[p2x,p2y],[p3x,p3y],[p5x,p5y],[p4x,p4y],[p6x,p6y]].join(' ')
} else { return [[p1x,p1y],[p2x,p2y],[p3x,p3y],[p4x,p4y],[p5x,p5y],[p6x,p6y]].join(' ') }
};
function draw_lcbs(lcb, index, color, color2) {
var lcbs = container.selectAll('lcb' + index)
.data(lcb)
.enter()
.append("rect")
.attr("width", function(d, i) {return convert(d.end - d.start);})
.attr("height", genome_height+lcb_overflow)
.attr("x", function(d, i) {return margin.left + convert(d.start);})
.attr("y", function(d, i) {
return margin.top-(lcb_overflow/2) + (d.id - 1)*genome_offset;
})
.attr("id", function(d, i){ return d.rid; })
//.style("fill", color2)
.style("fill", "blue")
.style("opacity", 0.5)
.on("click", function(genome){
calculate_offset(genome, index);
redraw();
});
lcbGroups.push(lcbs);
sliced_lcb = lcb.slice(0,lcb.length-1); // need to use +1, so slice array so no range error
var lcb_areas = container.selectAll('lcb_area' + index)
.data(sliced_lcb)
.enter()
.append("polygon")
.attr("points", function(d,i) {return configure_lcb_areas(lcb, i);})
.style("fill", "#c5c4c4")
//.style("stroke", "green")
//.style("stroke-width", "0.1")
//.style("fill", color)
.style("opacity", 0.65)
//.style("opacity", 0.5)
lcb_areaGroup.push(lcb_areas);
};
function draw_bars() {
// want to draw nucleotides upon full zoom eventually
var w = width/longest;
//adjusted_genomes.map(function(g, index) {
//var bars = container.selectAll("bars")
//.data(adjusted_genomes[0].seq)
//.enter()
//.append("rect")
//.attr("width", w)
//.attr("height", genome_height)
//.attr("x", function(d, i) {return margin.left + i*w})
//.attr("y", function(d) {console.log(d); return margin.top + 0*genome_offset})
//.style("fill", function(nuc) {
//switch(nuc) {
//case "A":
//return "blue";
//case "T":
//return "red";
//case "G":
//return "yellow";
//case "C":
//return "green";
//}
//});
//});
};
// find genome with longest length
function find_longest(fasta) {
return Math.max.apply(null, fasta.map(function(data) {
return data.length;
}));
}
// find how far apart genomes should be from each other
function set_genome_offset(num_genomes) {
genome_offset = height/num_genomes;
};
// adjust pixels to genome length
function adjust_genomes(data) {
$.each(data, function(key, fasta, i) {
adjusted_genomes.push({name: fasta.name, length: convert(fasta.length), x_offset:0, seq:''});
genome_map[fasta.name] = key;
});
};
// parse url
function parseQueryString(url) {
var urlParams = {};
url.replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) {
urlParams[$1] = $3;
}
);
return urlParams;
}
// space genes out by putting them on different rows
function assign_rows(gff3) {
last_placed = [null];
var rows = {0:[]}
gff3.map(function(gene, i) {
for (locs in last_placed) {
if (last_placed[locs] == null || convert(gene.start) > 1+last_placed[locs]) {
if (last_placed[locs] == null) {
last_placed.push(null);
rows[parseInt(locs)+1] = [];
}
last_placed[locs] = convert(gene.end);
rows[locs].push(i);
return;
}
}
});
return rows;
};
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
// if no file is specified in url:
var dataLocation = parseQueryString(location.search).url;
if (!dataLocation) {
dataLocation = 'data.json';
}
//get fasta, gff3, and xmfa data
$.getJSON(dataLocation, function(json) {
longest = find_longest(json.fasta); // use this as reference for length ratios
set_genome_offset(json.fasta.length); // how far apart the genomes should be
adjust_genomes(json.fasta);
draw_genomes();
var colors = ["#a6cee3","#b2df8a","#fb9a99","#fdbf6f","#cab2d6", "#ffff99"];
colors = colors.map(function(c){ return Color(c) });
//var colors = ["#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a"];
$.getJSON(json.xmfa, function(xmfa) {
promises = []
var promise1 = json.gff3.map(function(j) {
var p = $.get(j, function(gff3_data) {
var gff3 = gff.process(gff3_data, ['CDS']);
gff3s.push(gff3);
gene_rows[find_index(gff3[0].seqid)] = assign_rows(sortByKey(gff3, 'start'));
});
promises.push(p)
});
promises.push(promise1)
//for (var j in json.gff3) {
//$.get(json.gff3[j], function(gff3_data) {
//var gff3 = gff.process(gff3_data, ['CDS']);
//gff3s.push(gff3);
//gene_rows[find_index(gff3[0].seqid)] = assign_rows(sortByKey(gff3, 'start'));
//});
//}
Promise.all(promises).then(function(values) {
xmfas = xmfa;
var promise2 = xmfa.map(function(lcb, i) {
var c = colors[i % colors.length];
draw_lcbs(lcb, i, c.rgb().string(), c.darken(0.5).rgb().string());
});
promises.push(promise2)
Promise.all(promises).then(function(values) {
for (var i in gff3s) {
draw_features(gff3s[i]);
}
});
});
});
(json.fasta).map(function(f) {
$.get(f.path, function(sequence) {
adjusted_genomes[genome_map[f.name]].seq = sequence;
});
});
});