SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler offline
Writing Perl Scripts

Boyce Thompson Institute for Plant
            Research
           Tower Road
  Ithaca, New York 14853-1801
             U.S.A.

             by
 Aureliano Bombarely Gomez
Writing Perl Scripts:
   1. Four mandatory lines.

   2. Useful modules I: Files.

   3. Useful modules II: Options.

   4. Documentation and being verbose.

   5. Exercise: Assembly stats.
Writing Perl Scripts:
   1. Four mandatory lines.

   2. Useful modules I: Files.

   3. Useful modules II: Options.

   4. Documentation and being verbose.

   5. Exercise: Assembly stats.
1. Four mandatory lines.


   1.LINE:             #!/usr/bin/perl
      Where ?         At the beginning of the script.
      Why ?           It says to the operating system what
                      program needs to use to executate the script .

   2.LINE:            use warnings;
      Where ?         Before declare the modules and variables.
                      (sooner is better).
      Why ?           It will print any compilation warnings.

   3.LINE:            use strict;
      Where ?         Before declare the modules and variables.
                      (sooner is better).
      Why ?           It will check any gramatical error and it will not
                      Let run scripts with errors.
1. Four mandatory lines.


   4.LINE:                1;
      Where ?             At the end of the script.
      Why ?               It says to the operating system that the script
                          It is done.


        #!/usr/bin/perl

        use strict;
        use warnings;

        ###########
        ## MY CODE
        ###########

        1;
Writing Perl Scripts:
   1. Four mandatory lines.

   2. Useful modules I: Files.

   3. Useful modules II: Options.

   4. Documentation and being verbose.

   5. Exercise: Assembly stats.
2. Useful modules I: Files


   JUST A REMINDER: How open/Read/Write/Close files.

   1. OPEN FUNCTION.

          open (FILEHANDLE, MODE, REFERENCE);


          FILEHANDLES: undefined scalar variable autovivified.

          MODE:     read, input only:               <
                    write, output only:             >
                    append to a file:               >>
                    read/write update access:       +<
                    write/read update access        +>
                    read/append update access       +>>

          REFERENCE: Filename or reference to open
2. Useful modules I: Files


   JUST A REMINDER: How open/Read/Write/Close files.

   1. OPEN FUNCTION.

          open (my $ifh, '<', $input_filename);
          open (my $ofh, '>', $output_filename);


       SUGGESTIONS: “use autodie” instead “OR die(“my error”)”;


          open (my $ifh, '<', $input_filename)
             OR die(“ERROR OPENING FILE: $!”);
2. Useful modules I: Files


   JUST A REMINDER: How open/Read/Write/Close files.

   2. READING OPENED FILES.
          while(<FILEHANDLE>) {
              ## BLOCK USING $_ as LINE (don't forget chomp)
          }


       SUGGESTIONS: “Know the status of the file”

          my @filelines = <FILEHANDLE>;
          my $L = scalar(@filelines);
          my $l = 0;

          foreach my $line (@filelines) {
              $l++;
              print STDERR “Reading line $l of $L lines   r”;
          }
2. Useful modules I: Files


   JUST A REMINDER: How open/Read/Write/Close files.

   3. WRITE OVER OPENED FILES.

          print $ofh “This to print over the file”;




   4. CLOSE FILES.

          close($ofh);
1. Useful modules I: Files


   a) File::Basename;

       Parse file paths into directory, filename and suffix.


           use File::Basename;

           my ($name, $path, $suffix) = fileparse($fullname,@suffixlist);


           my $name = fileparse($fullname, @suffixlist);

           my $basename = basename($fullname, @suffixlist);

           my $dirname = dirname($fullname);
2. Useful modules I: Files


   b) File::Spec;

       Operations over filenames.


          use File::Spec;


          my $currdir = File::Spec->currdir();
          my $tempdir = File::Spec->tempdir();

          my $path = File::Spec->catfile($currdir, $filename);
