Truncating Bam Files With Samtools
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.
• 19,930 views
•
link
1 answer
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.
• 1 views
•
link
Log in to answer this question.
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.