Convert BAM to properly paired FASTQ files I thought I had figured this one out. But apparently not.

I need to convert a BAM file of paired-end alignments to two FASTQ files of paired reads to realign them, with a twist: I only want reads that fall within a defined region.

Currently I am using the following command (the region is an example; it also fails on different regions; at any rate, this is on hs37d5 but presumably the issue would exist for other references too):

samtools view -b -u -@ 48 alignments.bam 21:31,831,502-31,896,094 \
| samtools collate -n 128 -u -O -@ 48 - /tmp/bam-to-fastq- \
| samtools fastq -F 0x900 -@ 48 \
    -0 /dev/null -1 reads_R1.fastq.gz -2 reads_R2.fastq.gz -


(Apart from the region filtering, this command is essentially the same that word of god tells us to use.)

The resulting files are not properly paired:

⟩⟩⟩ diff &lt;(gunzip -c reads_R1.fastq.gz | sed -n -e 1~4p -e 10q) \
1 ⟩   &lt;(gunzip -c reads_R2.fastq.gz | sed -n -e 1~4p -e 10q)
1,2d0
&lt; @A00346:17:H52H7DSXX:3:2142:32316:24518
&lt; @A00346:17:H52H7DSXX:3:1153:6705:26412
3a2,3
&gt; @A00346:17:H52H7DSXX:3:1173:9118:3129
&gt; @A00346:17:H52H7DSXX:3:1109:29414:11757


As far as I can see from performing a spot check, these are all reads which are marked as paired in the BAM file, but for which only one mate falls within the filtered region.

And running bwa mem on the files gives me the error:


  [mem_sam_pe] paired reads have different names: […]


How can I ensure that only properly paired reads are included in the FASTQ files?
As noted in the comments, the problem is “some reads fall in the target region but their pairs fall outside it”, leading to non-trivial numbers of singleton reads coming out of samtools collate.

… | samtools fastq -F 0x900 -@ 48 \
    -0 /dev/null -1 reads_R1.fastq.gz -2 reads_R2.fastq.gz -


Your samtools fastq command is not doing anything to siphon off these singleton reads. You should add -s /dev/null to get rid of them.

See the discussion in samtools/samtools#874 (especially the part starting here). I have not looked at how the recent changes from jkbonfield, which will land in an upcoming samtools version (after 1.9), might affect this.