java doubt

0 paras pokhrel · October 23, 2014
hi, Im a new user... i have this code: im taking the input as pelpevettrrrlrplvlgqtlrqvvhrdpgrrlevpelkmdtkkvr from an external txt file but it is taking ages to run the code and give the result... in most cases it terminates... its taking more than 40 minutes if i keep the word length of input to 20 letters... need help to rectify it... plz help.



import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;

* Find the Frequency of Codon and Print the combinations of CODONS for the given Word
* @author
public class CountCodon {
   private static int count;

   private static String file = "C:\\input.txt";

   public static void main(String[] args) throws Exception {

       BufferedReader br = new BufferedReader(new FileReader(new File(file)));
       String[] words = br.readLine().trim().split(" ");

       Map<Character,String[]> hash=init();
       for(String word : words)
           combinations(hash, word , word.length(),0 ,"");
           System.out.println("Total Combinations :"+count);
           Collection<String[]> codons = hash.values();

           Iterator it = codons.iterator();

               String[] codes = (String[]);
               for(String code : codes )
                   int freq = getFrequencyCount(code, word,hash);
                   System.out.println("CODON : "+code + " ---> " + "FREQ : " +freq);


   private static void solve(String word, String codon) {
       Map<Character,String[]> hash=init();

       combinations(hash, word , word.length(),0 ,"");
       System.out.println("Total Combinations :"+count);

       int freq = getFrequencyCount(codon, word,hash);
       System.out.println("CODON : "+codon + " ---> " + "FREQ : " +freq);

    * Returns the frequency count of the CODON
    * @param codon
    * @param word
    * @param hash
    * @return
   private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {

       int currCount = 1;

       int len=word.length();
       //count the total combinations
       for(int i=0;i<len;i++)
           if(hash.get(word.charAt(i)) != null)
           currCount = currCount * hash.get(word.charAt(i)).length ;

       //System.out.println("Total Combinations :"+currCount);

       //Iterate over map and fetch, the character and size of the codons corresponding to that character
       char c = 0;
       int size=0;
       for(Entry<Character, String[]> entry : hash.entrySet())
           String[] vals =entry.getValue();
           for(String v:vals)
               if(v.equalsIgnoreCase(codon)) //if codon is found, get the character

       //If no charater is found corresponding to the CODON
           return 0;

       //No. of times the character appears in the word, tricky
       int charCount = word.split(c+"").length-1;

       return charCount * (currCount/size);

    * Prints All combinations of the codons
    * @param hash: storage unit of character and corresponding codons
    * @param word: the given word , eg:
    * @param len: length or 'word'
    * @param i: currrent character in 'word'
    * @param s: combination formed in intermediate steps
   private static void combinations(Map<Character, String[]> hash,
           String word, int len , int i,String s) {

       //if currCharacter reaches end of word, print

       //current Character
       char c = word.charAt(i);
       //Codons corresponding to the character
       String[] codons = hash.get(c);

       String temp = s;
           for(int j=0;j<codons.length;j++)
               s=temp; //initialize s , with the value held in temp everytime s gets modified
               s = s+":"+codons[j];

    * Initialzing Map
    * @return: returns map of character and its corresponding Codons
   private static Map<Character,String[]> init() {
       Map<Character,String[]> hash = new HashMap<Character,String[]>();
       hash.put('F',new String[]{"UUU","UUC"});
       hash.put('I',new String[]{"AUU","AUC","AUA"});
       hash.put('P',new String[]{"CCU","CCC","CCA","CCG"});
       hash.put('S',new String[]{"UCU","UCC","UCA","UCG","AGU","AGC"});
       hash.put('T',new String[]{"ACU","ACC","ACA","ACG"});
       hash.put('A',new String[]{"GCU","GCC","GCA","GCG"});
       hash.put('E',new String[]{"CAA","CAG"});
       hash.put('L',new String[]{"UUA","UUG","CUU","CUC","CUA","CUG"});
       hash.put('V',new String[]{"GUU","GUC","GUA","GUG"});
       hash.put('T',new String[]{"UAU","UAC"});
       hash.put('R',new String[]{"CGU","CGC","CGA","CGG","AGA","AGG"});
       hash.put('K',new String[]{"AAA","AAG"});
       hash.put('Y',new String[]{"UAU","UAC"});
       hash.put('H',new String[]{"CAU","CAC"});
       hash.put('N',new String[]{"AAU","AAC"});
       hash.put('D',new String[]{"GAU","GAC"});
       hash.put('C',new String[]{"UGU","UGC"});
       hash.put('W',new String[]{"UGG"});
       hash.put('G',new String[]{"GGU","GGC","GGA","GGG"});
       hash.put('M',new String[]{"AUG"});
       return hash;


Post a Reply


Oldest  Newest  Rating
0 Alex Sweps · October 23, 2014
Comment out chunks of your code until it runs fast. Then you can narrow where it starts to slow down and go from there. Iv never worked with hashmaps.
0 paras pokhrel · October 24, 2014
im taking a protein sequence and decoding it into codons... n outputting the frequency as well as the combinations of codons.... im running it on eclipse... its taking hours...
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator