8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format),

Slides:



Advertisements
Similar presentations
A primer on Perl programming First structures (with examples)
Advertisements

Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Programming and Perl for Bioinformatics Part III.
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
4ex.1 More loops. 4ex.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) {
Scripting Languages Chapter 5 Hashes. Hash Data structure, not unlike an array – it will hold any number of values It indexes values by name – not by.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
5.1 Previously on... PERL course (let ’ s practice some more loops)
7ex.1 Hashes. 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal Enter a phone number:
9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
for($i=0; $i/)
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
8.1 References and complex data structures. 8.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
8.1 Hashes (associative arrays). 8.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Dudi Enter a phone.
10.1 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
8ex.1 References and complex data structures. 8ex.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
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.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
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";
3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority.
9.1 Hashes. 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: Enter.
4.1 More loops. 4.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) { $num.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
Computer Programming for Biologists Class 7 Nov 27 th, 2014 Karsten Hokamp
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Prof. Alfred J Bird, Ph.D., NBCT -bird.wikispaces.umb.edu/ Office – McCormick 3rd floor.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
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
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
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";
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Perl Day 5. Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
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.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
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.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
Introduction to Perl Jarrad Battaglia.
Perl Variables: Hash Web Programming.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
The Selection Structure
Presentation transcript:

8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format), you should only rely on the format for parsing and not on the variable part of the input. Thus parsing by features such as these is wrong: –Assuming each line in the title will start with a lowercase letter –Assuming the title will be composed of only 2 lines It is legitimate to rely on the presence of the words ‘TITLE’ and ‘JOURNAL’ for the parsing as these are a part of the format. Reading the whole file at once = ;). This is risky in case the file is very large… When we do not need all the lines in the file at once, we try to use $line = in a ‘while’ loop. Performing an action on a variable without checking if it is defined. This can generate errors in some cases. Use of functions/features not taught in class.

8.2 Solution to HW3 Q#6 For each protein record print the first line (the LOCUS line) followed by a sorted list of its reference TITLEs. 1. Read the file 2. if reached LOCUS line print it 3. if reached TITLE start an inner loop until reaching the JOURNAL line (to take the full title) 4. push entire TITLE to titles array 5. If reached a FEATURES line print the title array and initialize...

8.3 my $line = ; # read input lines while (defined $line){ chomp($line); # if reached LOCUS line print it if ((substr($line,0,5) eq "LOCUS") ) { print "\n$line\n"; } # if reached TITLE start an inner loop until reaching the JOURNAL line if ( (length($line) > 7) && (substr($line,2,5) eq "TITLE") ) { while ((defined $line) && (substr($line,2,7) ne "JOURNAL")) { chomp $line; $title = $title.substr($line,12); # concatenate the title line $line = ; } # push entire title to title array $title=""; } # if reached FEATURES line - sort and print titles array if ((substr($line,0,8) eq "FEATURES") ) = foreach $title { print "$title\n"; = (); # empty title array } $line = ; }

8.4 Hashes (associative arrays)

8.5 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Dudi Enter a phone number: Enter a name that will be added to the phone book: Dudu Enter a phone number: Hash Motivation

8.6 An associative array (or simply – a hash) is an unordered set of pairs of keys and values. Each key is associated with a value. A hash variable name always start with a “%”: my %hash; Initialization: %hash = ("a"=>5, "bob"=>"zzz", 50=>"John"); Accessing: you can access a value by its key: print $hash{50};John Tip you can reset the hash (to an empty one) by %hash = (); Note: a key in a hash will be interpreted as a string. These are equivalent: Hash – an associative array %hash 5"a" => "zzz""bob" => "John"50 => 50=>”John” “50”=>”John” $hash{50} $hash{“50”}

8.7 modifying : $hash{bob} = "aaa"; (modifying an existing value) adding : $hash{555} = "z"; (adding a new key-value pair) You can ask whether a certain key exists in a hash: if (exists $hash{50} )... You can delete a certain key-value pair in a hash: delete($hash{50}); Hash – an associative array %hash 5"a" => "zzz""bob" => "John"50 => %hash 5"a" => "aaa""bob" => "John"50 => %hash 5"a" => "aaa""bob" => "John"50 => "z"555 => %hash 5"a" => "aaa""bob" => "z"555 =>

8.8 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}

8.9 An associative array of the phone book suggested in the first slide (we will see a more elaborated version later on): Declare my %phoneBook; Updating $phoneBook{"Dudi"} = 9245; $phoneBook{"Dudu"} = 7693; Fetching print $phoneBook{"Dudi"}; Hash – an associative array %hash 9245"Dudi" => 7693"Dudu" =>

8.10 It is possible to get a list of all the keys in %hash = keys(%hash); Similarly you can get an array of the values in %hash = values(%hash); Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 "bob"50 5 "John" "zzz"

8.11 To iterate over all the values in %hash = values(%hash); foreach my $value To iterate over the keys in %hash = keys(%hash); foreach my $key Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 "bob"50 5 "John" "zzz"

8.12 For example, iterating over the keys in %hash : = keys(%hash); foreach my $key { print "The key is $key\n"; print "The value is $hash{$key}\n"; } Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 => The key is bob The value is zzz The key is a The value is 5 The key is 50 The value is "bob"50 5 "John" "zzz"

8.13 Notably: The elements are given in an arbitrary order, so if you want a certain order use sort: = keys(%hash); = foreach $key { print "The key is $key\n"; print "The value is $hash{$key}\n"; } Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 "bob"50 5 "John" "zzz"

8.14 ###################################### # Purpose: Store names and phone numbers in a hash, # and allow the user to ask for the number of a certain name. # Input: Enter name-number pairs, enter "END" as a name to stop, # then enter a name to get his/her number # use strict; my %phoneNumbers = (); my $number; Example – phoneBook.pl #1

8.15 # Ask user for names and numbers and store in a hash my $name = ""; while (1==1) { print "Enter a name that will be added to the phone book:\n"; $name = ; chomp $name; if ($name eq "END") { last; } print "Enter a phone number: \n"; $number = ; chomp $number; $phoneNumbers{$name} = $number; } Example – phoneBook.pl #2

8.16 # Ask for a name and print the corresponding number $name = ""; while (1==1) { print "Enter a name to search for in the phone book:\n"; $name = ; chomp $name; if (exists($phoneNumbers{$name})) { print "The phone number of $name is: $phoneNumbers{$name}\n"; } elsif ($name eq "END") { last; } else { print "Name not found in the book\n"; } Example – phoneBook.pl #3

8.17 Class exercise 8 1.Write a script that reads a file with a list of protein names and lengths (proteinLengths ): AP_ AP_ AP_ stores the names of the sequences as hash keys, with the length of the sequence as the value. Print the keys of the hash.proteinLengths 2.Add to Q1: Read another file, and print the names that appeared in both files with the same length. Print a warning if the name is the same but the length is different. 3.Write a script that reads a GenPept file (you may use the preproinsulin record), finds all JOURNAL lines, and stores the journal name (as key) and year of publication (as value) in a hash:the preproinsulin record a. Store only the first year (order of appearance in the file) value for each journal name b*.Store all years for each journal name Then print the names and years, sorted by the journal name (no need to sort the years for the same journal in b*, unless you really want to do so … )