0

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)
4

3 回答 3

3

为了提高速度,我会为所有可用的密码子使用静态地图

使用 StringBuilder 构建最终结果

确保您的字符串输入是 3 的倍数

确保每个三元组对应一个有效密码子

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();
}
于 2014-05-07T16:21:49.557 回答
0

首先,您可以使用 substring() 方法一次性构建子字符串。然后,您可以使用 StringBuilder 构建输出。最后,您可以使用 switch 语句,这使代码更具可读性。结果如下所示:

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

另外,我不确定将 i 增加一个单位是否真的是您想要的,因为我在这方面一无所知,如果进行翻译,也许应该增加 3?

作为最后的评论,这个问题在 Code Review Stack Exchange 上可能更常见。

于 2014-05-07T16:07:56.913 回答
0

你得到一个 ArrayIndexOutOfBoundsException 的原因是你没有增加你的索引变量 i。因此,您不断重复前三个字符。

根本原因是,当您使用“继续”时,您将继续进行 while 循环的下一次迭代。而且因为你只在所有可能的匹配后增加 i,你永远不会达到这个语句。因此,尝试将增量放在大 if-then-else 语句之前。

此外,您可能希望增加 3,并在 i 小于或等于 k ​​-3 时循环。

减少一些不必要的代码的建议是:

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;
}
于 2014-05-07T16:16:12.193 回答