The Computer Language
24.11 Benchmarks Game

k-nucleotide Dart #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
*/

import 'dart:collection';
import 'dart:io';

List seqLines() {
   while (true) {
      final line = stdin.readLineSync();    
      if (line == null || line.startsWith(">THREE")) {
         break;
      } 
   }
   final lines = <String>[];
   while (true) {
      final line = stdin.readLineSync();    
      if (line == null || line.startsWith(">")) {
         break;
      } 
      lines.add(line);      
   }   
   return lines;
}

HashMap<String, int> baseCounts(int bases, String seq) {
   final counts = new HashMap<String, int>();   
   final size = seq.length + 1 - bases;   
   for (int i = 0; i < size; i++) {  
      final nucleo = seq.substring(i, i + bases);
      if (counts.containsKey(nucleo))
         counts[nucleo] = (counts[nucleo] ?? 0) + 1;
      else
         counts[nucleo] = 1;      
   }
   return counts;
}

Iterable<(String, String)> sortedFreq(int bases, String seq) {
   final keysValues = baseCounts(bases, seq).entries.toList();
   final size = seq.length + 1 - bases;     
   keysValues.sort((a, b) => b.value.compareTo(a.value)); 
   return 
      keysValues.map((each) => (each.key,  
         (100.0 * each.value / size ).toStringAsFixed(3))); 
}

specificCount(code, seq) {  
   return baseCounts(code.length, seq)[code];
}

void main(List<String> args) {
   var lines = seqLines();   
   var seq = lines.map((each) => each.toUpperCase()).join();    
   
   for (final base in [1, 2]) {  
      for (final (k,v) in sortedFreq(base,seq)) {
         print("$k $v");
      }
      print("");   
   }
       
   for (final code in ["GGT", "GGTA", "GGTATT",
            "GGTATTTTAATT", "GGTATTTTAATTTATAGT"]) {     
      print("${specificCount(code,seq)}\t$code"); 
   }
}

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Dart SDK version: 3.5.4 (stable)
Wed Oct 16 16:18:51 2024



 Wed, 23 Oct 2024 04:13:40 GMT

MAKE:
/opt/src/dart-sdk/bin/dart analyze 
Analyzing tmp...
No issues found!

/opt/src/dart-sdk/bin/dart compile exe knucleotide.dartexe-8.dartexe -o knucleotide.dartexe-8.dartexe_run
Generated: /home/dunham/all-benchmarksgame/benchmarksgame_i53330/knucleotide/tmp/knucleotide.dartexe-8.dartexe_run

4.72s to complete and log all make actions

COMMAND LINE:
 ./knucleotide.dartexe-8.dartexe_run 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