forked from Synbiota/GENtle2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
299 lines (257 loc) · 9.47 KB
/
common.js
File metadata and controls
299 lines (257 loc) · 9.47 KB
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
function getBlobBuilder () {
try {
if ( window.Blob ) return new window.Blob() ;
} catch ( e ) {
if ( window.BlobBuilder ) return new window.BlobBuilder() ;
if ( window.MozBlobBuilder ) return new window.MozBlobBuilder() ;
if ( window.WebKitBlobBuilder ) return new window.WebKitBlobBuilder() ;
if ( window.MsBlobBuilder ) return new window.MsBlobBuilder() ;
}
return undefined ;
}
// "length" of object
function object_length ( obj ) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// Just what it says...
String.prototype.reverse=function(){return this.split("").reverse().join("");}
// Just what it says...
function ucFirst ( string ) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// Add thousands separator commas
function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
// Reverse-complement sequence
// TODO: find the five ways this function could run faster if you're bored!
function rcSequence ( s ) {
var t = '' ;
for ( var i = 0 ; i < s.length ; i++ ) {
t = cd.rc[s.charAt(i)] + t ;
}
return t ;
}
// Post-process common data for easier access
function loadBaseData () {
// IUPAC to bases
cd.iupac2bases = {} ;
$.each ( cd.bases2iupac , function ( k , v ) { cd.iupac2bases[v] = k ; } ) ;
// Reverse-complement bases
cd.rc = {} ;
$.each ( cd.iupac2bases , function ( iupac , bases ) {
var nb = '' ;
if ( bases.match(/T/) ) nb += 'A' ;
if ( bases.match(/G/) ) nb += 'C' ;
if ( bases.match(/C/) ) nb += 'G' ;
if ( bases.match(/A/) ) nb += 'T' ;
if ( nb == '' ) cd.rc[iupac] = 'N' ; // N <=> N
else cd.rc[iupac] = cd.bases2iupac[nb] ;
} ) ;
// Amino Acids : short to long
cd.aa_s2l = {} ;
$.each ( cd.aa , function ( k , v ) { cd.aa_s2l[v.short] = v.long ; } ) ;
// Amino Acids : short to codons
cd.aa_s2c = {} ;
$.each ( cd.aa , function ( k , v ) { cd.aa_s2c[v.short] = v.codons ; } ) ;
// Amino Acids : codons to short
cd.aa_c2s = {} ;
$.each ( cd.aa , function ( k , v ) {
$.each ( v.codons , function ( k2 , v2 ) {
cd.aa_c2s[v2] = v.short ;
} ) ;
} ) ;
// Restriction enzymes : Accessible by name
cd.re = {} ;
$.each ( cd.restriction_enzymes , function ( k , v ) {
// Calculate regexp
var seq = v.seq ;
var pattern = '' ;
for ( var i = 0 ; i < seq.length ; i++ ) {
var r = cd.iupac2bases[seq[i]] ;
if ( r.length == 1 ) pattern += seq[i] ;
else pattern += '['+r+']' ;
}
cd.re[v.name] = {
seq:v.seq ,
cut:v.cut ,
offset:v.offset ,
is_palindromic : ( v.seq == rcSequence(v.seq) ) , // TODO check cut/offset for is_palindromic
rx : new RegExp('('+pattern+')','gi')
} ;
} ) ;
// Restriction enzymes : Group by recognition sequence and cut/offset
cd.re_s2n = {} ; // Sequence-to-name; actually [sequence_length][seq/cut/offset] = [ list,of,names ]
$.each ( cd.re , function ( k , v ) {
var l = v.seq.length ;
var s = v.seq + "/" + v.cut + "/" + v.offset ;
if ( undefined === cd.re_s2n[l] ) cd.re_s2n[l] = {} ;
if ( undefined === cd.re_s2n[l][s] ) cd.re_s2n[l][s] = [] ;
cd.re_s2n[l][s].push ( k ) ;
} ) ;
// Type colors
$.each ( cd.feature_types , function ( k , v ) {
gentle.features[k] = v.name || ucFirst ( k ) ;
if ( $('#dummy_feature_'+k).length == 0 ) $('body').append("<div id='dummy_feature_"+k+"' class='feat_"+k+"' style='display:none'></div>") ;
if ($('#dummy_feature_'+k).css ( 'background-color' ) != 'rgba(0, 0, 0, 0)' ){
cd.feature_types[k].col = $('#dummy_feature_'+k).css ( 'background-color' )
} else {
cd.feature_types[k].col = '#888888' ;
}
} ) ;
}
// This function allows for arbitrary text to be copied/pasted to the clipboard,
// but only during actual cut/copy actions from menu or keyboard. It does this by
// showing a textarea with the selected text to be copied when the action is performed;
// the textarea then executes the event. The textarea is then hidden again.
function copyToClipboard ( text ) {
$('#copytb').val(text);
$('#copywrap').show();
$('#copytb').focus();
$('#copytb').select();
setTimeout ( "$('#copywrap').hide();" , 100 ) ;
}
function clone(obj) {
if ( undefined === obj ) return undefined ;
return JSON.parse ( JSON.stringify(obj) );
/* // A clone of an object is an empty object
// with a prototype reference to the original.
// a private constructor, used only by this one clone.
function Clone() { }
Clone.prototype = obj;
var c = new Clone();
c.constructor = Clone;
return c;*/
}
String.prototype.replaceAt=function(index, character) { return this.substr(0, index) + character + this.substr(index+character.length); }
String.prototype.trunc =
function(n,useWordBoundary){
var tooLong = this.length>n,
s_ = tooLong ? this.substr(0,n-1) : this;
s_ = useWordBoundary && tooLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
return ""+(tooLong ? s_ + '...' : s_);
}; // adapted from http://stackoverflow.com/questions/1199352/smart-way-to-shorten-long-strings-with-javascript
// if fullEscape option is set to true, escapes everything, otherwise escapes only < and >
String.prototype.escapeHTML = function(fullEscape) {
return ""+ (fullEscape ? this.replace(/</i, '<').replace(/>/i, '>') : document.createTextNode(this).textContent);
}
/* Used to batch copy a set of CSS properties from a DOM element to another */
$.fn.copyCSSProperties = function (source, properties_array) {
for(var i in properties_array) {
var property = properties_array[i];
if(property == 'width')
this.width(source.width());
else if(property == 'height')
this.height(source.height());
else
this.css(property, source.css(property));
}
return this;
}
/* Paul Irish Animation shim:
http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/ */
(function() {
var lastTime = 0;
var vendors = ['webkit', 'moz'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
//
// Base64 utility methods (HTML5)
// (https://github.com/inexorabletash/polyfill)
//
(function (global) {
var B64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
global.atob = global.atob || function (input) {
input = String(input);
var position = 0,
output = [],
buffer = 0, bits = 0, n;
input = input.replace(/\s/g, '');
if ((input.length % 4) === 0) { input = input.replace(/=+$/, ''); }
if ((input.length % 4) === 1) { throw Error("InvalidCharacterError"); }
if (/[^+/0-9A-Za-z]/.test(input)) { throw Error("InvalidCharacterError"); }
while (position < input.length) {
n = B64_ALPHABET.indexOf(input.charAt(position));
buffer = (buffer << 6) | n;
bits += 6;
if (bits === 24) {
output.push(String.fromCharCode((buffer >> 16) & 0xFF));
output.push(String.fromCharCode((buffer >> 8) & 0xFF));
output.push(String.fromCharCode(buffer & 0xFF));
bits = 0;
buffer = 0;
}
position += 1;
}
if (bits === 12) {
buffer = buffer >> 4;
output.push(String.fromCharCode(buffer & 0xFF));
} else if (bits === 18) {
buffer = buffer >> 2;
output.push(String.fromCharCode((buffer >> 8) & 0xFF));
output.push(String.fromCharCode(buffer & 0xFF));
}
return output.join('');
};
global.btoa = global.btoa || function (input) {
input = String(input);
var position = 0,
out = [],
o1, o2, o3,
e1, e2, e3, e4;
if (/[^\x00-\xFF]/.test(input)) { throw Error("InvalidCharacterError"); }
while (position < input.length) {
o1 = input.charCodeAt(position++);
o2 = input.charCodeAt(position++);
o3 = input.charCodeAt(position++);
// 111111 112222 222233 333333
e1 = o1 >> 2;
e2 = ((o1 & 0x3) << 4) | (o2 >> 4);
e3 = ((o2 & 0xf) << 2) | (o3 >> 6);
e4 = o3 & 0x3f;
if (position === input.length + 2) {
e3 = 64; e4 = 64;
}
else if (position === input.length + 1) {
e4 = 64;
}
out.push(B64_ALPHABET.charAt(e1),
B64_ALPHABET.charAt(e2),
B64_ALPHABET.charAt(e3),
B64_ALPHABET.charAt(e4));
}
return out.join('');
};
}(this));
//Bootstrap multimodal fix
//https://stackoverflow.com/questions/13649459/twitter-bootstrap-multiple-modal-error
$.fn.modal.Constructor.prototype.enforceFocus = function () {};