Writing Perl Scripts:
   1. Four mandatory lines.

   2. Useful modules I: Files.

   3. Useful modules II: Options.

   4. Documentation and being verbose.

   5. Exercise: Assembly stats.
3. Useful modules II: Options


   Usual way to pass options: Using $ARGV

          user@comp$ myscript.pl argument1 argument2



          #!/usr/bin/perl

          use strict;
          use warnings;
          use autodie;

          my ($arg1, $arg2) = @ARGV;


          1;
3. Useful modules II: Options


   Usual way to pass options: Using $ARGV


   PROBLEM:

      When there are multiple arguments can be confusing.

      Mandatory arguments are difficult to check !!!

   SOLUTION:

      Use modules GetOpt::Std or GetOpt::Long
3. Useful modules II: Options


   GetOpt::Std;

      Process single-character arguments from the command line

          user@comp$ myscript.pl -i argument1 -o argument2 -V -H
          use GetOpt::Std;

          our( $opt_i, $opt_o, $opt_V, $opt_H);
          getopts(i:o:VH);

          ## i: and o: expect something aftter the switch.
          my $input = $opt_i || die(“ERROR: -i <input> was not supplied.”);
          my $output = $opt_i || die(“ERROR: -o <output> was not supplied.”);

          ## V and H don't expect anything after the switch.
          if ($opt_H) {
               print $help;
          }
Writing Perl Scripts:
   1. Four mandatory lines.

   2. Useful modules I: Files.

   3. Useful modules II: Options.

   4. Documentation and being verbose.

   5. Exercise: Assembly stats.
4. Documentation and being verbose


   Three types of documentation:

      1) Document code with #.
            GOOD: Useful for developers.
            BAD: Inaccessible for users if they not open the script.

      2) Document using perldoc.
            GOOD: Clear and formated information.
            BAD: perdoc is not always installed in the system.

      3) Document using an inside print function.
            GOOD: Frecuently easy to access. Intuitive.
            BAD: ??? Well increase the size of your script.
4. Documentation and being verbose


   Three types of documentation:

      1) Document code with #.
            GOOD: Useful for developers.
            BAD: Inaccessible for users if they not open the script.

      2) Document using perldoc.
            GOOD: Clear and formated information.
            BAD: perdoc is not always installed in the system.

      3) Document using an inside print function.
            GOOD: Frecuently easy to access. Intuitive.
            BAD: ??? Well increase the size of your script.
4. Documentation and being verbose


   Documenting through a function;
         sub help {

             print STDERR <<EOF;
             $0:
                 Description:
                     My program description.

                 Synopsis:
                    myscript.pl [-H] [-V] -i <input>

                 Arguments:
                    -i <input>        input file (mandatory)
                    -H <help>         print Help.
                    -V <verbose>      be verbose

             EOF;
             Exit(1);
         }
4. Documentation and being verbose


   Calling help;


        use GetOpt::Std;

        our( $opt_i, $opt_o, $opt_V, $opt_H);
        getopts(i:o:VH);

        ## i: and o: expect something aftter the switch.
        my $input = $opt_i || die(“ERROR: -i <input> was not supplied.”);
        my $output = $opt_i || die(“ERROR: -o <output> was not supplied.”);

        ## V and H don't expect anything after the switch.
        if ($opt_H) {
             help();
        }
4. Documentation and being verbose


   Being verbose;

        use GetOpt::Std;

        our( $opt_i, $opt_o, $opt_V, $opt_H);
        getopts(i:o:VH);

        ## i: and o: expect something aftter the switch.
        my $input = $opt_i || die(“ERROR: -i <input> was not supplied.”);
        my $output = $opt_i || die(“ERROR: -o <output> was not supplied.”);

        if ($opt_V) {
             my $date = `date`;
             chomp($date);
             print STDERR “Step 1 [$date]:ntParsing -i $input file.n”;
        }
4. Documentation and being verbose


   Being verbose;



         my @filelines = <FILEHANDLE>;
         my $L = scalar(@filelines);
         my $l = 0;

         foreach my $line (@filelines) {
             $l++;
             if ($opt_V) {
                  print STDERR “Reading line $l of $L lines   r”;
             }
         }
