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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

The Assembly Language Level
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Computer Science II Exam I Review Monday, February 6, 2006.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Physical Mapping II + Perl CIS 667 March 2, 2004.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA.
subroutines and references
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics 生物信息学理论和实践 唐继军
Introduction to Perl Yupu Liang cbio at MSKCC
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
PERL SCRIPTING. COMPUTER BASICS CPU, RAM, Hard drive CPU can only use data in the register directly CPU RAM HARD DRIVE.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
Containers and Lists CIS 40 – Introduction to Programming in Python
User-Defined Functions
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 7: User-Defined Functions II
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Presentation transcript:

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