Presentation is loading. Please wait.

Presentation is loading. Please wait.

10.1 References & Complex Data Structures. 10.2 Variable types in PERL ScalarArrayHash $number -3.54 $string %hash $reference

Similar presentations


Presentation on theme: "10.1 References & Complex Data Structures. 10.2 Variable types in PERL ScalarArrayHash $number -3.54 $string %hash $reference"— Presentation transcript:

1 10.1 References & Complex Data Structures

2 10.2 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash $reference 0x225d14 @array1 %hash @array2 @array3

3 10.3 Referencing array : $arrayRef = [@grades]; $gradesRef = \@grades; (careful) Referencing – Dereferencing Arrays Dereferencing array : @arr = @{$arrRef}; $element1 = $arrRef->[0]; BCA @grades $gradesRef BCA $arrRef BCA @arr $element1 = $arrRef->[0] = A

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

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

6 10.6 Get all the grades of Eyal: print $gradeHash{"Eyal"}; ARRAY(0x316c23) my @EyalGrades = @{$gradeHash{"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" 916785 82100 99775672 To get the array use @{$reference} Use ->[ x ] to get to the x element of the referenced array

7 10.7 Get sorted grades of Eyal: my @sortedGrades = sort(@{$gradeHash{"Eyal"}}); Push another grade to Neta: my $grade = 97; push (@{$gradeHash{"Neta"}},$grade); More de-referencing examples %gradesHash "Eyal" "Neta" "Era" 916785 82100 99775672 97

8 10.8 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 10.9 %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"

10 10.10 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" Use ->{ key } to get the value of key in the referenced hash To get the hash use %{$reference}

11 10.11 %bookHash References You can think of it as folders that contain inner folders that contains some data… $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" $bookHash{"Eyal"}{"phone"} = 5012; $bookHash{"Eyal"}{"address"} = "Swiss"; $bookHash{"Neta"}{"phone"} = 6023; $bookHash{"Neta"}{"address"} = "Yavne"; Change Neta's address: $bookHash{"Neta"}{"address"} = "Tel-Aviv"; Change Eyal's phone: $bookHash{"Eyal"}{"phone"} = 2209;

12 10.12 The general structure of the data structure: # $bookHash{$name}{"address"} = $address # $bookHash{$name}{"phone"} = $phone Get all the phones: @names= keys(%bookHash) foreach my $name (@names){ print "Phone of $name: "; print $bookHash{$name}{"phone"}."\n"; } De-referencing examples

13 10.13 Class exercise 10a (=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_000081181Nuc AP_000174104Cyt 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.

14 10.14 The general structure of the data structure: # $bookHash{$name}{"address"} = $address # {"phone"} = $phone # {"grades"} = [ @grades ] my %bookHash; $bookHash{"Eyal"}{"phone"} = 5012; $bookHash{"Eyal"}{"address"} = "Swiss"; my @grades = (85,91,67); $bookHash{"Eyal"}{"grades"} = [@grades]; More complex data structures

15 10.15 The general structure of the data structure: # $bookHash{$name}{"address"} = $address # {"phone"} = $phone # {"grades"} = [ @grades ] my %bookHash; $bookHash{"Eyal"}{"phone"} = 5012; $bookHash{"Eyal"}{"address"} = "Swiss"; $bookHash{"Eyal"}{"grades"}[0] = 85; $bookHash{"Eyal"}{"grades"}[1] = 91; $bookHash{"Eyal"}{"grades"}[2] = 67; More complex data structures

16 10.16 The general structure of the data structure: # $bookHash{$name}{"address"} = $address # {"phone"} = $phone # {"grades"} = [ @grades ] $bookHash{"Neta"}{"phone"} = 6023; $bookHash{"Neta"}{"address"} = "Yavne"; @grades = (100,82); $bookHash{"Neta"}{"grades"} = [@grades]; More complex data structures

17 10.17 The general structure of the data structure: # $bookHash{$name}{"address"} = $address # {"phone"} = $phone # {"grades"} = [ @grades ] $bookHash{"Era"}{"phone"} = 2209; $bookHash{"Era"}{"address"} = "Tel-Aviv"; @grades = (56,99,77); $bookHash{"Era"}{"grades"} = [@grades]; More complex data structures

18 10.18 The general structure of the data structure: # $bookHash{$name}{"address"} = $address # {"phone"} = $phone # {"grades"} = [ @grades ] Now let's print the phone and average of each one… More complex data structures

19 10.19 Now let's print the phone and average of each one… my @names = keys (%bookHash); foreach my $name (@names){ print "Phone of $name: $bookHash{$name}{phone}\n"; my @grades = @{ $bookHash{$name}{"grades"} }; my $sum = 0; foreach my $grade (@grades){ $sum = $sum + $grade; } my $avr = $sum / scalar(@grades); print "Average of $name: $avr\n"; } More complex data structures Phone of Era: 2209 Average of Era: 77.3333 Phone of Eyal: 5012 Average of Eyal: 81 Phone of Neta: 6023 Average of Neta: 91

20 10.20 Class exercise 10b 1.Write a script that reads a file with a list of protein names, lengths, location and expression levels (such as in proteinFullData.txt ), with lines such as: AP_000081181Nuc0.02,0.41,0.34,0.05,0.04 AP_000138145Cyt0.27,0.43,0.20 Stores the names of the sequences as hash keys, and use "length", "location" and "levels" 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". $proteins{"AP_000081"}{"levels"} should be an array with 0.02 in its first element 0.41 in its second element, and so on. a)Ask the user for a protein name and print its length and location and sorted levels. b)Print for each protein its name, location and the average of its levels.

21 10.21 Class exercise 10b 2.Add to the script of 10a question 2b a key to the inner hash containing the CDS coordinates, such that the structure of the data structure is as follows: $gbHash{"product"}{"protein_id"} = $protein_id $gbHash{"product"}{"db_xref"} = $db_xref $gbHash{"product"}{"strand"} = $strand (+/-) $gbHash{"product"}{"CDS"} = [ @CDS ] a)Ask the user for a product, and print its protein_id, db_xref and CDS coordinates. NOTE: for protein coded on the negative strand print the coordinates reversed. b)print all the product names of the proteins coded on the positive strand that starts after coordinate 2000

22 10.22 What about even more levels of hashes? For example:  Hash of names in which there are: o phone o address and the address has:  street name  number of house  city To Infinity and Beyond!!

23 10.23 What about even more levels of hashes? # $book{$name} {"phone"} = $phone # {"address"}{"street"} = $street # {"number"} = $number # {"city"} = $city To Infinity and Beyond!!

24 10.24 What about even more levels of hashes? my %bookHash; $bookHash{"Eyal"}{"phone"} = 5012; $bookHash{"Eyal"}{"address"}{"street"} = "Baugenhof St."; $bookHash{"Eyal"}{"address"}{"number"} = "31"; $bookHash{"Eyal"}{"address"}{"city"} = "Lausanne"; To Infinity and Beyond!!

25 10.25 What about an array of addresses?? Well… we know how to do that… # $book{$name} {"phone"} = $phone # {"address"} = [@addresses] To Infinity and Beyond!!

26 10.26 What about an array of addresses?? Well… we know how to do that… # $book{$name} {"phone"} = $phone # {"address"}[$i] = $address_i To Infinity and Beyond!!

27 10.27 What about an array of addresses?? Well… we know how to do that… my %bookHash; $bookHash{"Eyal"}{"phone"} = 5012; $bookHash{"Eyal"}{"address"}[0] = "Swiss"; $bookHash{"Eyal"}{"address"}[1] = "Yavne"; To Infinity and Beyond!!

28 10.28 What about an array of addresses, each containing data of street and city ??!!??! # $book{$name} {"phone"} = $phone # {"address"}[$i] = $address_i To Infinity and Beyond!!

29 10.29 What about an array of addresses, each containing data of street and city ??!!??! # $book{$name} {"phone"} = $phone # {"address"}[$i]{"street"} = $street_i # {"address"}[$i]{"city"} = $city_i To Infinity and Beyond!!

30 10.30 What about an array of addresses, each containing data of street and city ??!!??! my %bookHash; $bookHash{"Eyal"}{"address"}[0]{"street"} = "Baugenhof 7"; $bookHash{"Eyal"}{"address"}[0]{"city"} = "Lausanne"; $bookHash{"Eyal"}{"address"}[1]{"street"} = "Hetzel 21"; $bookHash{"Eyal"}{"address"}[1]{"city"} = "Yavne"; To Infinity and Beyond!!

31 10.31 Do you think it is possible to make matrices in Perl? # $matrix[$i][$j] = $a_ij; my @matrix; $matrix[0][0] = 1; $matrix[0][1] = 2; $matrix[0][2] = 3; $matrix[1][0] = 4; $matrix[1][1] = 5; $matrix[1][2] = 6; $matrix[2][0] = 7; $matrix[2][1] = 8; $matrix[2][2] = 9; Three dimensional matrices? The matrix 231564897 @matrix 231 564 897


Download ppt "10.1 References & Complex Data Structures. 10.2 Variable types in PERL ScalarArrayHash $number -3.54 $string %hash $reference"

Similar presentations


Ads by Google