This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Split a sam file by chromosome

Hi! I'm trying to use PRISM program to detect CNVs and the input files have to be .SAM files for each chromosome, I have the .BAM file of each sample but I don't know it it is convenient to convert it to .SAM and the n split it or the contrary.

for file in *_chr.bam; do 
filename=`echo $file | cut -d “.” -f 1`; 
for chrom in `seq 1 22` X Y; do 
samtools view -bh $file chr${chrom} > ${filename}_${chrom}.bam; 
done; 
done
cnvs ngs prism

If you want SAM files the use:

samtools view -h $file chr${chrom} > ${filename}_${chrom}.sam; 

1 answer

While some commands have changed over years this should still be useful: How Can I Split Bam Into Chromosome (In A Loop) Using Samtools?

Sounds like you know what you want and how to get there.

Log in to answer this question.