Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

File Handle and conditional Lecture 2. A typical bioinformatics file: FASTA format Name of the following file is: DNA_sequence.fasta >gi|34529|emb|Y |
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
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.
Practical Extraction & Report Language Picture taken from
CSC3530 Software Technology Tutorial Two PERL Basics.
Getting Started with Perl (and Excel) Biophysics 101 September 17, 2003 Griffin Weber (With material from Jon Radoff and Ivan Ovcharenko)
Places To Put Things Exploring Perl's Built-In Variable Containers: Arrays and Hashes.
CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Practical Extraction & Report Language PERL Joseph Beltran.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Lecture 8 perl pattern matching features
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Bioinformatics 生物信息学理论和实践 唐继军
Books. Perl 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.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
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 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
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
5 1 Data Files CGI/Perl Programming By Diane Zak.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
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.
Getting started in Perl: Intro to Perl for programmers Matthew Heusser – xndev.com - Presented to the West Michigan Perl User’s Group.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
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.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
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.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Perl for Bioinformatics
Perl Variables: Array Web Programming.
An Introduction to Perl
Control Structures: for & while Loops
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
String and Lists Dr. José M. Reyes Álamo.
The Selection Structure
INTRODUCTION to PERL PART 1.
Presentation transcript:

Bioinformatics Lecture 7: Introduction to Perl

Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration – File handling and error handling – Arrays, lists and hashes

First program a basic Strings program: Test.pl – #!/usr/bin/perl – print "Hello boys and girs!\n this is introduction to perl"; Open with notepad and type the above Save file as hello.pl Ensure that hide file extensions option is unchecked. Run via the command line

Variables declarations $variable name : intergers, floats, arrays Arithmetic operators: – +, -, *, /, **( exponentation); % modulus Double v single quotation marks – $x = ‘ I am from Cork ‘ – print “the value of $x is $x\n” – print ’the value of $x is $x\n’ – print “the value of \$x is $x\n” # note the \$x – #evaluating expressions in print (# comment line symbol) – $ x = 15; – Print “the value of x is “, $x + 3, “\n” (ArithmeticExample.pl)

Input, output and files handling Input – $var = <> (input a line of text and assign it to $var): also iputs return character – Chomp $var removes the return character from the #also used the word chop – Alternatively chomp($var = <>); – $line = reads in “hardcoded data” Output – print (already covered) File Handling – open MYFILE, ‘data.txt’ (open file for reading;) – open MYFILE, ‘>data.txt’ (open file for writing) – Open MYFILE, ‘>> data.txt’ (open file for appending) – $line = #read one line from file = ; (called slurping) #reads all the file into an array – print MYFILE “Do you like computers….”, $number/3, “\n” # write out to file – close MYFILE;

Conditional Operator == Equality $a == $b != Not equal $a != $b < Less than $a < $b > Greater than $a > $b <= Less than or equal to $a <= $b >= Greater than or equal to $a >= $b ! Logical not $ = !$b

String conditional operator eq Equality $a eq $b ne Not equal $a ne $b lt Less than $a lt $b gt Greater than $a gt $b le Less than or equal to $a le $b ge Greater than or equal to $a ge $b.Concatenation $a.$c =~ Pattern match $a =~ /gatc/

Conditional statements If and elseif and else if_else.pl #!/usr/bin/perl print “Enter your age: ”; $age = <>; if ($age <= 0) { print “You are way too young to be using a computer.\n”; } elseif ($age >= 100) { print “Not in a dog’s life!\n”; } else { print “Your age in dog years is ”,$age/7,“\n”; }

Iteration: loops While-loops – #!/usr/bin/perl – $count = 1; – while ($count <= 5) { – print “$count potato\n”; – $count = $count + 1; – } Until-loops – #!/usr/bin/perl – $count = 1; – until ($count > 5) { – print “$count potato\n”; – $count = $count + 1; – }

Loops with defined #!/usr/bin/perl # defined fnt is true if $line assigned a value print “Type something. ‘quit’ to finish\n ”; while ( defined($line = <>) ) { – chomp $line; – last if $line eq ‘quit’; # breaks out of loop at quit – print “You typed ‘$line’\n\n”; – print “Type something> ”; } print “goodbye!\n”; loops_defined.pl

Shorthand input notation #!/usr/bin/perl print “Type something. ‘quit’ to finish\n ”; while (<>) { – chomp; # $_ generic variable name – last if $_ eq ‘quit’; – print “You typed ‘$_ ’\n\n”; – print “Type something> ”; } print “goodbye!\n”;

