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
{
"metadata": {
"name": "Lecture 13"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": "Python programming concepts review"
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Common data types in python: lists, strings, dictionaries, ints, floats, boolean values, tuples"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# string \ns = '40'\nr = '20'\n\nprint r + s",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "2040\n"
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": "# int\ns = 40\nr = 20\n\nprint r + s",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "60\n"
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": "# float\ns = 40.0\nr = 20.0\n\nprint r + s",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "60.0\n"
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": "# boolean\ns = True\nr = False\n\nprint s or r\n\nprint s and r\n\nprint 42 == 42\n\nprint 4 > 5\n\nc = True\nprint (42 >= 41) and ('A' == 'a' or c)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "True\nFalse\nTrue\nFalse\nTrue\n"
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": "# list\nl = ['A', 'C', 'G', 'T']\nprint l[0]\nl[0] = 'a'",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "A\n"
}
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": "l.append('U')\nprint l",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['a', 'C', 'G', 'T', 'U']\n"
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": "print l[10]",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-27-acf326dacfd3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0ml\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
]
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": "# dict\nd = {'A':'T','T':'A','G':'C','C':'G'}\nprint d['A']\nprint d['T']",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "T\nA\n"
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": "d['A'] = 'U'\nd['U'] = 'A'\ndel d['T']",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": "print d['N']",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'N'",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-30-341d183ad361>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'N'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m: 'N'"
]
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": "# tuple\nt = (1,2,3)\nprint t[0]",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "1\n"
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": "t[1] = 5",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-33-e9323da3946c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"prompt_number": 33
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Loops"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "``for`` loops are much more commonly used in python then ``while`` loops (although this isn\u2019t necessarily true in other languages). ``for`` loops allow you to iterate over several steps of code to perform the same operation multiple times, usually with a different value for one or a few variables. For example, what does this code do:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "for c in 'ACCGATTGACC':\n print c.lower(),",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "a c c g a t t g a c c\n"
}
],
"prompt_number": 34
},
{
"cell_type": "markdown",
"metadata": {},
"source": "``c`` is a variable name that gets set to each entry in the string in turn. All code in the indented block will be executed before c gets assigned to the next value.\n\nSometimes you need to do the same thing some specified number of times with no different variable settings (which differs from the previous example where we\u2019re doing the same thing multiple times with a different value of ``c`` each time). To do this in python, you would typically use the range function, which when called as:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "n = 20\nrange(n)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 35,
"text": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
}
],
"prompt_number": 35
},
{
"cell_type": "markdown",
"metadata": {},
"source": "returns a list of numbers from ``0`` to ``n-1`` (i.e., ``[0, 1, 2, ..., n-1]``). For example, to randomly reorder the positions in a given DNA sequence 25 times and print the resulting sequences, you could do the following:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "sequence = list('ACCGAGGACCATACATTA')\nfor i in range(25):\n shuffle(sequence)\n print ''.join(sequence)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "CTTGAAGCACACGACAAT\nGATTAGAACCCATAGCCA\nATAGAGACATCCCGCAAT\nCGACGCATTAGAAAACTC\nACCAACATTAGGTAACCG\nCATCCCAGGATATAACAG\nACGCCACCGAGATTATAA\nCGTCCTGCAAAAAGCTAA\nCACAATATACGATGCGAC\nCAAAAGAGCACTTCGTAC\nAACATGACCAGCTTACAG\nAGATCATACAAGACGTCC\nTCCGAAAATAAGACGCTC\nCCATGGCATTCAAAACGA\nGGTTTACAAGCCCAACAA\nACGTACATACACAAGTGC\nCAAAGAATGTAGTCCACC\nACAACGTCGAATCGCAAT\nCAATAAATCCTCGGCGAA\nACCAGACGTATATGAACC\nACGGAATTCACAGACATC\nCGGACTCCATCAATAAGA\nTAACAAGCGTGAACCCAT\nCTCAAGGACTGCAACATA\nCACGACAGTCAACAAGTT\n"
}
],
"prompt_number": 36
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Note that we don\u2019t actually use the value assigned to the variable ``i`` ever: we just use that in combination with range to iterate 25 times.\n"
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Conditional statements"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Conditional statements allow you to apply some steps under certain conditions, such as when two values are equal, and apply some other steps in cases where those certain conditions are not met. The ``if``/``elif``/``else`` statements are used for conditionally executing some code in python. For example, describe line-by-line what this code does:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "sequence = 'ACCTAGGCAT'\nresult = []\nfor base in sequence:\n if base == 'T':\n result.append('U')\n elif base == 'A' or base == 'C' or base == 'G':\n result.append(base)\n else:\n print \"Unknown base (%s). Cannot complete.\" % base\n exit()\nprint ''.join(result)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "ACCUAGGCAU\n"
}
],
"prompt_number": 37
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": "Functions in python"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Functions are used to define blocks of re-usable code that you may want to work with multiple times."
},
{
"cell_type": "code",
"collapsed": false,
"input": "def complement_sequence(seq,complements={'A':'T','C':'G','T':'A','G':'C'}):\n result = []\n for base in seq:\n result.append(complements[base])\n return ''.join(result)\n\n# Define a sequence\nmy_seq = 'ACCGATTAGCCA'\n\n# Print its complement\nprint complement_sequence(my_seq)\n\n# Save its complement to a new variable\nmy_seq_comp = complement_sequence(my_seq)\nprint my_seq_comp\n\n# Change a default value in the function\nmy_seq_comp_non_default = complement_sequence(my_seq,{'A':'t','C':'g','T':'a','G':'c'})\nprint my_seq_comp_non_default",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "TGGCTAATCGGT\nTGGCTAATCGGT\ntggctaatcggt\n"
}
],
"prompt_number": 38
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"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.
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