Convert Part Of A Bam File To Sam
2
3
Entering edit mode
12.9 years ago
User 9906 ▴ 50

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 • 4.0k views
ADD COMMENT
9
Entering edit mode
12.9 years ago
Farhat ★ 2.9k

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.

ADD COMMENT
2
Entering edit mode
12.9 years ago
Pascal ★ 1.5k

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.

ADD COMMENT

Login before adding your answer.

Traffic: 2074 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6