Samtools merge bam issue with header tags
1
0
Entering edit mode
17 months ago
sam ▴ 30

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 • 987 views
ADD COMMENT
2
Entering edit mode
17 months ago
 grep "NM:i:0"

removes the SAM header.

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

  -e, --expr STR             ...match the filter expression STR
ADD COMMENT
0
Entering edit mode

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

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

Is this correct?

ADD REPLY
0
Entering edit mode

no, please read the manual.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

your version of samtools is just too old.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Thanks Pierre! Figured it out!

ADD REPLY

Login before adding your answer.

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