MAY 2018 VOL 4 ISSUE 5
“I seem to have been only like a boy playing on the seashore, and diverting myself in now and then finding a smoother pebble or a prettier shell than ordinary, whilst the great ocean of truth lay all undiscovered before me.� -
Isaac Newton
How to execute Unix/shell commands in a Perl script?
Perl Programming
Public Service Ad sponsored by IQLBioinformatics
Contents
May 2018
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Topics Editorial....
03 Bioinformatics Programming How to execute Unix/shell commands in a Perl script? 07
05
FOUNDER TARIQ ABDULLAH EDITORIAL EXECUTIVE EDITOR TARIQ ABDULLAH FOUNDING EDITOR MUNIBA FAIZA SECTION EDITORS FOZAIL AHMAD ALTAF ABDUL KALAM MANISH KUMAR MISHRA SANJAY KUMAR NABAJIT DAS
REPRINTS AND PERMISSIONS You must have permission before reproducing any material from Bioinformatics Review. Send E-mail requests to info@bioinformaticsreview.com. Please include contact detail in your message. BACK ISSUE Bioinformatics Review back issues can be downloaded in digital format from bioinformaticsreview.com at $5 per issue. Back issue in print format cost $2 for India delivery and $11 for international delivery, subject to availability. Pre-payment is required CONTACT PHONE +91. 991 1942-428 / 852 7572-667 MAIL Editorial: 101 FF Main Road Zakir Nagar, Okhla New Delhi IN 110025 STAFF ADDRESS To contact any of the Bioinformatics Review staff member, simply format the address as firstname@bioinformaticsreview.com PUBLICATION INFORMATION Volume 1, Number 1, Bioinformatics Reviewâ„¢ is published monthly for one year (12 issues) by Social and Educational Welfare Association (SEWA)trust (Registered under Trust Act 1882). Copyright 2015 Sewa Trust. All rights reserved. Bioinformatics Review is a trademark of Idea Quotient Labs and used under license by SEWA trust. Published in India
Bioinformatics- A broad future ahead: Editorial
EDITORIAL
It has been a wonderful time since BiR came into existence. As we enter a new year, BiR tries to look forward towards the development and wonderful achievements and providing the best knowledge regarding bioinformatics. In the past two years, BiR has hit a long road from a few readers to several thousand.
Muniba Faiza
Founding Editor
Every complimentary and appreciation mail we get feels like an achievement for us. Bioinformatics has got a great future ahead of it with a better understanding and precise methodologies for both dry and the wet lab experimentations. In the last two years, BiR has advanced in many aspects. We have come up with an android app which helps our readers to stay connected with the latest updates, our articles have started to appear in Google Scholar, we get a lot of cherishing emails, and collaboration proposals. BiR is trying to broaden the horizons by covering different domains of bioinformatics. Since bioinformatics is multidisciplinary, to date, the team of BiR has tried to go through almost every aspect of it including big data, sequence analysis, structural bioinformatics, data mining, tools, software, biostatistics, and so on. This year BiR is more focused to provide a rich content to our readers and help to understand the concepts of bioinformatics more easily. The team of BiR is trying to reach to the students to encourage them for their career in bioinformatics and to the researchers currently working in the same area. The last internship at BiR was a great success and we got an amazing response from our interns. We are looking forward to presenting our work at school and college level to introduce this to the young minds who are more fascinated by the technology. We have such a long road to drive on which is not possible without the support of our readers, subscribers, and contributors. We are thankful to our readers wholeheartedly for their support and suggestions and wish them a very happy and prosperous new year
Letters and responses: info@bioinformaticsreview.com
with new hopes and great achievements. We would like to hear your thoughts and feedback about BiR, and what other kinds of articles you would like to read.
EDITORIAL
Please write us at info@bioinformaticsrevew.com
BIOINFORMATICS PROGRAMMING
How to execute Unix/shell commands in a Perl script? Image Credit: Stock Photos
“BioPerl is a collection of Perl modules which is used to write Perl scripts applied in bioinformatics [1].� ioPerl is a collection of Perl modules which is used to write Perl scripts applied in bioinformatics [1]. It is used in bioinformatics programming such as in developing source codes, standalone software/tools, and algorithms. It's easy to install and provide various modules which make it easier to execute different functions. However, Python is mostly preferred over the Perl language, still, some of the bioinformatics software is based on Perl such as the standalone version of I-TASSER. Sometimes, it's big trouble to execute some Unix/shell commands in Perl script for the beginners, it's difficult to decide which function would be specific to a condition. Therefore, this article is a guide for the execution of Unix/shell commands in Perl script.
B
Perl offers different functions and operators to execute external commands (described as follows),
which are special in their own ways. They are slightly different from each other, which sometimes makes hard to choose for the beginners in Perl.
command. It can be called in two ways:
1. exec""
OR
syntax: exec "command"; It is a Perl function (perlfunc) which executes a command but never returns, as same as the return statement in a function. It keeps continuing processing the script while executing the command and doesn't wait for it to finish first, it returns false when the command is not found but never returns true. 2. system() syntax: system( "command" ); It is also a Perl function (perlfunc) which executes a command and waits for it to get finished first and then resume the Perl script. The return value is the exit status of the
system( "command arg1 arg2" );
system( "command", "arg2" );
"arg1",
3. Backticks `` or qx// syntax: `command`; syntax: qx/command/; It is a Perl operator (perlop) very similar to system(), execute a command and then resumes the Perl script after the execution has finished but the return value is STDOUT of the command. 4. IPC::Open2 syntax: $output
= open2(\*CHLD_OUT, \*CHLD_IN, 'command arg1 arg2');
Bioinformatics Review | 7
It runs a process for both reading and writing and creates a pipe to both STDIN and STDOUT. It can be used in both ways:
OR without using the shell
$output = open2(my $out, my $in, 'command arg1 arg2');
a2p syntax: a2p
OR without using the shell
[options]
[awkscript]
$output = open2(my $out, my $in, 'command', 'arg1', 'arg2');
You can read about it more in the documentation: IPC::Open2. 5. IPC::Open3
There is a Perl utility known as a2p which translates awk command to Perl. It takes awk script as input and generates a comparable Perl script as the output. Suppose, you want to execute the following awk statement awk '{$1 = ""; print}' f1.txt
syntax: $output = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR, 'command arg2', 'optarg',...);
$output = open3(my $out, my $in, 'command', 'arg1', 'arg2');
arg1
It is very similar to IPC::Open2 with capability to capture all three file handles of the process, i.e., STDIN, STDOUT, and STDERR. It can also be used with or without the shell. You can read about it more in the documentation: IPC::Open3. $output = open3(my $out, my $in, 'command arg1 arg2');
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_09]+=)(.*)/ && shift; # process any FOO=bar switches $, = ' '; # set output field separator $\ = "\n"; # set output record separator
$2
=
"";
This statement gives error sometimes even after escaping the variables (\$1, \$2) but by using a2p it can be easily converted to Perl script: #!/usr/local/bin/perl eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}' if $running_under_some_shell; # this emulates #! processing on NIH machines. # (remove #! line above if indigestible)
while (<>) { chomp; # strip record separator @Fld = split(' ', $_, -1); if ($awk) { $Fld[(1)-1] = ''; $Fld[(2)-1] = ''; print join($,,@Fld); } print join($,,@Fld) if $f1 . . $txt; }
For further information, you can read it's documentation: a2p References 1.
Stajich, J. E.; Block, D.; Boulez, K.; Brenner, S.; Chervitz, S.; Dagdigian, C.; Fuellen, G.; Gilbert, J.; Korf, I.; Lapp, H.; Lehväslaiho, H.; Matsalla, C.; Mungall, C. J.; Osborne, B. I.; Pocock, M. R.; Schattner, P.; Senger, M.; Stein, L. D.; Stupka, E.; Wilkinson, M. D.; Birney, E. (2002). "The BioPerl Toolkit: Perl Modules for the Life Sciences". Genome Research. 12(10): 1611â&#x20AC;&#x201C;1618.
Bioinformatics Review | 8
Subscribe to Bioinformatics Review newsletter to get the latest post in your mailbox and never miss out on any of your favorite topics. Log on to https://www.bioinformaticsreview.com
Bioinformatics Review | 9
Bioinformatics Review | 10