{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [PyBroMo](http://tritemio.github.io/PyBroMo/) - 2. Generate smFRET data, including mixtures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "This notebook is part of PyBroMo a \n", "python-based single-molecule Brownian motion diffusion simulator \n", "that simulates confocal smFRET\n", "experiments.\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## *Overview*\n", "\n", "*In this notebook we show how to generated smFRET data files from the diffusion trajectories*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading the software\n", "\n", "Import all the relevant libraries:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "from pathlib import Path\n", "import numpy as np\n", "import tables\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import pybromo as pbm\n", "print('Numpy version:', np.__version__)\n", "print('PyTables version:', tables.__version__)\n", "print('PyBroMo version:', pbm.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create smFRET data-files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a file for a single FRET efficiency\n", "\n", "In this section we show how to save a single smFRET data file. In the next section we will perform the same steps in a loop to generate a sequence of smFRET data files.\n", "\n", "Here we load a diffusion simulation opening a file to save\n", "timstamps in *write* mode. Use `'a'` (i.e. *append*) to keep \n", "previously simulated timestamps for the given diffusion." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "S = pbm.ParticlesSimulation.from_datafile('0168', mode='w')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "S.particles.diffusion_coeff_counts" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#S = pbm.ParticlesSimulation.from_datafile('0168')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulate timestamps of smFRET\n", "\n", "### Example1: single FRET population\n", "\n", "Define the simulation parameters with the following syntax:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "params = dict(\n", " em_rates = (200e3,), # Peak emission rates (cps) for each population (D+A)\n", " E_values = (0.75,), # FRET efficiency for each population\n", " num_particles = (20,), # Number of particles in each population\n", " bg_rate_d = 1500, # Poisson background rate (cps) Donor channel\n", " bg_rate_a = 800, # Poisson background rate (cps) Acceptor channel\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the object that will run the simulation and print a summary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mix_sim = pbm.TimestapSimulation(S, **params)\n", "mix_sim.summarize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the simualtion:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rs = np.random.RandomState(1234)\n", "mix_sim.run(rs=rs, overwrite=False, skip_existing=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save simulation to a smFRET [Photon-HDF5](http://photon-hdf5.org) file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mix_sim.save_photon_hdf5(identity=dict(author='John Doe', \n", " author_affiliation='Planet Mars'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 2: 2 FRET populations\n", "\n", "To simulate 2 population we just define the parameters with \n", "one value per population, except for the Poisson background \n", "rate that is a single value for each channel." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "params = dict(\n", " em_rates = (200e3, 180e3), # Peak emission rates (cps) for each population (D+A)\n", " E_values = (0.75, 0.35), # FRET efficiency for each population\n", " num_particles = (20, 15), # Number of particles in each population\n", " bg_rate_d = 1500, # Poisson background rate (cps) Donor channel\n", " bg_rate_a = 800, # Poisson background rate (cps) Acceptor channel\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mix_sim = pbm.TimestapSimulation(S, **params)\n", "mix_sim.summarize()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rs = np.random.RandomState(1234)\n", "mix_sim.run(rs=rs, overwrite=False, skip_existing=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mix_sim.save_photon_hdf5()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Burst analysis\n", "\n", "The generated Photon-HDF5 files can be analyzed by any smFRET burst\n", "analysis program. Here we show an example using the opensource\n", "[FRETBursts](https://github.com/tritemio/FRETBursts/) program:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import fretbursts as fb" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "filepath = list(Path('./').glob('smFRET_*'))\n", "filepath" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d = fb.loader.photon_hdf5(str(filepath[1]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d.A_em" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fb.dplot(d, fb.timetrace);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d.calc_bg(fun=fb.bg.exp_fit, tail_min_us='auto', F_bg=1.7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d.bg_dd, d.bg_ad" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d.burst_search(F=7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d.num_bursts" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ds = d.select_bursts(fb.select_bursts.size, th1=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ds.num_bursts" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fb.dplot(d, fb.timetrace, bursts=True);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fb.dplot(ds, fb.hist_fret, pdf=False)\n", "plt.axvline(0.75);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **NOTE:** Unless you simulated a diffusion of 30s or more the previous histogram will be very poor." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fb.bext.burst_data(ds)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }