10.1 references & complex data structures. 10.2 variable types in perl scalararrayhash $number...

31
10.1 References & Complex Data Structures

Post on 19-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

10.1 References &Complex Data Structures

10.2 Variable types in PERLScalar Array Hash

$number-3.54

$string"hi\n"

@array %hash

$reference0x225d14

@array1

%hash

@array2

@array3

10.3

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

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"

91 6785

@grades

82100

@grades

References example

91 6785

82100

99 7756

@grades

99 7756

%gradesHash

"Eyal"

"Neta"

%gradesHash

"Eyal"

"Neta"

"Era"

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"

91 6785

@grades

82100

@grades

References (bad) example

99 7756

@grades

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"

91 6785

82100

99 775672

To get the array use @{$reference}To get the array use @{$reference}

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

referenced array

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

referenced array

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"

91 6785

82100

99 775672

97

10.8

Referencing hash :

$hashRef = {%phoneBook};

$bookRef = \%phoneBook; (careful)

Referencing – Dereferencing HashesDereferencing hash :

%hash = %{$hashRef};

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

$bookRef %phoneBook

XA

YB

ZC

$hashRef

XA

YB

ZC

%hash

XA

YB

ZC

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

10.9

%bookHash%bookHash

my %details;

$details{"phone"} = 5012;

$details{"address"} = "Swiss";

my %bookHash;

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

$details{"phone"} = 6023;

$details{"address"} = "Yavne";

$bookHash{"Neta"} = {%details};

References example5012"phone"

"Swiss""addrs"

%details

5012"phone"

"Swiss""addrs"

"Eyal" 6023"phone"

"Yavne""addrs"

6023

"Yavne"

"Neta"

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%bookHash

5012"phone"

"Swiss""addrs"

"Eyal"

6023"phone"

"Yavne""addrs"

"Neta"

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

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

To get the hash use %{$reference}To get the hash use %{$reference}

10.11

%bookHash%bookHash

ReferencesYou 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;

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

10.13Class 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_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.2. 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.

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

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

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

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

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

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

10.20Class 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_000081 181 Nuc 0.02,0.41,0.34,0.05,0.04

AP_000138 145 Cyt 0.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.

1. Ask the user for a protein name and print its length and location and sorted levels.

2. Print for each protein its name, location and the average of its levels.

10.21Class exercise 10b

2. Add to the script of 10a question 2b a key to the inner hash containing the CDScoordinates, 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

10.22

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!!

10.23

What about even more levels of hashes?

# $book{$name} {"phone"} = $phone

# {"address"}{"street"} = $street

# {"number"} = $number

# {"city"} = $city

To Infinity and Beyond!!

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!!

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!!

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!!

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!!

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!!

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!!

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!!

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

2 31 5 64 8 97

@matrix

2 31

5 64

8 97