• No results found

Complex condition

In document python for biologists (Page 143-150)

#rint o&t the gene names for a genes hose name begins ith >-> or >h> e!ept those beonging to rosophila melanogaster .

7ig+ lo medium

For ea!h gene, print o&t a message giving the gene name an% saying hether its 4$ !ontent is high greater than 0.G=, o ess than 0.B= or me%i&m beteen 0.B an% 0.G=.

13 ChapterG:Con%itionatests

&olutions

everal species

$hese eer!ises are somehat more !ompi!ate% than previo&s ones, an% theyre going to re<&ire materia from m&tipe %ifferent !hapters to sove. $he first

probem is to %ea ith the format of the %ata fie. ?pen it &p in a tet e%itor an% ta-e a oo- before !ontin&ing.

e -no that ere going to have to open the fie !hapter 3= an% pro!ess the !ontents inebyine !hapter B=. $o %ea ith ea!h ine, e have to spit it to ma-e a ist of !o&mns !hapter B=, then appy the !on%ition this !hapter= in or%er to fig&re o&t hether or not e sho&% print it. 7eres a program that i rea% ea!h ine from the fie, spit it &sing !ommas as the %eimiter, then assign ea!h of the fo&r !o&mns to a variabe an% print the gene name:

data B open(*data.cs*# for line in data!

columns B line.rstrip(*n*#.split(*,*# species B columns$0 seuence B columns$1 name B columns$2 expression B columns$3 print(name#

;oti!e that e &se rstrip to remove the neine from the en% of the !&rrent ine before spitting it. e -no the or%er of the fie%s in the ine be!a&se they ere mentione% in the eer!ise %es!ription, so e !an easiy assign them to the fo&r  variabes. $his program %oesnt %o anything &sef&, b&t e !an !he!- the o&tp&t to

13G ChapterG:Con%itionatests :dy"'7 Ddg7"" :dy33 hdt73J hdu0' teg'3"

;o e !an a%% in the !on%ition. e ant to print the name if the spe!ies is eit+er  rosophila melanogaster or rosophila simulans. 'f the spe!ies name is neit+er of

those to, then e %ont ant to %o anything. $his is a yes9no type %e!ision, so e nee% an if statement:

data B open(*data.cs*# for line in data!

columns B line.rstrip(*n*#.split(*,*# species B columns$0

seuence B columns$1 name B columns$2

expression B columns$3

if species BB *@rosophila melanogaster* or species BB *@rosophila simulans*!

print(name#

$he ine !ontaining the if statement is <&ite ong, so it raps aro&n% onto the net ine on this page, b&t its sti 9&st a singe ine in the program fie. e !an !he!- the o&tp&t e get:

:dy"'7 Ddg7"" :dy33

13H ChapterG:Con%itionatests

#engt+ range

e !an re&se a arge part of the !o%e from the previo&s eer!ise to hep sove this one. e have another !ompe !on%ition: e ony ant to print names for genes  hose ength is beteen @0 an% 110 bases 5 in other or%s, genes hose ength is

greater than @0 and ess than 110. e have to !a!&ate the ength &sing the len f&n!tion. ?n!e eve %one that the rest of the program is <&ite straightforar%:

data B open(*data.cs*# for line in data!

columns B line.rstrip(*n*#.split(*,*# species B columns$0

seuence B columns$1 name B columns$2

expression B columns$3

if len(seuence# + J0 and len(seuence# ; 110! print(name#

4T content

$his eer!ise has a !ompe !on%ition i-e the others, b&t it aso re<&ires &s to %o a bit more !a!&ation 5 e nee% to be abe to !a!&ate the 4$ !ontent of ea!h

se<&en!e. ather than starting from s!rat!h, e simpy &se the f&n!tion that e  rote in the previo&s !hapter an% in!&%e it at the start of the program. ?n!e eve

%one that, its 9&st a !ase of &sing the o&tp&t from get_at_content as part of the !on%ition:

13I ChapterG:Con%itionatests

4 our function to get A) content def get/at/content(dna#!

length B len(dna#

a/count B dna.upper(#.count(A# t/count B dna.upper(#.count()#

at/content B (a/count C t/count#  length return at/content

data B open(*data.cs*# for line in data!

columns B line.rstrip(*n*#.split(*,*# species B columns$0

seuence B columns$1 name B columns$2

expression B columns$3

if get/at/content(seuence# ; 0. and expression + 200! print(name#

Complex condition

$here are no !a!&ations to !arry o&t for this eer!ise 5 the !ompeity !omes from the fa!t that there are three !omponents to the !on%ition, an% they have to be

9oine% together in the right ay:

data B open(*data.cs*# for line in data!

columns B line.rstrip(*n*#.split(*,*# species B columns$0

seuence B columns$1 name B columns$2

expression B columns$3

if (name.startsith(:# or name.startsith(h## and species 5B *@rosophila melanogaster*!

print(name#

$he ine !ontaining the if statement is <&ite ong, so it raps aro&n% onto the net ine on this page, b&t its sti 9&st a singe ine in the program fie. $here are to

13@ ChapterG:Con%itionatests

%ifferent ays to epress the re<&irement that the name is not rosophila melanogaster= 'n the above eampe eve &se% the note<&as sign 8Z= b&t e !o&% aso have &se% the not booean operator:

if (name.startsith(:# or name.startsith(h## and not species BB *@rosophila melanogaster*!

7ig+ lo medium

;o e !ome to an eer!ise that re<&ires the &se of m&tipe bran!hes. e have three %ifferent printing options for ea!h gene 5 high, o an% me%i&m 5 so e nee% an if..elif..else se!tion to han%e the !on%itions. e &se the

get_at_content f&n!tion as before:

4 our function to get A) content def get/at/content(dna#!

length B len(dna#

a/count B dna.upper(#.count(A# t/count B dna.upper(#.count()#

at/content B (a/count C t/count#  length return at/content

data B open(*data.cs*# for line in data!

columns B line.rstrip(*n*#.split(*,*# species B columns$0 seuence B columns$1 name B columns$2 expression B columns$3 if get/at/content(seuence# + 0."!

print(name C * has high A) content*# elif get/at/content(seuence# ; 0.'!

print(name C * has lo A) content*# else!

1B0 ChapterG:Con%itionatests

Che!-ing the o&tp&t !onfirms that the !on%itions are or-ing:

:dy"'7 has high A) content Ddg7"" has medium A) content :dy33 has medium A) content hdt73J has lo A) content hdu0' has medium A) content teg'3" has medium A) content

1B1 ChapterH:eg&arepressions

In document python for biologists (Page 143-150)