简体   繁体   中英

Genome Processing Server - Method is returning 0 and I don't understand why (java)

For my assignment, I must calculate the best similarity score in a genome when compared to a sequence. To determine the similarity score you must have the length of the sequence subtracted by the hamming distance all divided by the length of the sequence. The hamming distance is how many pieces of a substring of the genome does not equal the sequence. For example GTTC and AGGT have a hamming distance of 4 because they don't equal each other at any point.

If you were to give the method the genome="ATACGC" and the sequence="ACT" the best similarity score should be 0.67. But when I run my program it will only return 0. I am using an android app to interact with my genome processing server. Thank you, if you can help me with this issue!!!

Code to the method that is returning a 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;
}

I think it is because your answer is being cast as an integer. Here is how I arrived to the correct answer.

Add this:

Double len = sequence.length() * 1.0; 

Add it before the following line:

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

Then replace that line with this:

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

That should give you the answer you're looking for.

EDIT: fixed a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM