This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Header error while converting sam file to bam

Hi,

I am trying to convert sam files to bam file after bowtie aligmnet.

I checked previous posts but still have the same error.

I have miRNA-Seq data.

the codes from the script I used are given below: For alignment:

bowtie -n 2 -l 32 -f --norc --best --strata -m 1 --threads 16 $HUMANMIRBASEINDEX/hsa-index $OUTPUTDIR/$i/$i".min18max30L.fasta" --un $OUTPUTDIR/$i/$i"-maturemiRNA-unalignedReads-bowtie1-beststratam1.fasta" -S $OUTPUTDIR/$i/$i"-maturemiRNA-aligned-bowtie1-beststratam1.sam" 2> $OUTPUTDIR/$i/$i"-bowtie-maturemiRNA-beststratam1.log"

to convert sam file to bam files I tried first:

samtools sort $OUTPUTDIR/$i/$i"-maturemiRNA-aligned-bowtie1-beststratam1.sam" > $OUTPUTDIR/$i/$i"-maturemiRNA-aligned-bowtie1-beststratam1.bam"

secondly I tried index ref file with: samtools faidx ref.fa samtools view -bt ref.fa.fai -o aln.bam aln.sam

And here is the error that I always take:

 [E::sam_hrecs_error] Header line does not have a two character key at line 2659: "@A00821:815:HKYLJDRXY:1:2101:8567:1235        4       *    0       0  TCCCTGGTGGTCTAGTGGTTAGGATTCGGCGCTC       IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII      XM:i:0"
    samtools view: failed to add PG line to the header
errorconvertsamtobam samtools bowtie samtoolsheadererror

To convert SAM to BAM you have to specify that samtools should output BAM format using the -b flag. Have you tried:

samtools view -b input.sam -o output.bam

You can also convert to bam format coming out of bowtie by piping directly using something like:

bowtie -f -S index input.fasta | samtools view -b -o output.bam -

Get the simplest example to work before building up your complete pipeline.

Hi,

thank you for your answer. Yes I already tried these codes. But my problem is about header of sam file.

Even if I try to write directly bam files, the error is same.

[E::sam_hrecs_error] Header line does not have a two character key at line 2659: "@A00821:815:HKYLJDRXY:1:2101:6533:1031        4       *       0       0  TNAGTGCACTACAGAACTTTGT   IIIIIIIIIIIIIIIIIIIIII  XM:i:0"
samtools view: failed to add PG line to the header 

I also tried to add ID while running bowtie as follow:

bowtie -f index data.fa.gz --un data.fastq  -S  --sam-RG ID:A00821 data-maturemiRNA-aligned-bowtie1.sam

But still having the same header error at different line

[E::sam_hrecs_error] Header line does not have a two character key at line 2660: "@A00821:815:HKYLJDRXY:1:2101:6533:1031        4       *       0       0  TNAGTGCACTACAGAACTTTGT   IIIIIIIIIIIIIIIIIIIIII  XM:i:0"
samtools view: failed to add PG line to the header

1 answer

Read names in SAM are not allowed to have @ symbols in them. In the SAM spec the definition of a read name is

QNAME String [!-?A-~]{1,254}

Your read name of @A00821:815:HKYLJDRXY:1:2101:8567:1235 starts with an @ and samtools is treating it as a malformed part of the header (header entries always start with an @ and a two letter key e.g. @SQ).

You need read names that do not contain (or at least start with) an @.

Log in to answer this question.