#rint o&t the gene names for a genes hose name begins ith >-> or >h> e!ept those beonging 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 beteen 0.B an% 0.G=.
13 ChapterG:Con%itionatests
&olutions
everal species
$hese eer!ises are somehat more !ompi!ate% than previo&s ones, an% theyre going to re<&ire materia from m&tipe %ifferent !hapters to sove. $he first
probem is to %ea ith the format of the %ata fie. ?pen it &p in a tet e%itor an% ta-e a oo- before !ontin&ing.
e -no that ere going to have to open the fie !hapter 3= an% pro!ess the !ontents inebyine !hapter B=. $o %ea ith ea!h ine, e have to spit it to ma-e a ist of !o&mns !hapter B=, then appy the !on%ition this !hapter= in or%er to fig&re o&t hether or not e sho&% print it. 7eres a program that i rea% ea!h ine from the fie, spit it &sing !ommas as the %eimiter, then assign ea!h of the fo&r !o&mns to a variabe an% print the gene name:
data B open(*data.cs*# for line in data!
columns B line.rstrip(*n*#.split(*,*# species B columns$0 seuence B columns$1 name B columns$2 expression B columns$3 print(name#
;oti!e that e &se rstrip to remove the neine from the en% of the !&rrent ine before spitting it. e -no the or%er of the fie%s in the ine be!a&se they ere mentione% in the eer!ise %es!ription, so e !an easiy assign them to the fo&r variabes. $his program %oesnt %o anything &sef&, b&t e !an !he!- the o&tp&t to
13G ChapterG:Con%itionatests :dy"'7 Ddg7"" :dy33 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 to, then e %ont 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
seuence 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 net ine on this page, b&t its sti 9&st a singe ine in the program fie. e !an !he!- the o&tp&t e get:
:dy"'7 Ddg7"" :dy33
13H ChapterG:Con%itionatests
#engt+ range
e !an re&se a arge part of the !o%e from the previo&s eer!ise to hep sove this one. e have another !ompe !on%ition: e ony ant to print names for genes hose ength is beteen @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 eve %one that the rest of the program is <&ite straightforar%:
data B open(*data.cs*# for line in data!
columns B line.rstrip(*n*#.split(*,*# species B columns$0
seuence B columns$1 name B columns$2
expression B columns$3
if len(seuence# + J0 and len(seuence# ; 110! print(name#
4T content
$his eer!ise has a !ompe !on%ition i-e the others, b&t it aso re<&ires &s to %o a bit more !a!&ation 5 e nee% to be abe to !a!&ate the 4$ !ontent of ea!h
se<&en!e. ather than starting from s!rat!h, e simpy &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 eve
%one that, its 9&st a !ase of &sing the o&tp&t from get_at_content as part of the !on%ition:
13I ChapterG:Con%itionatests
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
seuence B columns$1 name B columns$2
expression B columns$3
if get/at/content(seuence# ; 0. and expression + 200! print(name#
Complex condition
$here are no !a!&ations to !arry o&t for this eer!ise 5 the !ompeity !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
seuence B columns$1 name B columns$2
expression B columns$3
if (name.startsith(:# or name.startsith(h## and species 5B *@rosophila melanogaster*!
print(name#
$he ine !ontaining the if statement is <&ite ong, so it raps aro&n% onto the net ine on this page, b&t its sti 9&st a singe ine in the program fie. $here are to
13@ ChapterG:Con%itionatests
%ifferent ays to epress the re<&irement that the name is not rosophila melanogaster= 'n the above eampe eve &se% the note<&as sign 8Z= b&t e !o&% aso have &se% the not booean operator:
if (name.startsith(:# or name.startsith(h## and not species BB *@rosophila melanogaster*!
7ig+ lo medium
;o e !ome to an eer!ise that re<&ires the &se of m&tipe 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 seuence B columns$1 name B columns$2 expression B columns$3 if get/at/content(seuence# + 0."!
print(name C * has high A) content*# elif get/at/content(seuence# ; 0.'!
print(name C * has lo A) content*# else!
1B0 ChapterG:Con%itionatests
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 :dy33 has medium A) content hdt73J has lo A) content hdu0' has medium A) content teg'3" has medium A) content
1B1 ChapterH:eg&arepressions