This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Convert Part Of A Bam File To Sam

Is it possible to work on a part of a BAM file?

Since these files are huge, I want to extract 1MB of the BAM file and convert it to SAM.

My main intention is to extract a meaningful part from the BAM file and then convert it to SAM instead of converting the whole BAM file to SAM.

Thanks in advance for your answers.

bam sam

2 answers

It would help if you gave an idea of what you are trying to do with the BAM file. To extract a portion of the BAM file you could try something like

samtools view -h aln.sorted.bam chr2:20,100,000-20,200,000 > out.sam

to get reads from a region of the bam file. Or to get a certain number of reads you may try

samtools view aln.sorted.bam chr2:20,100,000-20,200,000 | head -20000 | samtools view -t ref.fa.fai - > out.sam

where ref.fa.fai is a tab delimited file generated using samtools faidx command. The above command will pick 20000 reads from the selected region.

Just to complete Farhat answer: when using "samtools view" instead of redirecting output with ">" (std output) you should better use the "-o FILE Output file" option.

Log in to answer this question.