136 2/5/2017 12:28:25 PM


:

Scan left-to-right through the genome.

  • If start codon ATG found, set beg to index i.
  • If stop codon TAG found and substring length is a multiple of 3,
    • print gene and
    • reset beg to -1.

 

:

public class GeneFind
{
 public static void main(String[] args)
 {
 String start = args[0];
 String stop = args[1];
 String genome = StdIn.readAll();
 int beg = -1;
 for (int i = 0; i < genome.length() - 2; i++)
 {
 String codon = genome.substring(i, i+3);
 if (codon.equals(start)) beg = i;
 if (codon.equals(stop) && beg != -1 && beg+3 < i)
 {
 String gene = genome.substring(beg+3, i);
 if (gene.length() % 3 == 0)
 {
 StdOut.println(gene);
 beg = -1;
 }
 }
 }
 }
}

 e.g.

A T A G A T G C A T A G C G C A T A G C T A G A T G T G C T A G C