How can I transform a mapped BAM file into an unmapped BAM file?

In order to use MergeBamAlignment (Picard), I need an unmapped BAM file and a mapped BAM file. I have two mapped BAM files:

• one with reads mapped to the reference I want but without metadata such as read group
• a second one with reads mapped to the incorrect reference but with metadata

When I try to use MergeBamAlignment with these two files, I get errors, such as "Second read from pair not found in unmapped bam". So I am trying to unmap the reads from my second BAM file. How can I do that? Is there a tool for it? Otherwise, I'll have to manually change the bitwise FLAGs, reference names, CIGARS, etc.

2 Answers

Looks like you need Picard's RevertSam operation (described in context here, manual here).

java -jar picard.jar RevertSam \
I=input.bam \
O=reverted.bam


What happens if you use the bam with the metadata as the input to your aligner? Maybe it will keep all those fields.

• bwa mem requires a fastq file, it does not accept a BAM Jul 11 '19 at 13:19
• Okay, I didn't know you had to use that. You see that bwa aln will take a bam? Jul 11 '19 at 15:54
• good point, the RevertSam worked though Jul 11 '19 at 16:50