Question: Truncating Bam Files With Samtools
gravatar for User 9996
8.0 years ago by
User 9996800
User 9996800 wrote:

does anyone know a quick uinx command to take the first N alignments from a BAM file? I tried viewing it as SAM, with headers, taking first N lines and then piping back to samtools but it fails:

samtools view -h big_bam -b | head -n 1000 | samtools view - -h -b > small_bam

it yields the error:

[main_samview] truncated file.


ADD COMMENTlink written 8.0 years ago by User 9996800

I think your -b option in the first view is the problem - this tells the output to be binary, you want plain text. Kill that as GWW did below.

ADD REPLYlink written 8.0 years ago by Caddymob960
gravatar for Gww
8.0 years ago by
Gww2.7k wrote:

This worked for me:

samtools view -h big.bam | head -n 1000 | samtools view -bS - > little.bam

However, this won't give you exactly 1000 records since the samtools header file will take up a few lines:

samtools view little.bam | wc -l



Therefore, I recommend you first check how many lines your header is with

samtools view -H big.bam | wc -l

Then add that to the number of records you want to have in your file. You could probably jam it all together in one big bash command but it's easier to understand what's going on this way.

ADD COMMENTlink written 8.0 years ago by Gww2.7k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1603 users visited in the last hour