Introduction To Programming Using Python
Introduction To Programming Using Python
Python
Programming Course for Biologists at the
Pasteur Institute
The objective of this course is to teach programming concepts to biologists. It is thus aimed at people who are
not professional computer scientists, but who need a better control of computers for their own research. This pro-
gramming course is part of a course in informatics for biology [http://www.pasteur.fr/formation/infobio/infobio-
en.html]. If you are already a programmer, and if you are just looking for an introduction to Python, you can go
to this Python course [http://www.pasteur.fr/recherche/unites/sis/formation/python/] (in Bioinformatics).
Handouts for practical sessions (still under construction) will be available on request.
Contact: ieb@pasteur.fr
Table of Contents
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1. First session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1. Let’s run the python interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.2. Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.3. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.4. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.5. Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1. A first program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2. Why Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3. Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2. Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1. Data, values and types of values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2. Variables or naming values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3. Variable and keywords, variable syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4. Namespaces or representing variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.5. Reassignment of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3. Statements, expressions and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.1. Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2. Sequences or chaining statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3. Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4. Iterations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.5. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.6. Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.7. Composition and Evaluation of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4. Communication with outside . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.1. Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2. Formatting strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3. Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.4. Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.4.1. Reading from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.4.2. Writing into a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.4.3. Errors with files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5. Program execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.1. Executing code from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.2. Interpreter and Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.1. Values as objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.1. Working with strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.1.1. Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.1.2. Iterations on strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7. Branching and Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.1. Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.2. Conditions and Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
7.3. Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.4. Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.5. Chained conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7.6. Nested conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.7. Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8. Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.1. Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.2. Parameters and Arguments or the difference between a function definition and a function call 59
8.3. Functions and namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
8.4. Boolean functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
9. Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
9.1. Datatypes for collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
9.2. Methods, Operators and Functions on Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
9.3. Methods, Operators and Functions on Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.4. What data type for which collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10. Nested data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
10.1. Nested data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
10.2. Identity of objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.3. Copying complex data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
10.4. Modifying nested structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
11. Repetitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.1. Repetitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.2. The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.3. The while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
11.4. Comparison of for and while loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.5. Range and Xrange objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.6. The map function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
11.7. List comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
12. Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
12.1. General Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
12.2. Python built-in exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
12.3. Raising exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
12.4. Defining exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
13. Functions II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
13.1. Passing argument by name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
13.2. Defining default values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
13.3. Variable number of parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
13.4. Functions and namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
13.5. Defining a function as a parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
14. Modules and packages in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14.1. Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14.1.1. Using modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14.1.2. Building modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14.1.3. Where are the modules? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
14.1.4. How does it work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
14.1.5. Running a module from the command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
14.2. Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
14.2.1. Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
14.3. Getting information on available modules and packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
14.4. Designing a module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
15. Recursive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
15.1. Recursive functions definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
15.2. Flow of execution of recursive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
15.3. Recursive data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
15.4. Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
16. Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
16.1. Handle files in programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
16.2. Reading data from files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
16.3. Writing in files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
16.4. Design problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
16.5. Documentation strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
17. Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
17.1. Using the system environment: os and sys modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
17.2. Running Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
17.3. Parsing command line options with getopt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
17.4. Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
17.5. Searching for patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
17.5.1. Introduction to regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
17.5.2. Regular expressions in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
17.5.3. Prosite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
17.5.4. Searching for patterns and parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
18. Object-oriented programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
18.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
18.2. What are objects and classes? An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
18.2.1. Objects description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
18.2.2. Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
18.2.3. Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
18.2.4. Creating objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
18.3. Defining classes in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
18.4. Combining objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
18.5. Classes and objects in Python: technical aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
18.5.1. Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
18.5.2. Objects lifespan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
18.5.3. Objects equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
18.5.4. Classes and types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
18.5.5. Getting information on classes and instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
18.6. Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
19. Object-oriented design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
19.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
19.2. Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
19.2.1. Software quality factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
19.2.2. Large scale programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
19.2.3. Modularity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
19.2.4. Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
19.2.5. Reusability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
19.3. Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
19.3.1. Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
19.3.2. Information hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
19.3.3. Using special methods within classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
19.4. Inheritance: sharing code among classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
19.4.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
19.4.2. Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
19.5. Flexibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
19.5.1. Summary of mechanisms for flexibility in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
19.5.2. Manual overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
19.6. Object-oriented design patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
19.7. Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
List of Figures
1.1. History of programming languages(Source) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1. Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2. Reassigning values to variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.1. Syntax tree of the expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.1. Interpretation of formatting templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.1. Comparison of compiled and interpreted code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.2. Execution of byte compiled code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.1. String indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.2. Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
7.1. Flow of execution of a simple condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.2. If statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.3. Block structure of the if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.4. Flow of execution of an alternative condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7.5. Multiple alternatives or Chained conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.6. Nested conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.7. Multiple alternatives without elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.1. Function definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.2. Blocks and indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.3. Stack diagram of function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
9.1. Comparison some collection datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
10.1. Representation of nested lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
10.2. Accessing elements in nested lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
10.3. Representation of a nested dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.4. List comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.5. Copying nested structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.6. Modifying compound objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
11.1. The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
11.2. Flow of execution of a while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
11.3. Structure of the while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
11.4. Passing functions as arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
12.1. Exceptions class hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.1. Module namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
14.2. Loading specific components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
15.1. Stack diagram of recursive function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
15.2. Stack diagram of recursive function calls for function fact() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
15.3. A phylogenetic tree topology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
15.4. Tree representation using a recursive list structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
16.1. ReBase file format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
16.2. Flowchart of the processing of the sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
17.1. Manual parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
17.2. Event-based parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
17.3. Parsing: decorated grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
17.4. Parsing result as a hierarchical document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
17.5. Pattern searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
17.6. Python regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
17.7. Python regular expressions: classes and methods summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
18.1. A DNA object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
18.2. Representation showing object’s methods as counters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
18.3. A Protein object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
18.4. Protein and DNA objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
18.5. Classes and instances namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
18.6. Class attributes in class dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
18.7. Classes methods and bound methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
18.8. Types of classes and objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
19.1. Components as a language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
19.2. A stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
19.3. Dynamic binding (1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
19.4. Dynamic binding (2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
19.5. UML diagram for inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
19.6. Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
19.7. Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
19.8. A composite tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
List of Tables
3.1. Order of operator evaluation (highest to lowest) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.1. String formatting: Conversion characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.2. String formatting: Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.3. Type conversion functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.1. String methods, operators and builtin functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.2. Boolean methods and operators on strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
7.1. Boolean operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
9.1. Sequence types: Operators and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.2. List methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.3. Dictionary methods and operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
16.1. File methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
16.2. File modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
19.1. Stack class interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
19.2. Some of the special methods to redefine Python operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
List of Examples
5.1. Executing code from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.1. More complex function definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.2. Function to check whether a character is a valid amino acid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.1. A mixed nested datastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
11.1. Translate a cds sequence into its corresponding protein sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.2. First example of a while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.1. Filename error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
12.2. Give the user a chance to enter a proper filename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
12.3. Raising an exception in case of a wrong DNA character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
12.4. Raising your own exception in case of a wrong DNA character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
12.5. Exceptions defined in Biopython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
13.1. Default values of parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
13.2. Function execution namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
13.3. Global statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
13.4. Global statement (2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
14.1. A module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
14.2. Using the Bio.Fasta package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
14.3. A genealogy module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
15.1. Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
16.1. Reading from files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
16.2. Restriction of a DNA sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
17.1. Walking subdirectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
17.2. Running a program (1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
17.3. Running a program (2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
17.4. Running a program (3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
17.5. Getopt example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
17.6. Searching for the occurrence of PS00079 and PS00080 Prosite patterns in the Human Ferroxidase
protein . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
18.1. DNA, a class for DNA sequences (first version) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
18.2. DNA, a class for DNA sequences (complete version) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
19.1. A Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
19.2. Stack class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
19.3. Defining operators for the DNA class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
19.4. Inheritance example (1): sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
19.5. Curve class: manual overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
19.6. An uppercase sequence class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
19.7. ImmutableList class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
19.8. SequenceDB class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
19.9. A composite tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
19.10. A recursive visitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
List of Exercises
3.1. Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.1. Copying a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.1. Execute code from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.1. Iterating on strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.1. Chained conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2. Nested condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
10.1. Representing complex structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
11.1. Write the complete codon usage function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
11.2. Converting a list from integers to floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
11.3. Generate the list of all possible codons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
13.1. Sorting a dictionary by its values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
14.1. Locating modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
14.2. Bio.SwissProt package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
14.3. Using a class from a module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
14.4. Import from Bio.Clustalw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
15.1. Recursive definition of reverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
16.1. Multiple sequences for all enzymes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
17.1. Basename of the current working directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
17.2. Finding files in directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
18.1. A Point class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
18.2. A Point class (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
19.1. ADT for the Point class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
19.2. Operators for the DNA class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
19.3. A ConstantPoint class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
19.4. Example of an abstract framework: Enzyme parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
19.5. Point factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
19.6. An analyzed sequence class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
19.7. A partially editable sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Chapter 1. Introduction
Chapter 1. Introduction
1.1. First session
1.1.1. Let’s run the python interpreter
>>> float(1 / 2)
0.0
>>> 1 / 2.0
0.5
>>> float(1)/2
0.5
>>> ’aaa’
’aaa’
>>> len(’aaa’)
3
What happened?
1
Chapter 1. Introduction
You can check what the values you manipulate are, or, in programming
languages parlance, what their type is:
>>> type(1)
<type ’int’>
>>> type(’1’)
<type ’str’>
>>> type(1.0)
<type ’float’>
1.1.2. Variables
So far, we have manipulated values (integers, floats and strings). Instead of manipulating values directly, litterally,
you can associate a name to a value and access to the value through the associated name:
>>> a = 3
>>> a
3
Above, the interpreter displays the value (3) of the variable (a).
>>> myvar
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ’myvar’ is not defined
What happened?
>>> a = 2
>>> a
2
>>> a * 5
2
Chapter 1. Introduction
10
>>> b = a * 5
>>> b
10
>>> a = 1
>>> b
10
1.1.3. Strings
Some magical stuff, that will be explained later:
>>> codon=’atg’
>>> codon * 3
’atgatgatg’
>>> seq1 = ’agcgccttgaattcggcaccaggcaaatctcaaggagaagttccggggagaaggtgaaga’
>>> seq2 = ’cggggagtggggagttgagtcgcaagatgagcgagcggatgtccactatgagcgataata’
>>> len(seq)
120
3
Chapter 1. Introduction
>>> seq[1]
’g’
Why?
Because in computer science, strings are numbered from 0 to string length - 1
so the first character is:
>>> seq[0]
’a’
>>> seq[11]
’t’
>>> len(seq)
120
So, because we know the sequence length, we can display the last character
by:
>>> seq[119]
’a’
But this is not true for all the sequences we will work on.
Find a more generic way to do it.
>>> seq[len(seq) - 1]
’a’
Python provides a special form to get the characters from the end of a string:
>>> seq[-1]
’a’
>>> seq[-2]
’t’
4
Chapter 1. Introduction
>>> seq[:3]
’agc’
>>> seq[3:6]
’gcc’
>>> seq[-3:len(seq)]
’ata’
You can omit the second indice of the range if it’s len(seq):
>>> seq[-3:]
’ata’
Now, let us say that you want to find specific pattern on a DNA sequence:
5
Chapter 1. Introduction
Looking at the sequence you will see that EcoRI is present twice and
BamHI just once:
tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctgga
~~~~~~ ~~~
tccctagctaagatgtattattctgctgtgaattcgatcccactaaaga
~~~ ~~~~~~
Why ??
Why ?
>>> dna
’tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctgga\ntccctagctaagatgtattattctgctgtgaattcgatcc
6
Chapter 1. Introduction
’tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctggatccctagctaagatgtattattctgctgtgaattc
>>>find(dna, BamHI)
54
1.1.4. Functions
So far, we have used functions that perform a specific computation: len(), count(), lower(), find(), replace(), ... We
can can define our own functions:
7
Chapter 1. Introduction
>>> welcome("Pascal")
Welcome to Paris, Pascal!
I hope you will enjoy it
1.1.5. Lists
You can assemble values of any type into a list:
and access it with indices, exactly as you did with character strings:
>>> l[1]
’b’
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l = range(10)
>>>
... How would you produce the list of numbers from 10 to 20?
>>> help(range)
Now, how do you get the list of even numbers from 0 to 100?
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,
42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80,
82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
8
Chapter 1. Introduction
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
def gc_counter(seq):
nbre_c = count(seq, ’c’)
nbre_g = count(seq, ’g’)
long_seq = len(seq)
gc_percent = 100 * float(nbre_c + nbre_g) / long_seq
return gc_percent
gc = gc_counter("atgcggctgtgatgtagtcctttc")
print gc
seq = raw_input("enter a dna sequence: ")
print gc_counter(seq)
will be explained later in this course. Let’s say it enables our program to use functions for computing common
things on character strings: length, counting of elements, etc...
def gc_counter(seq):
nbre_c = count(seq, ’c’)
nbre_g = count(seq, ’g’)
long_seq = len(seq)
gc_percent = 100 * float(nbre_c + nbre_g) / long_seq
return gc_percent
The function name is: gc_counter. This is just a definition: in order to invoke this function, a call must be
issued, as we will have later in the program.
Obviously, this function performs the steps to compute a GC%. It counts the number of ’g’ and ’c’, adds them,
and finally divide the sum by the length of the sequence.
9
Chapter 1. Introduction
A special statement: return, specifies which value is returned to the statement that invoked the function, i.e the
value specified in the return, here gc_percent. So, in the following statement:
gc = gc_counter("atgcggctgtgatgtagtcctttc")
the evaluation of the gc_counter() call will eventually lead to the returned value.
gc = gc_counter("atgcggctgtgatgtagtcctttc")
print gc
seq = raw_input("enter a dna sequence: ")
print gc_counter(seq)
The 1st statement computes the GC% of a character string: "atgcggctgtgatgtagtcctttc" and stores the result in a
variable named gc. For this purpose, it calls the gc_counter function and provides this function with the
character string as an argument. The next statement prints the resulting GC%.
A 3rd statement prints a message on the screen and input a string entered by the user to the program. The last
statement print the result of calling the gc_counter function applied to the string just entered by the user.
You can put this program into a file and try it. For this purpose, run your text editor and copy the python code
above into the editor. Save this as a file called: my_gc.py. It is common and convenient to call a python file by a
name having ".py" as an extension.
Once saved, you can run your program by issuing the following command:
python my_gc.py
This program, eventhough it’s quite simple and short, is typical of programs that are used in scientific computing
(except for graphical and interactive programs): you provide data as input, a computation is performed and the
program outputs the results.
10
Chapter 1. Introduction
The Computers own language. Computers do not understand any of the natural languages such as English,
French or German. Their proper language, also called machine language, is only composed of two symbols “0”
and “1”, or power “on” - “off”. They have a sort of a dictionary containing all valid words of this language. These
words are the basic instructions, such as “add 1 to some number”, “are two values the same” or “copy a byte of
memory to another place”. The execution of these basic instructions are encoded by hardware components of the
processor.
Programming languages. Programming languages belongs to the group of formal languages. Some other
examples of formal languages are the system of mathematical expressions or the languages chemists use to
describe molecules. They have been invented as intermediate abstraction level between humans and computers.
Why do not use natural languages as programming languages? Programming languages are design to prevent
problems occurring with natural language.
Ambiguity Natural languages are full of ambiguities and we need the context of a word in order to
choose the appropriate meaning. “minute” for example is used as a unit of time as a noun,
but means tiny as adjective: only the context would distinguish the meaning.
Redundancy Natural languages are full of redundancy helping to solve ambiguity problems and to
minimize misunderstandings. When you say “We are playing tennis at the moment.”, “at
the moment” is not really necessary but underlines that it is happening now.
Literacy Natural languages are full of idioms and metaphors. The most popular in English is
probably “It rains cats and dogs.”. Besides, this can be very complicated even if you speak
a foreign language very well.
Programming languages are foreign languages for computers. Therefore you need a program that translates your
source code into the machine language. Programming languages are voluntarily unambiguous, nearly context
free and non-redundant, in order to prevent errors in the translation process.
History of programming languages. It is instructive to try to communicate with a computer in its own language.
This let you learn a lot about how processors work. However, in order to do this, you will have to manipulate only
0’s and 1’s. You will need a good memory, but probably you would never try to write a program solving real world
problems at this basic level of machine code.
Because humans have difficulties to understand, analyze and extract information from sequences of zeroes and
ones, they have written a language called Assembler that maps the instruction words to synonyms that give an idea
of what the instruction does, so for instance 0001 became add. Assembler increased the legibility of the code, but
the instruction set remained basic and depended on the hardware of the computer.
11
Chapter 1. Introduction
In order to write algorithms for solving more complex problems, there was a need for machine independent higher
level programming languages with a more elaborated instruction set than the low level Assembler. The first ones
were Fortran and C and a lot more have been invented right now. A short history of a subset of programming
languages is shown in Figure 1.1.
12
Chapter 1. Introduction
1956 Fortran I
1958 Lisp
1962
1964 PL/I
1966
1968 Smalltalk
1972
1974 Scheme
1976 Fortran 77 ML
1978 C (K&R)
1980 Smalltalk 80
1992
1994 Ruby
1996 OCaml
2000 C# 13
2002
Chapter 1. Introduction
14
Chapter 2. Variables
Chapter 2. Variables
2.1. Data, values and types of values
In the first session we have explored some basic issues about a DNA sequence. The specific DNA sequence
’atcgat’ was one of our data. For computer scientists this is also a value. During the program execution values
are represented in the memory of the computer. In order to interpret these representations correctly values have a
type.
Type
Types are sets of data or values sharing some specific properties and their associated operations.
We have modeled the DNA sequence, out of habit, as a string. 1 Strings are one of the basic types that Python can
handle. In the gc calculation we have seen two other ones: integers and floats. If you are not sure what sort of data
you are working with, you can ask Python about it.
>>> type(’atcgat’)
<type ’str’>
>>> type(1)
<type ’int’>
>>> type(’1’)
<type ’str’>
Binding
Binding is the process of naming a value.
Variable
Variables are names bound to values. You can also say that a variable is a name that refers to a value.
For Python the model is important because it knows nothing about DNA but it knows a lot about strings.
15
Chapter 2. Variables
So the variable EcoRI is a name that refers to the string value ’GAATTC’.
The construction used to give names to values is called an assignment. Python, as a lot of other programming
languages, use the sign = to assign value to variables. The two sides of the sign = can not be interchanged. The
left side has always to be a variable and the right side a value or a result of a calculation.
Caution
Do not confuse the usage of = in computer science and mathematics. In mathematics, it represents the
equality, whereas in Python it is used to give names. So all the following statements are not valid in
Python:
Important
Python variable names are case-sensitive, so EcoRI and ecoRI are not the same variable.
Among the words you can construct with these letters, there are some reserved words for Python and can not be
used as variable names. These keywords define the language rules and have special meanings in Python. Here is
the list of all of them:
16
Chapter 2. Variables
Namespace
A namespace is a mapping of variable names to their values.
You can also think about a namespace as a sort of dictionary containing all defined variable names and the
corresponding reference to their values.
Reference
A reference is a sort of pointer to a location in memory.
Therefore you do not have to know where exactly your value can be found in memory, Python handles this for you
via variables.
Namespace
EcoRI ’GAATTC’
gc
0.546
105
Memory space
Figure 2.1 shows a representation of some namespace. Values which have not been referenced by a variable, are
not accessible to you, because you can not access the memory space directly. So if a result of a calculation is
returned, you can use it directly and forget about it after that. Or you can create a variable holding this value and
then access this value via the variable as often as you want.
17
Chapter 2. Variables
In this example you can remember the result of the gc calculation, because it is stored in the variable gc.
Namespace
EcoRI ’GAATTC’
gc
0.546
0.45
105
Memory space
18
Chapter 2. Variables
Note
In Python, it is possible to reassign a new value with a different type to a variable. This is called dynamic
typing, because the type of the variable is assigned dynamically. Note that this is not the case in all
programming languages. Sometimes, as in C, the type of variables is assigned statically and has to be
declared before use. This is some way more secure because types of variables can be checked only by
examining the source code, whereas that is not possible if variables are dynamically typed.
19
Chapter 2. Variables
20
Chapter 3. Statements, expressions and functions
Statement
Statements are instructions or commands that the Python interpreter can execute. Each statement is read by the
interpreter, analyzed and then executed.
Sequence
Sequencing is a simple programming feature that allows to chain instructions that will be executed one by one
from top to bottom.
Later we are going to learn more complicated ways to control the flow of a program, such as branching and
repetition.
3.3. Conditionals
Sometimes the continuation of a program depends on a condition. We would either execute a part of the program
if this condition is fulfilled or adapt the behavior of the program depending on the truth value of the condition.
21
Chapter 3. Statements, expressions and functions
In the code above, the first block of statements, between the if and the else, is executed if the string contains some
’n’, whereas the second block, after the else, is executed when it does not. Notice that the two blocks are indented.
3.4. Iterations
It is possible to specify that statements are to be repeated. Say that you would like to print a hello message to a
list of persons. You could specify:
A nice feature is the for control statement, that enables to specify that a statement (or a more complex sequence
of statements) is to be repeated over a set of values:
Each value of the set is assigned one after the other to a variable, here the person variable. Such a construct is
generally called a loop, and the variable person is therefore called the loop variable. Notice the indented block
of statements below the for. This block corresponds to the statements that will be repeated over the set of values.
3.5. Functions
Function
Functions are named sequences of statements that execute some task.
>>> type(’GAATTC’)
<type ’str’>
>>> len(cds)
852
For example len is a function that calculates the length of things and we asked here for the length of our DNA
sequence cds.
22
Chapter 3. Statements, expressions and functions
Function call
Function calls are statements that execute or call a function. The Python syntax of function calls is the function
name followed by a comma separated list of arguments inclosed into parentheses. Even if a function does not take
any argument the parentheses are necessary.
Differences between function calls and variables. As variable names, function names are stored in a namespace
with a reference to their corresponding sequence of statements. When they are called, their name is searched in the
namespace and the reference to their sequence of statements is returned. The procedure is the same as for variable
names. But unlike them, the following parentheses indicate that the returned value is a sequence of statements that
has to be executed. That’s why they are even necessary for functions which are called without arguments.
Arguments of functions
Arguments are values provided to a function when the function is called. We will see more about them soon.
3.6. Operations
Operations and Operators
Operations are “basic” functions with their own syntax.
They have a special Operator (a sign or a word) that is the same as a function name. Unary Operators, operations
which take one argument, are followed by their argument, and secondary operators are surrounded by their two
arguments.
This is only a simpler way of writing these functions provided by Python, because humans are in general more
familiar with this syntax closely related to the mathematical formal language.
23
Chapter 3. Statements, expressions and functions
We have already used it. Here is the most complex example we have seen so far:
What is happening when this expression is executed? The first thing to say is that it is a mixed expression
of operations and function calls. Let’s start with the function calls. If a function is called with an argument
representing a composed expression, this one is executed first and the result value is passed to the calling function.
So the cds variable is evaluated, which returns the value that it refers to. This value is passed to the len function
which returns the length of this value. The same happens for the float function. The operation count(cds,
’G’) + count(cds, ’C’) is evaluated first, and the result is passed as argument to float.
Let’s continue with the operations. There is a precedence list, shown in Table 3.1, for all operators, which
determines what to execute first if there are no parentheses, otherwise it is the same as for function calls. So,
for the operation count(cds, ’G’) + count(cds, ’C’) the two count functions are executed first on
the value of the cds variable and “G” and “C” respectively. And the two counts are added. The result value
of the addition is then passed as argument to the float function followed by the division of the results of the
two functions float and len. Figure 3.1 shows the decomposition of the expression into its subcomponents.
Sub-expressions are evaluated from bottom to top.
float() len()
cds
+
’atgcctgt...’ ’atgcctgt...’
24
Chapter 3. Statements, expressions and functions
x + y, x - y Addition, subtraction
x << y, x >> y Bit shifting
x & y Bitwise and
x | y Bitwise or
x < y, x <= y, x > y, x >= y, x == y, Comparison, identity, sequence membership tests
x != y, x <> y, x is y, x is not y, x
in s, x not in s<
not x Logical negation
x and y Logical and
lambda args: expr Anonymous function
So, as in mathematics, the innermost function or operation is evaluated first and the result is passed as argument
to the enclosing function or operation. It is important to notice that variables are evaluated and only their values
are passed as argument to the function. We will have a closer look at this when we talk about function definitions
in Section 8.1.
25
Chapter 3. Statements, expressions and functions
26
Chapter 4. Communication with outside
The print statements can be followed by a variable number of values separated by commas. Without a value
print puts only a newline character on the standard output, generally the screen. If values are provided, they
are transformed into strings, and then are written in the given order, separated by a space character. The line is
terminated by a newline character. You can suppress the final newline character by adding a comma at the end of
the list. The following example illustrates all these possibilities:
#! /usr/local/bin/python
dna = "ATGCAGTGCATAAGTTGAGATTAGAGACCCGACAGTA"
print gc
27
Chapter 4. Communication with outside
Important
All data printed on the screen have to be character data. But values can have different types. Therefore
they have to be transformed into strings beforehand. This transformation is handled by the print
statement.
It is possible to control this transformation when a specific format is needed. In the examples above, the float
value of the gc calculation is written with lots of digits following the dot which are not very significant. The next
example shows a more reasonable output:
Figure 4.1 shows how to interpret the example above. The % (modulo) operator can be used to format strings. It
is preceded by the formatting template and followed by a comma separated list of values enclosed in parentheses.
These values replace the formatting place holders in the template string. A place holder starts with a % followed
by some modifiers and a character indicating the type of the value. There has to be the same number of values and
place holders.
28
Chapter 4. Communication with outside
% 3 . 1 f
letter indicating
indicates that the type of the
number of digits
a format total number value to format
following the
follows of digits dot
.
Table 4.1 provides the characters that you can use in the formatting template and Table 4.2 gives the modifiers of
the formatting character.
Important
Remember that the type of a formatting result is a string and no more the type of the input value.
29
Chapter 4. Communication with outside
4.3. Input
As you can print results on the screen, you can read data from the keyboard which is the standard input device.
Python provides the raw_input function for that, which is used as follows:
The prompt argument is optional and the input has to be terminated by a return.
Important
raw_input always returns a string, even if you entered a number. Therefore you have to convert
the string input by yourself into whatever you need. Table 4.3 gives an overview of all possible type
conversion function.
>>> nb
’12’
>>> type(nb)
<type ’str’>
30
Chapter 4. Communication with outside
>>> nb = int(nb)
>>> nb
12
>>> type(nb)
<type ’int’>
Notice that a user can enter whatever he wants. So, the input is probably not what you want, and the type
conversion can therefore fail. It is careful to test before converting input strings.
def read_number():
while 1:
nb = raw_input("Please enter a number:")
try:
nbconv = int(nb)
except:
print nb, "is not a number."
continue
else:
break
return nb
>>> read_number()
Please enter a number:toto
toto is not a number.
Please enter a number:12
’12’
31
Chapter 4. Communication with outside
4.4. Files
During a program execution data are stored in memory. But if the program ends the data are cleared from memory.
In this chapter we will explain how programs can communicate data permanently.
In the above sections we have seen how we can communicate interactively with programs during their execution.
However, if we need to store data permanently, we have to store them in files.
Files
Files are entities containing character data which are stored on external devices, such as disks or CD-ROMs.
Files are like books. You open them to start working, then read or write in them and you close them when you
have finished your work. However, you have always to know where you are in the book. As children use their
fingers when they start to learn reading, you manipulate a file pointer which indicates your current position in the
file.
f = open("myfile")
for l in f:
# do something with the line stored in variable l
f.close()
32
Chapter 4. Communication with outside
In order to write into a file, you must open it in ’w’ (write) mode.
f = open("myfile", ’w’)
f.write("hello, this is my new line of data")
f.close()
You might be surprised by the syntax shown above, where functions such as write and close are called with a
dot (’.’) put after the file pointer. This syntax and the related concept will be developed in Section 6.1.
f = open("myfileee")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: ’myfileee’
A common way to let your program handle this type of error properly is to specify what to do in case of errors:
try:
f = open("myfileee")
except IOError:
print "the file myfileee does not exist!!"
Handling this type of IO (input/output) error makes your program more robust.
33
Chapter 4. Communication with outside
34
Chapter 5. Program execution
cds = """atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaa
tttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtg
ctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggc
ccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatca
tcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacattt
attgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatac
gctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtg
ggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggag
gaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaac
gcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatg
tgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagttt
caggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcacta
tgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaac
cgttttatcgggcggggtaa""".replace("\n","")
print gc
Tip
You can name your file as you like. However, there is a convention for files containing python code to
have a py extension.
35
Chapter 5. Program execution
You can also make your file executable if you put the following line at the beginning of your file, indicating that
this file has to be executed with the Python interpreter:
#! /usr/local/bin/python
(Don’t forget to set the x execution bit under UNIX system.) Now you can execute your file:
caroline:~/python_cours> ./gc.py
0.54460093896713613
This will automatically call the Python interpreter and execute all the code in your file.
You can also load the code of a file in a interactive interpreter session with the -i option:
This will start the interpreter, execute all the code in your file and than give you a Python prompt to continue:
>>> cds
’atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaatttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaac
>>>cds="atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaatttcgggtgccgacctgacgcgcccgtt"
>>>cds
’atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaatttcgggtgccgacctgacgcgcccgtt’
>>>gc
0.54460093896713613
Important
It is important to remember that the Python interpreter executes code from top to bottom, this is also true
for code in a file. So, pay attention to define things before you use them.
36
Chapter 5. Program execution
Important
Notice that you have to ask explicitly for printing a result when you execute some code from a file, while
an interactive interpreter session the result of the execution of a statement is printed automatically. So to
view the result of the translate function in the code above, the print statement is necessary in the
file version, whereas during an interactive interpreter session we have never written it.
Interpreter
An Interpreter is a program that implements or simulates a virtual machine using the base set of instructions of
a programming language as its machine language.
You can also think of an Interpreter as a program that implements a library containing the implementation of the
basic instruction set of a programming language in machine language.
An Interpreter reads the statements of a program, analyzes them and then executes them on the virtual machine
by calling the corresponding instructions of the library.
Important
Pay attention, the READ - EVAL - PRINT loop is only entered in an interactive session. If you ask the
interpreter to execute code in a file, results of expression evaluations are not printed. You have to do this
by yourself.
Compiler
A Compiler is a program that translates code of a programming language in machine code, also called object
code. The object code can be executed directly on the machine where it was compiled.
37
Chapter 5. Program execution
source code
processor
Compiler
So using a compiler separates translation and execution of a program. In contrast of an interpreted program the
source code is translated only once.
The object code is machine-dependent meaning that the compiled program can only be executed on a machine
for which it has been compiled, whereas an interpreted program is not machine-dependent because the machine-
dependent part is in the interpreter itself.
Figure 5.2 illustrates another concept of program execution that tries to combine the advantage of more effective
execution of compiled code and the advantage of machine-independence of interpreted code. This concept is used
by the JAVA programming language for example and in a more subtle way by Python.
38
Chapter 5. Program execution
Interpreter
Compiler
processor
bytecode
In this case the source code is translated by a compiler in a sort of object code, also called byte code that is
then executed by an interpreter implementing a virtual machine using this byte code. The execution of the byte
code is faster than the interpretation of the source code, because the major part of the analysis and verification
of the source code is done during the compilation step. But the byte code is still machine-independent because
the machine-dependent part is implemented in the virtual machine. We will see later how this concept is used in
Python (Section 14.1).
39
Chapter 5. Program execution
40
Chapter 6. Strings
Chapter 6. Strings
So far we have seen a lot about strings. Before giving a summary about this data type, let us explain the object
syntax feature.
Object
Objects are things that know more than their values. In particular, you can ask them to perform specialized tasks
that only them can perform.
Up to now we have used some special functions handling string data available to us by the up to now magic
statement from string import *. But strings themselves know how to execute all of them and even more.
Look at this:
At the first glance this looks a little bit strange, but you can read the . (dot) operator as: “ask object motif to do
something” as: transform motif in an uppercase string (upper), ask whether it contains only letters (isalpha)
or count the number of “n” characters.
Objects as namespaces. How does it work? All objects have their own namespace containing all variable and
function names that are defined for that object. As already describeb in Section 2.4 you can see all names defined
for an object by using the dir function:
>>> dir(motif)
[’__add__’, ’__class__’, ’__contains__’, ’__delattr__’, ’__eq__’, ’__ge__’,
’__getattribute__’, ’__getitem__’, ’__getslice__’, ’__gt__’, ’__hash__’,
’__init__’, ’__le__’, ’__len__’, ’__lt__’, ’__mul__’, ’__ne__’, ’__new__’,
41
Chapter 6. Strings
The dot operator is used to access this namespace. It will look up in the namespace of the object for the name
following the dot operator.
>>> motif.__class__
<type ’str’>
>>> motif.replace(’a’, ’A’)
’gAAttc’
Variables and functions defined in object namespaces are called attributes and methods of the object.
Attribute
An attribute is a variable defined in a namespace of an object, which can only be accessed via the object himself.
Method
Methods are functions defined in a namespace of an object.
This is just a little introduction to objects making it possible to use the object syntax for the basic types in Python.
We will give further explanation into object-oriented programming in Chapter 18.
You can write them in Python using quotes, double quotes, triple quotes or triple double quotes. The triple quote
notation permits to write strings spanning multiple lines while keeping line feeds.
>>> ’ATGCA’
’ATGCA’
>>> "ATGCA"
’ATGCA’
>>> """ATGATA
... AGAGA"""
42
Chapter 6. Strings
’ATGATA\nAGAGA’
The first thing that we would sum up is how to extract characters or substrings . Characters are accessible using
their position in the string which has to be enclosed into brackets following the string. The position number can
be positive or negative, which means starting at the beginning or the end of the string. Substrings are extracted by
providing an interval of the start and end position separated by a colon. Both positions are optional which means
either to start at the beginning of the string or to extract the substring until the end. When accessing characters, it
is forbidden to access position that does not exist, whereas during substring extraction, the longest possible string
is extracted.
>>> motif[0:3]
’GAA’
>>> motif[1:3]
’AA’
>>> motif[:3]
’GAA’
>>> motif[3:]
’TTC’
>>> motif[3:6]
’TTC’
>>> motif[3:-1]
’TT’
>>> motif[-3:-1]
’TT’
>>> motif[:]
’GAATTC’
>>> motif[100]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: string index out of range
>>> motif[3:100]
’TTC’
>>> motif[3:2]
”
Caution
Figure 6.1 compares positive and negative indices. Be careful, forward string indices starts always with
0, whereas backward string indices starts with -1.
43
Chapter 6. Strings
motif =
G A A T T C
−6 −5 −4 −3 −2 −1
EcoRI[1:4] == EcoRI[−5:−2]
Caution
It is also important to notice that the character at the end position during a substring extraction is never
included in the extracted substring.
Warning
Strings are immutable in Python. This means you can neither change characters or substrings. You have
always to create a new copy of the string.
A list of all other methods, function and operators and their action on string objects are summarized in Table 6.1
and Table 6.2).
44
Chapter 6. Strings
45
Chapter 6. Strings
s < <=, >=, > t Checks if s appears before, before or at the same point,
after or at the same point, after than t in an alphabetically
sorted dictionary
s < <= t >=, > r Checks if r appears between s and t in an alphabetically
sorted dictionary
s ==, !=, is, not is t Checks the identity or difference of s and t
c in s, c not in s Checks if character c appears in s
s.endswith(suffix [,start [,end]]) Checks if s ends with suffix
s.isalnum() Checks whether all characters are alphanumeric
s.isalpha() Checks whether all characters are alphabetic
s.isdigit() Checks whether all characters are digits
s.islower() Checks whether all characters are lowercase
s.isspace() Checks whether all characters are whitespace
s.istitle() Checks whether s is title-case meaning all words are
capitalized
s.isupper()
s.startswith(prefix [, start [, Checks whether s starts with prefix between start and end
end]]))
6.1.1. Unicode
Characters are stored as numbers. The ASCII code is the default character encoding in computers (ASCII means
"American Standard Code for Information Interchange"). For instance, the ’a’ character is coded 97, and the ’b’
character is coded 98. You can check this under Python with the ord(). The chr() does the reverse. function:
>>> ord(’a’)
97
>>> ord(’b’)
98
>>> chr(122)
’z’
ASCII however is stored on a 7 bits only, and this is not enough to represent all the characters for several languages,
such as chinese. Unicode, which does not have this limitation, is a mapping between a numerical representation of
characters and characters for as many different non American standard character sets as possible. The purpose of
this paragraph is not to teach everything about Unicode, but just to save you time for the most common problems
you may encounter if you want to program in say, French, Arabic or Spanish. Figure 6.2 shows the general
mechanism: using Unicode, you can either decode a string from your character set (e.g: French) to Unicode,
or encode a Unicode string into a specific character set. This way, you can store and exchange your data into
46
Chapter 6. Strings
a universal code (Unicode), while being able to print and enter your strings in the appropriate language character
set.
encode()
decode()
The following program shows how to create a Unicode string. The next statement shows how to tell Python to
print this Unicode string into the ’latin-1’ character set.
Another way to create a Unicode string is by providing a literal, by using the prefix: u”.
The next important thing to know is how to tell your editor how to store and display non-ASCII characters. Under
the Emacs editor, you just have to add a comment line at the top of your source file (as the 1st or second line if
you put the interpreter line before):
#!/usr/local/bin/python
# -*- coding: latin-1 -*-
If you don’t put this line, Python may complain with an error such as:
47
Chapter 6. Strings
>>> r = ”
>>> for c in ’abcd’:
... r = c + r
...
>>> r
?
48
Chapter 7. Branching and Decisions
if condition : true
condition
block of statements executed
only if condition is true
if block false
The if statement has to be followed by a condition. Then a block is opened by a colon. This block contains the
sequence of statements that has to be executed if the condition is fulfilled. Figure 7.3 and Figure 7.2 highlight the
structural parts of the if statement.
49
Chapter 7. Branching and Decisions
>>> 1 < 0
False
>>> 1 > 0
True
>>> 1 == 0
False
>>> ’n’ in ’ATGCGTAnAGTA’
True
>>> ’A’ > ’C’
False
>>> ’AG’ < ’AA’
False
>>> ’AG’ != ’AC’
True
>>> len(’ATGACGA’) >= 10
False
Table 7.1 lists all boolean operators and their action on strings and numbers.
50
Chapter 7. Branching and Decisions
a
See (Section 10.2) for a detailed explanation.
Important
Do not confuse the assignment sign = with the logical operator ==. The second one is used to compare
two things and check their equality whereas the first one is used to bound values to variables. Python
does not accept an assignment as a condition but there are other programming languages that use the
same syntax for these two statements, but they do not warn when you use = instead of ==.
Caution
The or operation is not an exclusive or as it is sometimes used in the current languages. An or expression
is also true if both subexpressions are true.
51
Chapter 7. Branching and Decisions
An alternative sequence of statements, that will be executed if the condition is not fulfilled, can be specified with
the else statement.
Here the if and else are followed by a block containing the statements to execute depending on the truth value
of the condition. In this case exactly one of them is executed, which is illustrated in Figure 7.4
if condition:
true false
condition
block of statements executed
only if condition is true
else :
if block else block
block of statements executed
only if the condition is false
52
Chapter 7. Branching and Decisions
The elif statement is used to give an alternative condition. What happens when this is executed? The
conditions are evaluated from top to bottom. In our example with base = ’v’ as first condition, the if
condition base in ’ATGC’ is false, so the next condition, that of the elif statement is evaluated. base
in ’bdhkmnrsuvwxy’ is true, so the block of statements of this clause is executed and ambiguous
nucleotid is printed. Then the evaluation of the condition is stopped and the flow of execution continues
with the statement following the if-elif-else construction.
The else statement is optional. But it is more safe to catch the case where all of the given conditions are false.
53
Chapter 7. Branching and Decisions
54
Chapter 7. Branching and Decisions
Sometimes you can simplify nested conditions by constructing more complex conditions with boolean operators.
55
Chapter 7. Branching and Decisions
Caution
Even if the second version is easier to read, be careful and always check whether the complex condition
you have written, is what you really want. Such errors are called semantic error. They can not be detected
by the interpreter because the syntax of the program is correct, even if it is not necessarily what you want
to compute.
7.7. Solutions
Solution 7.1. Chained conditions
Exercise 7.1
56
Chapter 8. Defining Functions
This calculates the gc percentage of the specific DNA sequence cds, but we can use the same formula to calculate
the gc percentage of other DNA sequences. The only thing to do is to replace cds by the new DNA sequence in
the formula. But it is not very convenient to remember the formula and retype it all the time. It would be much
easier to type the following instead.
>>> gc(’ATGCAT’)
0.33333333333333331
>>> gc(cds)
0.54460093896713613
The only thing we have to remember is the name of the new function and its use.
Abstraction
The possibility to define such new function executing tasks specified by yourself, is an abstraction feature,
provided by all high level programming languages.
Important
It is also important to notice that functions have to be defined before they are called. You can not use
something that is not defined.
Let’s have a closer look at this definition. Figure 8.1 illustrates the structure of a function definition.
57
Chapter 8. Defining Functions
Basic instruction
Basic instructions are statements that define the language rules and the semantic of Python. They constitute the
basic set of instructions of Python. Each basic instruction has its own syntax that you have to learn in order to
master the programming language.
The return basic instruction is used to return the result of a function back, in our example the value of the GC
percentage of the specified DNA sequence.
The def basic instruction indicates to Python that a function definition follows. It has to be followed by the new
function name and a comma separated list of parameter names enclosed into parentheses.
Parameter
Parameters are variable names. When the function is called they are bound in the same order to the arguments
given.
The body of a function contains the piece of code needed to execute the subtask of the function. In the example
above, the body contains only the return statement. Here is a more complex example that excludes ambiguous
bases from the GC percentage calculation.
In this example the body of the function contains three instructions (2 assignments and the return statement). The
body of a function follows the definition line and is written as an indented block initiated by a colon.
58
Chapter 8. Defining Functions
Block
Blocks are structure elements of a program, that are used to group instructions. They are initiated by a colon and
are separated from the rest of the code by the same indentation step in Python.
In our example, the function body is written as a block to separate it from the rest of the program. The Python
interpreter used it to detect where the function body starts and ends.
Important
In other programming languages special words or characters are used to indicate the begin and the end of
blocks and the indentation is only used to make the code readable for humans. In Python the indentation
is used explicitly to do this. So in the above example of a function definition the end of the body is
detected when the next line starts without any indentation step. This is also illustrated in Figure 8.2.
indicate a block
59
Chapter 8. Defining Functions
Flow of execution. Some precision about the flow of execution. If you call a function the statements of the
function body are executed. The execution of the current sequence of statements is interrupted and the flow of
execution jumps to the sequence of statements named by the function. The function statements are executed and
then the flow of execution continues with the sequence from where the function was called.
Caution
The statements in the function body are only executed at a function call, so all errors in this piece of code
are only reported at the execution.
Now if we remember that within a function you can call other functions, the above scheme becomes rapidly
disturbing, but Python keeps a track from where a function is called and there is only one statement at a time that
is executed. You can visualize this using a stack diagram of the function calls.
Stack
A stack is an ordered set of things which can be accessed in two ways. You can put things on the top of the stack,
and only take back the thing on the top of the stack. A common example is a stack of plates (it is hard to get a
plate from within the stack). A stack is sometimes called a LIFO an abbreviation of last-in-first-out.
So what Python does is to handle a stack of function calls. If a function is called, Python puts the position from
where it is called on the stack, executes the function and when the function execution is finished Python gets the
position to continue back from the stack. Figure 8.3 shows the stack diagram of the execution of the gc function
(Example 8.1).
60
Chapter 8. Defining Functions
+ x 2 + x 2 + x 2
y y 2 y 2
gc seq ’ATGCNNATGC’ gc seq ’ATGCNNATGC’ gc seq ’ATGCNNATGC’ gc seq ’ATGCNNATGC’ gc seq ’ATGCNNATGC’ gc seq ’ATGCNNATGC’
nbases nbases nbases 2 nbases 2 nbases 2 nbases 2
gcpercent gcpercent gcpercent gcpercent gcpercent gcpercent
len s ’ATGCNNATGC’
− x − x 10
y 2 y 2
How does Python distinguish the two variables? It can do so, because they are not in the same namespace.
All functions have two associated namespaces, a local and a global namespace. The global namespace is the
namespace where the function is defined and the local namespace is a namespace created at the function definition
where parameters are defined. The local namespace contains also all local variables. Local variables are variables
that are bound to value in the function body. When a function is defined, its source code is analyzed and all used
names are sorted in the two namespaces.
If a variable name is searched during a function execution, Python looks in the local namespace for local variables
and in the global namespace for global variables. If a global variable is not found there, the builtin namespace is
searched. If it is not found there, an error is produced.
builtin namespace
The builtin namespace is a namespace that contains all predefined function of Python.
61
Chapter 8. Defining Functions
You can ask Python which names are defined in the global and the local namespace using the functions
globals() and locals().
>>> gc(seq)
local namespace: {’gcpercent’: 0.34999999999999998, ’nbases’: 3,
’seq’: ’ATGACGATAGGAGANNTATAGAN’}
global namespace: {’ascii_lowercase’: ’abcdefghijklmnopqrstuvwxyz’,
’upper’: <function upper at 0x1623f0>, ’punctuation’: ’"!#$%&\’()*+,-./:;<=>?@[\\]^_‘{|}~’,
’letters’: ’abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’,
’seq’: ’ATGACGATAGGAGANNTATAGAN’, ’lstrip’: <function lstrip at 0x160cf0>,
’uppercase’: ’ABCDEFGHIJKLMNOPQRSTUVWXYZ’,
’ascii_letters’: ’abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’,
’replace’: <function replace at 0x160b90>, ’capwords’: <function capwords at 0x160b10>,
62
Chapter 8. Defining Functions
The dir function shows all the names defined in the current namespace.
>>> dir()
[’__builtins__’, ’__doc__’, ’__name__’, ’ascii_letters’, ’ascii_lowercase’,
’ascii_uppercase’, ’atof’, ’atof_error’, ’atoi’, ’atoi_error’, ’atol’,
’atol_error’, ’capitalize’, ’capwords’, ’center’, ’count’, ’digits’,
’expandtabs’, ’find’, ’gc’, ’hexdigits’, ’index’, ’index_error’, ’join’,
’joinfields’, ’letters’, ’ljust’, ’lower’, ’lowercase’, ’lstrip’, ’maketrans’,
’octdigits’, ’printable’, ’punctuation’, ’replace’, ’rfind’, ’rindex’, ’rjust’,
’rstrip’, ’seq’, ’split’, ’splitfields’, ’strip’, ’swapcase’, ’translate’,
’upper’, ’uppercase’, ’whitespace’, ’zfill’]
Boolean function
Boolean functions are function that return truth values (in Python either False or True).
It is good style to give them a name that indicates its nature, often the function name starts with is.
63
Chapter 8. Defining Functions
Using this function makes your code easier to understand because the function name expresses what you want to
test:
>>> if isAminoAcid(oneaa):
... print ’ok’
... else:
print ’not a valid amino acid’
ok
64
Chapter 9. Collections
Chapter 9. Collections
9.1. Datatypes for collections
In a previous chapter we have seen that strings can be interpreted as collections of characters. But they are a
very restricted sort of collection, you can only put characters in a string and a string is always ordered. But we
need often to handle collections of all sorts of objects and sometimes these collections are not even homogeneous,
meaning that they may contain objects of different types. Python provides several predefined data types that
can manage such collections. The two most used structures are called Lists and Dictionaries. Both can handle
collections of different sorts of objects, but what are their differences?
List
Lists are mutable ordered collections of objects of different sorts. The objects are accessible using their position
in the ordered collection.
Dictionary
Dictionaries are mutable unordered collections which may contain objects of different sorts. The objects can be
accessed using a key.
Here are some examples comparing a list version of a collection of enzyme’s pattern and a dictionary version of
the same collection. Lists are created using a comma separated list of all elements enclosed into brackets, whereas
dictionaries are enclosed into braces and contain a comma separated list of key-value pairs, each separated by a
colon.
To access the elements we use the position in the list and the key for the dictionary.
Important
List indices have the same properties as string indices, in particular they start with 0 (remember Figure
6.1).
>>> patternList[0]
’gaattc’
>>> patternDict[’EcoRI’]
65
Chapter 9. Collections
’gaattc’
As for strings you can get the number of elements in the collection, as well as the smallest and the greatest element.
>>> len(patternList)
3
>>> len(patternDict)
3
Lists can be sliced but dictionaries cannot. Remember that dictionaries are unordered collections, so getting a
slice does not make any sense.
You can ask whether an element is in the collection. Dictionaries have two possibilities to perform this.
Unlike strings both collections are mutable. This means that you can remove, add or even change their elements.
66
Chapter 9. Collections
>>> patternDict
{’BamHI’: ”, ’EcoRI’: ’gaattc’, ’HindIII’: ’aagctt’}
>>> patternList[10]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: list index out of range
>>> patternDict[’ScaI’]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: ScaI
Figure 9.1 compares different actions on collections for strings, lists and dictionaries.
D[key]
Access to an element s[i] L[i]
key in D
Membership c in s e in L
D.has_key(D)
67
Chapter 9. Collections
Table 9.1 remembers the action of builtin functions and operators on list objects and Table 9.2 summarizes all
methods of list objects.
a
Important
shallow copy (see Section 10.2)
a
equal to the + operator but inplace
b
in place operation
68
Chapter 9. Collections
Important
It is important to know whether a function or method, that is applied to a mutable objects, modifies this
object internally or whether it returns a new object containing these modifications. Look at the following
example that shows two possibilities to concatenate lists. The + operator creates a new list whereas the
method extend adds one list to the other:
Important
It is important to notice that dictionary keys have to be unique and immutable.
69
Chapter 9. Collections
a
Important
shallow copy (see Section 10.2)
For list and dictionaries there are two important issues to consider:
2. Is it sufficient to access the elements by their position or do you prefer to access the collection with a more
complex, sometimes more descriptive, key rather than a number?
If you need an ordered collection you do not have any choice left, because only lists take care of the ordering.
However the way of accessing a collection is sometimes not so easy and often worth further consideration.
70
Chapter 10. Nested data structures
>>> len(l)
3
The first element is the integer 1, whereas the second and third element are lists themselves.
>>> l[0]
1
>>> l[1]
[2, 3, [4]]
>>> l[2]
[5, 6]
Figure 10.1 shows a representation of this list. Each element of a list is represented by a box containing a pointer
to the value of the element. Like pointers in a namespace representation, these pointers are references. Remember
that a reference is an address to a memory location (Section 2.4).
2 3 5 6
71
Chapter 10. Nested data structures
As far we have said that a list is an ordered collection of objects. But they do not really contain these objects.
They rather contain references to them.
How can we access to the elements of the internal list? Figure 10.1 makes clear there are different levels of the
nested structure. Each time we follow a reference we go to the next level of the structure.
To access the value 2 of our example list, we could store the value of the second element in a variable which
creates the new reference l1 to this sublist. And then ask for the first element of l1.
>>> l1 = l[1]
>>> l1
[2, 3, [4]]
>>> l1[0]
2
But we can also follow the references directly without creating a new variable.
>>> l[1][0]
2
Figure 10.2 colors the path of the references we have followed to access the value 2.
l1
2 3 5 6
72
Chapter 10. Nested data structures
Nested dictionaries. Dictionaries can be nested in the same way as lists. Here is an example of a dictionary that
stores restriction enzyme data.
>>> enzdict[’EcoRI’][’pattern’]
’GAATTC’
pattern GAATTC
EcoRI cut_position 1
BamHI
pattern GGATCC
cut_position 1
Mixed structures. In the same way as above you can construct mixed structures. Here is an example of a
restriction enzyme dictionary that stores, in addition to the pattern, all occurrences of the pattern in a sequence
stored in lists.
73
Chapter 10. Nested data structures
Python provides 2 different operators for assessing the equality of objects. The fisrt one: == generally tests that
the values are equal, whereas the second one tests whether the 2 objects are the same object, i.e whether they are
identical. In other words, the is operator looks if their memory locations are the same, whereas the == operator
rather does a content based comparison. Therefore for simple objects, such as integers, and immutable objects,
such as strings, the operators return the same result, but their behavior is different for composed objects such as
lists and dictionaries. The following listing shows some examples:
>>> 10.0 == 10
True
>>> 10.0 is 10
False
>>> 10 is 10
True
>>> 10.0 is 10.0
True
>>> L1 = [1, 2, 3]
>>> L1 == [1, 2, 3]
True
>>> L1 is [1, 2, 3]
False
>>> L2 = L1
>>> L1 is L2
True
>>> L3 = L1[:]
>>> L1 is L3
False
Figure 10.4 shows a representation to illustrate the examples of list comparisons done above.
74
Chapter 10. Nested data structures
L1
1 2 3 1 2 3
(the integers are the same as those of L1
L2 for lisibility reasons they are doubled here)
L3
Identity of objects. In Python all objects have an identifier that is unique for each object. Therefore two objects
are the same in the sense of is if they have the same identifier. You can ask for the identifier of an object in
Python using the id function:
>>> id(10)
1104200
>>> id(10.0)
1424356
>>> id("atg")
1183232
>>> id(start)
1183232
>>> id(L1)
1177744
>>> id(L2)
1177744
>>> id(L3)
1121328
>>> id([1, 2, 3])
1174048
Warning
In Python strings are immutable and handled as identical i.e the same in the sense of the is function if
they contain the same sequence of characters, but this is not the case in all programming languages. In C
for example, even strings with the same sequence of characters can have different memory locations.
75
Chapter 10. Nested data structures
>>> L3 = L1[:]
>>> L3 is L1
False
This is due to the fact that slicing a list creates a copy of a list and therefore L3 is only a copy of L1 but not the
same object in memory.
Figure 10.5 illustrates what happens. In fact slicing creates only a shallow copy of compound objects by creating
a new compound object and populating the new object with references to the members of the old list.
L1
1 4
2 3
L2
76
Chapter 10. Nested data structures
>>> L2[1][0] = 5
>>> L2
[1, [5, 3], 4]
>>> L1
[1, [5, 3], 4]
This example modifies the first element of the list that is the second element of L2. But the second element of L1
is the same in memory as the second element of L2. That is the reason why L1 is also modified, whereas if we
modify the first level of L1, L2 remains unmodified because it only changes independent references in the two
lists.
>>> L1
[1, [5, 3], 4]
>>> L2
[1, [5, 3], 4]
>>> L1[1] = 6
>>> L1
[1, 6, 4]
>>> L2
[1, [5, 3], 4]
Figure 10.6 highlights in red the first modification and in green the second one.
77
Chapter 10. Nested data structures
L1
1 4
5 2 3
L2
>>> a = [1, 2]
>>> b = a
>>> a is b
True
>>> c = [a, 3]
>>> c
[[1, 2], 3]
>>> c[0] is a
True
>>> a[1] = 0
>>> c
[[1, 0], 3]
>>> a
[1, 0]
>>> b
[1, 0]
>>> c[0] = 0
>>> c
[0, 3]
>>> a
[1, 0]
Independent copies. It is possible to get an independent copy via the deepcopy function defined in the copy
module.
78
Chapter 10. Nested data structures
79
Chapter 10. Nested data structures
80
Chapter 11. Repetitions
Because repetitions are common programming tasks, high level programming languages provide special structures
and statements for this purpose. Python has two of them: for and while. Let us see the for statement first.
b. end if
2. end for
81
Chapter 11. Repetitions
This sort of iteration is also known as a traversal, because the collection is traversed from left to right and a
particular task is applied to each element.
So in our example, the collection of elements is a string which we traverse with the for loop. We also have to
specify what a valid amino acid is, because computers do not know about it. Nevertheless we have already written
a function doing this (Example 8.2).
As def and if, the for statement is also a statement containing a block. The header line gives the two essential
information for the traversal. It starts with for followed by a variable name to which each element of the
collection is assigned during the execution of the block of statements. The second half of the header indicates the
collection in which the elements can be found. As in all functions, operations and expressions, the collection can
be specified by a value, a variable or even a composed expression.
Getting the position of invalid amino acids. As written above we cannot show the position of the invalid
amino acid in the protein sequence, because only the amino acid character is known in the body of the for loop.
Therefore, we have to iterate over the indices of the sequence. Here is the algorithm:
82
Chapter 11. Repetitions
b. end if
2. end for
What are the possible values of the positions? Remember the string indices, they start at 0 and end at one before
the length of the string. Python has a function that gives the possibility to create a list of numbers that is called
range. Using this function, we can translate the algorithms above into Python:
Note
There is a convention in most programming languages to use i, k, l as variable names for the
iteration variable.
Translate cds sequences. Let’s take another example. Say that we have a cds sequence and that we would like to
know such things as the corresponding amino acid sequence or the codon usage. How could we achieve this? For
sure, it looks like a traversal of the cds sequence, but the things we are looking for do not correspond anymore to
one nucleotide of the cds sequence but to a codon which is a three letter substring. If we could access the codons
one by one, the translation into the amino acid sequence would look like this:
• add the amino acid corresponding to the current codon to the protein sequence
In the body of the loop we need to establish the correspondence between a codon sequence and the amino acid.
We know how to do this by hand, by looking up a codon in the codon translation table. A dictionary is the data
type that gives us such sort of access to the collection of codons.
>>> code = { ’ttt’: ’F’, ’tct’: ’S’, ’tat’: ’Y’, ’tgt’: ’C’,
... ’ttc’: ’F’, ’tcc’: ’S’, ’tac’: ’Y’, ’tgc’: ’C’,
... ’tta’: ’L’, ’tca’: ’S’, ’taa’: ’*’, ’tga’: ’*’,
... ’ttg’: ’L’, ’tcg’: ’S’, ’tag’: ’*’, ’tgg’: ’W’,
... ’ctt’: ’L’, ’cct’: ’P’, ’cat’: ’H’, ’cgt’: ’R’,
... ’ctc’: ’L’, ’ccc’: ’P’, ’cac’: ’H’, ’cgc’: ’R’,
... ’cta’: ’L’, ’cca’: ’P’, ’caa’: ’Q’, ’cga’: ’R’,
... ’ctg’: ’L’, ’ccg’: ’P’, ’cag’: ’Q’, ’cgg’: ’R’,
... ’att’: ’I’, ’act’: ’T’, ’aat’: ’N’, ’agt’: ’S’,
... ’atc’: ’I’, ’acc’: ’T’, ’aac’: ’N’, ’agc’: ’S’,
... ’ata’: ’I’, ’aca’: ’T’, ’aaa’: ’K’, ’aga’: ’R’,
... ’atg’: ’M’, ’acg’: ’T’, ’aag’: ’K’, ’agg’: ’R’,
... ’gtt’: ’V’, ’gct’: ’A’, ’gat’: ’D’, ’ggt’: ’G’,
83
Chapter 11. Repetitions
Now let us go back to the first part of the problem: getting the codons. If we know where a codon starts, we only
have to extract the substring of length 3 starting from this position. Here is the algorithm:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(4,10)
[4, 5, 6, 7, 8, 9]
>>> range(0,10,3)
[0, 3, 6, 9]
84
Chapter 11. Repetitions
caa
taa
taa
If we replace the print statement with the access to the dictionary of translation, we can translate the codon into
the amino acid.
Example 11.1. Translate a cds sequence into its corresponding protein sequence
What about the computing of the codon usage? We do not need the translation table anymore. But we have to
count each codon now. We also need a data structure accessible by codons, although it is not to get information,
but to store the result. Here is the algorithm: do not forget that accessing a key which does not exist in a dictionary,
is not allowed.
b. otherwise:
85
Chapter 11. Repetitions
while condition :
block of statements executed
true
as long as condition is true while block condition
false
86
Chapter 11. Repetitions
Before saying more, let us illustrate that with an example. We have already shown how to use the find method of
strings to find pattern (Section 1.1). Let’s try now to find all occurrences of a specific pattern in a DNA sequence.
Here is one possible way to proceed:
OUTPUT: a list of positions matches containing all start positions of pat in seq
4. return matches
In Python a while loop is written using the while statement which is as the for and the if statements a
block statement. The header of the block statement contains the conditional expression and the body contains the
sequence of statements to repeat (Figure 11.3).
87
Chapter 11. Repetitions
Look at the following example. Do you know what happened during the execution?
while 1:
print "hello"
It simply never stops, because 1 is always true and therefore it is neither an algorithm nor a program. But it
illustrates an important feature of a while loop. The conditional expression has to contain at least one variable
whose value changes during the execution of the sequence of statements.
This variable is often a simple iterator, which is increased or decreased until it reaches a limit. The behavior of the
current_match variable in Example 11.2 is more complicated, because it represents two situations. Its value
is the start position of a pattern occurrence or it is -1 if there are no more occurrences. In this example the second
argument of the find function ensures that the next occurrence is found but no occurrence missed.
Because the iteration variable is used in the condition and the condition is evaluated first when a while loop
is entered, it has to be defined before entering the loop. This means there are two different affectations of this
variable. A first affectation or initialization before the loop is entered and a second affectation statement in the
repeated sequence of statements that gives it the next value. In general, this is the last statement of the sequence.
Even if it is not always the case, and sometimes not necessary, it is a convention to put it at the end if possible
because it is easy to find if it has been forgotten.
Warning
Conditions of while loops are frequent source of errors. They are sometimes difficult to detect because
they are semantic errors that produce wrong results on all or sometimes only special input data sets. You
have to ensure that the iteration variable changes and that the end condition is always reached.
The following example illustrates a common use of the while construct to get input from an interactive user:
en2fr = {
88
Chapter 11. Repetitions
’comma’: ’virgule’,
’hyphen’: ’tiret’,
’colon’: ’deux-points’
}
x = raw_input("Enter an English word (hit return to finish): ")
while x != "":
if en2fr.has_key(x):
print en2fr[x]
else:
print x, "does not exist"
x = raw_input("Enter an English word (hit return to finish): ")
This loop uses what is called a sentinel, a special value to indicate the end of the input. The sentinel has to be
chosen in order to be a value that does not belong to the collection.
One first basic rule is that you should use the for loop when the number of iterations is known. For instance:
• The loop iterates over a range of values which is known in advance at execution time.
• The object provides a way to iterate over its elements with a for statements (e.g files).
The while loop is the more general one because its loop condition is more flexible and can be more complicated
than that of a for loop. The condition of a for loop is always the same and implicit in the construction. A for
loop stops if there are no more elements in the collection to treat. For simple traversals or iterations over index
ranges it is a good advice to use the for statement because it handles the iteration variable for you, so it is more
secure than while where you have to handle the end of the iteration and the change of the iteration variable by
yourself.
The while loop can take every boolean expression as condition and permits therefore more complex end
conditions. It is also the better choice if the iteration variable does not change evently by the same step or if
there are more than one iteration variable. Even if you can handle more than one iteration variable in a for
statement, the collections from which to choose the values must have the same number of elements.
89
Chapter 11. Repetitions
Sometimes you need to iterate over a great interval of integers. In this case the list generated by the range
function would be long and therefore take a lot of memory space and time to be constructed. In such cases it is
more appropriated to use the xrange function which is used in the same way as the range function. But rather
than to create the list of integers physically in memory, it creates an object that calculates the new value if it is
asked for the next element in the list.
Passing functions as arguments. This is the first example where we pass a function as an argument. Even if this
looks strange at the beginning, function names are references as variable names which are bound to the sequences
of statements defined in the body of the function definition. The sequence of statements can be viewed as an object
which has the particularity that it can be called with arguments. Figure 11.4 shows what happens when passing a
function as argument. This illustrates that there is no difference between variables and functions as arguments.
90
Chapter 11. Repetitions
global namespace
Another example that handles function names as references is renaming a function as follows:
The only difference between function and variable names is that function names are bound to objects that can be
called using the function call syntax.
>>> isAminoAcid
<function isAminoAcid at 0x111ad0>
>>> isAminoAcid(’a’)
True
Without the parenthesis the object bound to the name isAminoAcid is shown by the interpreter, whereas adding
the parentheses will call the function object bound to isAminoAcid.
Rewriting map with for. map with a function that takes only one argument, can be rewritten as follows..
91
Chapter 11. Repetitions
3. return res
l = []
for x in [1,2,3]:
l.append(x*x)
You can create any type of elements of the new list, e.g sub-lists, strings, booleans:
92
Chapter 11. Repetitions
l = []
for x in [0,1,2]:
for y in [0,1,2]:
l.append(x+y)
93
Chapter 11. Repetitions
94
Chapter 12. Exceptions
>>> f = open(’my_fil’)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: ’my_fil’
try:
f = open(’my_fil’)
except IOError, e:
print e
You could use this exception mechanism to prompt the user in order to get a proper filename.
import sys
filename = sys.argv[1]
max_tries = 3
tries = 0
95
Chapter 12. Exceptions
tries = tries + 1
if tries < max_tries:
filename = raw_input("Enter a filename (%d tries left): " % (max_tries - tries))
if tries == max_tries:
print "The program needs an existing filename, sorry."
sys.exit(-1)
f.close()
If you need to protect your code from more than one error, you can specify several exceptions:
try:
y = x / n
except ZeroDivisionError, e:
print "n is 0", e
except OverflowError, e:
print "%4.2f / %4.2f raises an overflow!" % (x,n), e
96
Chapter 12. Exceptions
SyntaxError TabError
IndentationError
SystemError
TypeError
ValueError UnicodeError
• AttributeError: when you attempt to access a non-existing attribute (method or variable) of an object.
• IndexError, KeyError: occurs when attempting to access either an out-of-range index in a list or a
non-existing key in a dictionary
97
Chapter 12. Exceptions
if something_wrong:
raise Exception
if something_wrong:
raise Exception, " something went wrong"
return True
Go to
Since exceptions are defined as classes and by inheritance, you will need some knowledge about classes
in order to fully understand this section (see Chapter 18).
Example 12.4. Raising your own exception in case of a wrong DNA character
In the following code, you define an exception AlphabetError that can be used when the sequence passed to
the function does not correspond to the alphabet.
class AlphabetError(ValueError): ❶
pass
98
Chapter 12. Exceptions
return 1
99
Chapter 12. Exceptions
100
Chapter 13. Functions II
seq = "..."
BamHI = ’GGATCC’
print isunique(seq, BamHI)
This is called passing arguments by position. It is also possible to pass arguments by name:
You can mix either style, but named argument must be put after positioned arguments.
101
Chapter 13. Functions II
if c not in alphabet:
return False
return True
To use the DNA alphabet as default value for the alphabet parameters, the checkseq function can be
redefined as follow:
alphabet = "actg"
def checkseq(seq, alphabet=alphabet):
for c in seq:
if c not in alphabet:
return False
return True
If you change the global alphabet variable later, this will not be taken into account, except if the default value is a
mutable type of course (e.g: list or dictionary).
alphabet = "actg"
def checkseq(seq, alphabet=alphabet):
for c in seq:
if c not in alphabet:
return False
return True
alphabet = "acug"
>>> result = checkseq("atgcgtgatgdtgragt")
True
102
Chapter 13. Functions II
To illustrate how you could use this feature, look at the following function, which generates all possible digests
with 2 enzymes:
def all_2_digests(enzymes):
digests = []
for i in range(len(enzymes)):
for k in range(i+1, len(enzymes)):
digests.append( [enzymes[i], enzymes[k]] )
return digests
This function takes a list. We can transform this function such that it can be applied as follow:
instead of:
def all_2_digests(*enzymes):
digests = []
for i in range(len(enzymes)):
for k in range(i+1, len(enzymes)):
digests.append( [enzymes[i], enzymes[k]] )
103
Chapter 13. Functions II
return digests
Optional variables can also by passed as keywords, if the last parameter is preceded by **. In this case, the
optional variables are available within the function as a dictionary.
>>> hello("kate")
Hello kate
>>> hello("kate", duration=10)
Hello kate
Enjoy your 10 days vacation!!
>>> hello("kate", duration=10, origin="France")
Hello kate
We hope you had a nice flight from France
Enjoy your 10 days vacation!!
>>> enz = []
>>> add_enz(’EcoRI’)
enz: []
new: (’EcoRI’,)
104
Chapter 13. Functions II
>>> enz
[ ’EcoRI’ ]
By default, variables are defined in the local namespace. In order to access a global variable, there is nothing to
do (see Example 13.2 above). In order to define a global variable within a function (i.e to assign a value to it), it
has to be declared explicitly as global variable, using the global statement.
105
Chapter 13. Functions II
else: # x<y
return -1
106
Chapter 14. Modules and packages in Python
#!/local/bin/python
import sys
Say that you stored in a modexa.py file, and that you run it like this:
./modexa.py 1 a seq.fasta
Explanation: By using the argv variable defined in the sys module, you can thus access to the values provided
on the command line when launching the program. As shown in this example, the access to this information is
made possible by:
• importing the module through the import statement, which provides access to the module’s definitions
• using the argv variable defined in the module by a qualified name: sys.argv.
107
Chapter 14. Modules and packages in Python
In this case, you only import one definition (the argv variable) from the sys module. The other definitions are
not loaded.
# file Valseq.py
def find_valid_key(e):
for key,value in valid_sequence_dict.items():
if value == e:
return key
import ValSeq
where ValSeq is the module name. You can then access to its definitions, which may be variables, functions,
classes, etc...:
108
Chapter 14. Modules and packages in Python
• in a path, i.e a colon (’:’) separated list of file paths, stored in the environment variable PYTHONPATH. You
can check this path through the sys.path variable.
• Python files, suffixed by .py (when loaded for the first time, compiled version of the file is stored in the
corresponding .pyc file),
• defined as C extensions,
current namespace
...
sys
argv
path
...
109
Chapter 14. Modules and packages in Python
open
A module is loaded only once, i.e, a second import statement will neither re-execute the code inside the module
(see Python reload statement in the reference guides), nor will it re-create the corresponding namespace.
the other components stay hidden. As illustrated in Figure 14.2, no new namespace is created, the imported
definition is just added in the current name space.
ValSeq find_valid_key
valid_sequence_dict
find_valid_key
This can causes errors if the definition that is imported needs to access to other definitions of the module, e.g:
You can also load "all" the components from a module, which makes them available directly into your code:
110
Chapter 14. Modules and packages in Python
You probably did this many times in order to use the string module’s definitions, right? The result of:
is that all the definitions of the module are copied in your current namespace.
Caution
Be aware of potential names collision: for instance, if you current namespace contains a definition of a
variable called, say: count, it will be destroyed and overloaded by the string module’s definition of
the count function.
Caution
You can restrict the components being imported by an import * statement. The __all__ variable,
also used for packages (Section 14.2), can explicitly list the components that should be directly accessible
(see Exercise 14.4).
% python ValSeq.py
the module does not behaves like a module anymore. It is, instead, run within the default __main__ module (i.e
not the ValSeq module):
% python -i ValSeq.py
>>> ValSeq.find_valid_key("linear RNA")
NameError: name ’ValSeq’ is not defined
>>> find_valid_key("linear RNA")
’DL’
For this reason, the code executed when the module is loaded (e.g: either with import or from the command line)
can be made dependent of its current name by testing this name. The current module’s name is stored in a special
purpose variable __name__:
if __name__ == ’__main__’:
# statements that you want to be executed only when the
# module is executed from the command line
# (not when importing the code by an import statement)
print find_valid_key("linear RNA")
111
Chapter 14. Modules and packages in Python
14.2. Packages
A package is a set of modules or sub-packages. A package is actually a directory containing either .py files or
sub-directories defining other packages.
The dot (.) operator is used to describe a hierarchy of packages and modules. For instance, the module
Bio.WWW.ExPASy is located in the file PYTHONHOME/site-packages/Bio/WWW/ExPASy.py. This
module belongs to the Bio.WWW package located into the PYTHONHOME/site-packages/Bio/WWW/
directory.
14.2.1. Loading
When loading a package, the __init__.py file is executed. If the __init__.py defines classes, functions,
etc... they become available at once, as shown in the following example:
However, loading a package does not automatically load the inner modules. For instance, even though the
Bio.Fasta package directory contains the following files:
% ls Bio/Fasta
FastaAlign.py FastaAlign.pyc __init__.py __init__.pyc
this does not imply that importing the Bio.Fasta package loads the Bio.Fasta.FastaAlign module:
Issuing:
will however load the Bio.Fasta.FastaAlign, because this module is mentioned in the __all__ attribute
in the Bio/Fasta/__init__.py file:
112
Chapter 14. Modules and packages in Python
__all__ = [
’FastaAlign’,
]
• __name__
• __path__
• __file__
expasy = ExPASy.get_sprot_raw(’CERU_HUMAN’)
sp = SProt.Iterator(expasy, SProt.RecordParser())
record = sp.next()
print record.keywords
???
???
113
Chapter 14. Modules and packages in Python
a=ClustalAlignment()
NameError: name ’ClustalAlignment’ is not defined
???
>>> help("modules")
To search through the documentation of modules for a specific word, for instance "SProt", also use the help
command like this:
Here is a list of matching modules. Enter any module name to get more help.
Bio.SwissProt.KeyWList - KeyWList.py
Bio.SwissProt.SProt - SProt.py
Bio.SwissProt (package)
Let’ start by listing the operations that we need to construct the tree and idividuals.
114
Chapter 14. Modules and packages in Python
• Create person: this will produces a data structure containing the information related to an individual: his/her
name, gender, spouse, parents and children.
• Register a marriage: after checking that the persons are not already married, this will update the information
about their respective spouse.
• Register a birth: this will create the child as a person and update information in the father’s and mother’s data
structures about their children.
• Create a family: this creates a list of persons together with their descendants.
• Test whether two persons are identical. Our module is illustrative and basic, so we will define the identity by
the fact that two persons have the same name.
• Display the data related to a person (name, gender, spouse, parents and children).
We can start to build our module. We do not need to write code; we will just define the functions and their
parameters, and add a docstring to these definitions. Each definition will just contain a single pass statement in
order to have an executable file, that can be used for instance by the pydoc to display the whole documentation of
the module. A header docstring will contain the general description of the module.
"""
"""
"""
Returns: a dictionary (keys: ’name’, ’gender’, ’spouse’, ’father’, ’mother’, ’children’).
"""
pass
115
Chapter 14. Modules and packages in Python
"""
If one of the person is already married, raises an error.
Updates information of each spouse.
"""
pass
def grand_fathers(person):
"""
Returns the list of grand fathers.
"""
pass
def grand_mothers(person):
"""
Return the list of grand mothers.
"""
pass
def sibchip(person):
"""
Returns the list of brothers and sisters.
"""
pass
116
Chapter 14. Modules and packages in Python
def name_list(family):
"""
Returns the names of the family members as a list.
"""
def display(person):
"""
Returns a printable character strings describing the person (name,
gender, spouse, parents and children).
"""
def display_family(family):
"""
Returns a printable character strings describig the persons
belonging to the family.
"""
l_children = children[:]
return {’name’: name, ’children’: l_children, ’father’: father, ’mother’: mother, ’spouse’
117
Chapter 14. Modules and packages in Python
def parents(person):
return [person[’father’], person[’mother’]]
def grand_fathers(person):
l = []
father = person[’father’]
mother = person[’mother’]
if father is not None:
if father[’father’] is not None:
l.append(father[’father’])
if mother is not None:
if mother[’father’] is not None:
l.append(mother[’father’])
return l
def grand_mothers(person):
l = []
if person[’father’] is not None:
if person[’father’][’mother’] is not None:
l.append(person[’father’][’mother’])
if person[’mother’] is not None:
if person[’mother’][’mother’] is not None:
l.append(person[’mother’][’mother’])
return l
def sibchip(person):
l = []
father = person[’father’]
mother = person[’mother’]
if father is not None:
for child in father[’children’]:
l.append(child)
if mother is not None:
for child in mother[’children’]:
if l.count(child) == 0:
l.append(child)
return l
118
Chapter 14. Modules and packages in Python
if identity(person, member):
return True
return False
def list_names(family):
return [member[’name’] for member in family]
def display(person):
if person is None:
return ""
s = "Name: " + person[’name’]
if person[’spouse’] is not None:
s = s + "\n\t" + "spouse: " + person[’spouse’][’name’]
if person[’father’] is not None:
s = s + "\n\t" + "father: " + person[’father’][’name’]
if person[’mother’] is not None:
s = s + "\n\t" + "father: " + person[’mother’][’name’]
if len(person[’children’]) > 0:
s = s + "\n\t" + "children: "
for e in person[’children’]:
s = s + e[’name’] + " "
return s
def display_family(family):
s = ""
done = {}
for member in family:
if not done.has_key(member[’name’]):
s = s + display(member) + "\n"
done[member[’name’]] = True
return s
if __name__ == "__main__":
print "----------- test code --------------"
henri = create_person("Henri", "male")
marianne = create_person("Marianne", "female")
register_marriage(henri, marianne)
jeanne = register_birth(henri, marianne, "Jeanne", "female")
print display(jeanne)
oscar = create_person("Oscar", "male")
try:
register_marriage(oscar, marianne)
except Exception, e:
print "\n---- marriage is not possible: " + str(e) + " ----"
119
Chapter 14. Modules and packages in Python
120
Chapter 15. Recursive functions
Translate a cds sequence into its corresponding protein. Let’s start with the example of the cds translation.
In Chapter 11 we have already written functions that solve this task using either for (Example 11.1) or while
(???). Let’s remind the pseudocode of the examples:
3. return prot
• add the corresponding amino acid of the current codon to sequence prot using genetic code code
3. return prot
• the concatenation of the first codon of the sequence and the translation of the cds sequence without this codon
or in more mathematical terms:
121
Chapter 15. Recursive functions
Recursive function
A recursive function is a function that use itself during the calculation procedure.
Recursive definition
A recursive definition of a function is a definition that uses the function itself in the definition.
It is important to notice that we need a terminal condition otherwise the recursion would never stop. In our case
the recursion stops if there are no more codons to translate:
Therefore the pseudocode can be written as follow without using loop structures:
1. if cds is empty:
2. otherwise:
122
Chapter 15. Recursive functions
Writing recursive functions is sometimes difficult, because we doubt that we have defined a function executing the
specific task, when we call the it . Therefore it is useful to remember this fact during the development of recursive
functions.
Stack diagram of recursive functions. Let’s have a deeper look at the flow of execution of recursive functions.
Figure 15.1 shows the stack diagram of function calls.
123
Chapter 15. Recursive functions
cds ""
rectranslate
code return ""
cds "ggt"
rectranslate
code return
"G"
cds "gctggt"
return "AG"
rectranslate
code { ’ttt’: ’F’ , ..., ’ggg’: ’G’ }
cds "attgctggt"
rectranslate
code return
"IAG"
cds "atgattgctggt"
rectranslate
return "MIAG"
code
Figure 15.1 shows that the translate function is called with a cds sequence of decreasing length and the local
function namespaces are piled up until the terminal condition is reached.
124
Chapter 15. Recursive functions
Important
Recursive function calls can take a lot of memory space because for each recursive call the local
namespace for the given function call has to be stored. It is important to pay attention on this fact
because memory space is a limited resource on a computer.
def fact(n):
if n == 0 or n == 1:
return 1
else:
return n * fact(n - 1)
Figure 15.2 show the stack of the recursive calls with pending return values (you can fill the missing part of these
values from top to bottom to follow the successive computations).
Figure 15.2. Stack diagram of recursive function calls for function fact()
fact(5)
n 1 return 1
fact
n 2 return 2 * ______
fact
n 3 return 3 * ______
fact
n 4 return 4 * ______
fact
n 5 return 5 * ______
fact
125
Chapter 15. Recursive functions
Solution 15.1
Nested list
A nested list is an ordered collection of elements that are:
• either items
• or nested lists
The items define the content, whereas the lists define the structure of the collection.
How can we use this? Let’s use phylogenetic trees as examples for such a recursive list structure. Figure 15.3
shows an example of a phylogenetic tree topology that can be implemented in Python as follows:
126
Chapter 15. Recursive functions
• tree_traversal(element)
127
Chapter 15. Recursive functions
3. otherwise:
• *** end condition of the recursion, here the leaves of the tree ***
Doing something when you enter the branching point, is also known as preorder traversal of a tree, whereas doing
something when leaving a branching point is also called postorder traversal of a tree.
We will give two example applications of this strategy for our tree. The first one prints all species of the trees:
import types
def print_species(tree):
if type(tree) is types.ListType:
for child in tree:
print_species(child)
else:
print tree
>>> print_species(tree)
human
chimpanzee
gorilla
orang-utan
gibbon
and the second one that prints for a tree or a binary tree 2 the two populations that are split at this point.
import types
def splits(tree):
if type(tree) is types.ListType:
all_leaves = []
for child in tree:
child_leaves = splits(child)
print child_leaves,
all_leaves += child_leaves
print
return all_leaves
A binary tree is a tree with maximal two subtrees for each branching point.
128
Chapter 15. Recursive functions
else:
return [ tree ]
def binary_splits(tree):
if type(tree) is types.ListType:
left = binary_splits(tree[0])
right = binary_splits(tree[1])
print left, right
return left + right
else:
return [ tree ]
>>> splits(tree)
[’human’] [’chimpanzee’]
[’human’, ’chimpanzee’] [’gorilla’]
[’human’, ’chimpanzee’, ’gorilla’] [’orang-utan’]
[’human’, ’chimpanzee’, ’gorilla’, ’orang-utan’] [’gibbon’]
[’human’, ’chimpanzee’, ’gorilla’, ’orang-utan’, ’gibbon’]
Finally, we will show the code that separates the work to do from the tree traversal itself by functions. The
traversal function tree_traversal does not change for the task. The user has only to redefine the functions
do_prework, do_postwork and do_leafwork.
import types
def do_prework(node):
print "prework:", node
def do_postwork(node):
print "postwork:", node
def is_leaf(node):
return type(node) is not types.ListType
def do_leafwork(leaf):
print "that is a leaf:", leaf
if not is_leaf(node):
for child in node:
tree_traversal(child)
else:
do_leafwork(node)
129
Chapter 15. Recursive functions
do_postwork(node)
15.4. Solutions
Solution 15.1. Recursive reverse
Exercise 15.1
def recreverse(s):
if s == "":
return s
else:
return recreverse(s[1:]) + s[0]
130
Chapter 16. Files
File data are ordered collections, such as lists. But you have to traverse all elements to get to a position, whereas
lists can by accessed directly using an index.
Opening. When you open a file in Python using the built-in function open, you have to indicate, in addition to
its name, whether you want to read from it or write in it. The default is reading.
Working. The file object created by the open function has methods to read from the file, to write in it and to move
the file pointer to another position.
Although Python can handle files containing binary data as well as text data, file objects have special functions to
work with text files, such as reading line per line.
At this point we like to show two possibilities to handle text file data line per line. The first one uses the loop
while:
infile="<some-file-name">
infh=open(infile)
line=infh.readline()
while line:
#do something with the line
line=infh.readline()
infh.close()
infh.close()
There exists two file methods, readlines and xreadlines, with the same properties as range and xrange.
The first one reads all the lines of a file and puts them in a list, whereas the second one allows to iterate over all
lines without creating the list. The first one is appropriated for small files, but if you are not sure about the number
of lines in your file, the second one prevents to overload the memory.
131
Chapter 16. Files
Table 16.1 gives a list of all common used file methods and Table 16.2 explains all possible modes to work with
files.
a
See Section 11.5 for more informations
Closing. Although Python closes all opened files when the interpreter exits, it is a good idea to close them
explicitly. Why ???
Important
Pay attention, while open is a built in function creating a file object, close is a method of the created
file object.
132
Chapter 16. Files
Let us work on a concrete example. In Section 11.3, we have written the findpos function that finds all
occurrences of a pattern in a sequence. In the following example, we will show how we can read restriction
site patterns of enzymes described in the ReBase [http://www.rebase.org] database.
ReBase database format. In our example we will use an excerpt of the gcgenz.dat file of the restriction
enzyme ReBase database. The information for each restriction enzyme is stored in one line of this file. The
information about isoschizomers and the header of the file containing explanations have been omitted in the
excerpt that we will use. Figure 16.1 shows a part of this file. Each line starts with the enzyme name, followed by
the number of bases after which it cuts the restriction site pattern, the sequence of the restriction site, the length of
the overhang and some comments about the isoschizomeres and the availability of the enzyme.
Using the general scheme of reading lines from files shown above (Example 16.1), the following shows one
possibility for extracting the restriction site pattern from the line.
OUTPUT: a dictionary enz_dict containing all restriction patterns indexed by their name
4. close infh
5. return enz_dict
133
Chapter 16. Files
def get_site_only(pat):
newpat = ""
for c in pat:
if c.isalpha():
newpat +=c
return newpat
def read_rebase(filename):
enz_dict={}
infh= open(filename)
for line in infh.xreadlines():
fields = line.split()
name = fields[0]
pat = fields[2]
enz_dict[name] = get_site_only(pat)
infh.close()
return enz_dict
print read_rebase("../data/rebase.dat")
INPUT: a dictionary enz_dict containing all restriction site patterns accessible by enzyme name, a sequence seq
to search for
OUTPUT: list of start position of every occurrence for each pattern in the dictionary.
134
Chapter 16. Files
The print_matches in the following listing of functions prints the results of the analysis on the screen.
seq = ""
def isexact(pat):
for c in pat.upper():
if c not in ’ATGC’:
return 0
return 1
In order to store the results permanently, we will see now how we can write the information in a file. As for
reading, we have to open a file although now in a writing mode, write our results in the file and then close it.
135
Chapter 16. Files
The problem with this print_matches function is that it prints only the result of last enzyme. Because if we
close the file after writing the information, the next time we will open the file for writing the next result, we will
overwrite the old result. We have two possibilities to solve this. First, we can open the file to append at the end
of the file. Or second, we can open the file for writing in the main stream of the program and then pass the file
object as argument to print_matches, and close the file only when all results have been written. We prefer
the second solution.
seq = ""
def isexact(pat):
for c in pat.upper():
if c not in ’ATGC’:
return 0
return 1
Although it is possible to use the write or writelines methods of the file object, we have shown in the above
example how to pass a file object to the print statement. Which syntax you will use in your own code, is a
question of taste. But the code could be difficult to read if you mix them.
136
Chapter 16. Files
Here is the complete program to find all occurrences of restriction sites from a set of enzymes in a sequence. The
enzyme data are read from a file and the results are also stored in a file.
def isexact(pat):
for c in pat.upper():
if c not in ’ATGC’:
return 0
return 1
def get_site_only(pat):
newpat = ""
for c in pat:
if c.isalpha():
newpat += c
return newpat
def read_rebase(filename):
enz_dict = {}
infh = open(filename)
for line in infh.xreadlines():
fields = line.split()
name = fields[0]
pat = fields[2]
enz_dict[name] = get_site_only(pat)
infh.close()
return enz_dict
137
Chapter 16. Files
return matches
seq = """atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaa
tttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtg
ctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggc
ccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatca
tcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacattt
attgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatac
gctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtg
ggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggag
gaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaac
gcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatg
tgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagttt
caggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcacta
tgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaac
cgttttatcgggcggggtaa""".replace("\n","").upper()
enzdict = read_rebase("../data/rebase.dat")
We have written this program step by step by broadening the problem at each step, but we have never looked at
the global design of the program. Therefore we have used two strategies while reading and writing. In the reading
step all data are read at once, whereas the results are written each time we got them.
In biological problems it is often necessary to handle lots of data and this can lead to problems if you try to handle
all of them in memory, as we have done with the enzyme data. Sometimes we need all the data to solve a problem,
but often we can treat them each at a time. If the dataset is small, the choice of the strategy does not change a lot,
but if you handle a large amount of data, it is worth asking whether you really need it all in memory?
In our restriction problem it is not really necessary to store all the enzyme data in the memory. It is possible to
treat enzyme by enzyme with one loop that reads the enzyme data, processes the sequence and writes the result.
138
Chapter 16. Files
return 1
def get_site_only(pat):
newpat = ""
for c in pat:
if c.isalpha():
newpat += c
return newpat
seq = """atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaa
tttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtg
ctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggc
ccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatca
tcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacattt
attgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatac
gctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtg
ggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggag
gaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaac
gcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatg
tgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagttt
caggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcacta
tgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaac
cgttttatcgggcggggtaa""".replace("\n","").upper()
ifh = open("../data/rebase.dat")
ofh = open("rebase.res", "w")
line = ifh.readline()
while line:
fields = line.split()
name = fields[0]
139
Chapter 16. Files
pat = get_site_only(fields[2])
if isexact(pat):
print_matches(ofh, name, findpos(seq, pat))
line = ifh.readline()
else:
line = ifh.readline()
ofh.close()
ifh.close()
Important
Notice that there are two files opened at the same time during the loop.
Figure 16.2 shows three flowchart comparing our first solution, with the new design and a version that first reads
all data in, handles them and writes all results at the end.
140
Chapter 16. Files
open the input file open the input file open the input file
141
Chapter 16. Files
For this purpose you can use comments that are ignored by the interpreter. In Python, all lines starting with a #
character are comments. But Python has a more powerful mechanism to document programs. If the first statement
of a function or file is a string, this string is used as documentation string which is available by the pydoc command
in a shell or the help function of the interpreter.
#!/usr/bin/python
"""
program that finds all occurences of restriction enzymes in a sequence
def isexact(pat):
"""
checks if a pattern is an exact DNA sequence
all strings containing only the following characters are considered to be
an exact DNA sequence: atgcATGC
"""
for c in pat.upper():
if c not in ’ATGC’:
return 0
return 1
def get_site_only(pat):
"""
clean a pattern string read from Rebase to obtain only the recognition site
sequence
"""
newpat = ""
for c in pat:
142
Chapter 16. Files
if c.isalpha():
newpat += c
return newpat
seq = """atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaa
tttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtg
ctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggc
ccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatca
tcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacattt
attgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatac
gctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtg
ggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggag
gaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaac
gcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatg
tgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagttt
caggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcacta
tgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaac
cgttttatcgggcggggtaa""".replace("\n","").upper()
143
Chapter 16. Files
line = ifh.readline()
NAME
restrict_documented - Program that find all occurrences of restriction enzymes in a sequence
FILE
FILE
/home/maufrais/cours_python/code/restrict_documented.py
DESCRIPTION
At the moment the sequence is contained in the seq variable
FUNCTIONS
get_site_only(pat)
clean a pattern string red from Rebase to obtain only the recognition site
sequence
isexact(pat)
checks if a pattern is an exact DNA sequence
all strings containing only the following characters are considered to be
an exact DNA sequence: atgcATGC
findpos(seq, pat)
find all occurrences of restriction site pattern in a sequence
RETURN VALUE: a list containing the start positions of all occurrences
RESTRICTION: can only process exact patterns, searching with ambiguous
pattern strings would not produce an error
DATA
__file__ = ’./restrict_documented.pyc’
144
Chapter 16. Files
__name__ = ’restrict_documented’
fields = [’ZraI’, ’3’, "GAC’GTC", ’0’, ’!’, ’AatII’, ’>I’, ’136’]
ifh = <closed file ’../data/rebase.dat’, mode ’r’>
line = ”
name = ’ZraI’
ofh = <closed file ’rebase.res’, mode ’w’>
pat = ’GACGTC’
seq = ’ATGAGTGAACGTCTGAGCATTACCCCGCTGGGGCCGTATATCGGCGC...CATCGGGCGACGA...
145
Chapter 16. Files
146
Chapter 17. Scripting
The sys module. The sys module provides an interface with the Python interpreter: you can retrieve the
version, the strings displayed as prompt (by default: ’>>>’ and ’...’), etc... You can find the arguments that
were provided on the command line:
The file handle for the standard input, output and error are accessible from the sys module:
The os module. This module is very helpful to handle files and directories, processus, and also to get environment
variables (see environ dictionary). One of the most useful component is the os.path module, that you use to
get informations on files:
147
Chapter 17. Scripting
The os.path module provides a method: walk that enables to walk in all the directories from a starting directory
and to call a given function on each.
The arguments of function f must be: dirname, which is the name of the directory, and fnames which is a list
containing the names of the files and subdirectories in dirname. arg is a free parameter, that is passed to walk
(here: None).
• Getting results.
148
Chapter 17. Scripting
import os
cmd="golden swissprot:malk_ecoli"
status = os.system(cmd)
print "Status: ", status
Another Python module, commands, enables to store the result of the execution in a string:
import commands
cmd="golden swissprot:malk_ecoli"
output = commands.getoutput(cmd)
print "Output: ", output
import commands
cmd="golden swissprot:malk_ecoli"
status, output = commands.getstatusoutput(cmd)
print "Output: ", output
print "Status: ", status
import os
import string
cmd="golden swissprot:malk_ecoli" ❶
handle = os.popen(cmd, ’r’) ❷
print string.join(handle.readlines()) ❸
handle.close()
❶ Builds the command line with a program name and the arguments.
❷ Runs the command and stores a handle in the handle variable. A handle for a command is the same kind
of objects as a file handle: you open it (with the popen command, read from it, and close it.
❸ Reads all the lines from the handle, and prints the joint result.
149
Chapter 17. Scripting
If the program takes time and if you wish to read the result step by step as long as results show up, you can do like
this:
import os
import sys
What if the entry name does not have a corresponding entry in the database? Let us try the following code:
import os
import sys
import string
cmd="golden swissprot:" + sys.argv[1] ❶
handle = os.popen(cmd, ’r’)
print string.join(handle.readlines())
status = handle.close()
if status is not None:
print "An error occured: ", status ❷
❶ Takes the entry name from the Python command line arguments, by using the sys module argv variable.
❷ If the provided entry name is invalid, the program returns a non zero value, that is returned by the close
function.
If you wish to get the complete error message from the program, use the popen3 function:
import os
import sys
cmd="golden swissprot:" + sys.argv[1]
tochild, fromchild, childerror = os.popen3(cmd, ’r’)
err = childerror.readlines()
if len(err) > 0:
print err
else:
print fromchild.readlines()
In this script, the call returns three objects: one to get results: fromchild (standard output), one to write to the
program - on its standard input - (for instance when the program is prompting for a value (see Example 17.4):
tochild, and one to access to the standard error file descriptor of the program.
150
Chapter 17. Scripting
import popen2
import os.path
cmd = "dnapars"
if os.path.exists(’treefile’): ❶
os.unlink(’treefile’)
if os.path.exists(’outfile’):
os.unlink(’outfile’)
child = popen2.Popen3(cmd) ❷
print "PID: ", child.pid
child.tochild.write("y\n") ❸
child.tochild.close()
child.wait() ❹
print "".join(child.fromchild.readlines()) ❺
status = child.fromchild.close()
if status is not None:
print "status: ", status
151
Chapter 17. Scripting
import sys
import getopt
def usage(prog="filtersites"): ❶
print """
filteralig : filter sites in alignments
"""
o, a = getopt.getopt(sys.argv[1:], ’ct:f:i:h’) ❷
opts = {}
for k,v in o: ❸
opts[k] = v
if opts.has_key(’-h’): ❹
usage(); sys.exit(0)
if len(a) < 1: ❺
usage(); sys.exit("alignment file missing")
❶ A usage function is very useful to help the user in case of an error on the command line.
❷ The first parameter for the getopt function should be a string containing the actual arguments the script
has been called with, not including the script name, available in sys.argv[0].
The second parameter is a string describing the expected options. The options string which is passed to
getopt is here: ’ct:f:i:h’. This means that the following options are available: c, t, f, i and h. When a ’:’ is
added just after, this means that the option expects a value. For instance, the ’-t’ option requires a threshold
value. See the usage!
152
Chapter 17. Scripting
The getopt function returns tuple, whose first element is a list of (option, value) pairs. The second element
is the list of program arguments left after the option list was stripped. Here, a filename for an alignment file
is expected.
❸ Storing (option, value) pairs in a dictionary.
❹ If the user has entered a -h, help is printed.
❺ Has the user provided a filename ? If so, it is available in a[0].
17.4. Parsing
In Bioinformatics, parsing is very important, since it enables to extract informations from data files or to extract
results produced by various analysis programs, and to make them available in your programs. For instance, a
Blast parser will transform a text output into a list of hits and their alignment, that can be made available as a data
structure, such as, for example, Biopython Bio.Blast.Record objects, that you can use in a Python program.
The purpose of this section is not to present everything about parsing, but just to introduce some basic notions.
Parsing means analyzing a text and producing structured data in a form that is useful for programs. It can be a
list of strings, a set of classes instances, or just a boolean result: this depends on the needs and the parsing system
you are using. An important aspect of parsing is the architecture that is used to process the text that you want to
analyze.
• Parsing can be done by just reading a file or a stream line by line, and by looking for the occurrence(s) of
a word, or a pattern. In Figure 17.1, lines are searched for a header pattern, or a hit pattern, and processed
accordingly.
Python program
153
Chapter 17. Scripting
• You can tell an external component which set of words you are interested in, by providing a description of set
of words to be found. You then feed this component a document to parse, and wait him to tell you when these
words are found as well as sending you the occurrences. This kind of system is said to be event-driven, and
XML provides tools for such type of parsing.
header_handler(data)
Engine
hit_handler(data)
• You can describe the whole document by set of hierarchical subparts, and associate to each sub-part actions
to be automatically executed by a parsing engine. Figure 17.3 shows such a system to parse a Blast report. A
Blast report is described as a header followed by a list of hits. A hit is described as a score and a list of HSP, and
a HSP is described as a score and a list of alignments. You define these subparts by a set of rules, sometimes
using patterns, in what is usually called a grammar, or even a decorated grammar, since you decorate each
sub-part with an associated action. The lex and yacc system is such a parsing engine.
154
Chapter 17. Scripting
blast_report
Engine
decorated grammar
• You can have a parsing engine process your data according to a grammar, and returns a hierarchical data
structure in a form that your program can understand (for instance, in Python, as a set of objects). The
XML/DOM engine behaves like this.
grammar
155
Chapter 17. Scripting
So, in all the cases, there is an engine driving the whole process, be it a simple loop or a specialized component.
In this chapter, we will just do some "manual" parsing with patterns that are introduced in Section 17.5, as well as
some event-driven parsing will be done as a practical work on abstract frameworks (see Exercise 19.4), and during
the Web/XML course.
The aim of a pattern is to define not only one word to be searched for, but a set of words. This definition is
provided in a given language, depending on the system you are working with; the set of corresponding words also
depends on the system.
ls s*
means list all the files beginning by ’s’, and potentially followed by anything. The command:
ls s[ie]n*
means list all the files beginning by ’s’, followed by either ’i’ or ’e’, and followed by anything, including nothing.
So, in the context of the ls function within a Unix shell, the set of words is defined as the files existing on the
filesytem (as opposed, for instance, to the files stored on another computer, not being made available through a
distributed filesystem).
In the Prosite database, patterns describing protein domains are described, such as:
H-C-H-x(3)-H-x(3)-[AG]-[LM]
which represent the following set of amino-acid sequences: sequences begining by ’HCH’, followed by 3 positions
containing any amino-acid letter, followed by ’H’, followed again by 3 free positions, followed by either ’A’ or
’G’, followed by either ’L’ or ’M’. As you can notice, the language to define patterns is different from the Unix
shell, as well as the set of corresponding words. Here, they are just sequences of amino-acids letters.
In the grep Unix command, a command to search for patterns in files, although similar to the shell pattern syntax,
there is a slight difference. Say that a file contains:
156
Chapter 17. Scripting
science
s
another
will return ... all the lines, because ’s*’ means all the words beginning by 0 or any number of ’s’. In the grep
command, the set of words is composed of the lines of the file. So the term "set of words" must be understood in a
broad sense. It must be understood also that the set is not actually generated, of course: it can be infinite! Instead,
an operational representation of this set is built, through a finite-state automaton.
means that you restrict the query to the clones whose identifier does not begin with a ’A’, is followed by any
character N times, and ends with an even number (Sybase).
While the set of words corresponding to a pattern is described by the given expression and the semantics of the
system being used, the set of found words, also called occurrences, depends on data. So, occurrences are the
words from the set of words that were actually found within data. Figure 17.5 summarizes the concepts.
expression
set of words
(representation of)
occurrences of
words in data
data
157
Chapter 17. Scripting
>>> import re
Before searching for a pattern, you must first compile it (this builds the "set of words", or rather the representation
of this set):
pattern is a pattern object. You then issue a search, for instance in the small sequence seq, by a request to the
pattern object:
This establishes the matching, or correspondances, between the set of possible words and data. match is called
a match object. To get the occurrences, you can ask the match object for the start and end of the match in the
searched text:
or the group:
>>> match.group(0)
’PAD’
158
Chapter 17. Scripting
expression
pattern object
match object
pattern.search()
pattern.match()
match.start()
match.end()
match.group(0)
data
Example 17.6. Searching for the occurrence of PS00079 and PS00080 Prosite patterns in
the Human Ferroxidase protein
import sys
import re
from Bio.SwissProt import SProt
sp = open(sys.argv[1])
iterator = SProt.Iterator(sp, SProt.SequenceParser())
seq = iterator.next().seq
sp.close()
PS00079 = ’G.[FYW].[LIVMFYW].[CST].{8,8}G[LM]...[LIVMFYW]’ ❶
pattern = re.compile(PS00079) ❷
match = pattern.search(seq.tostring()) ❸
print PS00079
print match.start(), match.end(), seq[match.start():match.end()] ❹
159
Chapter 17. Scripting
There are several methods to search: search and match, the difference being that match looks for a match at
the beginning of the string. So, back to the example, the following statement:
match = pattern.match(seq.tostring())
would return a positive result only if the sequence begins by an occurrence of PS00079.
import sys
import re
from Bio.SwissProt import SProt
sp = open(sys.argv[1])
iterator = SProt.Iterator(sp, SProt.SequenceParser())
seq = iterator.next().seq
sp.close()
PS00080 = ’(?P<copper3>H)CH...H...[AG](?P<copper1>[LM])’ ❶
pattern = re.compile(PS00080)
match = pattern.search(seq.tostring())
print PS00080
print match.start(), match.end(), seq[match.start():match.end()]
Shortcuts. The re module provides shortcuts to directly search for an expression without compiling the pattern
into a pattern object:
You can also directly get the occurrences of a pattern object in a string:
160
Chapter 17. Scripting
Figure 17.7 summarizes re module objects and methods to perform pattern searching.
compile()
match(), search()
match(), search()
pattern group()
re
Search modes.
Text substitutions.
17.5.3. Prosite
This section presents the Prosite classes in Biopython, which have a common interface with the Python pattern
module.
161
Chapter 17. Scripting
entry = prosite[’PS00079’]
prosite = Bio.Prosite.ExPASyDictionary()
As you can guess by the name of the module, you actually fetch the Prosite entry on the Web. You could also fetch
the Prosite entry from a local database with the golden program (see ???). The entry fetched above is actually a
string. In order to have the dictionary return a record, you must rather create it like this:
prosite = Bio.Prosite.ExPASyDictionary(parser=Bio.Prosite.RecordParser())
162
Chapter 18. Object-oriented programming
In the next chapter (Chapter 19), concepts related to object-oriented design will be developped.
name
seq
18.2.2. Methods
As it is described so far, the DNA object is just a kind of record to group data. There are other ways of grouping
data in Python: you can either use a list, or a dictionary:
163
Chapter 18. Object-oriented programming
but an object is actually more than just that. As shown in Figure 18.2 (and as stated in Section 6.1), an object is
indeed more than just a value or a record of values: it can "do" things, and it "knows" what it can do, or. In other
words, it has an associated behaviour. This behaviour is described by functions that are associated to it, and these
functions are called methods. For instance, our DNA object can perform operations: it can translate its sequence
or it can compute its GC percent by calling the gc function:
>>> s1.gc()
0.26
So, available operations are an important aspect of objects, that we need to add in our representation. In Figure
18.2, methods are represented as "counters", as at the post office.
name
gc
seq
translate
revcompl
setname
So, with objects, not only do we have a convenient way to group data that are related, but we also have a way to
group data and their related operations in the same place.
Object
An object is a construction to group values and operations on these values.
18.2.3. Classes
Now, how do you define objects, such as our DNA object, with their attributes and methods?
Class
A class is an object maker: it contains all the statements needed to create an object, its attributes, as well as the
statements to describe the operations that the object will be able to perform.
164
Chapter 18. Object-oriented programming
The term class can be somewhat misleading, for it also refers to something belonging to a classification. In object-
oriented programming, a class can indeed also be a member of a set of classes organized in a hierarchy. This aspect
will be introduced later (see Section 19.4), through the concept of inheritance. But for now, we will use the term
class as just a maker of objects, or an object factory.
Having a class defined, you can create as many objects as needed. These objects are called instances of this class.
All the instances created with a given class will have the same structure and behaviour. They will only differ
regarding their state, i.e regarding the value of their attributes. Namely, names and sequences will not be the same
for all sequences objects that will be created.
Instance
Instances are objects created with a class.
The behaviour of the instances, i.e, the operations that are available for them either to modify them or to ask them
for services, is described by functions associated to the class. These functions are called methods.
Method
A method is a function defined for a class, specifying the behaviour of the class instances.
Important
Classes and instances are not the same: the class is the maker, whereas the instance is the object being
made, according to the model defined by the class.
But the actual direct syntax to create objects or to instantiate a class, i.e to create an instance of class, is by calling
a function with the same name as the class. So, in order to create an instance of the DNA class we have just defined,
i.e an object which handles a DNA sequence, you do:
>>> s1 = DNA()
After the DNA() function is called, the instance of the class DNA is referred to by the s1 variable, just like with
any Python value. You could also provide more information at instantiation. Let’s say that we can create the name
and seq attributes, we can do :
165
Chapter 18. Object-oriented programming
This time, the new DNA instance has been created with values for name and seq attributes. You can also use
keyword arguments (especially if you are no longer sure in what order they should be entered) :
So, this creates the object and makes it available from the s2 variable. You can now use this object to call DNA
class methods, by using the dot operator:
>>> s2.gc()
0.66
The interpretor can find which class s2 belongs to, and calls the gc method that is defined for the DNA class. You
can also actually access to object’s attributes by using the dot operator:
>>> s2.name
’seq2’
>>> s2.seq
’acaagatgccattgtcccccggcctcctgctgctgctgctctccggggcca’
The setname method enables to change the name attribute of our object:
❶
class DNA:
166
Chapter 18. Object-oriented programming
def gc(self):
count_c = self.seq.count(’c’) ❹
count_g = self.seq.count(’g’)
return float(count_c + count_g) / len(self.seq)
The self parameter represents the object itself. You could of course use any other word like carrot or ego, but
this would not help the reading of your code by others... So self is present in the Class and methods definitions
each time the reference to the object instance is needed.
Let us first look at one of these methods definitions, the gc method, which computes the GC percent of the
sequence:
def gc(self):
count_c = self.seq.count(’c’)
count_g = self.seq.count(’g’)
return float(count_c + count_g) / len(self.seq)
Method definitions follow exactly the same pattern as standard function definitions, except that they must have
declared a first parameter (here: self) for referring to the instance. Indeed, an instance identificator, actually a
reference to the instance, is required in order for the statements within methods to access to the current instance
attributes: here, the access to the seq attribute is needed in order perform the count. In fact, Python automatically
passes the instance reference as the first argument of a method. Hence, it is associated to the first parameter
which is the self parameter. You don’t need to specify the argument for the reference to the instance. This is
automatically done by Python. In fact, calling:
>>> s2.gc()
0.66
is equivalent to:
167
Chapter 18. Object-oriented programming
>>> DNA.gc(s2)
0.66
The interpretor can find which class s2 belongs to, and handles the passing of the instance reference argument
automatically.
How does the method computes its result? For this, it needs to access to the character sequence of the DNA
object. This is done by using the seq attribute, that was defined at instantiation (i.e by the __init__ method,
see below). Within the method, the attribute is available through the object by the dot operator: self.seq. This
shows that the object attributes are always available, at least as long as the object itself still exists. Attributes are
thus accessible from all the methods of the class. They are a way to share data among methods. The method also
use local variables: count_c and count_g to compute intermediate results. These variables have a local scope,
restricted to the method’s namespace, exactly like local variables that are defined in functions.
This is a special method, which, when defined, is called at class instantiation, e.g when you run the following
statement:
the __init__ method defined for the DNA class is in fact called with 3 arguments. As for the other methods, the
self argument is automatically provided by Python as a reference to the newly created instance. You don’t have
to provide an __init__ method, but it is usually the good place to put initialization statements. Initial values
for attributes can be passed as arguments and associated to attributes. A good practice is to assign them default
values, such as None. You can also notice that the seq attribute is initialized with the lower string function: the
other methods will thus not have to check for this in order to perform their computation.
Attributes ca be changed at any time of course, not only in the __init__ method. When called with a string
argument standing for a new name, the setname method changes the name attribute of our object:
The body of this method is quite straightforward, it consists in a single statement which assigns a new value, the
one passed as an argument to the method, to the name attribute.
168
Chapter 18. Object-oriented programming
>>> p1 = Point(2,3)
>>> p2 = Point(3,3)
>>> p1.show()
(2, 3)
>>> p2.show()
(3, 3)
>>> p1.move(10, -10)
>>> p1.show()
(12, -7)
>>> p2.show()
(3, 3)
Solution 18.1
The following definition is the complete definition of the DNA, with two methods added, one for computing the
reverse complement, the second one for computing the translated protein sequence.
class DNA:
def gc(self):
count_c = self.seq.count(’c’)
count_g = self.seq.count(’g’)
return float(count_c + count_g) / len(self.seq)
def revcompl(self):
revseq = ”
for c in self.seq:
revseq = c + revseq ❶
revcompseq = ”
for base in revseq:
if base == ’a’:
revcompseq += ’t’
elif base == ’t’:
revcompseq += ’a’
169
Chapter 18. Object-oriented programming
return revcompseq
if frame > 2:
return ”
protseq = ”
return protseq
❶ This method defines how to compute the reverse complement of the sequence.
❷ This method defines how to translate the DNA sequence into a protein sequence.
❸ The Standard_Genetic_Code dictionary is defined elsewhere.
if frame > 2:
return ”
170
Chapter 18. Object-oriented programming
protseq = ”
nb_codons = len(seq)/3
return protseq
>>> s2.translate(0)
TRCHCPPASCCCCSPGP
Indeed, this method has declared a frame parameter. It thus takes two arguments: self and frame. As for the
gc method, the first parameter does not have to be specified at calling time, only the remaining ones.
The translate method needs to call another method of the class: revcompl, which computes the reverse
complement. Notice that this method is called without any argument. This is not necessary, because it is called on
the object itself, referenced by the variable self with the dot operator:
self.revcompl()
revcompl will return a character string, that will be used by translate to compute a negative frame.
First of all, let us look at how we could represent a simplified Protein object. Figure Figure 18.3 shows an
object having one method: mw, and 2 attributes: name and seq.
name
mw
seq
setname
171
Chapter 18. Object-oriented programming
class Protein:
default_prosite_file = ’prosite.dat’
def mw(self):
molW = 0
for aa in self.seq:
molW += Protein.weight[aa]
return molW
You can notice that the class starts by the definition of weight and default_prosite_file variables. As
we will see later, this class variable is available to all instances of the class.
Now, the DNA objects knows how to be translated, right? So it would be more clever for the DNA class
translate method to return a Protein object... The new definition of the translate method is:
if frame > 2:
return ”
protseq = ”
nb_codons = len(seq)/3
172
Chapter 18. Object-oriented programming
return new_protein
Look at the returned value: it is now a Protein object. The argument for the seq parameter of the Protein
class’s __init__ method is the value of the newly computed protseq, and the argument for the name is
constructed from the Protein object’s name.
In the Protein object, we might also be interested in keeping the reference to the initial DNA object. This can
help to analyze the protein sequence later. Figure 18.4 shows the DNA and Protein objects, and the link between
them. Now, the Protein object has 3 attributes: name, seq and dna.
name
gc
seq
translate
revcompl
setname
p1
name
mw
seq
dna setname
173
Chapter 18. Object-oriented programming
self.dna = dna
The final code for Protein object instantiation in the translate method is now:
return new_protein
Look at the value provided for the dna parameter of the Protein __init__ method. It is a reference to the
DNA object, i.e: self. Notice that none of the parameters is mandatory (except self of course). In particular,
the dna parameter does not have to be provided when the Protein is directly created from a file, as opposed to
translated from a DNA object.
>>> p1 = Point(2,3)
>>> p2 = Point(3,3)
>>> p1.distance(p2)
1.0
Solution 18.2
18.5.1. Namespaces
174
Chapter 18. Object-oriented programming
Classes and instances have their own namespaces, that is accessible with the dot (’.’) operator. As illustrated by
Figure 18.5, these namespaces are implemented by dictionaries, one for each instance, and one for the class (see
also [Martelli2002]).
DNA gc
translate
revcompl
setname
class class
"seq1" "seq2"
name name
namespace namespace
seq "aaacaa..." seq "aatag.."
s1 s2
Instances attributes. As we have learnt, a class may define attributes for its instances. For example, attributes of
s1, such as the name, are directly available through the dot operator:
>>> s1.name
’seq1’
The dictionary for the instance attributes is also accessible by its __dict__ variable, or the vars() function:
>>> s1.__dict__
{’seq: ’aaacaacttcgtaagtata’, ’name’: ’seq1’}
>>> vars(s1)
{’seq’: ’aaacaacttcgtaagtata’, ’name’: ’seq1’}
>>> dir(s1)
[’__doc__’, ’__init__’, ’__module__’, ’gc’, ’translate’, ’name’, ’seq’]
because it is not limited to the dictionary of the instance. It actually also displays its class attributes, and recursively
the attributes of its class base classes (see Section 19.4). You can add attributes to an instance that were not defined
by the class, such as the annotation in the following:
175
Chapter 18. Object-oriented programming
Adding attributes on-the-fly is not something that is available in many object-oriented programming languages!
Be aware that this type of programming should be used carefully, since by doing this, you start to have instances
that have different behaviour, at least if you consider that the list of attributes defines a behaviour. This is not the
same as having a different state by having different values for the same attribute. But this matter is probably a
topic of discussion.
Class attributes. It is also possible to define attributes at the class level. These attributes will be shared by all the
instances (Figure 18.6). You define such attributes in the class body part, usually at the top, for legibility:
class Protein:
...
weight = {"A":71.08,"C":103.14 ,"D":115.09 ,"E":129.12 ,"F":147.18 ,"G":57.06 ,"H":1
default_prosite_file = ’prosite.dat’
...
>>> Protein.default_prosite_file
’prosite.dat’
>>> Protein.weight
{"A":71.08,"C":103.14 ,"D":115.09 ,"E":129.12 ,"F":147.18 ,"G":57.06 ,"H":137.15 ,"I":113.17 ,"K":128
176
Chapter 18. Object-oriented programming
Protein mw
setname
weight {"A":71.08,"C":103.":71.08, ..}
DNA gc
translate
revcompl
setname
class class
"seq1" "seq2"
name name
namespace namespace
seq "aaacaa..." seq "aatag.."
s1 s2
You can also access to this attribute through an instance:
>>> p1.default_prosite_file
’prosite.dat’
❶ This just creates a new default_prosite_file attribute for the p1 instance, which masks the class
attribute, by the way.
177
Chapter 18. Object-oriented programming
The class attributes are displayed by the pydoc command, as opposed to the instance attributes (see Section
18.5.5).
Class methods are referenced in the class dictionary: but what is their value actually? As shown in Figure 18.6,
the class dictionary entries for methods are pointing to standard Python functions. When accessing to a method
through an instance name, such as in p1.mw, there is an intermediate data structure, that itself points to the class
and the instance. Objects in this data structure are called bound methods:
>>> s1.gc
<bound method DNA.gc> of <__main__.DNA instance at 0x4016a56c>
They are said to be bound, because they are bound to a particular instance, and know about it. This is really
important, for it is the only way to know what to do, and on which object to operate.
DNA gc
translate
revcompl
setname
class
bound methods :
gc
s1.gc
"seq1" translate s1.translate
name
revcompl s1.revcompl
s1 seq "aaacaa..."
setname s1.setname
class C2:
def show(self):
print "I am an instance of class ", self.__class__
def create_C2_ref_in(p):
p.c2 = C2() ❶
178
Chapter 18. Object-oriented programming
c1 = C1()
create_C2_ref_in(c1) ❷
c1.c2.show() ❸
❶ This function creates an instance of class C2 and stores its reference in an attribute of p, an instance of class
C1.
❷ Creation of the C2 instance by calling create_C2_ref_in
❸ This statement displays: "I am an instance of class __main__.C2"
As you can observe, the C2 instance exists after exiting the create_C2_ref_in function. It will exist as long
as its reference remains in the c1.c2 attribute. If you issue:
c1.c2 = None
There will be no reference left to our C2 instance, and it will be automatically deleted. The same would happen if
you would issue an additional call to the create_C2_ref_in function:
create_C2_ref_in(c1)
it would overwrite the preceeding reference to the former C2 instance, and delete it. You can check this by asking
the c1.c2 reference for its identity:
id(c1.c2)
del c1.c2
179
Chapter 18. Object-oriented programming
True
This means that the equality operator must be defined by the programmer. We will see the __eq__ special method
later in Section 19.3.3.
Instances identity means that two objects are in fact the same object, or more exactly, that two variables refer to
the same object.
34
’a nice string’
0.006
[7, ’a’, 45]
whereas there is no literal for a class. The reason for this difference between types and classes is that you can
define a predicate for recognizing expressions of a type [Wegner89], while, with class, you cannot, you can only
define collections of objects after a template.
As shown in Figure 18.8, the Python type() can be used to know whether a variable is a class or an instance.
It will very basically answer ClassType or InstanceType, as defined in module types, but it will not tell
you which class an instance belongs to.
DNA type()
ClassType
__class__
s1 InstanceType
type()
180
Chapter 18. Object-oriented programming
pydoc sequence.DNA
See also the embedding module, which might bring additional documentation about related components. This
may be important when several classes work together, as is described in Section 18.4.
When you consult the documentation of a class with the pydoc command, you get most of the time a strange list of
method names, such as __str__ or __getitem__. These methods are special methods to redefine operators,
and will be explained in the next chapter on object-oriented design (Section 19.3.3).
Caution: the defined instances attributes will not be listed by pydoc, since they belong to the instances rather than
to the class. That is why they should be described in the documentation string of the class. If they are not, which
sometimes happens..., run the Python interpretor and create an instance, then ask for its dictionary or use the dir()
command:
>>> s1 = DNA()
>>> dir(s1)
[’__doc__’, ’__init__’, ’__module__’, ’gc’, ’revcompl’, ’translate’, ’name’, ’seq’]
Information on instances. There are some mechanisms to know about the class of a given instance. You can use
the special attribute __class__:
>>> s1 = DNA()
>>> s1.__class__
class __main__.DNA at 0x81d1d64>
>>> s2=s1.__class__()
>>> s2
__main__.DNA instance at 0x8194ca4>
This can be useful if you need to create an object of the same class of another source object, without knowing the
class of the source object. You can also ask whether an object belongs to a given class:
>>> isinstance(s1,DNA)
181
Chapter 18. Object-oriented programming
True
As mentionned above, the Python type() will not provide the class of an instance, but just: InstanceType
(see Figure 18.8).
18.6. Solutions
Solution 18.1. A Point class
Exercise 18.1
class Point:
def show(self):
return self.x, self.y
class Point:
def show(self):
return self.x, self.y
182
Chapter 19. Object-oriented design
19.2. Components
19.2.1. Software quality factors
The topics introduced in this section address some of the issues of software quality, and how Python can help on
this matter.
Before entering into details, let us just summarize some important concepts (you can find a good and more
exhaustive description in [Meyer97]). There is no absolute quality in software: depending on the context, scale,
scope and goals of the program being developped, you might very well either write on-the-fly short pieces of code
to solve a temporary problem, or spend a significant effort to have your application follow an industrial design
process. So, rather than only a global so-called standard that should be applied for each program you write, there
are a few quality factors to be evaluated according to the actual needs of the project. Among these factors, one
usually distinguish between internal and external factors. External quality factors are the ones that corresponds to
visible requirements, directly important for the user of the software, such as validity, robustness or efficiency.
Internal quality factors are properties of the code itself, such as legibility or modularity. In fact, internal factors
often indirectly help to get external quality. Some factors, such as reusability, extensibility and compatibility,
that we will study more thoroughly here, belong to external quality factors in the sense that they can be required
by the users of a software library or the programmers of a shared source code; they are also important internal
quality factors in the sense that they make the internal quality of the source code better. The aim of this chapter is
mainly to describe these factors, as well as internal quality factors.
• They can become very large, resulting in thousand lines of code where it is becoming difficult to make even a
slight change (extensibility problem).
183
Chapter 19. Object-oriented design
• When an application is developped within a team, it is important for different people to be able to share the
code and combine parts developped by different people (compatibility) ; having big and complex source files
can become a problem.
• During your programmer’s life, or within a team, you will very often have to re-use the same kind of
instructions set: searching for an item in a collection, organizing a hierarchical data structure, converting data
formats, ...; moreover, such typical blocks of code have certainly already been done elsewhere (reusability).
Generally, source code not well designed for re-use can thus be a problem.
So, depending on the context of the project, there are some issues which are just related to the management of
source code by humans, as opposed to the specification of the task to perform. And if you think about it, you
probably tend to use variable names that are relevant to your data and problem, aren’t you? So, why? This is
probably not for the computer, but, of course, rather for the human reader. So, in order to handle source structure
and management issues, several conceptual and technical solutions have been designed in modern programming
languages. This is the topic of this chapter.
Let us say that we have to develop source code for a big application and that we want this source code to be spread
and shared among several team members, to be easy to maintain and evolve (extensible), and to be useful outside
of the project for other people (reusable). What are the properties a source code should have for these purpose?
• they should be as independant as possible: you should not have to use chunk A each time you need to use
chunk B
The most important concept to obtain these properties is called modularity, or how to build modular software
components. The general idea related to software components is that an application program can be built by
combining small logical building blocks. In this approach, as shown in figure Figure 19.1, building blocks form a
kind of high-level language.
184
Chapter 19. Object-oriented design
Application
Components
Python
19.2.3. Modularity
The simplest form of modularity is actually something that you already know: writing a function to encapsulate a
block of statements within a logical unit, with some form of generalization, or abstraction, through the definition
of some parameters. But there are more general and elaborated forms of components, namely: modules and
packages.
So, what is modularity? As developped in [Meyer97], modularity is again not a general single property, but is
rather described by a few principles:
• A few interfaces: a component must communicate with as few other components as possible. The graph of
dependencies between components should be rather loosely coupled.
• Small interfaces: whenever two components communicate, there should be as few communication as possible
between them.
• Explicit interfaces: interfaces should be explicit. Indirect coupling, in particular through shared variables,
should be made explicitly public.
• Information hiding: information in a component should generally remain private, except for elements
explicitly belonging to the interface. This means that it should not be necessary to use non public attributes
elements of a component in order to use it. In languages such as Python, as we will see later, it is technically
difficult to hide a component’s attributes. So, some care must be taken in order to document public and private
attributes.
185
Chapter 19. Object-oriented design
• Syntactic units: Components must correspond to syntactic units of the language. In Python, this means that
components should correspond to known elements such as modules, packages, classes, or functions that you
use in Python statements:
import dna
from Bio.Seq import Seq
dna, Bio, Bio.Seq and Seq are syntactic units, not only files, directories or block of statements. In fact,
Python really helps in defining components: almost everything that you define in a module is a syntactic unit.
You can view this approach as though not only the user of the application would be taken into account, but also
the programmer, as the user of an intermediate level product. This is why there is a need for interfaces design also
at the component level.
19.2.4. Methodology
These properties may be easier to obtain by choosing an appropriate design methodogy. A design methodoly
should indeed:
• help in designing self-understandable components: a programmer should be able to understand how to use a
component by looking only at this component;
• help in defining extensible components; the more independant components are, the more they are easy to
evolve; for instance, components sharing an internal data structure representation are difficult to change,
because you have to modify all of them whenever you change the data structure.
19.2.5. Reusability
Programming is by definition a very repetitive task, and programmers have dreamed a lot of being able to pick off-
the-shelves general purpose components, relieving them from this burden of programming the same code again
and again. However, this objective has, by far, not really been reached so far. There are several both non technical
and technical reasons for this. Non-technical reasons encompass organisational and psychological obstacles:
although this has probably been reduced by the wide access to the Web, being aware of existing software, taking
the time to learn it, and accepting to use something you don’t have built yourself are common difficulties in reusing
components. On the technical side, there are some conditions for modules to be reusable.
1. Flexibility: One of the main difficulty for making reusable components lies in the fact that, while having the
impression that you are again programming the same stereotyped code, one does not really repeat exactly the
same code. There are indeed slight variations that typically concern to following aspects (for instance, in a
standard table lookup):
186
Chapter 19. Object-oriented design
• types: the exact data type being used may vary: the table might contain integers, strings, ...
• data structures and algorithms may vary: the table might be implemented with an array, a dictionary, a
binary search tree, ... ; the comparison function in the sort procedure may also vary according to the type
of the items.
So, as you can understand from these remarks, the more flexible the component is, the more reusable it is.
Flexibility can be partly obtained by modularity, as long as modules are well designed. However, in order to
get real flexibility, other techniques are required, such as genericity, polymorphism, or inheritance, that are
described in Section 19.5.
2. Independancy towards the internal representation: by providing a interface that does not imply any specific
internal data structure, the module can be used more safely. The client program will be able to use the same
interface, even if the internal representation is modified.
3. Group of related objects: it is easier to use components when all objects that should be used together (the
data structures, data and algorithms) are actually grouped in the same component.
4. Common features: common features or similar templates among different modules should be made shareable,
thus making the whole set of modules more consistent.
• The implementor of the class can change the implementation for maintenance, bug fixes or optimization
reasons, without disturbing the client code.
• The data type is defined as a set of high-level services with a semantic contract defining both the output that
is provided and the input that is required from the client. This actually correspond to the general definition of
a type in programming: a type is defined as a set of possible values and a set of operations available on these
values.
Among the methods defined in the interface of a data type, there are methods that build or change the state of the
corresponding object, which are called constructors and modificators, and there are methods to access the object,
which are called accessors.
187
Chapter 19. Object-oriented design
Stacks serve to pile up objects of any kind, and take them out according to the rule "last in, first out" (LIFO).
Imagine a pile of plates at home, in your cupboard, the safest way to take a plate out is to take the last one put
in (Figure 18.8). Such structures are omni-present in programing, they serve to move along graphs, to compile
programs, etc...
Conceiving an abstract data type (ADT) for the stack consists in describing the functions needed making
abstraction of the implementation choice that will be made at the end (and that may change). For instance,
you might use a python List to implement a stack. But will you add elements at the beginning or at the end
of the list? This concrete decision is out of concern for the user of your class, who should be taken away from
knowing about this. Indeed in all cases, the basic services are the same, and can be given common names, that do
not "betray" how the list is used "inside" of the code. The set of services is listed in Table 19.1. In this table, the
description of the services includes:
• which results occur, including changes within the internal state of the object.
• A short description is provided, sometimes providing more details on the context and conditions under which
the service is executed.
188
Chapter 19. Object-oriented design
The description provided by Table 19.1 should suffice for the client to use the class. What is more important here,
is that the client does not have to know anything about the internal representation of the stack.
In this ADT accessors are: item and empty, constructor is make , and modificators are: put and remove.
The next step,once this description of the ADT is made for the client, consists in specifying the ADT, that is,
describing in formal or mathematical language what the functions are doing. This involves four steps:
• Types: this corresponds in Python to the classes that are used for the ADT. In our example there is one class,
Stack.
• Functions: That is their names and the input and output they have, together with the changes that might occur,
as shown in Table 19.1, first four columns.
• Axioms: The rules to which the functions obey, and that are sufficient to describe them. For the stack, these
axioms are, for any stack s and element e:
• item(put(s, e))=e
• remove(put(s, e))=s
• empty(make) is True
• empty(put(s, e) is False
• Pre-conditions: The functions we need will not operate under all conditions. For example, you cannot remove
an element from an empty stack. These pre-conditions can be enumerated as follows for the stack:
189
Chapter 19. Object-oriented design
Axioms and pre-conditions formulate more precisely what is said in the fifth column of Table 19.1
The problem is that there is no real mechanism in Python to prevent a client code from accessing to an instance
attributes, as there are in other languages (such as Java or C++), where you can declare private and public
attributes. Python provides just the following: if you name a variable with a ’__’ prefix and no ’_’ suffix, such as:
__list = []
within a class named, e.g: Stack, Python automatically changes it to: _Stack__list. You can still access it,
but you are aware that you are probably doing something odd, and it can help to avoid name collisions.
Another way to distinguish public and private attributes and methods is to prefix the private ones with a single ’_’
character. This does not provoke any automatic addition by Python, but it is warning the reader that this part of the
code is private. Another style matter is the naming of classes, where it is a good idea to capitalize the first letter of
class names, to distinguish them from variable or function names. Be aware finally that some methods in Python
are framed by double ’__’ on both side, they are called special methods and described in the next p aragraph.
The lesson to be learnt is that attributes you want to be accessible should be mentionned explicitely, and
documented as such.
Now that the ADT is specified, that we know which attributes and methods are public or private, We can turn to
the implementation part of our example. As shown in Example 19.2, we have decided to use a Python list, and to
add new elements to the end of this list (another option wold have been to insert the new element at the beginning
of the list).
class Stack:
"""
A class to handle stacks
"""
def __init__(self):
self._list = []
190
Chapter 19. Object-oriented design
"""
- places an item on top of the stack
- does not return anything
"""
self._list.append(item)
def remove(self):
"""
- if not empty, removes the top item, i.e the last item placed on the stack
else does not do anything
- does not return anything
"""
if len(self._list) > 0:
self._list.pop()
def item(self):
"""
- if not empty, returns the item on top of the stack
without removing it
else does not do anything
"""
if len(self._list) > 0:
return self._list[-1]
def empty(self):
"""
- tells if the stack is empty
- returns a Boolean
"""
return len(self._list) == 0
191
Chapter 19. Object-oriented design
We would prefer a more informative output, providing the name of the sequence and the sequence itself (a sort of
Fasta formatted output):
>>> print s1
>seq1
acaagatgccattgtc
How do we get this? As mentioned above, we just have to add a special method to our DNA class, called __str__,
which must return a printable character string:
def __str__(self):
return ">" + self.name + "\n" + self.seq + "\n"
Such special methods have obligate names, and are framed by ’__’, such as __str__ for print. A set a very
precious pages in the book ’Python. Essential reference’ concerns all the tables (3.7 to 3.10) listing these obligate
names. Here below in Table 19.2, we will list a small number of examples.
192
Chapter 19. Object-oriented design
There is a way however to specify how the == operator should behave for your class. We need to define ourselves
what equality means for our class. The following code redefines the == operator as returning True whenever the
name and seq attributes are equal.
class DNA:
...
Another important operator that could be useful for sequences is the [] operator. For strings, this operator enables
to access a character at a specific position. What we would like fo a DNA object is to access the ith character of
the seq attribute:
>>> s1[3]
’a’
class DNA:
...
193
Chapter 19. Object-oriented design
19.4.1. Introduction
What we have seen so far, object-based programming, consists in designing programs with objects, that are built
with classes. In most object-oriented programming languages, you also have a mechanism that enables classes to
share code. The idea is very simple: whenever there are some commonalities between classes, why having them
repeat the same code, thus leading to maintenance complexity and potential inconsistency? So the principle of
this mechanism is to define some classes as being the same as other ones, with some differences.
class DNA(Sequence):
The DNA class is called a subclass, and the Sequence is called a superclass or a base class. Following the
class statement, there are only the definitions specific to the DNA or Protein classes: you do not need to
re-specify the other ones. The following code shows how to specify the Sequence superclass.
class Sequence:
def clean(self):
seq = ""
for c in self.seq:
if c in self.alphabet:
seq += c
self.seq = seq
def __str__(self):
return ">"+self.name+"\n"+self.seq
def getname(self):
return self.seq
def getseq(self):
194
Chapter 19. Object-oriented design
return self.seq
class DNA(Sequence):
alphabet = "atcg"
def gc(self):
"""GC percent"""
...
def revcompl(self):
"""reverse complement"""
...
def translate(self):
"""translation into a protein"""
...
class Protein(Sequence):
alphabet = weight.keys()
def mw(self):
molW = 0
for aa in self.seq:
molW += self.weight[aa]
molW += 18.02
molW = molW / 1000
return molW
The __str__ method that is called when issuing a print statement is not defined for the DNA class. So python
looks up one level further, finds a definition at the Sequence class definition level and calls it. Provided with
195
Chapter 19. Object-oriented design
the self reference to the object, exactly the same way as for DNA class methods, it can access to the attributes in
order to produce a printable string of the object.
The clean method is a common method, except that it uses a specific alphabet for each type of sequence. This
is why each subclass must define an alphabet class attribute. When referring to self.alphabet, python
looks up in the object’s namespace first, then one level up in the class namespace and finds it.
Now look carefuly at the __init__ method: it is now defined in the superclass. There are indeed common
things to do for both DNA and Protein classes: attributes initialization, sequence cleaning, ... So it is enough
to define the __init__ method at the superclass level: whenever the __init__ method, or any method is not
defined at the subclass level, it is the method at the superclass level which is called (see below Section 19.4.1.2).
But, remember, the DNA sequence is supposed to be in lowercase, whereas the protein sequence is in uppercase.
How can we do that? The idea is to define an __init__ method at the subclass level that calls the __init__
method of the superclass, and then proceed to actions specific to the DNA or the Protein sequences.
class Sequence:
...
class DNA(Sequence):
...
class Protein(Sequence):
19.4.1.1. Overloading
Let us decide that a generic mw method can be generalized and put at the Sequence class level. This way, a
potential RNA subclas of Sequence will have a pre-defined mw method, common to all the molecular sequences.
We do not need any mw method at the Protein level anymore. But, since the computation of the molecular
weight differs slightly for DNA sequences, we define a specific mw method in the DNA class.
class Sequence:
196
Chapter 19. Object-oriented design
def mw(self):
molW = 0
for c in self.seq:
molW += self.weight[c]
return molW
...
class DNA(Sequence):
def mw(self):
"""
Computes the molecular weight of the 2 strands of a DNA sequence.
"""
molW = 0
for c in self.seq:
molW += self.weight[c]
for c in self.revcompl():
molW += self.weight[c]
return molW
...
When a method is redefined (or overriden) in the subclass, which is the case of the mw method, it is said that the
method is overloaded. This means that, according to the actual class of a sequence instance, which can be either
DNA or Protein, the method actually called can vary.
Overloading
Overloading a method is to redefine at a subclass level a method that exists in upper classes of a class hierarchy.
Another term that is used in object-oriented programming is the term: "polymorphism". "Polymorphism" litteraly
means: several forms. In other words, the name mw has several meanings, depending on the context of the call.
Polymorphism
A method is said to be polymorphic if it has several definitions at different levels of a class hierarchy.
The term "polymorphism" is also used about operators. The ’+’ operator, for instance, is polymorphic in the sense
that it refers to different operations when used with strings and integers.
197
Chapter 19. Object-oriented design
>>> my_dna.gc()
python looks up for the gc method (Figure 19.3). Python finds it in the current class. When looking up for other
methods, such as the __str__ method, which is not defined in the DNA class, Python follows the graph of base
classes (Figure 19.4). Here, we only have one: Sequence, where __str__ is defined.
DNA __dict__
gc
translate
__class__
s1
s1.gc()
198
Chapter 19. Object-oriented design
DNA __dict__
gc
translate
__class__
s1
print s1
Forms of inheritance. There are two forms of inheritance: extension and specialisation. In other words,
inheritance can be used to extend a base class adding functionalities, or to specialise a base class. In the case
of the DNA class, it is rather an extension. Sometimes, the term subclass is criticized because, in the case of
extension, you actually provide more services, rather than a subset. The term subclass fits better to the case of
specialization, where the subclass addresses a specific subset of the base class potential objects (for instance, dogs
are a subset of mammals).
199
Chapter 19. Object-oriented design
UML diagrams. Classes relationships (as well as instances relationships, not represented here) can be represented
by so-called UML diagrams, as illustrated in Figure 19.5.
Sequence
DNA Protein
Testing A
19.4.2. Discussion
200
Chapter 19. Object-oriented design
Benefits of inheritance.
• Regarding flexibility, the inheritance mechanism provides a convenient way to define variants at the method
level: with inheritance indeed, the methods become parameters, since you can redefine any of them. So you
get more than just the possibility of changing a parameter value.
• Regarding reusability, inheritance is very efficient, since the objective is to reuse a code which is already
defined. Components designed with the idea of reuse in mind very often have a clean inheritance hierarchy in
order to provide a way for programmers to adapt the component to their own need.
• Inheritance also provides an elegant extensibility mechanism, by definition. It lets you extend a class without
changing the code of this class, or the code of the module containing the class.
Combining class or combining objects? Using inheritance is not mandatory. The main risk of using it too much
is to get a complex set of classes having a lot of dependencies and that are difficult to understand. There are
generally two possibilities to get things done in object-oriented programming:
• Inheritance: you combine classes in order to get a "rich" class providing a set of services coming from each
combined class.
The use of composition instead of inheritance is also illustrated by the design patterns from [Gamma95], that are
introduced in Section 19.6.
Problem with inheritance for extension. When using inheritance to extend a base class, you might want to have
a method in the subclass not just overloading the method in the base class, but as a complement. In this case,
one usually first calls the base class, and then performs the complementary stuff. In Python, you have to know
explicitely the name of the superclass to perform this call (see for instance method __init__). Be aware, that
this can become rather tricky sometimes, for you have to design a real protocol describing the sequence of calls
that have to be done among classes, when not only one method is involved.
When using inheritance or composition: summary. The question of choosing between inheritance and
composition to combine objects A and B results in deciding whether A is-a B or whether A has-a B. Unfortunately,
it is not always possible to decide about this, only on the basis of the nature of A and B. There a few guidelines,
though (see [Harrison97], chapter 2).
• The main advantage of inheritance over composition is that method binding, e.g lookup and actual method
call, is done automatically: you do not have to perform the method lookup and call, whereas, when combining
objects, you have to know which one has the appropriate method. For instance, a Protein instance may
have a dna if created by translate.
• Use composition when you catch yourself making exceptions to the is-a rule and redefining several inherited
methods or willing to protect access to the base class attributes and/or methods. In such a case, the advantage
described in the previous item of having automatic attribute and method access becomes a problem.
201
Chapter 19. Object-oriented design
• Use composition when the relationships between objects are dynamic. For instance, a different way to design
sequence tools, such as clean, __str__, etc... could be to design a SeqTools class:
class SeqTools:
del clean(self):
seq = ""
for c in self.seqobject.seq:
if c in self.seqobject.alphabet:
seq = seq + c
self.seqobject.seq = seq
def __str__(self):
return ">"+self.seqobject.name+"\n"+self.seqobject.seq
...
The DNA or Protein instances can then have a reference to a SeqTools instance, created at instantiation:
class DNA:
def __str__(self):
return self.toolbox.__str__()
When a program then calls the usual DNA methods, such as:
202
Chapter 19. Object-oriented design
’a’
it first invokes the DNA methods, which just behaves as wrappers for the SeqTools methods.
The main advantage is flexibility: you could more easily redefine which sequence toolbox to use, provided the
public interface is the same, than with inheritance. For this purpose, you could provide a toolbox constructor,
where the class to be used for the tools could be easily changed at runtime:
class DNA:
class DNA(Sequence):
...
• Use composition when a single object must have more than one part of a given type.
• Do not use inheritance when you get too many combined method calls between base class and sub-classes,
which can happen when using inheritance for extension.
• Use inheritance when you want to build an abstract framework, which purpose is to be specialized for various
uses (see Exercise 19.4). A good example is the parsing framework in Biopython, that lets you create as many
new parsers as needed. Inheritance also provides a good mechanism to design several layers of abstraction,
that define interfaces that programmers must follow when developping their components. Bioperl modules,
although not developped in a true object-oriented language, are a good example of this.
203
Chapter 19. Object-oriented design
19.5. Flexibility
19.5.1. Summary of mechanisms for flexibility in Python
Earlier, in Section 19.2.5, we have seen that one of the powerful properties a software piece can have is to be
reusable. We also concluded that the more flexible it is, the more general and thus reusable it will be. Let us now
summarize some mechanisms in object-oriented programming that help in achieving more flexibility.
• Genericity: genericity (as available in the ADA programming language), is a technique to define parameters
for a module, exactly as you define parameters for a function, thus making the module more general. For
instance, you can define the type of an element of a Table module, or the function to move to the next item, as
being generic. There is no specific syntax in Python to define generic components, but in a way, it is not really
necessary because Python is dynamically typed, and can handle functions as parameters for other functions.
• Inheritance: as we have just seen in Section 19.4 this describes the ability, in object-oriented programming, to
derive a class from another, either to extend it or to specialize it.
• Overloading, which refers to the possibility for an operator or a method to behave differently according to
the actual data types of their operands or arguments. This feature does not directly address the problem of
flexibility: it is rather an elegant syntactic mean not to have different operators or names to perform similar
tasks on different objects or sets of objects. In this sense, it is actually a form of polymorphism. In object-
oriented programming, overloading moreover exactly means being able to redefine a method in a derived
class, thus enabling polymorphism of instances: given an instance, it is possible that you do not know exactly
to which class in a hierarchy it belongs (e.g DNA or Protein). In other programming languages, such as
Java, there is another kind of overloading: it is possible, within the same class, to have several definitions of
the same method, but with different signatures, i.e a different set of parameters. This does not exist in Python,
and as described below (see Section 19.5.2), you have to handle it manually.
• Polymorphism: refers to the possibility for something to be of several forms, e.g, for an object to be of any
type. In Python, for instance, lists are polymorphic, since you can put items of any type in a list. Overloading
is one aspect of polymorphism, polymorphism of methods. Polymorphism brings a lot of flexibility, just only
because you do not have to define as many classes as potential data type for it, as you would have to do in
statically typed languages. In this sense, Python helps in designing more simple classes frameworks.
204
Chapter 19. Object-oriented design
Some of these techniques, mainly inheritance, are available in Python, other, such as genericity and overloading,
are not, or in a restricted way.
205
Chapter 19. Object-oriented design
Python is not able to do this analysis automatically, because it is dynamically typed. You do not have any mean
to specify the type of the parameter. In a language providing static typing and full method overloading, i.e also
within the same class, you could have several __getitem__ definitions that would look like:
As you can notice, this is of course not valid Python code, since there is no possibility to define the type of a
parameter in Python.
Computer science is full of design patterns, and there is no exception for object-oriented programming. A catalog
of object-oriented has been published by [Gamma95], and [Christopher2002] has a chapter dedicated to a few
of them, with examples implemented in Python. Design patterns are not programs: they are widely used design
choices to build programs. As such, they form a kind of conceptual catalog that you are encouraged to reuse to
build your application. Not only are they useful in order not to reinvent the wheel and to save your time, but also
because they provide a comprehension framework for programmers who intend to reuse your code and who need
to understand it. This part does not aim at an exhaustive presentation of object-oriented design patterns, which are
very well described elsewhere. Its main purpose is to give a taste of it, with examples in bioinformatics, and to
introduce the main related ideas.
• Structural patterns: patterns that can be used to combine objects and classes in order to build structured objects.
• Behavioral patterns: patterns that can be used to build a computation and to control data flows.
206
Chapter 19. Object-oriented design
Creational patterns. There are two main categories of creational patterns: those for creating objects without
having to know the class name, that you could call "abstract object makers" (abstract factory and factory method),
and those to ensure a certain property regarding object creation, such as prohibiting more than one instance for
a class (singleton), building a set of instances from different classes in a consistent way (builder), or creating an
instance with a specific state (prototype).
• Factory method: a factory method (or a factory function) is a function that creates an object. You don’t
necessarily know the exact name of the class of this object, but you know its interface.
• A function reading a Fasta formatted file may return an instance of the DNA class, of the Protein class,
or any class providing it has a similar interface:
>>> fh = open(myfile)
>>> my_seq = fasta_read(fh)
>>> print my_seq
s1
atgaaa
• For instance, you can create a sequence object (Bio.Seq.Seq in Biopython) by asking the
get_seq_by_num method of an alignment object (Bio.Align.Generic.Alignment):
first_seq = align.get_seq_by_num(0)
The method which creates this instance of Bio.Seq.Seq is a factory method. The difference with a
factory class is also that the factory method is often more than an object maker: it sometimes incorporates
much more knowledge about the way to create the object than a factory would.
• A simple factory method would be a new method defined in a class to create new instances of the same
class:
other_seq = seq.new()
• Abstract factory: an abstract factory is an object maker, where, instead of specifying a class name, you specify
the kind of object you want. It is very similar to an abstract method: just, instead of being a method, it is
a class. For example, the FastaLoader is a factory of sequences. Given a filehandle, it returns a list of
sequence objects of the proper class:
seq_factory = FastaLoader()
fh = open(my_fasta_file)
my_seqs = seq_factory.load(fh)
fh.close()
207
Chapter 19. Object-oriented design
factory = PointFactory()
p1 = factory.create(3,4)
p2 = factory.create(4,5)
p3 = factory.create(4,5)
p = factory.search(3,4)
p.show()
factory.delete(4,5)
Solution 19.1
For instance, say that you want to create an agent to run analysis programs, you can ask a factory to do it for
you:
clustalw = AnalysisFactory.program(’clustalw’)
result = clustalw.run(seqfile = ’myseqs’)
print result.alig
The clustalw object is an instance of, say, the AnalysisAgent.Clustalw class, but you do not have
to know about it at creation time. The only thing you know is the name of the program you want (’clustalw’),
and the factory will do the rest for you.
• Singleton: ensures that you cannot create more than one instance. A class attribute could be used to check the
number of instanciations:
class Singleton:
_nb = 0
def __init__(self):
if Singleton._nb > 0:
raise Exception, "not more than one instance of " + str(self.__class__)
Singleton._nb = 1
208
Chapter 19. Object-oriented design
For example, if you can define a class to contain operations and data for genetic code: you need only one
instance of this class to perform the task. Actually, this pattern would not be implemented with a class in
Python, but rather with a module, at least if you can define it statically (a dynamic singleton could not be a
module, for a module has to be a file):
• Prototype: this pattern also lets you create a new object without knowing its class, but here, the new target
object is created with the same state as the source object:
another_seq = seq.copy()
The interest is that you do not get an "empty" object here, but an object identical to seq. You can thus play
with another_seq, change its attributes, etc... without breaking the original object.
• Builder: you sometimes need to create a complex object composed of several parts. This is the role of the
builder.
• For instance, a builder is needed to build the whole set of nodes and leafs of a tree.
• The Blast parser in Biopython simultaneously instantiates several classes that are all component parts of
of hit: Description, Alignment and Header.
Structural patterns. Structural patterns address issues regarding how to combine and structure objects. For
this reason, several structural patterns provide alternative solutions to design problems that would else involve
inheritance relationships between classes.
• Decorator, proxy, adapter: these patterns all enable to combine two (or more) components, as shown in Figure
19.7. There is one component, A, "in front" of another one, B. A is the visible object a client will see. The role
of A is either to extend or restrict B, or help in using B. So, this pattern is similar to subclassing, except that,
where a sub-class inherits a method from a base class, the decorator delegates to its decoratee when it does not
have the required method. The advantage is flexibility (see Section 19.4.2): you can combine several of these
components in any order at run time without having to create a big and complex hierarchy of subclasses.
209
Chapter 19. Object-oriented design
class A:
def __init__(self, b):
"""storing of the decoratee b (b is an instance of class B)"""
self.b = b
class B:
def f(self):
return "result of f"
>>> b = B()
>>> a = A(b)
>>> print a.f()
’result of f’
Everything that class A cannot perform is forwarded to b (providing that class B knows about it).
• The decorator enables to add functionalities to another object. Example 19.6 shows a very simple decorator
that prints a sequence in uppercase.
210
Chapter 19. Object-oriented design
import string
class UpSeq:
def __str__(self):
return string.upper(self.seqobj.seq)
def __getattr__(self,attr):
return getattr(self.seqobj, attr)
seq.f()
without actually defining all the possible f methods within the sequence class?
• The proxy rather handles the access to an object. There are several kinds of proxy:
211
Chapter 19. Object-oriented design
import UserList
class ImmutableList(UserList.UserList):
def reverse(self):
raise ValueError, "operation not supported"
def sort(self):
raise ValueError, "operation not supported"
l = ImmutableList([3, 4, 5])
try:
l.append(10)
except ValueError, e:
print e
You could now consider that a list your program is managing can be temporarily made mutable. In
contrast with the example above which is using inheritance, you can use the following class definition:
class ImmutableList:
212
Chapter 19. Object-oriented design
def reverse(self):
raise ValueError, "operation not supported"
def sort(self):
raise ValueError, "operation not supported"
l1 = [3, 4, 5]
l2 = ImmutableList(l1)
# by this component
other_function(l2)
l1.append(8)
213
Chapter 19. Object-oriented design
del seq[i]
seq[i] = ’A’
on a ’N’ character?
• virtual proxy: to physically fetch data only when needed. Database dictionaries in Biopython work this
way:
prosite = Bio.Prosite.ExPASyDictionary()
entry = prosite[’PS00079’]
• The adapter (or wrapper) helps in connecting two components that have been developped independantly
and that have a different interface.
For instance, the Pise package transforms Unix programs interfaces in standardized interfaces, either
Web interfaces, or API. For instance, the golden Unix command has the following interface:
But the Pise wrapper enables to run it and get the result by a Python program, having an interface defined
in the Python language:
factory = PiseFactory()
golden = factory.program("golden",db="embl",query="MMVASP")
job = golden.run()
print job->content()
• Collection: this is a general pattern on how to collect items. You may need your own collection classes in
numerous cases where you have several items to manage as a set, either ordered (list-like) or not ordered but
accessible through keys (dictionary-like). Generally, you will need accessors to access the items, in various
way:
214
Chapter 19. Object-oriented design
my_collection[i]
or by a key:
my_collection[key]
• by a search on values:
my_collection.search(value)
• printing:
def __str__(self):
output = ""
for x in my_collection:
output = output + str(x)
return output
(as you can notice, the collection’s __str__ method in turn invokes the items’ __str__ method, since
the python str() function does invoke this special method when defined)
• filtering:
my_collection.save(fh)
You will also have a set of modificators, defined with your own specific rules:
215
Chapter 19. Object-oriented design
• adding/removing elements:
my_collection.append(value)
(here, for instance, you can check that the collection has unique values, or that the item is added at the
right place to keep the collection sorted, etc...)
my_collection.remove(value)
or
del my_collection[key]
• modification, but also creation, can be enabled by assignation if the collection has a dictionary behaviour:
my_collection[i] = value
or assignation of an element and its key:
my_collection[key] = value
my_collection.load(fh)
• re-ordering:
my_collection.sort()
or:
my_collection.reverse()
216
Chapter 19. Object-oriented design
# loading
fh = open(fastafile)
seqdb = DnaDB(fh)
fh.close()
# searching
l = seqdb.search(’cctac’)
for seq in l:
print l
# modifying:
seqdb[’new_seq’] = ’ccaggaggccctggcctctcactgaacccggccactcctctttggc’
print seqdb[’new_seq’]
# output the whole db:
print seqdb
# saving:
fh = open(fastafile, ’w’)
seqdb.save(fh)
fh.close()
class SequenceDB:
if seq != ”:
self[name] = seq
217
Chapter 19. Object-oriented design
def __str__(self):
result = ""
for seq in self._db.values():
result += str(seq) + "\n"
return result[:-1]
class DnaDB(SequenceDB):
class ProteinDB(SequenceDB):
As you can notice, this example is not only a collection, it illustrates in fact several patterns. The decorator
since SequenceDB is composed with a Python dictionary and delegates most native dictionary methods
through the __getattr__ special method. Thanks to this delegation, you can do anything a dictionary
could do with your database, although it does not use inheritance. Its also an abstract factory for the class
creates of set of objects which class does not have to be specified. It also uses the protection proxy, since the
__setitem__ method does not allow to override an existing entry.
• Composite: this pattern is often used to handle complex composite recursive structures. Example 19.9 shows
a set of classes for a tree structure, illustrated in Figure 19.8. The main idea of the composite design pattern
is to provide an uniform interface to instances from different classes in the same hierarchy, where instances
are all components of the same composite complex object. In Example 19.9, you have two types of nodes:
Node and Leaf, but you want a similar interface for them, that is at least defined by a common base class,
AbstractNode, with two operations: print subtree. These operations should be callable on any node
instance, without knowing its actual sub-class.
218
Chapter 19. Object-oriented design
>>> print t3
(A: 0.71399, ((B: -0.00804, C: 0.0747): 0.15685, D: -0.04732): 0.0666)
>>> t4 = t3.right.subtree()
>>> print t4
((B: -0.00804, C: 0.0747): 0.15685, D: -0.04732)
>>> t5 = t3.left.subtree()
>>> print t5
’A’: 0.71399
❶
class AbstractNode:
def __str__(self):
pass
def subtree(self):
pass
❷
class Node(AbstractNode):
def __init__(self, left=None, right=None, length=None):
219
Chapter 19. Object-oriented design
self.left=left
self.right=right
self.length=length
def __str__(self):
if self.length:
return "(" + self.left.__str__() + ", " + self.right.__str__() + ")" + ": " + str(self.length
else:
return "(" + self.left.__str__() + ", " + self.right.__str__() + ")"
def subtree(self): ❸
return Node(self.left, self.right)
class Leaf(AbstractNode):
def __init__(self, name, length=None):
self.name = name
self.length=length
self.left = None
self.right = None
def __str__(self):
return self.name + ": " + str(self.length)
def subtree(self):
return Leaf(self.name, self.length)
if __name__ == "__main__":
t1 = Leaf( ’A’, 0.71399)
print t1
t2 = Node (Leaf(’B’, -0.00804),
Leaf(’C’, 0.07470))
print t2
t3 = Node ( Leaf ( ’A’, 0.71399),
Node ( Node ( Leaf(’B’, -0.00804),
Leaf(’C’, 0.07470),
0.15685),
Leaf (’D’, -0.04732),
0.0666),
)
print t3
220
Chapter 19. Object-oriented design
❶ Abstract class AbstractNode, base class for both Node and Leaf
❷ Internal nodes are instances of Node class.
❸ Leafs are instances of Leaf class.
Behavioral patterns. Patterns of this category are very useful in sequence analysis, where you often have to
combine algorithms and to analyze complex data structure in a flexible way.
• Template: this pattern consists in separating the invariant part of an algorithm from the variant part. In a sorting
procedure, you can generally separate the function which compares items from the main body of the algorithm.
The template method, in this case, is the sort() method, whereas the compare() can be defined by each
subclass depending on its implementation or data types.
The following function defines a typical search, which objective is to verify a property on the whole sequence.
So it stops whenever a property is not verified on a single element of the sequence. The structure of this search
is a kind of template.
This type of search is so common that you could imagine to provide an boolean function to this function for
the control to perform, in order to have a generic function.
A related template would be to search in the sequence whether a propoerty is verified at least once:
• Strategy: the boolean function in the examples above can be called a strategy, because it provides the logic
which decides for the result of the search. Examples of such function are listed below:
def is_dna(c):
return c in ’atcgATCG’
def is_gap(c):
return c in ’-.’
def is_amino_acid(c):
AA = upper(c)
if AA < ’A’ or AA > ’Z’:
return False
221
Chapter 19. Object-oriented design
if AA in ’JOU’:
return False
return True
def is_upper(c):
return c == upper(c)
def is_letter(c):
return c in ascii_letters
check_whole_seq(’tgtcgt’, is_dna)
check_whole_seq(’MAI’, is_amino_acid)
check_has_seq(’tg-tcgt’, is_gap)
class MotifDB:
Applied to all elements of a set, this function is provided as a parameter to filter the elements. It can be used
the following way:
where only elements of this motifs databse containing ’kinase’ in their description will be returned.
• Iterator: an iterator is an object that let you browse a sequence of items from the beginning to the end.
Generally, it provides:
222
Chapter 19. Object-oriented design
Usually, one distinguishes internal versus external iterators. An external iterator is an iterator which enables
to do a for or a while loop on a range of values that are returned by the iterator:
for e in l.elements():
f(e)
or:
i = l.iterator()
e = i.next()
while e:
f(e)
e = i.next()
In the above examples, you control the loop. On the other hand, an internal iterator just lets you define a
function or a method (say, f, called a visitor, see below) to apply to all elements:
l.iterate(f)
In the Biopython package, files and databases are generally available through an iterator.
handle = open(...) ❶
iter = Bio.Fasta.Iterator(handle) ❷
seq = iter.next() ❸
while seq:
print seq.name
print seq.seq ❹
seq = iter.next()
handle.close()
223
Chapter 19. Object-oriented design
• Visitor: this pattern is useful to specify a function that will be applied on each item of a collection. The Python
map function provides a way to use visitors, such as the f function, which visits each item of the l list in turn:
The map is an example of an internal iterator (with the f function as a visitor). The f_test function in the
MotifDB class above is also a visitor. A visitor is also a strategy that applies to all the elements of a set.
def output(e):
print e
Similarly, the following binary traversal takes a function as a parameter (make>), which purpose is to build
the result of the traversal.
def build_list(*l):
if len(l) == 1:
224
Chapter 19. Object-oriented design
return [l[0]]
_l = []
for e in l:
_l = _l + e
return _l
t1 = binary_traverse(tree, build_list)
[’human’] [’chimpanzee’]
[’human’, ’chimpanzee’] [’gorilla’]
[’human’, ’chimpanzee’, ’gorilla’] [’orang-utan’]
[’human’, ’chimpanzee’, ’gorilla’, ’orang-utan’] [’gibbon’]
def build_string(*l):
if len(l) == 1:
return str(l[0])
_s = "("
for e in l:
_s = _s + str(e) + ","
return _s[:-1] + ")"
t2 = binary_traverse(tree, build_string)
human chimpanzee
(human,chimpanzee) gorilla
((human,chimpanzee),gorilla) orang-utan
(((human,chimpanzee),gorilla),orang-utan) gibbon
• Observer The observer pattern provides a framework to maintain a consistent distributed state between loosely
coupled components. One agent, the observer, is in charge of maintaining a list of subscribers, e.g components
that have subscribed to be informed about changes in a given state. Whenever a change occurs in a state, the
observer has to inform each subscriber about it.
A well-known example is the Model-View-Controller [Krasner88] framework. The view components, the ones
who actually display data, subscribe to "edit events" in order to be able to refresh and redisplay them whenever
a change occurs.
19.7. Solutions
Solution 19.1.
Exercise 19.5
class PointFactory:
def __init__(self):
self._points = {}
225
Chapter 19. Object-oriented design
if self._points.has_key((x,y)):
return self._points[(x,y)]
point = Point(x,y)
self._points[(x,y)] = point
return point
226
Chapter 19. Object-oriented design
Bibliography
Python Books
[Beazley2001] Python. Essential Reference. 2. David M. Beazley. 0-7357-1091-0. New Riders. 2001.
[Christopher2002] Python Programming Patterns. Thomas W Christopher. 0-13-040956-1. Prentice Hall. 2002.
[Deitel2002] Python. How to program. H. M. Deitel, P. J. Deitel, J. P. Liperi, and B. A. Wiederman. 0-13-092361-
3. Prentice Hall. 2002.
[Downey2002] How to Think Like a Computer Scientist. Learining with Python. Allen Downey, Jeffrey
Elkner, and Chris Meyers. 0-97167775-0-6. Green Tea Press. 2002. www.thinkpython.com
[http://www.thinkpython.com].
[Grayson2000] Python and Tkinter Programming. John E. Grayson. 1-884777-81-3. Manning Publications Co..
2000.
[LutzAscher2001] Learning Python. Mark Lutz and David Ascher. 1-56592-464-9. O’Reilly. 2001.
[Martelli2002] Python Cookbook. Alex Martelli and David Ascher. 0-596-00167-3. O’Reilly. 2002.
[Zelle2003] Python Programming. An introduction to computer science. John Zelle. 1887902996. Franklin,
Beedle and Associates, Inc.. 2003. http://mcsp.wartburg.edu/zelle/python/ .
Other Books
[Meyer97] Object Oriented software Construction. Bertrand Meyer. 0-13-629155-4. Prentice Hall Professional
Technical Reference. 1997.
[Booch94] Object-Oriented Analysis and Design with Applications. Grady Booch. Addison-Wesley Object
Technology Series. 1994.
[Gamma95] Design Patterns, Elements of Reusable Object-Oriented Software. Erich Gamma, Richard Helm,
Ralph Johnson, and John Vlissides. Addison Wesley. 1995.
[Mangalam2002] The Bio* toolkits - a brief overview. “Briefings in Bioinformatics”. Harry Mangalam. 296-302.
2002. http://bib.oxfordjournals.org/cgi/content/abstract/3/3/296 .
227
Chapter 19. Object-oriented design
[Rossum99] Computer Programming for Everybody. Guido van Rossum. 1999. http://www.python.org/doc/
essays/cp4e.html .
[Wang2000] A Qualitative Analysis of the Usability of Perl, Python, and Tcl. Proceedings of The Tenth
International Python Conference. Lingyun Wang and Phil Pfeiffer. 2002.
228