Upload
jordi-villa-freixa
View
2.524
Download
0
Embed Size (px)
DESCRIPTION
an intro to Python
Citation preview
MScBIOINFO
2011-2012
Introduction to PythonMSc Bioinformatics for Health Sciences
César L. Ávila1 and Jordi Villà i Freixa2
[email protected] Nacional de Tucumá[email protected] // cbbl.imim.es
Universitat Pompeu Fabrac©Michael A. Johnston 2007; Jordi Villà-Freixa 2007-2012
Barcelona, Winter 2012
1
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
1 Intro
2 Functional programming
3 Classes
4 Exceptions
5 BioPython
6 Graphics
7 CGI scripting
8 Packaging
9 Extensions
10 Glossary
11 Annexes
2
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Programming
Programs operate on various "data types": integers,strings, doublesConcept of variable and assignment:
Age = 3
Expresions create and process data:
x>3y=x∗2
Control of flow: conditioning testing (if, else) anditerations (for, while loops)Procedural programming: using functions to divide yourprogram into logical chunks
Basic programming concepts! Only syntax change.
3
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Python
Dynamical, interpreted, object oriented programminglanguageSoftware quality: designed to be readable, coherent andmaintainableDeveloper productivity: very compact code (20-33% ofthe size of the corresponding java/C code): less code→less to debug→ less to maintain→ less to learn
Some help: http://greenteapress.com/thinkpython/thinkpython.html
4
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
The subject
About making it quicker for you and others to write, maintainand extend programs. To do so:
Reduce the time spent in programming & debugging:OOP, testingMake it easy to extend your program: code reuse (OOP)Reduce the time for others to understand your program:documentation, program readability
5
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
The Python interpreter
Python 2.6.6 (r266:84292, Jan 3 2011, 14:28:29)[GCC 4.2.1 (Apple Inc. build 5664)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> print 1 + 12
Alternatively, you can store code in a file and use theinterpreter to execute the contents of the file. Such a file iscalled a script. For example, you could use a text editor tocreate a fle named dinsdale.py with the following contents:
pr in t 1 + 1
By convention, Python scripts have names that end with .py.
6
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
IDLE or other IDEs
IDLE is the Python Integrated Development Environment:http://docs.python.org/library/idle.html
First step in making it easier to write Python codeSyntax highlightingCode completionInline documentationMany other useful features
Eclipse is... "an open extensible IDE for anything andnothing in particular". Extension to Python throughPyDEViPython aims... "to create a comprehensive environmentfor interactive and exploratory computing"but check also other possibilities:http://wiki.python.org/moin/PythonEditors
7
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Structure of a program
Programs are composed of modulesModules contain statements:
Function definitionsControl statements (if, while, etc)Variable assignments
Statements contain expressions:
x<3a=x∗x+2
Expressions create and process objects
8
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Python keywords
and del from not whileas e l i f global or wi thasser t else i f pass y i e l dbreak except import pr in tclass exec in raisecontinue f i n a l l y is returndef for lambda t ry
9
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Numbers
Types: integer, floating point, long integers, bool (True,False)Basic expression operators & precedencehttp://www.ibiblio.org/g2swap/byteofpython/read/operator-precedence.html
Conversion: mixed types are converted up, e.g., Integers→ floating point
40+3.14
10
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Dynamic typing
Variable types are decided at runtimeVariables are created when you assign values to themVariables refer to an object, e.g., a numberThe object has a type; the variable does notWhen a variable appears in an expression, it isimmediately replaced by the object it refers to
Example
a=3
Crerate an object of type integer that represents thenumber 3Create variable a if it does not exist yetlink the variable a to the new object 3
11
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Dynamic typing
12
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Dynamic typing
12
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Garbage collection
When no variables are left that reference an object, it isdestroyed (automatic memory management)
13
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Modules
Every file containing python code whose name ends in.py is a moduleA module usually contains a number of items e.g.Variables and functions which you can access. Theseitems are called attributesYou load a module using the import statementJust like a number a module is an objectYou can reload a module after changing it using thereload() functionYou access modules attributes using the . operator:myModule.myAttribute
Modules are the highest level way of organising yourprogramLarge programs have multiple module files each of whichcontains related code
14
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Documentation
Documentation is one of the core parts of goodprogrammingPython contains an inbuilt documentation mechanismusing "doc strings"For modules the doc string is the first string in the modulefile.Doc strings must be enclosed in triple quotes.A modules doc string is accessible through an attributecalled __doc__
>>> import os>>> os.access.__doc__’access(path, mode) -> 1 if granted, 0 otherwiseTest for access to a file.’>>>
15
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
More docstring examples
def phase_of_the_moon ( ) :" This f u n c t i o n re tu rns a s l i g h t l y randomizedi n t e g e r t h a t s h u f f l e s data around i n a wayconvenient f o r the XYZ c lass . "# Working code here .return value
def something ( ) :" This i s a f i r s t paragraph .
This i s a second paragraph . The i n t e r v e n i n gblank l i n e means t h i s must be a new paragraph . "# . . .
16
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Module attributes
__doc__ is one of four special module attributesThe others are:
__name__ - The module name__file__ - The modules file name (complete path)__builtin__ - Ignore for now.
All special names in python begin and end with __
You can see all the attributes of a module using thedir() function, which returns a list data type - more onlists later
dir returns a list of the attributes and methods of any object:modules, functions, strings, lists, dictionaries...
17
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
The import search path
>>> import sys>>> sys . path[ ’ ’ , ’ / usr / l o c a l / l i b / python2 .2 ’ , ’ / usr / l o c a l / l i b / python2 . 2 / p la t−l i n u x 2 ’ ,’ / usr / l o c a l / l i b / python2 . 2 / l i b −dynload ’ , ’ / usr / l o c a l / l i b / python2 . 2 / s i t e−packages ’ ,’ / usr / l o c a l / l i b / python2 . 2 / s i t e−packages / PIL ’ ,’ / usr / l o c a l / l i b / python2 . 2 / s i t e−packages / p idd le ’ ]>>> sys<module ’ sys ’ ( b u i l t −in ) >>>> sys . path . append ( ’ /my/ new / path ’ )
import sys , ospr in t ’ sys . argv [ 0 ] = ’ , sys . argv [ 0 ]pathname = os . path . dirname ( sys . argv [ 0 ] )pr in t ’ path = ’ , pathnamepr in t ’ f u l l path = ’ , os . path . abspath ( pathname )
18
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Function basics
http://docs.python.org/library/functions.html
We have already seen two functions - reload() &dir()
Functions are defined using the def statementThe return statement sends a functions result back tothe caller.All code that is in the function must be indentedThe function ends when the indentation level is the sameas the def statement that created it.The functions arguments are given in brackets after thename
Note you do not declare types in the argument list!You can use any object as the arguments to a function:e.g. Numbers, modules and even other functions!
19
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
An example function
def mult ( a , b ) :i f b == 0:
return 0r e s t = mult ( a , b − 1)value = a + r e s treturn value
pr in t " 3 ∗ 2 = " , mul t (3 , 2)
20
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Recursivity
ExampleWrite a function for the factorial of a number
ExampleWrite a function for counting down from a given integer
21
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Factorial
def f a c t o r i a l ( n ) :i f n <= 1:
return 1return n ∗ f a c t o r i a l ( n − 1)
pr in t " 2 ! = " , f a c t o r i a l ( 2 )pr in t " 3 ! = " , f a c t o r i a l ( 3 )pr in t " 4 ! = " , f a c t o r i a l ( 4 )pr in t " 5 ! = " , f a c t o r i a l ( 5 )
22
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Countdown
def count_down ( n ) :pr in t ni f n > 0:
return count_down ( n−1)
count_down ( 5 )
23
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
More on functions
The function is not created until def is executedLike numbers and modules, functions are objectsWhen def executes it creates a function object andassociates a name with it.
24
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Argument values
def ask_ok ( prompt , r e t r i e s =4 , compla in t= ’ Yes or no , please ! ’ ) :while True :
ok = raw_input ( prompt )i f ok in ( ’ y ’ , ’ ye ’ , ’ yes ’ ) :
return Truei f ok in ( ’ n ’ , ’ no ’ , ’ nop ’ , ’ nope ’ ) :
return Falser e t r i e s = r e t r i e s − 1i f r e t r i e s < 0:
raise IOError ( ’ r e fusen i k user ’ )pr in t compla in t
ask_ok ( ’Do you r e a l l y want to q u i t ? ’ )ask_ok ( ’OK to ove rwr i t e the f i l e ? ’ , 2)ask_ok ( ’OK to ove rwr i t e the f i l e ? ’ , 2 , ’Come on , on ly yes or no ! ’ )
25
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Lambda forms
Lambda forms can be used wherever function objects arerequired. They are syntactically restricted to a singleexpression.
>>> def make_incrementor ( n ) :. . . return lambda x : x + n. . .>>> f = make_incrementor (42)>>> f ( 0 )42>>> f ( 1 )43
http://www.secnetix.de/olli/Python/lambda_functions.hawk
26
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Function documentation
Like modules functions can also have doc strings.The doc string is the first string after the functiondefinition.It must be enclosed in triple quotes ”’ ”’.It is accessible through the attribute __doc__.
27
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Objects and attributes
In python everything is an object.NumbersFunctionsModules
In python all objects have attributesThe dir() function lists the attributes of any objectRemember objects also have types
Functions are of type functionIntegers have type int etc.
Use the type() function to get an objects type.
28
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
ExampleCreate a module called firstExercise.py. Define thefollowing functions and variables in the module:
A function called objectDocumention which takes oneargument and returns the doc string of the argument.A function called objectName which takes oneargument and returns its __name__ attribute.A function called multiply(a, b) which returns a× b.Try passing objects other than numbers.A function called integerMultiply(a,b) whichconverts its arguments to integers before multiplyingthem. Hint: Use the function int() to convert objects tointegers. Try with mixed numbers and strings
Load the module from the interactive shell and test it.29
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
ExampleWrite a program (Python script) named madlib.py, whichasks the user to enter a series of nouns, verbs, adjectives,adverbs, plural nouns, past tense verbs, etc., and thengenerates a paragraph which is syntactically correct butsemantically ridiculous
30
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Coercion
Converting an object from one type to another is calledcoercion>>> x=2>>> y=3.>>> coerce(x,y)(2.0, 3.0)
However not all objects can be coerced.When performing numeric operations the object with thesmaller type is converted to the larger type.When using and or or the left hand operand is convertedto a bool.The standard coercion functions for the types we haveseen so far are int(), float(), str(), bool()
31
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Bool conversions
Any non-zero number or non-empty object converts toTrue
A zero number or an empty object is False.
32
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Operator overloading
Operators that perform different actions depending onthe types of the operands are said to be overloaded*
Multiplies when the operands are both numbersReplicates when one is a number and the other a string
+Adds when the operands are both numbersConcatenates when the operands are both strings.
Many operators in python are overloaded.Notice that when the operands do not support theoperator python raises an error. There is no point inchecking your self.Also when the operators meaning is ambiguous an erroris raised: using + with a string and a number - addition orconcatenation?
33
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Some other terminology
Assigning an object to a name e.g. a = 3,firstFunction = secondFunction, is often calledbinding.Changing what a name refers to is called rebinding.a = 3 Binds the name a to the object 3a = "aString" Rebinds the name a to the object"aString"
34
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Strings
A string is an ordered collection of characters.They are immutable i.e. They cannot be changed.You can create strings using
Double quotes - ""Single quotes - ”Triple quotes ”’ ”’ - i.e. Doc strings.
Double and single quotes are the sameTriple quotes create block strings which can spanmultiple lines.
h e l l o = " This i s a r a t h e r long s t r i n g con ta in ing \ n \severa l l i n e s o f t e x t j u s t as you would do i n C . \ n \
Note t h a t whitespace at the beginning o f the l i n e i s \s i g n i f i c a n t . "
pr in t h e l l o
35
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Basic String Operations
We’ve already seen * (replicate) and + (concatenate)Since strings are ordered collection of characters we canaccess their components by indexingThe first character in the string has position 0.The position of the last character is equal to the numberof characters in the string -1.[] is the index operator
aString = "Genial"aString[1]You can also index from the end using negative numbers
aString[-1] (This is the position = number ofcharacters in the string -1)"Genial" = length is 6"Genial"[-1] is position 6 - 1 = 5 ("l")
36
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Slicing
Slicing takes specified parts of a string and creates anew string.[start:end] Take from position start up to but notincluding position endAstring[1:3]
If start is blank i.e. [:end]. It means from the firstpositionIf end is blank i.e. [start:]. It means go to the lastpositionExtended slicing [start:end:step]
[1:10:2] - Get the characters from 1 to 10 taking stepsof 2.
37
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
String examples
>>> word = ’ Help ’ + ’A ’>>> word’ HelpA ’>>> ’< ’ + word∗5 + ’> ’’ <HelpAHelpAHelpAHelpAHelpA> ’
>>> ’ s t r ’ ’ ing ’ # <− This i s ok’ s t r i n g ’>>> ’ s t r ’ . s t r i p ( ) + ’ ing ’ # <− This i s ok’ s t r i n g ’>>> ’ s t r ’ . s t r i p ( ) ’ ing ’ # <− This i s i n v a l i d
F i l e "<s td in > " , l i n e 1 , in ?’ s t r ’ . s t r i p ( ) ’ ing ’
^SyntaxError : i n v a l i d syntax
38
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
String examples
>>> word [ 0 ] = ’ x ’Traceback ( most recent c a l l l a s t ) :
F i l e "<s td in > " , l i n e 1 , in ?TypeError : ob jec t does not support i tem assignment
>>> word [ −2: ] # The l a s t two charac te rs’pA ’>>> word [ −100:]’ HelpA ’>>> word [−10] # e r r o rTraceback ( most recent c a l l l a s t ) :
F i l e "<s td in > " , l i n e 1 , in ?IndexEr ror : s t r i n g index out o f range
+---+---+---+---+---+| H | e | l | p | A |+---+---+---+---+---+0 1 2 3 4 5-5 -4 -3 -2 -1
39
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
String examples
s t r i n g 1 = "A, B, C, D, E, F"
pr in t " S t r i n g i s : " , s t r i n g 1pr in t " S p l i t s t r i n g by spaces : " , s t r i n g 1 . s p l i t ( )pr in t " S p l i t s t r i n g by commas : " , s t r i n g 1 . s p l i t ( " , " )pr in t " S p l i t s t r i n g by commas, max 2: " , s t r i n g 1 . s p l i t ( " , " , 2 )pr in t
Removing leading and/or trailing characters in a string:
s t r i n g 1 = " \ t \ n This i s a t e s t s t r i n g . \ t \ t \ n "pr in t ’ O r i g i n a l s t r i n g : "%s " \ n ’ % s t r i n g 1pr in t ’ Using s t r i p : "%s " \ n ’ % s t r i n g 1 . s t r i p ( )pr in t ’ Using l e f t s t r i p : "%s " \ n ’ % s t r i n g 1 . l s t r i p ( )pr in t ’ Using r i g h t s t r i p : "%s " \ n ’ % s t r i n g 1 . r s t r i p ( )
40
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Lists
Lists contain ordered collections of any type of object:Numbers, strings, other lists.List Properties:
MutableCan change the object at any positionCan add and remove items from a list (more later)
HeterogenousCan contain a mixture of data
Creating a listmyList = []myList = [3, 4, "Jordi"]myList = ["aString", [3, 4, "Jordi"]]
41
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
List Operations
A list like a string is a sequence. All the operators thatwork on strings work on lists (more overloading)
* (replication)+ (concatenation)[] (indexing)[:] (slicing)
In addition a list is mutable - you can assign to listpositions
Index assignment: myList[3] = "Hello"Slice assignment: MyList[0:3] = [0,1] (Two steps:Deletion - the slice on the left is deleted; Insertion - theslice on the right is inserted in its place.
42
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
List Operations
Trying to access a position that does not exist in asequence is an errorThe function len() returns the number of items in asequence.There are two more sequence operators
x in sequence evaluates as True if the object x is inthe sequence or false if its not. e.g. 3 in [1,2,3], "J"in "Jordi"x not in sequence, the opposite of in.
43
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Examples with lists
>>> q = [2 , 3 ]>>> p = [1 , q , 4 ]>>> len ( p )3>>> p [ 1 ][ 2 , 3 ]>>> p [ 1 ] [ 0 ]2>>> p [ 1 ] . append ( ’ x t r a ’ )>>> p[1 , [ 2 , 3 , ’ x t r a ’ ] , 4 ]>>> q[2 , 3 , ’ x t r a ’ ]
44
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Shallow vs Deep list copy
Shallow Copy: (copies chunks of memory from one locationto another)
a = [ ’ one ’ , ’ two ’ , ’ th ree ’ ]b = a [ : ]b [ 1 ] = 2pr in t i d ( a ) , a #Output : 1077248300 [ ’ one ’ , ’ two ’ , ’ th ree ’ ]pr in t i d ( b ) , b #Output : 1077248908 [ ’ one ’ , 2 , ’ th ree ’ ]
Deep Copy: (Copies object reference)
a = [ ’ one ’ , ’ two ’ , ’ th ree ’ ]b = ab [ 1 ] = 2pr in t i d ( a ) , a #Output : 1077248300 [ ’ one ’ , 2 , ’ th ree ’ ]pr in t i d ( b ) , b #Output : 1077248300 [ ’ one ’ , 2 , ’ th ree ’ ]
45
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
The del statement
>>> a = [−1 , 1 , 66.25 , 333 , 333 , 1234.5]>>> del a [ 0 ]>>> a[1 , 66.25 , 333 , 333 , 1234.5]>>> del a [ 2 : 4 ]>>> a[1 , 66.25 , 1234.5]>>> del a [ : ]>>> a[ ]
46
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
if statement
i f t e s t 1 :<statements1 >
e l i f t e s t 2 :<statements2 >
else :<statements3 >
All code that exists in the if statement must be indented(there are no braces etc.)Expression is any python expression that evaluates to aboolean i.e True or False
47
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Example if statement
>>> x = i n t ( raw_input ( " Please enter an i n t e g e r : " ) )Please enter an i n t e g e r : 42>>> i f x < 0:. . . x = 0. . . pr in t ’ Negative changed to zero ’. . . e l i f x == 0:. . . pr in t ’ Zero ’. . . e l i f x == 1:. . . pr in t ’ S ing le ’. . . else :. . . pr in t ’ More ’. . .More
48
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
While loops
while t e s t :<statements >
Repeatedly executes <statements> until test is trueExample:>>> # Fibonacc i se r i es :. . . # the sum of two elements de f ines the next. . . a , b = 0 , 1>>> while b < 10:. . . pr in t b. . . a , b = b , a+b. . .112358
49
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
for loop
for < ta rge t > in <objec t >:<statements >
When python runs this loop it assigns the elements in<object>, one by one to the variable <target>
Remember <target> is only a reference to an item inthe sequence. Rebinding <target> does not changethe item in the sequence.To change the elements of a list you need to use therange() function.
Exampletry changing the characters of "Peter" to "Roman" bydifferent methods (use while, for, ...)
50
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
for loop examples
>>> # Measure some s t r i n g s :. . . a = [ ’ ca t ’ , ’ window ’ , ’ de fenes t ra te ’ ]>>> for x in a :. . . pr in t x , len ( x ). . .ca t 3window 6defenes t ra te 12
>>> for x in a [ : ] : # make a s l i c e copy of the e n t i r e l i s t. . . i f l en ( x ) > 6 : a . i n s e r t (0 , x ). . .>>> a[ ’ de fenes t ra te ’ , ’ ca t ’ , ’ window ’ , ’ de fenes t ra te ’ ]
a.insert(len(a), x) is equivalent to a.append(x)
51
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
for loop examples
>>> a = [ ’ Mary ’ , ’ had ’ , ’ a ’ , ’ l i t t l e ’ , ’ lamb ’ ]>>> for i in range ( len ( a ) ) :. . . pr in t i , a [ i ]. . .0 Mary1 had2 a3 l i t t l e4 lamb
52
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Loop statements
break Jumps out of the innermost loop. Use when youwant a loop to end immediately due to some conditionbeing reachedcontinue Jumps to the top of the innermost loop. Usewhen you dont want to execute any more code for thisiterationpass for empty loopselse block, Executed if a loop was not exited due to abreak statement
53
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Some examples
>>> for n in range (2 , 10 ) :. . . for x in range (2 , n ) :. . . i f n % x == 0:. . . pr in t n , ’ equals ’ , x , ’ ∗ ’ , n / x. . . break. . . else :. . . # loop f e l l through w i thou t f i n d i n g a f a c t o r. . . pr in t n , ’ i s a prime number ’. . .2 is a prime number3 is a prime number4 equals 2 ∗ 25 is a prime number6 equals 2 ∗ 37 is a prime number8 equals 2 ∗ 49 equals 3 ∗ 3
>>> while True :. . . pass # Busy−wai t f o r keyboard i n t e r r u p t ( C t r l +C). . .
54
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
List comprehensions
>>> l i = [ 1 , 9 , 8 , 4 ]>>> [ elem∗2 for elem in l i ][ 2 , 18 , 16 , 8 ]>>> l i[ 1 , 9 , 8 , 4 ]>>> l i = [ elem∗2 for elem in l i ]>>> l i[ 2 , 18 , 16 , 8 ]
look at it from right to left. li is the list you’re mapping
>>> params = { " server " : " mpi lgr im " , " database " : " master " , " u id " : " sa " , "pwd" : " sec re t " }>>> [ "%s=%s " % ( k , v ) for k , v in params . i tems ( ) ][ ’ server=mpi lgr im ’ , ’ u id=sa ’ , ’ database=master ’ , ’pwd=secre t ’ ]>>> " ; " . j o i n ( [ "%s=%s " % ( k , v ) for k , v in params . i tems ( ) ] )’ server=mpi lgr im ; u id=sa ; database=master ; pwd=secre t ’
55
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Examples list comphrehensions
>>> vec1 = [2 , 4 , 6 ]>>> vec2 = [4 , 3 , −9]>>> [ x∗y for x in vec1 for y in vec2 ][ 8 , 6 , −18, 16 , 12 , −36, 24 , 18 , −54]>>> [ x+y for x in vec1 for y in vec2 ][ 6 , 5 , −7, 8 , 7 , −5, 10 , 9 , −3]>>> [ vec1 [ i ]∗ vec2 [ i ] for i in range ( len ( vec1 ) ) ][ 8 , 12 , −54]>>> [ s t r ( round (355/113.0 , i ) ) for i in range ( 1 , 6 ) ][ ’ 3.1 ’ , ’ 3.14 ’ , ’ 3.142 ’ , ’ 3.1416 ’ , ’ 3.14159 ’ ]
56
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Files
The file object in python represents a file that you canread from and write toUnlike the other python objects you can not useoperators on them e.g. +, *, [] etc.Creation
myFile = open ( l o c a t i o n )
Some Methodsread()readline()readlines()write()writelines()close()
57
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
File manipulation examples
http://docs.python.org/library/stdtypes.html?highlight=tell#file.tell http://docs.python.org/tutorial/inputoutput.html
f i l e H a n d l e = open ( ’ t e s t . t x t ’ , ’w ’ )f i l e H a n d l e . w r i t e ( ’ Test ing f i l e s i n Python . \ neas i l y ’ )f i l e H a n d l e . c lose ( )f i l e H a n d l e = open ( ’ t e s t . t x t ’ , ’ a ’ )f i l e H a n d l e . w r i t e ( ’ \ n \ n \ nBottom l i n e . ’ )f i l e H a n d l e . c lose ( )f i l e H a n d l e = open ( ’ t e s t . t x t ’ )pr in t f i l e H a n d l e . read ( )f i l e H a n d l e . c lose ( )
58
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
File manipulation examples
f i l e H a n d l e = open ( ’ t e s t . t x t ’ )pr in t f i l e H a n d l e . r ead l i ne ( )pr in t f i l e H a n d l e . t e l l ( ) # p o s i t i o n w i t h i n the f i l epr in t f i l e H a n d l e . r ead l i ne ( )f i l e H a n d l e = open ( ’ t e s t . t x t ’ )pr in t f i l e H a n d l e . read ( 1 )f i l e H a n d l e . seek ( 4 )pr in t Fi leHandle . read ( 1 )f i l e H a n d l e = open ( ’ t e s t B i n a r y . t x t ’ , ’wb ’ )f i l e H a n d l e . w r i t e ( ’ There i s no spoon . ’ )f i l e H a n d l e . c lose ( )f i l e H a n d l e = open ( ’ t e s t B i n a r y . t x t ’ , ’ rb ’ )pr in t f i l e H a n d l e . read ( )f i l e H a n d l e . c lose ( )
59
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
More sophisticated file manipulation
http://docs.python.org/library/glob.html
import os , glob , s h u t i lf i l e _ e x t = raw_input ( " Extension f o r the f i l e s : \ n " )f i l e _ c o u n t = raw_input ( " F i l e s count i n each new d i r : \ n " )f i l e _ c o u n t = i n t ( f i l e _ c o u n t )dir_base_name = raw_input ( "name base f o r d i r s : \ n " )f i lenames = glob . glob ( ( ’ ∗ . ’ + f i l e _ e x t ) )f i lenames . s o r t ( )dir_number = 0while f i lenames :
dir_number += 1new_dir = dir_base_name + s t r ( dir_number )os . mkdir ( new_dir )for n in range ( min ( f i l e _ c o u n t , len ( f i lenames ) ) ) :
s r c _ f i l e = f i lenames . pop ( 0 )s h u t i l . copy ( s r c _ f i l e , new_dir )os . u n l i n k ( s r c _ f i l e )
60
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Methods
We have seen that everything in python is an object andthat all objects have attributes. The attributes can havedifferent types e.g string, int, functionAnother type of attribute an object can have is called amethodAn objects methods are special functions that operate onthe object itself.invoked with object.method() the method doessomething with objectSome objects like modules have no methods or veryrarely used methods e.g. Functions and numbers.Lists and strings have many very commonly usedmethods.
61
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Example: String methods
Here are some string methodscapitalizecountfindindexsplit
Some methods take arguments, others don’t.Check http://docs.python.org/lib/string-methods.htmlfor a full description of the string methods.Check http://docs.python.org/lib/typesseq-mutable.html for a description of listmethods.
62
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Object attributes
We have seen that objects can have many attributes andthat all attributes are objects. (Remember dir())Generally an object’s attributes are divided into two types
Callable - They can perform some action and return aresult: Functions, methodsNot callable - Everything else (strings, lists, numbers etc.)
You can check if an object is callable using thecallable() function.Another useful function is getattr()getattr() returns an attribute of an object if you knowits name as a string.>>> l i = [ " Lar ry " , " Cur ly " ]>>> g e t a t t r ( l i , " pop " )< b u i l t −in method pop of l i s t ob jec t a t 010DF884>>>> value = ob j . a t t r i b u t e>>> value = g e t a t t r ( obj , " a t t r i b u t e " )
63
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Augmented assignment
Based on CShort hand for writing common expressions
Traditional: X = X + YAugmented: X += YX *= Y, X -=Y, X /= Y etc.
Less typingAutomatically chooses optimal method
L = L + [3,4]L.extend([3,4])L += [3,4] - Automatically chooses extend
64
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
String formatting
%
Format operator.You place a string to the right of the operator withconversion targets embedded in it.A conversion target is a % followed by a letter. The letterindicates the conversion to be performedOn the right of the format operator you place, inparentheses, one object for each conversion target in thestring.Python inserts each object into the string, the first at thefirst conversion target etc, performing the necessaryconversion first."Name %s. Age %d" % ("Joe", 52)
65
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Extended formatting
Since all basic objects in python have a string descriptionusually %s is all thats neededHowever with numbers more control is often required.%d, %e, %E, %f
Extended formatting syntax%[flags][width][.precision]codeFlags
- left justify+ add plus for positive numbers0 pad with zeros
Width is the maximum width the conversion can have.precision is the number of places after the decimalpoint.
66
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
String formatting vs. concatenating
>>> uid = " sa ">>> pwd = " secre t ">>> pr in t pwd + " i s not a good password f o r " + u idsecre t is not a good password for sa>>> pr in t "%s i s not a good password f o r %s " % (pwd , u id )secre t is not a good password for sa>>> userCount = 6>>> pr in t " Users connected : %d " % ( userCount , )Users connected : 6>>> pr in t " Users connected : " + userCountTraceback ( innermost l a s t ) :
F i l e "< i n t e r a c t i v e input > " , l i n e 1 , in ?TypeError : cannot concatenate ’ s t r ’ and ’ i n t ’ ob jec ts
See also http://docs.python.org/tutorial/inputoutput.html
67
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Tuples
A tuple is an immutable list with no methodsOrdered collection of arbitrary objectsCreation
() e.g. (3, "Name"), e.g. 3, "Name" (Not advisable)A tuple with a single element is a special case: (40,) -require a trailing comma
Can be operated on by all the immutable sequenceoperators
*, +, [], [:], inAccessed by position starting from 0Use len() to get length of a tuple
Note than only the tuple is immutable. Mutable objects ina tuple are still mutable.Tuples provide integrity (one needs to be sure thatsomething cannot be changed)
68
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Using tuples to assign values
>>> v = ( ’ a ’ , ’ b ’ , ’ e ’ )>>> ( x , y , z ) = v>>> x’ a ’>>> y’ b ’>>> z’ e ’
v is a tuple of three elements, and (x, y, z) is a tuple ofthree variables.
69
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Sequence conversion
Like int(), float() etc. there are functions forconverting objects to lists & tuples.list()
tuple()
These functions can only coerce objects that are alsosequences i.e. strings, lists, tupleslist(3) - will not worklist("3") - will work
70
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Sequence functions
filter()Filters the elements of a sequence based on a functionand produces a new sequence
map()Applies a function to every element of a sequence andreturns a list of the results.Can be used with multiple lists
reduce()Applies a function to the items of a sequence from left toright to reduce the list to a single value.Calls the function using the first two values of thesequence. Then on the result and the third item etc.
zip()Takes any number of lists as argumentsReturns a list of tuples where the first contains the firstelement of each sequence, the second the secondelement of each etc.
71
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
>>> foo = [2 , 18 , 9 , 22 , 17 , 24 , 8 , 12 , 27]>>>>>> pr in t f i l t e r ( lambda x : x % 3 == 0 , foo )[18 , 9 , 24 , 12 , 27]>>>>>> pr in t map( lambda x : x ∗ 2 + 10 , foo )[14 , 46 , 28 , 54 , 44 , 58 , 26 , 34 , 64]>>>>>> pr in t reduce ( lambda x , y : x + y , foo )139
72
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Example of the use of filter
>>> def odd ( n ) :. . . return n%2. . .>>> l i = [ 1 , 2 , 3 , 5 , 9 , 10 , 256 , −3]>>> f i l t e r ( odd , l i )[ 1 , 3 , 5 , 9 , −3]>>> f i l t e r e d L i s t = [ ]>>> for n in l i :. . . i f odd ( n ) :. . . f i l t e r e d L i s t . append ( n ). . .>>> f i l t e r e d L i s t[ 1 , 3 , 5 , 9 , −3]
odd returns 1 if n is odd and 0 if n is even.filter takes two arguments, a function (odd) and a list(li). It loops through the list and calls odd per element.You could accomplish the same thing with a for loop. Butat the cost of less compact code.
73
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Example of the use of zip
>>> mat = [. . . [ 1 , 2 , 3 ] ,. . . [ 4 , 5 , 6 ] ,. . . [ 7 , 8 , 9 ] ,. . . ]>>> z ip (∗mat )[ ( 1 , 4 , 7 ) , (2 , 5 , 8 ) , (3 , 6 , 9 ) ]
names = [ " Jesus " , " Marc " , " Michal " , "Graham" ]places = [ " Spain " , "USA" , " Poland " , "UK" ]combo = z ip (names , places )who = d i c t ( combo)
74
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Examples of the use of map
>>> pr in t map( lambda w: len (w) ,’ I t i s r a i n i n g cats and dogs ’ . s p l i t ( ) )
[ 2 , 2 , 7 , 4 , 3 , 4 ]
>>>map( f , sequence )>>>[ f ( x ) for x in sequence ]
>>>map( f , sequence1 , sequence2 )>>>[ f ( x1 , x2 ) for x1 , x2 in z ip ( sequence1 , sequence2 ) ]
75
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Exercises
ExampleWrite a code that computes the prime numbers up to 50 (hint:use the filter function)
Example
Write a code that writes a value table (x , f (x)) forf (x) = sin(x) (hint: use the map function)
ExampleWrite a code that calculates the geometric mean of a givenlist of values (hint: use the reduce function)
76
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Dictionaries
Dictionaries are mappingsUnordered collection of objects (Python 3 includes order)Access items via a key (case sensitive)Equivalent to hashes in perlVery fast retrievalMutable
Creation{} - an empty dictionary{’age’: 40, ’name’: "unknown"}
77
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Example dictionaries
>>> d = { " server " : " mpi lgr im " , " database " : " master " }>>> d{ ’ server ’ : ’ mpi lgr im ’ , ’ database ’ : ’ master ’ }>>> d [ " server " ]’ mpi lgr im ’>>> d [ " database " ]’ master ’>>> d [ " database " ] = " pubs ">>> d{ ’ server ’ : ’ mpi lgr im ’ , ’ database ’ : ’ pubs ’ }>>> d [ " u id " ] = " sa ">>> d{ ’ server ’ : ’ mpi lgr im ’ , ’ u id ’ : ’ sa ’ , ’ database ’ : ’ pubs ’ }>>> del d [ ’ u id ’ ]>>> d [ " mpi lgr im " ]Traceback ( innermost l a s t ) :
F i l e "< i n t e r a c t i v e input > " , l i n e 1 , in ?KeyError : mpi lgr im
78
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Dictionary operations
AccessingDict[key]len() - Returns the number of stored entries
AssignmentDict[key] = object
Removaldel Dict[key]The del statement can be used with lists or attributes etc.
Constructiondict(zip(keys, values))
79
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Dictionary methods
has_key()
keys()
values()
copy() . . .
80
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Note on function arguments
>>> range (3 , 6) # normal c a l l w i th separate arguments[3 , 4 , 5 ]>>> args = [3 , 6 ]>>> range (∗ args ) # c a l l w i th arguments unpacked from a l i s t[ 3 , 4 , 5 ]
def cheeseshop ( kind , ∗arguments , ∗∗keywords ) :pr in t "−− Do you have any " , kind , " ? "pr in t "−− I ’m sorry , we ’ re a l l out o f " , k indfor arg in arguments : pr in t argpr in t "−" ∗ 40keys = keywords . keys ( )keys . s o r t ( )for kw in keys : pr in t kw , " : " , keywords [ kw ]
cheeseshop ( " Limburger " , " I t ’ s very runny , s i r . " ," I t ’ s r e a l l y very , VERY runny , s i r . " ,shopkeeper= ’ Michael Pa l i n ’ ,c l i e n t = " John Cleese " ,sketch=" Cheese Shop Sketch " )
81
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Naming convention
docstrings:http://www.python.org/dev/peps/pep-0257/,and general text:http://www.python.org/dev/peps/pep-0008/.Function names should describe what the function does.
The more general the better though there is a balance.Name should be enough to give an idea of what it does.General does not mean short! Use full words
Arguments names should be as general as possible.object, aString, aFunction, comparisonFunction.A variabe name should describe what it is.
Use full words.You should not use reserved words (see page 9).Names beginning and ending in two __ are systemdefined names and have a special meaning for theinterpreter
82
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
finding substrings
>>> dna = " " " t t cacc tag tc taggacccac taa tgcaga tcc tg tgt g t c t a g c t a a g a t g t a t t a t a t c t a t a t t c a c t g g g c t t a t t g g g c c a atgaaaatatgcaagaaaggaaaaaaaagatgtagacaaggaat tc tat t t " " ">>> E= ’ gat ’>>> dna . f i n d (E)48>>> dna . index (E)48
Try looking for a non-existing substring with both methods
ExampleWrite a function that returns the list of codons for a DNAsequence and a given frame
83
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
First view at regular expressions
http://python.about.com/od/regularexpressions/a/
regexprimer.htm
http://docs.python.org/howto/regex.html#regex-howto
>>> import re>>> m = re . search ( ’ (?<=abc ) def ’ , ’ abcdef ’ )>>> m. group ( 0 )’ def ’>>> m = re . search ( ’ (? <=−)\w+ ’ , ’ spam−egg ’ )>>> m. group ( 0 )’ egg ’>>> m = re . match ( r " ( \w+) ( \w+) " , " Isaac Newton , p h y s i c i s t " )>>> m. group ( 0 ) # The e n t i r e match’ Isaac Newton ’>>> m. group ( 1 ) # The f i r s t parenthesized subgroup .’ Isaac ’>>> m. group ( 2 ) # The second parenthesized subgroup .’ Newton ’>>> m. group (1 , 2) # M u l t i p l e arguments g ive us a tup l e .( ’ Isaac ’ , ’ Newton ’ )
84
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Writing regex
compile() Compile a regular expression pattern into aregular expression object, which can be used formatching using its match() and search()methods
search() Scan through string looking for a location wherethe regular expression pattern produces amatch, and return a corresponding MatchObjectinstance.
match() If zero or more characters at the beginning ofstring match the regular expression pattern,return a corresponding MatchObject instance
split() Split string by the occurrences of pattern
http://docs.python.org/dev/howto/regex.html
85
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Regular expressions
A regular expression is a pattern that a string is searched for.Unix commands such as "rm *.*" are similar to regularexpressions, but the syntax of regular expressions is moreelaborated. Several Unix programs (grep, sed, awk, ed, vi,emacs) use regular expressions and many modernprogramming languages (such as Java) also support them. InPython, a regular expression is first compiled:
keyword = re . compile ( r " the " )keyword . search ( l i n e )not keyword . search ( l i n e )keyword = re . compile ( v a r i a b l e )keyword = re . compile ( r " the " , re . I ) # f o r i n s e n s i t i v e search
86
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
re.finditer()
import reimport u r l l i b 2
html = u r l l i b 2 . ur lopen ( ’ h t t p : / / cbb l . imim . es ’ ) . read ( )pa t t e rn = r ’ \ b ( the \ s +\w+ ) \ s+ ’regex = re . compile ( pa t te rn , re . IGNORECASE)for match in regex . f i n d i t e r ( html ) :
pr in t "%s : %s " % ( match . s t a r t ( ) , match . group ( 1 ) )
87
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
ExampleGiven a string of A, C, T, and G, and X, find a string where Xmatches any single character, e.g., CATGG is contained inACTGGGXXAXGGTTT.
ExampleWrite a regular expression to extract the coding sequencefrom a DNA string. It starts with the ATG codon and ends witha stop codon (TAA, TAG, or TGA).
88
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Regular expressions
>>> import re>>> re . f i n d a l l ( r ’ \ b f [ a−z ]∗ ’ , ’ which f o o t or hand f e l l f a s t e s t ’ )[ ’ f o o t ’ , ’ f e l l ’ , ’ f a s t e s t ’ ]>>> re . sub ( r ’ ( \ b [ a−z ] + ) \1 ’ , r ’ \1 ’ , ’ ca t i n the the hat ’ )’ ca t i n the hat ’>>> ’ tea f o r too ’ . rep lace ( ’ too ’ , ’ two ’ )’ tea f o r two ’
89
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Regular expressions
# ! / usr / b in / env pythonimport re
# open a f i l ef i l e = open ( " a l i c e . t x t " , " r " )t e x t = f i l e . read l i nes ( )f i l e . c lose ( )
# compi l ing the regu la r expression :keyword = re . compile ( r " the " )
# searching the f i l e content l i n e by l i n e :for l i n e in t e x t :
i f keyword . search ( l i n e ) :pr in t l i n e ,
90
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Regular expressions
# ! / usr / b in / env pythonimport re
# open a f i l ef i l e = open ( " a l i c e . t x t " , " r " )t e x t = f i l e . read l i nes ( )f i l e . c lose ( )
# searching the f i l e content l i n e by l i n e :keyword = re . compile ( r " the " )
for l i n e in t e x t :r e s u l t = keyword . search ( l i n e )i f r e s u l t :
pr in t r e s u l t . group ( ) , " : " , l i n e ,
http://docs.python.org/library/re.htmlhttp://www.amk.ca/python/howto/regex/
91
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Write scripts that
ExampleRetrieve all lines from a given file that do not contain "the ".Retrieve all lines that contain "the " with lower or upper caseletters (hint: use the ignore case option)
ExampleRetrieve lines from a long sequence (eg, CFTR) that containa given codon, and then a given first and third letter for eachtriad
http://www.upriss.org.uk/python/session7.html#chars
92
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
ExampleWrite a script that asks users for their name, address andphone number. Test each input for accuracy, for example,there should be no letters in a phone number. A phonenumber should have a certain length. An address shouldhave a certain format, etc. Ask the user to repeat the input incase your script identfies it as incorrect.
93
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Classes: Some defs
Namespace mapping from names to objects. There isabsolutely norelation between names in differentnamescapes (different local names in a functioninvocation, for example; that is why we prefixthem with the name of the function, for example).
Scope textual region of a Python program where anamespace is directly accessible.
Attributes anything you can call in theform:object.attribute (data and methods).
Instance objects created by instantiation of classes.
http://docs.python.org/tutorial/classes.htmlhttp://pytut.infogami.com/node11-baseline.html
94
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Global vs local variables
# ! / usr / l o c a l / b in / python" " " h t t p : / / www. wel lho . net / resources / ex . php4? i tem=y105 / l ocva r . py " " "# Var iab le scope
f i r s t = 1
def one ( ) :" Double a g loba l va r iab le , r e t u r n i t + 3 . "global f i r s tf i r s t ∗= 2r e s u l t = f i r s t +3return r e s u l t
pr in t one . __doc__pr in t one ( )pr in t one ( )pr in t one ( )pr in t " f i r s t now has the value " , f i r s tpr in t " r e s u l t has the value " , r e s u l t
95
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
A first example of a class
# ! / usr / b in / python" " " house . py −− A house program . " " "
class House ( ob jec t ) :" " " Some s t u f f " " "
my_house = House ( ) # c lass i n s t a n t i a t i o nmy_house . number = 40 # data a t t r i b u t emy_house . rooms = 8my_house . garden = 1
pr in t "My house i s number " , my_house . numberpr in t " I t has " , my_house . rooms , " rooms "i f my_house . garden :
garden_text = " has "else :
garden_text = " does not have "pr in t " I t " , garden_text , " a garden "
96
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
A second example of a class
# ! / usr / b in / python" " " house2 . py −− Another house ." " "
class House ( ob jec t ) :def _ _ i n i t _ _ ( s e l f , number , rooms , garden ) :
s e l f . number = numbers e l f . rooms = roomss e l f . garden = garden
my_house = House(20 , 1 , 0)
pr in t "My house i s number " , my_house . numberpr in t " I t has " , my_house . rooms , " rooms "i f my_house . garden :
garden_text = " has "else :
garden_text = " does not have "pr in t " I t " , garden_text , " a garden "
97
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Adding methods
# ! / usr / b in / python" " " square . py −− Make some noise about a square ." " "
class Square :def _ _ i n i t _ _ ( s e l f , length , width ) :
s e l f . l eng th = leng ths e l f . w id th = width
def area ( s e l f ) :return s e l f . l eng th ∗ s e l f . w id th
my_square = Square (5 , 2)pr in t my_square . area ( )
http://www.ibiblio.org/g2swap/byteofpython/read/oops.html
98
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Some terminology
A class creates a new type where objects are instancesof the class.The ’functions’ that are part of an object are calledmethods.The fields and methods are called ’attributes’.You can examine all the methods and attributes that areassociated with an object using the dir command :print dir(some_obj)
Fields are of two types - they can belong to eachinstance/object of the class or they can belong to theclass itself. They are called instance variables and classvariables respectively.
http://www.voidspace.org.uk/python/articles/OOP.shtml
99
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Arrays and classes
# ! / usr / b in / python" " " person . py −− A person example ." " "class Person ( ob jec t ) :
def _ _ i n i t _ _ ( s e l f , age , house_number ) :s e l f . age = ages e l f . house_number = house_number
a lex = [ ]for i in range ( 5 ) :
ob j = Person ( i , i )a lex . append ( ob j )
pr in t " Alex [ 3 ] age i s " , a lex [ 3 ] . agepr in t
for alexsub in alex :pr in t "Age i s " , alexsub . agepr in t " House number i s " , alexsub . house_number
100
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Examples
ExampleWrite a simple program that reads from a CSV file containinga list of names, addresses, and ages and returns the name,address and age for a particular person upon request.
ExampleExtend the above program to include e-mail addresses andphone numbers to the student’s data. (Hint: http://www.upriss.org.uk/python/session13.html)
101
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Syntax errors
http://docs.python.org/tutorial/errors.html
>>> while True pr in t ’ He l lo wor ld ’F i l e "<s td in > " , l i n e 1 , in ?
while True pr in t ’ He l lo wor ld ’^
SyntaxError : i n v a l i d syntax
102
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Syntax errors
>>> 10 ∗ ( 1 / 0 )Traceback ( most recent c a l l l a s t ) :
F i l e "<s td in > " , l i n e 1 , in ?ZeroD iv i s i onEr ro r : i n t e g e r d i v i s i o n or modulo by zero>>> 4 + spam∗3Traceback ( most recent c a l l l a s t ) :
F i l e "<s td in > " , l i n e 1 , in ?NameError : name ’spam ’ is not def ined>>> ’ 2 ’ + 2Traceback ( most recent c a l l l a s t ) :
F i l e "<s td in > " , l i n e 1 , in ?TypeError : cannot concatenate ’ s t r ’ and ’ i n t ’ ob jec ts
103
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Handling exceptions
# ! / usr / b in / env python## Program to read and p r i n t a f i l e
import sys
t ry :f i l e = open ( " a l i c e . t x t " , " r " )
except IOError :pr in t " Could not open f i l e "sys . e x i t ( )
t e x t = f i l e . read l i nes ( )f i l e . c lose ( )
for l i n e in t e x t :pr in t l i n e ,
pr in t
104
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Exceptions
. . . except ( RuntimeError , TypeError , NameError ) :
. . . pass
>>> def t h i s _ f a i l s ( ) :. . . x = 1/0. . .>>> t ry :. . . t h i s _ f a i l s ( ). . . except ZeroD iv i s i onEr ro r as d e t a i l :. . . pr in t ’ Handl ing run−t ime e r r o r : ’ , d e t a i l. . .Handl ing run−t ime e r r o r : i n t e g e r d i v i s i o n or modulo by zero
105
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
A useful case
import getopt , sys
def main ( ) :t ry :
opts , args = getopt . ge topt ( sys . argv [ 1 : ] , " ho : " , [ " help " , " output= " ] )except getopt . GetoptEr ror :
# p r i n t help i n fo rma t i on and e x i t :usage ( )sys . e x i t ( 2 )
output = Nonefor o , a in opts :
i f o in ( "−h " , "−−help " ) :usage ( )sys . e x i t ( )
i f o in ( "−o " , "−−output " ) :ou tput = a
# . . .i f __name__ == " __main__ " :
main ( )106
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Exceptions
>>> def d i v i d e ( x , y ) :. . . t ry :. . . r e s u l t = x / y. . . except ZeroD iv i s i onEr ro r :. . . pr in t " d i v i s i o n by zero ! ". . . else :. . . pr in t " r e s u l t i s " , r e s u l t. . . f i n a l l y :. . . pr in t " execut ing f i n a l l y c lause ". . .>>> d i v i d e (2 , 1)r e s u l t is 2execut ing f i n a l l y clause>>> d i v i d e (2 , 0)d i v i s i o n by zero !execut ing f i n a l l y clause>>> d i v i d e ( " 2 " , " 1 " )execut ing f i n a l l y clauseTraceback ( most recent c a l l l a s t ) :
F i l e "<s td in > " , l i n e 1 , in ?F i l e "<s td in > " , l i n e 3 , in d i v i d e
TypeError : unsupported operand type ( s ) for / : ’ s t r ’ and ’ s t r ’
107
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
User defined exceptions
>>> class MyError ( Except ion ) :. . . def _ _ i n i t _ _ ( s e l f , value ) :. . . s e l f . value = value. . . def __str__ ( s e l f ) :. . . return repr ( s e l f . value ). . .>>> t ry :. . . raise MyError (2∗2). . . except MyError as e :. . . pr in t ’My except ion occurred , value : ’ , e . value. . .
My except ion occurred , value : 4>>> raise MyError ( ’ oops ! ’ )Traceback ( most recent c a l l l a s t ) :
F i l e "<s td in > " , l i n e 1 , in ?__main__ . MyError : ’ oops ! ’
108
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
User defined exceptions
class Er ro r ( Except ion ) :" " " Base c lass f o r except ions i n t h i s module . " " "pass
class I n p u t E r r o r ( E r ro r ) :" " " Except ion ra ised f o r e r r o r s i n the i npu t .
A t t r i b u t e s :expr −− i npu t expression i n which the e r r o r occurredmsg −− exp lana t ion o f the e r r o r
" " "
def _ _ i n i t _ _ ( s e l f , expr , msg ) :s e l f . expr = exprs e l f .msg = msg
109
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
BioPython
Set of modules and packages for biology (sequence analysis,database access, parsers...).http://biopython.org/DIST/docs/tutorial/Tutorial.html
http://biopython.org/DIST/docs/api/
110
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Examples
>>> from Bio . Seq import Seq>>> my_seq = Seq ( "AGTACACTGGT" )>>> my_seqSeq ( ’AGTACACTGGT ’ , Alphabet ( ) )>>> pr in t my_seqAGTACACTGGT>>> my_seq . a lphabetAlphabet ( )>>> my_seq . complement ( )Seq ( ’TCATGTGACCA ’ , Alphabet ( ) )>>> my_seq . reverse_complement ( )Seq ( ’ACCAGTGTACT ’ , Alphabet ( ) )
111
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
A couple of simple exercises
ExampleSearch for CFTR nucleotide sequences in the NCBI server.Save the sequences as FASTA and GeneBank. Using theSeqIO parser extract the sequences from the files and printthem on screen.
http://biopython.org/DIST/docs/api/Bio.
SeqIO-module.html#parse
ExampleDownload an alignment for the CFTR protein entries fromPFAM (use the seed for ABC transporters). Using the AlignIOparser, extract the sequences from FASTA or Stocholmformatted files downloaded from PFAM.
http://biopython.org/DIST/docs/api/Bio.
AlignIO-module.html#parse112
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
from Bio . A l i gn . Generic import Alignmentfrom Bio . Alphabet import IUPAC , Gappedalphabet = Gapped ( IUPAC . unambiguous_dna )
a l i gn1 = Alignment ( a lphabet )a l i gn1 . add_sequence ( " Alpha " , "ACTGCTAGCTAG" )a l i gn1 . add_sequence ( " Beta " , "ACT−CTAGCTAG" )a l i gn1 . add_sequence ( "Gamma" , "ACTGCTAGDTAG" )
a l i gn2 = Alignment ( a lphabet )a l i gn2 . add_sequence ( " Del ta " , "GTCAGC−AG" )a l i gn2 . add_sequence ( " Ep is lon " , "GACAGCTAG" )a l i gn2 . add_sequence ( " Zeta " , "GTCAGCTAG" )
my_alignments = [ a l ign1 , a l i gn2 ]
See, better, MultipleSeqAlignment
113
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Converting between sequence alignmentformats
from Bio import Al ignIOcount = Al ignIO . conver t ( " PF05371_seed . s th " , " stockholm " ,
" PF05371_seed . a ln " , " c l u s t a l " )pr in t " Converted %i al ignments " % count
from Bio import Al ignIOal ignments = Al ignIO . parse ( open ( " PF05371_seed . s th " ) ,
" stockholm " )handle = open ( " PF05371_seed . a ln " , "w" )count = Al ignIO . w r i t e ( al ignments , handle , " c l u s t a l " )handle . c lose ( )pr in t " Converted %i al ignments " % count
from Bio import Al ignIOal ignment = Al ignIO . read ( open ( " PF05371_seed . s th " ) ,
" stockholm " )pr in t al ignment . format ( " c l u s t a l " )
114
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Performing alignments
BioPython provides tools for command line execution. Forexample:
>>> import os>>> import subprocess>>> from Bio . A l i gn . App l i ca t i ons import ClustalwCommandline>>> help ( ClustalwCommandline )>>> c_exe = r " / App l i ca t i ons / c lus ta lw2 ">>> asser t os . path . i s f i l e ( c_exe ) , " C l u s t a l W missing ">>> c l = ClustalwCommandline ( c_exe , i n f i l e = " c f t r . f a s t a " )>>> return_code = subprocess . c a l l ( s t r ( c l ) ,. . . s tdou t = open ( os . devnu l l ) ,. . . s t d e r r = open ( os . devnu l l ) ,. . . s h e l l =( sys . p la t f o rm != " win32 " ) )
http://docs.python.org/library/subprocess.html
http://jimmyg.org/blog/2009/working-with-python-subprocess.html
115
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Working with streams and subprocesses
import syswhile 1:
t ry :i npu t = sys . s t d i n . r ead l i ne ( )i f i npu t :
sys . s tdou t . w r i t e ( ’ Echo to s tdou t : %s ’%inpu t )sys . s t d e r r . w r i t e ( ’ Echo to s t d e r r : %s ’%inpu t )
except KeyboardError :sys . e x i t ( )
>>> subprocess . Popen ( ’ echo $PWD ’ , s h e l l =True )/ home / james / Desktop
>>> subprocess . Popen ( " " ". . . ca t << EOF > new . t x t. . . He l lo World !. . . EOF. . . " " " , s h e l l =True )
116
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Dealing with PDB files
http://www.biopython.org/DIST/docs/tutorial/Tutorial.
html#htoc133
See also [Fufezan and Specht, 2009]
117
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
PDB parsing example
>>> from Bio .PDB. PDBParser import PDBParser>>> parser=PDBParser ( )>>> s t r u c t u r e =parser . g e t _ s t r u c t u r e ( " t e s t " , " 1WQ1. pdb " )>>> s t r u c t u r e . g e t _ l i s t ( )[ <Model i d =0>]>>> model= s t r u c t u r e [ 0 ]>>> model . g e t _ l i s t ( )[ < Chain i d =R> , <Chain i d =G>]>>> chain=model [ "R" ]>>> chain . g e t _ l i s t ( )[ < Residue MET het= resseq=1 icode= > , <Residue THR het=resseq=2 icode= > , <Residue GLU het= resseq=3 icode= > , <Residue TYR het=resseq=4 icode= > , <Residue LYS het= resseq=5 icode= >
118
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Retrieving a PDB file
>>> from Bio .PDB import PDBList>>> pdbl=PDBList ( )>>> pdbl . r e t r i e v e _ p d b _ f i l e ( ’ 5P21 ’ )r e t r i e v i n g f t p : / / f t p . wwpdb . org / pub / pdb / data / s t r u c t u r e s / d i v ided / pdb / p2 / pdb5p21 . ent . gz’ / Users / j o r d i v i l l a / merda / p2 / pdb5p21 . ent ’
http://www.biopython.org/DIST/docs/cookbook/biopdb_faq.pdf or:
import u r l l i bdef fetch_pdb ( i d ) :
u r l = ’ h t t p : / / www. rcsb . org / pdb / f i l e s /%s . pdb ’ % i dreturn u r l l i b . ur lopen ( u r l ) . read ( )
119
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Plotting with Python
Matplotlib is the reference tool for plotting 2D data in Python.iPython has a "pylab" mode specific for interacting withmatplotlib.http://wiki.python.org/moin/NumericAndScientific/Plottinghttp://bmi.bmt.tue.nl/~philbers/8C080/matplotlibtutorial.html
>>> from pylab import randn , h i s t>>> x = randn (10000)>>> h i s t ( x , 100)
The pylab mode offers interaction similar to Matlab.http://matplotlib.sourceforge.net/ Check alsohttp://gnuplot-py.sourceforge.net/
120
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
pyplot
http://www.scipy.org/PyLab
import m a t p l o t l i b . pyp lo t as p l tp l t . p l o t ( [ 1 , 2 , 3 ] )p l t . y l a b e l ( ’some numbers ’ )p l t . show ( )
import m a t p l o t l i b . pyp lo t as p l tp l t . p l o t ( [ 1 , 2 , 3 , 4 ] , [ 1 ,4 ,9 ,16 ] , ’ ro ’ )p l t . ax is ( [ 0 , 6 , 0 , 20 ] )
121
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
RPy
http://rpy.sourceforge.net/ http://www.daimi.au.dk/~besen/TBiB2007/lecture-notes/rpy.htmlhttp://rpy.sourceforge.net/rpy2/doc-2.1/html/index.html
>>> from rpy import ∗>>>>>> degrees = 4>>> g r i d = r . seq (0 , 10 , leng th =100)>>> values = [ r . dchisq ( x , degrees ) for x in g r i d ]>>> r . par ( ann=0)>>> r . p l o t ( g r id , values , type= ’ l i n e s ’ )
122
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
working with numpy arrays
import numpy as npimport m a t p l o t l i b . pyp lo t as p l t
# evenly sampled t ime at 200ms i n t e r v a l st = np . arange ( 0 . , 5 . , 0 .2 )
# red dashes , blue squares and green t r i a n g l e sp l t . p l o t ( t , t , ’ r−− ’ , t , t ∗∗2 , ’ bs ’ , t , t ∗∗3 , ’ g^ ’ )
123
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Even before talking on CGI
import u r l l i b
fwcURL = " h t t p : / / cbb l . imim . es "
t ry :pr in t " Going to Web f o r data "f w c a l l = u r l l i b . ur lopen ( fwcURL ) . read ( )pr in t " Successfu l "pr in t " W i l l now p r i n t a l l o f the data to screen "pr in t " f w c a l l = " , f w c a l l
except :pr in t " Could not ob ta in data from Web"
124
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Even before talking on CGI
>>> import u r l l i b 2>>> for l i n e in u r l l i b 2 . ur lopen ( ’ h t t p : / / tycho . usno . navy . m i l / cg i−bin / t imer . p l ’ ) :. . . i f ’EST ’ in l i n e or ’EDT ’ in l i n e : # look f o r Eastern Time. . . pr in t l i n e
<BR>Nov . 25 , 09:43:32 PM EST
>>> import smtp l i b>>> server = smtp l i b .SMTP( ’ l o c a l h o s t ’ )>>> server . sendmail ( ’ soothsayer@example . org ’ , ’ jcaesar@example . org ’ ,. . . " " " To : jcaesar@example . org. . . From : soothsayer@example . org. . .. . . Beware the Ides o f March .. . . " " " )>>> server . q u i t ( )
125
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
# ! / usr / b in / env python
import cg ipr in t " Content−Type : t e x t / html \ n "
pr in t " " "<HTML><HEAD><TITLE>Hel lo World </ TITLE></HEAD><BODY><H1>Greet ings </H1></BODY></HTML>" " "
126
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Interface design
1 Encapsulation2 Generalization3 Interface design4 Refactoring
127
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Extending/embedding Python
Python provides bindings to other languages that allow forpowerful large project building. Checkhttp://docs.python.org/extending/index.html forgeneral information. Check alshttp://pyobjc.sourceforge.net/ for a bridgebetween Python and Objective C, needed for example whenbuilding framework based software.
128
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Glossary I
problem solving The process of formulating a problem, finding a solution, and expressing the solution.
high-level language A programming language like Python that is designed to be easy for humans to read andwrite.
low-level language A programming language that is designed to be easy for a computer to execute; alsocalled "machine language" or "assembly language"
portability A property of a program that can run on more than one kind of computer.
interpret To execute a program in a high-level language by translating it one line at a time.
compile To translate a program written in a high-level language into a low-level language all atonce, in preparation for later execution.
source code A program in a high-level language before being compiled.
ob ject code The output of the compiler after it translates the program.
executable Another name for ob ject code that is ready to be executed.
prompt Characters displayed by the interpreter to indicate that it is ready to take input from theuser.
script A program stored in a file (usually one that will be interpreted).
program A set of instructions that specifies a computation.
algorithm A general process for solving a category of problems.
bug An error in a program.
debugging The process of finding and removing any of the three kinds of programming errors.
syntax The structure of a program.
syntax error An error in a program that makes it impossible to parse (and therefore impossible tointerpret).
129
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Glossary II
exception An error that is detected while the program is running.
semantics The meaning of a program.
semantic error An error in a program that makes it do something other than what the programmerintended.
natural language Any one of the languages that people speak that evolved naturally.
formal language Any one of the languages that people have designed for specific purposes, such asrepresenting mathematical ideas or computer programs; all programming languages areformal languages.
token One of the basic elements of the syntactic structure of a program, analogous to a word ina natural language.
parse To examine a program and analyze the syntactic structure.
print statement An instruction that causes the Python interpreter to display a value on the screen.
instance A member of a set.
loop A part of a program that can execute repeatedly.
encapsulation The process of transforming a sequence of statements into a function definition.
generalization The process of replacing something unnecessarily specific (like a number) withsomething appropriately general (like a variable or parameter).
interface A description of how to use a function, including the name and descriptions of thearguments and return value.
development plan A process for writing programs.
docstring A string that appears in a function definition to document the function’s interface.
130
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
This document’s history
1 2007 : Original version by Michael A. Johnston2 2008 : modifications and examples added by JVF3 2010-: LATEX2e version and extensions by JVF
131
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Sources
Style guide for Python codehttp://www.python.org/dev/peps/pep-0008/
Library: http://docs.python.org/library/
http://www.thinkpython.com
http://diveintopython.org/toc/index.html
http://docs.python.org/tutorial/introduction.html
http://openbookproject.net/thinkcs/python/english2e/
http://www.penzilla.net/tutorials/python/
http://www.awaretek.com/tutorials.html
http://www.rexx.com/~dkuhlman/
http://code.google.com/edu/languages/google-python-class/
http://www.sthurlow.com/python/132
MScBIOINFO
2011-2012
Intro
Functionalprogram-ming
Classes
Exceptions
BioPython
Graphics
CGIscripting
Packaging
Extensions
Glossary
Annexes
Fufezan, C. and Specht, M. (2009).p3d–python module for structural bioinformatics.BMC Bioinformatics, 10:258.
133