Writing Perl Scripts:
   1. Four mandatory lines.

   2. Useful modules I: Files.

   3. Useful modules II: Options.

   4. Documentation and being verbose.

   5. Exercise: Assembly stats.
5. Exercise: Assembly Stats


    GOAL: Create a script to calculate:

       1) Number of sequence in a file.

       2) Total BP of a file.

       3) Longest sequence

       4) Shortest sequence.

       5) Average and SD.

       6) N25, N50, N75, N90, N95 (length and indexes)
5. Exercise: Assembly Stats


       6) N25, N50, N75, N90, N95 (length and indexes)

          Just a reminder:

             N50 Length is the minimun length contained by the
                 50% of the size of the file (in bp) when it is ordered
                 by decreasing length.

             N50 Index is the number os sequences contained by the
                 50% of the size of the file (in bp) when it is ordered
                 by decreasing length.

Mais conteúdo relacionado

Mais procurados

CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11Combell NV
 
Cli the other sapi pbc11
Cli the other sapi pbc11Cli the other sapi pbc11
Cli the other sapi pbc11Combell NV
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1兎 伊藤
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPIPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPGuilherme Blanco
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021Ayesh Karunaratne
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11Combell NV
 
Methods of debugging - Atomate.net
Methods of debugging - Atomate.netMethods of debugging - Atomate.net
Methods of debugging - Atomate.netVitalie Chiperi
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 

Mais procurados (20)

SPL, not a bridge too far
SPL, not a bridge too farSPL, not a bridge too far
SPL, not a bridge too far
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11
 
Syntax
SyntaxSyntax
Syntax
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Cli the other sapi pbc11
Cli the other sapi pbc11Cli the other sapi pbc11
Cli the other sapi pbc11
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Workshop unittesting
Workshop unittestingWorkshop unittesting
Workshop unittesting
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPIPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11
 
Methods of debugging - Atomate.net
Methods of debugging - Atomate.netMethods of debugging - Atomate.net
Methods of debugging - Atomate.net
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 

Destaque (6)

Introduction2R
Introduction2RIntroduction2R
Introduction2R
 
BasicGraphsWithR
BasicGraphsWithRBasicGraphsWithR
BasicGraphsWithR
 
BasicLinux
BasicLinuxBasicLinux
BasicLinux
 
GoTermsAnalysisWithR
GoTermsAnalysisWithRGoTermsAnalysisWithR
GoTermsAnalysisWithR
 
Genome Assembly
Genome AssemblyGenome Assembly
Genome Assembly
 
RNAseq Analysis
RNAseq AnalysisRNAseq Analysis
RNAseq Analysis
 

Semelhante a PerlScripting

Semelhante a PerlScripting (20)

John's Top PECL Picks
John's Top PECL PicksJohn's Top PECL Picks
John's Top PECL Picks
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
The Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitThe Essential Perl Hacker's Toolkit
The Essential Perl Hacker's Toolkit
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013
 
Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshop
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Linux advanced privilege escalation
Linux advanced privilege escalationLinux advanced privilege escalation
Linux advanced privilege escalation
 
Introduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable PerlIntroduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable Perl
 
release_python_day2_slides_201606.pdf
release_python_day2_slides_201606.pdfrelease_python_day2_slides_201606.pdf
release_python_day2_slides_201606.pdf
 
20 ruby input output
20 ruby input output20 ruby input output
20 ruby input output
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentation
 
Bioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekingeBioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekinge
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 
Tips
TipsTips
Tips
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 

Último

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 

