This is a test version of Biostars. For the public version, visit https://www.biostars.org.
fetch fully unaligned pairs and pairs with one unaligned read with samtools view

How do I fetch fully unaligned pairs and pairs with one unaligned read with samtools view?

samtools bam

With a only a single command? That's not actually possible with the base samtools. You can do this with pysam (and probably Pierre's jvarkit), though.

It boils down to lack of support for OR-ing flags in samtools, so that the only way to accomplish what you ask is splitting the set into three and concatenating the results (as shown below by Devon). Given that unaligned reads are infrequent, this means it takes 3x more CPU time than the straightforward approach.
Other than pysam, you could use sambamba (I'm the author) with flags -F "unmapped or mate_is_unmapped"

1 answer

Cheating by using process substitution:

samtools cat -o output.bam <(samtools view -uf 12 input.bam) <(samtools view -uf 4 -F 8 input.bam) <(samtools view -uf 8 -F 4 input.bam)

Edit: Changed things to not compress, which should make things a bit faster.

Log in to answer this question.