SlideShare a Scribd company logo
Marc Logghe
Perl“A script is what you give an actor, but a program is what you give an audience.”
GoalsPerl Positioning System: find your way in the Perl WorldWrite Once, Use Many TimesObject Oriented PerlConsumerDeveloperThou shalt not be afraid of the Bioperl Beast
Marcs (bio)perl course
Agenda Day1Perl refresherScalarsArrays and listsHashesSubroutines and functionsPerldocCreating and running a Perl scriptReferences and advanced data structuresPackages and modulesObjects, (multiple) inheritance, polymorphism
Agenda Day 2What is bioperl ?Taming the Bioperl BeastFinding modulesFinding methodsData::DumperSequence processingOne image says more than 1000 words
VariablesData of any type may be stored within three basic types of variables:Scalar (strings, numbers, references)Array (aka list but not quite the same)Hash (aka associative array)Variable names are always preceded by a “dereferencing symbol” or prefix. If needed: {}$ - Scalar variables@ - List variables% - Associative array aka hash variables
VariablesYou do NOT have to Declare the variable before using itDefine the variable’s data typeAllocate memory for new data values
Scalar variablesScalar variable stores a string, a number, a character, a reference, undef$name, ${name}, ${‘name’}More magic: $_
Array variablesArray variable stores a list of scalars@name, @{name}, @{‘name’}IndexMap: index => scalar valuezero-indexed (distance from start)
Array variablesList assignment:Individual assignment: $count[2] = 42Individual acces: print $count[2]Special variable $#<array name>Scalar context@count = (1, 2, 3, 4, 5);@count = (‘apple’, ‘bat’, ‘cat’);@count2 = @count;
Array variables	Access multiple values via array slice:Assign multiple values via array slice:print @array[3,2,4,1,0,-1];@array[3,2,4,1,0,-1] = @new_values;
ListsList = temporary sequence of comma separated values usually in () or result of qw operatorArray = container for a listUse:Array initializationExtract values from arraymy @array = qw/blood sweat tears/;my ($var1, $var2[42], $var3, @var4) = @args;my ($var5, $var6) = @args;
ListsList flatteningRemember: each element of list must be a scalar, not another list=> NOT hierarchical list of 3 elementsFLATTENED list of scalar valuesIndividual access and slicing cf. arraysmy @vehicles = (‘truck’, @cars, (‘tank’,’jeep’));my @vehicles = (‘truck’, @cars, (‘tank’,’jeep’))[2,-1];
Hash variablesHash variables are denoted by the % dereferencing symbol.Hash variables is a list of key-value pairsBoth keys and values must be scalarNotice the ‘=>’ aka ‘quotifying comma’my %fruit_color = ("apple", "red", "banana", "yellow");my %fruit_color = (        apple  => "red",        banana => "yellow",    );
Hash variablesIndividual access: $hash{key}Access multiple values via slice:Assign multiple values via slice:@slice = @hash{‘key2’,’key23’,’key4’} @hash{‘key2’,’key23’,’key4’} = @new_values;
Non data typesFilehandleThere are several predefined filehandles, including STDIN, STDOUT, STDERR and DATA (default opened).No prefixCode value aka subroutineDereferencing symbol “&”
Marcs (bio)perl course
SubroutinesWe can reuse a segment of Perl code by placing it within a subroutine.The subroutine is defined using the sub keyword and a name (= variable name !!).The subroutine body is defined by placing code statements within the {} code block symbols.sub MySubroutine{    #Perl code goes here.    my @args = @_;}
SubroutinesTo call a subroutine, prepend the name with the & symbol:&MySubroutine; # w/o argumentsOr:MySubroutine(); # with or w/o arguments
SubroutinesArguments in underscore array variable (@_)List flattening !!my @results = MySubroutine(@arg1, ‘arg2’, (‘arg3’, ‘arg4’));sub MySubroutine{    #Perl code goes here.    my ($thingy, @args) = @_;}
SubroutinesReturn valueNothingScalar valueList valueReturn valueExplicit with return functionImplicit: value of the last statementsub MySubroutine{    #Perl code goes here.    my ($thingy, @args) = @_;do_something(@args);}
SubroutinesCalling contextsVoidScalarListwantarray functionVoid =>  undefScalar => 0List => 1getFiles($dir);my $num = getFiles($dir);my @files = getFiles($dir);
Functions and operators  Built-in routinesFunctionArguments at right hand sideSensible name (defined, open, print, ...)
FunctionsPerl provides a rich set of built-in functions to help you perform common tasks.Several categories of useful built-in function includeArithmetic functions (sqrt, sin, … )List functions (push, chomp, … )String functions (length, substr, … )Existance functions (defined, undef)
Array functionsArray as queue: push/shift (FIFO)Array as stack: push/pop (LIFO)@row1pushshift123unshiftpop
List functionschomp: remove newline from every element in the listmap: kind of loop without escape, every element ($_) is ‘processed’grep: kind of filtersort join
Hash functionskeys: returns the hash keys in random ordervalues: returns values of the hash in random order but same order as keys function calleach: returns (key, value) pairsdelete: remove a particular key (and associated value) from a hash
Operators OperatorComplex and subtle (=,<>, <=>, ?:, ->,=>,...)Symbolic name (+,<,>,&,!, ...)
OperatorsCalling contextEg. assignment operator ‘=‘($item1, $item2) = @array;$item1 = @array;
Marcs (bio)perl course
perldocAccess to Perl’s documentation systemCommand lineWeb: http://perldoc.perl.org/Documentation overview: perldoc perlFunction info:perldoc perlfuncperldoc -f <function name>Operator info: perldoc perlopSearching the FAQs: perldoc -q <FAQ keyword>
perldocLooking up module infoDocumentation: perldoc <module>Installation path: perldoc -l <module>Source: perldoc -m <module>All installed modules: perldoc -q installed
perldocMove around
perldocSearching
Creating a scriptText editor (vi, textpad, notepad++, ...)IDE (Komodo, Eclipse, EMACS, Geany, ...)See: www.perlide.org Shebang (not for modules)#!/usr/bin/perl
Executing a scriptCommand lineWindows.pl extension*NIXShebang linechmod +x script./script
Executing a scriptGeany IDE
Marcs (bio)perl course
References(and referents)A reference is a special scalar value which “refers to” or “points to” any value.A variable name is one kind of reference that you are already familiar with. It’s a given name.Reference is a kind of private, internal, computer generated nameA referent is the value that the reference is pointing to
Creating References	Method 1: references to variables are created by using the backslash(\) operator.$name = ‘bioperl’;	$reference = \$name;	$array_reference = \@array_name;	$hash_reference = \%hash_name;	$subroutine_ref = \&sub_name;
Creating References	Method 2:[ ITEMS ] makes a new, anonymous array and returns a reference to that array.{ ITEMS } makes a new, anonymous hash, and returns a reference to that hashmy $array_ref = [ 1, ‘foo’, undef, 13 ]; my $hash_ref = {one => 1, two => 2};
Dereferencing a Reference	Use the appropriate dereferencing symbolScalar: $Array: @Hash: %Subroutine: &
Dereferencing a Reference	Remember $name, ${‘name’} ?Means: give me the scalar value where the variable ‘name’ is pointing to.A reference $reference ìs a name, so $$reference, ${$reference}Means: give me the scalar value where the reference $reference is pointing to
Dereferencing a Reference	The arrow operator: ->Arrays and hashesSubroutinesmy $array_ref = [ 1, ‘foo’, undef, 13 ]; my $hash_ref = {one => 1, two => 2};${$array_ref}[1] = ${$hash_ref}{‘two’}# can be written as:$array_ref->[1] = $hash_ref->{two}&{$sub_ref}($arg1,$arg2)# can be written as:$sub_ref->($arg1, $arg2)
Identifying a referentref function
ReferencesWhy do we need references ???Create complex data structures!! Arrays and hashes can only store scalar values Pass arrays, hashes, subroutines, ... as arguments to subroutines and functions!! List flattening
Complex data structures	Remind:Reference is a scalar valueArrays and hashes are sets of scalar valuesIn one go: my $array_ref = [ 1, 2, 3 ];my $hash_ref = {one => 1, two => 2}; my %data = (	arrayref => $array_ref,hash_ref => $hash_ref);my %data = (	arrayref => [ 1, 2, 3 ],hash_ref => {one => 1, two => 2} 			);
Complex data structures	Individual accessmy %data = (	arrayref => [ 1, 2, 3 ],hash_ref => {one => 1,                           two => [‘a’,’b’]});How to access this value ?my $wanted_value = $data{hash_ref}->{two}->[1];
Complex data structuresmy @row1 = (1..3);my @row2 = (2,4,6);my @row3 = (3,6,9);my @rows = (\@row1,\@row2,\@row3);my $table = \@rows;@row1$table123@rows@row2246@row3369
Complex data structuresmy $table = [   [1, 2, 3],   [2, 4, 6],   [3, 6, 9]];$table123246369
Complex data structuresIndividual accessmy $wanted_value = $table->[1]->[2];# shorter form:$wanted_value = $table->[1][2] $table123246369
Packages and modules2 types of variables:Global aka package variablesLexical variables
Packages and modulesGlobal / package variablesVisible everywhere in every programYou get the if you don’t say otherwise!! AutovivificationName has 2 parts: family name + given nameDefault family name is ‘main’.    $John is actually $main::John$Cleese::John has nothing to do with $Wayne::JohnFamily name = package name$var1 = 42;print “$var1, “, ++$var2;# results in:42, 1
Marcs (bio)perl course
Packages and modulesLexical / private variablesExplicitely declared as Only visible within the boundaries of a code block or file.They cease to exist as soon as the program leaves the code block or the program endsThe do not have a family name aka they do not belong to a packageALWAYS USE LEXICAL VARIABLES(except for subroutines ...)my $var1 = 42;#!/usr/bin/perluse strict;my $var1 = 42;
PackagesWikipedia:Family where the (global!) variables (incl. subroutines) live (remember $John)In general, a namespace is a container that provides context for the identifiers (variable names) it holds, and allows the disambiguation of homonym identifiers residing in different namespaces.
PackagesFamily has a:name, defined via package declarationHouse, block or blocks of code that follow the package declaration package Bio::SeqIO::genbank;# welcome to the Bio::SeqIO::genbank familysub write_seq{}package Bio::SeqIO::fasta;# welcome to the Bio::SeqIO::fasta familysub write_seq{}
PackagesWhy do we need packages ???To organize codeTo improve maintainabilityTo avoid name space collisions
ModulesWhat ?A text file(with a .pm suffix) containing Perl source code, that can contain any number of namespaces. It must evaluate to a true value.LoadingAt compile time: use <module>At run time: require <expr><expr> and <module>:compiler translates each double-colon '::' into a path separator and appends '.pm'.E.g. Data::Dumper yields Data/Dumper.pmuse Data::Dumper;require Data::Dumper;require ‘my_file.pl’;require $class;
ModulesA module can contain multiple packages, but convention dictates that each module contains a package of the same name. easy to quickly locate the code in any given package (perldoc –m <module>)not obligatory !!A module name is unique1 to 1 mapping to file system !!Should start with capital letter
Module filesModule files are stored in a subdirectory hierarchy that parallels the module name hierarchy.All module files must have an extension of .pm.
ModulesModule path is relative. So, where is Perl searching for that module ?Possible modules roots@INC[]$ perldoc –V…@INC:    /etc/perl    /usr/local/lib/perl/5.10.1    /usr/local/share/perl/5.10.1    /usr/lib/perl5    /usr/share/perl5    /usr/lib/perl/5.10    /usr/share/perl/5.10    /usr/local/lib/site_perl    .
ModulesAlternative module roots (perldoc -q library)In scriptCommand line Environmentuse lib ‘/my/alternative/module/path’;[]$ perl -I/my/alternative/module/path script.plexport PERL5LIB=$PERL5LIB:/my/alternative/module/path
ModulesTest/Speak.pmTest.plpackage My::Package::Says::Hello;sub speak{  print __PACKAGE__, " says: 'Hello'\n";}package My::Package::Says::Blah;sub speak{  print __PACKAGE__, " says: 'Blah'\n";}1;#!/usr/bin/perluse strict;use Test::Speak;My::Package::Says::Hello>speak;My::Package::Says::Blah->speak;
ModulesWhy do we need modules???To organize packages into files/foldersCode reuse (ne copy & paste !)Module repository: CPANhttp://search.cpan.orghttps://metacpan.org/PragmaSpecial module that influences the code (compilation)LowercaseLexically scoped
ModulesModule informationIn standard distribution: perldoc perlmodlibManually installed: perldoc perllocalAll modules: perldoc –q installedDocumentation: perldoc <module name>Location: perldoc –l <module name>Source: perldoc –m <module name>
Packages and Modules - SummaryA package is a separate namespace within Perl code.A module can have more than one package defined within it.The default package is main.We can get to variables (and subroutines) within packages by using the fully qualified nameTo write a package, just write package <package name> where you want the package to start.Package declarations last until the end of the enclosing block, file or until the next package statementThe require and use keywords can be used to import the contents of other files for use in a program.Files which are included must end with a true value.Perl looks for modules in a list of directories stored in @INCModule names map to the file system
Marcs (bio)perl course
ExercisesBioperl Training Exercise 1: perldocBioperl Training Exercise 2: thou shalt not forgetBioperl Training Exercise 3: arraysBioperl Training Exercise 4: hashesBioperl Training Exercise 5: packages and modules 1Bioperl Training Exercise 6: packages and modules 2Bioperl Training Exercise 7: complex data structures
Marcs (bio)perl course
Object Oriented Programming in PerlWhy do we need objects and OOP ?It’s funCode reuseAbstraction
Object Oriented Programming in PerlWhat is an object ?An object is a (complex) data structure representing a new, user defined type with a collection of behaviors (functions aka methods)Collection of attributesDeveloper’s perspective: 3 little make rulesTo create a class, build a packageTo create a method, write a subroutineTo create an object, bless a referent
Rule 1: To create a class, build a packageDefining a classA class is simply a package with subroutines that function as methods. Class name = type = label = namespacepackage Cat;1;
Rule 2: To create a method, write a subroutineFirst argument of methods is always class name or object itself (or rather:  reference)Subroutine call the OO way (method invocation  arrow operator)package Cat;sub meow {  my $self = shift;  print __PACKAGE__ “ says: meow !\n”;}1;Cat->meow;$cat->meow;
Rule 3: To create an object, bless a referent ‘Special’ method: constructorAny name will do, in most cases newObject can be anything, in most cases hashReference to object is stored in variableblessArguments: reference (+ class). Does not change !!Underlying referent is blessed (= typed, labelled)Returns referencepackage Cat;sub new {  my ($class, @args) = @_;  my $self = { _name => $_args[0] };  bless $self, $class;}
ObjectsPerl objects are data structures ( a collection of attributes).To create an object we have to take 3 rules into account:Classes are just packagesMethods are just subroutinesBlessing a referent creates an object
ObjectsObjects are passed around as referencesCalling an object method can be done using the method invocation arrow:Constructor functions in Perl are conventionally called new() and can be called by writing: $object_ref->method()$object_ref = ClassName->new()
InheritanceConceptWay to extend functionality of a class by deriving a (more specific) sub-class from itIn Perl:Way of specifying where to look for methodsstore the name of 1 or more classes in the package variable @ISAMultiple inheritance  !!package NorthAmericanCat;use Cat;@ISA = qw(Cat);package NorthAmericanCat;use Cat;use Animal;@ISA = qw(Cat Animal);
InheritanceUNIVERSAL, parent of all classesPredifined methodsisa(‘<class name>’): check if the object inherits from a particular classcan(‘<method name>’): check if <method name> is a callable method
InheritanceSUPER: superclass of the current packagestart looking in @ISA for a class that can() do_somethingexplicitely call a method of a parental classoften used by Bioperl to initialize object attributes$self->SUPER::do_something()
PolymorphismConceptmethods defined in the base class will override methods defined in the parent classessame method has different behaviours

More Related Content

PDF
Marc’s (bio)perl course
PDF
Php array
PPT
Php array
PDF
Arrays in PHP
PPT
Php Using Arrays
PDF
PHP Unit 4 arrays
PPT
Class 4 - PHP Arrays
Marc’s (bio)perl course
Php array
Php array
Arrays in PHP
Php Using Arrays
PHP Unit 4 arrays
Class 4 - PHP Arrays

What's hot (18)

PPTX
PHP array 1
PPT
PPTX
Chap 3php array part1
PDF
4.1 PHP Arrays
PDF
DBIx::Class introduction - 2010
PPTX
Data structure in perl
PDF
Scripting3
PPT
Introduction to perl_lists
PPTX
Array in php
PPT
03 Php Array String Functions
PPTX
Array,lists and hashes in perl
PDF
Working with text, Regular expressions
ODP
Intermediate Perl
ODP
Introduction to Perl
PDF
GDI Seattle - Intro to JavaScript Class 2
PPTX
Array
PHP array 1
Chap 3php array part1
4.1 PHP Arrays
DBIx::Class introduction - 2010
Data structure in perl
Scripting3
Introduction to perl_lists
Array in php
03 Php Array String Functions
Array,lists and hashes in perl
Working with text, Regular expressions
Intermediate Perl
Introduction to Perl
GDI Seattle - Intro to JavaScript Class 2
Array
Ad

Viewers also liked (20)

DOC
تحليل محتوى عاشر تك الانظمة
PPT
Programming for biologists
PDF
BioPerl: Developming Open Source Software
PDF
PERL- Bioperl modules
PDF
Bioinformatics and BioPerl
PDF
Fields bosc2010 bio_perl
PPS
Sant jordi
PPT
Presentación1
PDF
Smau milano 2013 fratepietro vaciago
PPTX
EXAMEN PRÁCTICO DE COMPUTACIÓN 2DO BIM
PDF
eChaty magazín Jaro 2013
DOCX
Trabajo Práctico de Periodismo - noticia
PDF
Ultimos avances tecnológicos
PDF
Social Media for CEOs - The External Opportunity
PDF
Guardia Civil
PDF
[기아워캠9기] 정연지 활동결과보고_독일 IJGD4324
PPTX
Brainsprouting- Don Inservice 2016
PPT
Weathering Market Storms
PPS
Yo Misma
PDF
Natalija Popović - Budućnost kao imperativ, Controlling magazin 06
تحليل محتوى عاشر تك الانظمة
Programming for biologists
BioPerl: Developming Open Source Software
PERL- Bioperl modules
Bioinformatics and BioPerl
Fields bosc2010 bio_perl
Sant jordi
Presentación1
Smau milano 2013 fratepietro vaciago
EXAMEN PRÁCTICO DE COMPUTACIÓN 2DO BIM
eChaty magazín Jaro 2013
Trabajo Práctico de Periodismo - noticia
Ultimos avances tecnológicos
Social Media for CEOs - The External Opportunity
Guardia Civil
[기아워캠9기] 정연지 활동결과보고_독일 IJGD4324
Brainsprouting- Don Inservice 2016
Weathering Market Storms
Yo Misma
Natalija Popović - Budućnost kao imperativ, Controlling magazin 06
Ad

Similar to Marcs (bio)perl course (20)

ODP
Introduction to Perl - Day 2
ODP
Perl Introduction
PPT
LPW: Beginners Perl
ODP
Introduction to Perl - Day 1
ODP
Beginning Perl
PPTX
Lecture 3 Perl & FreeBSD administration
PDF
Introduction to Perl
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
PPT
Bioinformatica 27-10-2011-p4-files
PPT
Bioinformatica 10-11-2011-p6-bioperl
PPT
Perl tutorial
PDF
Cs3430 lecture 17
PDF
Introduction to Perl and BioPerl
PPT
CGI With Object Oriented Perl
PPTX
PERL PROGRAMMING LANGUAGE Basic Introduction
PDF
Perl6 signatures, types and multicall
PPT
Introduction to perl_control structures
PPTX
Unit 1-subroutines in perl
Introduction to Perl - Day 2
Perl Introduction
LPW: Beginners Perl
Introduction to Perl - Day 1
Beginning Perl
Lecture 3 Perl & FreeBSD administration
Introduction to Perl
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Bioinformatica 27-10-2011-p4-files
Bioinformatica 10-11-2011-p6-bioperl
Perl tutorial
Cs3430 lecture 17
Introduction to Perl and BioPerl
CGI With Object Oriented Perl
PERL PROGRAMMING LANGUAGE Basic Introduction
Perl6 signatures, types and multicall
Introduction to perl_control structures
Unit 1-subroutines in perl

More from BITS (20)

PDF
RNA-seq for DE analysis: detecting differential expression - part 5
PDF
RNA-seq for DE analysis: extracting counts and QC - part 4
PDF
RNA-seq for DE analysis: the biology behind observed changes - part 6
PDF
RNA-seq: analysis of raw data and preprocessing - part 2
PDF
RNA-seq: general concept, goal and experimental design - part 1
PDF
RNA-seq: Mapping and quality control - part 3
PDF
Productivity tips - Introduction to linux for bioinformatics
PDF
Text mining on the command line - Introduction to linux for bioinformatics
PDF
The structure of Linux - Introduction to Linux for bioinformatics
PDF
Managing your data - Introduction to Linux for bioinformatics
PDF
Introduction to Linux for bioinformatics
PDF
BITS - Genevestigator to easily access transcriptomics data
PDF
BITS - Comparative genomics: the Contra tool
PDF
BITS - Comparative genomics on the genome level
PDF
BITS - Comparative genomics: gene family analysis
PDF
BITS - Introduction to comparative genomics
PDF
BITS - Protein inference from mass spectrometry data
PDF
BITS - Overview of sequence databases for mass spectrometry data analysis
PDF
BITS - Search engines for mass spec data
PDF
BITS - Introduction to proteomics
RNA-seq for DE analysis: detecting differential expression - part 5
RNA-seq for DE analysis: extracting counts and QC - part 4
RNA-seq for DE analysis: the biology behind observed changes - part 6
RNA-seq: analysis of raw data and preprocessing - part 2
RNA-seq: general concept, goal and experimental design - part 1
RNA-seq: Mapping and quality control - part 3
Productivity tips - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
The structure of Linux - Introduction to Linux for bioinformatics
Managing your data - Introduction to Linux for bioinformatics
Introduction to Linux for bioinformatics
BITS - Genevestigator to easily access transcriptomics data
BITS - Comparative genomics: the Contra tool
BITS - Comparative genomics on the genome level
BITS - Comparative genomics: gene family analysis
BITS - Introduction to comparative genomics
BITS - Protein inference from mass spectrometry data
BITS - Overview of sequence databases for mass spectrometry data analysis
BITS - Search engines for mass spec data
BITS - Introduction to proteomics

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx

Marcs (bio)perl course

  • 2. Perl“A script is what you give an actor, but a program is what you give an audience.”
  • 3. GoalsPerl Positioning System: find your way in the Perl WorldWrite Once, Use Many TimesObject Oriented PerlConsumerDeveloperThou shalt not be afraid of the Bioperl Beast
  • 5. Agenda Day1Perl refresherScalarsArrays and listsHashesSubroutines and functionsPerldocCreating and running a Perl scriptReferences and advanced data structuresPackages and modulesObjects, (multiple) inheritance, polymorphism
  • 6. Agenda Day 2What is bioperl ?Taming the Bioperl BeastFinding modulesFinding methodsData::DumperSequence processingOne image says more than 1000 words
  • 7. VariablesData of any type may be stored within three basic types of variables:Scalar (strings, numbers, references)Array (aka list but not quite the same)Hash (aka associative array)Variable names are always preceded by a “dereferencing symbol” or prefix. If needed: {}$ - Scalar variables@ - List variables% - Associative array aka hash variables
  • 8. VariablesYou do NOT have to Declare the variable before using itDefine the variable’s data typeAllocate memory for new data values
  • 9. Scalar variablesScalar variable stores a string, a number, a character, a reference, undef$name, ${name}, ${‘name’}More magic: $_
  • 10. Array variablesArray variable stores a list of scalars@name, @{name}, @{‘name’}IndexMap: index => scalar valuezero-indexed (distance from start)
  • 11. Array variablesList assignment:Individual assignment: $count[2] = 42Individual acces: print $count[2]Special variable $#<array name>Scalar context@count = (1, 2, 3, 4, 5);@count = (‘apple’, ‘bat’, ‘cat’);@count2 = @count;
  • 12. Array variables Access multiple values via array slice:Assign multiple values via array slice:print @array[3,2,4,1,0,-1];@array[3,2,4,1,0,-1] = @new_values;
  • 13. ListsList = temporary sequence of comma separated values usually in () or result of qw operatorArray = container for a listUse:Array initializationExtract values from arraymy @array = qw/blood sweat tears/;my ($var1, $var2[42], $var3, @var4) = @args;my ($var5, $var6) = @args;
  • 14. ListsList flatteningRemember: each element of list must be a scalar, not another list=> NOT hierarchical list of 3 elementsFLATTENED list of scalar valuesIndividual access and slicing cf. arraysmy @vehicles = (‘truck’, @cars, (‘tank’,’jeep’));my @vehicles = (‘truck’, @cars, (‘tank’,’jeep’))[2,-1];
  • 15. Hash variablesHash variables are denoted by the % dereferencing symbol.Hash variables is a list of key-value pairsBoth keys and values must be scalarNotice the ‘=>’ aka ‘quotifying comma’my %fruit_color = ("apple", "red", "banana", "yellow");my %fruit_color = ( apple => "red", banana => "yellow", );
  • 16. Hash variablesIndividual access: $hash{key}Access multiple values via slice:Assign multiple values via slice:@slice = @hash{‘key2’,’key23’,’key4’} @hash{‘key2’,’key23’,’key4’} = @new_values;
  • 17. Non data typesFilehandleThere are several predefined filehandles, including STDIN, STDOUT, STDERR and DATA (default opened).No prefixCode value aka subroutineDereferencing symbol “&”
  • 19. SubroutinesWe can reuse a segment of Perl code by placing it within a subroutine.The subroutine is defined using the sub keyword and a name (= variable name !!).The subroutine body is defined by placing code statements within the {} code block symbols.sub MySubroutine{ #Perl code goes here. my @args = @_;}
  • 20. SubroutinesTo call a subroutine, prepend the name with the & symbol:&MySubroutine; # w/o argumentsOr:MySubroutine(); # with or w/o arguments
  • 21. SubroutinesArguments in underscore array variable (@_)List flattening !!my @results = MySubroutine(@arg1, ‘arg2’, (‘arg3’, ‘arg4’));sub MySubroutine{ #Perl code goes here. my ($thingy, @args) = @_;}
  • 22. SubroutinesReturn valueNothingScalar valueList valueReturn valueExplicit with return functionImplicit: value of the last statementsub MySubroutine{ #Perl code goes here. my ($thingy, @args) = @_;do_something(@args);}
  • 23. SubroutinesCalling contextsVoidScalarListwantarray functionVoid => undefScalar => 0List => 1getFiles($dir);my $num = getFiles($dir);my @files = getFiles($dir);
  • 24. Functions and operators Built-in routinesFunctionArguments at right hand sideSensible name (defined, open, print, ...)
  • 25. FunctionsPerl provides a rich set of built-in functions to help you perform common tasks.Several categories of useful built-in function includeArithmetic functions (sqrt, sin, … )List functions (push, chomp, … )String functions (length, substr, … )Existance functions (defined, undef)
  • 26. Array functionsArray as queue: push/shift (FIFO)Array as stack: push/pop (LIFO)@row1pushshift123unshiftpop
  • 27. List functionschomp: remove newline from every element in the listmap: kind of loop without escape, every element ($_) is ‘processed’grep: kind of filtersort join
  • 28. Hash functionskeys: returns the hash keys in random ordervalues: returns values of the hash in random order but same order as keys function calleach: returns (key, value) pairsdelete: remove a particular key (and associated value) from a hash
  • 29. Operators OperatorComplex and subtle (=,<>, <=>, ?:, ->,=>,...)Symbolic name (+,<,>,&,!, ...)
  • 30. OperatorsCalling contextEg. assignment operator ‘=‘($item1, $item2) = @array;$item1 = @array;
  • 32. perldocAccess to Perl’s documentation systemCommand lineWeb: http://perldoc.perl.org/Documentation overview: perldoc perlFunction info:perldoc perlfuncperldoc -f <function name>Operator info: perldoc perlopSearching the FAQs: perldoc -q <FAQ keyword>
  • 33. perldocLooking up module infoDocumentation: perldoc <module>Installation path: perldoc -l <module>Source: perldoc -m <module>All installed modules: perldoc -q installed
  • 36. Creating a scriptText editor (vi, textpad, notepad++, ...)IDE (Komodo, Eclipse, EMACS, Geany, ...)See: www.perlide.org Shebang (not for modules)#!/usr/bin/perl
  • 37. Executing a scriptCommand lineWindows.pl extension*NIXShebang linechmod +x script./script
  • 40. References(and referents)A reference is a special scalar value which “refers to” or “points to” any value.A variable name is one kind of reference that you are already familiar with. It’s a given name.Reference is a kind of private, internal, computer generated nameA referent is the value that the reference is pointing to
  • 41. Creating References Method 1: references to variables are created by using the backslash(\) operator.$name = ‘bioperl’; $reference = \$name; $array_reference = \@array_name; $hash_reference = \%hash_name; $subroutine_ref = \&sub_name;
  • 42. Creating References Method 2:[ ITEMS ] makes a new, anonymous array and returns a reference to that array.{ ITEMS } makes a new, anonymous hash, and returns a reference to that hashmy $array_ref = [ 1, ‘foo’, undef, 13 ]; my $hash_ref = {one => 1, two => 2};
  • 43. Dereferencing a Reference Use the appropriate dereferencing symbolScalar: $Array: @Hash: %Subroutine: &
  • 44. Dereferencing a Reference Remember $name, ${‘name’} ?Means: give me the scalar value where the variable ‘name’ is pointing to.A reference $reference ìs a name, so $$reference, ${$reference}Means: give me the scalar value where the reference $reference is pointing to
  • 45. Dereferencing a Reference The arrow operator: ->Arrays and hashesSubroutinesmy $array_ref = [ 1, ‘foo’, undef, 13 ]; my $hash_ref = {one => 1, two => 2};${$array_ref}[1] = ${$hash_ref}{‘two’}# can be written as:$array_ref->[1] = $hash_ref->{two}&{$sub_ref}($arg1,$arg2)# can be written as:$sub_ref->($arg1, $arg2)
  • 47. ReferencesWhy do we need references ???Create complex data structures!! Arrays and hashes can only store scalar values Pass arrays, hashes, subroutines, ... as arguments to subroutines and functions!! List flattening
  • 48. Complex data structures Remind:Reference is a scalar valueArrays and hashes are sets of scalar valuesIn one go: my $array_ref = [ 1, 2, 3 ];my $hash_ref = {one => 1, two => 2}; my %data = ( arrayref => $array_ref,hash_ref => $hash_ref);my %data = ( arrayref => [ 1, 2, 3 ],hash_ref => {one => 1, two => 2} );
  • 49. Complex data structures Individual accessmy %data = ( arrayref => [ 1, 2, 3 ],hash_ref => {one => 1, two => [‘a’,’b’]});How to access this value ?my $wanted_value = $data{hash_ref}->{two}->[1];
  • 50. Complex data structuresmy @row1 = (1..3);my @row2 = (2,4,6);my @row3 = (3,6,9);my @rows = (\@row1,\@row2,\@row3);my $table = \@rows;@row1$table123@rows@row2246@row3369
  • 51. Complex data structuresmy $table = [ [1, 2, 3], [2, 4, 6], [3, 6, 9]];$table123246369
  • 52. Complex data structuresIndividual accessmy $wanted_value = $table->[1]->[2];# shorter form:$wanted_value = $table->[1][2] $table123246369
  • 53. Packages and modules2 types of variables:Global aka package variablesLexical variables
  • 54. Packages and modulesGlobal / package variablesVisible everywhere in every programYou get the if you don’t say otherwise!! AutovivificationName has 2 parts: family name + given nameDefault family name is ‘main’. $John is actually $main::John$Cleese::John has nothing to do with $Wayne::JohnFamily name = package name$var1 = 42;print “$var1, “, ++$var2;# results in:42, 1
  • 56. Packages and modulesLexical / private variablesExplicitely declared as Only visible within the boundaries of a code block or file.They cease to exist as soon as the program leaves the code block or the program endsThe do not have a family name aka they do not belong to a packageALWAYS USE LEXICAL VARIABLES(except for subroutines ...)my $var1 = 42;#!/usr/bin/perluse strict;my $var1 = 42;
  • 57. PackagesWikipedia:Family where the (global!) variables (incl. subroutines) live (remember $John)In general, a namespace is a container that provides context for the identifiers (variable names) it holds, and allows the disambiguation of homonym identifiers residing in different namespaces.
  • 58. PackagesFamily has a:name, defined via package declarationHouse, block or blocks of code that follow the package declaration package Bio::SeqIO::genbank;# welcome to the Bio::SeqIO::genbank familysub write_seq{}package Bio::SeqIO::fasta;# welcome to the Bio::SeqIO::fasta familysub write_seq{}
  • 59. PackagesWhy do we need packages ???To organize codeTo improve maintainabilityTo avoid name space collisions
  • 60. ModulesWhat ?A text file(with a .pm suffix) containing Perl source code, that can contain any number of namespaces. It must evaluate to a true value.LoadingAt compile time: use <module>At run time: require <expr><expr> and <module>:compiler translates each double-colon '::' into a path separator and appends '.pm'.E.g. Data::Dumper yields Data/Dumper.pmuse Data::Dumper;require Data::Dumper;require ‘my_file.pl’;require $class;
  • 61. ModulesA module can contain multiple packages, but convention dictates that each module contains a package of the same name. easy to quickly locate the code in any given package (perldoc –m <module>)not obligatory !!A module name is unique1 to 1 mapping to file system !!Should start with capital letter
  • 62. Module filesModule files are stored in a subdirectory hierarchy that parallels the module name hierarchy.All module files must have an extension of .pm.
  • 63. ModulesModule path is relative. So, where is Perl searching for that module ?Possible modules roots@INC[]$ perldoc –V…@INC: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .
  • 64. ModulesAlternative module roots (perldoc -q library)In scriptCommand line Environmentuse lib ‘/my/alternative/module/path’;[]$ perl -I/my/alternative/module/path script.plexport PERL5LIB=$PERL5LIB:/my/alternative/module/path
  • 65. ModulesTest/Speak.pmTest.plpackage My::Package::Says::Hello;sub speak{ print __PACKAGE__, " says: 'Hello'\n";}package My::Package::Says::Blah;sub speak{ print __PACKAGE__, " says: 'Blah'\n";}1;#!/usr/bin/perluse strict;use Test::Speak;My::Package::Says::Hello>speak;My::Package::Says::Blah->speak;
  • 66. ModulesWhy do we need modules???To organize packages into files/foldersCode reuse (ne copy & paste !)Module repository: CPANhttp://search.cpan.orghttps://metacpan.org/PragmaSpecial module that influences the code (compilation)LowercaseLexically scoped
  • 67. ModulesModule informationIn standard distribution: perldoc perlmodlibManually installed: perldoc perllocalAll modules: perldoc –q installedDocumentation: perldoc <module name>Location: perldoc –l <module name>Source: perldoc –m <module name>
  • 68. Packages and Modules - SummaryA package is a separate namespace within Perl code.A module can have more than one package defined within it.The default package is main.We can get to variables (and subroutines) within packages by using the fully qualified nameTo write a package, just write package <package name> where you want the package to start.Package declarations last until the end of the enclosing block, file or until the next package statementThe require and use keywords can be used to import the contents of other files for use in a program.Files which are included must end with a true value.Perl looks for modules in a list of directories stored in @INCModule names map to the file system
  • 70. ExercisesBioperl Training Exercise 1: perldocBioperl Training Exercise 2: thou shalt not forgetBioperl Training Exercise 3: arraysBioperl Training Exercise 4: hashesBioperl Training Exercise 5: packages and modules 1Bioperl Training Exercise 6: packages and modules 2Bioperl Training Exercise 7: complex data structures
  • 72. Object Oriented Programming in PerlWhy do we need objects and OOP ?It’s funCode reuseAbstraction
  • 73. Object Oriented Programming in PerlWhat is an object ?An object is a (complex) data structure representing a new, user defined type with a collection of behaviors (functions aka methods)Collection of attributesDeveloper’s perspective: 3 little make rulesTo create a class, build a packageTo create a method, write a subroutineTo create an object, bless a referent
  • 74. Rule 1: To create a class, build a packageDefining a classA class is simply a package with subroutines that function as methods. Class name = type = label = namespacepackage Cat;1;
  • 75. Rule 2: To create a method, write a subroutineFirst argument of methods is always class name or object itself (or rather: reference)Subroutine call the OO way (method invocation arrow operator)package Cat;sub meow { my $self = shift; print __PACKAGE__ “ says: meow !\n”;}1;Cat->meow;$cat->meow;
  • 76. Rule 3: To create an object, bless a referent ‘Special’ method: constructorAny name will do, in most cases newObject can be anything, in most cases hashReference to object is stored in variableblessArguments: reference (+ class). Does not change !!Underlying referent is blessed (= typed, labelled)Returns referencepackage Cat;sub new { my ($class, @args) = @_; my $self = { _name => $_args[0] }; bless $self, $class;}
  • 77. ObjectsPerl objects are data structures ( a collection of attributes).To create an object we have to take 3 rules into account:Classes are just packagesMethods are just subroutinesBlessing a referent creates an object
  • 78. ObjectsObjects are passed around as referencesCalling an object method can be done using the method invocation arrow:Constructor functions in Perl are conventionally called new() and can be called by writing: $object_ref->method()$object_ref = ClassName->new()
  • 79. InheritanceConceptWay to extend functionality of a class by deriving a (more specific) sub-class from itIn Perl:Way of specifying where to look for methodsstore the name of 1 or more classes in the package variable @ISAMultiple inheritance !!package NorthAmericanCat;use Cat;@ISA = qw(Cat);package NorthAmericanCat;use Cat;use Animal;@ISA = qw(Cat Animal);
  • 80. InheritanceUNIVERSAL, parent of all classesPredifined methodsisa(‘<class name>’): check if the object inherits from a particular classcan(‘<method name>’): check if <method name> is a callable method
  • 81. InheritanceSUPER: superclass of the current packagestart looking in @ISA for a class that can() do_somethingexplicitely call a method of a parental classoften used by Bioperl to initialize object attributes$self->SUPER::do_something()
  • 82. PolymorphismConceptmethods defined in the base class will override methods defined in the parent classessame method has different behaviours
  • 84. ExercisesBioperl Training Exercise 8: OOPBioperl Training Exercise 9: inheritance, polymorphismBioperl Training Exercise 10: aggregation, delegation

Editor's Notes

  • #5: Remember to wear it !
  • #10: Not often needed. Why might you need the braces ? String interpolation:$name = ‘Johnny’;Print “$name1”; # =&gt; nothing printedPrint “${name}1”; # =&gt; ‘Johnny1’
  • #14: If there are more variables in the list than elements in the array, the extra variables are assigned the udefined value. If there are fewer variables than array elements, the extra elements are ignored.Distributiviteit: my ()
  • #15: If there are more variables in the list than elements in the array, the extra variables are assigned the udefined value. If there are fewer variables than array elements, the extra elements are ignored.
  • #25: Comma is operator: flattens (‘concatenates’) lists/arrays
  • #30: Comma is operator: flattens (‘concatenates’) lists/arrays
  • #49: No parens needed: comma operators produce list
  • #55: main should have been called ‘our’ ;-)Not needed to use the family name when you are with your family. If you call John for dinner, John will know it’s him and you know who will come.But if your family has visitors of another family and they have a John in the family as well ...Family name + given name = fully qualified variable name