rogerjms / bedtools

Automatically exported from code.google.com/p/bedtools
0 stars 0 forks source link

bamToBed cannot read from stdin on Solaris #74

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I encountered an error when trying to open a BAM stream on stdin with bamToBed 
from bedtools 2.12.0 on Solaris. I dumped part of the BAM stream to a file and 
succeeded in opening the file using "bamToBed -i", but was unable to open the 
same BAM file when read from stdin:

########################################
$ bamToBed -i test.bam | head -n 1
Gm19    38182344        38182807        F1RQIAA01A000V  255     +
$ bamToBed < test.bam     
BGZF ERROR: unable to open file stdin
########################################

The partial BAM file could be read from stdin on OS X 10.5, however.

Some debugging revealed that the issue occurs in BGZF.cpp, lines 245-256:

########################################
    // read BGZF data from stdin
    else if ( (filename == "stdin") && (strcmp(mode, "rb") == 0 ) )
        Stream = freopen(NULL, mode, stdin);

    // write BGZF data to stdout
    else if ( (filename == "stdout") && (strcmp(mode, "wb") == 0) )
        Stream = freopen(NULL, mode, stdout);

    if ( !Stream ) {
        printf("BGZF ERROR: unable to open file %s\n", filename.c_str() );
        return false;
    }
########################################

The Solaris freopen() man page indicates that it behave in a nonstandard manner 
(always returns NULL if the first argument is NULL) unless "the application 
conforms to SUSv3" (i.e., the _XOPEN_SOURCE feature test macro is defined with 
the value 600).

A simple solution is to bypass the call to freopen, which should be unnecessary 
for stdin and stdout, and simply assign stdin/stdout to Stream (see the 
attached patch).

--
Nathan Weeks
USDA-ARS
SoyBase http://soybase.org

Original issue reported on code.google.com by nate.we...@gmail.com on 11 Jul 2011 at 4:37

Attachments: