How to split SAM file to different chromosomes
I have a SAM file which I now want to split into different chromosome from chromosome chr1..chr20 as follows.
/home/SpliceGraph/Human/chr1.sam
/home/SpliceGraph/Human/chr2.sam/
/home/SpliceGraph/Human/chr3.sam/
/home/SpliceGraph/Human/chr4.sam/
/home/SpliceGraph/Human/chr5.sam
Which command can I use to do this?
• 2,207 views
•
link
1 answer
## Convert SAM to BAM and index it:
samtools view -o out.bam in.sam
samtools index out.bam
## Extract chromsosome names:
samtools idxstats out.bam | cut -f1 | grep -v '*' > chr.names
## Split bam file with w while loop
while read p
do
samtools view -o out_${p}.bam out.bam ${p}
done < chr.names
If you really want SAM instead of BAM files then use samtools view -ho out_${p}.sam out.bam ${p}.
Given you have the resources you can of course use something like GNU parallel instead of a loop to make it more efficient.
• 0 views
•
link
Log in to answer this question.
How To Split A Bam File By Chromosome
The following code:
samtools idxstats out.bam | cut -f1 | grep -v '*' > chr.namesis not giving the list of chromosomes. It give something like this:Those are the chromosome (reference) names that are present in your BAM file.
Please use
ADD COMMENT/ADD REPLYwhen responding to existing posts to keep threads logically organized.SUBMIT ANSWERis for new answers to original question.Please use
ADD COMMENTto reply to answers. It gives you the names that were in the fasta file that you mapped against. The command itself is correct. If you want different names then align against a fasta file that contains the names you would like.