Using Modules - University of Iowa

Download Report

Transcript Using Modules - University of Iowa

Using Modules
1
Module Example
Next I will describe a very simple
implementation of a perl module.
This will be the minimum amount of
information needed to use modules as
intended.
This is useful for when we attempt to use
modules (such as File::Basename), we'll
have a better understanding of what they
are.
2
Exporting
• require Exporter; # a package written to do symbol (variable)
exporting
• @ISA = qw(Exporter);
# "is a" is simply an array that specifies to perl the classes/packages to
search for methods/subroutines
# mechanism for inheritance that we'll come back to
# sufficient to know that if you omit this, perl modules will not work as
described here
# Note that on quoted words – you may define your own boundary chars
• @EXPORT = qw(&say_hello $text);
# lists the symbols that are exported
• reference:
perldoc Exporter
3
Example (use)
# Silly.pm
package Silly; #package statement
#!/usr/bin/perl
# main.pl
#
#require "Silly.pm";
# Note no quotes, or .pm
use Silly;
require Exporter; #we now know what this means
@ISA = qw(Exporter); #technically we know what is
going on
@EXPORT = qw(&say_hello $text);
my $name = "Terry";
$text=""; # Note, I did not use "my"
# "my" would make it local to this package
# so even though it is "exported", it would
# not be available to the "main"
#Silly::say_hello($name);
sub say_hello {
# We don't know what this SR does yet
&say_hello($name);
$text = $_[0];
# Why don't I do $text = shift ????
# Don't know what the value of this is either print "Hello $text\n";
}
print "The _text_ in Silly = $text\n";
1;
#Hello Terry
#The text in Silly = Terry
4
Example (require)
package Silly; #package statement
#!/usr/bin/perl
# main.pl
#
require "Silly.pm";
#use Silly;
my $name = "Terry";
Silly::say_hello($name);
#&say_hello($name);
print "The _text_ in Silly = $Silly::text\n";
require Exporter; #we now know what this means
#QUESTION: why no ".pm" here,
# that is used in main.pl????
# Answer: could have done require Silly
# (no quotes)
@ISA = qw(Exporter); #technically we know what is
going on
@EXPORT = qw(&say_hello $text);
$text=""; # Note, I did not use "my"
# "my" would make it local to this package
# so even though it is "exported", it would
# not be available to the "main"
sub say_hello {
$text = $_[0];
# Why don't I do $text = shift ????
print "Hello $text\n";
}
1;
5
POD
perldoc perlpod
•
•
NAME
perlpod − the Plain Old Documentation format
•
•
DESCRIPTION
Pod is a simple‐to‐use markup language used for writing documentation for
Perl, Perl programs, and Perl modules.
•
•
Allows you to intersperse codes and comments (if you like).
You can also put all of your documentation at the end of6 your code following
a :__END__
POD
All command paragraphs (which are typically only one line long) start
with "=", followed by an identifier, followed by arbitrary text that
the command can use however it pleases. Currently recognized commands
are
=head1 Heading Text
=head2 Heading Text
=head3 Heading Text
=head4 Heading Text
=over indentlevel
=item stuff
=back
=cut
=pod
=begin format
=end format
=for format text...
7
Process
• Markup your module
• podchecker (can check syntax)
• pod2text (to see your documentation)
• perldoc NameOfYourModule.pm
• perldoc Silly-doc.pm
8
Annoyance
• The formatting markup requires blank
spaces before/after each line (rather
annoying).
9
Example
=head1 NAME
Silly -- example of a perl module
=head1 SYNOPSIS
This is
all text that
is displayed
by perldoc
use Silly;
say_hello("any text");
print "The _text_ in Silly = $text\n";
=head1 DESRIPTION
These routines have no useful function whatsoever.
=cut
Marks end of formatting, or
the start of real code
10
package Silly; #package statement
require Exporter; #we now know what this means
@ISA = qw/Exporter/; #technically we know what is going on
@EXPORT = qw(&say_hello $text);
$text=""; # Note, I did not use "my"
# "my" would make it local to this package
# so even though it is "exported", it would
# not be available to the "main"
=over 4
=item say_hello($string)
This function takes a string and simply prints it to the screen
proceeded by the text 'Hello '. The string is also stored in
local variable $text;
=cut
11
sub say_hello {
$text = $_[0];
# Why don't I do $text = shift ????
print "Hello $text\n";
}
=back
=cut
1;
End of itemized list
12
Very Basic Intro to BLAST
• Basic Local Alignment Search Tool
• Application to search for 1 sequence against a
database of sequences
• Note, that the database of sequences may be a
single sequence itself, so that BLAST may be
used to compare 2 sequences
• Nucleotide to nucleotide
• Amino acid to amino acid (protein)
• Nucelotide to aa
• aa to nt
• etc.
13
Example
http://www.ncbi.nlm.nih.gov/BLAST/
14
Pairwise Sequence Alignment Example
• Example:
S1: TTACTTGCC (9 bases)
S2: ATGACGAC (8 bases)
• Scoring (1 possibility):
+2 match
0 mismatch
-1 gap in either sequence
• One Possible alignment:
T T - A C T T G C C
A T G A C - - G A C
0 2-1 2 2-1-1 2 0 2
Score = 10 – 3 = 7
15
Cue to a Data Structure
Gap in S2
Gap in S1
Alignment
(match/mismatch)
16
How hard can this be?
• Brute force approach: consider all possible
alignments, and choose the one with best
score
• 3 choices at each internal branch point
• Assume n x n comparison. 3n comparisons
– n = 3  33 = 27 paths
– n = 20  320 = 3.4 x 109 paths
– n = 200  3200 = 2.6 x 1095 paths
• If 1 path takes 1 nanosecond (10-9 secs)
– 8.4 x 1078 years!
• But, using data structures cleverly, this can
be greatly sped up to O(n2)
17