200 likes | 430 Views
Python Tutorial II Monty Python, Game of Life and Sequence Alignment Feb 1, 2011. Daniel Fernandez and Alejandro Quiroz dfernan@gmail.com aquiroz@hsph.harvard.edu. 1 st ACT (1 hour) Random Module Monty Hall Game of Life Sequence Alignment INTERMISSION Chillout sessions (10 min)
E N D
Python TutorialII Monty Python, Game of Life and Sequence AlignmentFeb 1, 2011 Daniel Fernandez and Alejandro Quiroz dfernan@gmail.com aquiroz@hsph.harvard.edu
1st ACT (1 hour) Random Module Monty Hall Game of Life Sequence Alignment INTERMISSION Chillout sessions (10 min) 2nd ACT (1 hour 50 min) Homework help Q5, Q6, Q7 and Q8.
Example. Simulate Flip of a Coin. import random coin = [‘heads’, ‘tails’] num_heads = 0 num_tails = 0 for i in range(0,1000): flip = random.choice(coin) if flip == ‘heads’: num_heads += 1 else: num_tails += 1 print ‘number of heads: ‘, num_heads print ‘number of heads: ‘, num_tails
Monty Hall Problem Suppose you’re on a game show, and you’re given a choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say number 3, and the host, who knows what’s behind the doors, opens another door, say number 2, which has a goat. He says to you, ‘Do you want to pick door number 1?’ Is it to your advantage to switch your choice of doors?
Monty Hall Problem Solution: montyhall.py Usage: python montyhall.py • Run montyhall.py to see the results. • Read montyhall.py and try to understand what did the program do? Visual Simulation. Python source.
Exercise 1. Read a fasta file. • Write a python module for reading fasta files – add it to your utils.py module – if feeling lazy read q7 code. Solution: ex1_fasta.py Usage: from ex1_fasta import *
Exercise 2. Complimentary DNA sequence and palindromic sequence • Write a program that takes as an input a DNA sequence 5’ to 3’ and returns the same sequence 3’ to 5’ end (i.e., its reverse complement). • Also make the program to output if the sequence is a palindromic sequence or not. • HINT: http://en.wikipedia.org/wiki/Complementarity_(molecular_biology) Solution: ex2_complimentarydna.py Usage: python ex2_complimentarydna.py
Life is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: (i) if an empty cell has three neighbors, fill it next turn; (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and (iii) if an occupied cell has four or more neighbors, it dies of overcrowding. You can get really strange, unpredictable behavior out of very simple initial patterns, and many mathematicians have spent a lot of time thinking about how this works. GAME OF LIFE
Life is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: (i) if an empty cell has three neighbors, fill it next turn; (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and (iii) if an occupied cell has four or more neighbors, it dies of overcrowding. You can get really strange, unpredictable behavior out of very simple initial patterns, and many mathematicians have spent a lot of time thinking about how this works. Game of Life • Life is a "game" or cellular automaton developed by Conway. • Instructions: • Start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: • (i) if an empty cell has three neighbors, fill it next turn; • (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and • (iii) if an occupied cell has four or more neighbors, it dies of overcrowding.
Game of Life Solution: LifeGame.py (GridMutator.py) Usage: jythonLifeGame.py • Run the game of life (in the terminal) • First Install Jython Standard package into • Then add to your .bash_profile # For Jython export JYTHON_HOME=/Users/dfernan/bin/jython2.5.2/ export PATH=$JYTHON_HOME:$PATH export CLASSPATH=$JYTHON_HOME/jython.jar:$CLASSPATH • jythonLifeGame.py
HH Question 5. Melting Temp Usage: python q5.py q5_input.txt q5.output 20 55
HH Question 6. Longest Sequence • Any ideas for retrieving the longest exact matching sequence between two sequences? • How to read a fasta file? Write a function that takes a file name as an input and outputs a list containing each sequence in the fasta file. • If lazy, just look at homework q7. Solution: fasta.py, Q8_input.fasta Usage: Use it as a python module containing the fasta class and the read_fasta function
Life is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: (i) if an empty cell has three neighbors, fill it next turn; (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and (iii) if an occupied cell has four or more neighbors, it dies of overcrowding. You can get really strange, unpredictable behavior out of very simple initial patterns, and many mathematicians have spent a lot of time thinking about how this works. Sequence Alignment How many operations? _____
Sequence Alignment H O M O L O G O U S Paralogs Orthologous
Life is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: (i) if an empty cell has three neighbors, fill it next turn; (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and (iii) if an occupied cell has four or more neighbors, it dies of overcrowding. You can get really strange, unpredictable behavior out of very simple initial patterns, and many mathematicians have spent a lot of time thinking about how this works. Sequence Alignment
Life is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: (i) if an empty cell has three neighbors, fill it next turn; (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and (iii) if an occupied cell has four or more neighbors, it dies of overcrowding. You can get really strange, unpredictable behavior out of very simple initial patterns, and many mathematicians have spent a lot of time thinking about how this works. Sequence Alignment • Align the following sequences and explain it. Bellow are the sequences and the match/mismatch (sub)BLOSUM matrix (HH1 and HH7)
Life is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: (i) if an empty cell has three neighbors, fill it next turn; (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and (iii) if an occupied cell has four or more neighbors, it dies of overcrowding. You can get really strange, unpredictable behavior out of very simple initial patterns, and many mathematicians have spent a lot of time thinking about how this works. Sequence Alignment Dynamic Programming: “The art of dividing a problem into simpler (sub)problems and then apply the sub-solutions recursively in order to obtain the final solution” New best alignment = Best previous alignment + align (i,j) i j How many operations? _____ Memory cost? _______
Life is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules are: (i) if an empty cell has three neighbors, fill it next turn; (ii) if an occupied cell has zero or one neighbor, it dies of loneliness; and (iii) if an occupied cell has four or more neighbors, it dies of overcrowding. You can get really strange, unpredictable behavior out of very simple initial patterns, and many mathematicians have spent a lot of time thinking about how this works. Sequence Alignment • Strategy: • Align the two sequences . • Read template code and think how to fill it in.