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.6.2
node.js v9.4.0


Wed, 10 Jan 2018 20:29:35 GMT

MAKE:
mv knucleotide.typescript knucleotide.ts
/opt/src/node-v9.4.0-linux-x64/bin/tsc --alwaysStrict -t ESNEXT  knucleotide.ts
/opt/src/node-v8.8.1-linux-x64/lib/node_modules/babel-cli/bin/babel.js --plugins transform-es2015-modules-commonjs knucleotide.js -o knucleotide.js
make: /opt/src/node-v8.8.1-linux-x64/lib/node_modules/babel-cli/bin/babel.js: Command not found
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:649: recipe for target 'knucleotide.typescript_run' failed
make: [knucleotide.typescript_run] Error 127 (ignored)

2.93s to complete and log all make actions

COMMAND LINE:
/opt/src/node-v9.4.0-linux-x64/bin/node --use_strict knucleotide.js 0 < knucleotide-input250000.txt

PROGRAM FAILED 


PROGRAM OUTPUT:

/home/dunham/benchmarksgame_quadcore/knucleotide/tmp/knucleotide.js:7
import { createInterface } from "readline";
^^^^^^

SyntaxError: Unexpected token import
    at new Script (vm.js:51:7)
    at createScript (vm.js:138:10)
    at Object.runInThisContext (vm.js:199:10)
    at Module._compile (module.js:624:28)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:193:16)