9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number -3.54 $string %hash => $array[0] $hash{key}

Slides:



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

Advanced Perl WUC Perl Poetry Variable Scope.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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.
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
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.
10.1 References & Complex Data Structures Variable types in PERL ScalarArrayHash $number $string %hash $reference
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
11ex.1 Modules and BioPerl. 11ex.2 sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } my $revSeq = reverseComplement("GCAGTG");
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:
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.
Sup.1 Supplemental Material (NOT part of the material for the exam)
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
14.1 Wrapping up Revision 14.3 References are your friends…
7.1 Some Eclipse Tips Try Ctrl+Shift+L Quick help (keyboard shortcuts) Try Ctrl+SPACE Auto-complete Source→Format ( Ctrl+Shift+F ) Correct indentation.
13.1 Wrapping up Running Other Programs 13.3 You may run programs using the system function: $exitValue = system("blastall.exe..."); if ($exitValue!=0)
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.
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
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.
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
Advanced Perl for Bioinformatics Lecture 5. Regular expressions - review You can put the pattern you want to match between //, bind the pattern to the.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
Basic Perl Programming
Computer Programming for Biologists Class 7 Nov 27 th, 2014 Karsten Hokamp
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
9 1 DBM Databases 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.
Data Structures Using Java1 Chapter 4 Linked Lists.
Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;
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.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
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),
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.
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.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Maps Nick Mouriski.
C++ Review STL CONTAINERS.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
CSE 143 Lecture 11: Sets and Maps reading:
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Perl References arrays and hashes can only contain scalars (numbers and strings)‏ if we want something more complicated (like an array of arrays) we use.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
References and Data Structures
Introduction to Perl: Part II
Modules and BioPerl.
Perl Variables: Hash Web Programming.
Recitation Outline C++ STL associative containers Examples
Presentation transcript:

9.1 Hash revision

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

9.3 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; Inserting values: $hash{"a"} = 5; $hash{"bob"} = "zzz"; $hash{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 = (); Hash – an associative array %hash 5"a" => "zzz""bob" => "John"50 =>

9.4 modifying : $hash{bob} = "aaa"; (modifying an existing value) 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" =>

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

9.6 References & Complex Data Structures

9.7 References are your friends…

9.8 Variable types in PERL ScalarArrayHash $number $string %hash $reference @array3

9.9 A reference to a variable is a scalar value that “points” to another variable. creates a copy of the array and return a reference to this copy: = (85,91,67); my $arrayRef = References $arrayRef

9.10 A reference to a variable is a scalar value that “points” to another variable. creates a copy of the array and return a reference to this copy: = (85,91,67); my %gradeHash; $gradeHash{"Eyal"} = References %gradesHash "Eyal"

9.11 A reference to a variable is a scalar value that “points” to another variable. creates a copy of the array and return a reference to this copy: = (85,91,67); my %gradeHash; $gradeHash{"Eyal"} = (100,82); $gradeHash{"Neta"} = (56,99,77); $gradeHash{"Era"} = %gradesHash "Eyal" References example %gradesHash "Eyal" "Neta" %gradesHash "Eyal" "Neta" "Era"

9.12 A reference to a variable is a scalar value that “points” to another variable. return a reference to the array itself. THIS MIGHT BE DANGEROUS. = (85,91,67); my $arrayRef = my %gradeHash; $gradeHash{"Eyal"} = References $arrayRef %gradesHash "Eyal"

9.13 A reference to a variable is a scalar value that “points” to another variable. return a reference to the array itself. THIS MIGHT BE DANGEROUS. = (85,91,67); my %gradeHash; $gradeHash{"Eyal"} = (100,82); $gradeHash{"Neta"} = (56,99,77); $gradeHash{"Era"} = %gradesHash "Eyal" %gradesHash "Eyal" "Neta" %gradesHash "Eyal" "Neta" "Era" References (bad) example

9.14 To access the data from a reference we need to dereference it: = (85,91,67); my $arrayRef = print $arrayRef;ARRAY(0x225d14) print De-referencing $arrayRef To get the array

9.15 To access the data from a reference we need to dereference it: = (85,91,67); my $arrayRef = my $firstGrade = $arrayRef->[0]; print $firstGrade; 85 De-referencing $arrayRef Use ->[ x ] to get to the x element of the referenced array

9.16 Get all the grades of Eyal: Get second grade of Neta: my $Neta2 = $gradeHash{"Neta"}->[1]; Change first grade of Era: $gradeHash{"Era"}->[0] = 72; De-referencing examples %gradesHash "Eyal" "Neta" "Era"

9.17 Get sorted grades of Eyal: = Push another grade to Neta: my $grade = 97; push More de-referencing examples %gradesHash "Eyal" "Neta" "Era"

9.18 Referencing array : $arrayRef = $gradesRef = (careful) Referencing – Dereferencing Arrays Dereferencing array $element1 = $arrRef->[0]; $gradesRef BCA $arrRef $element1 = $arrRef->[0] = A

9.19 Class exercise 9a 1.Write a script that reads a file with a list of protein names, and their levels measured in different time points, such as: AP_ ,0.54,0.90,0.04,0.04 AP_ ,0.20,0.50 Store the information in a hash. The names of the proteins as hash keys, and the protein levels as referenced arrays. a)Ask the user for a protein name and print out the sorted array of the levels measured of that protein. For example, if the user enter AP_000155, The script should print b)Ask the user for a protein name and a protein level, and add this level as the last measurement of the appropriate protein, and print out the updated array of level. 2. Read the adenovirus genome file and build a hash of genes, where the key is the "product" name and the CDS start and end coordinates are an array referenced to by that key. Ask the user for a product and print its coordinated. For example if the user types " E1B 19K ", the script should print out: " ". (note that the CDS lines appear before the product line…)

