Question: how to test if BLAST installed in perl script?
0
gravatar for biolab
4.2 years ago by
biolab1.1k
biolab1.1k wrote:

Hi, eveyone,

In a perl script, I want to test if blast was installed using the following lines. However on a computer without BLAST, there is no error message shown. What's wrong with the script?  Thank you very much!

 

check_blast();
sub check_blast {
    (open (my $BLAST, "blastn -h |")) || print STDERR "ERROR: blast not installed";
    close $BLAST;
}
blast perl • 1.4k views
ADD COMMENTlink modified 4.2 years ago by Jean-Karim Heriche18k • written 4.2 years ago by biolab1.1k
0
gravatar for Siva
4.2 years ago by
Siva1.6k
United States
Siva1.6k wrote:

Do you mean you need  the error message from the Shell like this?

sh: blastn: command not found

It seems open() only reads the STDOUT of your command not the STDERR as explained here:

How can I capture STDERR from an external command?

If you want the error message, may be you can redirect it to a file like this:

 (open (my $BLAST, "blastn -h 2>error.txt |"));

 

ADD COMMENTlink written 4.2 years ago by Siva1.6k

Thanks a lot, Siva, your comment is helpful. Can I ask you one more thing: why you add the number 2 after blastn -h? what's the number mean? THANKS!

ADD REPLYlink modified 4.2 years ago • written 4.2 years ago by biolab1.1k

It's UNIX redirection. '<' is used to denote input (a file to replace STDIN), '1>' denotes output (a file to replace STDOUT) and '2>' denotes error output (a file to replace STDERR).

Here, Siva asks the shell to run the command blastn -h and redirect any error messages to a file called error.txt.

ADD REPLYlink written 4.2 years ago by RamRS20k

Thanks, RamRS, your comment is very helpful.

ADD REPLYlink written 4.2 years ago by biolab1.1k

You're most welcome :)

ADD REPLYlink written 4.2 years ago by RamRS20k
0
gravatar for Jean-Karim Heriche
4.2 years ago by
EMBL Heidelberg, Germany
Jean-Karim Heriche18k wrote:

I tend to check availability of an external program with

$path_to_blastn = qx(which blastn);
unless ($path_to_blastn) { die "\nERROR: blastn not found"; }  

I think Siva's suggestion should be (note the & after 2)

blastn -h 2&>error.txt

The problem I have with it is that you then have to parse the file and decide whether what's inside  is an error message and whether it indicates that the program is not installed. Also relying on shell redirection is less portable than relying on 'which' (for example I think 2&> doesn't work with tcsh).

As an alternative you can also use system:

system("blastn -h ") == 0 or die ... 

but then you need to process the returned error code yourself.

Finally, if you still want to go with open, check IPC::Open2 and IPC::Open3. Open3 is, I believe, the only way of getting STDERR without involving the shell.

ADD COMMENTlink written 4.2 years ago by Jean-Karim Heriche18k

Hi Jean-Karim, your detailed answer is very helpful for my work. Thanks a lot!

ADD REPLYlink written 4.2 years ago by biolab1.1k

I don't understand what the purpose of & here is, especially before the redirection operator >. Is it not & used when you want to duplicate the file descriptor? For example,

>file 2>&1 

which redirects both STDERR and STDOUT to the same file.

I agree that outputing the STDERR to a file and parsing it is not the best way. There seems to be better ways to do this.

How to check if a program exists from a bash script?

 

ADD REPLYlink written 4.2 years ago by Siva1.6k

That's from a bash script. OP is looking for a Perl option. system() call with a $? check seems to be the way to go here.

ADD REPLYlink written 4.2 years ago by RamRS20k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2005 users visited in the last hour