9/16/2015BCHB524 - 2015 - Edwards Introduction to Python BCHB524 2015 Lecture 5.

Slides:



Advertisements
Similar presentations
Recitation 1 Programming for Engineers in Python.
Advertisements

Group practice in problem design and problem solving
Python programs How can I run a program? Input and output.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
General Programming Introduction to Computing Science and Programming I.
Computer Science 101 Introduction to Programming.
Variables and Expressions CMSC 201 Chang (rev )
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
10/20/2014BCHB Edwards Advanced Python Concepts: Modules BCHB Lecture 14.
8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
9/28/2015BCHB Edwards Basic Python Review BCHB Lecture 8.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
11/4/2015BCHB Edwards Advanced Python Concepts: Object Oriented Programming BCHB Lecture 17.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
1 Project 4: Palindrome Detector. 2 Assignment Write a C++ program that reads a line of text from the keyboard and reports to the user whether the text.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Introduction to Python
Introduction to Python
How to python source: Web_Dev fan on pinterest.
Advanced Python Idioms
Exam #1 You will have exactly 30 Mins to complete the exam.
Introduction to Python
Introduction to Python
Introduction to Programming
Chapter 7 Text Input/Output Objectives
Advanced Python Concepts: Modules
Introduction to Python
Advanced Python Data Structures
Introduction to Python
Introduction to Python
Advanced Python Data Structures
Introduction to Python
Introduction to Python
CS1371 Introduction to Computing for Engineers
Topic: Functions – Part 2
Think What will be the output?
Psuedo Code.
Advanced Python Concepts: Object Oriented Programming
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Python Data Structures: Lists
Introduction to Programming
Introduction to Python
Python – a HowTo Peter Wad Sackett and Henrike Zschach.
Advanced Python Concepts: Exceptions
Introduction to Python
Advanced Python Data Structures
Advanced Python Concepts: Modules
Introduction to Python
Advanced Python Idioms
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Introduction to Python
Python Data Structures: Lists
Advanced Python Concepts: Exceptions
Introduction to Python
Introduction to Python
Python Data Structures: Lists
Advanced Python Idioms
Introduction to Computer Science
Advanced Python Concepts: Modules
Advanced Python Concepts: Object Oriented Programming
Introduction to Programming
Using Modules.
Presentation transcript:

9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5

9/16/2015BCHB Edwards Outline Review, Homework #2 DNA as a string Extracting codons in DNA Counting in-frame codons in DNA Reverse Complement Program Input/Output raw_input, command-line arguments standard-input, standard-output, redirection 2

9/16/2015BCHB 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 Control Flow: if statements – conditional execution for statements – iterative execution 3

9/16/2015BCHB Edwards DNA as a string seq = "gcatgacgttattacgactctgtgtggcgtctgctgggg" seqlen = len(seq) # set i to 0, 3, 6, 9,..., 36 for i in range(0,seqlen,3): # extract the codon as a string codon = seq[i:i+3] print codon print "Number of Met. amino-acids", seq.count("atg") 4

9/16/2015BCHB Edwards DNA as a string What about upper and lower case? ATG vs atg? Differences between DNA and RNA sequence? Substitute U for each T? How about ambiguous nucleotide symbols? What should we do with ‘N’ and other ambiguity codes (R, Y, W, S, M, K, H, B, V, D)? Strings don’t know any biology! 5

9/16/2015BCHB Edwards DNA as a string seq = "gcatgacgttattacgactctgtgtggcgtctgctgggg" def inFrameMet(seq): seqlen = len(seq) count = 0 for i in range(0,seqlen,3): codon = seq[i:i+3] if codon.upper() == "ATG": count = count + 1 return count print "Number of Met. amino-acids", inFrameMet(seq) 6

9/16/2015BCHB Edwards DNA as a string input_seq = "catgacgttattacgactctgtgtggcgtctgctgggg" def complement(nuc): nucleotides = 'ACGT' complements = 'TGCA' i = nucleotides.find(nuc) comp = complements[i] return comp def reverseComplement(seq): newseq = "" for nuc in seq: newseq = complement(nuc) + newseq return newseq print "Reverse complement:", reverseComplement(input_seq) 7

9/16/2015BCHB Edwards DNA as a string input_seq = "catgacgttattacgactctgtgtggcgtctgctgggg" def complement(nuc): nucleotides = 'ACGT' complements = 'TGCA' i = nucleotides.find(nuc) if i >= 0: comp = complements[i] else: comp = nuc return comp def reverseComplement(seq): seq = seq.upper() newseq = "" for nuc in seq: newseq = complement(nuc) + newseq return newseq print "Reverse complement:", reverseComplement(input_seq) 8

