-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigwigTrackDom.js
835 lines (780 loc) · 34.3 KB
/
bigwigTrackDom.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
/**
* @license
* Copyright 2017-2019 The Regents of the University of California.
* All Rights Reserved.
*
* Created by Xiaoyi Cao
* Department of Bioengineering
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ### Overview
*
* `<bigwig-track-dom>` is the Web Component to display BigWig tracks. It's part
* of `GIVe.BigWigTrack` object and is used to visualize data from the
* `GIVe.BigWigTrack` object.
*
* ### Visibility level
*
* ### References
* * [`GIVe.TrackObject`](../index.html) for details on tracks in
* general;
* * [`GIVe.BigWigTrack`](./bed-track/index.html) for details on BED
* track implementation;
* * [Polymer element registration](https://www.polymer-project.org/1.0/docs/devguide/registering-elements)
* for Polymer Element guide, including lifecycles, properties, methods and others.
*/
var GIVe = (function (give) {
'use strict'
class BigWigTrackDom extends give.TrackDom {
constructor (track, props) {
super(...arguments)
/**
* A simple calculation of where the top and bottom of where to draw the track is.
*/
this.drawingBoundary = {
top: this.margin,
bottom: this.height - this.margin
}
this.labelMarginRight = this.margin + this.textRightPadding
if (!(this.windowMax > this.windowMin)) {
give._verbConsole.warn(
'Window Range Error: windowMax = ' +
this.windowMax + '; windowMin = ' + this.windowMin
)
give.fireSignal('give-warning',
{
msg: 'Window Range Error: windowMax = ' +
this.windowMax + '; windowMin = ' + this.windowMin
}, null, this)
this.windowMax = this.constructor.defaultProperties.windowMax
this.windowMin = this.constructor.defaultProperties.windowMin
}
this.dataPoints = []
}
_initProperties (props) {
super._initProperties(props)
/**
* The overall margin for displaying, in px.
* @type {number}
*/
this.margin = this._initPropertyItem(
'margin', props, 'margin', 'float')
/**
* The color index for the text, should be an integer
* @type {number}
*/
this.textColorIndex = this._initPropertyItem(
'textColorIndex', props, 'textColorIndex', 'integer')
/**
* The color index for overflow (orange), should be an integer
* @type {number}
*/
this.overflowColorIndex = this._initPropertyItem(
'overflowColorIndex', props, 'overflowColorIndex', 'integer')
/**
* The width of the line that is used for the overflow
* @type {number}
*/
this.overflowStrokeWidth = this._initPropertyItem(
'overflowStrokeWidth', props, 'overflowStrokeWidth', 'integer')
/**
* The size of the text used in axis labels
* @type {number}
*/
this.scaleTextSize = this._initPropertyItem(
'scaleTextSize', props, 'scaleTextSize', 'integer')
/**
* Minimum resolution required per pixel, used to determine how fine
* the data should be retrieved
* @type {number}
*/
this.minResolutionPerPixel = this._initPropertyItem(
'minResolutionPerPixel', props, 'minResolutionPerPixel', 'integer')
/**
* The upper bound for the track display. Any values above this will be
* displayed as a overflow line.
* @type {number}
*/
this.windowMax = this._initPropertyItem(
'windowMax', props, 'windowMax', 'float')
/**
* The lower bound for the track display. One might think that this is
* obviously zero, but in some cases there are actually negative intensity
* values at a point.
* @type {number}
*/
this.windowMin = this._initPropertyItem(
'windowMin', props, 'windowMin', 'float')
/**
* The length of ticks in axes.
* @type {number}
*/
this.scaleTickLength = this._initPropertyItem(
'scaleTickLength', props, 'scaleTickLength', 'float')
/**
* Whether or not to automatically scale the window bounds according to
* the data displayed.
*/
this.autoScale = this._initPropertyItem(
'autoScale', props, 'autoScale', 'boolean')
/**
* Whether or not to include zero when doing auto scaling.
* @type {boolean}
*/
this.includeZero = this._initPropertyItem(
'includeZero', props, 'includeZero', 'boolean')
/**
* When auto scaling, the upper bound will be chosen at this proportion of
* data. Everything above this proportion will be put above the upper
* display boundary.
*
* This takes a float value between 0 and 1, where 0 means no data will be
* put out of the boundary.
* @type {number}
*/
this.upperProportion = this._initPropertyItem(
'upperProportion', props, 'upperProportion', 'float')
/**
* When auto scaling, the lower bound will be chosen at this proportion of
* data. Everything below this proportion will be put below the lower
* display boundary.
*
* This takes a float value between 0 and 1, where 0 means no data will be
* put out of the boundary.
* @type {number}
*/
this.lowerProportion = this._initPropertyItem(
'lowerProportion', props, 'lowerProportion', 'float')
/**
* Number of digits to show on axes labels, should be an integer
* @type {number}
*/
this.numOfDigits = this._initPropertyItem(
'numOfDigits', props, 'numOfDigits', 'integer')
/**
* Half width of sliding window, in pixel. Should be an integer.
*
* For example, to use a 5-pixel sliding window, set this value to 2.
* @type {number}
*/
this.slidingWindowHalfWidth = this._initPropertyItem(
'slidingWindowHalfWidth', props, 'slidingWindowHalfWidth', 'integer')
}
_getResolution (newVWindow, index) {
let width =
(typeof this._pendingWindowWidth === 'number' &&
this._pendingWindowWidth > 0)
? this._pendingWindowWidth : this.windowWidth
return Math.max(Math.floor(
newVWindow.length / width /
this.minResolutionPerPixel
), 1)
}
get windowRange () {
return this.windowMax - this.windowMin
}
_pushSingleRawPointEntry (
dataPointArray, chrRange, dataEntry
) {
// start is the end of the last entry in dataPointArray
// or chrRange.start
let start = chrRange.start
let lastElem = dataPointArray[dataPointArray.length - 1]
if (lastElem && lastElem.end) {
// lastElem is a ChromRegion object, may need to add some data
if (lastElem.overlaps(dataEntry) > 0) {
lastElem.data.value += dataEntry.data.value *
lastElem.overlaps(dataEntry) / lastElem.length
}
start = lastElem.end
}
if (start < dataEntry.end) {
let span = Math.max(parseInt(chrRange.resolution) || 1, 1)
let chrSpan = new give.ChromRegion({
chr: chrRange.chr,
start: start,
end: start + span
})
while (chrSpan.start < dataEntry.end &&
chrSpan.start < chrRange.end
) {
// This will insert 0 in the gaps, split value in overlaps
// and have full value afterwards
let newData = chrSpan.clone()
newData.data = {
value: dataEntry.data.value * newData.overlaps(dataEntry) /
newData.length
}
dataPointArray.push(newData)
chrSpan.move(span)
}
}
}
_generateRawPointsList (vwindow) {
let rawDataPoints = []
// First populate the raw data points
this.parent.getData(vwindow.chr).traverse(vwindow,
entry => this._pushSingleRawPointEntry(rawDataPoints, vwindow, entry),
null, false)
// Insert more zeroes to the end
let lastEnd = rawDataPoints.length
? rawDataPoints[rawDataPoints.length - 1].end
: vwindow.start
while (lastEnd < vwindow.end) {
rawDataPoints.push(new give.ChromRegion({
chr: vwindow.chr,
start: lastEnd,
end: lastEnd + vwindow.resolution
}, this.parent.ref, { data: { value: 0.0 } }))
lastEnd += vwindow.resolution
}
return rawDataPoints
}
_generateSmoothedPoints () {
// Get the smoothing points
let vwindow = this.viewWindow
let slidingWindowSpan = this._getResolution(vwindow) *
this.slidingWindowHalfWidth * this.minResolutionPerPixel
let halfWindowIndexLength = Math.ceil(this.slidingWindowHalfWidth *
this.minResolutionPerPixel / 2)
let extendedWindow = vwindow.getExtension(slidingWindowSpan)
.clipRegion(this.parent.ref)
extendedWindow.resolution = this._getResolution(vwindow)
// Get the raw points list first
let rawDataPoints = this._generateRawPointsList(extendedWindow)
// Then smooth out with this.slidingWindowHalfWidth
// Needs to check if the borders do not have enough points
// Prepare for the sliding sumData
let smoothedPointsList = []
for (let currIndex = 0; currIndex < rawDataPoints.length; currIndex++) {
let slidingSum = 0
let windowElemCount = 0
for (let i = -halfWindowIndexLength; i <= halfWindowIndexLength; i++) {
if (rawDataPoints[currIndex + i]) {
slidingSum += rawDataPoints[currIndex + i].data.value
windowElemCount++
}
}
smoothedPointsList.push(new give.ChromRegion({
chr: vwindow.chr,
start: rawDataPoints[currIndex].start,
end: rawDataPoints[currIndex].end
}, this.parent.ref, { data: { value: slidingSum / windowElemCount } }))
}
return smoothedPointsList
}
_findAutoScale () {
this.windowMax = this.includeZero ? 0 : Number.NEGATIVE_INFINITY
this.windowMin = this.includeZero ? 0 : Number.POSITIVE_INFINITY
try {
let extremities = give._findQuantile(this.dataPoints.map(
function (dataEntry) {
return dataEntry.data.value
}, this), this.upperProportion, this.lowerProportion)
if (this.windowMax < extremities.upper) {
this.windowMax = extremities.upper
}
if (this.windowMin > extremities.lower) {
this.windowMin = extremities.lower
}
} catch (ignore) {
}
if (!(this.windowMax > this.windowMin)) {
this.windowMax = 1
this.windowMin = 0
}
}
drawData () {
// default drawing mode is 0 (sliding window smoothing)
let mode = parseInt(give.getParameterByName('mode') || 0)
// draw the given point with height determined by signal strength
this.clear()
let vwindow = this._mainSvg.viewWindow
if (this.parent.getData(vwindow.chr)) {
this.dataPoints = this._generateSmoothedPoints()
if (this.autoScale) {
this._findAutoScale()
}
if (mode === 0) {
this.drawPeak(this.dataPoints)
}
if (mode === 1) {
this.bSpline()
}
if ((mode === 2) || (mode === 3)) {
this.newBCurve()
}
}
this.drawScale()
}
/**
* anonymous function - transforms a signal strength into a y coordinate on the svg.
*
* @param {type} signal signal strength
* @param {type} flags has two booleans
* @return {type} returns the y coordinate
*/
transformYCoordinate (signal, flags) {
// transform Y coordinate from signal with this.windowMin and
// this.windowMax
// notice that if the value exceeds either boundary,
// it will return the Y value at the boundary, but will mark
// flags.EXCEED_MAX or flags.EXCEED_MIN as true
// (flags needs to be an object from the caller to receive such values)
flags = flags || {}
delete flags.EXCEED_MIN
delete flags.EXCEED_MAX // clear flags first
if (signal > this.windowMax) {
flags.EXCEED_MAX = true
return this.drawingBoundary.top
} else if (signal < this.windowMin) {
flags.EXCEED_MIN = true
return this.drawingBoundary.bottom
}
return (signal - this.windowMax) / (this.windowMin - this.windowMax) *
(this.drawingBoundary.bottom - this.drawingBoundary.top) +
this.drawingBoundary.top
}
/**
* Draws the horizontal thick, different colored lines where the values go
* further than the window ranges.
*
* @param {type} overflows the list of overflow areas
* @return {type} description
*/
drawOverflowLines (overflows) {
let drawOverflowRectFunc = (y, region) => {
this.drawRectangle(region,
this.constructor.colorSet[this.overflowColorIndex],
y, this.overflowStrokeWidth, this._mainSvg)
}
overflows.exceedMax.forEach(region => drawOverflowRectFunc(0, region))
overflows.exceedMin.forEach(region => drawOverflowRectFunc(
this.height - this.overflowStrokeWidth, region))
}
/**
* anonymous function - Adds a point from the dataEntry to the datapoints.
*
* @param {type} dataEntry holds many points, just not in the correct format (yet).
* @return {type} description
*/
addPoint (vwindowChr, dataEntry) {
// var x = dataEntry.start
// var y = dataEntry.end
// var z = dataEntry.data instanceof give.TrackObjectImpl._BigWigImpl._SummaryCtor
// ? dataEntry.data.sumData / dataEntry.length : dataEntry.data.value
// var vwindow = this._mainSvg.viewWindow
this.parent.data[vwindowChr].dataPoints.push(dataEntry)
}
// another function called draw splines? which converts the four bezier points to scale on the svg and draws them as a curve?
// use svg function
drawBCurve (bPoints) {
var svgToDraw = this._mainSvg
var BCurve = document.createElementNS(this.svgNS, 'path')
BCurve.setAttribute('d', 'M' + bPoints[0][0] + ' ' + bPoints[0][1] + ' C ' + bPoints[1][0] + ' ' + bPoints[1][1] + ' ' + bPoints[2][0] + ' ' + bPoints[2][1] + ' ' + bPoints[3][0] + ' ' + bPoints[3][1])// use c for the rest
BCurve.setAttribute('stroke', 'black')
BCurve.setAttribute('fill', 'transparent')
this.addElement(BCurve, svgToDraw)
}
drawDownLine (x, y) {
var svgToDraw = this._mainSvg
var DownLine = document.createElementNS(this.svgNS, 'line')
DownLine.setAttribute('x1', x)
DownLine.setAttribute('x2', x)
DownLine.setAttribute('y1', y)
DownLine.setAttribute('y2', 0)
DownLine.setAttribute('stroke', 'black')
this.addElement(DownLine, svgToDraw)
}
bSpline (points) {
// for each (four?) points: calculate the tripmid point things, store them as p1.t2 , p2.t1, p2.t2, p3.t1, p3.t2, p4.t1
// calculate the midpoints of each connected trimidpoint pair, store as p2.m, p3.m
// calculate input the points as bezier: p2.m, p2.t2, p3.t1, p3.m, with the middle two as control points.
// this should take points and bspline it? ToDo: create separate functions for regular average and bspline, to switch between them easily.
var vwindow = this._mainSvg.viewWindow
var windowToDraw = this._mainSvg.viewWindow
for (var i = 0; i < this.parent.data[vwindow.chr].dataPoints.length - 4; i++) { // calculate thirdpoints and put them in arrays
var p1 = this.parent.data[vwindow.chr].dataPoints[i]
var p2 = this.parent.data[vwindow.chr].dataPoints[i + 1]// reassign for easier coding
var p3 = this.parent.data[vwindow.chr].dataPoints[i + 2]
var p4 = this.parent.data[vwindow.chr].dataPoints[i + 3]
var broken = false
for (var j = i; j < i + 3; j++) {
if (this.parent.data[vwindow.chr].dataPoints[j].end !== this.parent.data[vwindow.chr].dataPoints[j + 1].start) {
broken = true// draw line down, might be drawn more than once
this.drawDownLine(this.transformXCoordinate({
chr: windowToDraw.chr, coor: (this.parent.data[vwindow.chr].dataPoints[j].start)
}, false), this.transformYCoordinate(this.parent.data[vwindow.chr].dataPoints[j].data.value))
this.drawDownLine(this.transformXCoordinate({
chr: windowToDraw.chr, coor: (this.parent.data[vwindow.chr].dataPoints[j + 1].start)
}, false), this.transformYCoordinate(this.parent.data[vwindow.chr].dataPoints[j + 1].data.value))
}
}
if (broken) {
continue
}
// p1.t2 = [(p1.start + 1 / 3 * (p2.start - p1.start)), (p1.value + 1 / 3 * (p2.value - p1.value))]
// draw lines between points, split lines into thirds
p2.t1 = [(p1.start + 2 / 3 * (p2.start - p1.start)), (p1.data.value + 2 / 3 * (p2.data.value - p1.data.value))]
p2.t2 = [(p2.start + 1 / 3 * (p3.start - p2.start)), (p2.data.value + 1 / 3 * (p3.data.value - p2.data.value))]
p3.t1 = [(p2.start + 2 / 3 * (p3.start - p2.start)), (p2.data.value + 2 / 3 * (p3.data.value - p2.data.value))]
p3.t2 = [(p3.start + 1 / 3 * (p4.start - p3.start)), (p3.data.value + 1 / 3 * (p4.data.value - p3.data.value))]
// p4.t1 = [(p3.start + 2 / 3 * (p4.start - p3.start)), (p4.value + 2 / 3 * (p4.value - p3.value))]
p2.midPoint = [(p2.t1[0] + p2.t2[0]) / 2, (p2.t1[1] + p2.t2[1]) / 2]
p3.midPoint = [(p3.t1[0] + p3.t2[0]) / 2, (p3.t1[1] + p3.t2[1]) / 2]
var bPoints = [p2.midPoint, p2.t2, p3.t1, p3.midPoint]
p2.adjustedMidPoint = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p2.midPoint[0]) }, false), this.transformYCoordinate(p2.midPoint[1])]
p2.adjustedt2 = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p2.t2[0]) }, false), this.transformYCoordinate(p2.t2[1])]
p3.adjustedt1 = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p3.t1[0]) }, false), this.transformYCoordinate(p3.t1[1])]
p3.adjustedMidPoint = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p3.midPoint[0]) }, false), this.transformYCoordinate(p3.midPoint[1])]
var adjustedBPoints = [p2.adjustedMidPoint, p2.adjustedt2, p3.adjustedt1, p3.adjustedMidPoint]
this.drawBCurve(adjustedBPoints)
}
// this.transformXCoordinate({chr: windowToDraw.chr, coor: (start + end) / 2}, false)
// drawSpline(points)
}
newBCurve () { // this one fills it too. note that some zooms have less than four points to workwith.
var vwindow = this._mainSvg.viewWindow
var svgToDraw = this._mainSvg
var curveStart = 0
var zeroCounter = 0 // if its is equal to 3 then consider new polygon
var mode = parseInt(give.getParameterByName('mode') || '2')
if (mode == 2) {
for (var i = 0; i < this.parent.data[vwindow.chr].dataPoints.length - 2; i++) {
if (this.parent.data[vwindow.chr].dataPoints[i].end != this.parent.data[vwindow.chr].dataPoints[i + 1].start) {
this.newDrawBCurve(curveStart, i + 1)
curveStart = i + 1// will add in 0s to datapoints, need to break them if three in a row, also move to midpoinnt vs getStart
}
}
}
if (mode === 3) {
for (var i = 0; i < this.parent.data[vwindow.chr].dataPoints.length - 2; i++) { // this is with 0's.
if (this.parent.data[vwindow.chr].dataPoints[i].data.value == 0) {
zeroCounter += 1
}
if (zeroCounter >= 3) {
this.newDrawBCurve(curveStart, i + 1)
curveStart = i + 1// will add in 0s to datapoints, need to break them if three in a row, also move to midpoinnt vs getStart
zeroCounter = 0
}
}
}
}
calculateSvgCurvePoints (i) {
var vwindow = this._mainSvg.viewWindow
var svgToDraw = this._mainSvg
var windowToDraw = this._mainSvg.viewWindow
var p1 = this.parent.data[vwindow.chr].dataPoints[i]
var p2 = this.parent.data[vwindow.chr].dataPoints[i + 1]// reassign for easier coding
var p3 = this.parent.data[vwindow.chr].dataPoints[i + 2]
var p4 = this.parent.data[vwindow.chr].dataPoints[i + 3]
// p1.t2 = [(p1.start + 1 / 3 * (p2.start - p1.start)), (p1.value + 1 / 3 * (p2.value - p1.value))]
// draw lines between points, split lines into thirds
p2.t1 = [(p1.start + 2 / 3 * (p2.start - p1.start)), (p1.data.value + 2 / 3 * (p2.data.value - p1.data.value))]
p2.t2 = [(p2.start + 1 / 3 * (p3.start - p2.start)), (p2.data.value + 1 / 3 * (p3.data.value - p2.data.value))]
p3.t1 = [(p2.start + 2 / 3 * (p3.start - p2.start)), (p2.data.value + 2 / 3 * (p3.data.value - p2.data.value))]
p3.t2 = [(p3.start + 1 / 3 * (p4.start - p3.start)), (p3.data.value + 1 / 3 * (p4.data.value - p3.data.value))]
// p4.t1 = [(p3.start + 2 / 3 * (p4.start - p3.start)), (p4.value + 2 / 3 * (p4.value - p3.value))]
p2.midPoint = [(p2.t1[0] + p2.t2[0]) / 2, (p2.t1[1] + p2.t2[1]) / 2]
p3.midPoint = [(p3.t1[0] + p3.t2[0]) / 2, (p3.t1[1] + p3.t2[1]) / 2]
var bPoints = [p2.midPoint, p2.t2, p3.t1, p3.midPoint]
p2.adjustedMidPoint = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p2.midPoint[0]) }, false), this.transformYCoordinate(p2.midPoint[1])]
p2.adjustedt2 = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p2.t2[0]) }, false), this.transformYCoordinate(p2.t2[1])]
p3.adjustedt1 = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p3.t1[0]) }, false), this.transformYCoordinate(p3.t1[1])]
p3.adjustedMidPoint = [this.transformXCoordinate({chr: windowToDraw.chr, coor: (p3.midPoint[0]) }, false), this.transformYCoordinate(p3.midPoint[1])]
var adjustedBPoints = [p2.adjustedMidPoint, p2.adjustedt2, p3.adjustedt1, p3.adjustedMidPoint]
return adjustedBPoints
}
newDrawBCurve (start, end) {
var vwindow = this._mainSvg.viewWindow
var svgToDraw = this._mainSvg
var windowToDraw = this._mainSvg.viewWindow
var BCurve = document.createElementNS(this.svgNS, 'path')
var d = 'M'
BCurve.setAttribute('stroke', 'black')
BCurve.setAttribute('fill', 'blue')
// BCurve.setAttribute("d","M")
var SvgPoints = this.calculateSvgCurvePoints(start)
// Start with 0
d = d + SvgPoints[0][0] + ' ' + this.transformYCoordinate(0) + ' C ' + SvgPoints[1][0] + ' ' + SvgPoints[1][1] + ' ' + SvgPoints[2][0] + ' ' + SvgPoints[2][1] + ' ' + SvgPoints[3][0] + ' ' + SvgPoints[3][1]
for (var i = start + 1; i < end - 3; i++) { // sytnax: M10 10 C 10 10 10 10 10 10 C 10 10 10 10 10 10 ...
SvgPoints = this.calculateSvgCurvePoints(i)
d = d + ' C ' + SvgPoints[1][0] + ' ' + SvgPoints[1][1] + ' ' + SvgPoints[2][0] + ' ' + SvgPoints[2][1] + ' ' + SvgPoints[3][0] + ' ' + SvgPoints[3][1]
}
SvgPoints = this.calculateSvgCurvePoints(end - 3)
// end with 0
d = d + ' C ' + SvgPoints[1][0] + ' ' + SvgPoints[1][1] + ' ' + SvgPoints[2][0] + ' ' + SvgPoints[2][1] + ' ' + SvgPoints[3][0] + ' ' + this.transformYCoordinate(0)
BCurve.setAttribute('d', d)
this.addElement(BCurve, svgToDraw)
}
/**
* anonymous function - Takes a specified amount of points (by slidingWindowHalfWidth) around the point and assigns the average y value of all those points to the middle point's display.
* An even amount of points will mean taking an extra point to the left in the average.
*
* @return {type} description
*/
averagePoints () {
var splitAmount = Math.ceil((this.slidingWindowHalfWidth - 1) / 2)
var vwindow = this._mainSvg.viewWindow
this.parent.data[vwindow.chr].averageDataPoints = []
for (var i = splitAmount; i < this.parent.data[vwindow.chr].dataPoints.length - splitAmount - 1; i++) {
var temp = this.parent.data[vwindow.chr].dataPoints[i]
var sum = 0
for (var j = 0; j < this.slidingWindowHalfWidth; j++) {
sum += this.parent.data[vwindow.chr].dataPoints[i - splitAmount + j].data.value
}
temp.data.value = parseInt(sum / this.slidingWindowHalfWidth + 0.5)
this.parent.data[vwindow.chr].averageDataPoints.push(temp)
}
}
_pushCoorToPolygon (polygon, vwindow, xCoor, yValue, flags, replace) {
if (!replace) {
polygon.points.push(this.transformXCoordinate({
chr: vwindow.chr,
coor: xCoor
}, false) + ',' + this.transformYCoordinate(yValue, flags))
} else {
polygon.points[0] = this.transformXCoordinate({
chr: vwindow.chr,
coor: xCoor
}, false) + ',' + this.transformYCoordinate(yValue, flags)
}
polygon.lastX = xCoor
}
_endPolygon (polygon) {
var svgToDraw = this._mainSvg
this.createRawPolygon(polygon.points, {
class: 'wiggleShapes',
fill: this.constructor.colorSet[this.forecolorIndex],
stroke: this.constructor.colorSet[this.forecolorIndex]
}, svgToDraw)
this.drawOverflowLines(polygon.overflows)
polygon.points = []
polygon.overflows = {exceedMax: [], exceedMin: []}
}
/**
* drawPeak - Draws the given points onto the svg window.
*
* @param {type} currPolygon description
* @param {type} dataEntry description
* @return {type} description
*/
drawPeak (dataPoints) {
let svgToDraw = this._mainSvg
let windowToDraw = svgToDraw.viewWindow
let currPolygon = { points: [], overflows: {exceedMax: [], exceedMin: []} }
for (let i = 0; i < dataPoints.length; i++) {
let start = dataPoints[i].start
let end = dataPoints[i].end
let yValue = dataPoints[i].data.value
// first decide whether only one point will be pushed
let middlePointOnly = (this.transformXCoordinate({chr: windowToDraw.chr, coor: end}) -
this.transformXCoordinate({chr: windowToDraw.chr, coor: start}) < 1)
if (yValue !== 0) {
// this point (two points) should be pushed to currPolygon
if (currPolygon.points.length === 0) {
// no point yet, push a zero first
this._pushCoorToPolygon(currPolygon, windowToDraw,
(middlePointOnly ? (start + end) / 2 : start), 0)
}
let flags = {}
if (middlePointOnly) {
this._pushCoorToPolygon(currPolygon, windowToDraw,
(start + end) / 2, yValue, flags)
} else {
this._pushCoorToPolygon(currPolygon, windowToDraw,
start, yValue, flags)
this._pushCoorToPolygon(currPolygon, windowToDraw,
end, yValue, flags)
}
// process overflows
if (flags.EXCEED_MIN) {
let lastExceed = currPolygon.overflows.exceedMin[currPolygon.overflows.exceedMin.length - 1]
if (!lastExceed || !lastExceed.concat(dataPoints[i])) {
currPolygon.overflows.exceedMin.push(dataPoints[i].clone())
}
} else if (flags.EXCEED_MAX) {
let lastExceed = currPolygon.overflows.exceedMax[currPolygon.overflows.exceedMax.length - 1]
if (!lastExceed || !lastExceed.concat(dataPoints[i])) {
currPolygon.overflows.exceedMax.push(dataPoints[i].clone())
}
}
} else { // yValue === 0
if (currPolygon.points.length > 1) {
// there is an old polygon, finish this first
this._pushCoorToPolygon(currPolygon, windowToDraw,
(middlePointOnly ? (start + end) / 2 : start), 0)
this._endPolygon(currPolygon)
}
// then put the zero in currPolygon
// (replace the previous one if exists)
this._pushCoorToPolygon(currPolygon, windowToDraw,
(middlePointOnly ? (start + end) / 2 : end), 0, null, true)
}
} // end iterating dataPoints
if (currPolygon.points.length > 1) {
this._pushCoorToPolygon(currPolygon, windowToDraw,
currPolygon.lastX, 0)
this._endPolygon(currPolygon)
}
}
_drawPeakFromBedGraph (currPolygon, dataEntry) {
let svgToDraw = this._mainSvg
let windowToDraw = svgToDraw.viewWindow
let flags = {}
currPolygon = currPolygon || {}
if (!currPolygon.hasOwnProperty('points')) {
currPolygon.points = []
}
let start = dataEntry ? Math.max(dataEntry.start, windowToDraw.start) : windowToDraw.end + 1
let end = dataEntry ? Math.min(dataEntry.end, windowToDraw.end) : windowToDraw.end + 2
if (start < end) {
if (currPolygon.points.length > 0) {
// old polygon is there
if (currPolygon.lastX < start) {
// it's a separate polygon
currPolygon.points.push(
this.transformXCoordinate({chr: windowToDraw.chr, coor: currPolygon.lastX}, false) +
',' + this.transformYCoordinate(0))
this.createRawPolygon(currPolygon.points, {class: 'wiggleShapes',
fill: this.constructor.rgbToHex(this.constructor.colorSet[0]),
stroke: this.constructor.rgbToHex(this.constructor.colorSet[0])
}, svgToDraw)
this.drawOverflowLines(currPolygon.overflows)
currPolygon.points = []
}
}
if (dataEntry && dataEntry.chr === windowToDraw.chr) {
let yvalue = dataEntry.data instanceof give.TrackObjectImpl._BigWigImpl._SummaryCtor
? dataEntry.data.sumData / dataEntry.length : dataEntry.data.value
if (currPolygon.points.length <= 0) {
// start a new polygon, push the (x, 0) point of this segment
currPolygon.points.push(
this.transformXCoordinate({chr: windowToDraw.chr, coor: start}, false) +
',' + this.transformYCoordinate(0))
currPolygon.overflows = {exceedMax: [], exceedMin: []}
}
// then push the top two points
if (/* this.transformXCoordinate({chr: windowToDraw.chr, coor: end}) -
this.transformXCoordinate({chr: windowToDraw.chr, coor: start}) < 1 */true) {
// both are within one pixel
currPolygon.points.push(
this.transformXCoordinate({chr: windowToDraw.chr, coor: (start + end) / 2}, false) +
',' + this.transformYCoordinate(yvalue, flags))
} else {
currPolygon.points.push(
this.transformXCoordinate({chr: windowToDraw.chr, coor: start}, false) +
',' + this.transformYCoordinate(yvalue, flags))
currPolygon.points.push(
this.transformXCoordinate({chr: windowToDraw.chr, coor: end}, false) +
',' + this.transformYCoordinate(yvalue, flags))
}
currPolygon.lastX = end
}
}
// process overflows
if (flags.EXCEED_MIN) {
let lastExceed = currPolygon.overflows.exceedMin[currPolygon.overflows.exceedMin.length - 1]
if (!lastExceed || !lastExceed.concat(dataEntry)) {
currPolygon.overflows.exceedMin.push(dataEntry.clone())
}
} else if (flags.EXCEED_MAX) {
let lastExceed = currPolygon.overflows.exceedMax[currPolygon.overflows.exceedMax.length - 1]
if (!lastExceed || !lastExceed.concat(dataEntry)) {
currPolygon.overflows.exceedMax.push(dataEntry.clone())
}
}
return true
}
/**
* drawScale - Draws the signal strength scale on the left side of the
* track.
*/
drawScale () {
if (this._textSvg) {
// first draw the lines
this.drawLine(
this.textMargin - this.margin, this.drawingBoundary.top,
this.textMargin - this.margin, this.drawingBoundary.bottom,
this.textColorIndex, this._textSvg)
this.drawLine(this.textMargin - this.margin - this.scaleTickLength,
this.drawingBoundary.top,
this.textMargin - this.margin, this.drawingBoundary.top,
this.textColorIndex, this._textSvg)
this.drawLine(this.textMargin - this.margin - this.scaleTickLength,
this.drawingBoundary.bottom,
this.textMargin - this.margin, this.drawingBoundary.bottom,
this.textColorIndex, this._textSvg)
// then the text
this.drawText(this.textMargin - this.margin -
this.scaleTickLength - this.textRightPadding,
this.drawingBoundary.top + this.textSize * 0.4,
give._maxDecimalDigits(this.windowMax,
this.numOfDigits).toString(),
'end', null, this._textSvg)
this.drawText(this.textMargin - this.margin -
this.scaleTickLength - this.textRightPadding,
this.drawingBoundary.bottom - this.textSize * 0.4,
give._maxDecimalDigits(this.windowMin,
this.numOfDigits).toString(),
'end', null, this._textSvg)
}
}
/**
* anonymous function - Sets the window range.
*
* @param {type} min description
* @param {type} max description
* @return {type} description
*/
setExtremes (min, max) {
if (max > min) {
this.windowMin = min
this.windowMax = max
this.autoScale = false
} else {} // there is an error
}
/**
* anonymous function - Automatically scales the window, taking the maximum and minimum signal strengths in the display window as the range extremes.
*
* @return {type} description
*/
autoWindow () {
this.autoScale = true
}
static get defaultProperties () {
return Object.assign(super.defaultProperties || {}, {
margin: 2,
textColorIndex: 0, // the color index for text
overflowColorIndex: 8, // the color index for overflow (red)
overflowStrokeWidth: 4,
scaleTextSize: 9,
minResolutionPerPixel: 1,
windowMax: 20,
windowMin: 0,
autoScale: true,
includeZero: true,
upperProportion: 0.1,
lowerProportion: 0.1,
numOfDigits: 2,
scaleTickLength: 5,
slidingWindowHalfWidth: 5,
forecolorIndex: 1
})
}
}
BigWigTrackDom.DEFAULT_HEIGHT = 80
give.BigWigTrackDom = BigWigTrackDom
return give
})(GIVe || {})