Presentation is loading. Please wait.

Presentation is loading. Please wait.

BINF 634 Fall 2015 - LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.

Similar presentations


Presentation on theme: "BINF 634 Fall 2015 - LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline."— Presentation transcript:

1 BINF 634 Fall 2015 - LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline

2 BINF 634 Fall 2015 - 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

3 BINF 634 Fall 2015 - 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

4 Data Structures and Algorithm Efficiency # An inefficient way to compute intersections my @a = qw/ A B C D E F G H I J K X Y Z /; my @b = qw/ Q R S A C D T U G H V I J K X Z /; my @intersection = (); for my $i (@a) { for my $j (@b) { if ($i eq $j) { push @intersection, $i; last; } print "@intersection\n"; 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 2015 - LECTURE06

5 Algorithm is O(N) N = size of Lists Data Structures and Algorithm Efficiency # A better way to compute intersections my @a = qw/ A B C D E F G H I J K X Y Z /; my @b = qw/ Q R S A C D T U G H V I J K X Z /; my @intersection = (); # "mark" each item in @a my %mark = (); for my $i (@a) { $mark{$i} = 1 } # intersection = any "marked" item in @b for my $j (@b) { if (exists $mark{$j}) { push @intersection, $j; } print "@intersection\n"; exit; Output: A C D G H I J K X Z version 2 version 1 Algorithm Efficiency 5BINF 634 Fall 2015 - LECTURE06

6 6 Demonstration Unix commands: /usr/bin/time head diff cmp % wc -l list1 list2 24762 list1 12381 list2 37143 total % /usr/bin/time intersect1.pl list1 list2 > out1 22.91 real 22.88 user 0.02 sys % /usr/bin/time intersect2.pl list1 list2 > out2 0.06 real 0.05 user 0.00 sys 22.88/.05 = 458 Algorithm Efficiency

7 BINF 634 Fall 2015 - 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

8 BINF 634 Fall 2015 - LECTURE068 Examples 1. Write a subroutine get_intersection(\@a, \@b) that returns the intersection of two lists. 2. Write a subroutine first_list_only(\@a, \@b) that returns the items that are in list @a but not in @b. 3. Write a subroutine unique(@a) that return the unique items in list @a (that is, remove the duplicates). 4. Write a subroutine dups($n, @a) that returns a list of items that appear in @a at least $n times. Hashes

9 BINF 634 Fall 2015 - 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

10 BINF 634 Fall 2015 - LECTURE0610 Sorting Our First Attempt #!/usr/bin/perl use strict; use warnings; { my(@unsorted) = (17, 8, 2, 111); my(@sorted) = sort @unsorted; print "@unsorted \n"; print "@sorted \n"; exit; } Output: 17 8 2 111 111 17 2 8 Sorting

11 BINF 634 Fall 2015 - 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

12 BINF 634 Fall 2015 - LECTURE0612 Sorting Numerically #!/usr/bin/perl use strict; use warnings; { my(@unsorted) = (17, 8, 2, 111); my(@sorted) = sort { $a $b }@unsorted; print "@unsorted \n"; print "@sorted \n"; exit; } Output: 17 8 2 111 2 8 17 111 Sorting

13 BINF 634 Fall 2015 - LECTURE0613 Sorting Using a Subroutine #!/usr/bin/perl use strict; use warnings; { my(@unsorted) = (17, 8, 2, 111); my(@sorted) = sort numerically @unsorted; print "@unsorted \n"; print "@sorted \n"; exit; } sub numerically { $a $b } Output: 17 8 2 111 2 8 17 111 Sorting

14 BINF 634 Fall 2015 - LECTURE0614 Sorting Descending #!/usr/bin/perl use strict; use warnings; { my(@unsorted) = (17, 8, 2, 111); my(@reversesorted) = reverse sort numerically @unsorted; print "@unsorted \n"; print "@reversesorted \n"; exit; } sub numerically { $a $b } Output: 17 8 2 111 111 17 8 2 Sorting

