Title: Books
1Books
2Perl
- Perl (Practical Extraction and Report Language)
by Larry Wall - Perl 1.0 was released to usenet's
alt.comp.sources in 1987 - Perl 5 was released in 1994.
Low Level lang. (C/C,Pascal) hard to write,
fast runtime, unlimited.
High Level lang. (shell, awk, ) hard, slow,
very limited.
Perl easy, mostly fast, nearly unlimited.
3Perl
- Optimized to work with text
- Free source code, very good support
- www.cpan.org (Perl source, docs, extensions)
- www.perldoc.com/
- Perl for Windows www.activestate.com
4Just for Start
gtsort.pl .txt
5Hello Perl
On Unix create text file, hello.pl (any
name/extension is ok)
!/usr/bin/perl print Hello, Perl!\n comment
your code var1 Hello, World! . \n No
variable declaration is needed print
var1 ivar 10 scalar variables
initialized with 0 (strings
with empty string ) print This is ivar \n
6Hello Perl (2)
!/usr/bin/perl always use this header (without
any spaces)
Set on executable flag gtchmod x hello.pl Run
it gthello.pl
7Strings
Hello Perl\n not equal to Hello
Perl\n Hello var not equal to Hello
var Concatenation Hello . Perl\n
Hello Perl\n Copy Perl x 3
PerlPerlPerl
8Print
print hello print 3ivar print \n print
hello , 3ivar, \n
worldworld print hello , world,
\n print hello . world . \n print
hello world \n print hello worlds \n
9Comparison Operators
If( ivar ! 5) If( str ne hello)
10(No Transcript)
11User Input
line ltSTDINgt while (line ltSTDINgt) chomp(l
ine) remove new line char \n if(line eq
quit) exit(0)
12Arrays
rocks0bedrock rocks1lava rocks99
rock now there are 100 elements print
rocks rocks \n prints last element
rock size rocks 1 number of elements
13Lists
(0,5,6,7,8) (0,5..8) (..) range
operator (0,5..8) contains two elements 0
and string 58
14List Assignment
(color, tree, list) (green, red-black,
linked)
swap (color, tree) (tree, color) (i,
j)(1..3) 3 is ignored (i, j, k)(1,2)
k gets undef
15List Assignment (2)
(color0, color1, color2)(red, blue,
green) _at_colors(red, blue,
green) _at_more_colors(white, _at_colors,
yellow) more_colors contains white,
red, blue, green, yellow
print Five colors _at_more_colors. \n five
colors white red blue green yellow.
16Foreach
_at_colors(red, blue, green) for(i0i lt
colors i) print colorsi \n
other way to do the same but array is
changed foreach col (_at_colors) col . \n
print _at_colors
much better foreach col (_at_colors) print col
\n
17Push and Pop
_at_array 1..5 push _at_array, 6 array contains
1..6 six pop _at_array array contains
1..5 _at_others 6..10 push _at_array, _at_others array
contains 1..10
18Shift and Unshift
Push and Pop for the end of an array Shift and
Unshift for the start of an array _at_array
1..5 five shift _at_array array contains
25 unshift _at_array, 1 array contains
15 unshift _at_array, (-2,-1,0) array contains
25 ! Shift and Unshift, unlike Push and Pop,
change indices of all array elements
19Default variable _
_at_colors(red, blue, green) foreach
(_at_colors) _ . \n _ default
variable\n print prints default variable\n
20Reverse, Sort operators
_at_array 1..100 print reverse(_at_array) _at_array
reverse (_at_array) array contains 100..1
sorts in ASCII order _at_array sort (_at_array)
array contains 1, 100 ,11,12 19,2,20,,9,9099
21ltINPUTgt in List Context
open FILE, readme.txt or die Cannot open
file ! while (line ltFILEgt) chomp(line)
remove new line char \n push _at_lines, line
better way _at_lines ltFILEgt chomp( _at_lines )
or this way chomp ( _at_lines ltFILEgt )
22Perl is Context Dependent
_at_sorted_array sort _at_array list context ii
3 _at_array scalar context 3ArraySize (ii)
_at_array list context iiarray0 _at_array 38
list context _at_array(38)
str ltFILEgt return next line form
FILE _at_lines ltFILEgt returns all remaining lines
23Example
Task Write a program that prints each line in a
right-justified 20 character column. First print
a ruler-line of digits. 1234567890123456789012
345678901234567890
hello test-test
20
24Example (solution)
For more info on printf run perldoc f sprintf
chomp( _at_lines ltSTDINgt ) print 1234567890 x 7,
\n foreach (_at_lines) printf 20s\n,
_
format 20s\n x _at_lines printf format,
_at_lines
25Control Statements unless, until
unless ( a b)
if( a ! b)
26Control Statements elsif
if ( expression1 ) elsif(expression2
) elsif(expression3 ) else
Only the block of the first true conditional
expression is executed, or else otherwise.
27Control Statements next, last
while( lt gt ) if( /Protein ID/ ) print
next .. if( /Remarks/ ) last
28Control Statements redo
while ( ) if( ) redo starts the
current iteration from the beginning (control
statement is not evaluated) notice the
difference with the next (starts the next
iteration)
29Control Statements logical operator
proteinID proteins id or Unknown or
like in C - expression ? TrueExpression
FalseExpression proteinID proteins id ?
proteins id Unknown
30Example Parsing FASTA file
gtroa1_drome Rea guano receptor type III gtgt
0.1 MVNSNQNQNGNSNGHDDDFPQDSITEPEHMRKLFIGGLDYRTTDEN
LKAHEKWGNIVDVV VMKDPRTKRSRGFGFITYSHSSMIDEAQKSRPHKI
DGRVEPKRAVPRQDIDSPNAGATVK KLFVGALKDDHDEQSIRDYFQHFG
NIVDNIVIDKETGKKRGFAFVEFDDYDPVDKVVLQK QHQLNGKMVDVKK
ALPKNDQQGGGGGRGGPGGRAGGNRGNMGGGNYGNQNGGGNWNNGGN NW
GNNRGNDNWGNNSFGGGGGGGGGYGGGNNSWGNNNPWDNGNGGGNFGGGG
NNWNGGND FGGYQQNYGGGPQRGGGNFNNNRMQPYQGGGGFKAGGGNQG
NYGNNQGFNNGGNNRRY gtroa2_drome Rea guano
ligand MVNSNQNQNGNSNGHDDDFPQDSITEPEHMRKLFIGGLDYRTT
DENLKAHEKWGNIVDVV VMKDPTSTSTSTSTSTSTSTSTMIDEAQKSRP
HKIDGRVEPKRAVPRQDIDSPNAGATVK KLFVGALKDDHDEQSIRDYFQ
HLLLLLLLDLLLLDLLLLDLLLFVEFDDYDPVDKVVLQK QHQLNGKMVD
VKKALPKNDQQGGGGGRGGPGGRAGGNRGNMGGGNYGNQNGGGNWNNGGN
NWGNNRGNDNWGNNSFGGGGGGGGGYGGGNNSWGNNNPWDNGNGGGNFG
GGGNNWNGGND FGGYQQNYGGGPQRGGGNFNNNRMQPYQGGGGFKAGGG
NQGNYGNNQGFNNGGNNRRY
31Example Parsing FASTA file (2)
print Input file name filenameltSTDINgt open
FASTA, filename _at_lines ltFASTAgt foreach
line (_at_lines) f(line /\s/)
next continue if empty line
if(line /gt/) print "Header",
line else print "Seq", line
Parses file in FASTA format. Filename is input to
the program.
32Example Parsing FASTA file (3)
die "Can't open fileARGV0" unless open(
FASTA, ARGV0) _at_lines ltFASTAgt foreach
(_at_lines) unless( /\s/ ) continue if
empty line if( /gt/ ) print
"Header", _ else print
"Seq", _
Parses file in FASTA format. Filename is argument
to the program.
33Example Parsing FASTA file (4)
Reads all input files (from the argument list)
_at_lines ltgt foreach (_at_lines) unless(
/\s/ ) continue if empty line if(
/gt/ ) print "Header", _
else print "Seq", _
Automatic error message in case of invalid
filename.
Parses file in FASTA format. Filenames are
arguments to the program.
34Example Parsing FASTA file (5)
while ( ltgt ) unless( /\s/ ) continue
if empty line if( /gt/ )
print "Header", _ else
print "Seq", _
Parses file in FASTA format. Filenames are
arguments to the program.
35HomeWork
- Parsing FASTA file. Write two separate programs.
- Read FASTA file. Output reverse sequence in FASTA
format. Hint use split function (perldoc f
split). - For DNA sequence output reverse complement. Hint
use substr function (perldoc f substr) - (c) Create your personal I-net Home Page.