home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (regexdna.java)

This example Groovy source code file (regexdna.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

bufferedreader, bufferedreader, d, h, io, k, matcher, r, regex, string, string, stringbuffer, stringbuffer, w, w, y

The Groovy regexdna.java source code


/* The Computer Language Shootout
   http://shootout.alioth.debian.org/
   contributed by Josh Goldfoot
   based on the Nice entry by Isaac Guoy
*/

import java.io.*;
import java.lang.*;
import java.util.regex.*;

public class regexdna {
    
    public regexdna() {
    }

    public static void main(String[] args) {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer(10240);
        char[] cbuf = new char[10240];
        int charsRead = 0;
        try {
            while ((charsRead = r.read(cbuf, 0, 10240)) != -1) 
                sb.append(cbuf, 0, charsRead);
        } catch (java.io.IOException e) {
            return;
        }
        String sequence = sb.toString();
        
        int initialLength = sequence.length();
        sequence = Pattern.compile(">.*\n|\n").matcher(sequence).replaceAll("");
        int codeLength = sequence.length();
        
        String[] variants = { "agggtaaa|tttaccct" ,"[cgt]gggtaaa|tttaccc[acg]", "a[act]ggtaaa|tttacc[agt]t", 
                 "ag[act]gtaaa|tttac[agt]ct", "agg[act]taaa|ttta[agt]cct", "aggg[acg]aaa|ttt[cgt]ccct",                     
                 "agggt[cgt]aa|tt[acg]accct", "agggta[cgt]a|t[acg]taccct", "agggtaa[cgt]|[acg]ttaccct" };
        for (int i = 0; i < variants.length; i++) {
            int count = 0;
            Matcher m = Pattern.compile(variants[i]).matcher(sequence);
            while (m.find())
                count++;
            System.out.println(variants[i] + " " + count);
        }
        
        sequence = Pattern.compile("B").matcher(sequence).replaceAll("(c|g|t)");
        sequence = Pattern.compile("D").matcher(sequence).replaceAll("(a|g|t)");
        sequence = Pattern.compile("H").matcher(sequence).replaceAll("(a|c|t)");
        sequence = Pattern.compile("K").matcher(sequence).replaceAll("(g|t)");
        sequence = Pattern.compile("M").matcher(sequence).replaceAll("(a|c)");
        sequence = Pattern.compile("N").matcher(sequence).replaceAll("(a|c|g|t)");
        sequence = Pattern.compile("R").matcher(sequence).replaceAll("(a|g)");
        sequence = Pattern.compile("S").matcher(sequence).replaceAll("(c|g)");
        sequence = Pattern.compile("V").matcher(sequence).replaceAll("(a|c|g)");
        sequence = Pattern.compile("W").matcher(sequence).replaceAll("(a|t)");
        sequence = Pattern.compile("Y").matcher(sequence).replaceAll("(c|t)");
        
        System.out.println();
        System.out.println(initialLength);
        System.out.println(codeLength);
        System.out.println(sequence.length());
    }
}

Other Groovy examples (source code examples)

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

new blog posts

 

Copyright 1998-2016 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.