Eldan Aranye has asked for the wisdom of the Perl Monks concerning the following question:

Hi all! I'm executing a samtools command via Perl and it gives me this diagnostic message:

samopen SAM header is present: 66338 sequences.

Now, I need to incorporate the Perl script in Galaxy, but because this diagnostic message gets interpreted as an error by Galaxy, I cannot proceed to the next command. Is there a way that I could trap the diagnostic message given by samtools in my Perl script and redirect it to STDOUT? Below is my script:

open INP, "< $ARGV[0]" or die "Cannot read file: $!"; open OUT, "> $ARGV[1]" or die "Cannot open file: $!"; @out = `/home/applications/samtools-0.1.7a/samtools view -bS $ARGV[0]` +; print OUT @out; close INP; close OUT;

If there's something wrong with the construct of my Perl code, do please tell me, too. Thanks! Diana

Replies are listed 'Best First'.
Re: Trapping diagnostic messages
by 2teez (Vicar) on Apr 25, 2012 at 04:40 UTC

    You might try to change the backtick you used here

    @out = `/home/applications/samtools-0.1.7a/samtools view -bS $ARGV[0]`
    to using system("...") instead.
    check perldoc -q "backtick", it could be of help.

      Thanks for the reply!
Re: Trapping diagnostic messages
by Mr. Muskrat (Canon) on Apr 25, 2012 at 18:13 UTC

    If my Google searches results are correct, the "SAM header is present" line comes out on STDOUT. The backticks will capture STDOUT by default but with some redirection, we can make it only capture STDERR where real errors should show up. See http://perldoc.perl.org/perlop.html#`STRING` for more details.

    # discard STDOUT but capture STDERR in @out my @out = `/home/applications/samtools-0.1.7a/samtools view -bS $ARGV[ +0] 2>&1 1>/dev/null`; print OUT @out;

    If you really do want the "SAM header is present" line to come out on STDOUT then you could do something like this.

    # redirect STDOUT to stdout.log and capture STDERR in @out my @out = `/home/applications/samtools-0.1.7a/samtools view -bS $ARGV[ +0] 2>&1 1>stdout.log`; print OUT @out; my $stdout = do { local( @ARGV, $/ ) = 'stdout.log'; <> }; # slurp the + contents of stdout.log into $stdout print $stdout;
      Thank you for your reply! Actually, I tried adding 2>&1 to the command yesterday and it worked. It captured the STDOUT, thus preventing Galaxy from interpreting it as an error. Thanks again!