-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneFinder.java
76 lines (69 loc) · 2.06 KB
/
GeneFinder.java
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
// Sarthak Gupta
// 11/14/2019
// Includes all methods to find all of the genes in a strand of DNA
public class GeneFinder {
// finds the gene in the DNA string by calling other methods
public static String findGene(String DNA, int where) {
String emptystring = "";
int startIndex = DNA.indexOf("ATG", where);
if (startIndex == -1) {
return emptystring;
}
int TAAIndex = findStopCodon(DNA, startIndex, "TAA");
int TAGIndex = findStopCodon(DNA, startIndex, "TAG");
int TGAIndex = findStopCodon(DNA, startIndex, "TGA");
int minIndex = Math.min(TAAIndex, Math.min(TAGIndex, TGAIndex));
if (minIndex == DNA.length()) {
return emptystring;
}
return DNA.substring(startIndex, minIndex + 3);
}
// finds the location of the first stop codon after a start codon
// STOP CODONS: TAA, TAG, or TGA
public static int findStopCodon(String dna, int startIndex, String stopCodon) {
int currIndex = dna.indexOf(stopCodon, startIndex + 3);
while (currIndex != -1) {
int diff = currIndex - startIndex;
if (diff % 3 == 0) {
return currIndex;
} else {
currIndex = dna.indexOf(stopCodon, currIndex + 1);
}
}
return dna.length();
}
// returns all of the genes found in a string of DNA
public static String printAllGenes(String DNA) {
int startIndex = 0;
int numGenes = 0;
String genes = "";
while (true) {
String currentGene = findGene(DNA, startIndex);
if (currentGene.isEmpty()) {
break;
}
genes += (currentGene + "\n");
numGenes++;
startIndex = DNA.indexOf(currentGene, startIndex) + currentGene.length();
}
// System.out.println("Total number of genes found: " + numGenes);
return ("Total number of genes found: " + numGenes + "\n\n" + genes);
}
// counts the number of codons in the DNA string
public static int countCodon(String DNA, String codon) {
int count = 0;
int startIndex = 0;
if (startIndex == -1) {
return -1;
}
while (true) {
int codonIndex = DNA.indexOf(codon, startIndex + 3);
if (codonIndex == -1) {
break;
}
count++;
startIndex = codonIndex + 3;
}
return count;
}
}