<p>Hi,
I have a large paired-end dataset in the <a href='http://samtools.sourceforge.net/SAM1.pdf'>BAM</a> format and a list of read IDs which belong to a single mate of a pair. What I want to do is to extract their second mates from the whole dataset. Could you please advise me some efficient ways to do this like using, let's say, Bio-SamTools or something like that? Something memory- and time-efficient.
Thanks!</p>

**2018**: use picard https://broadinstitute.github.io/picard/command-line-overview.html#FilterSamReads

____

<p>The following C++ code should filters only print the reads contained in your file (I've added to my variation toolkit <a href='http://code.google.com/p/variationtoolkit'>http://code.google.com/p/variationtoolkit</a> ) </p>

<p><a href='http://code.google.com/p/variationtoolkit/source/browse/trunk/src/bamgrepreads.cpp'>http://code.google.com/p/variationtoolkit/source/browse/trunk/src/bamgrepreads.cpp</a></p>

<p><strong>Compilation</strong>:</p>

<pre><code>g++ -O3 -Wall -I  path/to/samtool -L path/to/samtool bamgrepreads.cpp -lbam -lz
</code></pre>

<p><strong>Usage</strong>:</p>

<p>./a.out -R file_containing_the_reads_name.txt (stdin|bam1 bam2...)</p>

<p><strong>Options</strong>:</p>

<pre><code> -f INT   required flag
 -F INT   filtering flag
 -R FILE reads file
 -e  only one match per name (goes faster)
</code></pre>
