Download presentation
Presentation is loading. Please wait.
Published byClaribel McCarthy Modified over 8 years ago
1
An Introduction to Python – Part II Dr. Nancy Warter-Perez
2
Introduction to Python – Part II2 Overview Solution to Programming Workshop #1 If tests Loops for while Example amino acid search program Programming Workshop #2
3
Introduction to Python – Part II3 Solution to Programming Workshop 1 Write a Python program to compute the hydrophobicity of an amino acid # Program to compute the hydrophobicity of an amino acid # (solution only includes first 3 amino acids) # Written by: Prof. Warter-Perez # Date created: April 15, 2004 # Last modified: hydro = {"A":1.8,"C":2.5,"D":-3.5} aa = raw_input ("Please enter amino acid: ") print "The hydrophobicity of %s is %f."% (aa, hydro[aa])
4
Introduction to Python – Part II4 Make solution case insensitive # Program to compute the hydrophobicity of an amino acid # Written by: Prof. Warter-Perez # Date created: April 15, 2004 # Last modified: April 20, 2004 - made script case insensitive for # amino acids hydro = {"A":1.8,"C":2.5,"D":-3.5} aa = raw_input ("Please enter amino acid: ") aa = aa.upper() print "The hydrophobicity of %s is %f."% (aa, hydro[aa])
5
Introduction to Python – Part II5 Python Basics – Relational and Logical Operators Relational operators ==equal !=not equal >greater than >=greater than or equal <less than <=less than or equal Logical operatorsandornot
6
Introduction to Python – Part II6 if Statement if expression: action Example: a1 = 'A' a2 = 'C' match = 0 if a1 == a2 : match = match+1
7
Introduction to Python – Part II7 if-elif-else Statement if expression: action 1 elif expression: action 2 else : action 3 Example: mismatch = 0 match = 0; gap = 0 if a1[i] == a2[j]: match+=1 elif a1[i] == '-' or a2[i]] == '-' : gap += 1 else: mismatch += 1
8
Introduction to Python – Part II8 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
9
Introduction to Python – Part II9 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
10
Introduction to Python – Part II10 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
11
Introduction to Python – Part II11 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: !
12
Introduction to Python – Part II12 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!
13
Introduction to Python – Part II13 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)
14
Introduction to Python – Part II14 Example: Amino Acid Search (2) #this program will calculate the number of occurrences of an amino acid in a #sequence #by Bryce Ready done=0 while (not done): sequence=raw_input("Please enter a sequence:"); aa=raw_input("Please enter the amino acid to look for:");
15
Introduction to Python – Part II15 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
16
Introduction to Python – Part II16 Creating a Python Program Enter your program in the editor Notice that the editor has a color coding Comments Key words Etc… Also notice that it automatically indents Don’t override!! – this is how python tells when block statements end! If doesn’t indent to proper location – indicates bug
17
Introduction to Python – Part II17 Running your Program To build your program Under File->Run… Select No Debugging in the drop-down window Fix any errors, then run again
18
Introduction to Python – Part II18 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
19
Introduction to Python – Part II19 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
20
Introduction to Python – Part II20 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)
21
Introduction to Python – Part II21 Homework #1P Chapter 13 – Problem 2 Write a sliding window program that computes the %GC of DNA sequence. The program should prompt the user for the DNA sequence and the window size. You can assume that the window increment is 1, that is, as the program slides the window it will move the window over one nucleotide. Test your program on the following DNA sequence: GAACTCATACGAATTCACGTCAGCCCATCGTGCCACGT
22
Introduction to Python – Part II22 Homework #1P Chapter 13 – Problem 5 Write a program to detect transmembrane regions in a protein. Your program should input the protein file and the results.txt file generated by the Kyte_Doolittle_v3.py program. Kyte and Doolittle found that to detect transmembrane regions in a protein a window size of 19 is needed. Transmembrane regions are identified by average hydropathy scores greater than 1.6. Your program should output the following information in tabular form: ****************** Transmembrane Regions ****************** Window Window SequenceAverage Hydropathy (> 1.6) Steps: 1. Write your program. 2. Run the Kyte_Doolittle_v3.py program (see exercise 3) on the bacteriorhodopsin protein with a window size of 19. 3. Test your program using the bacteriorhodopsin protein file with the result.txt file generated from step ii.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.