Ekaterina1234
Ekaterina1234

Reputation: 410

Writing a method that calls a member variable

I want to find genes in a DNA string. Genes have START and STOP codons.

The start codon is ATG. The stop codons are: TGA, TAA, TAG.

This is an example of a DNA string: AAATGCCCTAATTAGAATAGTTCAA

In order to find a gene, we must find the start codon (ATG) and the closest stop codon (TAG, TGA or TAA), that is a multiple of 3 characters away from the start codon, so in this case the gene would be

ATGCCCTAA

and not

ATGCCCTAATTAG,

or

ATGCCCTAATTAGAATGA,

because TAG is 7 chars away from ATG and TGA, even though it's 12 chars away, it's not the closest STOP codon.

I am to write a method called printAll, with one parameter, String dna, that calls the findStopIndex member variable, which will be used to print all the genes found in a DNA String, such as this one:CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA.

Here's the code I wrote so far:

public class ProteinFinder {
    public void printAllStarts(String dna)  {
        int start = 0;
        while (true) {
            int loc = dna.indexOf("atg", start);
            if (loc == -1) {
                break;
            }
            System.out.println("Starts at " + loc);
            start = loc + 3;
        }
    }

    public int findStopIndex(String dna, int index) {
        int stop1 = dna.indexOf("tga", index);
        if (stop1 == -1 || (stop1 - index) % 3!=0) { 
            stop1 = dna.length();
        }
        System.out.println(stop1);

        int stop2 = dna.indexOf("taa", index);
        if (stop2 == -1 || (stop2 - index) %3 !=0) {
            stop2 = dna.length();
        }
        System.out.println(stop2);
        int stop3 = dna.indexOf("tag", index);
        if (stop3 == -1 || (stop3 - index) %3 !=0) {
            stop3 = dna.length();
        }
        System.out.println(stop3);
        return Math.min(stop1, Math.min(stop2, stop3));
    }    
    public void printAll(String dna) {

    }    
}

Could you help me write this method? Thank you.

Upvotes: 3

Views: 122

Answers (1)

user6482572
user6482572

Reputation:

This should work:

public void printAll(String dna) {
    int start = 0;
    while (true) {
        int atg = dna.indexOf("atg", start);
        if (atg == -1) {
           break; 
        }
        int end = findStopIndex(dna, atg+3);
        if (end != dna.length()) {
            System.out.println(dna.substring(atg, end+3));
            start = end + 3;
        }
        else {
            start = start+3;
        }
    }
}  

Upvotes: 2

Related Questions