9/16/2015BCHB Edwards Creating reusable programs Need to get input data and options from the user …often us, but sometimes others, or us later. Sometimes, want completely new inputs …but often, want the same or similar input. Sometimes, typing the input is OK …but often, want to use data in a file. Sometimes, output to the screen is OK …but often, want the result to go into a file. 9

Interactive input 9/16/2015BCHB Edwards input_seq = raw_input("Type your codon: ") def complement(nuc): nucleotides = 'ACGT' complements = 'TGCA' i = nucleotides.find(nuc) if i >= 0: comp = complements[i] else: comp = nuc return comp def reverseComplement(seq): seq = seq.upper() newseq = "" for nuc in seq: newseq = complement(nuc) + newseq return newseq print "Reverse complement:", reverseComplement(input_seq) 10

Command-line input 9/16/2015BCHB Edwards import sys input_seq = sys.argv[1] def complement(nuc): nucleotides = 'ACGT' complements = 'TGCA' i = nucleotides.find(nuc) if i >= 0: comp = complements[i] else: comp = nuc return comp def reverseComplement(seq): seq = seq.upper() newseq = "" for nuc in seq: newseq = complement(nuc) + newseq return newseq print "Reverse complement:", reverseComplement(input_seq) 11

Interactive and file input 9/16/2015BCHB Edwards import sys input_seq = sys.stdin.read() def complement(nuc): nucleotides = 'ACGT' complements = 'TGCA' i = nucleotides.find(nuc) if i >= 0: comp = complements[i] else: comp = nuc return comp def reverseComplement(seq): seq = seq.upper() newseq = "" for nuc in seq: newseq = complement(nuc) + newseq return newseq print "Reverse complement:", reverseComplement(input_seq) 12

File input only 9/16/2015BCHB Edwards import sys seq_file = sys.argv[1] # MAGIC: open file, read contents, and remove whitespace input_seq = ''.join(open(seq_file).read().split()) def complement(nuc): nucleotides = 'ACGT' complements = 'TGCA' i = nucleotides.find(nuc) if i >= 0: comp = complements[i] else: comp = nuc return comp def reverseComplement(seq): seq = seq.upper() newseq = "" for nuc in seq: newseq = complement(nuc) + newseq return newseq print "Reverse complement:", reverseComplement(input_seq) 13

Input Summary raw_input provides interactive values from the user (also copy-and-paste) sys.stdin.read() provides interactive or file-based values from the user (also copy-and-paste) sys.argv[1] provides command-line values from the user (also copy-and-paste) value can be a filename that provides user-input Terminal standard-input redirection "<" can be used to send a file's contents to raw_input or sys.stdin.read() 9/16/2015BCHB Edwards14

Output is easy… Just use print, right? Print statements go to the terminal's standard-output. We can redirect to a file using ">" Errors still get printed to the terminal. We can also link programs together – standard-output to standard-input using "|" Also, cat just writes its file to standard out 9/16/2015BCHB Edwards15

Connect reverse complement w/ codon counting… Create and test rc.py from earlier slides: Sequence from standard-input Reverse complement sequence to standard-output Create and test codons.py from earlier slides: Sequence from standard-input Count to standard-output Place example sequence in file: test.seq Execute: cat test.seq | python rc.py | python codons.py 9/16/2015BCHB Edwards16

In general Windows and OS X have similar facilities cmd in windows, terminal in OS X Powerful mechanism for making reusable programs No knowledge of python required for use! Most bioinformatics software is used from the command-line w/ command-line arguments: Files provide sequence data, etc. I'll promote this style of program I/O. 9/16/2015BCHB Edwards17

9/16/2015BCHB Edwards Exercise 1 Use NCBI Probe (“google NCBI Probe”) to look up PCR markers for your favorite gene Write a command-line program to compute the reverse complement sequence for the forward and reverse primer. 18

9/16/2015BCHB Edwards Exercise 2 Write a command-line program to test whether a PCR primer is a reverse complement palindrome. Such a primer might fold and self-hybridize! Test your program on the following primers: TTGAGTAGACGCGTCTACTCAA TTGAGTAGACGTCGTCTACTCAA ATATATATATATATAT ATCTATATATATGTAT 19

9/16/2015BCHB Edwards Homework 3 Due Monday, September 21 st. Submit using Blackboard Use only the techniques introduced so far. Make sure you can run the programs demonstrated in lecture(s). Exercises 4.1, 4.2 from Lecture 4 Exercises 5.1, 5.2 from Lecture 5 20