BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline
BINF 634 Fall LECTURE062 Be Careful With Scope #!/usr/bin/perl use strict; use warnings; my $x = 23; print "value in main body is $x \n"; mysub($x); print "value in main body is $x \n"; exit; sub mysub{ print "value in subroutine is $x \n"; $x=33; } value in main body is 23 value in subroutine is 23 value in main body is 33 #!/usr/bin/perl use strict; use warnings; { my $x = 23; print "value in main body is $x \n"; mysub($x); print "value in main body is $x \n"; exit; } sub mysub{ print "value in subroutine is $x \n"; $x=33; } This will not compile Scoping
BINF 634 Fall LECTURE063 Be Careful With Scope (cont.) #!/usr/bin/perl use strict; use warnings; { my $x = 23; print "value in main body is $x \n"; mysub($x); print "value in main body is $x \n"; exit; } sub mysub{ my($x) $x=33; print "value in subroutine is $x \n"; } value in main body is 23 value in subroutine is 33 value in main body is 23 Scoping
Data Structures and Algorithm Efficiency # An inefficient way to compute intersections = qw/ A B C D E F G H I J K X Y Z /; = qw/ Q R S A C D T U G H V I J K X Z /; = (); for my $i { for my $j { if ($i eq $j) { $i; last; } print exit; Output: A C D G H I J K X Z Algorithm is O(N 2 ) N = size of Lists Algorithm Efficiency 4BINF 634 Fall LECTURE06
Algorithm is O(N) N = size of Lists Data Structures and Algorithm Efficiency # A better way to compute intersections = qw/ A B C D E F G H I J K X Y Z /; = qw/ Q R S A C D T U G H V I J K X Z /; = (); # "mark" each item my %mark = (); for my $i { $mark{$i} = 1 } # intersection = any "marked" item for my $j { if (exists $mark{$j}) { $j; } print exit; Output: A C D G H I J K X Z version 2 version 1 Algorithm Efficiency 5BINF 634 Fall LECTURE06
6 Demonstration Unix commands: /usr/bin/time head diff cmp % wc -l list1 list list list total % /usr/bin/time intersect1.pl list1 list2 > out real user 0.02 sys % /usr/bin/time intersect2.pl list1 list2 > out real 0.05 user 0.00 sys 22.88/.05 = 458 Algorithm Efficiency
BINF 634 Fall LECTURE067 Hashes and Efficiency Hashes provide a very fast way to look up information associated with a set of scalar values (keys) Examples: Count how many time each word appears in a file Also: whether or not a certain work appeared in a file Count how many time each codon appears in a DNA sequence Whether a given codon appears in a sequence How many time an item appears in a given list Intersections Hashes
BINF 634 Fall LECTURE068 Examples 1. Write a subroutine that returns the intersection of two lists. 2. Write a subroutine that returns the items that are in but not 3. Write a subroutine that return the unique items in (that is, remove the duplicates). 4. Write a subroutine that returns a list of items that appear at least $n times. Hashes
BINF 634 Fall LECTURE069 Sorting sort LIST -- returns list sorted in string order sort BLOCK LIST -- compares according to BLOCK sort USERSUB LIST -- compares according subroutine SUB Sorting
BINF 634 Fall LECTURE0610 Sorting Our First Attempt #!/usr/bin/perl use strict; use warnings; { = (17, 8, 2, 111); = print \n"; print \n"; exit; } Output: Sorting
BINF 634 Fall LECTURE0611 The Comparison Operator 1. $a $b returns 0 if equal, 1 if $a > $b, -1 if $a < $b 2. The "cmp" operator gives similar results for strings 3. $a and $b are special global variables: do NOT declare with "my" and do NOT modify. Sorting
BINF 634 Fall LECTURE0612 Sorting Numerically #!/usr/bin/perl use strict; use warnings; { = (17, 8, 2, 111); = sort { $a $b print \n"; print \n"; exit; } Output: Sorting
BINF 634 Fall LECTURE0613 Sorting Using a Subroutine #!/usr/bin/perl use strict; use warnings; { = (17, 8, 2, 111); = sort print \n"; print \n"; exit; } sub numerically { $a $b } Output: Sorting
BINF 634 Fall LECTURE0614 Sorting Descending #!/usr/bin/perl use strict; use warnings; { = (17, 8, 2, 111); = reverse sort print \n"; print \n"; exit; } sub numerically { $a $b } Output: Sorting
BINF 634 Fall LECTURE0615 Sorting DNA by Length !/usr/bin/perl use strict; use warnings; { # Sorting strings: = qw/ TATAATG TTTT GT CTCAT /; ## by = sort { length($a) length($b) print # Output: GT TTTT CTCAT TATAATG exit; } Output: GT TTTT CTCAT TATAATG Sorting
BINF 634 Fall LECTURE0616 Sorting DNA by Number of T’s (Largest First) #!/usr/bin/perl use strict; use warnings; { # Sorting strings: = qw/ TATAATG TTTT GT CTCAT = sort { ($b =~ tr/Tt//) ($a =~ tr/Tt//) print # Output: TTTT TATAATG CTCAT GT exit; } Output: TTTT TATAATG CTCAT GT Sorting
BINF 634 Fall LECTURE0617 Sorting DNA by Number of T’s (Largest First) (Take 2) #!/usr/bin/perl use strict; use warnings; { # Sorting strings: = qw/ TATAATG TTTT GT CTCAT = reverse sort { ($a =~ tr/Tt//) ($b =~ tr/Tt//) print # Output: TTTT TATAATG CTCAT GT exit; } Output: TTTT TATAATG CTCAT GT Sorting
BINF 634 Fall LECTURE0618 Sorting Strings Without Regard to Case #!/usr/bin/perl use strict; use warnings; { # Sort strings without regard to case: = qw/ mouse Rat HUMAN eColi /; = sort { lc($a) cmp lc($b) print \n"; print \n"; exit; } Output: mouse Rat HUMAN eColi eColi HUMAN mouse Rat Sorting
BINF 634 Fall LECTURE0619 Sorting Hashes by Value #!/usr/bin/perl use strict; use warnings; { my(%sales_amount) = ( auto=>100, kitchen=>2000, hardware=>200 ); sub bysales { $sales_amount{$b} $sales_amount{$a} } for my $dept (sort bysales keys %sales_amount) { printf "%s:\t%4d\n", $dept, $sales_amount{$dept}; } exit; } Output: kitchen:2000 hardware: 200 auto: 100 Sorting
BINF 634 Fall LECTURE0620 Review for Midterm BINF634 Material Tisdall Chapters 1-9 Wall Chapter 5 Lecture notes The exam will be open book and notes You cannot work together on it You cannot use outside material You will have the full period to take the midterm You will be asked to program Midterm
BINF 634 Fall LECTURE0621 Some Example Questions Given two DNA fragments contained in $DNA1 and $DNA2 how can we concatenate these to make a third string $DNA3? $DNA3 = “$DNA1$DNA2”; $DNA3 = $DNA1. $DNA2; Midterm
BINF 634 Fall LECTURE0622 Some Example Questions What does this line of code do? $RNA = ~ s/T/U/ig Substitute T’s with U’s in a case insensitive manner globally within the string $RNA Midterm
BINF 634 Fall LECTURE0623 Some Example Questions What does this statement do? $revcom =~ tr/ACGT/TGCA/; It performs the mapping A T C G G C T A all at once Midterm
BINF 634 Fall LECTURE0624 Some Example Questions What do these four lines = (‘A’, ‘C’, ‘G’, ‘T’); $base1 = unshift $base1); print T A C G Midterm
BINF 634 Fall LECTURE0625 Some Example Questions What does this code snippet do if COND is true unless(COND){ #do something } Midterm nothing
BINF 634 Fall LECTURE0626 Some Example Questions What does this code fragment do? $protein = Converts the into a scalar $protein with no space between The entries Midterm
BINF 634 Fall LECTURE0627 Some Example Questions What does this code fragment do? $myfile = “myfile”; Open(MYFILE, “>$myfile”) Opens the file $myfile with the file handle MYFILE for writing Midterm
BINF 634 Fall LECTURE0628 Some Example Questions What does this code fragment do? while($DNA =~ /a/ig){$a++} Counts the occurrences of the letter a or A within the string $DNA Midterm
BINF 634 Fall LECTURE0629 Some Example Questions What is the effect of using the command use strict; at the beginning of your program? Midterm It insists that your programs have all their variables declared as my variables
BINF 634 Fall LECTURE0630 Some Example Questions What is contained in the reserved variable $0 and in the ? Midterm $0 contains the name of the contains the command line arguments for the program
BINF 634 Fall LECTURE0631 Some Example Questions What is the difference between “pass by value” and “pass by reference” ? Midterm In “pass by value” you provide a subroutine with a “copy” of your variable. In “pass by reference” you provide a subroutine with a pointer to your variable. In this manner the subroutine can change the contents of the variable.
BINF 634 Fall LECTURE0632 Some Example Questions What is a pointer and what does it mean to dereference a pointer? Midterm A pointer is an address in memory to a particular variable. Dereferecing a pointer is the act of obtaining the information that is stored at a particular pointer location.
BINF 634 Fall LECTURE0633 Some Example Questions How do you invoke perl with the debugger? Midterm perl - d
BINF 634 Fall LECTURE0634 Some Example Questions Given an what is going on here? Midterm rand wants an integer so it uses rand then generates a random number between 0 and length of the This is then converted to an integer to index
For the Curious Regarding Data Structures and Their Implications Niklaus Wirth, Algorithms + Data Structures = Programs, Prentice Hall Dated in terms of language, Pascal, but very well written and understandable BINF 634 Fall LECTURE0635