-
Notifications
You must be signed in to change notification settings - Fork 3
/
d3panels.js
7285 lines (5440 loc) · 219 KB
/
d3panels.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!function() { // encapsulate d3panels functions
var d3panels = {
version: "1.8.4"
};
"use strict";
// Generated by CoffeeScript 2.6.1
// A variety of utility functions used by the different panel functions
// determine rounding of axis labels
d3panels.formatAxis = function (d) {
var extra_digits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var gap, i, ndig, x; // determine typical gap between values
//# remove nulls and sort
d = function () {
var k, len, results;
results = [];
for (k = 0, len = d.length; k < len; k++) {
x = d[k];
if (x != null) {
results.push(x);
}
}
return results;
}();
d.sort();
if (d.length <= 1) {
gap = 1;
} else {
// take differences
d = function () {
var k, ref, results;
results = [];
for (i = k = 1, ref = d.length - 1; 1 <= ref ? k <= ref : k >= ref; i = 1 <= ref ? ++k : --k) {
results.push(d[i] - d[i - 1]);
}
return results;
}(); // get mean
gap = d3.mean(d);
} // turn gap into number of digits
ndig = Math.round(d3panels.log10(Math.abs(gap)));
if (ndig < -8) {
ndig = -8;
}
if (ndig > 0) {
ndig = 0;
}
ndig = Math.abs(ndig) + extra_digits; // function to return
return function (val) {
if (val != null && val !== "NA") {
return d3.format("." + ndig + "f")(val);
}
return "NA";
};
}; // unique values of array (ignore nulls)
d3panels.unique = function (x) {
var k, len, output, v;
output = {};
for (k = 0, len = x.length; k < len; k++) {
v = x[k];
if (v != null) {
output[v] = v;
}
}
output = function () {
var results;
results = [];
for (v in output) {
results.push(output[v]);
}
return results;
}();
return d3panels.forceAsArray(output);
}; // Pull out a variable (column) from a two-dimensional array
// [NO LONGER USED]
d3panels.pullVarAsArray = function (data, variable) {
var i, results;
results = [];
for (i in data) {
results.push(data[i][variable]);
}
return results;
}; // reorganize lod/pos by chromosome
// uniquechr = vector of unique chromosome IDs
// chr = vector of chromosome IDs
d3panels.reorgByChr = function (uniquechr, chr, vector) {
var c, i, k, len, result;
if (chr.length !== vector.length) {
d3panels.displayError("reorgByChr: chr.length (" + chr.length + ") != vector.length (" + vector.length + ")");
}
result = {};
for (i = k = 0, len = uniquechr.length; k < len; i = ++k) {
c = uniquechr[i];
result[c] = function () {
var results;
results = [];
for (i in vector) {
if (chr[i] === c) {
results.push(vector[i]);
}
}
return results;
}();
}
return result;
}; // reorganize lod and position by chromosome
// also, add markerinfo object
d3panels.reorgLodData = function (data) {
var i;
data.posByChr = d3panels.reorgByChr(data.chrname, data.chr, data.pos);
data.lodByChr = d3panels.reorgByChr(data.chrname, data.chr, data.lod);
if (data.poslabel != null) {
data.poslabelByChr = d3panels.reorgByChr(data.chrname, data.chr, data.poslabel);
}
if (data.marker != null) {
data.markerinfo = function () {
var results;
results = [];
for (i in data.marker) {
if (data.marker[i] !== "") {
results.push({
name: data.marker[i],
chr: data.chr[i],
pos: data.pos[i],
lod: data.lod[i]
});
}
}
return results;
}();
}
return data;
}; // calculate chromosome scales for chrpanelframe (and so lodchart)
// type = "xaxis" normal x-axis scale
// = "yaxis" y-axis scale
// = "revyaxis" reversed y-axis scale
d3panels.calc_chrscales = function (plot_width, left_margin, gap, chr, start, end) {
var reverse = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
var chr_end_pixels, chr_length, chr_start_pixels, domain, i, k, n_chr, range, ref, right, tot_chr_length, tot_pixels, xscale; // calculate chromosome lengths, start and end in pixels
n_chr = chr.length;
chr_length = function () {
var results;
results = [];
for (i in end) {
results.push(end[i] - start[i]);
}
return results;
}();
tot_chr_length = chr_length.reduce(function (t, s) {
return t + s;
});
tot_pixels = plot_width - gap * n_chr;
chr_start_pixels = [left_margin + gap / 2];
chr_end_pixels = [left_margin + gap / 2 + tot_pixels / tot_chr_length * chr_length[0]];
for (i = k = 1, ref = n_chr - 1; 1 <= ref ? k <= ref : k >= ref; i = 1 <= ref ? ++k : --k) {
chr_start_pixels.push(chr_end_pixels[i - 1] + gap);
chr_end_pixels.push(chr_start_pixels[i] + tot_pixels / tot_chr_length * chr_length[i]);
}
right = plot_width + left_margin * 2;
xscale = {};
for (i in chr) {
domain = [start[i], end[i]];
range = [chr_start_pixels[i], chr_end_pixels[i]];
if (reverse) {
// for y-axis
domain.reverse();
range = [right - range[1], right - range[0]];
}
xscale[chr[i]] = d3.scaleLinear().domain(domain).range(range);
} // return the scale
return xscale;
}; // Select a set of categorical colors
// ngroup is positive integer
// palette = "dark" or "pastel"
d3panels.selectGroupColors = function (ngroup, palette) {
var cat20, pastel1, pastel20, set1;
if (ngroup === 0) {
return [];
} // ColorBrewer, now in D3 v5
set1 = ["#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf", "#999999"]; // ColorBrewer, now in D3 v5
pastel1 = ["#fbb4ae", "#b3cde3", "#ccebc5", "#decbe4", "#fed9a6", "#ffffcc", "#e5d8bd", "#fddaec", "#f2f2f2"]; // from D3 v4
cat20 = ["#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c", "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5", "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", "#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5"]; // my own rough attempt to make a _big_ pastel palette
pastel20 = ["#8fc7f4", "#fed7f8", "#ffbf8e", "#fffbb8", "#8ce08c", "#d8ffca", "#f68788", "#ffd8d6", "#d4a7fd", "#f5f0f5", "#cc968b", "#f4dcd4", "#f3b7f2", "#f7f6f2", "#bfbfbf", "#f7f7f7", "#fcfd82", "#fbfbcd", "#87feff", "#defaf5"];
if (palette === "dark") {
if (ngroup === 1) {
return ["slateblue"];
}
if (ngroup === 2) {
return ["MediumVioletRed", "slateblue"];
}
if (ngroup === 3) {
return ["MediumVioletRed", "MediumSeaGreen", "slateblue"];
}
if (ngroup <= 9) {
return set1.slice(0, ngroup);
}
return cat20.slice(0, ngroup);
} else {
if (ngroup === 1) {
return ["#bebebe"];
}
if (ngroup === 2) {
return ["lightpink", "lightblue"];
}
if (ngroup <= 9) {
return pastel1.slice(0, ngroup);
}
return pastel20.slice(0, ngroup);
}
}; // expand element/array (e.g., of colors) to a given length
// single elment -> array, then repeated to length n
d3panels.expand2vector = function (input, n) {
var i;
if (input == null) {
return input; // return null if null
}
if (Array.isArray(input) && input.length >= n) {
return input;
}
if (!Array.isArray(input)) {
input = [input];
}
if (input.length > 1 && n > 1) {
input = function () {
var results;
results = [];
for (i in d3.range(n)) {
results.push(input[i % input.length]);
}
return results;
}();
}
if (input.length === 1 && n > 1) {
input = function () {
var results;
results = [];
for (i in d3.range(n)) {
results.push(input[0]);
}
return results;
}();
}
return input;
}; // median of a vector
d3panels.median = function (x) {
var n, xv;
if (x == null) {
return null;
}
x = function () {
var k, len, results;
results = [];
for (k = 0, len = x.length; k < len; k++) {
xv = x[k];
if (xv != null) {
results.push(xv);
}
}
return results;
}();
n = x.length;
if (!(n > 0)) {
return null;
}
x.sort(function (a, b) {
return a - b;
});
if (n % 2 === 1) {
return x[(n - 1) / 2];
}
return (x[n / 2] + x[n / 2 - 1]) / 2;
}; // pad a vector to left and right
d3panels.pad_vector = function (x) {
var pad = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
// x should be sorted
if (pad == null) {
return [x[0] - (x[1] - x[0])].concat(x).concat([x[x.length - 1] + (x[x.length - 1] - x[x.length - 2])]);
}
return [x[0] - pad].concat(x).concat(x[x.length - 1] + pad);
}; // calculate midpoints
d3panels.calc_midpoints = function (x) {
// x should be sorted
var i, k, ref, results;
results = [];
for (i = k = 0, ref = x.length - 2; 0 <= ref ? k <= ref : k >= ref; i = 0 <= ref ? ++k : --k) {
results.push((x[i] + x[i + 1]) / 2);
}
return results;
}; // calc cell rectangles (left, right, top, bottom)
d3panels.calc_cell_rect = function (cells, xmid, ymid) {
var bottom, cell, k, left, len, results, right, top;
results = [];
for (k = 0, len = cells.length; k < len; k++) {
cell = cells[k];
left = xmid[cell.xindex];
right = xmid[1 + cell.xindex];
top = ymid[cell.yindex];
bottom = ymid[1 + cell.yindex];
cell.left = d3.min([left, right]);
cell.width = Math.abs(right - left);
cell.top = d3.min([top, bottom]);
results.push(cell.height = Math.abs(bottom - top));
}
return results;
}; // calc chr cell rectangles (left, right, top, bottom)
d3panels.calc_chrcell_rect = function (cells, xmid, ymid) {
var bottom, cell, k, left, len, results, right, top;
results = [];
for (k = 0, len = cells.length; k < len; k++) {
cell = cells[k];
left = xmid[cell.chr][cell.posindex];
right = xmid[cell.chr][1 + cell.posindex];
top = ymid[cell.lodindex];
bottom = ymid[1 + cell.lodindex];
cell.left = d3.min([left, right]);
cell.width = Math.abs(right - left);
cell.top = d3.min([top, bottom]);
results.push(cell.height = Math.abs(bottom - top));
}
return results;
}; // calc chr cell rectangles (left, right, top, bottom)
d3panels.calc_2dchrcell_rect = function (cells, xmid, ymid) {
var bottom, cell, k, left, len, results, right, top;
results = [];
for (k = 0, len = cells.length; k < len; k++) {
cell = cells[k];
left = xmid[cell.chrx][cell.xindexByChr];
right = xmid[cell.chrx][1 + cell.xindexByChr];
top = ymid[cell.chry][cell.yindexByChr];
bottom = ymid[cell.chry][1 + cell.yindexByChr];
cell.left = d3.min([left, right]);
cell.width = Math.abs(right - left);
cell.top = d3.min([top, bottom]);
results.push(cell.height = Math.abs(bottom - top));
}
return results;
}; // maximum difference between adjacent values in a vector
// [NO LONGER USED]
d3panels.maxdiff = function (x) {
var d, i, k, ref, result;
if (x.length < 2) {
return null;
}
result = x[1] - x[0];
if (x.length < 3) {
return result;
}
for (i = k = 2, ref = x.length; 2 <= ref ? k < ref : k > ref; i = 2 <= ref ? ++k : --k) {
d = x[i] - x[i - 1];
if (d > result) {
result = d;
}
}
return result;
}; // matrix extent, min, max, and max of absolute values
d3panels.matrixMin = function (mat) {
var i, j, result;
result = mat[0][0];
for (i in mat) {
for (j in mat[i]) {
if (!(result != null) || result > mat[i][j] && mat[i][j] != null) {
result = mat[i][j];
}
}
}
return result;
};
d3panels.matrixMax = function (mat) {
var i, j, result;
result = mat[0][0];
for (i in mat) {
for (j in mat[i]) {
if (!(result != null) || result < mat[i][j] && mat[i][j] != null) {
result = mat[i][j];
}
}
}
return result;
};
d3panels.matrixMaxAbs = function (mat) {
var i, j, result;
result = Math.abs(mat[0][0]);
for (i in mat) {
for (j in mat[i]) {
if (!(result != null) || result < Math.abs(mat[i][j]) && mat[i][j] != null) {
result = Math.abs(mat[i][j]);
}
}
}
return result;
};
d3panels.matrixExtent = function (mat) {
return [d3panels.matrixMin(mat), d3panels.matrixMax(mat)];
}; // force an object to be an array (rather than a scalar)
d3panels.forceAsArray = function (x) {
if (x == null) {
return x; // if null, return null
}
if (Array.isArray(x)) {
return x;
}
return [x];
}; // any values in vec that appear in missing are made null
d3panels.missing2null = function (vec) {
var missingvalues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['NA', ''];
return vec.map(function (value) {
if (missingvalues.indexOf(value) > -1) {
return null;
} else {
return value;
}
});
}; // display error at top of page
d3panels.displayError = function (message) {
var divid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var div;
div = "div.error";
if (divid != null) {
div += "#" + divid;
}
if (d3.select(div).empty()) {
// no errors yet
d3.select("body").insert("div", ":first-child").attr("class", "error");
}
return d3.select(div).append("p").text(message);
}; // sum values in an array
d3panels.sumArray = function (vec) {
var x;
vec = function () {
var k, len, results;
results = [];
for (k = 0, len = vec.length; k < len; k++) {
x = vec[k];
if (x != null) {
results.push(x);
}
}
return results;
}();
if (!(vec.length > 0)) {
return null;
}
return vec.reduce(function (a, b) {
return a * 1 + b * 1;
});
}; // calculate cross-tabulation
d3panels.calc_crosstab = function (data) {
var col, cs, i, k, l, ncol, nrow, ref, ref1, result, row, rs;
nrow = data.ycat.length;
ncol = data.xcat.length;
result = function () {
var k, ref, results; // matrix of 0's
results = [];
for (row = k = 0, ref = nrow; 0 <= ref ? k <= ref : k >= ref; row = 0 <= ref ? ++k : --k) {
results.push(function () {
var l, ref1, results1;
results1 = [];
for (col = l = 0, ref1 = ncol; 0 <= ref1 ? l <= ref1 : l >= ref1; col = 0 <= ref1 ? ++l : --l) {
results1.push(0);
}
return results1;
}());
}
return results;
}(); // count things up
for (i in data.x) {
result[data.y[i]][data.x[i]] += 1;
} // row and column sums
rs = d3panels.rowSums(result);
cs = d3panels.colSums(result); // fill in column sums
for (i = k = 0, ref = ncol; 0 <= ref ? k < ref : k > ref; i = 0 <= ref ? ++k : --k) {
result[nrow][i] = cs[i];
} // fill in row sums
for (i = l = 0, ref1 = nrow; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) {
result[i][ncol] = rs[i];
} // fill in total
result[nrow][ncol] = d3panels.sumArray(rs);
return result;
}; // rowSums: the sums for each row
d3panels.rowSums = function (mat) {
var k, len, results, x;
results = [];
for (k = 0, len = mat.length; k < len; k++) {
x = mat[k];
results.push(d3panels.sumArray(x));
}
return results;
}; // transpose: matrix transpose
d3panels.transpose = function (mat) {
var i, j, k, ref, results;
results = [];
for (j = k = 0, ref = mat[0].length; 0 <= ref ? k < ref : k > ref; j = 0 <= ref ? ++k : --k) {
results.push(function () {
var l, ref1, results1;
results1 = [];
for (i = l = 0, ref1 = mat.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) {
results1.push(mat[i][j]);
}
return results1;
}());
}
return results;
}; // colSums = the sums for each column
d3panels.colSums = function (mat) {
return d3panels.rowSums(d3panels.transpose(mat));
}; // log base 2
d3panels.log2 = function (x) {
if (x == null) {
return x;
}
return Math.log(x) / Math.log(2.0);
}; // log base 10
d3panels.log10 = function (x) {
if (x == null) {
return x;
}
return Math.log(x) / Math.log(10.0);
}; // absolute value, preserving nulls
d3panels.abs = function (x) {
if (x == null) {
return x;
}
return Math.abs(x);
}; // mean of y for each possible value of g
d3panels.mean_by_group = function (g, y) {
var i, means, n;
means = {};
n = {};
for (i in g) {
if (n[g[i]] != null) {
if (y[i] != null) {
means[g[i]] += y[i];
}
if (y[i] != null) {
n[g[i]] += 1;
}
} else {
if (y[i] != null) {
means[g[i]] = y[i];
}
if (y[i] != null) {
n[g[i]] = 1;
}
}
}
for (i in means) {
means[i] /= n[i];
}
return means;
}; // SD of y for each possible value of g
d3panels.sd_by_group = function (g, y) {
var dev, i, means, n, sds;
means = d3panels.mean_by_group(g, y);
sds = {};
n = {};
for (i in g) {
dev = y[i] - means[g[i]];
if (n[g[i]] != null) {
if (y[i] != null) {
sds[g[i]] += dev * dev;
}
if (y[i] != null) {
n[g[i]] += 1;
}
} else {
if (y[i] != null) {
sds[g[i]] = dev * dev;
}
if (y[i] != null) {
n[g[i]] = 1;
}
}
}
for (i in sds) {
sds[i] = n[i] < 2 ? null : Math.sqrt(sds[i] / (n[i] - 1));
}
return sds;
}; // count groups
d3panels.count_groups = function (g, y) {
var i, n;
n = {};
for (i in g) {
if (n[g[i]] != null) {
if (y[i] != null) {
n[g[i]] += 1;
}
} else {
if (y[i] != null) {
n[g[i]] = 1;
}
}
}
return n;
}; // CI for mean(y) for each possible value of g
// (as mean +/- m*SD)
d3panels.ci_by_group = function (g, y) {
var m = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
var ci, dev, i, means, n, sds;
means = d3panels.mean_by_group(g, y); // repeat the code for the SD, so I do two passes but not three
sds = {};
n = {};
for (i in g) {
dev = y[i] - means[g[i]];
if (n[g[i]] != null) {
if (y[i] != null) {
sds[g[i]] += dev * dev;
}
if (y[i] != null) {
n[g[i]] += 1;
}
} else {
if (y[i] != null) {
sds[g[i]] = dev * dev;
}
if (y[i] != null) {
n[g[i]] = 1;
}
}
}
for (i in sds) {
sds[i] = n[i] < 2 ? null : Math.sqrt(sds[i] / (n[i] - 1));
}
ci = {};
for (i in means) {
ci[i] = {
mean: means[i],
low: n[i] > 0 ? means[i] - m * sds[i] / Math.sqrt(n[i]) : means[i],
high: n[i] > 0 ? means[i] + m * sds[i] / Math.sqrt(n[i]) : means[i]
};
}
return ci;
}; // pad y-axis limits a bit
d3panels.pad_ylim = function (ylim) {
var p = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0.025;
var d;
d = ylim[1] - ylim[0];
return [ylim[0] - d * p, ylim[1] + d * p];
}; // add to data: unique chromosome names, chr starting values, chr end values
d3panels.add_chrname_start_end = function (data) {
var c, i, k, l, len, len1, ref, ref1, these_pos;
if (data.chrname == null) {
data.chrname = d3panels.unique(data.chr);
}
data.chrname = d3panels.forceAsArray(data.chrname);
if (data.chrstart == null) {
data.chrstart = [];
ref = data.chrname;
for (k = 0, len = ref.length; k < len; k++) {
c = ref[k];
these_pos = function () {
var results;
results = [];
for (i in data.chr) {
if (data.chr[i] === c) {
results.push(data.pos[i]);
}
}
return results;
}();
data.chrstart.push(d3.min(these_pos));
}
}
if (data.chrend == null) {
data.chrend = [];
ref1 = data.chrname;
for (l = 0, len1 = ref1.length; l < len1; l++) {
c = ref1[l];
these_pos = function () {
var results;
results = [];
for (i in data.chr) {
if (data.chr[i] === c) {
results.push(data.pos[i]);
}
}
return results;
}();
data.chrend.push(d3.max(these_pos));
}
}
data.start = d3panels.forceAsArray(data.start);
data.end = d3panels.forceAsArray(data.end);
return data;
}; // calculate breakpoints in prepara
d3panels.calc_breaks = function (number, low, high) {
var d, i, results;
if (low >= high) {
d3panels.displayError("calc_breaks: should have low < high");
if (low > high) {
var _ref = [high, low];
low = _ref[0];
high = _ref[1];
}
if (low === high) {
low -= 0.5;
high += 0.5;
}
}
if (number < 2) {
d3panels.displayError("calc_breaks: number should be >= 2");
number = 2;
}
d = (high - low) / (number - 1);
results = [];
for (i in d3.range(number)) {
results.push(low + d * i);
}
return results;
}; // calculate frequencies for histogram
d3panels.calc_freq = function (values, breaks) {
var return_counts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var br, i, k, len, n, ref, result, v, z; // clone and sort
v = values.slice(0);
v.sort(function (a, b) {
return +a - b;
});
br = breaks.slice(0);
br.sort(function (a, b) {
return +a - b;
});
br[0] -= 1e-6;
br[br.length - 1] += 1e-6;
result = function () {
var k, len, ref, results;
ref = d3.range(br.length - 1);
results = [];
for (k = 0, len = ref.length; k < len; k++) {
i = ref[k];
results.push(0);
}
return results;
}();
n = v.length;
v = function () {
var k, len, results;
results = [];
for (k = 0, len = v.length; k < len; k++) {
z = v[k];