Python and Perl - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Python and Perl

Description:

Good for extracting text from flat files (text files) ... Perl the subgroup is stored in the builtin variable $1. Note: In this example we ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 15
Provided by: andersb6
Category:
Tags: builtin | perl | python

less

Transcript and Presenter's Notes

Title: Python and Perl


1
Python and Perl
  • Lecture 1
  • History, Motivation and Simple Syntax
  • Saturday, December 12, 2009

2
Perl
  • Practical Extraction and Reporting Language
  • About 20 years old
  • Good for extracting text from flat files (text
    files)
  • Widespread use in Bioinformatics, since
    biological databanks are usually based on flat
    files.

3
Advantages - Perl
  • Script based
  • You can easily and fast write short scripts (if
    you know the syntax).
  • A lot of premade Modules for bioinformatics.
    Dont reinvent the wheel!
  • The first using Regular expressions, which now
    are a world standard for powerful, generic text
    matching.

4
Disadvantages - Perl
  • Difficult for humans to read.
  • Hundreds of modules exist, but only a few are
    included in the standard distribution.
  • Very loosely typed. Many implicit conversions.
    Most often, Perl makes the right guesses, but not
    always. When not, results are unpredictable and
    debugging becomes very difficult.
  • Object oriented programming was added in lately,
    and is messy.

5
Python
  • More modern, from 1991
  • Is a combination of script based and object
    oriented language.
  • Advantages (compared to Perl)
  • Same strengths as Perl, but has better structure
    and cleaner syntax.
  • Most modules are included in the standard
    distribution.
  • More strongly typed, but does not require
    declaration of variables.
  • Better object orientation.
  • Easy to make advanced graphical and internet
    applications.

6
Drawbacks - Python
  • Regular Expressions are not as integrated as in
    Perl (but equally powerful as in Perl)
  • Few existing modules for bioinformatics.
  • Cannot write as extremely short scripts as in
    Perl (good/bad?).

7
My first Python and Perl program
  • Execution
  • gt perl hi.pl
  • Hello world!
  • gt

My first Python program Filename hi.py
print Hello World!
My first Perl program Filename hi.pl print
Hello World!\n
  • Execution
  • gt python hi.py
  • Hello world!
  • gt

Easy? Yeah! Most things look the same in both
languages. Everything following a is
interpreted as a comment and is ignored. In Perl
you need to end each statement with a semicolon
. In Python, print implicitly writes a
newline character (\n), in Perl this must be
added explicitly.
8
My 2nd Python and Perl program
My 2nd Perl program Filename ex2.pl
integer 4 string some text _at_array (
1, 3, 4 ) print integer string _at_array\n
My 2nd Python program Filename ex2.py
integer 4 string some text array 1, 3,
4 print integer, string, array
  • Execution
  • gt perl ex2.pl
  • 4 some text 1 3 4
  • gt
  • Execution
  • gt python ex2.py
  • 4 some text 1,3,4
  • gt

These examples show that Perl is not as clean as
Python Both languages are dynamically typed ( no
need to declare variables or allocate memory) In
Perl indicates a variable with one value
(scalar), _at_ indicates variable with several
values (like array or vector).
9
My 3rd Python and Perl program
My 3rd Python program Filename ex3.py s
ACGTA if 3 len(s) word two single
ticks else word not print \s\ is
word length 3
My 3rd Perl program Filename ex3.pl s
ACGTA if (3 length(s)) word
two single ticks else word not print
\s\ is . word . length 3\n
  • Execution
  • gt python ex3.py
  • s is not length 3
  • gt

Execution gt perl ex3.pl s is not length 3 gt
In Python, indentation groups code lines into
blocks. Perl uses braces , . Strings
are concatenated using in Python and with .
in Perl.
10
My 4th Python and Perl program
My 4th Python program Filename ex4.py
array 1, 3, 4 print array0, array1 for
x in array print x, print
My 4th Perl program Filename ex4.pl
_at_array (1, 3, 4) print array0 . .
array1 . \n foreach x ( _at_array ) print
x . print \n
Execution gt perl ex4.pl 1 3 1 3 4 gt
  • Execution
  • gt python ex4.py
  • 1 3
  • 1 3 4
  • gt

