BNFO 135 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

BNFO 135

Description:

... that they are indexed by user defined keys instead of non-negative integers. ... Write a program to prompt the user for a Yes or No response. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 24
Provided by: usmanr
Category:

less

Transcript and Presenter's Notes

Title: BNFO 135


1
Lecture 2
  • BNFO 135
  • Usman Roshan

2
Perl variables
  • Scalar
  • Number
  • String
  • Examples
  • myname Roshan
  • year 2006

3
Number operators
4
Strings
  • Perl supports very powerful operations on
    strings. Basic operators below.

5
Arrays
  • Array variables begin with _at_.
  • Example
  • _at_myarray(1,2,3,4,5)
  • _at_s(dna, rna, protein)
  • print _at_s outputs dnarnaprotein

6
Array operations
7
Hashes
  • Hashes are like arrays, except that they are
    indexed by user defined keys instead of
    non-negative integers.
  • Hashes begin with
  • Examples
  • h(red, 1, blue, 2, green, 3)
  • h(first, usman, last, name)

8
Perl online references
  • http//www.rexswain.com/perl5.html
  • http//www.perl.com/pub/q/documentation
  • http//www-cgi.cs.cmu.edu/cgi-bin/perl-man
  • http//www.squirrel.nl/pub/perlref-5.004.1.pdf
  • http//perldoc.perl.org/index-language.html

9
Input and Output
  • printf C-like formatted output, very powerful
  • print unformatted output
  • Example
  • printf My name is s\n, name
  • printf Today is s d d\n, month, day, year

10
File I/O
  • open(IN, in.txt)
  • lineltINgt
  • print line
  • close IN
  • open(OUT, gtout.txt)
  • print OUT line1\n
  • close OUT

11
File I/O
  • open(OUT, gtgtout.txt)
  • print OUT line2\n
  • close OUT
  • open(IN, in.txt)
  • _at_linesltINgt reads all lines into array
  • print _at_lines
  • close IN
  • inputltSTDINgt read from standard input

12
Control structures---if else block
  • if (c)
  • s1 s2 ...
  • if (c)
  • s1 s2...
  • else
  • s1 s2...
  • if (c1)
  • s1 s2 ...
  • elsif (c2)
  • ...
  • elsif (c3)
  • ....
  • else
  • ...

13
Control structures---if else block
  • This is program to compute max of two
  • Numbers.
  • max0
  • if(x gt y) max x
  • else max y

14
Control structures---for loop
  • for(init_exp test_exp iterate_exp)
  • s1 s2
  • foreach i (_at_some_list) s1 s2 ...
  • Examples
  • for (i0 i lt _at_arr i)
  • print arri
  • foreach e (_at_arr)
  • print e

15
Control structures---while loop
  • while (condition) s1 s2...
  • do s1 s2 ... while condition
  • while (condition) s1if(c)last s2...
  • last means exit the loop
  • while (condition) s1if(c)next s2...
  • next means go to next iteration

16
  • Write a program to prompt the user for a Yes or
    No response. Read in the user response using the
    STDIN file handle and print OK is the user
    enters Yes, I hear you if the user enters
    No, and Make up your mind! if the user enters
    something other than Yes or No.
  • Create a script called foods.pl that asks the
    user for their favorite foods. Ask the user to
    enter at least 5 foods, each separated by a space
    (or some other delimiter). Store their answer in
    a scalar. Split the scalar into an array. Once
    the array is created, have the script do the
    following
  • Print the array
  • Print the number of elements in the array
  • Create an array slice from three elements
    of the array and print the new array
  • Write a program to open a file containing SSNs
    and read in the first five records into an array.

17
  • Write a program called count.pl that reads in a
    set of strings from a file called string.txt and
    prints the number of As, Bs, Cs, and Ds. For
    example, if the file looks like
  • AABCAA
  • CDAAD
  • CCC
  • then your program should output
  • A 6
  • B 1
  • C 5
  • D 2

18
Subroutines
  • sub ltsubroutine_namegt
  • parameters are placed in _at__
  • ltcodegt
  • .
  • .
  • return

19
Scope
  • You can create local variables using my.
  • Otherwise all variables are assumed global, i.e.
    they can be accessed and modified by any part of
    the code.
  • Local variables are only accessible in the scope
    of the code.

20
Pass by value and pass by reference
  • All variables are passed by value to subroutines.
    This means the original variable lying outside
    the subroutine scope does not get changed.
  • You can pass arrays by reference using \. This is
    the same as passing the memory location of the
    array.

21
Splitting and joining strings
  • split splits a string by regular expression and
    returns array
  • _at_s split(/,/)
  • _at_s split(/\s/)
  • join joins elements of array and returns a
    string (opposite of split)
  • seqjoin(, _at_pieces)
  • seqjoin(X, _at_pieces)

22
Searching and substitution
  • x /y/ ---- true if expression y found in
    x
  • x /ATG/ --- true if open reading frame ATG
    found in x
  • x ! /GC/ --- true if GC not found in x
  • x s/T/U/g --- replace all Ts with Us
  • x s/g/G/g --- convert all lower case
    g to upper case G

23
DNA regular expressions
Taken from Jagotas Perl for Bioinformatics
Write a Comment
User Comments (0)
About PowerShow.com