Advertisement
Guest User

Untitled

a guest
Nov 8th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  The Computer Language Benchmarks Game
  2.     http://benchmarksgame.alioth.debian.org/
  3.  
  4.     Contributed by Joe Farro
  5.     parts taken from solution contributed by
  6.     Jesse Millikan which was modified by Matt Baker
  7. */
  8.  
  9.  
  10.  
  11. function SeqSets(len) {
  12.     this.seqLen = len;
  13.     this.uintLeft = 0;
  14.     this.uintRight = 0;
  15.     this.maskLeft = len <= 12 ? 0 : Math.pow(2, (len - 12) * 2) - 1;
  16.     this.maskRight = Math.pow(2, Math.min(12, len) * 2) - 1;
  17.     this.data = {};
  18.     this.lastUintLeft = undefined;
  19.     this.lastLeftData = undefined;
  20. }
  21.  
  22. SeqSets.prototype.pushToken = function(char) {
  23.     this.uintLeft = (this.uintLeft << 2 | this.uintRight >>> 22) & this.maskLeft;
  24.     this.uintRight = (this.uintRight << 2 | char) & this.maskRight;
  25. };
  26.  
  27. SeqSets.prototype.inc = function(char) {
  28.     if (this.uintLeft !== this.lastUintLeft) {
  29.         this.lastUintLeft = this.uintLeft;
  30.         this.lastLeftData = this.data[this.uintLeft] = (this.data[this.uintLeft] || {});
  31.     }
  32.     this.lastLeftData[this.uintRight] = (this.lastLeftData[this.uintRight] || 0) + 1;
  33. };
  34.  
  35. SeqSets.prototype.incWithToken = function(char) {
  36.     this.pushToken(char);
  37.     this.inc();
  38. };
  39.  
  40. SeqSets.prototype.getCount = function(seq) {
  41.     var seqLeft = seq.length <= 12 ? '' : seq.substr(0, seq.length - 12),
  42.         seqRight = seq.substr(-12),
  43.         uintLeft = seqLeft && toUint(seqLeft) || 0,
  44.         uintRight = toUint(seqRight);
  45.  
  46.     return this.data[uintLeft][uintRight];
  47. };
  48.  
  49.  
  50. function charToInt(str) {
  51.     switch (str) {
  52.         case 'a': return 0;
  53.         case 'c': return 1;
  54.         case 'g': return 2;
  55.         case 't': return 3;
  56.     }
  57. }
  58.  
  59. function toStr(num, len) {
  60.     var res = '';
  61.     while (len > 0) {
  62.         switch (num & 3) {
  63.             case 0: res = 'A' + res; break;
  64.             case 1: res = 'C' + res; break;
  65.             case 2: res = 'G' + res; break;
  66.             case 3: res = 'T' + res; break;
  67.         }
  68.         num = num >>> 2;
  69.         len--;
  70.     }
  71.     return res;
  72. }
  73.  
  74. function toUint(str) {
  75.  
  76.     var offset = 2 * str.length,
  77.         uint = new Uint32Array(new ArrayBuffer(4)),
  78.         i = 0;
  79.  
  80.     while (offset) {
  81.         offset -= 2;
  82.         uint[0] |= (charToInt(str[i]) << offset);
  83.         i++;
  84.     }
  85.     return uint[0];
  86. }
  87.  
  88.  
  89. var dataLength = 0;
  90.  
  91. var seq1 = new SeqSets(1),
  92.     seq2 = new SeqSets(2),
  93.     seq3 = new SeqSets(3),
  94.     seq4 = new SeqSets(4),
  95.     seq6 = new SeqSets(6),
  96.     seq12 = new SeqSets(12),
  97.     seq18 = new SeqSets(18);
  98.  
  99. var tables = [
  100.     seq1,
  101.     seq2,
  102.     seq3,
  103.     seq4,
  104.     seq6,
  105.     seq12,
  106.     seq18,
  107. ];
  108.  
  109.  
  110. function readInput() {
  111.  
  112.     var len = 0,
  113.         line,
  114.         i,
  115.         char,
  116.         si,
  117.         slen = tables.length,
  118.         seqSet;
  119.  
  120.     while (readline().substr(0, 3) !== '>TH') {
  121.     }
  122.  
  123.     line = readline();
  124.     i = 0;
  125.     len = line.length;
  126.  
  127.     // the first-line is a special case as not all the counts should start
  128.     // saving immediately
  129.     while (i < 18) {
  130.  
  131.         char = charToInt(line[i]);
  132.  
  133.         si = 0;
  134.         iPlusOne = i + 1;
  135.         for (; si < slen; si++) {
  136.             seqSet = tables[si];
  137.             seqSet.pushToken(char);
  138.             if (seqSet.seqLen <= i + 1) {
  139.                 seqSet.inc();
  140.             }
  141.         }
  142.         i++;
  143.     }
  144.  
  145.     // use do-loop bc want to preserve `i` position on first line
  146.     do {
  147.  
  148.         len = line.length;
  149.         dataLength += len;
  150.         while (i < len) {
  151.  
  152.             char = charToInt(line[i]);
  153.  
  154.             seq1.incWithToken(char);
  155.             seq2.incWithToken(char);
  156.             seq3.incWithToken(char);
  157.             seq4.incWithToken(char);
  158.             seq6.incWithToken(char);
  159.             seq12.incWithToken(char);
  160.             seq18.incWithToken(char);
  161.  
  162.             i++;
  163.         }
  164.         i = 0;
  165.     } while ((line = readline()) && line[0] !== '>')
  166. }
  167.  
  168.  
  169. function sortCounts(data, seqLen) {
  170.  
  171.     var keys = Object.keys(data),
  172.         pctFactor = 100 / (dataLength - seqLen + 1);
  173.  
  174.     keys.sort(function(a, b) {
  175.         return data[b] - data[a];
  176.     });
  177.  
  178.     keys.forEach(function(code) {
  179.         print(toStr(code, seqLen), (data[code] * pctFactor).toFixed(3));
  180.     });
  181.     print();
  182. }
  183.  
  184. readInput();
  185.  
  186. sortCounts(seq1.data[0], 1);
  187. sortCounts(seq2.data[0], 2);
  188.  
  189. print(seq3.getCount('ggt') +'\t' + 'GGT');
  190. print(seq4.getCount('ggta') +'\t' + 'GGTA');
  191. print(seq6.getCount('ggtatt') +'\t' + 'GGTATT');
  192. print(seq12.getCount('ggtattttaatt') +'\t' + 'GGTATTTTAATT');
  193. print(seq18.getCount('ggtattttaatttatagt') + '\t' + 'GGTATTTTAATTTATAGT');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement