Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-10T18:38:00.173Z
Answers.LastActivityDate: 2019-07-10T18:38:00.173Z
Answers.UserDisplayName: Daniel Standage
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-11T13:19:16.543Z
Answers.Text: bwa mem requires a fastq file, it does not accept a BAM
Answers.UserDisplayName: Biomagician
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-11T15:54:28.863Z
Answers.Text: Okay, I didn't know you had to use that.  You see that bwa aln will take a bam?
Answers.UserDisplayName: swbarnes2
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-11T16:50:20.280Z
Answers.Text: good point, the RevertSam worked though
Answers.UserDisplayName: Biomagician
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-11T05:47:56.127Z
Answers.LastActivityDate: 2019-07-11T05:47:56.127Z
Answers.UserDisplayName: swbarnes2
Body: <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>
  
Tags: <sam><read-mapping><software-recommendation>
Title: How can I transform a mapped BAM file into an unmapped BAM file?
ThreadText: # 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](https://software.broadinstitute.org/gatk/documentation/article?id=6484#optionB), manual [here](https://broadinstitute.github.io/picard/command-line-overview.html#RevertSam)).
  
  ```
  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  
  
  ---
  
  