The Computer Language
Benchmarks Game

k-nucleotide Dart snapshot program

source code

/* The Computer Language Benchmarks Game

   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   Contributed by Philip Rogers
   Based on a javascript implementation by Jesse Millikan and Matt Baker
*/

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

String readLine() => stdin.readLineSync();

String readInput() {
  while(readLine().substring(0, 6) != '>THREE');

  List<String> lines = new List();
  String line = readLine();
  while (line != null && line[0] != '>') {
    lines.add(line);
    line = readLine();
  };
  return lines.join('').toUpperCase();
}

HashMap<String, int> frequency(String sequence, int length) {
  HashMap<String, int> freq = new HashMap<String, int>();
  int n = sequence.length - length + 1;
  String sub;
  for (int i = 0; i < n; i++) {
    sub = sequence.substring(i, i + length);
    if (freq.containsKey(sub))
      freq[sub] = freq[sub] + 1;
    else
      freq[sub] = 1;
  }
  return freq;
}

void sort(String sequence, int length) {
  HashMap<String, int> freq = frequency(sequence, length);
  List<String> keys = freq.keys.toList();
  int n = sequence.length - length + 1;
  keys.sort((a, b) => (freq[b] - freq[a]));
  for (String key in keys) {
    String count = (freq[key] * 100 / n).toStringAsFixed(3);
    print('$key $count');
  }
  print('');
}

void find(String sequence, String string) {
  HashMap<String, int> freq = frequency(sequence, string.length);
  print('${(freq[string])}\t$string');
}

void main(args) {
  String sequence = readInput();
  if (sequence == null)
    return;
  sort(sequence, 1);
  sort(sequence, 2);
  find(sequence, 'GGT');
  find(sequence, 'GGTA');
  find(sequence, 'GGTATT');
  find(sequence, 'GGTATTTTAATT');
  find(sequence, 'GGTATTTTAATTTATAGT');
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Dart VM version: 2.6.0 (Thu Oct 24 17:52:22 2019 +0200) on "linux_x64"
--snapshot-kind=app-jit


Wed, 06 Nov 2019 05:16:43 GMT

MAKE:
/opt/src/dartsdk-linux-x64-release/dart-sdk/bin/dartanalyzer knucleotide.dartsnapshot
Analyzing knucleotide.dartsnapshot...
No issues found!
/opt/src/dartsdk-linux-x64-release/dart-sdk/bin/dart --snapshot=knucleotide.snapshot --snapshot-kind=app-jit knucleotide.dartsnapshot 0 < ../knucleotide-input50000.txt
A 30.346
T 30.052
C 19.807
G 19.795

AA 9.212
TA 9.118
AT 9.097
TT 9.040
AC 6.019
AG 6.018
CA 6.018
GA 5.998
CT 5.959
GT 5.957
TC 5.951
TG 5.944
GC 3.923
GG 3.917
CG 3.916
CC 3.914

2948	GGT
889	GGTA
89	GGTATT
2	GGTATTTTAATT
2	GGTATTTTAATTTATAGT

9.98s to complete and log all make actions

COMMAND LINE:
/opt/src/dartsdk-linux-x64-release/dart-sdk/bin/dart  knucleotide.snapshot 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