+ All Categories
Home > Documents > 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Date post: 14-Jan-2016
Category:
Upload: blanche-robinson
View: 215 times
Download: 0 times
Share this document with a friend
18
9/14/2015 BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4
Transcript
Page 1: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015 BCHB524 - 2015 - Edwards

Introduction to Python

BCHB5242015

Lecture 4

Page 2: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015 BCHB524 - 2015 - Edwards

Outline

Review Homework #1 Notes Control flow: if statement Control flow: for statement Exercises

2

Page 3: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015 BCHB524 - 2015 - Edwards

Review

Printing and execution Variables and basic data-types:

integers, floats, strings Arithmetic with, conversion between String characters and chunks, string methods

Functions, using/calling and defining: Use in any expression Parameters as input, return for output Functions calling other functions (oh my!)

If statements – conditional execution

3

Page 4: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Homework #1 Notes

Python programs: Upload .py files Don't paste into comment box Don't paste into your writeup

Writeup: Upload .txt files, Don't paste into comment box Text document preferred

9/14/2015 BCHB524 - 2015 - Edwards 4

Page 5: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Homework #1 Notes

Multiple submissions: OK, but… …I'll ignore all except the last one Make each (re-)submission complete

Grading: Random grading order Comments Grading "curve"

9/14/2015 BCHB524 - 2015 - Edwards 5

Page 6: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Control Flow: if statement

Execution path depends on string in seq. Make sure you change seq to different

values.9/14/2015 BCHB524 - 2015 - Edwards

# The input DNA sequenceseq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg'

# Remove the initial Met codon if it is thereif seq.startswith('atg'): print "Sequence without initial Met:",seq[3:]else: print "Sequence (no initial Met):",seq

6

Page 7: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Control Flow: if statement

9/14/2015 BCHB524 - 2015 - Edwards

# The input DNA sequenceseq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg'

# Remove the initial Met codon if it is thereif seq.startswith('atg'): initMet = True newseq = seq[3:]else: initMet = False newseq = seq

# Output the resultsprint "Original sequence:",seqprint "Sequence starts with Met:",initMetprint "Sequence without initial Met:",newseq

7

Page 8: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Control Flow: if statement

9/14/2015 BCHB524 - 2015 - Edwards

# The input DNA sequenceseq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg'

# Remove the initial Met codon if it is thereinitMet = seq.startswith('atg'):if initMet: newseq = seq[3:]else: newseq = seq

# Output the resultsprint "Original sequence:",seqprint "Sequence starts with Met:",initMetprint "Sequence without initial Met:",newseq

8

Page 9: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Control Flow: if statement

9/14/2015 BCHB524 - 2015 - Edwards

# The input DNA sequenceseq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg'

# Remove the initial Met codon if it is thereinitMet = seq.startswith('atg')if initMet: seq = seq[3:]

# Output the resultsprint "Sequence starts with Met:",initMetprint "Sequence without initial Met:",seq

9

Page 10: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Serial if statement

9/14/2015 BCHB524 - 2015 - Edwards

# Determine the complementary nucleotidedef 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 functionprint "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')

10

Page 11: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Compound if statement

9/14/2015 BCHB524 - 2015 - Edwards

# Determine the complementary nucleotidedef 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 functionprint "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')

11

Page 12: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

If statement conditions

Any expression (variable, arithmetic, function call, etc.) that evaluates to True or False

Any expression tested against another expression using: == (equality), != (inequality) < (less than), <= (less than or equal) > (greater than), >= (greater than or equal) in (an element of)

Conditions can be combined using: and, or, not, and parentheses

9/14/2015 BCHB524 - 2015 - Edwards 12

Page 13: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Sequential/Iterative execution

Note use of indentation to define a block!

9/14/2015 BCHB524 - 2015 - Edwards 13

For (each) statements

# Print the numbers 0 through 4for i in range(0,5): print i

# Print the nucleotides in seqseq = 'ATGGCAT'for nuc in seq: print nuc

Page 14: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015 BCHB524 - 2015 - Edwards 14

For (each) statements

# Input to programseq = 'AGTAGTTCGCGTAGCTAGCTAGCTATGCG'

# Examine each symbol in seq and count the A'scount = 0for nuc in seq: if nuc == 'A': count = count + 1

# Output the resultprint "Sequence",seq,"contains",count,"A symbols"

Page 15: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015 BCHB524 - 2015 - Edwards 15

For (each) statements

# Examine each symbol in seq and count the A'sdef countAs(seq): count = 0 for nuc in seq: if nuc == 'A': count = count + 1 return count

# Input to programinseq = 'AGTAGTTCGCGTAGCTAGCTAGCTATGCG'

# Compute countaCount = countAs(inseq)

# Output the resultprint "Sequence",inseq,"contains",aCount,"A symbols"

Page 16: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015 BCHB524 - 2015 - Edwards 16

For (each) statements

# Examine each symbol in seq and count those that match symdef countSym(seq,sym): count = 0 for nuc in seq: if nuc == sym: count = count + 1 return count

# Input to programinseq = 'AGTAGTTCGCGTAGCTAGCTAGCTATGCG'

# Compute countaCount = countSym(inseq,'A')

# Output the resultprint "Sequence",inseq,"contains",aCount,"A symbols"

Page 17: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

Exercise 1 Write a Python program to compute the

reverse complement of a codon Use my solution to Homework #1 Exercise #1 as a

starting point Add the “complement” function of this lecture

(slide 12) as provided. Modularize! Place the reverse complement

code in a new function. Call the new function with a variety of codons

Change the complement function to handle upper and lower-case nucleotide symbols. Test your code with upper and lower-case codons.

9/14/2015 BCHB524 - 2015 - Edwards 17

Page 18: 9/14/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 4.

9/14/2015 BCHB524 - 2015 - Edwards 18

Exercise 2 Write a Python program to determine whether or not

a DNA sequence consists of a (integer) number of (perfect) "tandem" repeats. Test it on sequences:

AAAAAAAAAAAAAAAA CACACACACACACAC ATTCGATTCGATTCG GTAGTAGTAGTAGTA TCAGTCACTCACTCAG

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?


Recommended