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

From OpenWetWare
Jump to navigationJump to search
Line 80: Line 80:


===Questions===
===Questions===
* Modify this script so that you can overlay many independent paths as you want (Tip = use the function capability from 'R').
* Modify this script so that you can overlay as many independent paths as you want (Tip = use the function capability from 'R').
* Build a function to only return the last X coordinate after nbSteps.
* Write a function to only return the last X coordinate after nbSteps.
* Use the previous function to build the distribution of X coordinate from 1000 independent simulations, after nbSteps.
* Use the previous function to build the distribution of X coordinates from 1000 independent simulations, after nbSteps.
* Extend the first script to be able to simualte 2D random walks.
* Extend the first script to be able to simulate 2D random walks.


==Part 2: Enzymatic reaction analysis==
==Part 2: Enzymatic reaction analysis==

Revision as of 03:36, 9 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 walks are important to model as they relate for example to the phenomenon of diffusion.

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

<syntax type='R'>

  1. Random Walk 1D / 1 Trace
  1. number of steps

nbSteps <- 1000

  1. sampling and path construction
  1. First method: Looping
  1. draw from uniform distribtion between [0,1]

x <- runif(n = nbSteps)

  1. initialise path variable

xSteps <- rep(0, nbSteps) #as opposed to x1Steps <-0

  1. build independent steps series

for(i in 1:nbSteps){ if(x[i]>0.5){ xSteps[i] <- 1} else{ xSteps[i] <- -1 } }

  1. build path from independent steps

xPath <- cumsum(xSteps)

  1. Second method: vectorisation (faster)
  2. x <- runif(n = nbSteps)
  3. xSteps <- ifelse(x>0.5, 1, -1)
  4. xPath <- cumsum(xSteps)
  1. plot path

plot(xPath, 1:nbSteps, type='l', col='red', lwd =2,

     main = 'Random Walk 1D',
     xlab = "X coordinate",
     ylab = "Steps")

</syntax>

Questions

  • Modify this script so that you can overlay as many independent paths as you want (Tip = use the function capability from 'R').
  • Write a function to only return the last X coordinate after nbSteps.
  • Use the previous function to build the distribution of X coordinates from 1000 independent simulations, after nbSteps.
  • Extend the first script to be able to simulate 2D random walks.

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:
      • ...