Below is my highly inefficient and non working code for converting RNA sequence to Protein:
下面是我将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;
}
Now there are two problems associated with it:
现在有两个与之相关的问题:
- It is not working(Array out of bound exception)
- 它不起作用(Array out of bound of exception)
- 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
有什么方法可以使用正则表达式来解决这个问题吗? (我的意思是在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)
3 个解决方案
#1
3
To increase speed I would use a static Map for all available codons
为了提高速度,我会对所有可用的密码子使用静态Map
Use a StringBuilder to build your finalresult
使用StringBuilder构建最终结果
Be sure that your String input is a multiple of 3
确保您的String输入是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();
}
#2
0
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:
首先,您可以使用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"));
}
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 ?
另外,我不确定是否一个单位增加i真的是你想要的,因为我在这个领域什么都不知道,如果翻译的话,它应该增加3?
As a final remark, this question would possibly be more at home on the Code Review Stack Exchange.
作为最后的评论,这个问题可能在Code Review Stack Exchange上更为重要。
#3
0
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.
您获得ArrayIndexOutOfBoundsException的原因是因为您没有增加索引变量i。因此,您不断重复前三个字符。
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.
根本原因是,当您使用“继续”时,您将继续执行while循环的下一次迭代。而且因为你只在所有可能的匹配后增加i,所以你永远不会达到这个声明。因此,请尝试在大if-then-else语句之前添加增量。
Also, you might want to increment by 3, and loop while i is smaller than or equal to k -3.
此外,您可能希望增加3,并在i小于或等于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;
}
#1
3
To increase speed I would use a static Map for all available codons
为了提高速度,我会对所有可用的密码子使用静态Map
Use a StringBuilder to build your finalresult
使用StringBuilder构建最终结果
Be sure that your String input is a multiple of 3
确保您的String输入是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();
}
#2
0
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:
首先,您可以使用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"));
}
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 ?
另外,我不确定是否一个单位增加i真的是你想要的,因为我在这个领域什么都不知道,如果翻译的话,它应该增加3?
As a final remark, this question would possibly be more at home on the Code Review Stack Exchange.
作为最后的评论,这个问题可能在Code Review Stack Exchange上更为重要。
#3
0
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.
您获得ArrayIndexOutOfBoundsException的原因是因为您没有增加索引变量i。因此,您不断重复前三个字符。
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.
根本原因是,当您使用“继续”时,您将继续执行while循环的下一次迭代。而且因为你只在所有可能的匹配后增加i,所以你永远不会达到这个声明。因此,请尝试在大if-then-else语句之前添加增量。
Also, you might want to increment by 3, and loop while i is smaller than or equal to k -3.
此外,您可能希望增加3,并在i小于或等于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;
}