forked from Synbiota/GENtle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
149 lines (129 loc) · 4.46 KB
/
common.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
function getBlobBuilder () {
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[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>") ;
cd.feature_types[k].col = $('#dummy_feature_'+k).css ( 'background-color' ) || '#DDDDDD' ;
} ) ;
}
// 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;*/
}