Skip to content

Instantly share code, notes, and snippets.

@gregcaporaso
Last active December 11, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregcaporaso/4657175 to your computer and use it in GitHub Desktop.
Save gregcaporaso/4657175 to your computer and use it in GitHub Desktop.
IPython Notebooks to support Greg Caporaso's Spring 2013 Bioinformatics I class at Northern Arizona University: http://caporaso.us/teaching/courses/bio299_spring_2013/index.html This work is licensed under the Creative Commons Attribution 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/

IPython Notebooks to support Greg Caporaso's Spring 2013 Bioinformatics I class at Northern Arizona University.

This work is licensed under the Creative Commons Attribution 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Feel free to use or modify these notebooks, but please credit me by placing the following attribution information where you feel that it makes sense: Greg Caporaso, www.caporaso.us.

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Lecture 4"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "In python 2, division of integers is not true division. This will be changed in python 3. For example:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "1/4",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Note that this isn't rounding, but truncation of the remainder."
},
{
"cell_type": "code",
"collapsed": false,
"input": "3/4",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "There are a few ways around this, but most convenient is to add the the following ``import`` statement. It's always a good idea to add this to your python code to avoid any surprises. "
},
{
"cell_type": "code",
"collapsed": false,
"input": "from __future__ import division",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "3/4",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "**Genetic bottleneck programming example**\n\nThis example illustrates the concept of genetic drift in a population of genotypes. Imagine we start with four genotypes, A, B, C, and D and 10,000 individuals. First, let\u2019s define a starting population with the following genotype frequencies:\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "genotype_frequencies = ['A'] * 5000 + ['B'] * 2500 + ['C'] * 1250 + ['D'] * 1250",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "This syntax may be new to you. Break this apart to individual addends to figure out what the full statement is doing.\n\nNext, let\u2019s define a function that will conveniently allow us to summarize this population:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "def summarize_composition(population):\n population_size = len(population)\n print 'A: %0.4f' % (population.count('A') / population_size)\n print 'B: %0.4f' % (population.count('B') / population_size)\n print 'C: %0.4f' % (population.count('C') / population_size)\n print 'D: %0.4f' % (population.count('D') / population_size)",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "You can then call this as follows:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "summarize_composition(genotype_frequencies)",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Next we\u2019re going to import the ``sample`` function from the ``random`` module. Given a list (``population``) and a number of elements (``k``) to select, sample randomly samples (without replacement) ``k`` elements from the list and returns those as a new list. So, if we sample the full population and summarize the genotype composition, we should get the same result \u2013 let\u2019s test it out:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "from random import sample\nnew_genotype_frequencies = sample(genotype_frequencies,10000)\nsummarize_composition(new_genotype_frequencies)",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Now, let\u2019s simulate genetic drift. Imagine we have a population of organisms with the genotype frequencies represented in our ``genotype_frequencies`` list. Regardless of which of these genotypes confers the most selective advantage a random removal of a large component from the population has the ability to affect the resulting genotypic composition.\n\nTo simulate an event that randomly kills off 10% of the total population, and look at the resulting genotype composition:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "new_genotype_frequencies = sample(genotype_frequencies,9000)\nsummarize_composition(new_genotype_frequencies)",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Do this a few times. You should notice that the frequencies don\u2019t change a lot. What happens if instead of this relatively small dying off, there is a near-extinction event. Simulate an event that randomly kills off 99.9% of the population. What happens now? Run this simulation several times and explain the results of this experiment."
}
],
"metadata": {}
}
]
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment