Question: Loop to change a @RG in a BAM file
gravatar for mwerseb1
15 days ago by
mwerseb10 wrote:


I am trying to change the @RG from "ID:i" to "ID:MyFileName" in a number of files using a while read loop. My syntax is as follows:

ls *.sorted.bam|awk -F "." '{print $1}'| while read name; do
samtools view -H $name.sorted.bam| sed 's/ID:i/ID:$name/'|samtools reheader - $name.sorted.bam > $name.sorted_rh.bam;

Everything works perfectly except in the reheadered bam file the "ID:i" is replaced with "ID:$name" not the file name. Any ideas about how to fix this? When not in a loop the sed command changes the strings correctly.


bash samtools bam loop • 110 views
ADD COMMENTlink modified 15 days ago by Pierre Lindenbaum123k • written 15 days ago by mwerseb10

This should work as well:

samtools addreplacerg -r "ID:$name" -o $name.sorted_rh.bam $name.sorted.bam
ADD REPLYlink written 14 days ago by finswimmer12k
gravatar for Pierre Lindenbaum
15 days ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum123k wrote:

use double quote instead of single quote

sed "s/ID:i/ID:$name/"


ADD COMMENTlink written 15 days ago by Pierre Lindenbaum123k

That seems to have worked! Thanks!

ADD REPLYlink written 15 days ago by mwerseb10
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1282 users visited in the last hour