Objectives
- Understand and work with functions and strings
- Be able to test functions
You can find project1 note: functions and stringβ s β on Moodle.
Question 1: pyramid, surface area
A Pyramid with a square base, 4 triangular faces, and an apex is a square pyramid. Write a function β surfaceAreaβΒ Β Β Β Β Β Β Β Β Β Β Β Β Β β to calculate and returnβΒ Β Β Β Β Β Β Β Β Β Β Β the surface area of a square pyramid. The function takes two doubleβΒ Β Β Β Β Β Β Β parameters β the base length(b) and slant height(s) of a squareβΒ Β Β Β Β pyramid, and calculates and returns the surface area as a doubleβΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β .β
Function specifications:Β
- The function name: surfaceAreaβ
- The function parameters (in this order)
βΒ Β Β The base length, doubleβ
βΒ Β Β The slant height, doubleβ
- The function return value: surface area, doubleβ Β Β Β Β Β Β Β Β The function should not print anything.
- Edge cases: if one or more arguments are not positive numbers, then it should return -1
Β
Sample run 1 (boldβ is user input)β
Enter the base length: 3β
Enter the slant height: 4β
The surface area: 33
Β
Sample run 2 (boldβ is user input)β
Enter the base length: 3β
Enter the slant height: -1β
Invalid values
Β
The file should be named as pyramid.cppβ . Donβt forget to head over to the code runner onβΒ Β Β Β Β Β Β Moodle and paste only your function in the answer box! In the main, make sure that you call the function and output the surface area.
Question 2: Braking Distance
Displacement, D (i.e. a distance) for a braking vehicle can be calculated with the initial velocity, Iβ, the final velocity, βFβ, and deceleration,β aβ.
Write a function called βdisplacementβ that takes as parameters: initial velocity, βIβ, final velocity Fβ, and decelerationβ βaβ.
Function specifications: Β
- Your function must be named βdisplacement
- Your function takes three parameters (all double, in this order)
βΒ Β Β Initial velocity βI
βΒ Β Β Final velocity βF
βΒ Β Β Deceleration βa
- Your function should return the appropriate displacement, βDβ, of the vehicle as βdoubleβ. If one or more values are invalid, the function print all the error messages in this order and return β0β.
- If your function receives negative velocities then it must print ββVelocity should be greater than zero.ββ
- If your function receives a zero deceleration then it must print ββNo brakes were applied.ββ
- If your function receives a negative deceleration then it must print ββThe vehicle is speeding up.ββ
- If your function receives βF > Iβ then it must print ββError in acceleration values.ββ
Β
For example:β Bob is riding his bicycle to the store at a velocity of 4 m/s, when an aardvark runs out in front of him. He quickly brakes to a complete stop, with a deceleration of 2 m/s. What is his displacement?
Note that because Bob is stopped, the final velocity, βF = 0β. His initial velocity, βI = 4β m/s. The deceleration is β2β m/s.
Therefore the function should return 4.
Sample run 1 (boldβ is user input)β
Enter initial velocity: 10β
Enter final velocity: 1β
Enter deceleration: 5β
Displacement is 9.9
Sample run 2 (boldβ is user input)β
Enter initial velocity: 1β
Enter final velocity: 2β Enter deceleration: 0β
No brakes were applied.
Error in acceleration values.
The file should be named as displacement.cppβΒ . Donβt forget to head over to the codeβΒ Β Β Β Β Β Β Β Β Β Β Β Β runner on Moodle and paste only your function in the answer box! In the main, make sure that you call the function and output the displacement.
Question 3: change to upper case
Write a function called toUpperβΒ Β Β Β Β Β Β Β Β that has a single parameter and returns a new string with allβΒ Β Β Β Β Β Β Β Β Β Β Β Β characters converted to uppercase.
Β
Function specificationsβ:
- The function name: toUpperβ Β Β Β Β Β Β Β Β The function parameter
βΒ Β Β The string that will be converted to upper cases, stringβΒ Β Β Β βΒ Β Β Β Β The function return value: the string with all upper cases, stringβΒ βΒ Β Β Β Β The function should not print anything.
- Edge cases: Non alphabetical characters remain unchanged.
Β
Sample run 1 (boldβ is user input)β
Enter the string: Β Β Β Β Β Β Β Β Convert this string to UPPER case!!!βΒ Β Β Β Β CONVERT THIS STRING TO UPPER CASE!!!
Β
The file should be named as toUpper.cppβ . Donβt forget to head over to the code runner onβΒ Β Β Β Β Β Β Moodle and paste only your function in the answer box! In the main, make sure that you call the function and output the surface area.
DNA Analyzer
Background: Measuring DNA Similarity
Β
DNA β is the hereditary material in humans and other species. Almost every cell in a personβs body has the same DNA. All information in DNA is stored as code in four chemical bases: adenine (Aβ )β , guanine (Gβ )β , cytosine (Cβ )β and thymine (Tβ )β .Β The differences in the order of these bases is a means of specifying different information.
Β
We refer to an organismβs complete set of chemical bases as their genomeβΒ Β Β Β Β Β Β Β Β Β Β Β Β Β β. Every genome looks something like this:
Β
GATCAATGAGGTGGACACCAGAGGCGGGGACTTGTAAATAACACTGGGCTGTAGGAGTGATGGGGTTCACCTCTAATTCTAAGATGGCTAGATAAT
GCATCTTTCAGGGTTGTGCTTCTATCTAGAAGGTAGAGCTGTGGTCGTTCAATAAAAGTCCTCAAGAGGTTGGTTAATACGCATGTTTAATAGTACA GTATGGTGACTATAGTCAACAATAATTTATTGTACATTTTTAAATAGCTAGAAGAAAAGCATTGGGAAGTTTCCAACATGAAGAAAAGATAAATGGTC
AAGGGAATGGATATCCTAATTACCCTGATTTGATCATTATGCATTATATACATGAATCAAAATATCACACATACCTTCAAACTATGTACAAATATTATAT
ACCAATAAAAAATCATCATCATCATCTCCATCATCACCACCCTCCTCCTCATCACCACCAGCATCACCACCATCATCACCACCACCATCATCACCAC
CACCACTGCCATCATCATCACCACCACTGTGCCATCATCATCACCACCACTGTCATTATCACCACCACCATCATCACCAACACCACTGCCATCGTCA
TCACCACCACTGTCATTATCACCACCACCATCACCAACATCACCACCACCATTATCACCACCATCAACACCACCACCCCCATCATCATCATCACTAC
TACCATCATTACCAGCACCACCACCACTATCACCACCACCACCACAATCACCATCACCACTATCATCAACATCATCACTACCACCATCACCAACACC
A
Human Genome: First 1000 lines of Chromosome 1
The human genome has about 3 billion DNA base pairs. That means the entire human genome can be represented by a (very long) string of ~3 billion characters, where every character in the string is A, C, G, or T. You can find the first 1000 lines of Chromosome 1 of the Human Genome at this webpagβΒ Β Β Β e.β
In the lectures, there have been examples of string representation and use in C++. In this assignment, we will create strings that represent DNA sequences.Β We will be implementing a number of functions that are used to search for substrings that represent sequences within the DNA.
Interaction of DNA (orange) with histones (blue), a DNA binding protein. These proteinsβ basic amino acids bind to the acidic phosphate groups on DNA. Thomas Splettstoesser
One of the challenges in computational biologβΒ Β Β Β Β Β Β Β Β Β Β Β Β yβ is determining where a DNA binding proteiβ n β will bind in a genome.Β Each DNA binding protein has a preference for a specific sequence of nucleotides.Β This preference sequence is known as a motif.Β The locations of a motif within a genome are important to understanding the behavior of a cellβs response to a changing environment.
To find each possible location along the DNA, we must consider how well the motif matches the DNA sequence at each possible position.Β The protein does not require an exact match to bind and can bind when the sequence is similar to the motif.
Another common DNA analysis function is the comparison of newly discovered DNA genomic sequences to the large databases of known DNA sequences and using the similarity of the sequences to help identify the origin of the new sequences.
Β
Goal of your project
Suppose that the emergency room of a hospital sees a sudden and drastic increase in patients presenting with a particular set of symptoms. Doctors determine the cause to be bacterial, but without knowing the specific species involved they are unable to treat patients effectively. One way of identifying the cause is to obtain a DNA sample and compare it against known bacterial genomes. With a set of similarity scores, doctors can make more informed decisions regarding treatment, prevention, and tracking of the disease.
Β
For this project (questions 4 β 7) you will be writing a program that performs three main functionalities:
- Calculate the similarity score (This will be question 4)
- Find the best similarity score in the given genome (This will be question 5)
- Given three genomes and a sequence from the unknown bacteria, determine the most probable match (This will be question 6)
Β
So, in question 7, you will be developing the mainβΒ Β Β function using the functions you create inβΒ Β Β Β Β problems 4-6. Weβve nicely broken down this program into three functions. Youβre welcome to write additional helper functions as you need. Write your code and test each function on your Cloud 9. Then, once you complete, you can submit it on Moodle coderunner to make sure itβs fully functional. All the functions and the mainβ function should be in one file,β
DNAanalyzer.cpp.β
Β
Question 4: calcSimScore
Write a function calcSimScoreβΒ Β Β Β Β Β Β Β that returns the similarity score for two sequences. TheβΒ Β Β Β Β Β Β similarity score for two sequences is calculated as follows:
similarity_score = (string_length β hamming_distance) / string_length
Where hamming distancβΒ Β Β Β Β Β Β Β Β Β Β e is the number of positions at which the corresponding characters areβΒ Β Β Β Β Β Β different. Two strings with a small Hamming distance are more similar than two strings with a larger Hamming distance. The two strings must always be the same lengthβΒ Β Β Β Β Β Β Β Β Β Β Β when calculating aβΒ Β Β Β Β Β Hamming distance. If the length of the genomes is different, then the similarity score cannot be calculated. Similarity score will be in the range [0.0,1.0].
Β
Example:βΒ Β Β Β Β Β Β Β Β Β first string = βACCTβΒ Β Β Β Β Β Β Β Β Β Β Β Β Β second string = βACCGβ
Β
AΒ C C T
|Β Β |Β Β |Β *
A C C G
In this example, string_length = 4. Since there is one mismatch, the hamming_distance = 1 . And the similarity score for the two strings on this example would be, similarity_score = (4-1)/4 =Β 3/4Β Β = 0.75
Function specificationsβ:
- The function name: calcSimScoreβ
- The function parametesr(in this order):
βΒ Β Β Sequence 1, stringβ
βΒ Β Β Sequence 2, stringβ
- The function should return the similarity score as a doubleβ
- If the length of the strings is the same, then the function calculates the similarity score. If not, the similarity score would be 0.
- The function should not print anything.
Β
Examples: Arguments and their expected return values for the β calcSimScoreβ
| sequence1 | sequence2 | Similarity scoreΒ |
| ATGC | ATGA | 0.75 |
| CCDCCD | CCDCCD | 1 |
| ATG | GAT | 0 |
| ACCDT | ACT | 0 |
Β
Once you have tested your code on Cloud 9 / VS code, then head over to code runner on Moodle and only your function in the answer box! Make sure that your unit tests are in the main function (commented out).
Question 5: findBestSimScore
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.
Function specificationsβ:
- The function name: findBestSimScoreβ Β Β Β Β Β Β Β Β The function 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)Β βΒ The function should return the highest similarity score as a double.
- If the length of the sequence is longer than the genome, then the function should return
- (the best similarity score is 0)
- The function should not print anything.
- The best similarity scores is [0.0,1.0]
Note:β 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. Follow the example below:
| genome sub-sequence | sequence | similarity score |
| ATAβCGC | ACT | 0.33 |
| AβTACβGC | ACT | 0 |
| ATβACGβC | ACT | 0.66 |
| ATAβCGC | ACT | 0 |
Β
βΒ findBestSimScore βΒ Β Β Β Β Β Β Β Β Β Β Β returns 0.66,βΒ Β Β Β since that is the highest similarity score found
Once you have tested your code on Cloud 9 / VS code,Β then head over to code runner on Moodle and only your functions in the answer box! Make sure that your unit tests are in the main function (commented out).
Question 6: findMatchedGenome
Write a function called findMatchedGenome that takes three genomes and a sequence and prints the list of matched genomes.
Function specificationsβ:
- The function name: findMatchedGenomeβ Β Β Β Β Β Β Β Β The function parameters(in this order):
- Genome 1, a string
βΒ Β Β Genome 2, a string
βΒ Β Β Genome 3, a string
βΒ Β Β sequence (sub-sequence of the genomes), a stringΒ βΒ Β Β Β Β Β Β Β The function should not return anything.
- Unexpected values might be passed into the function and your function should be able to handle them without crashing the program:
- Case 1: If one or more genomes or the sequence is an empty string, then your function should onlyβ printΒ ββ Genomes or sequence is empty.βΒ Β Β Β Β Β Β Β Β Β ββ
β Case 2: If the length of the three genomes are different, your function should only printΒ βLengths of genomes are different.βΒ Β Β Β Β Β Β Β Β Β Β Β ββ
βΒ Β Β Your function must check these cases in the order specified above; i.e. first check for empty genomes or sequences, then check for different length genomes.
Β
We compute the highest similarity scores found in each genome. Among the three highest similarity scores, the best matched genome is the one with the highest similarity score. If two or more genomes have the same highest similarity scores, then it lists the genomes with the highest similarity score.
Β
| genome 1 | AATGTTTCAC |
| genome 2 | GACCGACTAA |
| genome 3 | AAGGTGCTCC |
| sequence | TACTA |
| Genome 2 is the best match. | |
Example1: One of the genomes has the best matchβ
Β
Explanation:
Since the length of the three genomes (genome1, genome2 and genome3) are the same, we calculate the best similarity score with each genome. The best-matched genome is then calculated based on the highest of the previously calculated best similarity scores.
Β
Β
| Β | Β | Highest similarity score |
| genome 1 | AATGTβTTCAC | 0.4 |
| genome 2 | GACCβGACTAβA | 0.8 |
| genome 3 | AAGGβTGCTCβC | 0.6 |
Β
The best similarity score for genome1 and sequence is 0.4, for genome2 and sequence is 0.8, and for genome3 and sequence is 0.6. Since genome 2 has the highest similarity scores among the three genomes, genome 2 is best matched.
Β
| genome 1 | AACT |
| genome 2 | AACT |
| genome 3 | AATG |
| sequence | AACT |
| Genome 1 is the best match. Genome 2 is the best match. | |
Example2: Two genomes match with a sequenceβ
Β
Explanation:Β
The best-matched genome is calculated based on the highest similarity scores given in each sequence.
| Β | Β | Highest Similarity Score |
| genome 1 | AACT | 1.0 |
| genome 2 | AACT | 1.0 |
| genome 3 | AATG | 0.5 |
Β
The length of the three genomes are the same, so we calculate the best similarity score. The best similarity for genome1 and genome2 will be 1.0, while the genome3 is 0.5. Since genome 1 and 2 have higher similarity scores than the genome3, it will be printed in the format specified.
Β
Example 3 (edge case):β length of genomes is different Β
| genome 1 | AACTC |
| genome 2 | AACT |
| genome 3 | AATG |
| sequence | AACT |
| Lengths of genomes are different. | |
Β
Example 4 (edge case): One or more genomes or sequence is emptyβ
| genome 1 | AACTC |
| genome 2 | Β |
| genome 3 | AATG |
| sequence | AACT |
| Genomes or sequence is empty. | |
Β
Once you have tested your code on Cloud 9 / VS code, then head over to code runner on Moodle and paste only your functions in the answer box! Make sure that your unit tests are in the main function (commented out).
Β
Question 7: Put it all together
To make it usable for doctors and researchers, weβll create a menu option and call the appropriate functions you have created.
Β
The menu has the following options:
- Calculate similarity score
- Find the best similarity score
- Analyze the genome sequences
- Quit
Β
The menu will run on a loop, continually offering the user four options until they opt to quit. You need to fill in the code for each of the options. Be sure to use the functions you wrote in questions 4-6.
Option1:β Find similarity score
The program asks two sequences (sequence1 and sequence2), then it displays the similarity score between the two sequences.
Enter sequence 1: AACT
Enter sequence 2:
AATC
Similarity score: 0.5
Β
Β
Option2:β Find the best similarity score
The program asks for a genome and sequence (subset of the genome) and it displays the highest similarity scores found in the genome.
| Enter genome:
AATCTCTTTAA Enter sequence: TCA Best similarity score: 0.666667 |
Β
Option3:β Analyze the genome sequences
In this option, the program takes 3 genomes and a sequence and shows the best matched genome.
Enter genome 1: AATGTTTCAC Enter genome 2: GACCGACTAA Enter genome 3: AAGGTGCTCC Enter sequence:
TACTA
Genome 2 is the best match.
Β
Option4:β Quit
When the user opt this option, the program prints βGood bye!ββ And exits the program.β
Β
Invalid option
Your program should be able to handle the user error (i.e. users might choose the option that is not listed). If an invalid option is entered, the program should display βInvalidβΒ Β Β Β Β Β Β Β Β Β Β Β option.β and show the menu options again.
Β
Sample run (boldβ is user input)β
Select a numerical option:
=== menu ===
- Find similarity score
- Find the best similarity score
- Analyze the genome sequences
- Quit
10
Invalid option.
Select a numerical option:
=== menu ===
- Find similarity score
- Find the best similarity score
- Analyze the genome sequences
- Quit
4 Good bye!
Β
Β
Below is an example of running the main program.
| Select a numerical option:
=== menu === 1.Β Find similarity score 2.Β Find the best similarity score 3.Β Analyze the genome sequences 4.Β Quit 1 Enter sequence 1: ATTCT Enter sequence 2: TAACT Similarity score: 0.4 Select a numerical option: === menu === 1.Β Find similarity score 2.Β Find the best similarity score 3.Β Analyze the genome sequences 4.Β Quit 2 Enter genome: ATTCCCTAA Enter sequence: TAC Best similarity score: 0.666667 Select a numerical option: === menu === 1.Β Find similarity score 2.Β Find the best similarity score 3.Β Analyze the genome sequences 4.Β Quit 3 Enter genome 1: ACCTT Enter genome 2: TCCTT Enter genome 3: TCCTT |
Enter sequence:
CCT
Genome 1 is the best match.
Genome 2 is the best match.
Genome 3 is the best match.
Select a numerical option:
=== menu ===
- Find similarity score
- Find the best similarity score
- Analyze the genome sequences
- Quit
6
Invalid option.
Select a numerical option:
=== menu ===
- Find similarity score
- Find the best similarity score
- Analyze the genome sequences
- Quit
4
Good bye!
Β
Once you have tested your code on Cloud 9 / VS code,Β then head over to code runner on Moodle and paste the entire program (functions from questions 4 β 6 and the mainβΒ Β Β Β Β Β Β Β function fromβΒ this question 7) in to the answer box!
Β