Change Standard input/ output redirect Sdout to a file – U:\test test.pl > stdout.txt [produces a text file ] print file goes to file and not to screen Run Loops_defined to redirect to output to file The <> input has one feature where if a file name is on the command line it beings to read from it otherwise it reads from keyboards – U:\test commandline.pl stdin.txt

Finding length of file #!/usr/bin/perl #File_size_1.pl # file size.pl $length = 0; # set length counter to zero $lines = 0; # set number of lines to zero print “enter text one line at a time and press (ctrl z) to quit”; while (<>) { # read file one line at a time – chomp; # remove terminal newline – $length = $length + length $_ ; – $lines = $lines + 1; } print “LENGTH = $length\n”; print “LINES = $lines\n”; Try using keyboard as Stdin (ctrl Z) and file name on command line

Dynamic Arrays Declaration of an array in perl = (‘123a’, ‘23ed4’, ‘2334d’); – Array contains 3 strings!!! Array operations: – $one_seq {zero based array} assigns arrays = ‘125f’); adding an value – Removing = 1, 2 – slicing Splice_slice_array.pl

Dynamic Arrays – ‘2345d’; (adds element to end of array) – removes and returns (function returns) last element of array – Shifting: removes and returns the first element of an array. – Unshifting: Adds an element or list of elements onto the beginning of an array.

Shift Pop push unshift example #! /usr/bin/perl # The 'pushpop' program - pushing, popping, shifting and = ( 'TTATTATGTT', 'GCTCAGTTCT', 'GACCTCTTAA', 'CTATGCGGTA', 'ATCTGACCTC' ); print $last = print $first = print $last; print ( $first, $last ); print What is the expected output (run code to confirm)

Arrays: two more functions Substr (extracting a substring from a string) – $sub = substr ($string, offset position[position to begin extraction], size of substring) Substr and index: To obtain the reverse complement of a DNA sequence: assume the sequence is stored in array: (GGGGTTTT becomes AAAACCCC) Iterating through an array: – foreach $dna – { $dna = reverse $dna; # reverse the contents of a scalar $dna $dna =~ tr/gatcGATC/ctagCTAG/; – # tr (translate first set into second; e.g. g becomes c ) complement (replace) – }

Questions how would you read in a file of DNA sequence into an array and print both the original and reverse complementary copy What use could this program have? (biology related answer)

Array and lists Lists are an array of constants or variables – Values of a list assigned to any = (’192a8’,’18c10’,’327h1’,’201e4’); – Values in an array assigned to a list – ($first,$second,$third)

Hashes: associative arrays Similar arrays but elements are unordered – Two parts: the identifer (name), a scalar value (string) – Add Elements are referred to by strings: %oligos = (); $oligos{’192a8’} = ‘GGGTTCCGATTTCCAA’; $oligos{’18c10’} = ‘CTCTCTCTAGAGAGAGCCCC’; $oligos{’327h1’} = ‘GGACCTAACCTATTGGC’; – Note in the name part use ‘ ‘ – Removing elements: Delete $oligos{’192a8’};

Hashes Outputting hash results $s = $oligos{’192a8’}; print “oligo 192a8 is $s\n”; print “oligo 192a8 is ”,length $oligos{’192a8’},“ base pairs long\n”; print “oligo 18c10 is $oligos{’18c10’}\n”; Expected output: input_output_hash.pl oligo 192a8 is GGGTTCCGATTTCCAA oligo 192a8 is 16 base pairs long oligo 18c10 is CTCTCTCTAGAGAGAGCCCC

Hashes Example of the use of a Hash table – hash_bases.pl program For loops and hash tables – foreach $clone (’327h1’,’192a8’,’18c10’) { – print “$clone: $oligos{$clone}\n”; – } – %oligos is refers to the hash table – $oligos is used to refer to elements $size = keys %oligo; returns the number of entries

Displaying all entries in a hash table while ( ( $genome, $count ) = each %gene_counts ) { print "`$genome' has a gene count of $count\n"; } foreach $genome ( sort keys %gene_counts ) { print "`$genome' has a gene count of $gene_counts { $genome }\n";} Refer to genes.pl

Error Handling die function: open myfile, ‘stdin.txt’ or Die “could not open file aborting…\n”; – If file does not exits the program terminates with the above message Write a program to read in data from a file to an array and when all the data is input to output in reverse order Create a hash table that performs the condon to AA conversion and use it to convert codons {entered from the key board} into their corresponding Amino Acids