4.1 Reading and writing files. 4.2 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle,

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Introduction to C Programming
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
4ex.1 More loops. 4ex.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) {
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
5.1 Previously on... PERL course (let ’ s practice some more loops)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.
4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Introduction to C Programming
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Random Bits of Perl None of this stuff is worthy of it’s own lecture, but it’s all a bunch of things you should learn to use Perl well.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Files Tutor: You will need ….
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Input from STDIN STDIN, standard input, comes from the keyboard.
CIRC Summer School 2017 Baowei Liu
Distributed Computing Systems
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
Module 6 Working with Files and Directories
Presentation transcript:

4.1 Reading and writing files

4.2 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle, exactly like you would from : my $line = ; = ; foreach $line Every filehandle opened should be closed: close(IN); Always check the open didn’t fail (e.g. if a file by that name doesn’t exists): open(IN, "<$file") or die "can't open file $file"; Reading files

4.3 Open a file for writing, and link it to a filehandle: open(OUT, ">EHD.analysis") or die... NOTE: If a file by that name already exists it will be overwriten! Or, you can add lines at the end of an existing file (append): open(OUT, ">>EHD.analysis") or die... Print to a file: print OUT "The mutation is in exon $exonNumber\n"; Writing to files no comma here

4.4 You can ask questions about a file or a directory name (not filehandle): if (-e $name) { print "The file $name exists!\n"; } -e$name exists -r$name is readable -w$name is writable by you -z$name has zero size -s$name has non-zero size (returns size) -f$name is a file -d$name is a directory -l$name is a symbolic link -T$name is a text file -B$name is a binary file (opposite of -T). File Test Operators

4.5 open(IN, '<D:\workspace\Perl\p53.fasta'); Always use a full path name, it is safer and clearer to read Remember to use \\ in double quotes open(IN, "<D:\\workspace\\Perl\\$name.fasta"); (usually) you can also use / open(IN, "<D:/workspace/Perl/$name.fasta"); Working with paths

4.6 It is common to give parameters within the command-line for a program or a script: They will be stored in the contains: ("my","argument","list"); foreach my $arg print "$arg\n"; } Command line parameters > perl -w findProtein.pl my argument list my argument list

4.7 It is common to give parameters within the command-line for a program or a script: They will be stored in the contains: ("my argument list"); foreach my $arg print "$arg\n"; } > perl -w findProtein.pl "my argument list" Command line parameters my argument list

4.8 It is common to give parameters within the command-line for a program or a script: They will be stored in the : my $inFile = $ARGV[0]; my $outFile = $ARGV[1]; Or more simply: my ($inFile,$outFile) Command line parameters > perl -w findProtein.pl D:\perl\input.fasta D:\perl\output.txt

4.9 Command line parameters in PerlExpress

4.10 Reminder: the class exercise of 3 days ago. Reading files - example Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

4.11 Reading files: example $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(" ", $line); $name = = split(",", $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print "$name $sum\n"; # Read next line $line = ; chomp $line; } Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

4.12 Reading files: example my ($inFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(" ", $line); $name = = split(",", $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

4.13 Reading files: example my ($inFileName, $outFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; open(OUT, ">$outFileName") or die "can't open $outFileName"; $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(" ", $line); $name = = split(",", $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print OUT "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); close(OUT); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

4.14 Reading files: example my ($inFileName, $outFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; open(OUT, ">$outFileName") or die "can't open $outFileName"; $line = ; chomp $line; # loop processes one input line and print output for line while (defined $line) { # Separate name and = split(" ", $line); $name = = split(",", $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print OUT "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); close(OUT); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 Output: Yossi 27.6 Dana 27 Refael 45.1

4.15 Reading files: example my ($inFileName, $outFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; open(OUT, ">$outFileName") or die "can't open $outFileName"; $line = ; # loop processes one input line and print output for line while (defined $line) { chomp $line; # Separate name and = split(" ", $line); $name = = split(",", $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print OUT "$name $sum\n"; # Read next line $line = ; } close(IN); close(OUT); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 Output: Yossi 27.6 Dana 27 Refael 45.1

4.16 Class exercise 5 1.Write a script that reads a file containing a Perl script, that is named by the first command-line parameter Print out the script without comment lines (lines that begin with #). 2.Now write the results to a file that is named by the second command- line parameter. 3.Now remove all other comments as well (that may not start at the beginning of a line).

4.17 Perl allows easy access to the files in a directory by “globbing”: The * represents any string character. For example, *.fasta represents all filenames with the extension ".fasta" = ; foreach $fileName { open(IN, $fileName) or die "can't open file $fileName"; foreach $line ( ) { do something... } } Note: the “glob” gives a list of the file names in the directory. Reading directories no " here

4.18 You can interpolate variables in the glob, as in double-quoted = ; If $chromosome is 4 then we may get these files chr4.fasta chr4_NT_ fasta chr4_NT_ fasta Reading directories

4.19 Delete a file: unlink ("fred.txt") or die "can't delete fred.txt"; Delete all files in a directory whose name matches a certain “pattern”: unlink or die "can't delete files in fred"; (Here – all file names that end with “.txt”) Move/rename files: rename ("fred.txt", "friends\\bob.txt") or die "can't move fred.txt"; Manipulating files

4.20 Generally, you can execute any command of the operating system: $systemReturn = system("delete fred.txt"); Or: $systemReturn = system("copy fred.txt george.txt"); When checking the value returned by a system call, usually 0 means no errors: if ($systemReturn != 0) { die "can't copy fred.txt"; } Calling system commands

4.21 Class exercise 6 1.Write a script that prints a list of all Perl files (i.e. files with extension “.pl ” ) in a given directory, that is named by the first command-line parameter. 2.Change the script from class exercise 5.1 so that it will read all Perl files in a given directory, that is named by the first command-line parameter, and print them out to the screen without the comment lines. 3* Change the script so that each script will be written to a file named as the input file with an added extension “.noComments” e.g. input “class_ex.2.2.pl”  output “class_ex.2.2.pl.noComments”