python for biologists

Share Embed Donate


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://pythonforbioogists.!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 bioogy, an% starte% tea!hing other peope to program soon after. "in!e then he has ta&ght intro%&!tory intro%&!tor y programming to h&n%re%s of bioogists, from &n%ergra%&ates to #'s, an% has maintaine% a phiosophy that programming !o&rses m&st be frien%y, approa!habe, approa!habe, 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 !&rrenty +e!t&rer in (ioinformati!s.

iii

 Preface e!ome to #ython for (ioogists. (efore yo& rea% any f&rther, ma-e s&re that this is the most re!ent version of the boo-. #ython for (ioogists is being !ontin&ay &p%ate% an% improve% to ta-e into a!!o&nt !orre!tions, amen%ments an% !hanges to #ython itsef, so its important that yo& are rea%ing the most &pto%ate version. $his fie is revision n&mber 189. $he n&mber of the most re!ent revision !an aays be fo&n% at: http://pythonforbioogists.!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&tof%ate o&tof%ate !opy, an% yo& nee% to %onoa% the atest version from http://pythonforbioogists.!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 "hare4i-e "hare 4i-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, apaper yo&r ab ith it 5 as ong as yo& -eep the attrib&tion. o& !an aso mo%ify it, as ong as yo& i!ense yo&r mo%ifi!ation &n%er the same terms. $he ony thing that the i!ense %oesnt ao 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 martin6pythonforbioogists.!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 on 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& probaby %ont nee% to be !onvin!e% that programming is be!oming an in!reasingy essentia part of the too -it for bioogists of a types. o& might, hoever, nee% to be !onvin!e% that a boo- i-e this one, %eveope% espe!iay for bioogists, !an %o a better 9ob of tea!hing yo& to program than a generap&rpose intro%&!tory programming boo-. 7ere are a fe of  the reason hy ' thin- that is the !ase. 4 bioogyspe!ifi! programming boo- aos &s to &se eampes an% eer!ises that &se bioogi!a probems. $his serves to important p&rposes: firsty, it provi%es motivation an% %emonstrates the types of probems that programming !an hep to sove. )perien!e has shon 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 bioogi!a eampes, the !o%e an% eer!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 sove reaife probems. 'n bioogy, as in a fie%s of programming, the same probems ten% to re!&r time an% time again, so its very &sef& to have this !oe!tion of eampes to a!t as a referen!e 5 something thats not possibe ith a generap&rpose programming boo-. 4 bioogyspe!ifi! programming boo- !an aso !on!entrate on the feat&res of the ang&age that are most &sef& to bioogists. 4 ang&age i-e #ython has many feat&res an% in the !o&rse of earning it e inevitaby have to !on!entrate on some an% miss others o&t. $he set of feat&res hi!h are important to &s in bioogy are sighty %ifferent to those hi!h are most &sef& for generap&rpose programming 5 for eampe, e are m&!h more intereste% in manip&ating tet in!&%ing things i-e D;4 an% protein serong> programming ang&age is very &ni-ey to mean the %ifferen!e beteen fai&re an% s&!!ess hen earning. ?ther fa!tors motivation, having time to %evote to earning, hepf& !oeag&es= are far more important, yet re!eive ess attention. $he reason that peope pa!e so m&!h eight on the :what language shoul"  learn?: . ?n in%os, it is -non as >!omman% prompt>. $o r&n a #ython program, e 9&st type the path to the #ython ee!&tabe fooe% by the name of the fie 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: usrlocal-inpython

?n in%os, it i be something i-e: c!Python27python

1 2 3 B

http://.python.org/getit/ http://.python.org/getit/ma!/ http://.python.org/getit/in%os/ hen e refer to >a #ython program> in this boo-, e are &s&ay ta-ing abo&t the tet fie that ho%s the !o%e.

10

Chapter 1: 'ntro%&!tion an% environment

$o r&n a #ython program, its generay easiest to be in the same fo%er as it. (y !onvention, #ython programs are given the etension .py, so to r&n a program !ae% test.py, e 9&st type: usrlocal-inpython test.py

$here are a !o&pe of tri!-s that !an be &sef& hen eperimenting ith programs 1. Firsty, yo& !an r&n #ython in an intera!tive or >she>= mo%e by r&nning it itho&t the name of a program fie. $his aos yo& to type in%ivi%&a statements an% see the res&t straight aay. "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 eamine the state of variabes 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 tet 5 in this !ase, the o&tp&t is %ispaye% in yo&r termina. $his is not stri!ty tr&e, b&t its easier to 9&st foo 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 aays 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 tet

$he arg&ments te #ython hat e ant to %o more spe!ifi!ay 5 in this !ase, the arg&ment tes #ython ea!ty hat it is e ant to print: a frien%y greeting. 4ss&ming yo&ve fooe% 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 tet e%itor, save it, an% r&n it. o& sho&% see a singe ine of o&tp&t i-e this: ello orld

L&otes are important 'n norma riting, e ony s&rro&n% a bit of tet in
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF