Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 2. Control structures File IO - reading and writing Subroutines

Similar presentations


Presentation on theme: "Lesson 2. Control structures File IO - reading and writing Subroutines"— Presentation transcript:

1 Lesson 2. Control structures File IO - reading and writing Subroutines
Modules BioPerl

2 Comparisons and if statements
The if statement: if (<condition>) { # do something } elsif (<another_condition>) { # do something else else { # do yet another thing a code block

3 comparators The result of a comparison is either true or false
Boolean algebra Numeric values Compare numbers Is $a equal to $b? Larger? Smaller? ==, >, <, <=, >=, !=, <=> if ($a > $b) { print “$a is larger than $b\n”; } String values eq, ne, gt (greater than), lt (less than)

4 Boolean algebra AND OR NOT

5 Boolean values in Perl Perl has no boolean type
Values that are “false” 0 (incl numerically assigned expressions that evaluate to zero) “” (the empty string) (NB. “ “ (a string containing a space) is true) undef (value of a variable that was declared but nothing assigned) The empty list The empty hash True values Everything else that is not true Convention: Assigning false: use 0. Assigning true: use 1.

6 Operator precedence * , / precedes +, - and, not precedes or
5 + (6 * 3) (5 + 6) * 3 and, not precedes or ( $x < 2 or $x > 5 and $y < 100) ) vs. ( ( $x < 2 or $x > 5 ) and $y < 100) See man perlop See also de Morgan's law: (! $x) and (! $y) = ! ( $x or $y ) (! $x) or (! $y) = ! ( $x and $y)

7 Boolean values in Perl Perl has no boolean type
Values that are “false” 0 (incl numerically assigned expressions that evaluate to zero) “” (the empty string) (NB. “ “ (a string containing a space) is true) undef (value of a variable that was declared but nothing assigned) The empty list The empty hash True values Everything else that is not true Convention: Assigning false: use 0. Assigning true: use 1.

8 Operator precedence * , / precedes +, - and, not precedes or
5 + (6 * 3) (5 + 6) * 3 and, not precedes or ( $x < 2 or $x > 5 and $y < 100) ) vs. ( ( $x < 2 or $x > 5 ) and $y < 100) See man perlop See also de Morgan's law: (! $x) and (! $y) = ! ( $x or $y ) (! $x) or (! $y) = ! ( $x and $y)

9 File I/O - Reading Opening a file
open(my $F, “<”, $filename); # “<”=reading # “>”=writing Reading from a file while (<$F>) { # reads into default var $_ chomp(); # removes newline my ($firstname, $lastname) = split /\t/; } Closing a file close($F);

10 File I/O - Writing Opening a file
open(my $F, “>”, $filename); # “<”=reading # “>”=writing Writing to a file print $F “Hello World!\n”; Closing a file close($F);

11 File checks File check flags Check of the form if (-f $myfile) {
-d directory -f plain file -l symbolic link -T text file Check of the form if (-f $myfile) { open ($F, “<”, $myfile) # etc.... See also

12 The open or die construct
open (my $F, “<”, $myfile) or die “Can't open file $myfile”; The open() call returns true on success, false otherwise. The or operator evaluates the second operand only if the first operand is false (if the first operand is true, or will always be true!)

13 Reading directory listings
The glob function = glob “*.txt”; foreach my $f { print “$f\n” }

14 Subroutines Code that is duplicated in a script may be better in a subroutine Subroutines are called like other Perl functions Declaration and basic structure sub foo { # do something... then... return $result; } Function call my $result = foo($param1, $param2);

15 Subroutine parameters
array is an alias to the original parameters The original parameters will be changed is changed! is assigned to more descriptive variables Once assigned, the variables are not aliases anymore Changing their values does not change the originals Examples sub foo { my $chromosome = my $marker = shift; #operates by default my ($surface, $diameter)

16 CAVEAT! Subroutine parameters – multiple lists
Function parameters such as: @y) Don't work, because in the subroutine: sub foo { my @y) All elements will be assigned Use listrefs (or hashrefs) in this case my ($xref, $yref)

17 Subroutine return values
Subroutines that return values are sometimes called functions It is better for a function to return a value than to change the function parameters Return values can be scalars, lists, or hashes. Be careful with returning multiple lists or hashes. Need to return listrefs or hashrefs!

18 Modules Create re-usable code that scripts can load and use
A module is a namespace Subroutines that are used from different scripts are better placed into modules Easier to maintain Documention Bug fixes

19 Modules Declaration package Foo; Notes about naming:
Package names should be uppercase Often, package names are in CamelCase Mapping to filesystem: package Foo should be a file called Foo.pm package Foo::Bar is in Foo/Bar.pm Using modules use Foo::Bar;

20 @INC is like $PATH for modules
How does Perl know where to look for modules? Perl looks in the current directory Perl looks in the order of directories given @INC is populated by Perl at startup List of paths given in the environment variable $PERL5LIB is pre-pended

21 Accessing other packages
You are ALWAYS in a package in Perl The default package is called “main”. The “main” package identifier can be omitted. You can access all global variables declared in main by our $example=100; print $main::example.”\n”; You can access globals in other packages by print $MyOtherPackage::example; print $Foo::Bar::example;

22 Some standard modules Math File Getopt::Std Test Math::Complex
Math::BigInt File File::Temp File::Basename File::Spec Getopt::Std Test

23 Bio::SeqIO Sequence input/output Usage use Bio::SeqIO;
my $in = Bio::SeqIO->new(-format=>'fastq', - file=>$file); my $out = Bio::SeqIO->new(-format=>'fasta', - file=>”>$file.fasta”); while (my $s = $in->next_seq()) { $out->write_seq($s); }

24 Changing object properties
use Bio::SeqIO; my $in = Bio::SeqIO->new(-format=>'fasta', - file=>$file); my $out = Bio::SeqIO->new(-format=>'fasta', - file=>”>$file.fasta”); while (my $s = $in->next_seq()) { my $id = $s->id(); my $new_id = $new_ids{$id}; $s->id($new_id); $out->write_seq($s); }


Download ppt "Lesson 2. Control structures File IO - reading and writing Subroutines"

Similar presentations


Ads by Google