In: Computer Science
In C++
Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double.
Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome.
Your function MUST be named findBestSimScore
Your function should take two parameters in this order:
a string parameter for the genome (complete set of genes)
a string parameter for the sequence (sub-sequence of the genome)
Your function should return the highest similarity score as a double.
Your function should not print anything.
The best similarity scores is [0.0,1.0]
Our sequence is "ACT", which is a string of length 3. That means we need to compare our sequence with all the 3 character long sub-sequences (substrings) in the genome.
Examples:
genome sub-sequence |
sequence |
similarity score |
|
ATACGC |
ACT |
0.33 |
|
ATACGC |
ACT |
0 |
|
ATACGC |
ACT |
0.66 |
← findBestMatch returns 0.66, since that is the highest similarity score found |
ATACGC |
ACT |
0 |
NOTE: The complete program code is given to implement findBestSimScore() function. Please ignore the complete program code, if you want function code only.
Function Code to Copy:
double findBestSimScore(string genome, string seq)
{
// Declare a variable count to find the number of
// matching of a sequence.
// Declare and initialize a variable max to 0 to
// hold maximum number of a matching sequence.
int count, max=0;
// Check whether the length of genome is less than
// sequence, then return 0.
if(genome.length()<seq.length())
{
return 0;
}
// Use for loop to traverse through elements of
// genome and sequence to find number of matchings.
for(int i=0;i<genome.length()-seq.length()+1;i++)
{
// Initialize count variable to 0.
count=0;
// Inner for loop is used to compare all
// character of sequence with same number of
// genome string.
for(int j=0;j<seq.length();j++)
{
// Check genome character is equal to sequence
// character or not.
if(genome[i+j]==seq[j])
// Increment the count variable by 1.
count++;
}
// Check if current count of matching is greater
// than previous one.
// Assign highest count value to max variable.
if(count>max)
{
max=count;
}
}
// Return a double type highest similarity score by
// dividing max value with length of sequence string.
return max/double(seq.length());
}
_________________________________________________________________________________
NOTE : PLEASE IGNORE COMPLETE PROGRAM CODE, IF YOU WANT FUNCTION CODE ONLY.
_________________________________________________________________________________
Complete Program Code to Copy:
// Include the required header files.
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
// Define a double type function findBestSimScore() to
// take genome and sequence as arguments and returns
// the highest similarity score.
double findBestSimScore(string genome, string seq)
{
// Declare a variable count to find the number of
// matching of a sequence.
// Declare and initialize a variable max to 0 to
// hold maximum number of a matching sequence.
int count, max=0;
// Check whether the length of genome is less than
// sequence, then return 0.
if(genome.length()<seq.length())
{
return 0;
}
// Use for loop to traverse through elements of
// genome and sequence to find number of matchings.
for(int i=0;i<genome.length()-seq.length()+1;i++)
{
// Initialize count variable to 0.
count=0;
// Inner for loop is used to compare all
// character of sequence with same number of
// genome string.
for(int j=0;j<seq.length();j++)
{
// Check genome character is equal to sequence
// character or not.
if(genome[i+j]==seq[j])
// Increment the count variable by 1.
count++;
}
// Check if current count of matching is greater
// than previous one.
// Assign highest count value to max variable.
if(count>max)
{
max=count;
}
}
// Return a double type highest similarity score by
// dividing max value with length of sequence string.
return max/double(seq.length());
}
// Start the main() function
int main()
{
// Declare two string type variable to hold genome
// and sequence value.
string genome, seq;
// Declare variables to hold the highest similarity
// score.
double s, score;
// Prompt the user to input genome string and sequence
// string.
cout<<"Enter a genome sub-sequence : ";
getline(cin,genome);
cout<<"Enter a sub-sequence to be searched : ";
getline(cin,seq);
// Call the findBestSimScore() function and store the
// returned highest similarity score by function.
s=findBestSimScore(genome,seq);
// Store the highest similarity score without rounding
// off the last digit.
score=floor(100*s)/100;
// Display the highest similarity score up to 2 decimal
// points by using fixed and setprecision.
cout<<"Highest similarity score : ";
cout<<fixed<<setprecision(2)<<score<<endl;
// Return 0 to the main() function.
return 0;
// End of the main() function.
}