python for biologists
Short Description
computer programming, bio-python, a programming course for complete beginners. martin jones, pdf....
Description
i
Copyright Copyright © 2013 Dr. Martin Jones
This work is licensed licensed under a Creative Commons AttributionAttribution-NonCommer NonCommercialcialShareAlike 3.0 Unported License. License .
For more information, visit http://pythonforbioogists.!om "et in #$ "erif an% Source Code Pro
ii
About the author Martin starte% his programming !areer by earning #er %&ring the !o&rse of his #hD in evo&tionary bioogy, an% starte% tea!hing other peope to program soon after. "in!e then he has ta&ght intro%&!tory intro%&!tor y programming to h&n%re%s of bioogists, from &n%ergra%&ates to #'s, an% has maintaine% a phiosophy that programming !o&rses m&st be frien%y, approa!habe, approa!habe, an% pra!ti!a. Martin has ta&ght intro%&!tory programming as part of the (ioinformati!s M"! !o&rse at )%inb&rgh *niversity for the past five years, an% is !&rrenty +e!t&rer in (ioinformati!s.
iii
Preface e!ome to #ython for (ioogists. (efore yo& rea% any f&rther, ma-e s&re that this is the most re!ent version of the boo-. #ython for (ioogists is being !ontin&ay &p%ate% an% improve% to ta-e into a!!o&nt !orre!tions, amen%ments an% !hanges to #ython itsef, so its important that yo& are rea%ing the most &pto%ate version. $his fie is revision n&mber 189. $he n&mber of the most re!ent revision !an aays be fo&n% at: http://pythonforbioogists.!om/in%e.php/version/ 'f the revision n&mber iste% at the *+ is higher than the one in bo%, then this is an o&tof%ate o&tof%ate !opy, an% yo& nee% to %onoa% the atest version from http://pythonforbioogists.!om o& noti!e from the !opyright page that the !ontents of this boo- are i!ense% &n%er a Creative Commons 4ttrib&tion 4ttrib&tion "hare4i-e "hare 4i-e i!ense. $his means that yo&re free to %o hat yo& i-e ith it 5 !opy !opy it, emai it to yo&r frien%s, apaper yo&r ab ith it 5 as ong as yo& -eep the attrib&tion. o& !an aso mo%ify it, as ong as yo& i!ense yo&r mo%ifi!ation &n%er the same terms. $he ony thing that the i!ense %oesnt ao is !ommer!ia &se 5 if yo&% i-e to &se the !ontents of this !o&rse for for !ommer!ia p&rposes, get in to&!h ith me at martin6pythonforbioogists.!om
7appy programming8
iv
Table of Contents About the author » ii Preface » iii
1: Introduction and environment
1
Why have a programming book for biologists? » 1 Why Python? » 2 How to use this book » 5 !ercises an" solutions » # $etting in touch » % &etting up your environment » % 'e!t e"itors » 11 (ea"ing the "ocumentation » 12
2: Printing and manipulating text
13
Why are we so intereste" in working with te!t? » 1) Printing a message to the screen » 1* +uotes are important » 15 ,se comments to annotate your co"e » 1 rror messages an" "ebugging » 1% Printing special characters » 21 &toring strings in variables » 21 'ools for manipulating strings » 2* (ecap » )* !ercises » )&olutions » ).
3: Reading and riting files Why are we so intereste" in working with files? » 52 (ea"ing te!t from a file » 5) /iles0 contents an" file names » 55 ealing with newlines » 5# issing files » -3
!2
v
Writing te!t to files » -3 4losing files » -) Paths an" fol"ers » -) (ecap » -* !ercises » -5 &olutions » -#
": #ists and loops
$"
Why "o we nee" lists an" loops? » #* 4reating lists an" retrieving elements » #Working with list elements » ## Writing a loop » #. n"entation errors » %2 ,sing a string as a list » %) &plitting a string to make a list » %* terating over lines in a file » %* 6ooping with ranges » %5 (ecap » %# !ercises » %. &olutions » .3
!: %riting our on functions Why "o we want to write our own functions? » . . efining a function » 133 4alling an" improving our function » 13) ncapsulation with functions » 135 /unctions "on7t always have to take an argument » 13 /unctions "on7t always have to return a value » 13% /unctions can be calle" with name" arguments » 13% /unction arguments can have "efaults » 113 'esting functions » 111 (ecap » 11) !ercises » 115 &olutions » 11-
99
vi
&: Conditional tests
121
Programs nee" to make "ecisions » 121 4on"itions0 'rue an" /alse » 121 if statements » 12* else statements » 125 elif statements » 12while loops » 12% 8uil"ing up comple! con"itions » 12% Writing true9false functions » 1)3 (ecap » 1)1 !ercises » 1)) &olutions » 1)5
$: Regular expressions
1"1
'he importance of patterns in biology » 1*1 o"ules in Python » 1*) (aw strings » 1** &earching for a pattern in a string » 1*5 !tracting the part of the string that matche" » 153 $etting the position of a match » 152 &plitting a string using a regular e!pression » 15) /in"ing multiple matches » 15* (ecap » 155 !ercises » 15# &olutions » 15%
8: 'ictionaries
1&8
&toring paire" "ata » 1-% 4reating a "ictionary » 1#) terating over a "ictionary » 1#. (ecap » 1%2 !ercises » 1%) &olutions » 1%*
9: (iles) programs) and user input
19!
vii
/ile contents an" manipulation » 1.5 8asic file manipulation » 1. eleting files an" fol"ers » 1.% 6isting fol"er contents » 1.% (unning e!ternal programs » 1.. (unning a program » 233 &aving program output » 231 ,ser input makes our programs more fle!ible » 231 nteractive user input » 23) 4omman" line arguments » 23* (ecap » 235 !ercises » 23# &olutions » 23%
1
Chapter 1: 'ntro%&!tion an% environment
1:
Introduction and environment
Why have a programming book for biologists? 'f yo&re rea%ing this boo-, then yo& probaby %ont nee% to be !onvin!e% that programming is be!oming an in!reasingy essentia part of the too -it for bioogists of a types. o& might, hoever, nee% to be !onvin!e% that a boo- i-e this one, %eveope% espe!iay for bioogists, !an %o a better 9ob of tea!hing yo& to program than a generap&rpose intro%&!tory programming boo-. 7ere are a fe of the reason hy ' thin- that is the !ase. 4 bioogyspe!ifi! programming boo- aos &s to &se eampes an% eer!ises that &se bioogi!a probems. $his serves to important p&rposes: firsty, it provi%es motivation an% %emonstrates the types of probems that programming !an hep to sove. )perien!e has shon that beginners ma-e m&!h better progress hen they are motivate% by the tho&ght of ho the programs they rite i ma-e their ife easier8 "e!on%y, by &sing bioogi!a eampes, the !o%e an% eer!ises thro&gho&t the boo- !an form a ibrary of &sef& !o%e snippets, hi!h e !an refer ba!- to hen e ant to sove reaife probems. 'n bioogy, as in a fie%s of programming, the same probems ten% to re!&r time an% time again, so its very &sef& to have this !oe!tion of eampes to a!t as a referen!e 5 something thats not possibe ith a generap&rpose programming boo-. 4 bioogyspe!ifi! programming boo- !an aso !on!entrate on the feat&res of the ang&age that are most &sef& to bioogists. 4 ang&age i-e #ython has many feat&res an% in the !o&rse of earning it e inevitaby have to !on!entrate on some an% miss others o&t. $he set of feat&res hi!h are important to &s in bioogy are sighty %ifferent to those hi!h are most &sef& for generap&rpose programming 5 for eampe, e are m&!h more intereste% in manip&ating tet in!&%ing things i-e D;4 an% protein serong> programming ang&age is very &ni-ey to mean the %ifferen!e beteen fai&re an% s&!!ess hen earning. ?ther fa!tors motivation, having time to %evote to earning, hepf& !oeag&es= are far more important, yet re!eive ess attention. $he reason that peope pa!e so m&!h eight on the :what language shoul" learn?: . ?n in%os, it is -non as >!omman% prompt>. $o r&n a #ython program, e 9&st type the path to the #ython ee!&tabe fooe% by the name of the fie that !ontains the !o%e e ant to r&n B. ?n a +in& or Ma! ma!hine, the path i be something i-e: usrlocal-inpython
?n in%os, it i be something i-e: c!Python27python
1 2 3 B
http://.python.org/getit/ http://.python.org/getit/ma!/ http://.python.org/getit/in%os/ hen e refer to >a #ython program> in this boo-, e are &s&ay ta-ing abo&t the tet fie that ho%s the !o%e.
10
Chapter 1: 'ntro%&!tion an% environment
$o r&n a #ython program, its generay easiest to be in the same fo%er as it. (y !onvention, #ython programs are given the etension .py, so to r&n a program !ae% test.py, e 9&st type: usrlocal-inpython test.py
$here are a !o&pe of tri!-s that !an be &sef& hen eperimenting ith programs 1. Firsty, yo& !an r&n #ython in an intera!tive or >she>= mo%e by r&nning it itho&t the name of a program fie. $his aos yo& to type in%ivi%&a statements an% see the res&t straight aay. "e!on%y, yo& !an r&n #ython ith the -i option, hi!h i !a&se it to r&n yo&r program an% t+en enter intera!tive mo%e. $his !an be han%y if yo& ant to eamine the state of variabes after yo&r !o%e has r&n.
P*t+on 2 vs, P*t+on 3 4s i is &se% for any o!!asion hen o&r program o&tp&ts some tet 5 in this !ase, the o&tp&t is %ispaye% in yo&r termina. $his is not stri!ty tr&e, b&t its easier to 9&st foo this r&e than orry abo&t the e!eptions. $here are severa %ifferent types of bra!-ets in #ython, so for !arity e i aays refer to parentheses hen e mean these: =, s;uare brackets hen e mean these: N an% curly brackets hen e mean these: OP
1
Chapter 2: #rinting an% manip&ating tet
$he arg&ments te #ython hat e ant to %o more spe!ifi!ay 5 in this !ase, the arg&ment tes #ython ea!ty hat it is e ant to print: a frien%y greeting. 4ss&ming yo&ve fooe% the instr&!tions in !hapter 1 an% set &p yo&r #ython environment, type the ine of !o%e above into yo&r favo&rite tet e%itor, save it, an% r&n it. o& sho&% see a singe ine of o&tp&t i-e this: ello orld
L&otes are important 'n norma riting, e ony s&rro&n% a bit of tet in
View more...
Comments