(Created page with "var DNA = {}; (function(){ var bases = 'ACTG'; var encodeUInt = function(num){ num = Math.floor(num); // truncate to int num = num < 0 ? -num : num; // make positive...") |
|||
Line 4: | Line 4: | ||
var bases = 'ACTG'; | var bases = 'ACTG'; | ||
− | var encodeUInt = function(num){ | + | var encodeUInt = function(num, len){ |
num = Math.floor(num); // truncate to int | num = Math.floor(num); // truncate to int | ||
num = num < 0 ? -num : num; // make positive | num = num < 0 ? -num : num; // make positive | ||
Line 13: | Line 13: | ||
ret += bases[num & 3]; // lowest 2 bits as index to the base-pairs | ret += bases[num & 3]; // lowest 2 bits as index to the base-pairs | ||
num = num >>> 2; // shift in next 2 bits | num = num >>> 2; // shift in next 2 bits | ||
+ | } | ||
+ | |||
+ | while(ret.length < len){ | ||
+ | ret += 'A'; | ||
} | } | ||
Line 33: | Line 37: | ||
return ret; | return ret; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}; | }; | ||
DNA = { | DNA = { | ||
'encodeUInt': encodeUInt, | 'encodeUInt': encodeUInt, | ||
− | 'decodeUInt': decodeUInt | + | 'decodeUInt': decodeUInt |
− | + | ||
}; | }; | ||
})(); | })(); |
Revision as of 13:18, 8 July 2016
var DNA = {};
(function(){ var bases = 'ACTG';
var encodeUInt = function(num, len){ num = Math.floor(num); // truncate to int num = num < 0 ? -num : num; // make positive
var ret = num ? : 'A'; // 0 -> A
while(num){ // != 0 ret += bases[num & 3]; // lowest 2 bits as index to the base-pairs num = num >>> 2; // shift in next 2 bits }
while(ret.length < len){ ret += 'A'; }
return ret; };
var decodeUInt = function(dna){ var ret = 0;
for(var i = 0; i < dna.length; i++){ ret *= 4; // shift first (does nothing first time)
var idx = bases.indexOf(dna[dna.length - i - 1]); if(idx === -1){ // not a base-pair return null; }
ret += idx; }
return ret; };
DNA = { 'encodeUInt': encodeUInt, 'decodeUInt': decodeUInt }; })();