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

Slides:



Advertisements
Similar presentations
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.
Advertisements

Programming and Perl for Bioinformatics Part III.
10.1 References & Complex Data Structures Variable types in PERL ScalarArrayHash $number $string %hash $reference
PHP (2) – Functions, Arrays, Databases, and sessions.
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:
9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
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…
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.
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
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.
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);
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · references · complex data structres.
Computer Programming for Biologists Class 7 Nov 27 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.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
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.
Built-in Data Structures in Python An Introduction.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
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.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
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 Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
Maps Nick Mouriski.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
int [] scores = new int [10];
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
3.5 Perform Basic Matrix Operations Add Matrices Subtract Matrices Solve Matric equations for x and y.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut
References and Data Structures
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Introduction to Perl: Part II
Modules and BioPerl.
Data types: Complex types (Dictionaries)
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Creation, Traversal, Insertion and Removal
int [] scores = new int [10];
CS 1111 Introduction to Programming Fall 2018
COS 151 Bootcamp – Week 4 Department of Computer Science
Presentation transcript:

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

10.2 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 modifying : $hash{bob} = "aaa"; (modifying an existing value) adding : $hash{555} = "z"; (adding a new key-value pair) Hash – an associative array %hash 5"a" => "zzz""bob" => "John"50 =>

10.3 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 5 "zzz" "a" "bob" 50

10.4 You can use combinations of hashes (and arrays) together to construct more complex data structures. If the information is best represented in two levels it is useful to use a hash within a hash: my %hash; $hash{Key_level_1}{Key_level_2}; Hash within Hash

10.5 For example: for each name in the phone book, we want to store both the phone number and the address: my %phoneBook; $phoneBook{'Dudu'}{'Phone'} = " "; $phoneBook{'Dudu'}{'Address'} = "115 Menora St., Hulun"; $phoneBook{'Ofir'}{'Phone'} = " "; $phoneBook{'Ofir'}{'Address'} = "31 Horkanus St., Eilat"; Hash within Hash

8ex.6 Class exercise 10a 1. Write a script that reads a file with a list of protein names, lengths and location: AP_ Nuc AP_ Cyt AP_ Cyt and 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" 2. Use the phoneBook.pl example and change it such that for each name in the phone book, the user enters the following data:phoneBook.pl » Phone number » Address » ID number a. In the input section: ask for a name and it's corresponding phone, address and ID. b. In the retrieval section: ask for a name and a data type, print the requested detail associated with the given name (e.g. Dudu's phon number).

10.7 References and complex data structures

10.8 A reference to a variable is a scalar value that “points” to the variable: $nameRef = = (85,91,67); $gradesRef = $phoneBookRef = \%phoneBook; References $phoneBookRef %phoneBook $gradesRef$nameRef$name

10.9 A reference to a variable is a scalar value that “points” to the variable: $nameRef = = (85,91,67); $gradesRef = $phoneBookRef = \%phoneBook; We can make an anonymous reference without creating a variable with a name: [ITEMS] creates a new, anonymous array and returns a reference to it; {ITEMS} creates a hash: $arrayRef = [85,91,67]; $hashRef = {85=>4,91=>3}; (These are variables with no variable name) $gradesRef$arrayRef

10.10 $nameRef = \$name; $gradesRef = $phoneBookRef = \%phoneBook; print $gradesRef;ARRAY(0x225d14) To access the data from a reference we need to dereference it: print $$nameRef;Yossi print $$gradesRef[3] = 100; print $phoneNumber = $$phoneBookRef{"Yossi"}; De-referencing 100 was added to the original $gradesRef

10.11 $gradesRef = $phoneBookRef = \%phoneBook; print $$gradesRef[3] = 100; $phoneNumber = $$phoneBookRef{"Yossi"}; The following notation is equivalent, and sometimes it is more readable: $gradesRef->[3] = 100; $phoneNumber = $phoneBookRef->{"Yossi"}; $gradesRef

