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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Flow Charts, Loop Structures
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
Part 2 Biophysics 101 Jon Radoff, TF Teaching materials by Ivan Ovcharenko
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Practical Extraction & Report Language Picture taken from
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
1ex.1 Perl Programming for Biology Exercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman.
4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.
Introduction to C Programming
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Introduction to Python
Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Perl Practical(?)‏ Extraction and Report Language.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
CSC Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class.
Review, Pseudocode, Flow Charting, and Storyboarding.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introduction to Perl William G. Dishman CUR/516 November 5, 2014.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Digital Image Processing Introduction to M-function Programming.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Part 4 Arrays: Stacks foreach command Regular expressions: String structure analysis and substrings extractions and substitutions Command line arguments:
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
What is Binary Code? Computers use a special code of their own to express the digital information they process. It's called the binary code because it.
Perl Subroutines User Input Perl on linux Forks and Pipes.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
The full name of PERL is Practical extraction and report language. It is similar to shell script and lot easier & powerful language. Perl is free to download.
Teaching Materials by Ivan Ovcharenko
Variables, Expressions, and IO
Perl for Bioinformatics
Control Structures: for & while Loops
T. Jumana Abu Shmais – AOU - Riyadh
Faculty of Computer Science & Information System
Introduction to Computer Science
Presentation transcript:

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 The “3 minute popcorn” program: TIME 300 START commands parameter

What is a computer? InputOutput 0 = off 1 = on

What is Perl? Perl is a computer language Easier to use than binary code print 2+3; Perl Interpreter

Where do I get Perl? Course web site for download links and instructions: Windows: ActivePerl MacOS: MacPerl Unix/Linux (FAS): Already Installed

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

What does Perl look like? #!/usr/local/bin/perl print “Hello everyone\n”; Mandatory first line ! Draw something to the screen

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

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

Perl Calculator $c = 4 - 2; $c = 2 * 2; $c = 2 / 2; $c = 2 ** 4;- power : 2^4 2 4 =16 $c = 1.35 * 2 – log(3) / ( ); - subtract - multiply - divide natural log

Perl Calculator : Output print " = ". (2+2). "\n"; $c = 2 + 2; print " = $c \n"; print the value of $c = 4 or… strings expression concatenate. output

Datatypes # DEFINING NUMBERS $x = 4; $x = 1.25; $x = 2.345e-56; # DEFINING STRINGS $x = “ACTGGTA”; $y = “Hello everyone \n”; * *10**-56 Perl Examples: # - comment line (Perl ignores)

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

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”; } A AA AAA Source code: Indent = TA

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 ) { … } then do this - commands inside curly brackets

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:

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:

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 truedo thisotherwisedo this

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

Data Structures : Arrays # array of 5 = (7,3,4,-1,0); # array of = (“Mon”, “Tue”, “Wed”, “Thu”, indicates an array(…)- a list of values $a[2]- an element of the array (count from 0)

Data Structures : = (7,3,4,-1); # change the value of an element $a[1] = 5; # print all the elements in the for ($i=0; $i<=$#a; $i+=1) { print “a[$i] = $a[$i] \n”; } $#a- the index of the last element in the a[0] = 7 a[1] = 5 a[2] = 4 a[3] = -1

Two Dimensional Arrays # A 2x2 array of 9 = ([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

Working with Files Microsoft Word Open Edit Save Close Perl Files Open Read or Write (one line at a time) Close

Working with Files How to open and close a file “data.txt” from a perl program? # open data.txt file for READING open (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 handler close (FILE); - WRITE to a file

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.

Working with Files # open file data.txt for reading open (FILE, “<data.txt”); # read file line by line and print it out to the screen while ($line = ) { print “$line”; } #close file close(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

Working with Files Example. Calculating a sum of numbers in the file data.txt: #!/usr/local/bin/perl $sum = 0; open (FILE, “<data.txt”); while ($line = ) { chomp($line); $sum = $sum + $line; } close(FILE); print “Sum of the numbers in data.txt file is $sum\n”; Sum of the numbers in data.txt file is 44 chomp command removes “\n” (new line) symbol from the string

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.

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

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

Functions (Subroutines) $x = min(5,3); print “Smallest of 5 and 3 is: $x\n”; # Function min sub 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

Modules Perl 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 databases -- lots of Perl modules use X; - place near the beginning of your program tells Perl to use all the functions in module X

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

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

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

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

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

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

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

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