Question: Truncating Bam Files With Samtools
2
gravatar for User 9996
7.3 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.

thanks.

ADD COMMENTlink written 7.3 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 7.3 years ago by Caddymob950
14
gravatar for Gww
7.3 years ago by
Gww2.6k
Canada
Gww2.6k 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

Output:

991

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 7.3 years ago by Gww2.6k
Please log in to add an answer.

Help
Access

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