complex data structures revision - references to arrays

35
10.1 Complex data structures revision - references to arrays %gradesHash " Eyal " 91 67 85 82 100 99 77 56 %gradesHash " Eyal " " Neta " %gradesHash " Eyal " " Neta " " Era "

Upload: fathia

Post on 18-Mar-2016

61 views

Category:

Documents


0 download

DESCRIPTION

56. 85. 100. 99. 91. 82. 77. 67. Complex data structures revision - references to arrays. %gradesHash. %gradesHash. %gradesHash. "Neta". "Neta". "Era". "Eyal". "Eyal". "Eyal". Variable types in PERL. $number -3.54. @array. $string "hi\n". $reference 0x225d14. Scalar. Array. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Complex data structures revision - references to arrays

10.1 Complex data structuresrevision - references to arrays

%gradesHash

"Eyal" 91 6785

82100

99 7756

%gradesHash

"Eyal"

"Neta"

%gradesHash

"Eyal"

"Neta"

"Era"

Page 2: Complex data structures revision - references to arrays

10.2 Variable types in PERLScalar Array Hash

$number-3.54

$string"hi\n"

@array %hash

$reference0x225d14

@array1

%hash

@array2

@array3

$arr_ref_1

$arr_ref_2

$arr_ref_3

Page 3: Complex data structures revision - references to arrays

10.3

A reference to a variable is a scalar value that “points” to another variable.[@array] creates a copy of the array and returns 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"

91 6785

@grades

82100

@grades

References example

91 6785

82100

99 7756

@grades

99 7756

%gradesHash

"Eyal"

"Neta"

%gradesHash

"Eyal"

"Neta"

"Era"

Page 4: Complex data structures revision - references to arrays

10.4

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"

91 6785

82100

99 775672

To get the array use @{$reference}

Use ->[x] to get to the x element of the

referenced array

Page 5: Complex data structures revision - references to arrays

10.5

This:$gradeHash{"Neta"}->[1]

And this:$gradeHash{"Neta"}[1]

Are equivalent!!!

Syntactic Sugar

Syntactic Sugar is syntax within a programming language designed to make things easier to read.

It makes the language "sweeter" for humans to use.

Above is an example in Perl

Page 6: Complex data structures revision - references to arrays

10.6

Referencing array :$arrayRef = [@grades];$gradesRef = \@grades; (careful)

Referencing – Dereferencing ArraysDereferencing array :@arr = @{$arrRef};$element1 = $arrRef->[0];

B CA

@grades$gradesRef

B CA$arrRef

B CA

@arr

$element1 = $arrRef->[0] = A

Page 7: Complex data structures revision - references to arrays

10.7 Complex data structuresreferences to Hashes

Page 8: Complex data structures revision - references to arrays

10.8

•A reference to a variable is a scalar value that “points” to another variable.•{%hash} creates a copy of the hash and returns a reference to this copy:

my %details;$details{"Phone"} = 5012;$details{"Addrs"} = "Swiss";

my $hashRef = {%details};

References

5012"Phone""Swiss""Addrs"

%details

5012"Phone""Swiss""Addrs"

$hashRef

Page 9: Complex data structures revision - references to arrays

10.9

•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{"Addrs"} = "Swiss";

my %bookHash;$ bookHash{"Eyal"} = {%details};

Example: phone book

5012"Phone""Swiss""Addrs"

%details

5012"Phone""Swiss""Addrs"

%bookHash

"Eyal"

Page 10: Complex data structures revision - references to arrays

10.10

%bookHash%bookHash

my %details;$details{"Phone"} = 5012;$details{"Addrs"} = "Swiss";my %bookHash;$bookHash{"Eyal"} = {%details};

$details{"Phone"} = 6023;$details{"Addrs"} = "Yavne";$bookHash{"Neta"} = {%details};

Example: phone book5012"Phone"

"Swiss""Addrs"

%details

5012"Phone""Swiss""Addrs"

"Eyal" 6023"Phone""Yavne""Addrs"

6023"Yavne"

"Neta"

Page 11: Complex data structures revision - references to arrays

10.11

%bookHash%bookHash