9.20 A reference to a variable is a scalar value that “points” to another variable. {%hash} creates a copy of the hash and return a reference to this copy: my %details; $details{"phone"} = 5012; $details{"address"} = "Swiss"; my $hashRef = {%details}; References 5012"Phone" "Swiss""Addrs" %details 5012"Phone" "Swiss""Addrs" $hashRef

9.21 A reference to a variable is a scalar value that “points” to another variable. {%hash} creates a copy of the hash and return a reference to this copy: my %details; $details{"phone"} = 5012; $details{"address"} = "Swiss"; my %bookHash; $ bookHash{"Eyal"} = {%details}; References 5012"Phone" "Swiss""Addrs" %details 5012"Phone" "Swiss""Addrs" %bookHash "Eyal"

9.22 %bookHash my %details; $details{"phone"} = 5012; $details{"address"} = "Swiss"; my %bookHash; $bookHash{"Eyal"} = {%details}; $details{"phone"} = 6023; $details{"address"} = "Yavne"; $bookHash{"Neta"} = {%details}; References example 5012"Phone" "Swiss""Addrs" %details 5012"Phone" "Swiss""Addrs" "Eyal" 6023"Phone" "Yavne""Addrs" 6023 "Yavne" "Neta"

9.23 %bookHash References example Another way to build the same data structure: $bookHash{"Eyal"}->{"phone"} = 5012; $bookHash{"Eyal"}->{"address"} = "Swiss"; $bookHash{"Neta"}->{"phone"} = 6023; $bookHash{"Neta"}->{"address"} = "Yavne"; 5012"Phone" "Swiss""Addrs" "Eyal" 6023"Phone" "Yavne""Addrs" "Neta"

9.24 To access the data from a reference we need to dereference it: my $hashRef; $hashRef->{"Phone"} = 5012; $hashRef->{"Address"} = "Swiss; my %details = %{$arrayRef}; = values (%details); print 5012 Swiss De-referencing To get the hash use %{$reference} 5012"Phone" "Swiss""Addrs" $hashRef 5012"Phone" "Swiss""Addrs" %details

9.25 To access the data from a reference we need to dereference it: my $hashRef; $hashRef->{"Phone"} = 5012; $hashRef->{"Address"} = "Swiss; my $phone = $ hash Ref->{"Phone"}; print $phone; 5012 De-referencing 5012"Phone" "Swiss""Addrs" $hashRef Use ->{ key } to get the value of key in the referenced hash

9.26 Get all the details of Neta: my %NetaDetails= %{$bookHash{"Neta"}} Get the phone of Eyal: my $EyalPhone = $bookHash{"Eyal"}->{"Phone"}; De-referencing examples %bookHash 5012"Phone" "Swiss""Addrs" "Eyal" 6023"Phone" "Yavne""Addrs" "Neta"

9.27 Change Neta's address: $bookHash{"Neta"}->{"Address"} = "Tel-Aviv"; Get all the keys(%bookHash) forach my $name print "Phone of $name: "; print $bookHash{$name}->{"Phone"}."\n"; } De-referencing examples %bookHash 5012"Phone" "Swiss""Addrs" "Eyal" 6023"Phone" "Yavne""Addrs" "Neta" "TelAviv"

9.28 Referencing hash : $hashRef = {%phoneBook}; $bookRef = \%phoneBook; (careful) Referencing – Dereferencing Hashes Dereferencing hash : %hash = %{$hashRef}; $myVal = $hashRef->{"A"}; $bookRef %phoneBook XA YB ZC $hashRef XA YB ZC %hash XA YB ZC $myVal = $hashRef->{"A"} = "X"

9.29 Class exercise 9b 1.Write a script that reads a file with a list of protein names, lengths and location (such as in proteinLengthsAndLocation.txt ), with lines such as: AP_ Nuc AP_ Cyt Stores the names of the sequences as hash keys, and use "length" and "location" as keys in an internal hash for each protein. For example: $proteins{"AP_000081"}->{"length"} should be 181 $proteins{"AP_000081"}->{"location"} should be "Nuc". a)Ask the user for a protein name and print its length and location. b)Print for each protein its name and location. 2.Read the adenovirus GenBank file and build a hash of genes, where the key is the product name: For each gene store an internal hash with two keys, one contains the protein_id and the other contains the db_xref. a)Ask the user for a product, and print its protein_id and db_xref. b)Use the CDS line to decide whether the coding sequence is on the positive or negative stands (" complement " before the coordinates marks a sequence coded on the negative strand). Add a key strand to the hash of each gene that contains "+" if the coding sequence is coded on the positive strand or "-" if it is on the negative. print all the product names of the proteins coded on the negative strand.