Truncating Bam Files With Samtools
1
2
Entering edit mode
13.0 years ago
User 9996 ▴ 840

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.

samtools sam next-gen sequencing sequence • 18k views
ADD COMMENT
0
Entering edit mode

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 REPLY
21
Entering edit mode
13.0 years ago
Gww ★ 2.7k

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 COMMENT

Login before adding your answer.

Traffic: 1946 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