getting started with perl (and excel)

41
Getting Started with Perl (and Excel) Biophysics 101 September 17, 2003 Griffin Weber (With material from Jon Radoff and Ivan Ovcharenko)

Upload: hans

Post on 12-Jan-2016

37 views

Category:

Documents


3 download

DESCRIPTION

Getting Started with Perl (and Excel). Biophysics 101 September 17, 2003 Griffin Weber (With material from Jon Radoff and Ivan Ovcharenko). What is a computer?. Artificial brains? Smarter than you? Too complicated to understand?. What is a computer?. A machine with lots of buttons - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Getting Started with Perl (and Excel)

Getting Started with Perl

(and Excel)

Biophysics 101

September 17, 2003

Griffin Weber

(With material from Jon Radoff and Ivan Ovcharenko)

Page 2: Getting Started with Perl (and Excel)

What is a computer?

• Artificial brains?

• Smarter than

you?

• Too complicated

to understand?

Page 3: Getting Started with Perl (and Excel)

What is a computer?

• A machine with lots of buttons

• The “3 minute popcorn” program:

TIME 300

STARTcommands

parameter

Page 4: Getting Started with Perl (and Excel)

What is a computer?

01100101 10101101

Input Output

0 = off1 = on

Page 5: Getting Started with Perl (and Excel)

What is Perl?

• Perl is a computer language• Easier to use than binary code

01100101print 2+3;

Perl

Interpreter

Page 6: Getting Started with Perl (and Excel)

Where do I get Perl?

Course web site for download links and instructions:http://www.courses.fas.harvard.edu/~bphys101/links/

index.html

Windows: ActivePerl

MacOS: MacPerl

Unix/Linux (FAS): Already Installed

Page 7: Getting Started with Perl (and Excel)

How do you use Perl?

• Create a new text file• Type all the commands• Save it as a *.pl file (hello.pl)• Tell the computer to run your file

perl hello.pl

Page 8: Getting Started with Perl (and Excel)

What does Perl look like?

#!/usr/local/bin/perl

print “Hello everyone\n”;

Mandatory first line !

Draw something to the screen

Page 9: Getting Started with Perl (and Excel)

Printing Output to the Screen

print “Hello \n”;

print command:

" " - place output in quotes

\n - end-of-line character

(like pressing ‘Enter’)

; - ends every command

Page 10: Getting Started with Perl (and Excel)

Variables

2+2 = ? $a = 2;

$b = 2;

$c = $a + $b;

$ - indicates a variable

;

= - assigns a value to a variable

- don’t forget

a - the variable name

Page 11: Getting Started with Perl (and Excel)

Perl Calculator

$c = 4 - 2;

$c = 2 * 2;

$c = 2 / 2;

$c = 2 ** 4; - power : 2^4 <-> 24

=16$c = 1.35 * 2 – log(3) / (0.12 + 1);

- subtract

- multiply

- divide

natural log

Page 12: Getting Started with Perl (and Excel)

Perl Calculator : Output

print " 2 + 2 = " . (2+2) . "\n";

$c = 2 + 2;

print " 2 + 2 = $c \n";

print the value of $c

2 + 2 = 4

or… strings

expressionconcatenate.output

Page 13: Getting Started with Perl (and Excel)

Datatypes

# DEFINING NUMBERS

$x = 4;

$x = 1.25;

$x = 2.345e-56;

# DEFINING STRINGS

$x = “ACTGGTA”;

$y = “Hello everyone \n”;

2.345 * 10-56

2.345*10**-56

Perl Examples:# - comment

line (Perl ignores)

Page 14: Getting Started with Perl (and Excel)

Loops and Cycles : FOR Statement

# Output all the numbers from 1 to 100

for ($n=1; $n<=100; $n+=1) {

print “$n \n”;

}

1. Initialization

for ( $n=1 ; $n<=100 ; $n+=1 ) { … }

4. Increment ($n = $n + 1)

2. Termination (stop if this is not true)

3. Body (or block) of the loop - commands inside curly brackets

Page 15: Getting Started with Perl (and Excel)

FOR Loop Example

Output - Triangle of A’s:

for ($line=1; $line<=3; $line+=1) {

# output $line ‘A’ symbols

for ($n=1; $n<=$line; $n+=1) {

print “A”;

}

# end the line

print “\n”;

}

AAAAAA

Source code:

Indent = TA

Page 16: Getting Started with Perl (and Excel)

Conditional Statements$x = -1;

