Please download the answer file and edit it on Rstudio. When you finish, send the answers.R file to my mailbox. Be sure to use the correct email address and send only one file. If you send the wrong file, your grade will be 0. Please wait until you receive a confirmation email before closing your computer.

You can use your personal handwritten notes and the help window of RStudio. All questions are independent and can be answered in any order. Some questions may be answered in more than one way, only one correct answer is required.

Write your student number in the correct place at the beginning of the answer file. All answers are strictly personal. Any unethical behavior will be penalized.

# 1 Decomposition: Turtle in the Snow

A snowflake is a shape like the figure below. As you can see there are different levels of snow.

Snow level 1 is just a straight line of length L. Snow level 2 is made of four parts, each one is a straight line (that is, snow level 1) of length L/3. The turtle draws the first part, turns left 60 degrees, draws the second part, turns 120 degrees to the right, draws another part, turns 60 degrees leftm, and draws the last part. Snow level 3 has also four parts, each made of snow level 2 of length L/3. In general snow level N of length L is made of four parts of snow level N-1 of length L/3.

To draw a snow flake we need a recursive function, called snow(N, L), with two inputs: the level N and the length L. Please write the code of this function.

If your function is correct, you can test it with the following code, and you should get this figure.

library(grid)
library(TurtleGraphics)
turtle_init(mode="clip")
turtle_hide()
turtle_setangle(90)
turtle_setpos(10, 1)
snow(1, 80)
turtle_setpos(10, 5)
snow(2, 80)
turtle_setpos(10, 20)
snow(3, 80)
turtle_setpos(10, 37)
snow(4, 80)
turtle_setpos(10, 55)
snow(5, 80)
turtle_setpos(10, 75)
snow(6, 80) # 2 Algorithm design

In many important cases we have a vector x with growing values. That is, each value is bigger or equal to the previous one, and x[i+1] >= x[i] for all i indices. It is easy to see that the minimum value has to be at position 1. We also know that the maximum value is at the last position. What about the half value?

The half value is the average of the minimum and the maximum. For example if x is the vector c(1, 4, 4, 6, 10, 15) then the half value is (1+15)/2, that is 8.

The location of the half value of the vector x is the index of the first value that is equal or bigger than the half value of x. In the example the location of the half value is 5, since x is the smallest value that is bigger or equal than 8.

Please write a function called locate_half(), with one input called x. The function must return a single number, which is the index of the smallest value in x that is bigger than or equal to the average of minimum and maximum of x.

You can test your functions with the following code. The output should be this:

x <- 1:9
locate_half(x)
##  5
locate_half(x + 20)
##  5
locate_half(x * x)
##  7
locate_half(sqrt(x))
##  4

# 3 Systems: Polymerase Chain Reaction

The Polymerase Chain Reaction (PCR) is a method used to synthesize millions of copies of a given DNA sequence. A typical PCR reaction consists of series of thermic cycles involving template DNA denaturation, primer annealing, and extension of the annealed primers by DNA polymerase. This three-step cycle is repeated 25-30 times.

We can represent the PCR reaction as a system with two parts, DNA and primers and one process, the thermal cycle. Here we simplify the reaction and we forget about the polymerase and the dNTP. They both will be represented by primers. The system is represented by this diagram:

## 3.1 Simulate several PCR cycles

Please write a function to simulate the PCR reaction. The function name should be pcr and it must take four inputs: the number of cycles N, the initial DNA concentration dna_ini, the initial primer concentration primer_ini (default value 1E8), and the reaction rate rate (default value 1E-8). The function must return a data frame with two columns, named dna and primer.

If all is right, you should see this result

pcr(N=6, dna_ini=1e6)
##        dna    primer
## 1  1000000 100000000
## 2  2000000  99000000
## 3  3980000  97020000
## 4  7841396  93158604
## 5 15146331  85853669
## 6 28150012  72849988

## 3.2 PCR depends on initial concentration

The PCR reaction curve depends on the initial concentration of DNA. We want to understand this dependency for the following values of initial DNA concentration:

initial_dna <- 10**(0:6)
initial_dna
##  1e+00 1e+01 1e+02 1e+03 1e+04 1e+05 1e+06

You have to create a data frame named conc. This data frame has seven columns, with the result of pcr() for each of the initial DNA concentrations in initial_dna. That is, from one DNA molecule per litre up to one million molecules per litre. Simulate the PCR reaction for 30 cycles, and store only the value of the dna column.

If you did it correctly, you can use this code to create the following plot.

plot(x=c(1,30), y=c(0, max(conc)), type="n", xlab="PCR cycle",
ylab="DNA Concentration", main="Effect of initial DNA on PCR")
legend("topleft", legend=initial_dna, pch=1:7)
for(i in 1:ncol(conc)) {
points(conc[[i]], pch=i, type="b")
} # 4 Bonus: Quantitative PCR

We can combine question 2 and 3. Please write the code (not a function) to make a vector called CT. The vector CT contains the half value of each column of the data frame conc from Question 3.2. You can use the function locate_half() from Question 2.

If the code is correct, you can use this code to produce the following plot. We conclude that the CT value can be used to predict the initial concentration of DNA.

plot(initial_dna ~ CT, log="y") 