This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools view - F 4 removes sam header and truncates file??

Hi guys,

Question, I have a couple of sam files that I got from aligning with Bowtie2. I am trying to sort through them to remove the usual stuff (multi-mapping etc etc) after reading posts and guides etc etc. So far I've been successful in taking my sam files, running sed '/XS:/d' algn.sam > mappedonce.sam and getting rid of all those with an XS flag. When I then run the following :-

samtools view -c -f 4 mappedonce.sam
749598

samtools view -c -F 4 mappedonce.sam
12116420

I can see that I have 749598 reads that are unaligned which I want to remove. So I ran this as per posts on biostars

samtools view -F 4 -o mapped1_mapped.sam mappedonce.sam

However, when I then take this file after running the -F 4 filter, if I try and run anything else such as flagstat or even the samtools view -c -F 4 mappedonce.sam again, I get the following error:

[E::sam_parse1] missing SAM header
[W::sam_read1] parse error at line 1
[main_samview] truncated file.

So what is samtools doing to my file? Is there a reason why samtools would remove a header line and truncate my file? Am I doing something wrong? Consequently, if anyone has any non-samtools solutions to remove these 749598 reads I'd be open to that!

rna-seq alignment

1 answer

You need the -h option in samtools view so the header is also printed out.

Log in to answer this question.