1 / 21

An Introduction to Python Part III

Introduction to Python ? Part III. 2. Overview. AssignmentsSolution to Programming Workshop

don
Télécharger la présentation

An Introduction to Python Part III

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. An Introduction to Python Part III Dr. Nancy Warter-Perez

    2. Introduction to Python Part III 2 Overview Assignments Solution to Programming Workshop #2 2-D Lists List comprehensions Zip File I/O Split Functions Programming Workshop #3

    3. Introduction to Python Part III 3 Solution to Programming Workshop 2 # Script to calculate %GC of a sequence of nucleotides. # Inputs: sequence, window size # Outputs: nucleotide number, %GC for each window # Written by: Nancy Warter-Perez # Date created: April 22, 2004 # Last modified: print("Script for computing %GC.") seq = raw_input("Please enter sequence: ") winsize = input("Please enter window size: ")

    4. Introduction to Python Part III 4 Solution to Programming Workshop 2

    5. Introduction to Python Part III 5 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', '3.14159'] Ex: replace all occurrences of G or C in a string of amino acids with a 1 and A and T with a 0 >>> x = "acactgacct" >>> y = [int(i=='c' or i=='g') for i in x] >>> y [0, 1, 0, 1, 0, 1, 0, 1, 1, 0]

    6. Introduction to Python Part III 6 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 2nd row and 3rd column of L L[2][3] = 5

    7. Introduction to Python Part III 7 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 1 5 -- 6 2 6 -- 8 3 7 -- 10

    8. Introduction to Python Part III 8 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) [(1,4,7),(2,5,8)] -- truncates to shortest sequence

    9. Introduction to Python Part III 9 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}

    10. Introduction to Python Part III 10 File I/O To open a file myfile = open('pathname', <mode>) modes: 'r' = read 'w' = write Ex: infile = open("D:\\Docs\\test.txt", 'r') Ex: outfile = open("out.txt", 'w') in same directory

    11. Introduction to Python Part III 11 Common input file operations

    12. Introduction to Python Part III 12 Common output file operations

    13. Introduction to Python Part III 13 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).

    14. Introduction to Python Part III 14 Split Ex: >>> x = "a,b,c,d" >>> x.split(',')['a', 'b', 'c', 'd'] >>> x.split(',',2)['a', 'b', 'c,d'] Ex: >>> y = "5 33 a 4" >>> y.split()['5', '33', 'a', '4']

    15. Introduction to Python Part III 15 Functions Function definition def adder(a, b, c): return a+b+c Function calls adder(1, 2, 3) -> 6

    16. Introduction to Python Part III 16 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

    17. Introduction to Python Part III 17 Functions - Recursion def fn_Rec(x): if x == []: return fn_Rec(x[1:]) print x[0], y = [1,2,3,4] fn_Rec(y) >>> 4 3 2 1

    18. Introduction to Python Part III 18 Programming Workshop #3 Create a text file called "test1.txt" with the following data: # Sample data 1 2 3 4 5 # more data 6 7 8 9 10 Create another text filed called "test2.txt" with the following data: # More test data # With more header info A B C D E F G Write a script to do the following 1. Prompt the user for a filename 2. Open the file 3. Read the file into a list of strings. 4. If the line does not begin with a '#' print the line to the screen. Test your script on test1.txt and test2.txt.

    19. Introduction to Python Part III 19 Programming Homework #2P Write a program to prompt the user for a scoring matrix file name and read the data into a dictionary Download a representative set of PAM and Blossum Scoring Matrix Files Scoring matrices should be downloaded from ftp://ftp.ncbi.nih.gov/blast/matrices/ Due Date: Thursday, May 14th

    20. Introduction to Python Part III 20 Example Scoring Matrix File

    21. Introduction to Python Part III 21 Algorithm for Homework 2P Step 1: Create an empty list (of dictionaries) Step 2: Prompt the user for the scoring matrix file name Step 3: Open the file and read the contents as a list of strings. Ignore the comment lines Step 4: When you reach a line that doesnt start with '#' read in the amino acid symbols and split them into your keys for your dictionary Step 5: Read in the rest of the lines one at a time. For each line: Step 5a. Slice off the first character (amino acid). Step 5b. For the rest of the string split into individual numbers and convert to a list of integers (use a list comprehension). This is your data for your dictionary. Step 5c. Zip the keys and data together and convert into a dictionary. Step 5d. Add the dictionary to the list of dictionaries Step 6: After youve read all lines, create the dictionary of dictionaries by zipping the keys and the list of dictionaries and convert into a dictionary.

More Related