From b45a8326d33ad61f8490b6987aeb5b9002b18bbe Mon Sep 17 00:00:00 2001 From: jsmirob Date: Tue, 15 Jan 2013 16:29:12 -0500 Subject: [PATCH] Added new track type --- src/JBrowse/View/Track/BaseCoverage.js | 182 +++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 src/JBrowse/View/Track/BaseCoverage.js diff --git a/src/JBrowse/View/Track/BaseCoverage.js b/src/JBrowse/View/Track/BaseCoverage.js new file mode 100644 index 0000000000..5e8b378f63 --- /dev/null +++ b/src/JBrowse/View/Track/BaseCoverage.js @@ -0,0 +1,182 @@ +define( ['dojo/_base/declare', + 'dojo/_base/array', + 'JBrowse/View/Track/Wiggle', + 'JBrowse/Util' + ], + function( declare, array, Wiggle, Util ) { + +// feature class for the features we make for the calculated coverage +// values +var CoverageFeature = Util.fastDeclare( + { + get: function(f) { return this[f]; }, + tags: function() { return [ 'start', 'end', 'score' ]; }, + score: 0, + constructor: function( args ) { + this.start = args.start; + this.end = args.end; + this.score = args.score; + } + }); + +return declare( Wiggle, +{ + + _defaultConfig: function() { + return Util.deepUpdate( + dojo.clone( this.inherited(arguments) ), + { + min_score: 0, + max_score: 100 + } + ); + }, + + getGlobalStats: function() { + return {}; + }, + + getFeatures: function( query, featureCallback, finishCallback, errorCallback ) { + var leftBase = query.start; + var rightBase = query.end; + var scale = query.scale; // px/bp + var widthBp = rightBase-leftBase; + var widthPx = widthBp * ( query.scale || 1/query.basesPerSpan); + + var binWidth = Math.ceil( query.basesPerSpan ); // in bp + + var arraySize = Math.ceil( widthBp/binWidth ); + var coverageBins = new Array( arraySize ); + var aCoverage = new Array( arraySize ); + var tCoverage = new Array( arraySize ); + var cCoverage = new Array( arraySize ); + var gCoverage = new Array( arraySize ); + var bpToBin = function( bp ) { + return Math.floor( (bp-leftBase) / binWidth ); + }; + + this.store.getFeatures( + query, + dojo.hitch( this, function( feature ) { + var startBin = bpToBin( feature.get('start') ); + var endBin = bpToBin( feature.get('end')-1 ); + for( var i = startBin; i <= endBin; i++ ) { + coverageBins[i] = (coverageBins[i] || 0) + 1; + } + // Calculate SNP coverage + var mdTag = feature.get('MD'); + if(mdTag) { + var SNPs = this._mdToMismatches(feature, mdTag); + // loops through mismatches and updates coverage variables accordingly. + for (var i = 0; i= 0 ) { + context.fillStyle = bgColor; + context.fillRect( fRect.l, 0, fRect.w, canvasHeight ); + } + + if( fRect.t <= canvasHeight ) { // if the rectangle is visible at all + + if( fRect.t <= originY ) { + // bar goes upward + context.fillStyle = barColor[j]; + context.fillRect( fRect.l, fRect.t, fRect.w, originY-fRect.t+1); + if( !disableClipMarkers && fRect.t < 0 ) { // draw clip marker if necessary + context.fillStyle = clipColor || negColor; + context.fillRect( fRect.l, 0, fRect.w, 2 ); + } + } + else { + alert('Invalid data used. Negative values are not possible.'); + } + } + } + }, this ); + }, + //******************************************************************************************************************************* + + // a method blatently stolen from "Alignments.js" Perhaps it would be better just to include the file... + _mdToMismatches: function( feature, mdstring ) { + var mismatchRecords = []; + var curr = { start: 0, bases: '' }; + var seq = feature.get('seq'); + var nextRecord = function() { + mismatchRecords.push( curr ); + curr = { start: curr.start + curr.bases.length, bases: ''}; + }; + array.forEach( mdstring.match(/(\d+|\^[a-z]+|[a-z])/ig), function( token ) { + if( token.match(/^\d/) ) { // matching bases + curr.start += parseInt( token ); + } + else if( token.match(/^\^/) ) { // insertion in the template + var i = token.length-1; + while( i-- ) { + curr.bases += '*'; + } + nextRecord(); + } + else if( token.match(/^[a-z]/i) ) { // mismatch + curr.bases = seq.substr( curr.start, token.length ); + nextRecord(); + } + }); + return mismatchRecords; + } +}); +}); \ No newline at end of file