The Computer Language
24.11 Benchmarks Game

k-nucleotide Node.js #8 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
   
   Naive transliteration from bearophile's program
   contributed by Isaac Gouy   
*/

const rl = require("node:readline");

async function seq_lines() {
    const stdin = rl.createInterface({
        input: process.stdin,
    });
    for await (const line of stdin) {       
        if (line.startsWith(">THREE")) break;        
    }   
    const lines = [];    
    for await (const line of stdin) {    
        if (line.startsWith(">")) break; 
        lines.push(line);        
    }   
    return lines;
}

function base_counts(bases, seq) { 
    counts = new Map();
    size = seq.length + 1 - bases;     
    for (let i = 0; i < size; i++) {   
        const nucleo = seq.substring(i,i+bases); 
        var v; if (v = counts.get(nucleo))        
            counts.set(nucleo,v+1); 
        else
            counts.set(nucleo,1);          
    }
    return counts;    
}

function sorted_freq(bases, seq) {
    size = seq.length + 1 - bases;  
    counts = base_counts(bases, seq); 
    return Array.from(counts)
        .sort((a, b) => b[1] - a[1]) 
        .map((a) => [a[0], 100.0 * a[1] / size]);     
}   

function specific_count(code, seq) {  
    counts = base_counts(code.length,seq);
    return counts.get(code) ? counts.get(code) : 0;        
}   

function write_freq(a) {  
    console.log(`${a[0]} ${a[1].toFixed(3)}`);        
}  

function main(n) {
    seq_lines().then(a => {
        const seq = a.map((s) => s.toUpperCase()).join("");
        
        for (const base of [1,2]) {  
            sorted_freq(base,seq).forEach(write_freq);     
            console.log();             
        }
        
        for (const code of ["GGT", "GGTA", "GGTATT",
                "GGTATTTTAATT", "GGTATTTTAATTTATAGT"]) {  
            console.log(`${specific_count(code,seq)}\t${code}`);           
        }        
    } );        
}            

main(+process.argv[2] || 0)
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
v23.0.0


 Wed, 13 Nov 2024 00:15:16 GMT

MAKE:
cp -L knucleotide.node-8.node knucleotide.js

0.05s to complete and log all make actions

COMMAND LINE:
 /opt/src/node-v23.0.0/bin/node knucleotide.js 0 < knucleotide-input250000.txt

UNEXPECTED OUTPUT 

1,4c1,4
< A 30.542
< T 29.928
< C 20.222
< G 19.307
---
> A 30.298
> T 30.157
> C 19.793
> G 19.752
6,21c6,21
< AA 9.310
< TA 9.126
< AT 9.052
< TT 9.039
< CA 6.237
< AC 6.156
< CT 6.087
< TC 6.081
< AG 6.023
< GA 5.867
< GT 5.752
< TG 5.683
< GC 4.021
< CC 3.965
< CG 3.934
< GG 3.668
---
> AA 9.177
> TA 9.137
> AT 9.136
> TT 9.094
> AC 6.000
> CA 5.999
> GA 5.986
> AG 5.985
> TC 5.970
> CT 5.970
> GT 5.957
> TG 5.956
> CC 3.915
> CG 3.910
> GC 3.908
> GG 3.902
23,27c23,27
< 437	GGT
< 121	GGTA
< 8	GGTATT
< 0	GGTATTTTAATT
< 0	GGTATTTTAATTTATAGT
---
> 14717	GGT
> 4463	GGTA
> 472	GGTATT
> 9	GGTATTTTAATT
> 9	GGTATTTTAATTTATAGT

PROGRAM OUTPUT:
A 30.542
T 29.928
C 20.222
G 19.307

AA 9.310
TA 9.126
AT 9.052
TT 9.039
CA 6.237
AC 6.156
CT 6.087
TC 6.081
AG 6.023
GA 5.867
GT 5.752
TG 5.683
GC 4.021
CC 3.965
CG 3.934
GG 3.668

437	GGT
121	GGTA
8	GGTATT
0	GGTATTTTAATT
0	GGTATTTTAATTTATAGT