0

I have DNA sequences (as String) consisting of A, T, G and C chars. I want to know the percentage of G's and C's. I tried two methods of doing so but only one works and I wonder why.

Method 1:

public double calculateGc (String seq) {

    //count occurrences of G and C
    int nucC = seq.length() - seq.replace("C", "").length();
    int nucG = seq.length() - seq.replace("G", "").length();

    //implement formula: (G+C) / total * 100
    double gcContent = (nucG + nucC) / seq.length() * 100;

    return gcContent;
}

This method simply counts the occurrences of the chars I'm interested in. This works fine but after the formula is applied the output is 0.0 for every String I give to the method.

Method 2:

public double calculateGc (String seq) {

    //count occurrences of G and C
    int count = 0;
    double gcContent;
    for (int i = 0; i < seq.length(); i++) {
        if (seq.charAt(i) == 'G' || seq.charAt(i) == 'C') {
            count++;
        }
    }

    //implement formula
    gcContent = (count / (double)seq.length()) * 100;

    return gcContent;
}

This method works and gives the correct output.

Purely out of curiosity, why doesn't the first method works while the latter does? Is there something fundamental I'm missing? I would like to find out to get to know Java better.


I have to give credit to these answers:
For the first method
For the second

Community
  • 1
  • 1
Iarwain
  • 199
  • 10

0 Answers0