An introduction to Python and its use in Bioinformatics Csc 487/687 Computing for Bioinformatics Fall 2005.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
File I/O Dr. Nancy Warter-Perez. Introduction to Python – Part II2 Homework Submission Guidelines Submit your programs via
An Introduction to Python – Part III Dr. Nancy Warter-Perez.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez June 23, 2005.
An Introduction to Python – Part II Dr. Nancy Warter-Perez June 15, 2005.
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
An Introduction to Python – Part III Dr. Nancy Warter-Perez May 1, 2007.
Chapter 2 Writing Simple Programs
An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez April 19, 2005.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
Genome Sciences 373 Genome Informatics Quiz Section 2 April 7, 2015.
An Introduction to Python – Part III Dr. Nancy Warter-Perez.
An Introduction to Python Dr. Nancy Warter-Perez April 15, 2004.
An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez.
Python Control of Flow.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
For. for loop The for loop in Python is more like a foreach iterative-type loop in a shell scripting language than a traditional for conditional loop.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Strings CS303E: Elements of Computers and Programming.
If statements while loop for loop
A Review of C++ Dr. Nancy Warter-Perez June 16, 2003.
Built-in Data Structures in Python An Introduction.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Course A201: Introduction to Programming 09/30/2010.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
DAY 3. ADVANCED PYTHON PRACTICE SANGREA SHIM TAEYOUNG LEE.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Chapter 2 Writing Simple Programs
CSc 120 Introduction to Computer Programing II Adapted from slides by
Tuples and Lists.
An Introduction to Python and Its Use in Bioinformatics
CSc 120 Introduction to Computer Programing II
Basic operators - strings
Lecture 9 Shell Programming – Command substitution
MATLAB: Structures and File I/O
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Introduction to Python
Introduction to Computer Science
Presentation transcript:

An introduction to Python and its use in Bioinformatics Csc 487/687 Computing for Bioinformatics Fall 2005

if Statement if expression: action Example: a1 = 'A‘; a2 = 'C'; match = 0; if (a1 == a2) : match+=1;

if-elif-else Statement if expression: action 1 elif expression: action 2 else : action 3 Example: a1 = 'A‘; a2 = 'C'; match = 0; gap = 0; if (a1 == a2) : match+=1; elif (a1 > a2): else: gap+=1;

String operations mystring = “Hello World!” ExpressionValuePurpose len(mystring)12 number of characters in mystring “hello”+“world”“helloworld” Concatenate strings “%s world”%“hello”“hello world” Format strings (like sprintf) “world” == “hello” “world” == “world” 0 or False 1 or True Test for equality “a” < “b” “b” < “a” 1 or True 0 or False Alphabetical ordering

Lists mylist=[“a”,”b”,3.58,”d”,4,0] mylist[0] mylist[2] a 3.58 Indexing mylist[-1] mylist[-2] 0404 Negative indexing (counts from end) mylist[1:4][“b”,3.58,”d”]Slicing (like strings) “b” in mylist “e” not in mylist 1 or True mylist.append(8)[“a”,”b”,3.58,”d”,4,0,8]Add to end of list

