This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Samtools Adding Head reduces file size

Hi all,

I'm trying to convert my SAM to BAM files using samtools, to subsequently view them in IGV. When I do

samtools faidx reference.fasta

samtools view -bt reference.fasta.fai file1.sam > file1.bam

I get the error

[E::sam_parse1] missing SAM header 
[W::sam_read1] parse error at line 1

However, when I'm trying to add the header using the following code, my bam file size reduces massively (from 127mb to 27mb).

samtools view -bt reference.fasta.fai file1.sam > file1.bam

What's going on here and can someone give me a hint on what to do?

Many thanks guys!

samtools bam sam python igv

You mean a 127Mb SAM to a 27Mb BAM? BAM is a gzipped (compressed) SAM file. More or less.

IF I dont add '-bt reference.fasta.fai' my BAM file becomes 127mb (similar to SAM), whereas it becomes 27mb when adding that option.

You need the -b option to output a BAM file - hence when you have -b you are getting a compressed binary file of only 27MB.

When you do not have the -b option you are basically outputting in the SAM standard text format, which is uncompressed and gives you the 127MB.

This will correctly give you a SAM to BAM conversion

samtools view -bt reference.fasta.fai file1.sam > file1.bam

This will create a file called file1.bam but will be in SAM format not BAM format (just because you call a file .bam doesn't make it a BAM file)

samtools view file1.sam > file1.bam

0 answers

No answers yet.

Log in to answer this question.