The Computer Language
24.11 Benchmarks Game

k-nucleotide Java -Xint #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 java.io.*;
import java.util.*;
import java.util.stream.*;

public final class knucleotide {

   static ArrayList<String> seqLines() throws IOException 
   {
      final var in = new BufferedReader(new InputStreamReader(System.in));
      String line;
      while ((line = in.readLine()) != null) {
         if (line.startsWith(">THREE")) break;
      }
      final var lines = new ArrayList<String>();
      while ((line = in.readLine()) != null) {
         if (line.startsWith(">")) break;
         lines.add(line);
      }    
      return lines;      
    }         

   static HashMap<String,Integer> baseCounts(int bases, String seq)
   {
      var counts = new HashMap<String,Integer>();  
      final int size = seq.length() + 1 - bases;     
      for (int i = 0; i < size; i++) {
         var nucleo = seq.substring(i,i+bases);
         Integer v;
         if ((v = counts.get(nucleo)) != null) {
            counts.put(nucleo, v+1);
         } else {
            counts.put(nucleo, 1);
         }                           
      }      
      return counts;
   }
   
   static List<String> sortedFreq(int bases, String seq)
   {
      final int size = seq.length() + 1 - bases;
      return baseCounts(bases, seq).entrySet()     
         .stream()
         .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
         .map(e -> String.format("%s %.3f", 
               e.getKey(), 100.0 * e.getValue() / size))
         .toList();
   }   
   
   static int specificCount(String code, String seq)
   {
      return baseCounts(code.length(),seq).getOrDefault(code,0);   
   }

   public static void main(String[] args) throws Exception 
   {
      final var seq = seqLines().stream()
         .map(s -> s.toUpperCase())
         .collect(Collectors.joining(""));
      
      for (int i : Arrays.asList(1,2)) {  
         for (String s : sortedFreq(i,seq)) {      
            System.out.println(s); 
         }
         System.out.println();          
      }
      
      for (String code : Arrays.asList("GGT", "GGTA", "GGTATT",
            "GGTATTTTAATT", "GGTATTTTAATTTATAGT")) {       
         System.out.println(specificCount(code, seq) + "\t" + code);                        
      }
   }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
java 23 2024-09-17
Java HotSpot(TM) 64-Bit Server VM
(build 23+37-2369, 
interpreted mode, sharing)


 Tue, 12 Nov 2024 04:17:28 GMT

MAKE:
mv knucleotide.javaxint-8.javaxint knucleotide.java
/opt/src/jdk-23/bin/javac -d . -cp .:/opt/src/java-libs/fastutil-8.3.1.jar  knucleotide.java

3.55s to complete and log all make actions

COMMAND LINE:
 /opt/src/jdk-23/bin/java -Xint  -cp .:/opt/src/java-libs/fastutil-8.3.1.jar knucleotide 0 < knucleotide-input25000000.txt

TIMED OUT after 3600s


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