This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Samtools view -f "OR" operator possible?

Quick question:

Is it possible to do a samtools view -f XXX using a OR operator?

Say if I wanted to get all reads that were unmapped (4) or supplementary alignments (2048).

Is this possible? Because doing a 2052 is an "AND" (reads are unmapped and supplementary alignments which is impossible)

samtools

3 answers

using samjdk http://lindenb.github.io/jvarkit/SamJdk.html

 java -jar dist/samjdk.jar -e 'return record.getSupplementaryAlignmentFlag() || record.getReadUnmappedFlag();' in.bam

BamTools can do what you want, read the filter section of the tutorial. When you set multiple properties in one filter, these properties are AND-ed, and when you set multiple properties in different filters, these properties are OR-ed.

The closest I can think of is, using samtools (not tested):

samtools merge - \
    <(samtools view -u -f 4 in.bam) \
    <(samtools view -u -f 2048 in.bam)

This will output BAM to stdout, replace - with an actual output filename.

Maybe worth noting that this will output duplicate alignments if a record matches more than one OR conditions (e.g. for 2052 which is not really possible in this case but you never know...)

Log in to answer this question.