This book is designed to be useful in theory and practical classes of UG and PG bioinformatics students. Moreover, it will be beneficial to other persons who wish to learn Perl programming language. To explain the Perl jargon, program based approach is used throughout the book. Important points are provided at the end of each chapter along with questions to test the skills. Solutions of find errors are provided at the end of the book. All the programs are tested on Windows 7 with Perl v5.18.1.
The book is divided into thirteen chapters which gradually take a reader from basic to advanced Perl. Although the book has been drafted with utmost care, it is quite possible that some errors/misprints might have crept in the book. I request the readers for their valuable suggestions and comments for further improvement of the book.
Contents
1. Introduction (Quick start)
2. Variables and Data Formatting
Scalar variable
New line
Tab
Uppercase
Lowercase
3. Arithmetic Operations and Standard Input
Addition
Subtraction
Multiply
Division
Remainder
Exponent
Autoincrement
Autodecrement
Add assign
Subtract assign
4. Arrays
Quote word
Push
Pop
Unshift
Shift
Splice
Reverse
Sort
Join
5. Associative Arrays
Keys
Values
Each
Delete
6. Comparison Operators and Choices
Equal to
Not equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
if
if-else
if-elsif-else
Boolean operators
AND
OR
NOT
7. Loops
For
While
Foreach
8. Regular Expressions
Match operator
Substitute operator
Translate operator
9. Functions for Strings
Index
Substr
Length
Reverse
Split
10. References
Dereferencing
Anonymous array
Anonymous associative array
Two dimensional array
11. Subroutines
Arguments
Scalar as an argument
Array as an argument
Associative array as an argument
Return
12. File and Directory Handling
Read
Write
Append
Open
File handle
Opendir
Directory handle
Readdir
Close
13. Modules and Packages
Reusable code
Namespace
About the book
This book is designed to be useful in theory and practical classes of UG and PG bioinformatics students. Moreover, it will be beneficial to other persons who wish to learn Perl programming language. To explain the Perl jargon, program based approach is used throughout the book. Important points are provided at the end of each chapter along with questions to test the skills. Solutions of find errors are provided at the end of the book. All the programs are tested on Windows 7 with Perl v5.18.1.
The book is divided into thirteen chapters which gradually take a reader from basic to advanced Perl. Although the book has been drafted with utmost care, it is quite possible that some errors/misprints might have crept in the book. I request the readers for their valuable suggestions and comments for further improvement of the book.
Acknowledgements
To my teachers, students, family, friends, and The Architect of Universe.
Preface
The interdisciplinary field of bioinformatics establishes itself as an indispensible branch of science. It is impossible to conduct the high throughput biological studies without the involvement of bioinformatics. Even it is difficult to imagine large scale biological projects which do not use any bioinformatics tool. To fulfill this requirement bioinformaticians have developed several tools that can help in the study of genomics, proteomics, metabolomics and other areas of biological sciences. Apart from this a large number of biological databases have also been developed which are sometimes a byproduct of high throughput research. Programming languages play a very important role in these developments.
Practical Extraction and Report Language (PERL) proved to be one of the most useful programming languages in such projects including the human genome sequencing project. The pattern matching and parsing capabilities of Perl suits the tasks required in biological science research which makes it a choice of bio-programmers. Researchers and academicians are extensively using Perl in devising programming solutions for problems related to biological data. Ever since Perl is one of the most important programming languages for a bioinformatician, therefore, throughout the country Perl is included as a core component in the syllabi of UG and PG courses in bioinformatics. During my teaching experience I found that students of diverse backgrounds opt for bioinformatics courses. Among them the students with biological background face lots of difficulty in learning programming languages. This prompted me to undertake this exciting, challenging project and put my several years of teaching and research experience in it. Perl for Biologists: Solutions for Beginners has been written keeping in view the requirements of Perl readers and incorporating the syllabi of the UG and PG bioinformatics courses of various Indian universities. I tried to express Perl in an easy, understandable language through lots of programming solutions which can be comfortably grasped by the learners. The layout of the book ensures that the readers can easily understand the given concepts without having prior knowledge of programming languages. I hope this book comes out as a useful publication. Enjoy Programming!!!
List of Perl Programs
Chapter 1 Introduction (Quick start)
Program 1.1 Test program
Chapter 2 Variables and Data Formatting
Program 2.1 Program to check case sensitivity of variables name
Program 2.2 Program to show interpolation
Program 2.3 Program to assign values to scalar variable
Program 2.4 Program to use \n and \t
Program 2.5 Program to convert characters in uppercase
Program 2.6 Program to convert characters in lowercase
Chapter 3 Arithmetic Operations and Standard Input
Program 3.1 Program to add numbers
Program 3.2 Program to subtract numbers
Program 3.3 Program to multiply numbers
Program 3.4 Program for division
Program 3.5 Program to get remainder of division
Program 3.6 Program to get exponent
Program 3.7 Program to autoincrement a number
Program 3.8 Program to autodecrement a number
Program 3.9 Program for addition and assignment
Program 3.10 Program for subtraction and assignment
Program 3.11 Program to get input using keyboard
Program 3.12 Program to get input using keyboard in interactive manner
Program 3.13 Program to get input using keyboard and display the entered value
Program 3.14 Program to get input using keyboard and check hidden new line character
Program 3.15 Program to get input using keyboard and remove new line character
Program 3.16 Program to get strings from keyboard
Program 3.17 Program to get strings from keyboard and remove new line using chop
Program 3.18 Program to check the effect of chomp on a string
Program 3.19 Program to check the effect of chop on a string
Chapter 4 Arrays
Program 4.1 Program to create an array of strings
Program 4.2 Program to create an array of numbers
Program 4.3 Program to create an array of strings and numbers
Program 4.4 Program to assign array using quote word function
Program 4.5 Program to use quote word function in interpolation mode
Program 4.6 Program to assign array using scalar variables
Program 4.7 Program to assign one array to another array
Program 4.8 Program to merge multiple array into one array
Program 4.9 Program to assign values to specific index of an array
Program 4.10 Program to check the size of array
Program 4.11 Program to empty an array
Program 4.12 Program to check the size of array without scalar function
Program 4.13 Program to check the last index of array
Program 4.14 Program to check the size of array using last index
Program 4.15 Program to check the last index of array using array size
Program 4.16 Program to print value stored in last index of array
Program 4.17 Program to add array element at the end
Program 4.18 Program to remove array element from end
Program 4.19 Program to remove array element from end and print the value
Program 4.20 Program to add array element in the beginning
Program 4.21 Program to remove array element from beginning
Program 4.22 Program to remove array element from beginning and print the value
Program 4.23 Program to remove array element from beginning and add it at the end
Program 4.24 Program to check the size of array after array manipulation
Program 4.25 Program to check the last index of array after array manipulation
Program 4.26 Program to insert elements in-between array indexes by replacing other elements
Program 4.27 Program to insert elements in-between array indexes without replacing other elements
Program 4.28 Program to remove element from in-between array indexes without adding new elements
Program 4.29 Program to empty an array from specific index
Program 4.30 Program to empty an array from specific index and check array size
Program 4.31 Program to reverse array
Program 4.32 Program to sort array
Program 4.33 Program to join array elements
Program 4.34 Program to check the effect of chop on array
Chapter 5 Associative Arrays
Program 5.1 Program to create associative array
Program 5.2 Program to check effect of double quotes on associative array
Program 5.3 Program to assign value to a specific key
Program 5.4 Program to print value of a key
Program 5.5 Program to retrieve keys
Program 5.6 Program to retrieve values
Program 5.7 Program to empty associative array
Program 5.8 Program to undefine a value associated with a key
Program 5.9 Program to delete a key-value pair
Program 5.10 Program to check the effect of chop on associative array
Chapter 6 Comparison Operators and Choices
Program 6.1 Program to check the equality of two numbers
Program 6.2 Program to check the non-equality of two numbers
Program 6.3 Program to check which number is greater
Program 6.4 Program to check whether number is greater than or equal
Program 6.5 Program to check which number is lesser
Program 6.6 Program to check whether number is less than or equal
Program 6.7 Program to check the logical error
Program 6.8 Program to use if-else
Program 6.9 Program to use if-elsif-else
Program 6.10 Program to check equality of strings
Program 6.11 Program to check non-equality of strings
Program 6.12 Program to check which string is greater
Program 6.13 Program to check whether string is greater or equal
Program 6.14 Program to check which string is lesser
Program 6.15 Program to check whether string is lesser or equal
Program 6.16 Program to check whether a key in associative array contains a defined value
Program 6.17 Program to check defined value of a key
Program 6.18 Program to check whether key a exists in associative array
Program 6.19 Program to check whether a key exists in associative arrays using nested if
Program 6.20 Program to check whether a key exists in associative arrays using AND operator
Program 6.21 Program to check whether a key exists in any one of two associative arrays using OR operator
Program 6.22 Program to use NOT operator
Chapter 7 Loops
Program 7.1 Program to print a series of numbers from 0-9 using for loop
Program 7.2 Program to print a series of numbers from 0-9 using while loop
Program 7.3 Program to print elements of array using foreach loop
Program 7.4 Program to print elements of array using for loop
Program 7.5 Program to print elements of array using while loop
Program 7.6 Program to print each key-value pair of associative array in new line using foreach
Program 7.7 Program to print each key-value pair of associative array in new line using while loop
Program 7.8 Program to print values of associative array in new line using for loop
Program 7.9 Program to print key-value pair of associative array using each function
Program 7.10 Program to skip iteration of loop using next
Program 7.11 Program to terminate the loop using last
Chapter 8 Regular Expressions
Program 8.1 Program to find a pattern using match operator
Program 8.2 Program to find a pattern
Program 8.3 Program to find a pattern and count number of occurrences
Program 8.4 Program to perform case sensitive search of a pattern
Program 8.5 Program to perform case insensitive search of a pattern
Program 8.6 Program to substitute a pattern using substitute operator
Program 8.7 Program to substitute all occurrences of a pattern using substitute operator
Program 8.8 Program to substitute pattern in case insensitive mode
Program 8.9 Program to generate complementary sequence using tr operator
Program 8.10 Program to replace ambiguous characters from DNA sequence
Program 8.11 Program to count characters in a sequence
Program 8.12 Program to count characters in a sequence and replace them
Program 8.13 Program to replace ambiguous characters from DNA sequence and count them
Program 8.14 Program to replace ambiguous characters from DNA sequence and squeeze the gaps
Program 8.15 Program to replace ambiguous characters from DNA sequence and delete the gaps
Program 8.16 Program to find patterns like GAA, GTA, GCA, GGA
Program 8.17 Program to find patterns with specific choices
Program 8.18 Program to find sequence accession number in fasta comment line
Program 8.19 Program to find sequence accession number using quantifiers
Program 8.20
Program to use backslash escaped characters to find sequence accession number
Program 8.21
Program to find a pattern using quantifier symbol ?
Program 8.22
Program to find a pattern 0 or 1 times
Program 8.23
Program to find a pattern using quantifier symbol +
Program 8.24
Program to find a pattern 1 or more times
Program 8.25
Program to find a pattern using quantifier symbol *
Program 8.26
Program to find a pattern 0 or more times
Program 8.27
Program to find a pattern exactly 3 times
Program 8.28
Program to find a pattern exactly 3 or more times
Program 8.29
Program to find a pattern with minimum 3 and maximum 5 occurrences
Chapter 9 Functions for Strings
Program 9.1
Program to check length of a string
Program 9.2
Program to reverse a string
Program 9.3
Program to break a string from desired character
Program 9.4
Program to break a string from individual characters
Program 9.5
Program to find the index of a substring
Program 9.6
Program to find all positions of a substring
Program 9.7
Program to retrieve a substring from a string
Chapter 10 References
Program 10.1
Program to create reference to scalar variable
Program 10.2
Program to dereference a reference
Program 10.3
Program to create reference to array and dereference it
Program 10.4
Program to retrieve value of an array index using reference
Program 10.5
Program to create anonymous array
Program 10.6
Program to create reference to associative array and dereference it
Program 10.7
Program to retrieve value associated with a key using reference
Program 10.8
Program to create anonymous associative array
Program 10.9
Program to create a reference of reference
Program 10.10
Program to check type of referent
Program 10.11
Program to create two dimensional array
Chapter 11 Subroutines
Program 11.1
Program to add two numbers using subroutine
Program 11.2
Program to add two numbers using subroutine and return the value
Program 11.3
Program to pass array to subroutine
Program 11.4
Program to pass two arrays to subroutine
Program 11.5
Program to pass two arrays to subroutine as reference
Program 11.6
Program to pass associative array to subroutine
Program 11.7
Program to pass two associative arrays to subroutine
Program 11.8
Program to pass two associative arrays to subroutine as reference
Chapter 12 File and Directory Handling
Program 12.1
Program to open a file and read the contents
Program 12.2
Program to open a file and read the contents line by line
Program 12.3
Program to open a file stored in another directory
Program 12.4
Program to open a file and write its contents in new file
Program 12.5
Program to open a file and write its contents in existing file
Program 12.6
Program to open a directory and get the file names
Program 12.7
Program to open a directory and get the relevant file names
Program 12.8
Program to open a directory and read the contents of all files one by one
Chapter 13 Modules and Packages
Program 13.1
Program to generate complementary DNA
Program 13.2
Program to generate complementary DNA using module stored in a directory
Program 13.3
Program to generate complementary DNA and RNA sequence using same subroutine name
Advice Before You Start
- Have patience.
- Carefully read the warnings and error messages.
Chapter 1
Introduction (Quick start)
- PERL is an acronym of Practical Extraction and Report Language developed by Larry Wall in 1987.
- Perl is freely available as a part of various operating system distributions (Linux, Mac etc.) and can be easily installed on Windows OS.
- To check the version of Perl installed on your system type perl –v followed by enter on command prompt (Start à Accessories à Command Prompt) and it will show you the installed version, if any.
Abbildung in dieser Leseprobe nicht enthalten.
- It is a case-sensitive, interpreter based, procedural programming language but can also be used to write programs in object oriented programming paradigm.
- Perl works with the motto that "There's more than one way to do it."
- To start with Perl open a text editor (Notepad in Windows and vim in Linux) and type following lines
print "Hello there";
save the file with filename “first_ program.pl”. To execute this Perl program type perl first_ program.pl on command prompt
Abbildung in dieser Leseprobe nicht enthalten.
- It is good to write comments in your program. A # sign is used to write comment in Perl and interpreter skips the text written after this sign.
- In this program the print function has been used to display the string (Hello there) on standard output (monitor).
- Almost all statements in Perl end with a semicolon (;).
- Windows users take care if Perl is not in the system path then save the program in Perl/bin folder where the interpreter will be present. Perl folder will be present in the directory where Perl has been installed.
- It is a good practice to save Perl programs with .pl extension and the program name must be related to the problem for which program is written. For example, substring.pl is a suitable name for a program that can be used to retrieve a substring from a string.
Points to remember
- Usually beginners do mistake by not remembering the location of program where it is saved. When program has been executed from any other location where it is not stored, an error message similar to “Can't open Perl script "ProgramName.pl": No such file or directory” appears on the command prompt.
- Always remember the location of the stored program. After opening the command prompt navigate to the same location where the program is stored then execute it.
- Type the program name correctly. Incorrect name leads to error message.
- Check whether Perl is present in system path. If not, ask system administrator to include it in the system path, otherwise save the program in Perl/bin folder and execute.
Test Yourself
1. Write a program to print your name.
2. Write a program to print your class.
3. Write a program to print the name of your institute.
4. Write a program to print your age.
[...]
-
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X.