<p>I want to split my bam file into paired aligned reads and unpaired aligned reads for a much easier downstream analysis.</p>

<p>1) Is this possible to do in one run through the file with samtools?</p>

<p>2) If this is not possible with samtools directly, is it possible with the pysam wrapper library?</p>

<p>If not, perhaps just running the two filters in parallel should reduce IO due to caching...</p>

<p>Ps. I want the pairs in the paired file to be on contiguous lines, so that line 0 and 1 contain the first pair, line 2 and 3 contain the second pair and so on.</p>

<p>3) Is sorting the file by name enough to ensure that paired reads end up next to each other?</p>

<p>Sorry for these possibly dumb questions, I am a complete samtools/paired end reads newb.</p>

<pre>
samtools view -f 4 -o umapped.bam -U mapped.bam in.bam</pre>

<p>http://www.htslib.org/doc/samtools-1.1.html</p>

<p>&quot;-U FILE Write alignments that are <em>not</em> selected by the various filter options to <em>FILE</em>. When this option is used, all alignments (or all alignments intersecting the <em>regions</em> specified) are written to either the output file or this file, but never both. &quot;</p>
