Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Practical: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
Line 115: Line 115:




==Part 2: Enzymatic reaction analysis==
<syntax type='R'>
#
# R Example, inspired by
#
#
#
# Enzymatic analysis
#
concentration <- c(0.3330, 0.1670, 0.0833, 0.0416, 0.0208, 0.0104, 0.0052)
velocity <- c(3.636, 3.636, 3.236, 2.660, 2.114, 1.466, 0.866)
# create data frame by combining data
df <- data.frame(concentration, velocity)
# print 'df'
df
# Correct one value from 'velocity'
velocity[4] <- 2.666
# Check that data frame 'df' has also been updated
df$velocity[4] # or df[4,2]
# Visualise data
plot(df$concentration, df$velocity)
# Linear regression
# create new variable to linearise plot
df$ytrans <- df$conc/df$vel
# plot new relationship
plot(df$conc, df$ytrans)
# compute linear regression
lmfit <- lm(ytrans~conc, data=df)
names(lmfit)
summary(lmfit)
plot(lmfit)
class(lmfit)
coef(lmfit)
plot(df$conc, df$ytrans)
abline(coef(lmfit))
Vm <- 1/coef(lmfit)[2]
K <- Vm*coef(lmfit)[1]
</syntax>
* Basic intro to 'R' language
* Basic intro to 'R' language
** types: vector, string, factor, data_frame
** types: vector, string, factor, data_frame

Revision as of 09:28, 6 October 2009

Fall 2009 - Synthetic Biology (MRes class)

Home        Lecture        'R' Tutorial        Resources        Literature

<html> <body> <!-- Start of StatCounter Code --> <script type="text/javascript"> var sc_project=3315864; var sc_invisible=0; var sc_partition=36; var sc_security="8bb2efcd"; </script>

<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script><noscript><div class="statcounter"><a class="statcounter" href="http://www.statcounter.com/"><img class="statcounter" src="http://c37.statcounter.com/3315864/0/8bb2efcd/0/" alt="blog stats" /></a></div></noscript> <!-- End of StatCounter Code -->

</body> </html>

Introduction to 'R'




Part 1: Random Walks

Below is a basic 'R' script to simulate a random walk along the X axis. Random walk is important to model as it is related to the phenomenon of diffusion.

  • Copy and save this script into a file.
  • Run this script within 'R'

<syntax type='R'>

############################
#
# Random Walk 1D / 1 Trace
#
############################
# number of samples
N <- 1000
# draw 3xN samples from normal distribution
samples_1 <- rnorm(N)
# Calculate 1D Paths 
path_1 <- cumsum(samples_1)
#Plot samples, time series, and histogram 
steps <- seq(1:N)
plot(steps, path_1, 

type='l', lwd = 2, col='red', main = "1D Random Walk", xlab = "Steps", ylab = "X axis", xlim = c(0, N), ylim = c(-50,50) ) </syntax>

Question

  • Modify this script so that you can overlay as many independent paths as you want (Tip = use the function capability of 'R').

Here is a modified script to model a 2D Random Walk.

<syntax type='R'>

  1. Random Walk 2D
  1. number of samples

N <- 1000

  1. draw N samples from normal distribution

x_samples <- rnorm(N, mean = 0, sd = 1) y_samples <- rnorm(N, mean = 0, sd = 0.5)

  1. Calculate 1D Path in each direction

X_path <- cumsum(x_samples) Y_path <- cumsum(y_samples)

  1. Plot time series, and histogram
  1. enable 2 plots to be displayed within the same graphics window

par(mfrow = c(1,2))

  1. Scatter plot for X/Y distribution

plot(x_samples, y_samples, type = 'p', lwd = 1, col = 'green', main = 'X/Y distribution', xlab = "X Samples", ylab = "Y Samples", xlim = c(-5, 5), ylim = c(-5,5))

  1. Time Serie for the particle path

plot(X_path, Y_path, type='l', lwd = 2, col='red', main = "2D Random Walk", xlab = "X coordinate", ylab = "Y coordinate", xlim = c(-50, 50), ylim = c(-50,50) ) </syntax>

Question

Part 2: Enzymatic reaction analysis

<syntax type='R'>

  1. R Example, inspired by
  1. Enzymatic analysis

concentration <- c(0.3330, 0.1670, 0.0833, 0.0416, 0.0208, 0.0104, 0.0052) velocity <- c(3.636, 3.636, 3.236, 2.660, 2.114, 1.466, 0.866)


  1. create data frame by combining data

df <- data.frame(concentration, velocity)

  1. print 'df'

df

  1. Correct one value from 'velocity'

velocity[4] <- 2.666

  1. Check that data frame 'df' has also been updated

df$velocity[4] # or df[4,2]

  1. Visualise data

plot(df$concentration, df$velocity)


  1. Linear regression
  1. create new variable to linearise plot

df$ytrans <- df$conc/df$vel

  1. plot new relationship

plot(df$conc, df$ytrans)

  1. compute linear regression

lmfit <- lm(ytrans~conc, data=df)

names(lmfit)

summary(lmfit)

plot(lmfit)

class(lmfit)

coef(lmfit)

plot(df$conc, df$ytrans) abline(coef(lmfit))

Vm <- 1/coef(lmfit)[2] K <- Vm*coef(lmfit)[1]

</syntax>

  • Basic intro to 'R' language
    • types: vector, string, factor, data_frame
    • functions for basic stats
    • Exercises:
      • ...
  • Import / Export CSV files (other file formats ?)
    • Exercises:
      • ...
  • Fitting (least square method)
    • Exercises:
      • ...
  • Plotting (scatter plots, histograms, time series)
    • Exercises:
      • ...
  • ODE simulations (based on basic gene expression models)
    • Exercises:
      • ...