Dictionaries mydict={“r”:1,”g”:2,”y”:3.5,8.5:8,9:”nine”} mydict.keys()['y', 8.5, 'r', 'g', 9]List of the keys mydict.values()[3.5, 8, 1, 2, 'nine']List of the values mydict[“y”]3.5Value lookup mydict.has_key(“r”)True or 1Check for keys mydict.update({“a”:75}){8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y': 3.5, 9: 'nine'} Add pairs to dictionary

for Statement for var in list: action Sets var to each item in list and performs action range() function generates lists of numbers: range (5) -> [0,1,2,3,4] Example mylist=[“hello”,”hi”,”hey”,”!”]; for i in mylist: print i Iteration 1 prints: hello Iteration 2 prints: hi Iteration 3 prints: hey Iteration 4 prints: !

while Statement while expression: action Example x = 0; while x != 3: x = x + 1 Iteration 1: x=0+1=1 Iteration 2: x=1+1=2 Iteration 3: x=2+1=3 Iteration 4: don’t exec / 2 Infinite loop!

Example: Amino Acid Search Write a program to count the number of occurrences of an amino acid in a sequence. – The program should prompt the user for A sequence of amino acids (seq) The search amino acid (aa) – The program should display the number of times the search amino acid (aa) occurred in the sequence (seq)

Example: Amino Acid Search (2) #this program will calculate the number of occurrences of an amino acid in a sequence done=0 while (not done): sequence=raw_input("Please enter a sequence:"); aa=raw_input("Please enter the amino acid to look for:");

Example: Amino Acid Search (3) #compute the number of occurrences using for loop cnt=0 for i in sequence: if i == aa: cnt+=1 if cnt == 1: print "%s occurs in that sequence once" % aa; else: print "%s occurs in that sequence %d times" % (aa, cnt); answer=raw_input("try again? [yn]") if answer == "n" or answer == "N": done = 1

Programming Workshop #2 Write a sliding window program to compute the %GC in a sequence of nucleotides. – The program should prompt the user for The DNA sequence The window size (assume the window increment is 1) – Inputs: sequence, window size – Outputs: nucleotide number, %GC for each window

Python List Comprehensions Precise way to create a list Consists of an expression followed by a for clause, then zero or more for or if clauses Ex: >>> [str(round(355/113.0, i)) for i in range(1,6)] ['3.1', '3.14', '3.142', '3.1416', ' '] Ex: >>> x = "acactgacct" >>> y = [int(i=='c' or i=='g') for i in x] >>> y

Creating 2-D Lists To create a 2-D list L, with C columns and R rows initialized to 0: L = [[]] #empty 2-Dlist L = [[0 for col in range(C)] for row in range(R)] To assign the value 5 to the element at the 2 nd row and 3 rd column of L L[2][3] = 5

Zip – for parallel traversals Visit multiple sequences in parallel Ex: >>> L1 = [1,2,3] >>> L2 = [5,6,7] >>> zip(L1, L2) [(1,5), (2,6), (3,7)] Ex: >>> for(x,y) in zip(L1, L2): …print x, y, '--', x+y

More on Zip Zip more than two arguments and any type of sequence Ex: >>> T1, T2, T3 = (1,2,3),(4,5,6),(7,8) >>> T3 (7,8) >>> zip(T1, T2, T3) ?

Dictionary Construction with zip Ex: >>> keys = ['a', 'b', 'd'] >>> vals = [1.8, 2.5, -3.5] >>> hydro = dict(zip(keys,vals)) >>> hydro {'a': 1.8, 'b': 2.5, 'd': -3.5}

File I/O To open a file – myfile = open('pathname', ) modes: 'r' = read 'w' = write – Ex: infile = open("D:\\Docs\\test.txt", 'r') – Ex: outfile = open("out.txt", 'w') – in same directory

Common input file operations OperationInterpretation input = open ('file', 'r')open input file S = input.read()read entire file into string S S = input.read(N)Read N bytes (N>= 1) S = input.readline()Read next line L = input.readlines()Read entire file into list of line strings

Common output file operations OperationInterpretation output = open('file', 'w')create output file output.write(S)Write string S into file output.writelines(L)Write all line strings in list L into file output.close()Manual close (good habit)

Extracting data from string – split – String.split([sep, [maxsplit]]) - Return a list of the words of the string s. – If the optional argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). – If the argument sep is present and not None, it specifies a string to be used as the word separator. – The optional argument maxsplit defaults to 0. If it is nonzero, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements).

Split Ex: >>> x = "a,b,c,d" >>> x.split(',') >>> x.split(',',2) Ex: >>> y = "533a4" >>> y.split()

Functions Function definition – def adder(a, b, c): return a+b+c Function calls – adder(1, 2, 3) -> 6

Functions – Polymorphism >>>def fn2(c): …a = c * 3 …return a >>> print fn2(5) 15 >>> print fn2(1.5) 4.5 >>> print fn2([1,2,3]) [1,2,3,1,2,3,1,2,3] >>> print fn2("Hi") HiHiHi

Functions - Recursion def fn_Rec(x): if x == []: return fn_Rec(x[1:]) print x[0], y = [1,2,3,4] fn_Rec(y) >>> ?

Programming Workshop #3 Write a program to prompt the user for a scoring matrix file name and read the data into a dictionary ftp://ftp.ncbi.nih.gov/blast/matrices/