-
Notifications
You must be signed in to change notification settings - Fork 199
/
WiggleBase.js
372 lines (332 loc) · 13.5 KB
/
WiggleBase.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
define( [
'dojo/_base/declare',
'dojo/_base/array',
'dojo/on',
'JBrowse/View/Track/Canvas',
'JBrowse/View/Track/ExportMixin',
'JBrowse/Util',
'JBrowse/Digest/Crc32'
],
function( declare, array, on, CanvasTrack, ExportMixin, Util, Digest ) {
var Wiggle = declare( CanvasTrack, {
constructor: function( args ) {
this.store = args.store;
}
});
/**
* Mixin: JBrowse.View.Track.ExportMixin.
*/
dojo.safeMixin( Wiggle.prototype, ExportMixin );
/**
* @lends JBrowse.View.Track.Wiggle.prototype
*/
Wiggle.extend({
_defaultConfig: function() {
return { maxExportSpan: 500000 };
},
_getScaling: function( successCallback, errorCallback ) {
this.getRegionStats( this._getScalingRegion(), dojo.hitch(this, function( stats ) {
//calculate the scaling if necessary
var statsFingerprint = Digest.objectFingerprint( stats );
if( ! this.lastScaling || this.lastScaling._statsFingerprint != statsFingerprint ) {
try {
this.lastScaling = this._calculateScaling( stats );
successCallback( this.lastScaling );
} catch( e ) {
errorCallback(e);
}
} else {
successCallback( this.lastScaling );
}
}), errorCallback );
},
// TODO: implement more regions over which we can get the
// stats. currently over whole ref seq
_getScalingRegion: function() {
return dojo.clone( this.browser.getCurrentRefSeq() );
},
_calculateScaling: function( s ) {
// if either autoscale or scale is set to z_score, the other one should default to z_score
if( this.config.autoscale == 'z_score' && ! this.config.scale
|| this.config.scale == 'z_score' && !this.config.autoscale
) {
this.config.scale = 'z_score';
this.config.autoscale = 'z_score';
}
var z_score_bound = parseFloat( this.config.z_score_bound ) || 4;
var min = 'min_score' in this.config ? parseFloat( this.config.min_score ) :
(function() {
switch( this.config.autoscale ) {
case 'z_score':
return Math.max( -z_score_bound, (s.scoreMin-s.scoreMean) / s.scoreStdDev );
case 'global':
return s.scoreMin;;
case 'clipped_global':
default:
return Math.max( s.scoreMin, s.scoreMean - z_score_bound * s.scoreStdDev );
}
}).call(this);
var max = 'max_score' in this.config ? parseFloat( this.config.max_score ) :
(function() {
switch( this.config.autoscale ) {
case 'z_score':
return Math.min( z_score_bound, (s.scoreMax-s.scoreMean) / s.scoreStdDev );
case 'global':
return s.scoreMax;
case 'clipped_global':
default:
return Math.min( s.scoreMax, s.scoreMean + z_score_bound * s.scoreStdDev );
}
}).call(this);
if( typeof max != 'number' || isNaN(max) ) {
throw 'cannot display track '+this.name+', could not determine max_score. Do you need to set max_score in its configuration?';
}
if( typeof min != 'number' || isNaN(min) ) {
throw 'cannot display track '+this.name+', could not determine min_score. Do you need to set min_score in its configuration?';
}
// if we have a log scale, need to take the log of the min and max
if( this.config.scale == 'log' ) {
max = Math.log(max);
min = min ? Math.log(min) : 0;
}
var offset = parseFloat( this.config.data_offset ) || 0;
var origin = (function() {
if ( 'bicolor_pivot' in this.config ) {
if ( this.config.bicolor_pivot == 'mean' ) {
return s.scoreMean || 0;
} else if ( this.config.bicolor_pivot == 'zero' ) {
return 0;
} else {
return parseFloat( this.config.bicolor_pivot );
}
} else if ( this.config.scale == 'z_score' ) {
return s.scoreMean || 0;
} else if ( this.config.scale == 'log' ) {
return 1;
} else {
return 0;
}
}).call(this);
var scale = {
offset: offset,
min: min + offset,
max: max + offset,
range: max - min,
origin: origin,
_statsFingerprint: Digest.objectFingerprint( s )
};
// make a func that converts wiggle values to a range between
// 0 and 1, depending on what kind of scale we are using
scale.normalize = function() {
switch( this.config.scale ) {
case 'z_score':
return function( value ) {
with(scale)
return (value+offset-s.scoreMean) / s.scoreStdDev-min / range;
};
case 'log':
return function( value ) {
with(scale)
return ( Math.log(value+offset) - min )/range;
};
case 'linear':
default:
return function( value ) {
with(scale)
return ( value + offset - min ) / range;
};
}
}.call(this);
return scale;
},
getFeatures: function( query, callback, errorCallback ) {
this.store.getFeatures.apply( this.store, arguments );
},
getGlobalStats: function( successCallback, errorCallback ) {
this.store.getGlobalStats( successCallback, errorCallback );
},
getRegionStats: function( region, successCallback, errorCallback ) {
this.store.getRegionStats( region, successCallback, errorCallback );
},
fillBlock: function( args ) {
var blockIndex = args.blockIndex;
var block = args.block;
var leftBase = args.leftBase;
var rightBase = args.rightBase;
var scale = args.scale;
var finishCallback = args.finishCallback || function() {};
var blockWidth = rightBase - leftBase;
var canvasWidth = Math.ceil(( rightBase - leftBase ) * scale);
var canvasHeight = parseInt(( this.config.style || {}).height) || 100;
this.heightUpdate( canvasHeight, blockIndex );
try {
dojo.create('canvas').getContext('2d').fillStyle = 'red';
} catch( e ) {
this.error = 'This browser does not support HTML canvas elements.';
this.fillError( blockIndex, block );
return;
}
this._getScaling( dojo.hitch( this, function( dataScale ) {
var c = dojo.create(
'canvas',
{ height: canvasHeight,
width: canvasWidth,
style: { cursor: 'default' },
innerHTML: 'Your web browser cannot display this type of track.',
className: 'canvas-track'
},
block
);
c.startBase = leftBase;
var features = [];
this.getFeatures(
{ ref: this.refSeq.name,
basesPerSpan: 1/scale,
scale: scale,
start: leftBase,
end: rightBase+1
},
function(f) { features.push(f); },
dojo.hitch( this, function() {
var featureRects = array.map( features, function(f) {
return this._featureRect( scale, leftBase, c, f, dataScale );
}, this );
this._preDraw( scale, leftBase, rightBase, block, c, features, featureRects, dataScale );
this._drawFeatures( scale, leftBase, rightBase, block, c, features, featureRects, dataScale );
this._postDraw( scale, leftBase, rightBase, block, c, features, featureRects, dataScale );
this._makeScoreDisplay( scale, leftBase, rightBase, block, c, features, featureRects );
this.heightUpdate( c.height, blockIndex );
if( !( c.parentNode && c.parentNode.parentNode )) {
c.style.position = "absolute";
c.style.left = (100 * ((c.startBase - leftBase) / blockWidth)) + "%";
switch (this.config.align) {
case "top":
c.style.top = "0px";
break;
case "bottom":
default:
c.style.bottom = this.trackPadding + "px";
break;
}
}
finishCallback();
}));
}),
dojo.hitch( this, function(e) {
this.error = e;
this.fillError( blockIndex, block );
finishCallback();
})
);
},
/**
* Calculate the left and width, in pixels, of where this feature
* will be drawn on the canvas.
* @private
* @returns {Object} with l, r, and w
*/
_featureRect: function( scale, leftBase, canvas, feature, dataScale ) {
var fRect = {
w: Math.ceil( ( feature.get('end') - feature.get('start') ) * scale ),
l: Math.floor(( feature.get('start') - leftBase ) * scale )
};
// if fRect.l is negative (off the left
// side of the canvas), clip off the
// (possibly large!) non-visible
// portion
if( fRect.l < 0 ) {
fRect.w += fRect.l;
fRect.l = 0;
}
// also don't let fRect.w get overly big
fRect.w = Math.min( canvas.width-fRect.l, fRect.w );
fRect.r = fRect.w + fRect.l;
return fRect;
},
_preDraw: function( canvas ) {
},
/**
* Draw a set of features on the canvas.
* @private
*/
_drawFeatures: function( scale, leftBase, rightBase, block, canvas, features, featureRects ) {
},
_postDraw: function() {
},
_makeScoreDisplay: function( scale, leftBase, rightBase, block, canvas, features, featureRects ) {
// make an array of the max score at each pixel on the canvas
var pixelValues = new Array( canvas.width );
dojo.forEach( features, function( f, i ) {
var fRect = featureRects[i];
var jEnd = fRect.r;
var score = f.get('score');
for( var j = fRect.l; j < jEnd; j++ ) {
pixelValues[j] = j in pixelValues ? Math.max( pixelValues[j], score ) : score;
}
},this);
// make elements and events to display it
var scoreDisplay = dojo.create(
'div', {
className: 'wiggleValueDisplay',
style: {
position: 'fixed',
display: 'none',
zIndex: 15
}
}, block );
var verticalLine = dojo.create( 'div', {
className: 'wigglePositionIndicator',
style: {
position: 'fixed',
display: 'none',
height: canvas.height+'px',
zIndex: 15
}
}, block);
var outTimeout;
dojo.forEach( [canvas,verticalLine,scoreDisplay], function(element) {
on( element, 'mousemove', dojo.hitch(this,function(evt) {
var cPos = dojo.position(canvas);
var x = evt.pageX;
var cx = evt.pageX - cPos.x;
verticalLine.style.display = 'block';
verticalLine.style.left = x+'px';
verticalLine.style.top = cPos.y+'px';
if( this._showPixelValue( scoreDisplay, pixelValues[Math.round(cx)] ) ) {
scoreDisplay.style.left = x+'px';
scoreDisplay.style.top = cPos.y+'px';
scoreDisplay.style.display = 'block';
} else {
scoreDisplay.style.display = 'none';
}
}));
},this);
on( block, 'mouseout', function(evt) {
var target = evt.srcElement || evt.target;
var evtParent = evt.relatedTarget || evt.toElement;
if( !target || !evtParent || target.parentNode != evtParent.parentNode) {
scoreDisplay.style.display = 'none';
verticalLine.style.display = 'none';
}
});
},
_showPixelValue: function( scoreDisplay, score ) {
if( typeof score == 'number' ) {
// display the score with only 6
// significant digits, avoiding
// most confusion about the
// approximative properties of
// IEEE floating point numbers
// parsed out of BigWig files
scoreDisplay.innerHTML = parseFloat( score.toPrecision(6) );
return true;
} else {
return false;
}
},
_exportFormats: function() {
return ['bedGraph','Wiggle', 'GFF3' ];
}
});
return Wiggle;
});