Question: 6.21 P06-01 DNA Batch Analysis with Functions Summary: in an earlier exercise we analyzed a set of DNA strands for validity, CG percentage, and overall

6.21 P06-01 DNA Batch Analysis with Functions

Summary: in an earlier exercise we analyzed a set of DNA strands for validity, CG percentage, and overall stability. We going to redo that exercise, this time with user-defined functions.

In exercise 5.23, we wrote a program that analyzed a set of DNA strands for validity, CG percentage, and overall stability. For example, given the following input sequence of 4 DNA strands:

CCGA

ACTGATGC

ACTGATTX

TTCAA

#

The program would output the following analysis results:

CCGA: valid,CG=75%,highly stable

ACTGATGC: valid,CG=50%,stable

ACTGATXC: invalid

TTCAA: valid,CG=20%,unstable

The solution for this exercise is: ////

#include

#include

using namespace std;

int main()

{

string DNA;

cin >> DNA; // read first DNA strand:

while (DNA != "#") // if not end of input marker #, process input:

{

cout << DNA << ": ";

//

// First, is the DNA strand valid?

//

bool isValid = true;

for (unsigned int i = 0; i < DNA.length(); i=i+1)

{

if (DNA[i] != 'A' && DNA[i] != 'C' && DNA[i] != 'G' && DNA[i] != 'T')

{

isValid = false;

}

}

if (isValid == false) // not valid:

{

cout << "invalid";

}

else

{

cout << "valid,";

//

// Valid, so let's count the 'C' and 'G' bases:

//

double countCG = 0.0;

for (unsigned int i = 0; i < DNA.length(); i=i+1)

{

if (DNA[i] == 'C' || DNA[i] == 'G')

{

countCG = countCG + 1.0;

}

}

double percent;

percent = (countCG / DNA.length()) * 100.0;

cout << "CG=" << percent << "%,";

if (percent >= 75.0)

cout << "highly stable";

else if (percent >= 50.0)

cout << "stable";

else if (percent >= 25.0)

cout << "less stable";

else

cout << "unstable";

}

cout << endl;

cin >> DNA; // read next DNA strand:

}

return 0;

}

////

What we're doing to do is rewrite this program using three functions of our own design: IsValid(DNA), GetCGPercentage(DNA), and GetStability(percentage). Recall that we discussed this idea in class on Monday, October 2nd: lecture week 06, Day 09, PPT, PDF.

The provided code is actually spread across 2 C++ source files: "main.cpp" and "analysis.cpp". Start by reviewing the code for "main.cpp" which is visible in the editor pane --- note that this code is read-only (you cannot modify it). In particular, focus on the code within the while loop. You'll see that it calls the 3 functions mentioned earlier: IsValid, GetCGPercentage, and GetStability.

Now let's review the code for the user-defined functions. Above the editor pane you'll see the text "Current file: main.cpp", with a little drop-down arrow to the right. Click the drop-down and select "analysis.cpp". Then, immediately click the link "Load default template..." --- this avoids a bug that appears in some web browsers (which fail to properly load the file). You only need to do this once, the first time you view the file.

The editor pane should now be displaying the code for our three functions: IsValid, GetCGPercentage, and GetStability. Scroll through the file and confirm all 3 functions are defined. You'll also notice the body of each function says "TODO". Your task is to implement each of these functions to perform the duty summarized in the header comment above each function.

When all three functions are properly implemented, you will pass all 4 test cases. To start, work in Develop mode, enter the following input sequence

CCGA

ACTGATGC

ACTGATTX

TTCAA

#

and Run. The output will be incorrect. Then implement the first function, IsValid, and run again. Repeat until all 3 functions are implemented and work. Then switch to Submit mode and Submit for grading. When implementing your functions, keep in mind that "every function is an island". The only variables you have access to are the parameters, e.g. DNA or percentage. If you need a variable, declare it locally, i.e. at the top of the function just after the opening {. And normally, functions do not input from the keyboard and do not output to the screen --- so do not use cin nor cout in your functions. This is certainly true for this exercise, since the main() function is doing the cin and cout operations.

/*main.cpp*/

#include

#include

using namespace std;

//

// declarations of our user-defined functions:

//

bool IsValid(string DNA);

double GetCGPercentage(string DNA);

string GetStability(double percentage);

//

// main:

//

int main()

{

string DNA;

cin >> DNA;

while (DNA != "#")

{

cout << DNA << ": ";

if (IsValid(DNA) == false)

{

cout << "invalid" << endl;

}

else

{

cout << "valid,CG=";

double percentage = GetCGPercentage(DNA);

cout << percentage << "%,";

string stability = GetStability(percentage);

cout << stability << endl;

}

cin >> DNA;

}

return 0;

}

/*analysis.cpp*/

#include

#include

using namespace std;

//

// IsValid(dna)

// Returns true if the DNA strand is valid, false if not.

//

bool IsValid(string DNA)

{ //

// TODO:

//

return false;

}

//

// GetCGPercentage(dna)

//

// Returns the percentage of C and G bases in the DNA strand.

//

double GetCGPercentage(string DNA)

{

//

// TODO:

//

return -1.0;

}

//

// GetStability(percentage)

//

// Given the percentage of C and G bases, returns a string

// denoting the stability of the underlying DNA strand:

// "highly stable", "stable", "less stable", or "unstable".

//

string GetStability(double percentage)

{

//

// TODO:

//

return "todo";

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!