以下是我将RNA序列转换为蛋白质的非常低效且无法工作的代码:
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;
}
现在有两个与之相关的问题:
有什么办法可以用正则表达式解决这个问题吗? (我的意思是在perl中) 编辑:整个例外
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)
答案 0 :(得分:3)
为了提高速度,我会为所有可用的密码子使用静态地图
使用StringBuilder构建最终结果
确保您的String输入是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();
}
答案 1 :(得分: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"));
}
另外,我不确定是否一个单位增加我真的是你想要的,因为我不知道这方面的任何事情,如果进行翻译,它可能会增加3?
作为最后的评论,这个问题可能在Code Review Stack Exchange上更为重要。
答案 2 :(得分: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;
}