This is a test version of Biostars. For the public version, visit https://www.biostars.org.
File write failed (wrong size) when filtering a bam file with interset

I have a WES BAM file and I want to filter this using a BED file

 bedtools intersect -abam NA12878.bam -b BRCA1-expanded.bed  > ./NA_limited.bam

And I got this error

 E::bgzf_flush] File write failed (wrong size)
terminate called after throwing an instance of 'std::runtime_error'
  what():  can't write alignment record
Aborted (core dumped)
bedtools

Can you check if you have write permissions in the folder where you are writing the output?

drwxr-xr-x  2 mdb1c20 af 4096 Apr  7 20:38 bayes_NA

It runs for a while and the output file is created but a few minutes after starting running, the error occurs.

1 answer

It probably runs out of memory. Anyway, don't use bedtools here, there is no need for finding intersections. You can simply use samtools view -L where -L accepts that bed file to only return alignments overlapping it. That requires a indexed bam file.

Thanks for your answer. I have followed your suggestion and seems that worked. However, if later I want to call variants from the output generated after executing samtools view, it seems that the SQ lines are not present. So I cannot sort the reads, neither create an index and then use a variant caller. Any suggestion to do this?

You have to include the options for the header of course. -h is you output sam, ot -b for bam, read the manuals of tools you use. Probably --fetch-pairs makes sense as well.

Log in to answer this question.