Introduction to Python BCHB 524 Lecture 4 BCHB
Introduction to Python BCHB 524 Lecture 4 BCHB 524 - Edwards
Outline l Homework #1 Solutions l Review Control flow: if statement Control flow: for statement Exercises l l l BCHB 524 - Edwards 2
Review l l Printing and execution Variables and basic data-types: l l Functions, using/calling and defining: l l integers, floats, strings Arithmetic with, conversion between String characters and chunks, string methods Use in any expression Parameters as input, return for output Functions calling other functions (oh my!) If statements – conditional execution BCHB 524 - Edwards 3
Control Flow: if statement # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there if seq. startswith('atg'): print("Sequence without initial Met: ", seq[3: ]) else: print("Sequence (no initial Met): ", seq) l l Execution path depends on string in seq. Make sure you change seq to different values. BCHB 524 - Edwards 4
Control Flow: if statement # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there if seq. startswith('atg'): init. Met = True newseq = seq[3: ] else: init. Met = False newseq = seq # Output the results print("Original sequence: ", seq) print("Sequence starts with Met: ", init. Met) print("Sequence without initial Met: ", newseq) BCHB 524 - Edwards 5
Control Flow: if statement # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there init. Met = seq. startswith('atg') if init. Met: newseq = seq[3: ] else: newseq = seq # Output the results print("Original sequence: ", seq) print("Sequence starts with Met: ", init. Met) print("Sequence without initial Met: ", newseq) BCHB 524 - Edwards 6
Control Flow: if statement # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there init. Met = seq. startswith('atg') if init. Met: seq = seq[3: ] # Output the results print("Sequence starts with Met: ", init. Met) print("Sequence without initial Met: ", seq) BCHB 524 - Edwards 7
Serial if statement # Determine the complementary nucleotide def complement(nuc): if nuc == 'A': comp = 'T' if nuc == 'T': comp = 'A' if nuc == 'C': comp = 'G' if nuc == 'G': comp = 'C' return comp # Use the complement function print("The complement of A is", complement('A')) print("The complement of T is", complement('T')) print("The complement of C is", complement('C')) print("The complement of G is", complement('G')) BCHB 524 - Edwards 8
Compound if statement # Determine the complementary nucleotide def complement(nuc): if nuc == 'A': comp = 'T' elif nuc == 'T': comp = 'A' elif nuc == 'C': comp = 'G' elif nuc == 'G': comp = 'C' else: comp = nuc return comp # Use the complement function print("The complement of A is", complement('A')) print("The complement of T is", complement('T')) print("The complement of C is", complement('C')) print("The complement of G is", complement('G')) BCHB 524 - Edwards 9
If statement conditions l l Any expression (variable, arithmetic, function call, etc. ) that evaluates to True or False Any expression tested against another expression using: l l l == (equality), != (inequality) < (less than), <= (less than or equal) > (greater than), >= (greater than or equal) in (an element of) Conditions can be combined using: l and, or, not, and parentheses BCHB 524 - Edwards 10
For (each) statements l Sequential/Iterative execution # Print the numbers 0 through 4 for i in range(0, 5): print(i) # Print the nucleotides in seq = 'ATGGCAT' for nuc in seq: print(nuc) l Note use of indentation to define a block! BCHB 524 - Edwards 11
For (each) statements # Input to program seq = 'AGTAGTTCGCGTAGCTAGCTATGCG' # Examine each symbol in seq and count the A's count = 0 for nuc in seq: if nuc == 'A': count = count + 1 # Output the result print("Sequence", seq, "contains", count, "A symbols") BCHB 524 - Edwards 12
For (each) statements # Examine each symbol in seq and count the A's def count. As(seq): count = 0 for nuc in seq: if nuc == 'A': count = count + 1 return count # Input to program inseq = 'AGTAGTTCGCGTAGCTAGCTATGCG' # Compute count a. Count = count. As(inseq) # Output the result print("Sequence", inseq, "contains", a. Count, "A symbols") BCHB 524 - Edwards 13
For (each) statements # Examine each symbol in seq and count those that match sym def count. Sym(seq, sym): count = 0 for nuc in seq: if nuc == sym: count = count + 1 return count # Input to program inseq = 'AGTAGTTCGCGTAGCTAGCTATGCG' # Compute count a. Count = count. Sym(inseq, 'A') # Output the result print("Sequence", inseq, "contains", a. Count, "A symbols") BCHB 524 - Edwards 14
Exercise 1 l Write a Python program to compute the reverse complement of a codon l l l Modularize! Place the reverse complement code in a new function. l l Use my solution to Homework #1 Exercise #1 as a starting point Add the “complement” function of this lecture (slide 9) as provided. Call the new function with a variety of codons Change the complement function to handle upper and lower-case nucleotide symbols. l Test your code with upper and lower-case codons. BCHB 524 - Edwards 15
Exercise 2 l Write a Python program to determine whether or not a DNA sequence consists of a (integer) number of (perfect) "tandem" repeats. l l l Test it on sequences: l AAAAAAAA l CACACACAC l ATTCGATTCG l GTAGTAGTA l TCAGTCACTCAG Hint: Is the sequence the same as many repetitions of its first character? Hint: Is the first half of the sequence the same as the second half of the sequence? BCHB 524 - Edwards 16
Homework 2 l Due Monday, September 16 th. l Use only the techniques introduced so far. l Make sure you can run the programs demonstrated in lecture. l Submit Exercise 3. 1, 4. 2 solutions to Canvas. BCHB 524 - Edwards 17
- Slides: 17