This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Change Notation Of Chromosome In Bam File

Hi all,

I have a bam file with chromosomal notation 1,2.... and i need it to be as chr1,chr2.....

For this i have tried, samtools view -H sample.bam > header.sam

Then edited the header.sam file with required chromosomal notation and tried samtools reheader header.sam sample.bam > out.bam

when in tried to view out.bam using samtools it says "Segemntation fault".

I also tried with PICARD, java -Xmx4g -jar ReplaceSamHeader.jar INPUT=sample.bam HEADER=header.sam OUTPUT=out.bam CREATE_INDEX=TRUE

It reports the same error with samtools i.e. segmenation fault.

Could anyone help me to fix this?

bam samtools

2 answers

The reads need to have the same @SQ tags for the seqid.

samtools view your.bam | perl -lane 'if($_ =~ /\@/){print $_}else{$F[2] = "chr$F[2]" if $F[2] !~ /\*/; print join "\t", @F}' | bgzip or gzip

Of coure you will need to edit your header so the @SQ tags are correct.

Thanks. I have never worked with perl. So could you please let me know what the above command will do? What does the bgzip or gzip mean at the end?

It changes:

3 RNAME String *|[!-()+-<>-~][!-~]* Reference sequence NAME l

like you wanted.

So the output will be a bam file?

yes if you pipe the output to the correct compression software. I'm pretty sure you want bgzip.

I tried using the following command: samtools view my.bam | perl -lane 'if($_ =~ /\@/){print $_}else{$F[2] = "chr$F[2]" if $F[2] !~ /*/; print join "\t", @F}' | gzip - > out.bam.

The out.bam is viewed using samtools. This time it shows an error:

[bam_header_read] EOF marker is absent. The input is probably truncated. [bam_header_read] invalid BAM binary header (this is not a BAM file). [main_samview] fail to read the header from "out.bam".

Okay. Simplest solution don't compress it. This will be a SAM file. use samtools to convert it to a BAM.

Hi, I had the same task with my chomosome notation. Bam Header Edit helped me.

About the crash, beware of samtools version. Someone experimented crashes before version 0.1.19

Log in to answer this question.