---
title: "Enzymology Final Report"
author: "Freddie Buck"
date: "May 2, 2017"
output:
  word_document:
    toc: yes
  pdf_document:
    toc: yes
  html_document:
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Introduction

In this report we present the enzymatic reaction from an unknown enzyme. We will make every attempt to understand the results that we obtained.

For this first report we will attempt to analyse the data with the enzyme kinetics method of [Michaelis-Menten](https://en.wikipedia.org/wiki/Michaelis%E2%80%93Menten_kinetics)

# Data source and method

Enzyme was from **unknown** source and tested in the laboratory under standard conditions.

The analysis method is derived from the script from [Professor Rob Beynon](http://www.liv.ac.uk/cpr/) in the U.K.

In particular we will calculate the ***Vmax*** and ***Km*** with nonlinear least square methods and provide a theoretical curve.

# Raw data

The data was obtained from an experiement performed by ***Freddie Buck*** on: **`r format(Sys.time(), "%a %b %d %H:%M %Y")`**.

The substrate data was collected and written in the lab notebook. 

A vector ***S*** can be created to contain the substracte concentrations:

```{r create_S}
S <- c(0,1,2,5,8,12,30,50)
```

Similarly vector ***v*** will contain the velocity results for each ***S*** value:

```{r create_v}
v <- c(0,11.1,25.4,44.8,54.5,58.2,72.0,60.1)
```

The data can be combined and presented in simple tabular form. 

There are `r length(S)` observations for ***S*** and `r length(v)` for ***v***.

(Inside note: the number of observations for each vector is derived from an `R` calculation with the `length()` function.)

```{r combine_data}
# Combine data in a dataframe:
kinData <- data.frame(S,v)

# Print the data:
kinData
```

# Data exploration

We can explore the data with a simple first plot describing the reaction.

```{r first_plot}
plot (S,v)
```

It seems that the plot is consistent with an enzymatic reaction and matches the Michaelis-Menten equation.

# Model building

This enzymatic data plot indicates that the Michaelis-Menten equation can be used. 

We can use a statistical method to create a model and calculate constant parameters ***Km*** and ***Vmax***.

For this we will use standard statistical functions built-in in `R` base installation: `nls()` and `predict()` described as follows by `R` help:

`nls()`: *Determine the nonlinear (weighted) least-squares estimates of the parameters of a nonlinear model.* `nls()` is non-linear least squares optimiser that we can use to solve the ***Km*** and ***Vmax*** parameters based on first approximation obtained by visual inspection of the plot. 

`predict()` will be used to predict a theoretical curve based on the `nls()` calculations and a series of hypothetical ***S*** dilutions.

## Predict Vmax and Km
The Michaelis-Menten equation:

```{r theoretical_curve_formula}
# "velocity = Vmax times S divided by (Km plus S)", stored in MMcurve
MMcurve <- formula(v ~ Vmax*S/(Km+S))
```

By inspecting the plot we can determine approximate values for ***Km*** and ***Vmax*** that can be given to `nls()` in order to compute values closer to the truth:

***Vmax*** is about `50`

***Km*** is about `2` (value of [S] when Vmax is half.)

```{r}
bestfit <- nls(MMcurve, kinData, start=list(Vmax=50,Km=2))
# print the results
bestfit
```

We can therefore estimate the final values of ***Km*** and ***Vmax*** from this result rounded to 2 significant digits:

***Vmax*** = `r round(coef(bestfit)[1], digits = 2)`

***Km*** = `r round(coef(bestfit)[2], digits = 2)`

## Theoretical curve

We can use the model above to build a theoretical curve: we first create a list of hypothetical concentrations of the substrate ***S*** and then use the `predict()` funtion with the `bestfit` data obtained by `nls()` to compute theoretical values for velocity `v`:

```{r theoretical_curve_points}
# Hypothetical oncentration ranges for S:
SconcRange <- seq(0,50,0.1)

# Calculate the predicted velocities using the predict() function:
theorLine <- predict(bestfit,list(S=SconcRange))

```

This will be used in the final plot.

# Final plot based on data and model

This final plot contains all the data points and the theoretical curve, with the values of ***Vmax*** and ***Km*** included on the plot.

```{r replot_final}
# Now plot the data, the best fit line, and put the best fit coefficients in the plot
plot (kinData, 
      xlab="Subtrate (mM)", 
      ylab="Velocity (nmol/s)", 
      title(main="Fitted MM data"), 
      pch=17, col="blue", cex=1.5)

# Now add the theoretical line with the lines() function:
lines(SconcRange,theorLine,col="red")

# Add text with the values:
text(28,30, "Vmax = ")
text(35,30,round(coef(bestfit)[1],2))
text(29,25, "Km = ")
text(36,25,round(coef(bestfit)[2],2))
```


# Conclusion

In conclusion we can report that this enzyme follows a typical Michaelis-Menten pattern and the equation can be solved with ***Vmax*** = `r round(coef(bestfit)[1], digits = 2)` 
and
***Km*** = `r round(coef(bestfit)[2], digits = 2)`
based on 
`r length(S)` observations for ***S*** and `r length(v)` for ***v***.


# R Session:
```{r sessionInfo}
sessionInfo()
```

