|
| 1 | + |
| 2 | +/** |
| 3 | + * Finding many Genes |
| 4 | + * |
| 5 | + * @author Deny Kiantono |
| 6 | + * @version 1 |
| 7 | + */ |
| 8 | +public class Part1 { |
| 9 | + public int findStopCodon(String dna, int startIndex, String stopCodon) { |
| 10 | + int index = 0; |
| 11 | + |
| 12 | + while(true) { |
| 13 | + index = dna.indexOf(stopCodon, startIndex + 3); |
| 14 | + |
| 15 | + if (index == -1 || (index - startIndex) % 3 == 0) { |
| 16 | + break; |
| 17 | + } |
| 18 | + |
| 19 | + startIndex += 3; |
| 20 | + } |
| 21 | + |
| 22 | + if (index != -1) { |
| 23 | + return index; |
| 24 | + } else { |
| 25 | + return dna.length(); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public void testFindStopCodon() { |
| 30 | + String dna = "xxxyyyzzzTAAxxxyyyzzzTAAxx"; |
| 31 | + |
| 32 | + int index = findStopCodon(dna, 0, "TAA"); |
| 33 | + System.out.println("Index = " + index); |
| 34 | + |
| 35 | + index = findStopCodon(dna, 9, "TAA"); |
| 36 | + System.out.println("Index = " + index); |
| 37 | + |
| 38 | + index = findStopCodon(dna, 1, "TAA"); |
| 39 | + System.out.println("Index = " + index); |
| 40 | + |
| 41 | + index = findStopCodon(dna, 0, "TAG"); |
| 42 | + System.out.println("Index = " + index); |
| 43 | + } |
| 44 | + |
| 45 | + public String findGene(String dna, int start) { |
| 46 | + final String START_CODON = "ATG"; |
| 47 | + int startIndex = dna.indexOf(START_CODON, start); |
| 48 | + |
| 49 | + if (startIndex == -1) { |
| 50 | + return ""; |
| 51 | + } |
| 52 | + |
| 53 | + int taaIndex = findStopCodon(dna, startIndex, "TAA"); |
| 54 | + int tagIndex = findStopCodon(dna, startIndex, "TAG"); |
| 55 | + int tgaIndex = findStopCodon(dna, startIndex, "TGA"); |
| 56 | + |
| 57 | + int minIndex = Math.min(taaIndex, Math.min(tagIndex, tgaIndex)); |
| 58 | + |
| 59 | + if (minIndex == dna.length()) { |
| 60 | + return ""; |
| 61 | + } else { |
| 62 | + return dna.substring(startIndex, minIndex + 3); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public void testFindGene() { |
| 67 | + String dna = "GTTAATGTAGCTTAAACCTTTAAAGCAAGGCACTGAAAATGCCTAGATGA"; |
| 68 | + System.out.println("Gene: " + findGene(dna, 0)); |
| 69 | + |
| 70 | + dna = "GTGAGCTCACTCCATAGACACAAAGGTTTGGTCCTGGCCTTCTTATTAGT"; |
| 71 | + System.out.println("Gene: " + findGene(dna, 0)); |
| 72 | + |
| 73 | + dna = "TTTCAGTGAGCTTACACATGCAAGTATCCGCGCGCCAGTGAAAATGCCCT"; |
| 74 | + System.out.println("Gene: " + findGene(dna, 0)); |
| 75 | + |
| 76 | + dna = "TCAAATCATTACTGACCATAAAGGAGCGGGTATCAAGCACACACCTATGT"; |
| 77 | + System.out.println("Gene: " + findGene(dna, 0)); |
| 78 | + |
| 79 | + dna = "AGCTCACAACACCTTGCTTAGCCACACCCCCACGGGATACAGCAGTGATA"; |
| 80 | + System.out.println("Gene: " + findGene(dna, 0)); |
| 81 | + } |
| 82 | + |
| 83 | + public void printAllGenes(String dna) { |
| 84 | + int start = 0; |
| 85 | + |
| 86 | + while (true) { |
| 87 | + String gene = findGene(dna, start); |
| 88 | + |
| 89 | + if (gene.isEmpty()) { |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + System.out.println("Gene: " + gene); |
| 94 | + |
| 95 | + start = dna.indexOf(gene, start) + gene.length(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public void testPrintAllGenes() { |
| 100 | + String dna = "ATGATCTAATTTATGCTGCAACGGTGAAGA"; |
| 101 | + printAllGenes(dna); |
| 102 | + } |
| 103 | +} |
0 commit comments