Question

Below is my highly inefficient and non working code for converting RNA sequence to Protein:

String translation(String temp)
{
    char[] result;
    int k=temp.length();
    int i=0;
    int z=0;
    char[] pro=new char[100];
    result=new char[temp.length()];
    result = temp.toCharArray();
    while(i<k-3)
    {
     char[] store;
     store=new char[1];
     store[0]=result[i];
     String tempstore1 = new String(store);
     store[0]=result[i+1];
     String tempstore2 = new String(store);
     store[0]=result[i+2];
     String tempstore3 = new String(store);
     String storefinal=tempstore1+tempstore2+tempstore3;

     if(storefinal.matches("UUU")||storefinal.matches("UUC"))
     {
         pro[z]='F';
         z++;
         continue;
     }
     if(storefinal.matches("UUA")||storefinal.matches("UUG")||storefinal.matches("CUU")||storefinal.matches("CUC")||storefinal.matches("CUA")||storefinal.matches("CUA")||storefinal.matches("CUG"))
     {
         pro[z]='L';
         z++;
         continue;
     }
     if(storefinal.matches("AUU")||storefinal.matches("AUC")||storefinal.matches("AUA"))
     {
         pro[z]='I';
         z++;
         continue;
     }
     if(storefinal.matches("AUG"))
     {
         pro[z]='M';
         z++;
         continue;
     }
     if(storefinal.matches("GUU")||storefinal.matches("GUC")||storefinal.matches("GUA")||storefinal.matches("GUG"))
     {
         pro[z]='V';
         z++;
         continue;
     }
     if(storefinal.matches("UCU")||storefinal.matches("UCC")||storefinal.matches("UCA")||storefinal.matches("UCG"))
     {
         pro[z]='S';
         z++;
         continue;
     }
     if(storefinal.matches("AGA")||storefinal.matches("AGG"))
     {
         pro[z]='R';
         z++;
         continue;
     }
     if(storefinal.matches("AGU")||storefinal.matches("AGC"))
     {
         pro[z]='S';
         z++;
         continue;
     }
     if(storefinal.matches("UGG"))
     {
         pro[z]='W';
         z++;
         continue;
     }
     if(storefinal.matches("UGU")||storefinal.matches("UGC"))
     {
         pro[z]='C';
         z++;
         continue;
     }
     if(storefinal.matches("GAA")||storefinal.matches("GAG"))
     {
         pro[z]='E';
         z++;
         continue;
     }
     if(storefinal.matches("GAU")||storefinal.matches("GAC"))
     {
         pro[z]='D';
         z++;
         continue;
     }
     if(storefinal.matches("AAA")||storefinal.matches("AAG"))
     {
         pro[z]='K';
         z++;
         continue;
     }
     if(storefinal.matches("AAU")||storefinal.matches("AAC"))
     {
         pro[z]='N';
         z++;
         continue;
     }
     if(storefinal.matches("CAA")||storefinal.matches("CAG"))
     {
         pro[z]='Q';
         z++;
         continue;
     }
     if(storefinal.matches("CAU")||storefinal.matches("CAC"))
     {
         pro[z]='H';
         z++;
         continue;
     }
     if(storefinal.matches("UAU")||storefinal.matches("UAC"))
     {
         pro[z]='Y';
         z++;
         continue;
     }
     if(storefinal.matches("CCG")||storefinal.matches("CCA")||storefinal.matches("CCC")||storefinal.matches("CCU"))
     {
         pro[z]='P';
         z++;
         continue;
     }
     if(storefinal.matches("ACG")||storefinal.matches("ACA")||storefinal.matches("ACC")||storefinal.matches("ACU"))
     {
         pro[z]='T';
         z++;
         continue;
     }
     if(storefinal.matches("GCG")||storefinal.matches("GCA")||storefinal.matches("GCC")||storefinal.matches("GCU"))
     {
         pro[z]='A';
         z++;
         continue;
     }
     if(storefinal.matches("CGG")||storefinal.matches("CGA")||storefinal.matches("CGC")||storefinal.matches("CGU"))
     {
         pro[z]='R';
         z++;
         continue;
     }
     if(storefinal.matches("GGG")||storefinal.matches("GGA")||storefinal.matches("GGC")||storefinal.matches("GGU"))
     {
         pro[z]='G';
         z++;
         continue;
     }
     if(storefinal.matches("UAG")||storefinal.matches("UAA")||storefinal.matches("UGA"))
     {
         pro[z]='.';
         z++;
         continue;
     }
     i++;
    }    
    String finalreturn = new String(pro);
 return finalreturn;
}

Now there are two problems associated with it:

  1. It is not working(Array out of bound exception)
  2. I cannot figure out how to manage the flanking unassociated redundant codes, which don't code for anything.

Is there any way I can use regex to solve this problem? (I mean like in perl) Edit : whole exception

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100
at JSATS2.transcriplation.translation(transcriplation.java:197)
at JSATS2.TabExp.jButton7ActionPerformed(TabExp.java:835)
at JSATS2.TabExp.access$1100(TabExp.java:14)
at JSATS2.TabExp$12.actionPerformed(TabExp.java:599)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Was it helpful?

Solution

To increase speed I would use a static Map for all available codons

Use a StringBuilder to build your finalresult

Be sure that your String input is a multiple of 3

Be sure that every triplet corresponds to a valid codon

