-
Notifications
You must be signed in to change notification settings - Fork 199
/
Alignments.js
188 lines (166 loc) · 6.77 KB
/
Alignments.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
define( ['dojo/_base/declare',
'dojo/_base/array',
'JBrowse/View/Track/HTMLFeatures'
],
function( declare, array, HTMLFeatures ) {
return declare( HTMLFeatures,
/**
* @lends JBrowse.View.Track.Alignments
*/
{
constructor: function() {
},
renderFeature: function(feature, uniqueId, block, scale, containerStart, containerEnd, destBlock ) {
var featDiv = this.inherited( arguments );
this._drawMismatches( feature, featDiv, scale );
return featDiv;
},
_drawMismatches: function( feature, featDiv, scale ) {
// recall: scale is pixels/basepair
if ( scale >= 1 ) {
var mismatches = this._getMismatches( feature );
var charSize = this.getCharacterMeasurements();
var drawChars = scale >= charSize.w;
var featureLength = feature.get('end') - feature.get('start');
array.forEach( mismatches, function( mismatch ) {
array.forEach( mismatch.bases, function( base, i ) {
dojo.create('span', {
className: 'mismatch base base_'+(base == '*' ? 'deletion' : base.toLowerCase()),
style: {
position: 'absolute',
left: (mismatch.start+i)/featureLength*100 + '%',
width: scale + 'px'
},
innerHTML: drawChars ? base : ''
}, featDiv );
}, this );
});
}
},
_getMismatches: function( feature ) {
var seq = feature.get('seq');
if( ! seq )
return m;
// parse the MD tag if it has one
var mdTag = feature.get('MD');
if( mdTag ) {
return this._mdToMismatches( feature, mdTag );
}
return [];
},
/**
* parse a SAM MD tag to find mismatching bases of the template versus the reference
* @returns {Array[Object]} array of mismatches and their positions
* @private
*/
_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;
},
// stub out subfeature rendering, this track doesn't render subfeatures
renderSubfeature: function() {},
/**
* @returns {Object} containing <code>h</code> and <code>w</code>,
* in pixels, of the characters being used for sequences
*/
getCharacterMeasurements: function() {
if( !this._measurements )
this._measurements = this._measureSequenceCharacterSize( this.div );
return this._measurements;
},
/**
* Conducts a test with DOM elements to measure sequence text width
* and height.
*/
_measureSequenceCharacterSize: function( containerElement ) {
var widthTest = dojo.create('div', {
innerHTML: '<span class="base mismatch">A</span>'
+'<span class="base mismatch">C</span>'
+'<span class="base mismatch">T</span>'
+'<span class="base mismatch">G</span>'
+'<span class="base mismatch">N</span>',
style: {
visibility: 'hidden',
position: 'absolute',
left: '0px'
}
}, containerElement );
var result = {
w: widthTest.clientWidth / 5,
h: widthTest.clientHeight
};
containerElement.removeChild(widthTest);
return result;
},
/**
* Make a default feature detail page for the given feature.
* @returns {HTMLElement} feature detail page HTML
*/
defaultFeatureDetail: function( /** JBrowse.Track */ track, /** Object */ f, /** HTMLElement */ div ) {
var container = this.inherited( arguments );
var fmt = dojo.hitch( this, '_fmtDetailField' );
// if( f.get('strand') == -1 ) {
// dojo.create('div', { className: 'message', innerHTML: '' })
// }
var alignment = '<div class="alignment sequence">'+f.get('seq')+'</div>';
if( f.get('seq') && f.get('qual') ) {
container.innerHTML += fmt('Sequence quality scores', this._renderQual( f ) );
}
return container;
},
_renderQual: function( feature ) {
var seq = feature.get('seq'),
qual = feature.get('qual');
if( !seq || !qual )
return '';
qual = qual.split(/\s+/);
var fieldWidth = (''+Math.max.apply( Math, qual )).length;
// pad the sequence with spaces
var seqPadding = ' ';
while( seqPadding.length < fieldWidth-1 ) {
seqPadding += ' ';
}
var paddedSeq = array.map( seq, function(s) {
return s + seqPadding;
});
var tableRowHTML = function(fields, class_) {
class_ = class_ ? ' class="'+class_+'"' : '';
return '<tr'+class_+'>'+array.map( fields, function(f) { return '<td>'+f+'</td>'; }).join('')+'</tr>';
};
// insert newlines
var rendered = '';
var lineFields = Math.round(55/fieldWidth);
while( paddedSeq.length ) {
var line = paddedSeq.slice(0,Math.min( paddedSeq.length, lineFields ) );
paddedSeq = paddedSeq.slice(lineFields);
rendered += tableRowHTML( line, 'seq' );
line = qual.slice(0, Math.min( qual.length, lineFields ));
qual = qual.slice(lineFields);
rendered += tableRowHTML( line, 'qual' );
}
return '<table class="baseQuality">'+rendered+'</table>';
}
});
});