<p>Is there a Picard Tool to filter one or more reads through the following : </p>

<ul>
<li>FLAG (second column) </li>
<li>Mapping Quality (MAPQ 5th Column) </li>
<li>TLEN (9th Column)</li>
</ul>

<p>?? </p>

<p>Hypothetical scenario: Suppose you'd want to get all reads with flag 83=<code>paired reads|first in pair|read reverse mapped| proper pair</code> reads with mapping quality &gt; 30 from a <a href='http://samtools.sourceforge.net/SAM1.pdf'>bam</a> file and return output to a <a href='http://samtools.sourceforge.net/SAM1.pdf'>bam</a> file:</p>

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

<p>How it works?:</p>

<pre><code>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 &gt;= 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
&gt; output.bam               - simple unix redirection to output file
</code></pre>

<p>Basically, you can use the awk line to filter anything you want.</p>
