This is a test version of Biostars. For the public version, visit https://www.biostars.org.
BAM file header after splitting paired end reads

Hi,

I am processing BAM files using pysam as part of the HiFive set of tools for analysing Hi-C data. The input required are two BAM files with each one representing the forward and the reverse reads respectively from an alignment of paired-end reads.

In order to split the alignment BAM file into forward and reverse reads, I used:

samtools view -f 0x40 file_chr1.bam > file_chr1_1.bam
samtools view -f 0x80 file_chr1.bam > file_chr1_2.bam

Then discovered that the new BAM files do not have headers so I added the -h tag to the commands:

samtools view -h -f 0x40 file_chr1.bam > file_chr1_1.bam
samtools view -h -f 0x80 file_chr1.bam > file_chr1_2.bam

Now I am getting the following error:

for I in range(len(input.header['SQ'])):
  File "pysam/calignmentfile.pyx", line 1486, in pysam.calignmentfile.AlignmentFile.header.__get__ (pysam/calignmentfile.c:16469)
ValueError: malformatted header: no ':' in field

Any ideas on how to fix this?

Thank you very much

paired-end hifive sam samtools bam

Can you show me the header of the bam file?

samtools view -H bamFile.bam

1 answer

You have created SAM files, not BAM files. Use -b to force binary.

Thank you very much. I used -b and made sure it is a BAM file. Even so, it still would not let me read the file and output "malformatted header".

I then used samtools reheader and removed chromosome fragments e.g. chr_Un from the header.

HiFive still does not recognise the pair of reads and outputs "data is not valid" but at least pysam is reading the BAM files.

Log in to answer this question.