<p>Dear All,</p>

<p>I am using <a href='http://samtools.sourceforge.net/SAM1.pdf'>BAM</a> files for chip-seq analysis. The chromosome notation in a usual <a href='http://samtools.sourceforge.net/SAM1.pdf'>BAM</a> file is like: chr1. In my file the  chromosomme notation is 1. Is there a way to change that in the <a href='http://samtools.sourceforge.net/SAM1.pdf'>BAM</a> file? For further analysis it is very important to change the notation.</p>

<p>Many thanks!</p>

<p>Greetz Lisanne</p>

The following line works well for me. It will replace the names of chromosome 1 to 22, X, Y and MT (mitochondrial DNA). It will create a new file with the name `test_chr.bam` instead of `test.bam`.

use Samtools

    samtools view -H test.bam |\
       sed -e 's/SN:1/SN:chr1/' | sed -e 's/SN:2/SN:chr2/' | \
       sed -e 's/SN:3/SN:chr3/' | sed -e 's/SN:4/SN:chr4/' | \
       sed -e 's/SN:5/SN:chr5/' | sed -e 's/SN:6/SN:chr6/' | \
       sed -e 's/SN:7/SN:chr7/' | sed -e 's/SN:8/SN:chr8/' | \
       sed -e 's/SN:9/SN:chr9/' | sed -e 's/SN:10/SN:chr10/' | \
       sed -e 's/SN:11/SN:chr11/' | sed -e 's/SN:12/SN:chr12/' | \
       sed -e 's/SN:13/SN:chr13/' | sed -e 's/SN:14/SN:chr14/' | \
       sed -e 's/SN:15/SN:chr15/' | sed -e 's/SN:16/SN:chr16/' | \
       sed -e 's/SN:17/SN:chr17/' | sed -e 's/SN:18/SN:chr18/' | \
       sed -e 's/SN:19/SN:chr19/' | sed -e 's/SN:20/SN:chr20/' | \
       sed -e 's/SN:21/SN:chr21/' | sed -e 's/SN:22/SN:chr22/' | \
       sed -e 's/SN:X/SN:chrX/' | sed -e 's/SN:Y/SN:chrY/' | \
       sed -e 's/SN:MT/SN:chrM/' | samtools reheader - test.bam > test_chr.bam

If you want to do this for a bunch of bam files at once, use a loop. The variable `$filename` is your file without the extension (assuming there is no period except the one in ".bam").

    for file in *.bam
    do
      filename=`echo $file | cut -d "." -f 1`
      samtools view -H $file | \
          sed -e 's/SN:1/SN:chr1/' | sed -e 's/SN:2/SN:chr2/' | \
          sed -e 's/SN:3/SN:chr3/' | sed -e 's/SN:4/SN:chr4/' | \
          sed -e 's/SN:5/SN:chr5/' | sed -e 's/SN:6/SN:chr6/' | \
          sed -e 's/SN:7/SN:chr7/' | sed -e 's/SN:8/SN:chr8/' | \
          sed -e 's/SN:9/SN:chr9/' | sed -e 's/SN:10/SN:chr10/' | \
          sed -e 's/SN:11/SN:chr11/' | sed -e 's/SN:12/SN:chr12/' | \
          sed -e 's/SN:13/SN:chr13/' | sed -e 's/SN:14/SN:chr14/' | \
          sed -e 's/SN:15/SN:chr15/' | sed -e 's/SN:16/SN:chr16/' | \
          sed -e 's/SN:17/SN:chr17/' | sed -e 's/SN:18/SN:chr18/' | \
          sed -e 's/SN:19/SN:chr19/' | sed -e 's/SN:20/SN:chr20/' | \
          sed -e 's/SN:21/SN:chr21/' | sed -e 's/SN:22/SN:chr22/' | \
          sed -e 's/SN:X/SN:chrX/' | sed -e 's/SN:Y/SN:chrY/' | \
          sed -e 's/SN:MT/SN:chrM/' | samtools reheader - $file > ${filename}_chr.bam
    done