This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filtering Multiple Flags With Samtools

I am trying to remove paired-end reads from a .SAM file where neither segment is mapped (i.e. having both 0x4 AND 0x8 set). When I try the command "samtools view -F 12" it appears to remove reads where either 0x4 OR 0x8 are set.

1) Am I correct in understanding the samtools help notes that -F performs a logical OR (and that -f performs a logical AND)?

2) If so, any thoughts on how to perform this aside from awk?

samtools

Serially filtering with -f 4 -F 264 and -f 8 -F 260, and then merge/sort -n did the trick. Thanks!

PS. Any way to flag this as an answer?

Copy and paste it into the answer field. I believe you can accept your own answer.

Hrmm... when I try and click the green check symbol, I get a "You may not vote on your own post"

2 answers

Here it is added as answer: Have a look at this: http://www.novocraft.com/documentation/novoalign-2/novoalign-ngs-quick-start-tutorial/1040-2/

For the sake of completeness, here the awk snippet which you were were asking for

samtools view your-bam-file | \
  awk 'BEGIN {FS="\t"; OFS="\t"} {
    if (/^@/ && substr($2, 3, 1)==":") {print}
    else if (and($2, 0x4) && and($2, 0x8)) {print}}

The first line makes sure to use tabs as input and output delimiters. The second makes sure to print the header The third does your requested filtering: 0x4 and 0x8 have to bet set.

Andreas

Hi, I tried using this script. It prints the sam header, but not the rest of the file that matches the correct bits. It gives the error awk: "calling undefined function and input record number 4". There are only three lines of header.

Any ideas? Thanks!

Which awk version is this? I used gawk...

Log in to answer this question.