User Tools

Site Tools


tutorials:perl_exercises

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:perl_exercises [2010/07/31 22:45] chkuotutorials:perl_exercises [2010/08/07 00:42] (current) chkuo
Line 1: Line 1:
 ====== Perl Exercises ====== ====== Perl Exercises ======
  
-===== Read-and-Write ===== +===== read_and_write ===== 
-Read from an input file and write the content to an output file. Assume the two filenames are provided in the command line. You need to know about filehandles before attempting this exercise.+Read from an input file and write the content to an output file. Assume the two filenames are provided in the command line.  
 + 
 +**Hint: learn about filehandle before attempting this exercise.**
 <code bash> <code bash>
 $ cat input.txt  $ cat input.txt 
Line 18: Line 20:
 James Monroe James Monroe
 </code> </code>
 +See a sample answer [[tutorials:perl:read_and_write.pl|here]].
 +
 +===== parse_first_name =====
 +Read from an input file (containing a list of full names) and write the first names to an output file. Assume the two filenames are provided in the command line. 
 +
 +**Hint: learn about filehandle and array (or regular expression) before attempting this exercise.**
 +<code bash>
 +$ cat input.txt 
 +George Washington
 +John Adams
 +Thomas Jefferson
 +James Madison
 +James Monroe
 +$ perl parse_first_name.pl input.txt first_name.txt
 +$ cat first_name.txt 
 +George
 +John
 +Thomas
 +James
 +James
 +</code>
 +See a sample answer [[tutorials:perl:parse_first_name.pl|here]].
 +
 +===== count_first_name =====
 +Read an input file that contains several names (assuming one name in each line, the first name and the last name are separate by a space). Count how the number of times each first name appeared in the input. Print the result to STDOUT, sort the names alphabetically.
 +
 +**Hint: learn about filehandle, array, hash, and sorting before attempting this exercise.**
 +<code bash>
 +$ cat input.txt 
 +George Washington
 +John Adams
 +Thomas Jefferson
 +James Madison
 +James Monroe
 +$ perl count_first_name.pl input.txt 
 +George appeared 1 times
 +James appeared 2 times
 +John appeared 1 times
 +Thomas appeared 1 times
 +</code>
 +See a sample answer [[tutorials:perl:count_first_name.pl|here]].
 +
 +===== write_lines_to_files =====
 +Obtain an input file and an output directory from the command line. Produce one output file for each line in the input file, use the line number (i.e., 1, 2, 3, etc) as the filenames.
 +
 +**Hint: learn about filehandle and directory operation before attempting this exercise.**
 +
 +<code bash>
 +$ cat input.txt 
 +George Washington
 +John Adams
 +Thomas Jefferson
 +James Madison
 +James Monroe
 +$ perl write_lines_to_files.pl input.txt output/
 +$ head output/*
 +==> output/1 <==
 +George Washington
 +
 +==> output/2 <==
 +John Adams
 +
 +==> output/3 <==
 +Thomas Jefferson
 +
 +==> output/4 <==
 +James Madison
 +
 +==> output/5 <==
 +James Monroe
 +</code>
 +See a sample answer [[tutorials:perl:write_lines_to_files.pl|here]].
 +
 +
 +===== combine_files =====
 +The opposite of write_lines_to_files; obtain an input directory and an output file from the command line, read the files in the input directory and write their content to one single output file. Note that we want to exclude hidden files in the input directory.
 +
 +**Hint: learn about filehandle, directory operation, and pattern matching before attempting this exercise.**
 +
 +<code bash>
 +$ head output/*
 +==> output/1 <==
 +George Washington
 +
 +==> output/2 <==
 +John Adams
 +
 +==> output/3 <==
 +Thomas Jefferson
 +
 +==> output/4 <==
 +James Madison
 +
 +==> output/5 <==
 +James Monroe
 +$ perl combine_files.pl output/ combined.txt
 +$ cat combined.txt 
 +George Washington
 +John Adams
 +Thomas Jefferson
 +James Madison
 +James Monroe
 +</code>
 +See a sample answer [[tutorials:perl:combine_files.pl|here]].
 +
 +
 +
 +===== unwrap_fasta =====
 +Read a [[http://en.wikipedia.org/wiki/FASTA_format|fasta]] file, unwrap the sequences (i.e., remove all extra line breaks), and save the result to an output file.
 +
 +**Hint: learn about the ''$/'' variable (input record separator) before attempting this exercise.**
 +<code bash>
 +$ cat coding.fasta 
 +>NP_414542
 +ATGAAACGCATTAGCACCACCATT
 +accaccaccatcaccattaccacaggta
 +ACGGTGCGGGCTGA
 +>NP_414617
 +ATGACTCACATCGTTCGCTTTA
 +TCGGTCTACTA
 +ctactaaacgcatcttctttgcgcggta
 +GACGAGTGAGCGGCATCCAGCATTAA
 +$ perl unwrap_fasta.pl coding.fasta unwrap.fasta
 +$ cat unwrap.fasta 
 +>NP_414542
 +ATGAAACGCATTAGCACCACCATTACCACCACCATCACCATTACCACAGGTAACGGTGCGGGCTGA
 +>NP_414617
 +ATGACTCACATCGTTCGCTTTATCGGTCTACTACTACTAAACGCATCTTCTTTGCGCGGTAGACGAGTGAGCGGCATCCAGCATTAA
 +</code>
 +See a sample answer [[tutorials:perl:unwrap_fasta.pl|here]].
 +
 +===== display_codon =====
 +Read a [[http://en.wikipedia.org/wiki/FASTA_format|fasta]] file that contain protein-coding sequences. Re-format the sequences to show codons (10 codons per line) in the output file.
 +
 +**Hint: learn about the ''substr'' function or regular expression before attempting this exercise.**
 +<code bash>
 +$ cat coding.fasta 
 +>NP_414542
 +ATGAAACGCATTAGCACCACCATT
 +accaccaccatcaccattaccacaggta
 +ACGGTGCGGGCTGA
 +>NP_414617
 +ATGACTCACATCGTTCGCTTTA
 +TCGGTCTACTA
 +ctactaaacgcatcttctttgcgcggta
 +GACGAGTGAGCGGCATCCAGCATTAA
 +$ perl display_codon_1.pl coding.fasta codon.fasta 
 +$ cat codon.fasta 
 +>NP_414542
 +ATG AAA CGC ATT AGC ACC ACC ATT ACC ACC
 +ACC ATC ACC ATT ACC ACA GGT AAC GGT GCG
 +GGC TGA 
 +>NP_414617
 +ATG ACT CAC ATC GTT CGC TTT ATC GGT CTA
 +CTA CTA CTA AAC GCA TCT TCT TTG CGC GGT
 +AGA CGA GTG AGC GGC ATC CAG CAT TAA 
 +
 +</code>
 +See sample answer 1 [[tutorials:perl:display_codon_1.pl|here]] and sample answer 2 [[tutorials:perl:display_codon_2.pl|here]].
 +
 +===== count_EcoRI_site =====
 +Read a [[http://en.wikipedia.org/wiki/FASTA_format|fasta]] file and count the number of [[http://en.wikipedia.org/wiki/EcoRI|EcoRI]] restriction sites in each sequence.
 +
 +**Hint: learn about the regular expression before attempting this exercise.**
 +
 +<code bash>
 +$ cat EcoRI.fasta 
 +>Seq_1
 +nnnGAA
 +TTCnnnGAATTCnnnGaattCnnn
 +>Seq_2
 +nnnGAATTCnnngAa
 +TtCnnn
 +$ perl count_EcoRI_site.pl EcoRI.fasta 
 +Seq_1 has 3 EcoRI sites
 +Seq_2 has 2 EcoRI sites
 +</code>
 +See a sample answer [[tutorials:perl:count_EcoRI_site.pl|here]].
 +
 +
tutorials/perl_exercises.1280587555.txt.gz · Last modified: 2010/07/31 22:45 by chkuo