Loop to change a @RG in a BAM file
1
0
Entering edit mode
4.7 years ago
mwerseb1 • 0

Hello,

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;
done

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.

Thanks

BAM samtools Bash loop • 2.5k views
ADD COMMENT
1
Entering edit mode

This should work as well:

samtools addreplacerg -r "ID:$name" -o $name.sorted_rh.bam $name.sorted.bam
ADD REPLY
2
Entering edit mode
4.7 years ago

use double quote instead of single quote

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

see https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash

ADD COMMENT
0
Entering edit mode

That seems to have worked! Thanks!

ADD REPLY

Login before adding your answer.

Traffic: 1925 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6