This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Samtools merge bam issue with header tags

Hi,

I have a few bam files after mapping using Bowtie2.

I want to extract the multi-mapped reads from these bam files. For doing so I used the following command:

samtools view -h  $fq | grep -E "^\@|XS:i" | grep "NM:i:0" > multimapped_1.bam

To add the header I used the following command, I ran the following commands:

samtools view -H bamfile > $header_bam
samtools reheader $header_bam multimapped_1.bam

Now I wanted to merge the bam files together. However, while merging I get the following error:

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

It seems the header is not added.

I also tried the following command:

samtools view -h  $fq | grep -E "^\@|XS:i" | grep "NM:i:0" | samtools view -bS - > multimapped_1.bam

However, this produced a bam file that was less than 77 kb in size.

Looking forward to any suggestions.

Thank you!

samtools

1 answer

 grep "NM:i:0"

removes the SAM header.

you'd better use samtools view -e ...

  -e, --expr STR             ...match the filter expression STR

Thank you Pierre! Should i split the single command into two and i use

samtools view -e "NM:i:0" bamfile

Is this correct?

Hi again. I do not see -e option in samtools v1.9. Thanks!

I have now updated to version v1.17. Thanks for notifying that.

Thanks Pierre! Figured it out!

Log in to answer this question.