15 BINF 634 Fall 2015 - LECTURE0615 Sorting DNA by Length !/usr/bin/perl use strict; use warnings; { # Sorting strings: my @dna = qw/ TATAATG TTTT GT CTCAT /; ## Sort @dna by length: @dna = sort { length($a) length($b) }@dna; print "@dna\n"; # Output: GT TTTT CTCAT TATAATG exit; } Output: GT TTTT CTCAT TATAATG Sorting

16 BINF 634 Fall 2015 - LECTURE0616 Sorting DNA by Number of T’s (Largest First) #!/usr/bin/perl use strict; use warnings; { # Sorting strings: my @dna = qw/ TATAATG TTTT GT CTCAT /; @dna = sort { ($b =~ tr/Tt//) ($a =~ tr/Tt//) } @dna; print "@dna\n"; # Output: TTTT TATAATG CTCAT GT exit; } Output: TTTT TATAATG CTCAT GT Sorting

17 BINF 634 Fall 2015 - LECTURE0617 Sorting DNA by Number of T’s (Largest First) (Take 2) #!/usr/bin/perl use strict; use warnings; { # Sorting strings: my @dna = qw/ TATAATG TTTT GT CTCAT /; @dna = reverse sort { ($a =~ tr/Tt//) ($b =~ tr/Tt//) } @dna; print "@dna\n"; # Output: TTTT TATAATG CTCAT GT exit; } Output: TTTT TATAATG CTCAT GT Sorting

18 BINF 634 Fall 2015 - LECTURE0618 Sorting Strings Without Regard to Case #!/usr/bin/perl use strict; use warnings; { # Sort strings without regard to case: my(@unsorted) = qw/ mouse Rat HUMAN eColi /; my(@sorted) = sort { lc($a) cmp lc($b) } @unsorted; print "@unsorted \n"; print "@sorted \n"; exit; } Output: mouse Rat HUMAN eColi eColi HUMAN mouse Rat Sorting

19 BINF 634 Fall 2015 - 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

20 BINF 634 Fall 2015 - 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

21 BINF 634 Fall 2015 - 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

22 BINF 634 Fall 2015 - 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

23 BINF 634 Fall 2015 - 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

24 BINF 634 Fall 2015 - LECTURE0624 Some Example Questions What do these four lines do? @bases = (‘A’, ‘C’, ‘G’, ‘T’); $base1 = pop @bases; unshift (@bases, $base1); print “@bases\n\n”; T A C G Midterm

25 BINF 634 Fall 2015 - LECTURE0625 Some Example Questions What does this code snippet do if COND is true unless(COND){ #do something } Midterm nothing

26 BINF 634 Fall 2015 - LECTURE0626 Some Example Questions What does this code fragment do? $protein = join(‘’,@protein) Converts the array @protein into a scalar $protein with no space between The entries Midterm

27 BINF 634 Fall 2015 - 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

28 BINF 634 Fall 2015 - 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

29 BINF 634 Fall 2015 - 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

30 BINF 634 Fall 2015 - LECTURE0630 Some Example Questions What is contained in the reserved variable $0 and in the array @ARGV ? Midterm $0 contains the name of the program; @ARGV contains the command line arguments for the program

31 BINF 634 Fall 2015 - 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.

32 BINF 634 Fall 2015 - 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.

33 BINF 634 Fall 2015 - LECTURE0633 Some Example Questions How do you invoke perl with the debugger? Midterm perl - d

34 BINF 634 Fall 2015 - LECTURE0634 Some Example Questions Given an array @verbs what is going on here? $verbs[rand @verbs] Midterm rand wants an integer so it uses scalar @verbs rand then generates a random number between 0 and length of the array @verbs. This is then converted to an integer to index into @verbs

35 For the Curious Regarding Data Structures and Their Implications Niklaus Wirth, Algorithms + Data Structures = Programs, Prentice Hall 1976. Dated in terms of language, Pascal, but very well written and understandable BINF 634 Fall 2015 - LECTURE0635


Download ppt "BINF 634 Fall 2015 - LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline."

Similar presentations


Ads by Google