-1

To find a gene in a genome, we scan for the start codon, ATG, remember its index, then scan from the next stop codon, TAG. If the length of the intervening sequence is a multiple of 3, we have found a gene. Return the gene

Here's what I've gotten so far:

public static void main(String[] args) {
    System.out.println("This is a gene finder program.");
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the string that you want to locate genes:");
    String geneString = scan.nextLine();
    System.out.println(scanGene(geneString));
}
public static String scanGene(String geneString) {
    int i = 0;
    int x = 3;
    int y = 3;
    double startCondon, endCondon;
    while (i < geneString.length() - 3) {
        i = i+1;
        x = x+1;                
        if(geneString.substring(i,x) == "ATG") {
            startCondon = geneString.indexOf(x+1);
            while (y<geneString.length()) {
                y = y+1;
                if (geneString.substring(y,y+4) == "TAG") {
                    endCondon = geneString.indexOf(y-1);
                    if ((endCondon - startCondon) % 3 == 0)
                        System.out.println("We have found a gene!");
                }
            }

    }
}
    return geneString.substring(x+1,y);
}

Help please? It doesn't seem to be working...for some reason it gives me an error "Out of range" the index is a negative number or something. I've looked over the code but can't seem to find anything too wrong with it.... Thanks

4

2 回答 2

2

Let me help you make it waaay simpler. Just use a regex match. The following will do exactly what you want, it populates "matches" with all of the results. Alternatively you can use m.group() and put the result anywhere you want.

String dna = "xxxATGxxxxxxxxxxxxxxTAGxxxxxxxxx";
Matcher m = Pattern.compile("ATG.*TAG").matcher(dna);
List<String> genes = new ArrayList<String>();
while (m.find()) {
    genes.add(m.group());
}

And what your "Index out of range" error means is you are trying to access an index in an array that doesn't exist. EG you have an array of size 10, if you try to access array[10], or array[-1] or anything outside of [0-9], you'll get this error. Good luck with it!

于 2013-11-12T16:04:00.947 回答
0

First of all, improve your variable names... Second of all, add some debug statements.

Before you substring, print the index of the start and stop of what you're about to index. Be sure it's NEVER -1.

于 2016-08-21T05:05:02.690 回答