我有DNA序列(如String),由A,T,G和C字符组成。我想知道G和C的百分比。我尝试了两种方法,但只有一种方法有效,我想知道为什么。
方法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;
}
此方法只计算我感兴趣的字符的出现次数。这样可以正常工作但在应用公式后,对于我给方法的每个字符串,输出为0.0
。
方法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;
}
此方法有效,并提供正确的输出。
纯粹出于好奇,为什么第一种方法不起作用而后者呢?有什么根本我缺席的吗?我想了解更好地了解Java。
我必须赞扬这些答案:
For the first method
For the second