# Moving UMI from tag into read name

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.

• Did you check UMI-tools, this is afaik exactly what it is doing? Nov 22 '20 at 11:11
• @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. Nov 22 '20 at 11:38

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
awk '{OFS = "\t"} {for(i=1;i<=NF;i++)  if ($$i ~ /^RX:Z:/) {1=1"_"i; gsub("RX:Z:","",$$1); print}}' >> temp2.sam