This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Samtools Depth Stdin ?

Hi all,

I would like to use samtools depth in a pipeline to grab strand-specific coverage from a given interval, where the bam comes from STDIN. I thought that all of the samtools tools accepted STDIN data with the "-" switch. But, the following code fails with message "open: No such file or directory":

samtools view -u -f 0x10 example.bam chr1:1234-4567 | samtools depth -

As a counter-example, it works fine if mpileup is used in the pipeline, like follows:

samtools view -u -f 0x10 example.bam chr1:1234-4567 | samtools mpileup -

I know I could just write a temporary bam file, and the read it with samtools depth, but a pipe would be nicer...

samtools version 0.1.19-44428cd

samtools depth-of-coverage

2 answers

Just use /dev/stdin instead of -.

It won't work for cases where the tool tries to look for .bai index files based on the name (which in this case would only happen if you try to analyze a specific region with the -r flag), but it will work for the example you've given.

great, works like a champ! Analysis of a specific region is easily accomplished from the upstream samtools view call, so no need to use the depth option -r

in https://github.com/lh3/samtools/blob/master/

in the C code 'bam2depth.c" the bam is open using:

    data[i]->fp = bam_open(argv[optind+i], "r"); // open BAM

you can try to replace this line with:

    data[i]->fp = strcmp(argv[optind+i], "-")? bam_open(argv[optind+i], "r") : bam_dopen(fileno(stdin), "r");

and recompile bam2depth using 'make' (not tested)

BUT this hack won't work if there is more than one BAM and if the BAM needs a random-access.

Log in to answer this question.