Iterating over lists is easy. In Python, variable
x gets a new value from the list array for each
iteration. In Perl foreach gives a new value to
x in each iteration. Note one must enclose
_at_array in parentheses to interpret it as a list
rather than a single valued variable.
11
My 5th Python and Perl program
My 5th Python program Filename ex5.py
import sys arguments sys.argv progname
sys.argv0 firstarg sys.argv1 print
arguments print progname, firstarg
My 5th Perl program Filename ex5.pl
_at_arguments _at_ARGV progname 0 firstarg
ARGV0 print _at_arguments\n print progname
firstarg\n
Execution gt perl ex5.pl file1 4 file1 4 ex5.pl
file1 gt
  • Execution
  • gt python ex5.py file1 4
  • 'ex5.py', 'file1', '4'
  • ex5.py file1
  • gt

The arguments/parameters of a program are given
on the command line. In Python these are stored
in sys.argv, where the first element is the name
of the program. In Perl the arguments are stored
in the list/array with the name _at_ARGV, but there
the first element is the first parameter and the
program name is encoded in the built in variable
0.
12
My 6th Python and Perl program
My 6th Python program Filename ex6.py
emblfile open(embl.dat) for line in
emblfile if line.startswith(AC) print line,
My 6th Perl program Filename ex6.pl
open(EMBLFILE, embl.dat) while ( line
ltEMBLFILEgt) if ( line m/AC/ ) print
line
Execution gt perl ex6.pl AC AF03456 AC
M5036 .... gt
  • Execution
  • gt python ex6.py
  • AC AF03456
  • AC M5036
  • ...
  • gt

In Python it is possible to iterate directly on a
file object. In Perl one uses the line read
operator. For each time ltfileobjgt is called, a
new line is read and returned from the file
object. When all lines are read from the file it
returns EOF which breaks the while loop. Python
has a powerful string class with many methods. In
Perl, Regular Expressions are always close at
hand and can handle complex operations.
13
My 7th Python and Perl program
My 7th Python program Filename ex7.py
import sys, re dbfile open(sys.argv1) rac
re.compile(AC\s(.)) for line in dbfile m
rac.match(line) if m ac m.group(1) print
ac
My 7th Perl program Filename ex7.pl
open(DBFILE, ARGV0) while ( line
ltDBFFILEgt) if ( line m/AC\s(.)/
) print 1 , \n
Execution gt perl ex7.pl sprot.dat AF03456 M5036 .
... gt
  • Execution
  • gt python ex7.py sprot.dat
  • AF03456
  • M5036
  • ...
  • gt

Regular Expressions (RE) is an advanced search
system (compare to simpler systems using and
?). In Python, REs are object oriented.
Parentheses in a RE denote a subgroup which in
Python is returned by the group() method of the
match object, while in Perl the subgroup is
stored in the builtin variable 1. Note In
this example we use a command line parameter.
14
My 8th Python and Perl program
My 8th Python program Filename ex8.py kw
dict(AF03456 ATP-transport, M5036
phosphorylation) print kwM5036 for ac in
kw.keys() print ac \t kwkey
My 8th Perl program Filename ex8.pl kw
( AF03456 gt ATP-transport, M5036 gt
phosphorylation) print kwM5036 ,
\n foreach ac (keys kw) print
ac.\t.kwkey.\n
  • Execution
  • gt python ex8.py
  • phosphorylation
  • AF03456 ATP-transport
  • M5036 phosophorylation
  • gt

Execution gt perl ex8.py phosphorylation AF03456
ATP-transport M5036 phosophorylation gt
Hash / associative array / dictionary is a better
form of list. You use words or objects to look
things up. A hash is much faster than an
ordinary list with numbered indices, and you do
not have to implement search or sort algorithmns
yourself. The \t is a tabulator character that
gives us a prettier printout. Tip You are
almost always better off using a hash than an
ordinary list.
Write a Comment
User Comments (0)
About PowerShow.com