# check whether $x is positive or not

if ($x > 0) {

print “x = $x is positive\n”;

}

1. If this is true, ...

if ( some_expression ) { … }

2. ... then do this - commands inside curly brackets

Page 17: Getting Started with Perl (and Excel)

Expressions

$x = 1;

$y = 2;

if ($x < $y) { }

if ($x > $y) { }

if ($x <= $y) { }

if ($x >= $y) { }

if ($x == $y) { }

if ($x != $y) { }

With numbers:

Less than

Greater than

Less than or equal

Greater than or equal

Equal

Not equal

$a = “DNA”;

$b = “RNA”;

if ($a lt $b) { }

if ($a gt $b) { }

if ($a le $b) { }

if ($a ge $b) { }

if ($a eq $b) { }

if ($a ne $b) { }

With strings:

Page 18: Getting Started with Perl (and Excel)

Conditional Statements

Output:

$x = -1;

# check whether $x is positive or not

if ($x > 0) {

print “x = $x is positive\n”;

}

if ($x < 0) {

print “x = $x is negative\n”;

}

if ($x == 0) {

print “x is zero\n”;

}

x = -1 is negative

Source code:

Page 19: Getting Started with Perl (and Excel)

Conditional Statements

$x = -1;

# check whether $x is positive or not

if ($x > 0) {

print “x = $x is positive\n”;

} elsif ($x < 0) {

print “x = $x is negative\n”;

} else {

print “x is zero\n”;

}

The same thing:

if ( some_expression ) { … } else { … }

if this is true do this otherwise do this

Page 20: Getting Started with Perl (and Excel)

Putting It Together

FOR & IF -- all the even numbers from 1 to 100:

for ($n=1; $n<=100; $n+=1) {

if (($n % 2) == 0) {

print “$n”;

}

}Note: $a % $b -- Modulus (remainder when $a is divided by $b)

7 % 2 = 1 5 % 3 = 2 12 % 4 = 0

Page 21: Getting Started with Perl (and Excel)

Data Structures : Arrays

# array of 5 numbers

@a = (7,3,4,-1,0);

# array of strings

@day = (“Mon”, “Tue”, “Wed”, “Thu”, “Fri”);

@ - indicates an array (…)

- a list of values

$a[2] - an element of the array (count from 0)

Page 22: Getting Started with Perl (and Excel)

Data Structures : Arrays@a = (7,3,4,-1);

# change the value of an element$a[1] = 5;

# print all the elements in the array @a

for ($i=0; $i<=$#a; $i+=1) {

print “a[$i] = $a[$i] \n”;

}

$#a - the index of the last element in the array @a

a[0] = 7

a[1] = 5

a[2] = 4

a[3] = -1

Page 23: Getting Started with Perl (and Excel)

Two Dimensional Arrays

# A 2x2 array of 9 numbers

@a = ([1,2,3],[4,5,6],[7,8,9]);

([…],[…],…,[…])

- an array of arrays

# change the value of an element

$a[1][2] = -1;

print “ $a[2][0] \n”;7

Page 24: Getting Started with Perl (and Excel)

Working with Files

Microsoft Word

Open

Edit

Save

Close

Perl Files

Open

Read or Write(one line at a time)

Close

Page 25: Getting Started with Perl (and Excel)

Working with Files

How to open and close a file “data.txt” from a perl program? # open data.txt file for READINGopen (FILE, "<data.txt" );

File handler -This name will be used everywhere later in the program, when we will deal with this file.

<>

Direction of file data flow

- READ from a file

# close a file specified by FILE file handlerclose (FILE);

- WRITE to a file

Page 26: Getting Started with Perl (and Excel)

Working with Files

Writing “Hello everyone” to the “tmp.txt” file:

#!/usr/local/bin/perl

open (FILE, “>tmp.txt”);

print FILE “Hello everyone\n”;

close (FILE);

Note: If tmp.txt already exists, it will be erased.

Page 27: Getting Started with Perl (and Excel)

Working with Files

# open file data.txt for readingopen (FILE, “<data.txt”);

# read file line by line and print it out to the

screen

while ($line = <FILE>) {

print “$line”;

}

#close fileclose(FILE); while loop is analogous to the for loop.

All the body statements of it are executed until the condition in parenthesis is false.

Read the next line from the file specified by the filehandle <FILE>

Page 28: Getting Started with Perl (and Excel)

Working with FilesExample. Calculating a sum of numbers in the file data.txt:

