This is a test version of Biostars. For the public version, visit https://www.biostars.org.
add sample name to bam header based on bam file name

I have a group of bam files but some of them do not have sample name SM in @RG,

example: sample1.gh37.bam

@RG     ID:13048        CN:BCCAGSC      LB:NA   PL:illumina     PU:NA   SM:N/A

I want to add the file name (but without "gh37.bam" suffix) to SM , to get something like this

@RG     ID:13048        CN:BCCAGSC      LB:NA   PL:illumina     PU:NA   SM:sample1

Any help how I can do it

samtools

1 answer

picard AddOrReplaceReadGroups https://broadinstitute.github.io/picard/command-line-overview.html#AddOrReplaceReadGroups or samtools addreplacerg

for file in *.bam; do
base_name=$(basename $file .grch37.bam);
samtools addreplacerg -r "SM:${base_name}" -o ${base_name}.bam $file;
done

Already using this but I get The supplied RG line lacks an ID ta error

I do not want to change the ID, its the same. How can I adjust it?

Log in to answer this question.