Último (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 

PerlScripting

  • 1. Writing Perl Scripts Boyce Thompson Institute for Plant Research Tower Road Ithaca, New York 14853-1801 U.S.A. by Aureliano Bombarely Gomez
  • 2. Writing Perl Scripts: 1. Four mandatory lines. 2. Useful modules I: Files. 3. Useful modules II: Options. 4. Documentation and being verbose. 5. Exercise: Assembly stats.
  • 3. Writing Perl Scripts: 1. Four mandatory lines. 2. Useful modules I: Files. 3. Useful modules II: Options. 4. Documentation and being verbose. 5. Exercise: Assembly stats.
  • 4. 1. Four mandatory lines. 1.LINE: #!/usr/bin/perl Where ? At the beginning of the script. Why ? It says to the operating system what program needs to use to executate the script . 2.LINE: use warnings; Where ? Before declare the modules and variables. (sooner is better). Why ? It will print any compilation warnings. 3.LINE: use strict; Where ? Before declare the modules and variables. (sooner is better). Why ? It will check any gramatical error and it will not Let run scripts with errors.
  • 5. 1. Four mandatory lines. 4.LINE: 1; Where ? At the end of the script. Why ? It says to the operating system that the script It is done. #!/usr/bin/perl use strict; use warnings; ########### ## MY CODE ########### 1;
  • 6. Writing Perl Scripts: 1. Four mandatory lines. 2. Useful modules I: Files. 3. Useful modules II: Options. 4. Documentation and being verbose. 5. Exercise: Assembly stats.
  • 7. 2. Useful modules I: Files JUST A REMINDER: How open/Read/Write/Close files. 1. OPEN FUNCTION. open (FILEHANDLE, MODE, REFERENCE); FILEHANDLES: undefined scalar variable autovivified. MODE: read, input only: < write, output only: > append to a file: >> read/write update access: +< write/read update access +> read/append update access +>> REFERENCE: Filename or reference to open
  • 8. 2. Useful modules I: Files JUST A REMINDER: How open/Read/Write/Close files. 1. OPEN FUNCTION. open (my $ifh, '<', $input_filename); open (my $ofh, '>', $output_filename); SUGGESTIONS: “use autodie” instead “OR die(“my error”)”; open (my $ifh, '<', $input_filename) OR die(“ERROR OPENING FILE: $!”);
  • 9. 2. Useful modules I: Files JUST A REMINDER: How open/Read/Write/Close files. 2. READING OPENED FILES. while(<FILEHANDLE>) { ## BLOCK USING $_ as LINE (don't forget chomp) } SUGGESTIONS: “Know the status of the file” my @filelines = <FILEHANDLE>; my $L = scalar(@filelines); my $l = 0; foreach my $line (@filelines) { $l++; print STDERR “Reading line $l of $L lines r”; }
  • 10. 2. Useful modules I: Files JUST A REMINDER: How open/Read/Write/Close files. 3. WRITE OVER OPENED FILES. print $ofh “This to print over the file”; 4. CLOSE FILES. close($ofh);
  • 11. 1. Useful modules I: Files a) File::Basename; Parse file paths into directory, filename and suffix. use File::Basename; my ($name, $path, $suffix) = fileparse($fullname,@suffixlist); my $name = fileparse($fullname, @suffixlist); my $basename = basename($fullname, @suffixlist); my $dirname = dirname($fullname);
  • 12. 2. Useful modules I: Files b) File::Spec; Operations over filenames. use File::Spec; my $currdir = File::Spec->currdir(); my $tempdir = File::Spec->tempdir(); my $path = File::Spec->catfile($currdir, $filename);
  • 13. Writing Perl Scripts: 1. Four mandatory lines. 2. Useful modules I: Files. 3. Useful modules II: Options. 4. Documentation and being verbose. 5. Exercise: Assembly stats.
  • 14. 3. Useful modules II: Options Usual way to pass options: Using $ARGV user@comp$ myscript.pl argument1 argument2 #!/usr/bin/perl use strict; use warnings; use autodie; my ($arg1, $arg2) = @ARGV; 1;
  • 15. 3. Useful modules II: Options Usual way to pass options: Using $ARGV PROBLEM: When there are multiple arguments can be confusing. Mandatory arguments are difficult to check !!! SOLUTION: Use modules GetOpt::Std or GetOpt::Long
  • 16. 3. Useful modules II: Options GetOpt::Std; Process single-character arguments from the command line user@comp$ myscript.pl -i argument1 -o argument2 -V -H use GetOpt::Std; our( $opt_i, $opt_o, $opt_V, $opt_H); getopts(i:o:VH); ## i: and o: expect something aftter the switch. my $input = $opt_i || die(“ERROR: -i <input> was not supplied.”); my $output = $opt_i || die(“ERROR: -o <output> was not supplied.”); ## V and H don't expect anything after the switch. if ($opt_H) { print $help; }
  • 17. Writing Perl Scripts: 1. Four mandatory lines. 2. Useful modules I: Files. 3. Useful modules II: Options. 4. Documentation and being verbose. 5. Exercise: Assembly stats.
  • 18. 4. Documentation and being verbose Three types of documentation: 1) Document code with #. GOOD: Useful for developers. BAD: Inaccessible for users if they not open the script. 2) Document using perldoc. GOOD: Clear and formated information. BAD: perdoc is not always installed in the system. 3) Document using an inside print function. GOOD: Frecuently easy to access. Intuitive. BAD: ??? Well increase the size of your script.
  • 19. 4. Documentation and being verbose Three types of documentation: 1) Document code with #. GOOD: Useful for developers. BAD: Inaccessible for users if they not open the script. 2) Document using perldoc. GOOD: Clear and formated information. BAD: perdoc is not always installed in the system. 3) Document using an inside print function. GOOD: Frecuently easy to access. Intuitive. BAD: ??? Well increase the size of your script.
  • 20. 4. Documentation and being verbose Documenting through a function; sub help { print STDERR <<EOF; $0: Description: My program description. Synopsis: myscript.pl [-H] [-V] -i <input> Arguments: -i <input> input file (mandatory) -H <help> print Help. -V <verbose> be verbose EOF; Exit(1); }
  • 21. 4. Documentation and being verbose Calling help; use GetOpt::Std; our( $opt_i, $opt_o, $opt_V, $opt_H); getopts(i:o:VH); ## i: and o: expect something aftter the switch. my $input = $opt_i || die(“ERROR: -i <input> was not supplied.”); my $output = $opt_i || die(“ERROR: -o <output> was not supplied.”); ## V and H don't expect anything after the switch. if ($opt_H) { help(); }
  • 22. 4. Documentation and being verbose Being verbose; use GetOpt::Std; our( $opt_i, $opt_o, $opt_V, $opt_H); getopts(i:o:VH); ## i: and o: expect something aftter the switch. my $input = $opt_i || die(“ERROR: -i <input> was not supplied.”); my $output = $opt_i || die(“ERROR: -o <output> was not supplied.”); if ($opt_V) { my $date = `date`; chomp($date); print STDERR “Step 1 [$date]:ntParsing -i $input file.n”; }
  • 23. 4. Documentation and being verbose Being verbose; my @filelines = <FILEHANDLE>; my $L = scalar(@filelines); my $l = 0; foreach my $line (@filelines) { $l++; if ($opt_V) { print STDERR “Reading line $l of $L lines r”; } }
  • 24. Writing Perl Scripts: 1. Four mandatory lines. 2. Useful modules I: Files. 3. Useful modules II: Options. 4. Documentation and being verbose. 5. Exercise: Assembly stats.
  • 25. 5. Exercise: Assembly Stats GOAL: Create a script to calculate: 1) Number of sequence in a file. 2) Total BP of a file. 3) Longest sequence 4) Shortest sequence. 5) Average and SD. 6) N25, N50, N75, N90, N95 (length and indexes)
  • 26. 5. Exercise: Assembly Stats 6) N25, N50, N75, N90, N95 (length and indexes) Just a reminder: N50 Length is the minimun length contained by the 50% of the size of the file (in bp) when it is ordered by decreasing length. N50 Index is the number os sequences contained by the 50% of the size of the file (in bp) when it is ordered by decreasing length.