<p>The best thing would be to figure out how to do this in UMItools. I don't know that tool at all, but it seems that <a href="https://github.com/CGATOxford/UMI-tools/issues/347" rel="nofollow noreferrer">it is supposed to be able to deal with / tag both reads in paired data</a>. e.g. did you use <code>--paired</code> flag, etc.</p>
<p>I would suggest investigating that route first before you look at the next part of my answer.</p>
<hr />
<p>Per Devon Ryan's comment suggestion I would suggest a workflow similar to this:</p>
<ul>
<li>sort bam file by query name (<code>samtools sort -n</code>) to get the R1s immediately following the R2s</li>
<li>Write a short script/command that
<ol>
<li>reads through the bam file and for each line</li>
<li>looks up the tag in the R1, prints R1 line</li>
<li>reads the R2, substitutes the RX tag in, prints R2 line</li>
</ol>
</li>
<li>pipe that through <code>samtools view</code> and possibly <code>samtools sort</code> back to a bam file of the same type that you started with.</li>
</ul>
<p>My pysam is a little rusty but something like this might work</p>
<pre><code>import pysam

bam = pysam.AlignmentFile(&quot;bam&quot;, &quot;rb&quot;)
# print header
print(bam.header)

tag = None
for read in bam:
    r2 = False
    if tag:
        read.assign_umi_tag() # assign to read- not sure where/how
        r2 = True
    else:
        tag = read.lookup_umi_tag()  # i don't know where this tag is in bam format
    print(read)
    if r2:
        tag = None
</code></pre>
<p>I haven't tested that in any way, caveat emptor.</p>
<p>I am not personally familiar with UMItools and how it assigns tags so can't suggest anything more specific, sorry.</p>

CC BY-SA 4.0
2020-10-20T12:15:50.397Z
Thanks. My pysam is non-existent, but I can probably do it in Rsamtools.
SPPearce
CC BY-SA 4.0
2020-10-19T20:16:46.993Z
2020-10-19T20:30:32.413Z
2020-10-19T20:30:32.413Z
Maximilian Press
<p>I have a bam file from paired-end sequencing, in which each R1 has the <code>RX</code> tag assigned (via <code>UMItools group</code>) denoting a Unique Molecular Identifier (UMI).</p>
<p>I want to call molecular consensus reads using <code>fgbio</code> but <code>fgbio GroupReadsByUmi</code> gives me an error, because it expects both R1 and R2 to have the <code>RX</code> tag set.</p>
<p>I would like a tool that will go through my bam file and assign each R2 the same <code>RX</code> tag as the R1.</p>

CC BY-SA 4.0
2020-10-19T14:43:36.617Z
For something that specific you'll probably need to write something.
Devon Ryan
CC BY-SA 4.0
2020-10-19T20:26:42.953Z
I would also ask this of the umitools developers: https://github.com/CGATOxford/UMI-tools/issues
Maximilian Press
<sam><umi>
Add tags to the read pairs in a bam file
# Add tags to the read pairs in a bam file

(Asked by: username_0 on 2020-10-19)

I have a bam file from paired-end sequencing, in which each R1 has the `RX` tag assigned (via `UMItools group`) denoting a Unique Molecular Identifier (UMI).

I want to call molecular consensus reads using `fgbio` but `fgbio GroupReadsByUmi` gives me an error, because it expects both R1 and R2 to have the `RX` tag set.

I would like a tool that will go through my bam file and assign each R2 the same `RX` tag as the R1.

**Tags:** sam, umi
> **Comments:**
> **username_1**: For something that specific you'll probably need to write something.  
> **username_2**: I would also ask this of the umitools developers: https://github.com/CGATOxford/UMI-tools/issues  

---

## 1 answers

### Answer by username_2 on 2020-10-19. Score: 1

The best thing would be to figure out how to do this in UMItools. I don't know that tool at all, but it seems that it is supposed to be able to deal with / tag both reads in paired data. e.g. did you use --paired flag, etc.
I would suggest investigating that route first before you look at the next part of my answer.

Per Devon Ryan's comment suggestion I would suggest a workflow similar to this:

sort bam file by query name (samtools sort -n) to get the R1s immediately following the R2s
Write a short script/command that

reads through the bam file and for each line
looks up the tag in the R1, prints R1 line
reads the R2, substitutes the RX tag in, prints R2 line


pipe that through samtools view and possibly samtools sort back to a bam file of the same type that you started with.

My pysam is a little rusty but something like this might work
import pysam

bam = pysam.AlignmentFile("bam", "rb")
# print header
print(bam.header)

tag = None
for read in bam:
    r2 = False
    if tag:
        read.assign_umi_tag() # assign to read- not sure where/how
        r2 = True
    else:
        tag = read.lookup_umi_tag()  # i don't know where this tag is in bam format
    print(read)
    if r2:
        tag = None

I haven't tested that in any way, caveat emptor.
I am not personally familiar with UMItools and how it assigns tags so can't suggest anything more specific, sorry.

> **Comments:**
> **username_0**: Thanks. My pysam is non-existent, but I can probably do it in Rsamtools.  

---

