The Computer Language
Benchmarks Game

k-nucleotide TypeScript program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   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 3.6.3
-t ESNEXT
node.js v12.11.0



Sun, 29 Sep 2019 20:21:51 GMT

MAKE:
mv knucleotide.typescript knucleotide.ts
/opt/src/node-v12.11.0-linux-x64/bin/tsc --strict --noEmitOnError --removeComments -t ESNEXT  knucleotide.ts
knucleotide.ts(26,9): error TS2322: Type 'RefNum | undefined' is not assignable to type 'RefNum'.
  Type 'undefined' is not assignable to type 'RefNum'.
knucleotide.ts(41,26): error TS2532: Object is possibly 'undefined'.
knucleotide.ts(41,44): error TS2532: Object is possibly 'undefined'.
knucleotide.ts(44,22): error TS2532: Object is possibly 'undefined'.
make: [/home/dunham/8000-benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:646: knucleotide.typescript_run] Error 1 (ignored)
/opt/src/node-v12.11.0-linux-x64/lib/node_modules/babel-cli/bin/babel.js --plugins transform-es2015-modules-commonjs knucleotide.js -o knucleotide.js
knucleotide.js doesn't exist
make: [/home/dunham/8000-benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:647: knucleotide.typescript_run] Error 2 (ignored)

6.95s to complete and log all make actions

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

PROGRAM FAILED 


PROGRAM OUTPUT:

internal/modules/cjs/loader.js:783
    throw err;
    ^

Error: Cannot find module '/home/dunham/benchmarksgame_quadcore/knucleotide/tmp/knucleotide.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:780:15)
    at Function.Module._load (internal/modules/cjs/loader.js:685:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}