基因组处理服务器 - 方法返回 0,我不明白为什么 (java)



对于我的作业,我必须计算基因组中与序列相比的最佳相似性分数。要确定相似性分数,您必须将序列的长度减去汉明距离,全部除以序列的长度。汉明距离是基因组子链的多少片段不等于序列。例如,GTTC和AGGT的汉明距离为4,因为它们在任何时候都不相等。

如果你给方法基因组="ATACGC"和序列="ACT",最好的相似性得分应该是0.67。但是当我运行我的程序时,它只会返回 0。我正在使用安卓应用程序与我的基因组处理服务器进行交互。 谢谢,如果你能帮我解决这个问题!!

返回 0- 的方法的代码

public static String similarityScore(String genome, String sequence)
{
double bestSimilarity;
double similarity;
int i;
int j;
int hammingDistance;
String subString;
String miniSubString;
String subSequence;
String returnStr;
DecimalFormat df;
df=new DecimalFormat("#.##");
bestSimilarity=-1;
i=0;
//makes a substring of the genome so you can compare the substring to the sequence
//increments i by 1 each iteration to move down the string to make new substrings
while((i+sequence.length())<(genome.length()+1) && i<genome.length())
{
subString = genome.substring(i, i + sequence.length());
hammingDistance=0;
for (j=0;j<sequence.length();j++)
{
// these two lines make substrings of a single character
//to compare if they equal each other
miniSubString = subString.substring(j, j + 1);
subSequence = sequence.substring(j,j+1);
//if they dont equal each other increase hamming distance by 1
if(!miniSubString.equals(subSequence))
hammingDistance++;
}
//calculates hammingdistance, which is the
// (length of the sequence - the hamming distance) / the length of the sequence
similarity=(sequence.length()-hammingDistance)/sequence.length();
if (similarity>bestSimilarity)
bestSimilarity=similarity;
i++;
}
returnStr=df.format(bestSimilarity);
return returnStr;
}

我认为这是因为您的答案被转换为整数。这是我得出正确答案的方式。

添加这个:

Double len = sequence.length() * 1.0; 

将其添加到以下行之前:

similarity=(sequence.length()-hammingDistance)/sequence.length();

然后将该行替换为:

similarity=(sequence.length()-hammingDistance)/len;

这应该给你你想要的答案。

编辑:修复了一条线。

最新更新