Answers.Body: <p>My current solution is (<code>awk</code> is not a tool I'm very familiar with, so this may not be good <code>awk</code> code, there is presumably a way to get it to print the header lines unmodified.):</p>
  <pre><code>samtools view -H temp.sam &gt; temp2.sam
  samtools view temp.sam | 
  awk '{OFS = &quot;\t&quot;} {for(i=1;i&lt;=NF;i++)  if (<span class="math-container">$i ~ /^RX:Z:/) {$1=$1"_"$i; gsub("RX:Z:","",$</span>1); print}}' &gt;&gt; temp2.sam
  </code></pre>
  <p>This seems to work when I test it, but I don't know if I'm missing anything.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-22T11:08:22.583Z
Answers.LastActivityDate: 2020-11-23T11:35:37.933Z
Answers.LastEditDate: 2020-11-23T11:35:37.933Z
Answers.UserDisplayName: SPPearce
Body: <p>I have a bam file with Unique Molecular Identifiers (UMIs) for each read present on the RX tag ( such as <code>RX:Z:TGAGAAGGG</code>), as expected by picard and fgbio tools.</p>
  <p>However, several other tools (such as UMI-tools) expect the UMIs to be present at the end of the read name, such as (<code>NB501943:73:HK333BGXC:3:12402:20313:16437_TGAGAAGGG</code>).</p>
  <p>Does anyone know of a tool to do this?
  I have an <code>awk</code> solution which I'll self-answer with, but I don't know whether that is robust.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-22T11:38:21.217Z
Comments.Text: @ATpoint, unless I'm incorrect, UMI-tools can't do this directly. `umi_tools extract` moves sequences from a position in the FASTA sequence onto the read name, but in this case the reads have been added to a tag via `fgbio`.
Comments.UserDisplayName: SPPearce
Tags: <sam><umi>
Title: Moving UMI from tag into read name
ThreadText: # Moving UMI from tag into read name
  
  (Asked by: username_0 on 2020-11-22)
  
  I have a bam file with Unique Molecular Identifiers (UMIs) for each read present on the RX tag ( such as `RX:Z:TGAGAAGGG`), as expected by picard and fgbio tools.
  
  However, several other tools (such as UMI-tools) expect the UMIs to be present at the end of the read name, such as (`NB501943:73:HK333BGXC:3:12402:20313:16437_TGAGAAGGG`).
  
  Does anyone know of a tool to do this?
  I have an `awk` solution which I'll self-answer with, but I don't know whether that is robust.
  
  **Tags:** sam, umi
  
  > **Comments:**
  > **username_0**: @ATpoint, unless I'm incorrect, UMI-tools can't do this directly. `umi_tools extract` moves sequences from a position in the FASTA sequence onto the read name, but in this case the reads have been added to a tag via `fgbio`.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-11-22. Score: 1
  
  My current solution is (`awk` is not a tool I'm very familiar with, so this may not be good `awk` code, there is presumably a way to get it to print the header lines unmodified.):
  
  ```
  samtools view -H temp.sam > temp2.sam
  samtools view temp.sam | 
  awk '{OFS = "\t"} {for(i=1;i<=NF;i++)  if ($i ~ /^RX:Z:/) {$1=$1"_"$i; gsub("RX:Z:","",$1); print}}' >> temp2.sam
  
  ```
  
  This seems to work when I test it, but I don't know if I'm missing anything.
  
  
  ---
  
  