The Computer Language
Benchmarks Game

k-nucleotide TypeScript program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   contributed by Josh Goldfoot
*/


/// <reference path="./Include/node/index.d.ts" />

import { createInterface } from "readline";

class RefNum {
    num: number;
    constructor(n: number) {
        this.num = n;
    }
}

function frequency(sequence: string, length: number): Map<string, RefNum> {
    var freq = new Map<string, RefNum>();
    var n = sequence.length - length + 1;
    var sub = "";
    var m: RefNum;
    for (var i = 0; i < n; i++) {
        sub = sequence.substr(i, length);
        m = freq.get(sub);
        if (m === undefined) {
            freq.set(sub, new RefNum(1));
        } else {
            m.num += 1;
        }
    }
    return freq;
}

function sort(sequence: string, length: number): void {
    var freq = frequency(sequence, length);
    var keys = new Array<string>();
    for (let k of freq.keys())
        keys.push(k);
    keys.sort((a, b) => (freq.get(b).num - freq.get(a).num));
    var n = sequence.length - length + 1;
    keys.forEach(key => {
        var count = (freq.get(key).num * 100 / n).toFixed(3);
        console.log(key + " " + count);
    });
    console.log("");
}

function find(haystack: string, needle: string): void {
    var freq = frequency(haystack, needle.length);
    var m = freq.get(needle);
    var num = m ? m.num : 0;
    console.log(num + "\t" + needle);
}

function main() {
    var sequence = "";
    var reading = false;
    createInterface({ input: process.stdin, output: process.stdout })
        .on('line', line => {
            if (reading) {
                if (line[0] !== '>')
                    sequence += line.toUpperCase();
            } else
                reading = line.substr(0, 6) === '>THREE';
        }).on('close', () => {
            sort(sequence, 1);
            sort(sequence, 2);
            find(sequence, 'GGT');
            find(sequence, 'GGTA');
            find(sequence, 'GGTATT');
            find(sequence, 'GGTATTTTAATT');
            find(sequence, 'GGTATTTTAATTTATAGT');
        });
}

main();
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Version 2.2.1
node.js v7.6.0


Thu, 23 Feb 2017 17:50:27 GMT

MAKE:
mv knucleotide.typescript knucleotide.ts
/usr/local/src/node-v7.6.0-linux-x64/bin/tsc --alwaysStrict -t ESNEXT  knucleotide.ts
/usr/local/src/node-v7.6.0-linux-x64/lib/node_modules/babel-cli/bin/babel.js --plugins transform-es2015-modules-commonjs knucleotide.js -o knucleotide.js
6.31s to complete and log all make actions

COMMAND LINE:
/usr/local/src/node-v7.6.0-linux-x64/bin/node --use_strict knucleotide.js 0 < knucleotide-input25000000.txt

PROGRAM OUTPUT:
A 30.295
T 30.151
C 19.800
G 19.754

AA 9.177
TA 9.132
AT 9.131
TT 9.091
CA 6.002
AC 6.001
AG 5.987
GA 5.984
CT 5.971
TC 5.971
GT 5.957
TG 5.956
CC 3.917
GC 3.911
CG 3.909
GG 3.902

1471758	GGT
446535	GGTA
47336	GGTATT
893	GGTATTTTAATT
893	GGTATTTTAATTTATAGT