This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How Can I Edit Some Rows In .Bam Header File?

For example i have the following already in the bam header:

@RG    ID:110131_SN107_0398_A81DDCABXX_LANE2    PL:ILLUMINA    LB:P0007    SM:tumor
@RG    ID:110131_SN107_0398_A81DDCABXX_LANE4    PL:ILLUMINA    LB:P0007    SM:tumor
@RG    ID:110131_SN107_0398_A81DDCABXX_LANE6    PL:ILLUMINA    LB:P0007    SM:tumor
@RG    ID:110131_SN107_0398_A81DDCABXX_LANE8    PL:ILLUMINA    LB:P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE2    PL:ILLUMINA    LB:P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE4    PL:ILLUMINA    LB:P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE6    PL:ILLUMINA    LB:P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE8    PL:ILLUMINA    LB:P0007    SM:tumor

i want to make it:

@RG    ID:110131_SN107_0398_A81DDCABXX_LANE2    PL:ILLUMINA    LB:tumor_P0007    SM:tumor
@RG    ID:110131_SN107_0398_A81DDCABXX_LANE4    PL:ILLUMINA    LB:tumor_P0007    SM:tumor
@RG    ID:110131_SN107_0398_A81DDCABXX_LANE6    PL:ILLUMINA    LB:tumor_P0007    SM:tumor
@RG    ID:110131_SN107_0398_A81DDCABXX_LANE8    PL:ILLUMINA    LB:tumor_P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE2    PL:ILLUMINA    LB:tumor_P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE4    PL:ILLUMINA    LB:tumor_P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE6    PL:ILLUMINA    LB:tumor_P0007    SM:tumor
@RG    ID:110131_SN107_0399_B81CYUABXX_LANE8    PL:ILLUMINA    LB:tumor_P0007    SM:tumor

Thanks.

samtools bam
samtools view -H inputfile.bam | sed -e 's/ID:XXX:/ID:21Normal/' | sed -e 's/SM:XXX/SM:21Normal/' | samtools reheader - inputfile.bam > outbamfile.reheadered.bam

This command just change header file , Do you have any Idea How can I change ID in complete .bam file rather to just change in header. I know samtools merge command to change R:Z: in reads but It occupies extra space and very time consuming. Above command is fast but It just change ID in header not in complete .bam file. I like to change R:Z: with R:Z:XXX

1 answer

samtools view -H mybamfile.bam | sed -e 's/LB:/LB:tumor_/' | samtools reheader - mybamfile.bam > mybamfile.reheadered.bam

Smooth!

In my case I just need to add a g, global, after the substitution pattern:

sed -e 's/LB:/LB:tumor_/g'

can any one tell me how to change two columns? for example the same change in LB column and totally replacing "tumor" with "cancer" in SM column! thanks a lot. I'm not familiar with sed! is what I wrote correct?

code:

samtools view -H mybamfile.bam | sed -e 's/LB:/LB:tumor_/' | sed -e 's/SM:tumor/SM:cancer/' | samtools reheader - mybamfile.bam > mybamfile.reheadered.bam

Log in to answer this question.