#!/usr/local/bin/perl$sum = 0;open (FILE, “<data.txt”);while ($line = <FILE>) {

chomp($line);$sum = $sum + $line;

}close(FILE);print “Sum of the numbers in data.txt file is $sum\n”;

118232

Sum of the numbers in data.txt file is 44

chomp command removes “\n” (new line) symbol from the string

Page 29: Getting Started with Perl (and Excel)

More Strings

$DNA = “ACGTCG”;

# length of the string -> number of characters

inside

$seqLen = length ($DNA); # $seqLen =

6

# extracting a part of a string

$seqPart = substr ($DNA, 2, 3); # $seqPart =

“GTC”

substr ( $string, $offset, $n)

-- extracts $n characters from the string

$string, starting at the position $offset

(first position in a string is 0, not 1!)

Note: In Perl, a string is not an array of characters.

Page 30: Getting Started with Perl (and Excel)

More Strings$DNA = “ACGTCG”;

# length of the string -> number of characters

inside

$seqRev = reverse ($DNA); # $seqRev =

“GCTGCA”

# substitute all ‘C’ symbols with ‘T’ symbols

$DNA =~ s/C/T/gi; # $DNA = “ATGTTG”s - substitute

(search & replace)

g – global (everywhere)

i – case insensitive

# count the number of substitutions

$numG = ($DNA =~ s/G//gi); # $numG = 2

Page 31: Getting Started with Perl (and Excel)

More Strings

$str = “I like biology.”;

# replace “biology” with “computers”$str =~ s/biology/computers/; # $str = “I like computers.”

# replace ‘r’ with ‘n’, ‘e’ with ‘i’, and ‘s’ with ‘g’$str =~ tr/res/nig/; # $str = “I liki computing.”

Note: tr/// substitutes only symbols, while s/// substitutes strings

Page 32: Getting Started with Perl (and Excel)

Functions (Subroutines)

$x = min(5,3);print “Smallest of 5 and 3 is: $x\n”;

# Function minsub min {

($a, $b) = @_;if ($a < $b) {

$small = $a;} else {

$small = $b;}return $small;

}

A function is a program within a program.

define the function

input parameters

return the answer

call the function

Page 33: Getting Started with Perl (and Excel)

ModulesPerl does not have functions for everything, but many useful functions have already programmed by other people, and they share their libraries of functions, which are called modules

use bignum; # Work with large numbers

use CGI; # Build interactive web pages

use BioPerl; # Perform DNA sequence analysis

use GD; # Create pictures

use DBI; # Communicate with databaseshttp://cpan.org/ -- lots of Perl modules

use X; - place near the beginning of your program

tells Perl to use all the functions in module X

Page 34: Getting Started with Perl (and Excel)

Bugs!

$x = 1if ($x = 1) { $x = 2; }x = 0;$a = (1, 2, 3);$y = 5/$x;

Page 35: Getting Started with Perl (and Excel)

Bugs!

#!/usr/local/bin/perl $x = 1if ($x = 1) { $x = 2; }x = 0;$a = (1, 2, 3);$y = 5/$x;

Page 36: Getting Started with Perl (and Excel)

Bugs!

#!/usr/local/bin/perl $x = 1;if ($x = 1) { $x = 2; }x = 0;$a = (1, 2, 3);$y = 5/$x;

Page 37: Getting Started with Perl (and Excel)

Bugs!

#!/usr/local/bin/perl $x = 1;if ($x == 1) { $x = 2; }x = 0;$a = (1, 2, 3);$y = 5/$x;

Page 38: Getting Started with Perl (and Excel)

Bugs!

#!/usr/local/bin/perl $x = 1;if ($x == 1) { $x = 2; }$x = 0;$a = (1, 2, 3);$y = 5/$x;

Page 39: Getting Started with Perl (and Excel)

Bugs!

#!/usr/local/bin/perl $x = 1;if ($x == 1) { $x = 2; }$x = 0;@a = (1, 2, 3);$y = 5/$x;

Page 40: Getting Started with Perl (and Excel)

Bugs!

#!/usr/local/bin/perl $x = 1;if ($x == 1) { $x = 2; }$x = 0;@a = (1, 2, 3);$y = 5/$x; # divide by 0

Page 41: Getting Started with Perl (and Excel)

Excel

• What is a worksheet?

• Enter data by hand

• Import data from a file

• Use Excel built-in functions

• Perform statistical tests

• Make graphs from data