alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (knucleotide.java)

This example Groovy source code file (knucleotide.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

arraylist, arraylist, bufferedreader, comparator, exception, future, hashmap, io, map, map, string, string, stringbuilder, stringbuilder, threading, threads, three, util

The Groovy knucleotide.java source code

/* The Computer Language Benchmarks Game
 http://shootout.alioth.debian.org/
 
 contributed by James McIlree
 */

import java.util.*;
import java.io.*;
import java.util.concurrent.*;

public class knucleotide {
    String sequence;
    int count = 1;
    
    knucleotide(String sequence) {
	this.sequence = sequence;
    }

    static ArrayList<Callable< Map > createFragmentTasks(final String sequence, int[] fragmentLengths) {
	ArrayList<Callable> tasks = new ArrayList>>();
	for (int fragmentLength : fragmentLengths) {
	    for (int index=0; index<fragmentLength; index++) {
		final int offset = index;
		final int finalFragmentLength = fragmentLength;
		tasks.add(new Callable<Map() {
		    public Map<String, knucleotide> call() {
			return createFragmentMap(sequence, offset, finalFragmentLength);
		    }
		});
	    }
	}
	return tasks;
    }
    	
    static Map<String, knucleotide> createFragmentMap(String sequence, int offset, int fragmentLength) {
	HashMap<String, knucleotide> map = new HashMap();
	int lastIndex = sequence.length() - fragmentLength + 1;
	for (int index=offset; index<lastIndex; index+=fragmentLength) {
	    String temp = sequence.substring(index, index + fragmentLength);
	    knucleotide fragment = (knucleotide)map.get(temp);
	    if (fragment != null)
		fragment.count++;
	    else
		map.put(temp, new knucleotide(temp));
	}

	return map;
    }
        
    // Destructive!
    static Map<String, knucleotide> sumTwoMaps(Map map1, Map map2) {
	for (Map.Entry<String, knucleotide> entry : map2.entrySet()) {
	    knucleotide sum = (knucleotide)map1.get(entry.getKey());
	    if (sum != null)
		sum.count += entry.getValue().count;
	    else
		map1.put(entry.getKey(), entry.getValue());
	}
	return map1;
    }
    
    static String writeFrequencies(Map<String, knucleotide> frequencies) {
	ArrayList<knucleotide> list = new ArrayList(frequencies.size());
	int sum = 0;
	for (knucleotide fragment : frequencies.values()) {
	    list.add(fragment);
	    sum += fragment.count;
	}
	
	Collections.sort(list, new Comparator<knucleotide>() {
	    public int compare(knucleotide o1, knucleotide o2) {
		int c = o2.count - o1.count;
		if (c == 0) {
		    c = o1.sequence.compareTo(o2.sequence);
		}
		return c;
	    }
	});
	
	StringBuilder sb = new StringBuilder();
	for (knucleotide k : list)
	    sb.append(String.format("%s %.3f\n", k.sequence.toUpperCase(), (float)(k.count) * 100.0f / (double)sum));
	
	return sb.toString();
    }
    
    static String writeCount(List<Future> futures, String nucleotideFragment) throws Exception {
	int count = 0;
	for (Future<Map future : futures) {
	    knucleotide temp = future.get().get(nucleotideFragment);
	    if (temp != null) count += temp.count;
	}
	
	return count + "\t" + nucleotideFragment.toUpperCase();
    }
    
    public static void main (String[] args) throws Exception {		
	String line;
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	while ((line = in.readLine()) != null) {
	    if (line.startsWith(">THREE")) break;
	}
	
	StringBuilder sbuilder = new StringBuilder();
	while ((line = in.readLine()) != null) {
	    sbuilder.append(line);
	}
	
	ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
	int[] fragmentLengths = { 1, 2, 3, 4, 6, 12, 18 };
	List<Future> futures = pool.invokeAll(createFragmentTasks(sbuilder.toString(), fragmentLengths));
	pool.shutdown();
	
    	// Print the length 1 & 2 counts. We know the offsets of the tasks, so we can cheat.
	System.out.println(writeFrequencies(futures.get(0).get()));
	System.out.println(writeFrequencies(sumTwoMaps(futures.get(1).get(), futures.get(2).get())));
	
	System.out.println(writeCount(futures, "ggt"));
	System.out.println(writeCount(futures, "ggta"));
	System.out.println(writeCount(futures, "ggtatt"));
	System.out.println(writeCount(futures, "ggtattttaatt"));
	System.out.println(writeCount(futures, "ggtattttaatttatagt"));
    }
    
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy knucleotide.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.