10.12 Because a reference is a scalar value, we can store a reference to an hash in as an element in another hash: my %phoneBook; my %dudu = ('Phone' => " ", 'Address' => "Hulun"); $phoneBook{'dudu'} = \%dudu; Or with an anonymous hash: $phoneBook{'Shmuel'} = {'Phone' => " ", 'Address' => "Yavne"}; References allow complex structures - hash within hash %phoneBook NAME => {Phone => PHONE Address => ADDRESS} %phoneBook =>

10.13 Because a reference is a scalar value, we can store a reference to an hash in as an element in another hash: my %phoneBook; my %dudu = ('Phone' => " ", 'Address' => "Hulun"); $phoneBook{'dudu'} = \%dudu; Now the key “dudu” is paired to a reference value: print $phoneBook{"dudu"}; HASH(0x22e714) print "%{$phoneBook{"dudu"}}"; Phone AddressHulun print ${$phoneBook{"dudu"}}{"Phone"}; print $phoneBook{"Yossi"}->{"Phone"}; print $phoneBook{"Yossi"}{"Phone"}; This form is more readable, we strongly recommend it… References allow complex structures - hash within hash %phoneBook NAME => {Phone => PHONE Address => ADDRESS} %phoneBook =>

10.14 Now we can do it: “how to keep the phone number, address and list of grades for each student in a course?” $phoneBook{"dudu"} = {"Phone"=>3744, "Address"=>"34 HaShalom St.", "Grades"=>[93,72,87]}; print $phoneBook{"dudu"}->{"Grades"}->[2]; 87 It is more convenient to use a shorthand notation: print $phoneBook{"dudu"}{"Grades"}[2] But remember that there are references in there! References allow complex structures - array within hash within hash… %phoneBook => %phoneBook NAME => { " Phone " => PHONE " Address " => ADDRESS " Grades " => [GRADES]}

10.15 The following code is an example of iterating over two levels of the structure – The top hash (each student) and the internal arrays (lists of grades): foreach my $name (keys(%students)) { foreach my $grade { print $grade; } References allow complex structures %students => %students NAME => { " phone " => PHONE " address " => ADDRESS " grades " => [GRADES]}

10.16 When building a complex data structure in some loop (for example) you may come across a problem if you insert a non-anonymous array or hash into the data structure: my ($line, %students); while ($line = ) =... $students{$id} = } Let’s see what happens when we enter the lines: a b c The REUSED_ADDRESS problem This is the address (memory allocation) This is the re-use

10.17 The debugger will show you that there is a problem: The REUSED_ADDRESS problem

10.18 The problem is that for every student we store a reference to the same array. We have to create new array in every iteration: 1. We could declare (with my) the array inside the loop, so that a new one is created in every iteration: while ($line = ) { =... $students{$id} = } 2. Or, use an anonymous array reference: $students{$id} = [$grade1, $grade2]; or: $students{$id} = The REUSED_ADDRESS problem (Note: You may have this problem with the multiple #RP fields in ex5.5) Re-allocate memory

8ex.19 %genes PRODUCT => { " protein_id " => PROTEIN_ID " strand " => STRAND " CDS " => [START, END]} %genes PRODUCT => { " protein_id " => PROTEIN_ID " strand " => STRAND} %genes PRODUCT => { " protein_id " => PROTEIN_ID} Class exercise 10b 1. Read the adenovirus genome file and build a hash of genes, where the key is the "product" name: For each gene store a hash with the protein ID. Print all keys (names) in the hash. 2. Add to the hash the strand of the gene on the genome: “ + ” for the sense strand and “ - ” for the antisense strand. Print all antisense genes. 3. Add to the hash an array of two coordinates – the start and end of the CDS. Print genes shorter than 500bp. 4. Print the product name of all genes on the sense strand whose CDS spans more than 1kbp, and all genes on the antisense strand whose CDS spans less than 500bp.