This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How Can I Split Bam Into Chromosome (In A Loop) Using Samtools?

i want to split my bam file into chromosome and then index it.

split bam chromosome

3 answers

Depending on your chromosomes, something like:

for chrom in `seq 1 22` X Y
do

    samtools view -bh $BAM chr${chrom} | samtools sort - chr${chrom}
    samtools index chr${chrom}.bam
done

do we need to sort bam everytime we split it...or are we doing it to be on the safe side? Thanks for your response.

if your input bam is sorted, then you don't need to resort.

bamtools has a "split" command for exactly this purpose.

Cool. I didn't know that. It's a bit sparse on docs

You can use this tool to do exactly that.

http://code.google.com/p/chrom-bed/

Log in to answer this question.