1 PLAYING WITH PYTHON: THE FIRST STEPS IN READING DNA

advertisement
5.10.2014
Code Breaking: Reading the Genetic Code with Raspberry Pi, Visit 4
PLAYING WITH PYTHON: THE FIRST STEPS IN READING DNA
Use the code window to type the programs below. Remember:
Enter the program
DNA
RNA
Protein
Save it
Run it
Output appears in the shell window
What are the 4 DNA bases?
What are the pairs?
A = Adenine
T = Thymine
C = Cytosine
G = Guanine
A=T
C=G
A DNA sequence you have used before:
GTATACAAGTATCTGCTCAATTAGTCGACT
Program 1. Reading a DNA base
“=” allows to assign a value to a variable.
“if” allows to test if something is true (“==”) and print the assigned value, if it is. Variable
Remember from last time?
>>> dogs_name = “Bruno” >>> print dogs_name Bruno “” indicates
the value is
a string
Enter and run this program:
my_dna_base="A" if my_dna_base=="A": print "Base is Adenine" Just for fun:
How about re-writing the program for the other DNA bases?
Thymine
Cytosine
Guanine
1 5.10.2014
Code Breaking: Reading the Genetic Code with Raspberry Pi, Visit 4
Program 2. Reading several DNA bases, one at a time
“elif” and “else” statements allow you to do one of the specified things. If something is
“true” do one thing, otherwise check if something else is “true” and do something else if it is.
Remember from last time?
weather = raw_input (“What is the weather? (sunny/rainy/snowy)”) if weather == “sunny”: print “Go to the beach” elif weather == “rainy”: print “Grab your umbrella” elif weather == “snowy”: print “Get your skis” else: print “Error” Enter, save and run this program: my_dna_base="A" if my_dna_base == "A": print "Base is Adenine" elif my_dna_base == "G": print "Base is Guanine" elif my_dna_base == "C": print "Base is Cytosine" elif my_dna_base == "T": print "Base is Thymine" else: print "Not a DNA base!" The value of my DNA base is fixed
Just for fun:
See what happens if you change A to:
G
C
T
or any other a letter of your choice,
save and re-run the program!
Program 3. Reading a DNA base of your choice
raw_input allows the user to add a value to your variable “my_dna_base”
Remember from last time?
>>> rabbits = raw_input (“How many rabbits are there?”) >>> print rabbits User answer Use the program from above, modify it, save it as a new file and run it: my_dna_base = raw_input("Enter a DNA base, A,G,C or T:") if my_dna_base == "A": print "Base is Adenine" elif my_dna_base == "G": print "Base is Guanine" elif my_dna_base == "C": print "Base is Cytosine" elif my_dna_base == "T": print "Base is Thymine" else: print "Not a DNA base!" Notice what is
different from
above?
The part in
the box is
new:
“raw_input”
allows the
user to enter
one DNA
bases
2 5.10.2014
Code Breaking: Reading the Genetic Code with Raspberry Pi, Visit 4
Program 4. Reading a string (a sequence) of DNA bases at once Enter the program below in three different steps. Save it and run it each time you
add complexity. This allows you to see what happens at each individual step.
Enter 1st
Save it
Run it
my_dna = raw_input("Enter a string of DNA bases, A,G,C or T: ") number_of_bases = len(my_dna) “len” counts the number of
print "Number of bases:” DNA bases you have typed
print number_of_bases in
“range” lists the number of
list_of_positions=range(number_of_bases) positions for your DNA
print list_of_positions bases. If you’ve listed 2
bases, it would print 0,1.
"for base (b) in list” tells
for b in list_of_positions: the computer to scan and
if my_dna[b] == "A": read the DNA base at each
print "Base is Adenine" one of the positions and
elif my_dna[b] == "G": give as an output the full
print "Base is Guanine" name of DNA base.
elif my_dna[b] == "C": This is the “for loop”.
print "Base is Cytosine" elif my_dna[b] == "T": print "Base is Thymine" else: print "Not a DNA base!" Add 2nd
Save it
Run it
Add 3rd
Save it
Run it
What this section of your program tells the computer to do:
Step 1: Count the
position of the base
A T G C
Step 3: Print the name
you have assigned to
that base
Cytosine
Step 2: See what the base
is at that position
Guanine
Thymine
0 1 2 3
Adenine
Step 4:
Go to the next
position and
do it again:
this is called a
“for loop”
Position of the DNA
base starting at 0
DNA base you typed in
Full name of DNA base
3 5.10.2014
Code Breaking: Reading the Genetic Code with Raspberry Pi, Visit 4
Program 5. Making a dictionary and writing a short program, which allows
the computer to do the same job as program 4
Enter, save and run this program. Do you get the same result as above? my_dna = raw_input("Enter a string of DNA bases, A,G,C or T: ") number_of_bases = len(my_dna) print "Number of bases:” print number_of_bases list_of_positions=range(number_of_bases) This is the
dictionary. Note
dna_dictionary = {"A":"Adenine", the use of
"G":"Guanine", parenthesis.
"C":"Cytosine", "T":"Thymine"} Prints the DNA base
for b in list_of_positions: you’ve typed in
print my_dna[base] print dna_dictionary[ my_dna[base] ] Prints the full name of
the DNA base you
have assigned in the
dictionary
4 
Download