Working on a stream
SAMtools is designed to work on a stream. You can output SAM/BAM to the standard output (stdout) and pipe it to a SAMtools command via standard input (stdin) without generating a temporary file. For example, the following command runs pileup for reads from library libSC_NA12878_1: where `-u' asks samtools to output an uncompressed BAM. Option `-u' is similar to `-b', but is preferred in piping because it saves time on compression/decompression. Similarly, you can get the pileup of a particular region with: For more complex filtering, you can print out SAM and use Unix commands to filter it. For example, if we want to exclude reads in the read group ERR00001 and generate a new BAM, we can do in this way: or if you want to exclude all the gapped alignments in pileup:
In the examples above, we only see how to combine the `view' and `pileup' commands. In fact, most of samtools commands, except indexing and external sorting, recognize an input file `-' as stdin and an output file `-' as stdout. You can run several samtools commands on one stream, which extends the ability of each single command, saves diskspace and reduces wall-clock time.
NOTE: some options in the examples are implemented in the latest SVN (r347 or above) which is not released at the time of my writing this page.