how to test if BLAST installed in perl script?
2
0
Entering edit mode
10.0 years ago
biolab ★ 1.4k

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;
}
perl blast • 2.9k views
ADD COMMENT
0
Entering edit mode
10.0 years ago
Siva ★ 1.9k

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 COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

It's UNIX redirection. < is used to denote input (a file to replace STDIN), 1> (or just >) 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 REPLY
0
Entering edit mode

Thanks, RamRS, your comment is very helpful.

ADD REPLY
0
Entering edit mode

You're most welcome :)

ADD REPLY
0
Entering edit mode
10.0 years ago

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 COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 2041 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