Example: phone bookAnother way to build the same data structure:$bookHash{"Eyal"}->{"Phone"} = 5012; $bookHash{"Eyal"}->{"Addrs"} = "Swiss";$bookHash{"Neta"}->{"Phone"} = 6023; $bookHash{"Neta"}->{"Addrs"} = "Yavne"; 5012"Phone"

"Swiss""Addrs"

"Eyal"

6023"Phone""Yavne""Addrs"

"Neta"

Page 12: Complex data structures revision - references to arrays

10.12

To access the data from a reference we need to dereference it:my $hashRef;$hashRef->{"Phone"} = 5012;$hashRef->{"Addrs"} = "Swiss";

my %details = %{$hashRef};my @vals = values (%details);print "@vals"; 5012 Swiss

De-referencing “%{}”

To get the hash use %{$reference}

5012"Phone""Swiss""Addrs"

$hashRef

5012"Phone""Swiss""Addrs"

%details

Page 13: Complex data structures revision - references to arrays

10.13

To access the data from a reference we need to dereference it:my $hashRef;$hashRef->{"Phone"} = 5012;$hashRef->{"Addrs"} = "Swiss;

my $phone = $hashRef->{"Phone"};print $phone; 5012

5012"Phone""Swiss""Addrs"

$hashRef

Use ->{key} to get the value of key in the referenced hash

De-referencing “%{}”

Page 14: Complex data structures revision - references to arrays

10.14

Get all the details of Neta:my %NetaDetails= %{$bookHash{"Neta"}}

Get the phone of Eyal:my $EyalPhone = $bookHash{"Eyal"}->{"Phone"};

De-referencing examples

%bookHash%bookHash5012"Phone"

"Swiss""Addrs"

"Eyal"

6023"Phone""Yavne""Addrs"

"Neta"

Page 15: Complex data structures revision - references to arrays

10.15

%bookHash%bookHash

References – the simple version…You can think of it as folders that contain inner folders that contains some data…$bookHash{"Eyal"}->{"Phone"} = 5012;$bookHash{"Eyal"}->{"Addrs"} = "Swiss";$bookHash{"Neta"}->{"Phone"} = 6023;$bookHash{"Neta"}->{"Addrs"} = "Yavne"; 5012"phone"

"Swiss""addrs"

"Eyal"

6023"phone""Yavne""addrs"

"Neta"

$bookHash{"Eyal"}{"Phone"} = 5012; $bookHash{"Eyal"}{"Addrs"} = "Swiss";$bookHash{"Neta"}{"Phone"} = 6023; $bookHash{"Neta"}{"Addrs"} = "Yavne";

Change Neta's address:$bookHash{"Neta"}{"Addrs"} = "Tel-Aviv";

Change Eyal's phone:$bookHash{"Eyal"}{"Phone"} = 2209;

Page 16: Complex data structures revision - references to arrays

10.16

The general structure of the data structure:# $bookHash{$name}{"Addrs"} = $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

Page 17: Complex data structures revision - references to arrays

10.17

Referencing hash :$hashRef = {%phoneBook};$bookRef = \%phoneBook; (careful)

Referencing – Dereferencing Hashes - summary

Dereferencing hash :%hash = %{$hashRef};$myVal = $hashRef->{"A"};

$bookRef %phoneBook

XA

YB

ZC

$hashRef

XA

YB

ZC

%hash

XA

YB

ZC

$myVal = $hashRef->{"A"} = "X"

Page 18: Complex data structures revision - references to arrays

10.18Class exercise 10a =

9b1. 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_000081 181 NucAP_000174 104 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.

1. 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.

Page 19: Complex data structures revision - references to arrays

10.19

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

More complex data structures

Page 20: Complex data structures revision - references to arrays

10.20

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

More complex data structures

Page 21: Complex data structures revision - references to arrays

10.21

The general structure of the data structure:# $bookHash{$name}{"Addrs"} = $address# {"Phone"} = $phone# {"grades"} = [ @grades ]

$bookHash{"Neta"}{"Phone"} = 6023; $bookHash{"Neta"}{"Addrs"} = "Yavne";@grades = (100,82);$bookHash{"Neta"}{"grades"} = [@grades];

More complex data structures

Page 22: Complex data structures revision - references to arrays

10.22

The general structure of the data structure:# $bookHash{$name}{"Addrs"} = $address# {"Phone"} = $phone# {"grades"} = [ @grades ]

