<p>Looks like you need Picard's <code>RevertSam</code> operation (described in context <a href="https://software.broadinstitute.org/gatk/documentation/article?id=6484#optionB" rel="nofollow noreferrer">here</a>, manual <a href="https://broadinstitute.github.io/picard/command-line-overview.html#RevertSam" rel="nofollow noreferrer">here</a>).</p>

<pre><code>java -jar picard.jar RevertSam \
     I=input.bam \
     O=reverted.bam
</code></pre>

CC BY-SA 4.0
2019-07-10T18:38:00.173Z
2019-07-10T18:38:00.173Z
Daniel Standage
<p>What happens if you use the bam with the metadata as the input to your aligner?  Maybe it will keep all those fields.</p>

CC BY-SA 4.0
2019-07-11T13:19:16.543Z
bwa mem requires a fastq file, it does not accept a BAM
Biomagician
CC BY-SA 4.0
2019-07-11T15:54:28.863Z
Okay, I didn't know you had to use that.  You see that bwa aln will take a bam?
swbarnes2
CC BY-SA 4.0
2019-07-11T16:50:20.280Z
good point, the RevertSam worked though
Biomagician
CC BY-SA 4.0
2019-07-11T05:47:56.127Z
2019-07-11T05:47:56.127Z
swbarnes2
<p>In order to use <a href="https://software.broadinstitute.org/gatk/documentation/tooldocs/current/picard_sam_MergeBamAlignment.php" rel="nofollow noreferrer">MergeBamAlignment (Picard)</a>, I need an unmapped BAM file and a mapped BAM file. I have two mapped BAM files: </p>

<ul>
<li>one with reads mapped to the reference I want but without metadata such as read group</li>
<li>a second one with reads mapped to the incorrect reference but with metadata</li>
</ul>

<p>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.</p>

<sam><read-mapping><software-recommendation>
How can I transform a mapped BAM file into an unmapped BAM file?
# How can I transform a mapped BAM file into an unmapped BAM file?

(Asked by: username_0 on 2019-07-10)

In order to use [MergeBamAlignment (Picard)](https://software.broadinstitute.org/gatk/documentation/tooldocs/current/picard_sam_MergeBamAlignment.php), 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.

**Tags:** sam, read-mapping, software-recommendation

---

## 2 answers

### Answer by username_1 on 2019-07-10. Score: 2 | Accepted answer

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


---

### Answer by username_2 on 2019-07-11. Score: 0

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

> **Comments:**
> **username_0**: bwa mem requires a fastq file, it does not accept a BAM  
> **username_2**: Okay, I didn't know you had to use that.  You see that bwa aln will take a bam?  
> **username_0**: good point, the RevertSam worked though  

---

