Search code examples
javabioinformaticsdna-sequence

Efficient way of converting RNA sequence to protein in Java, and arrayoutofbound error


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)

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();
    }