Professional Documents
Culture Documents
Perl Exercises
Licence
This manual is 2007-13, Simon Andrews. This manual is distri uted under the creati!e commons Attri ution-"on-#ommercial-Share Ali$e 2.0 licence. This means that %ou are &ree' to co(%, distri ute, dis(la%, and (er&orm the wor$ to ma$e deri!ati!e wor$s
)nder the &ollowin* conditions' Attri ution. +ou must *i!e the ori*inal author credit. "on-#ommercial. +ou ma% not use this wor$ &or commercial (ur(oses. Share Ali$e. ,& %ou alter, trans&orm, or uild u(on this wor$, %ou ma% distri ute the resultin* wor$ onl% under a licence identical to this one.
Please note that' -or an% reuse or distri ution, %ou must ma$e clear to others the licence terms o& this wor$. An% o& these conditions can e wai!ed i& %ou *et (ermission &rom the co(%ri*ht holder. "othin* in this license im(airs or restricts the author.s moral ri*hts.
Perl Exercises
1b
0rite a scri(t which stores %our name in a !aria le. 1a!e it (rint out %our name as (art o& a hello statement sent to the screen. Tr% rea$in* %our name into se(arate &irst name and last name !aria les.
1c
0rite a scri(t which (rints out the text strin* on the &ollowin* line 2 tr% usin* oth sin*le and dou le 3uotes and note the di&&erences in esca(in* needed' 4i$e5exam(le.com sa%s 67800 &or a car 2 that9d e a *ood deal:;
1d
)se a here document to 3uote and (rint a multi-line &ormatted (iece o& text. 4a$e at least one !aria le su stitution within the od% o& the text.
1e
0rite a scri(t to &ind the answer to the &ollowin* e3uation'
x=
-or the &ollowin* !alues' a<2 <3 c<= a<-20 <8 c<3
(a + b) 2 / c
Exercise 3: Conditions
3a
0rite a scri(t which ta$es a strin* and will test to see i& it is (alindromic >reads the same when written ac$wards@. The test should e case insensiti!e. ,t doesn9t ha!e to co(e with s(aces ein* in di&&erent (laces >althou*h %ou can add this &unctionalit% a&ter %ou9!e reached Section =@.
Perl Exercises
3b
0rite a scri(t which wor$s out how old a child should e to $now a certain word. classi&ication should e ased on the len*th o& the word, as &ollows' 8 %ears B< 3 letters C %ears B< = letters D %ears B< C letters 10 %ears B< 10 letters 12 %ears < an% len*th The
3c
A cheer% one this. 0rite a scri(t to calculate when %ou9re *oin* to die. Assume that the a!era*e li&e ex(ectanc% is 70 and then adEust this accordin* to the &ollowin* recorded !aria les' Are %ou male or &emaleF -emales *et an extra = %ears. Are %ou a smo$erF Add 8 %ears i& not, su tract 8 %ears i& %ou are. 1ow o&ten >(er wee$@ do %ou exerciseF Su tract 3 %ears i& ne!er, add one %ear &or each exercise session. 1ow man% units o& alcohol do %ou drin$ (er wee$. Gemo!e 0.8 %ear &or e!er% unit o!er 7. Hain 2 %ears i& teetotal. Ao %ou eat &att% &oodF Add 3 %ears i& not.
#alculate the li&e ex(ectanc% o& a male non-smo$er who exercises twice a wee$, drin$s 10 units o& alcohol a wee$ and eats &att% &ood. "I This is "JT a scienti&ic calculation. , cannot e held res(onsi le &or mis-(redictin* %our death: , did loo$ at a (ro(er scienti&ic 3uestionnaire ut when it started as$in* a out owel mo!ements , decided to ma$e one u(.
3d
0rite a small (assword chec$in* scri(t. This will record the username, old (assword and new (assword. The rules are that a (assword is JK i& it is L7 characters lon*, contains some u((ercase characters and is di&&erent to the old (assword. The admin user >username ?admin9@ can do whate!er the% li$e. Print out whether the new (assword is JK. Tr% doin* this as one com(ound i& statement.
Perl Exercises
b
#reate an arra% and (o(ulate it with 100 random inte*ers etween 1 and 100 >(erldoc 2& rand@. Sort the arra% numericall% and write out the lowest 10 num ers on one line se(arated % ta s.
c
0rite a (ro*ram to calculate the melt tem(erature o& a (rimer. Ta$e a (rimer se3uence and s(lit it into its com(onent characters >see (erldoc 2& s(lit@, then wor$ out the tem(erature % addin* = de*rees &or e!er% H or # and 2 de*rees &or e!er% A,T or ).
!b
#reate a hash which translates codons into 3 letter amino acid codes. 0rite a scri(t which will extract, translate and (rint out the #AS &rom re&se3 entr% "4M020CC1. +ou can co(% o!er the raw se3uence and the #AS start and end into %our scri(t. -or onus (oints (rint out the ori*inal codons and translation ali*ned with each other. -or more onus (oints use the hash &rom exercise 8a to calculate the molecular wei*ht as %ou *o. Nea!e these additions until %ou.!e done the other exercises thou*h.
Perl Exercises
+ou should write a (ro*ram to &ilter this data. The &irst line is a header and should e $e(t. -or each other line calculate the lo* 2 o& Sam(le A data and Sam(le IR data and $ee( the line onl% i&' No*2 o& either Sam(le A or I is *reater than 2 The lo*2 di&&erence >either (ositi!e or ne*ati!e@ etween Sam(le A and I is *reater than 3 >ie an D &old chan*e in raw !alue@
Print the &iltered results in a &ile called &ilteredMdata.txt RPPerl does not ha!e a &unction to directl% calculate lo* 2. 1owe!er %ou can use an% ase lo* to do this calculation since lo* 2>x@ < lo*>x@/lo*>2@. +ou can there&ore use the (erl lo*>@ &unction 2 which calculates the natural lo*, to wor$ out lo* 2.Q
$b
0rite a scri(t which will *enerate simulated -astS se3uence &iles with de&ined se3uence com(osition. +ou should *enerate se(arate &iles which a!era*e H# content o& 10,20,30..T0U o!er a len*th o& 80 (. ,nitiall% %ou could *enerate the &iles with a &ixed 3ualit% !alue &or each ase, ut %ou could also tr% assi*nin* a random 3ualit% in the ran*e 0-30 to each ase. +ou can chec$ the results o& %our scri(t loo$in* at the (ro&ile which is *enerated. % loadin* the *enerated &iles into -astS# and
PThere is a !er% *ood wi$i(edia article descri in* the -astS &ormat. +ou.ll need to use rand to *enerate the random ases and scores. To encode a 3ualit% score %ou use the (erl .chr. &unction to turn a numeric score into an ascii text letterQ
$c
0rite a scri(t which shows a user a secret messa*e onl% when the% enter a !alid (assword. The user should e (rom(ted &or the (assword. ,& the (ro!ided (assword is incorrect the% should e in&ormed and (rom(ted a*ain. The scri(t should 3uit a&ter the user enters the ri*ht (assword, or a&ter the% ha!e (ro!ided 8 incorrect *uesses.
Perl Exercises
P"I -or this (ro*ram the (assword will e !isi le when the user t%(es it. ,t is (ossi le to hide the text the user t%(es, ut this re3uires the use o& a module >which will e co!ered in Section7@Q
$d
+ou will e (ro!ided with two &iles. Annotation.txt contains a list o& se3uence accession codes and their associated descri(tions, se(arated % ta s. Aata.txt has the same list o& accessions >thou*h not in the same order@ alon*side some ta se(arated data !alues. +ou should com ine these &iles to (roduce a sin*le &ile containin* the accession, data and descri(tion &or each *ene. +our scri(t should (er&orm asic sanit% chec$s on the data it reads >e* chec$in* that %ou ha!e oth an accession and descri(tion &or each *ene, and chec$in* that each accession in the data &ile reall% does ha!e annotation associated with it e&ore (rintin* it out@.
,& the name (ro!ided either doesn9t exist, or isn9t a director% the scri(t should throw a hel(&ul error messa*e.
Perl Exercises
*b
+ou will e (ro!ided with a list o& *ene descri(tions >desci(tions.txt@. Some o& these descri(tions contain enO%me classi&ication codes >which loo$ li$e E# 1.2.3.= where each num er can e o& an% len*th and there ma% or ma% not e a s(ace etween E# and the &irst num er@. 0rite a scri(t which will (arse throu*h the &ile (ullin* out all the E# num ers it can &ind and (rint out this list.
*c
+ou will e (ro!ided with a &ile called an*strom.txt. 0rite a scri(t which will read this &ile containin* measurements in An*stroms and will con!ert them into nm. To sa!e %ou loo$in* it u( 1 An*strom is 0.1nm. +our re(lacement should e a le to co(e with oth whole and &ractional An*strom measures, and &or them to e su&&ixed with A or An*strom. ,t should &ind and re(lace' 3A 12 A 2.78 an*stroms 0.123An*stroms ,t should not alter the &ollowin* exam(les' , li$e the num er 3. A !er% nice num er. There are 27 Aard!ar$s in Nondon Voo. P"I A standard re(lacement will treat the ri*ht hand (art o& the re(lacement as a dou le 3uoted strin*. ,& %ou want to (ut code in this (art >as %ou will need to &or this@, %ou need to tell (erl to e!aluate the re(lacement % usin* the ?e9 modi&ier a&ter the re(lacement. +ou can use the . o(erator to concatenate strin*s and calculations in the re(lacement, e* s/(\d+)/$1 . "squared is" . $1**2/e
*d
0rite a (ro*ram to (roduce a restriction ma( o& a se3uence. ,t should read in a se3uence in &asta &ormat and (roduce a list o& restriction site (ositions in it. -asta &ormat loo$s li$e this'
Perl Exercises
>sequence_name GATAGTCGTAGTGCTAGTGCTAGTGCTAGTGC GATAGTCGTAGTGCTAGTGCTAGTGCTAGTGC etc The restriction sites to search &or are shown elow alon* with their reco*nition sites. The *a( in the site re(resents the cut site, and the ase a&ter this *a( should e re(orted as the (osition o& the site. -or onus (oints ha!e the restriction ma( written out to a &ile named a&ter the se3uence name >e* se3uenceMnameMma(.txt@. +ou will e (ro!ided with a &asta &ile to test. Gestriction sites to use' EcoG, aattc It*, 4sl, c cr! ca!nn nnrt % ! # c $r t% n # a $r $r t $r c&
"r # a $r
P"I -or this exercise all matches must e loo(ed to ma$e sure %ou don9t Eust &ind the &irst cut site, and must e case insensiti!e as &asta se3uences can e either u((er or lower caseQ.
Perl Exercises
10
1.b
0rite a scri(t containin* a su routine to calculate the mean, !ariance and standard de!iation o& an arra% o& num ers. Henerate a random data set o& etween 10 and 20 measures with !alues etween 0 and 100 and (ass this to the su routine. The &ormula &or calculatin* the !ariance o& a sam(le is' v = (( x m) 2 ) /( n 1) ! < !ariance x < each indi!idual measure m < mean o& all measures n < num er o& measures The Standard Ae!iation is the s3uare root o& the !ariance.
11b
+ou will e (ro!ided with a &ile >tissues.txt@ containin* the o&&icial list o& all tissues. Each entr% consists o& an o&&icial name >term@, an identi&ier, a de&inition and a (u med re&erence to the de&inition. 0rite a scri(t which (arses this &ile and (uts all o& the in&ormation in it into a suita le data structure. All entries in this &ile are on a sin*le line. Nines startin* with : are comments.
Perl Exercises
11
)se this datastructure as (art o& a scri(t which (rom(ts the user &or a tissue name. ,& the% enter a !alid name return all the details &or that tissue. ,& the% enter a (artial name >e* ?o!9@ then return them the list o& (ossi le com(letions >o!ar%, o!ar% cancer cell, JW#AG-3 cell, JW#AG-8 cel, o!iduct, o!otestis). All matches should e case insensiti!e.
Perl Exercises
12
12b
XAcniocdr* to rceresah at an Eslni*h )is!rnite%, it dseon.t mteatr in waht oerdr the ltrtees in a wrod are, the oln% i(oatnrmt tnhi* is taht the &sirt and and lsat ltteer is at the rh*it (lcae. The rset can e a toatl mses and %ou can stlil raed it wtiuhot ( relom. Tihs is saceue we do not raed ere!% ltteer % iselt& ut the wrod as a wlohe.X 0rite a (ro*ram which will ta$e a section o& text and will mix it u( to read li$e the a o!e exam(le. To mix each word %ou should s(lit it into an arra% o& letters and use the *ist))+ti, module to shu&&le the contents >excludin* the &irst and last letters@. )se the Te-t)).ra/ module to write out the re&ormatted strin* to &ill D0 char columns.
12c
The *.0))+serA ent module allows %ou to retrie!e we (a*es into %our Perl (ro*rams. 0rite a scri(t which will retrie!e the Iioin&ormatics mission statement we (a*e &rom htt('//www. ioin&ormatics. src.ac.u$/c*i- in/mission.c*i , (arse out the mission statement and (rint it. P"I ,& %ou e!er want to &etch htt( (a*es &rom outside the IISG# then %ou must (ass all o& %our re3uests throu*h the IISG# (rox% ser!er. The address &or this ser!er is htt('//wwwcache. src.ac.u$'D0D0 See the Prox% Attri utes section o& the *.0))+serA ent documentation &or details o& how to tell the )serA*ent a out %our (rox% ser!er.Q
12d
-ind out how *ood the (erl rand() &unction is. -ind and install a module &rom #PA" which will (er&orm a #hi-S3uare test. 0rite a scri(t which simulates rollin* a dice 10,100,1000 and 10000 times and see how random the chi-s3uare test sa%s the data is. -or onus (oints use the 1et))2and$m module to *et some trul% random data &rom random.or* &or the same test. See how the results &or the real random num ers stac$ u( a*ainst rand(). Iecause 1et))2and$m &etches in&ormation &rom an external we site (lease onl% do this (art o& the exercise once %ou9re sure that %ou9!e *ot the rand() !ersion wor$in* (ro(erl%. 0e don9t want to *o u(settin* the (eo(le at random.or*.
12e
0rite a module called 31A))'ani/u,ate. This should (ro!ide two su routines' 1. re4c$m/ should ta$e a A"A se3uence and return its re!erse com(lement >%ou wrote the code &or this in exercise 10a@ 2. trans,ate should ta$e a A"A se3uence and a &rame >1,2,3@ and return a (rotein translation o& that &rame. ,nstall the module and chec$ %ou can call it &rom another (ro*ram.
Perl Exercises
13
,& %ou.re &eelin* old then tr% rewritin* the module as an J Eect-Jriented module where the A"A se3uence is (assed to the ne5() method and is then stored &or use in the other methods.
Perl Exercises
1=
13b
)se the ?date9 and ?time9 (ro*rams to collect the time and date and (rint them out to*ether. Ioth (ro*rams should e run with the /T switch so that the% don9t (rom(t %ou to chan*e the date/time a&ter the% ha!e een dis(la%ed.
13c
The ?hel(9 (ro*ram dis(la%s a hel( (a*e a out an% a!aila le 0indows command. There can e a lot o& in&ormation (resented, ut the &irst line returned is alwa%s a 3uic$ summar% o& what the command does. 0rite a scri(t which uses the hel( (ro*ram to &ind the &unction o& the &ollowin* commands' hel( attri dir assoc
13d
The nsloo$u(.exe (ro*ram will &ind the ,P address o& an% com(uter name *i!en to it. ,t9s out(ut loo$s li$e this >the ,P address in red is the one %ou want@' ')\>ns,$$6u/ 7i,in5s8.7a7ra(am.77src.ac.u6 9er4er) 7ii:s.7a7ra(am.77src.ac.u6 Address) 1;<.1==.1;;.2< 1ame) 7i,in5s8.7a7ra(am.77src.ac.u6 Address) 1;<.1==.1;>.8; 0rite a scri(t which will ta$e in a list o& com(uter names and, usin* nsloo$u(, will identi&% their ,P addresses. )se it to &ind the addresses o& the &ollowin* machines' Iilin1. a raham. Iilin2. a raham. src.ac.u$ src.ac.u$ src.ac.u$
Iilinws1. a raham.
13e
The ?dri!er3uer%9 command returns a list o& all the currentl% loaded dri!ers on %our s%stem. 0rite a scri(t which will (ull out a list o& all the dri!er modules &or the &ile s%stem >those listed as 6&ile s%stem; in the 6Ari!er T%(e; column. The data &rom this command is &ixed width, not delimited, so %ou will need to use su str rather than s(lit to (arse it.
Perl Exercises
18