private static final Map<String, Character> codonsMap;
static
{
    codonsMap = new HashMap<String, Character>();
    codonsMap.put("UUU", 'F');
    codonsMap.put("UUC", 'F');
    codonsMap.put("UUA", 'L');
    //and so on for all codons
}

//be sure that length of temp is a multiple of 3 and that every 3 characters correspond to a valid codon
public String translation(String temp)
{
    //Use StringBuilder for adding Characters, it is by far faster than adding chars to a mutable String
    StringBuilder finalreturn = new StringBuilder();
    String codon;       
    for (int i = 0; i < temp.length() - 2; i++) {
        codon = temp.substring(i, i+3);
        finalreturn.append(codonsMap.get(codon));
    }
    return finalreturn.toString();
}

OTHER TIPS

First, you can build the substring in one go with the substring() method. Then, you can build the output with a StringBuilder. Finally, you can use a switch statement, which makes the code more readable. The result looks like this:

static String translation (String rna) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < rna.length() - 2; i++) {
        String triplet = rna.substring(i, i+3);

        switch (triplet) {
        case "UUU":
        case "UUC":
            sb.append("F");
            break;
        case "UUA":
        case "UUG":
        case "CUU":
        case "CUC":
        case "CUA":
        case "CUG":
            sb.append("L");
            break;
        case "AUU":
        case "AUC":
        case "AUA":
            sb.append("I");
            break;
        case "AUG":
            sb.append("M");
            break;
        case "GUU":
        case "GUC":
        case "GUA":
        case "GUG":
            sb.append("V");
            break;
            // and so on
        default:
            // do nothing;
        }           
    }       

    return sb.toString();
}

public static void main(String[] args) {
    System.out.println(translation("UUCGUGAUU"));
}

Also, I am not sure if incrementing i by one unit is really what you want since I don't know anything in this area, maybe it should be incremented by 3 if a translation is made ?

As a final remark, this question would possibly be more at home on the Code Review Stack Exchange.

The reason why you are getting an ArrayIndexOutOfBoundsException is because you do not increase your the index variable i. Therefore you keep repeating for the first three characters.

The underlying reason is that when you use "continue" you continue with the next iteration of the while loop. And because you only increment i after all possible matches, you will never reach this statement. Therefore try putting the increment just before the big if-then-else statement.

Also, you might want to increment by 3, and loop while i is smaller than or equal to k -3.

A suggestion to reduce some of the unnessecary code would be:

public static String translation(String temp) {
    int i = 0;
    String result = "";
    while (i <= temp.length() - 3) {
    String triplet = temp.substring(i,i+=3);
        if (triplet.equals("UUU") || triplet.equals("UUC"))
            result += 'F';
        if (triplet.equals("UUA") || triplet.equals("UUG")
                || triplet.equals("CUU") || triplet.equals("CUC")
                || triplet.equals("CUA") || triplet.equals("CUA")
                || triplet.equals("CUG"))
            result += 'L';
        if (triplet.equals("AUU") || triplet.equals("AUC")
                || triplet.equals("AUA"))
            result += 'I';
        if (triplet.equals("AUG"))
            result += 'M';
        if (triplet.equals("GUU") || triplet.equals("GUC")
                || triplet.equals("GUA") || triplet.equals("GUG"))
            result += 'V';
        if (triplet.equals("UCU") || triplet.equals("UCC")
                || triplet.equals("UCA") || triplet.equals("UCG"))
            result += 'S';
        if (triplet.equals("AGA") || triplet.equals("AGG"))
            result += 'R';
        if (triplet.equals("AGU") || triplet.equals("AGC"))
            result += 'S';
        if (triplet.equals("UGG"))
            result += 'W';
        if (triplet.equals("UGU") || triplet.equals("UGC"))
            result += 'C';
        if (triplet.equals("GAA") || triplet.equals("GAG"))
            result += 'E';
        if (triplet.equals("GAU") || triplet.equals("GAC"))
            result += 'D';
        if (triplet.equals("AAA") || triplet.equals("AAG"))
            result += 'K';
        if (triplet.equals("AAU") || triplet.equals("AAC"))
            result += 'N';
        if (triplet.equals("CAA") || triplet.equals("CAG"))
            result += 'Q';
        if (triplet.equals("CAU") || triplet.equals("CAC"))
            result += 'H';
        if (triplet.equals("UAU") || triplet.equals("UAC"))
            result += 'Y';
        if (triplet.equals("CCG") || triplet.equals("CCA")
                || triplet.equals("CCC") || triplet.equals("CCU"))
            result += 'P';
        if (triplet.equals("ACG") || triplet.equals("ACA")
                || triplet.equals("ACC") || triplet.equals("ACU"))
            result += 'T';
        if (triplet.equals("GCG") || triplet.equals("GCA")
                || triplet.equals("GCC") || triplet.equals("GCU"))
            result += 'A';
        if (triplet.equals("CGG") || triplet.equals("CGA")
                || triplet.equals("CGC") || triplet.equals("CGU"))
            result += 'R';
        if (triplet.equals("GGG") || triplet.equals("GGA")
                || triplet.equals("GGC") || triplet.equals("GGU"))
            result += 'G';
        if (triplet.equals("UAG") || triplet.equals("UAA")
                || triplet.equals("UGA"))
            result += '.';
    }
    return result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top