170 likes | 247 Views
Learn the fundamentals of Perl programming for bioinformatics through variables, strings, and file operations. Understand the importance of strict usage and explore basic input and output methods. Discover how Perl is ideal for bioinformatics tasks.
E N D
Bioinformatics Introduction to Perl
Introduction • What is Perl • Basic concepts in Perl syntax: • variables, strings, • Use of strict (explicit variables) and warnings • Basic input and output • Read and writing to a File
Computer programming • This set of lectures will use the Active perl and notepad++ both of which are free. • Be aware: Some of the examples used in the perl lectures can be found in the accompanying zip file. • Perl is a scripting interpreter language that has: • good string handling functionality; • pattern matching functionality; • other features, which we will cover later, • This makes it suitable for bioinformatics
Variables: $variable_name • In perl to declare a variable use: • $variable_name • The $variable_name [memory location] can be assigned any value • There is no distinction between basic data types; the following are both valid: • $variable_name = “my name is Denis”; ( = is the assign operator) • $variable_name = 5.1;
“explicit” variable • The $variable is referred to as a global variable and it may lead to problems; • It is better to use an “explicit” variable using my $variablealong with use strict;and use warnings. • Consider the following examples:
Not using Strict and warnings • Example 1: • #!/usr/bin/perl • # not using strict and warnings • my $variable = 5; • print "the value of the variable is: ",$varaible;
Using strict with semantic error • Example 1: • #!/usr/bin/perl • # using strict and my $variables • use strict; • use warnings; • my $variable = 5; • print "the value of the variable is: ",$varaible;
Using Strict and no “semantic” error • Example 3: (Write and Run Program) • #!/usr/bin/perl • # using strict and my $variables • use strict; • use warnings; • my $variable = 5; • print "the value of the variable is: ",$variable;
Variables: $variable_name • The “value” associated with the variable can be assigned new values • $variable_name = 3.7; or $variable_name = “I like … “; • If the value of the variable is a number Arithmetic operators can be applied: • +, • -, • *, • / , • **( exponential or “to the power” ); • % modulus (the remainder after dividing; 3%2 is 1)
Arithmetic example Code output • #!/usr/bin/perl • #evaluating expressions in print (# comment line symbol) • $x = 15; • $y = 8; • print “the value of x is “, $x , “\n”; • print “the new value of x is “, $x + 3, “\n”; • print “the sum of x and y is “, $x + $y, “\n”; • print “the product of x and y is “, $x*$y, “\n”; • (ArithmeticExample.pl)
Format output • Double v single quotation marks • Sometimes we want to print a “ quotation“ so in perl it is done using ‘ ‘: • Print ’ ”the end justifies the means “ \n’; • More on output (refer to Output_Example.pl): • #!/usr/bin/perl • use strict; • use warnings • # formating output example • print ’ ”The secret to happiness is not doing what one likes to do but liking what one has to do “ \n’; • my $x = "I am from Cork "; #declare a string • my $y = "my name is Denis"; #declare another string • print "the value of \$x is $x\n"; # note the \$x • print "the value of $x is $x\n"; • print "the value of \$y concatenated to \$x is $y$x\n";
Inputting Data (assign data to a variable) • Data can be typed in from the keyboard, read from a file or “hardcoded” to the end of the program • Perl is good for reading text files – such as the Bioinfomatics “FASTA” file. • Reads input one line at a time inclusive of carriage return/end of line marker • Read data from the key board • #!/usr/bin/perl : This line must be at the start of each program. • use strict; use warnings; • $var = <> ; #(input a line of characters and assign it to $var) • chomp $var # removes the return character from the line. • Alternatively you can combine both statements together • chomp($var = <>);
Read data from a file (redirect input) • Normally you open a file and read/write to the file. However perl can put the name of the file as one of the “arguments” in the command line; let us consider a file called text1.txt • Perl code: • $variable = <> (same as that for inputting from the keyboard) • At the Command line type • C:\directory> Input.pl text1.txt
Exercise 1 • Declare 3 ($x, $y and $z) variables and assign them the following values: 12, 6, 8. • Perform and print the results of the following arithmetic operations • $x to the power of $y • $x modulus % $y • $x modulus $z • &x plus $y plus $z and assign it to a new variable ($a) • Perform one other simple arithmetic operation of your choice
Exercise 2 • Ask user to input a line of text from the keyboard and assign it to a variable then display the “value”. • Run the program • Re run the program but this time the input must come from a text file.
Useful link • http://www.perl.org/books/beginning-perl/ • perl tutorial book • http://www.webbasedprogramming.com/Perl-Quick-Reference/ch3.htm • This link covers all languages including perl