Question: Is There A Tool (Samtools, Picard ..) To Filter Certain Flags In Bam File ?
gravatar for madkitty
8.6 years ago by
madkitty620 wrote:

Is there a Picard Tool to filter one or more reads through the following :

  • FLAG (second column)
  • Mapping Quality (MAPQ 5th Column)
  • TLEN (9th Column)


bam samtools • 7.6k views
ADD COMMENTlink modified 5.0 years ago by Biostar ♦♦ 20 • written 8.6 years ago by madkitty620
gravatar for Arun
8.6 years ago by
Arun2.4k wrote:

Hypothetical scenario: Suppose you'd want to get all reads with flag 83=paired reads|first in pair|read reverse mapped| proper pair reads with mapping quality > 30 from a bam file and return output to a bam file:

samtools view -h input.bam | awk 'substr($0,1,1) == "@" || $2 == 83 || $5 >= 30 {print}' | samtools view -bS - > output.bam

How it works?:

samtools view -h input.bam - decodes bam and prints sam file with header to screen
awk 'substr($0,1,1) == "@" - keep all header lines
|| $2 == 83 || $5 >= 30 {print}        - the actual condition you would want to check. for unmapped reads you could use GAWK with bitwise operation and($2, 4) == 4 || and($2, 16) == 16, or something like that.
samtools view -bS          - pipe input S=sam to output b=bam and the "-" mediates samtools to take input from the screen
> output.bam               - simple unix redirection to output file

Basically, you can use the awk line to filter anything you want.

ADD COMMENTlink modified 8.6 years ago • written 8.6 years ago by Arun2.4k
gravatar for Fedor Gusev
8.6 years ago by
Fedor Gusev210
Russian Federation
Fedor Gusev210 wrote:

You can use BamTools with its filter utility. You need to write a .json file like this one:

"isMapped" : "true",
"mapQuality" : ">50",
"isPaired" : "true"

And run command

bamtools filter -in in.bam -out out.bam -script filter.json

See a tutorial here.

I don't see a way to extract TLEN with it, though.

ADD COMMENTlink written 8.6 years ago by Fedor Gusev210

(+1) haven't seen that. thanks.

ADD REPLYlink written 8.6 years ago by Arun2.4k

Will try it out. Thanks a lot :)

ADD REPLYlink written 6.6 years ago by madkitty620
gravatar for mathieu.bahin
7.0 years ago by
mathieu.bahin50 wrote:

How can I filter on a string in a "subtag" ?

I have an annotated BAM file (from CRAC) with an 'XE' tag. For example, the tags are:




I would like to get only the chimeras but I can't figure out how to use 'bamtools filter' correctly (in order to be quicker than a 'grep').

Anyone knows how to do ?


ADD COMMENTlink written 7.0 years ago by mathieu.bahin50
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1743 users visited in the last hour