Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • bob-loblaw
    Member
    • Jun 2012
    • 59

    samtools invalid BAM binary header

    Hi all,

    I've already done some googling on this and I haven't been able to find an answer to this incredibly infuriating problem!

    So I have 12 sam files, which I need to convert to bam, then sort, index etc. All 12 were processed in the same way, but samtools is having difficulty with 3 of them, specifically it gives this error when I try to sort

    -bash-3.2$ samtools sort x.bam x.sorted
    [bam_header_read] invalid BAM binary header (this is not a BAM file).
    [bam_sort_core] truncated file. Continue anyway.
    Segmentation fault

    Samtools was used to generate the bam files so it's strange that it can't recognize a file that it's generated itself. I've tried remaking the sam file using samtools view, same problem. I've tried regenerating the sam file (bowtie2) same problem again.

    Anyone got any advice? this has been driving me a bit mad for the past 2 days.
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #2
    Did you create the BAM file without a header? Presumably you did a "samtools view -Sbo x.bam x.sam" instead of "samtools view -hSbo x.bam x.sam" (or something like that). In that case, I would just reheader or recreate the BAM file. As a general rule, try "samtools view" on any file giving you errors like this.

    Comment

    • Richard Finney
      Senior Member
      • Feb 2009
      • 701

      #3
      Code in Heng Li's samtools source file bam.c is this ...

      Code:
             magic_len = bam_read(fp, buf, 4);
              if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) {
                      fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n");
                      return 0;
              }
      This comes after checking for EOF to see if it is truncated. So file is not a valid BAM file. Since 'B','A','M',0x1 is not in the first 4 bytes, it must be compressed.

      Try this

      ls *.bam | awk '{print "echo "$1";od -c -N4 "$1}' | bash

      to see if the bams all have same first few bytes. You should see this ...

      0000000 037 213 \b 004

      Some other checking is find out what size the bam files are.

      Also run this little command to see what "file" program says :

      file *.bam

      You should something like this example ...
      [xxxx@biowulf]$ file *.bam
      98017.bam: gzip compressed data, extra field
      98018.bam: gzip compressed data, extra field
      98029.bam: gzip compressed data, extra field
      98031.bam: gzip compressed data, extra field
      ....

      Comment

      • meetelbeet
        Junior Member
        • Jul 2012
        • 3

        #4
        bam file with no header upon conversion from sam

        Hi everyone
        I have a similar problem. I ran bowtie2 and filtered the sam file to get alignments with only certain fragment lenghts, I did this with awk:
        HTML Code:
        awk '($9>=74 || $9 <=-74) {print}' ctrl-new.sam > noovctrl.sam
        Then I converted the resulting sam file to bam:
        HTML Code:
        samtools view -bhS noovctrl.sam > ctrl-new.bam
        But this file has no header. When I try to look at its 'head' with:
        HTML Code:
        samtools view ctrl-new.bam | head
        I get the error:
        HTML Code:
        [bam_header_read] bgzf_check_EOF: Invalid argument
        [bam_header_read] invalid BAM binary header (this is not a BAM file).
        [main_samview] fail to read the header from "ctrl-new.bam"
        I have tried to cat the header, reheader... and nothing. By the way, earlier I did some other unix manipulations of the same original sam file and was able to convert to bams with no problems, so I don't think the problem is in the alignment file.

        I would appreciate any advice, thanks to all

        Comment

        • swbarnes2
          Senior Member
          • May 2008
          • 910

          #5
          I think the problem is that your awk statement is resulting in a .sam with no header, and I don't think .samtools view generates one, it just keeps the one that should already be there.

          Start with your initial sam with its intact header. Make the header its own file with

          Code:
          samtools view -H original.sam > header.txt
          Then cat that to the top of your awk-generated .sam, and now it should get converted.

          Comment

          Latest Articles

          Collapse

          • SEQadmin2
            Nine Things a Sample Prep Scientist Thinks About Before Sequencing
            by SEQadmin2


            I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

            Here are nine questions we think about, in roughly the order they matter, before...
            06-18-2026, 07:11 AM
          • SEQadmin2
            From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
            by SEQadmin2


            Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


            The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
            ...
            06-02-2026, 10:05 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by SEQadmin2, 06-26-2026, 11:10 AM
          0 responses
          14 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 06-17-2026, 06:09 AM
          0 responses
          48 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 06-09-2026, 11:58 AM
          0 responses
          107 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 06-05-2026, 10:09 AM
          0 responses
          125 views
          0 reactions
          Last Post SEQadmin2  
          Working...