$bookHash{"Era"}{"Phone"} = 2209; $bookHash{"Era"}{"Addrs"} = "Tel-Aviv";@grades = (56,99,77);$bookHash{"Era"}{"grades"} = [@grades];

More complex data structures

Page 23: Complex data structures revision - references to arrays

10.23

The general structure of the data structure:# $bookHash{$name}{"Addrs"} = $address# {"Phone"} = $phone# {"grades"} = [ @grades ]

Now let's print the phone and average of each one…

More complex data structures

Page 24: Complex data structures revision - references to arrays

10.24

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: 2209Average of Era: 77.3333Phone of Eyal: 5012Average of Eyal: 81Phone of Neta: 6023Average of Neta: 91

Page 25: Complex data structures revision - references to arrays

10.25 Class exercise 10b1. 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_000081 181 Nuc 0.02,0.41,0.34,0.05,0.04AP_000138 145 Cyt 0.27,0.43,0.20

Stores the names of the sequences as hash keys, and uses "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.

1. Ask the user for a protein name and print its length and location and levels. 2. Print for each protein its name, location and the average of its levels.

Page 26: Complex data structures revision - references to arrays

10.26Class exercise 10b

(cont.)2*. Add to the script of 10a question 2b a key to the inner hash containing the CDS

coordinates, with the following data structure:

$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 proteins coded on the negative strand print the coordinates reversed.

b) print all the product names of the proteins coded on the positive strand which start after coordinate 2000

Page 27: Complex data structures revision - references to arrays

10.27

What about even more levels of hashes?For example: Hash of names in which there are:

o phoneo address and the address has:

street name number of house city

To Infinity and Beyond!!

Page 28: Complex data structures revision - references to arrays

10.28

What about even more levels of hashes?# $book{$name}{"Phone"} = $phone# {"Addrs"}{"street"} = $street# {"number"} = $number# {"city"} = $city

To Infinity and Beyond!!

Page 29: Complex data structures revision - references to arrays

10.29

What about even more levels of hashes?

my %bookHash;$bookHash{"Eyal"}{"Phone"} = 5012; $bookHash{"Eyal"}{"Addrs"}{"street"} = "Baugenhof St.";$bookHash{"Eyal"}{"Addrs"}{"number"} = "31";$bookHash{"Eyal"}{"Addrs"}{"city"} = "Lausanne";

To Infinity and Beyond!!

Page 30: Complex data structures revision - references to arrays

10.30

What about an array of addresses?? Well… we know how to do that…# $book{$name}{"Phone"} = $phone# {"Addrs"} = [@addresses]

To Infinity and Beyond!!

Page 31: Complex data structures revision - references to arrays

10.31

What about an array of addresses?? Well… we know how to do that…# $book{$name}{"Phone"} = $phone# {"Addrs"}[$i] = $address_i

To Infinity and Beyond!!

Page 32: Complex data structures revision - references to arrays

10.32

What about an array of addresses?? Well… we know how to do that…

my %bookHash;$bookHash{"Eyal"}{"Phone"} = 5012;$bookHash{"Eyal"}{"Addrs"}[0] = "Swiss";$bookHash{"Eyal"}{"Addrs"}[1] = "Yavne";

To Infinity and Beyond!!

Page 33: Complex data structures revision - references to arrays

10.33

What about an array of addresses, each containing data of street and city ??!!??!# $book{$name}{"Phone"} = $phone# {"Addrs"}[$i]{"street"} = $street_i# {"Addrs"}[$i]{"city"} = $city_i

To Infinity and Beyond!!

Page 34: Complex data structures revision - references to arrays

10.34

What about an array of addresses, each containing data of street and city ??!!??!

my %bookHash;$bookHash{"Eyal"}{"Addrs"}[0]{"street"} = "Baugenhof 7";$bookHash{"Eyal"}{"Addrs"}[0]{"city"} = "Lausanne";$bookHash{"Eyal"}{"Addrs"}[1]{"street"} = "Hetzel 21";$bookHash{"Eyal"}{"Addrs"}[1]{"city"} = "Yavne";

To Infinity and Beyond!!

Page 35: Complex data structures revision - references to arrays

10.35

Is it possible to represent 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

2 31 5 64 8 97

@matrix

2 31

5 64

8 97