Quieting outputs from system calls in Perl
1
1
Entering edit mode
8.5 years ago

I wrote a perl script as a wrapper for a couple of calls to binaries. One of which is a call to the EMBOSS merger as such:

$mergeCMD = "merger -asequence temp1.fa -bsequence temp2.fa -outfile temp.me -outseq temp.fa;
system($mergeCMD);

It insists on printing "Merge two overlapping sequences" for every call which annoys me to no end and clutters up my terminal. There are no evident cmd line options to turn this off, so i was thinking it could be possible to redirect this output away from stdout? Better yet would of course be if someone knew an option to make merger quiet.

Perl Merger EMBOSS • 2.8k views
ADD COMMENT
2
Entering edit mode

This is not really a bioinformatics question, but have you tried: your.command 2>/dev/null?

ADD REPLY
1
Entering edit mode

I disagree with this approach and not just because there is a command-line flag to turn off the prompts. You are suppressing errors from the command which makes debugging impossible. If a command fails, which is inevitable, you will never know why. I would favor screening the message rather than throwing away all messages (if there wasn't an option for that). I know that approach is a standard convention but it is a source of a lot of wasted time in my experience.

ADD REPLY
1
Entering edit mode

You are rigth, in general it is better to deal with the output, an easy way would obviously be your.command > command.log 2>&1 to gather all the ouput in one file, another way, if the output and stderr needs to be processed by the perl script is to use IPC::Open3.

Normally, I run all my long running scripts through nohup, so I don't bother about any cluttered terminal and I normally don't bother silencing any external programs.

ADD REPLY
0
Entering edit mode

I have used the IPC::Open* modules before but I have gone the route of Capture::Tiny and IPCSS for ease of use. For me, those IPC::Open modules are still too complicated to get right. You have to do some bit shifting to get the return value and you have to use globs for sending output to. That is a lot to think about and it doesn't always work (at least for me). I like the explicit nature of the code with those I mentioned because you can run a command and simply return stdout/stderr or use tee to capture the output and let it pass through to the terminal.

ADD REPLY
3
Entering edit mode
8.5 years ago
SES 8.6k

You can add -auto to your command to turn off prompts. That is an EMBOSS standard.

The above is the right solution, but for reference, there are correct ways to capture the output of commands with Perl. Capture::Tiny is lightweight does a nice job and IPC::System::Simple is a drop-in replacement for system that works better than the built-in (and avoids the shell). On that note, it is better to give a list to system to avoid the shell and it is good to test if the command succeeded.

You can test a command by looking at the return value, but that is unfortunately not as simple as it might seem (e.g., you have to localize errors and test if an error is coming from Perl or the external command). Those are the reasons I mention these modules. They do the right thing without a lot of boilerplate or cryptic code. If you want to localize and catch the exceptions you can use Try::Tiny. All that is just for your consideration to make life a little easier. The command will work fine as written but it is good to keep these things in mind if you are building a pipeline. It is worth mentioning that there are wrappers for EMBOSS in BioPerl, so you can do something like the following (untested).

use strict;
use warnings;
use Bio::Factory::EMBOSS;

my $usage = "perl $0 a.seq b.seq";
my $aseq  = shift or die $usage;
my $bseq  = shift or die $usage;

my $factory = Bio::Factory::EMBOSS->new;
my $merger  = $factory->program('merger'); 

$merger->run({-asequence => $bseq,
              -bsequence => $aseq,
              -outseq    => 'out.seq',
              -outfile   => 'out.merged'});
ADD COMMENT
1
Entering edit mode

+1 for the Bioperl wrapper, this might be the best solution in this case.

ADD REPLY
0
Entering edit mode

It does avoid all the complications of trying to do the command yourself, and I think that you can return an object to work with.

ADD REPLY

Login before adding your answer.

Traffic: 1235 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6