-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindingGene.java
More file actions
78 lines (53 loc) · 1.59 KB
/
FindingGene.java
File metadata and controls
78 lines (53 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package week2;
public class FindingGene {
public static void main(String[] args) throws Exception {
String testDNA = args[0];
String codon = findProtein(testDNA);
String stopPos = stopCodon(codon);
System.out.format("Protein %s \n", codon);
System.out.format("End Tag %s \n", stopPos);
}
/**
* @param dna
*/
public static String findProtein(String dna) {
dna = dna.toLowerCase();
//find start position of the codon
int start = dna.indexOf("atg");
if (start == -1) {
System.out.println("Cannot find codon");
return null;
}
//find end position of the codon
int end = findEndPosition(dna, start);
if (end == -1) {
return null;
}
System.out.format("End Position: %s \n", end);
return dna.substring(start, end + 3);
}
public static int findEndPosition(String dna, int startPos) {
String[] endTags = {"tag", "tga", "taa"};
int endPos = -1;
for (String s : endTags) {
int i = dna.indexOf(s, startPos + 3);
int diff = (startPos - i) % 3;
if (i != -1 && diff == 0) {
endPos = i;
break;
}
}
return endPos;
}
public static String stopCodon(String codon) {
if (codon == null) {
return "";
}
int size = codon.length();
if (size > 10) {
return codon.substring(size - 3, size);
} else {
return "";
}
}
}