Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • granzanimo
    Junior Member
    • Apr 2014
    • 6

    How to build a BGZF

    Hi,

    I would know how to build correctly a BGZF header using bgzf.c or bam.c functions.
    I want to generate a BAM file using BGZF but I always get errors.

    I'm trying this for 1 week now, but no matter how I try to build the header, I always get this error.

    Code:
    [bam_header_read] EOF marker is absent. The input is probably truncated.
    [bam_header_read] invalid BAM binary header (this is not a BAM file).
  • maubp
    Peter (Biopython etc)
    • Jul 2009
    • 1544

    #2
    Are you familiar with hexdump? It is very useful for actually looking at the raw file and (for example) checking the final 28 bytes of the BAM/BGZF file to see if there is an EOF marker.

    Comment

    • granzanimo
      Junior Member
      • Apr 2014
      • 6

      #3
      No i'm not, I will give a try. But when i checked with gedit, I didn't saw this marker. I only saw the beginning marker (BAM\001)

      Comment

      • dpryan
        Devon Ryan
        • Jul 2011
        • 3478

        #4
        Have you looked at the bam_header_write function? Remembering that bam_write() is just bgzf_write(), if you want to avoid the bam_* functions for some reason. Have a look at bgzf_close() for how the EOF is added (it's not explicitly mentioned, but it's the empty block that's added there).

        Comment

        • maubp
          Peter (Biopython etc)
          • Jul 2009
          • 1544

          #5
          Originally posted by granzanimo View Post
          No i'm not, I will give a try.
          I find using hexdump -C most helpful, often piped into head or tail.
          Originally posted by granzanimo View Post
          But when i checked with gedit, I didn't saw this marker. I only saw the beginning marker (BAM\001)
          That is a problem - the 'naked' BAM files starts like that once you have removed the BGZF (GZIP) compression. You should see a (special) GZIP header at the start of the file.

          Comment

          • dpryan
            Devon Ryan
            • Jul 2011
            • 3478

            #6
            @granzanimo: In case you're still not getting things to work, this would be the absolute minimum BAM file:

            Code:
            #include "bam.h"
            int main(int argc, char *argv[]) {
                BGZF *fp = bgzf_open("foo.bam", "w");
                bam_header_t *header = bam_header_init();
                bam_header_write(fp,header);
                bgzf_close(fp);
                bam_header_destroy(header);
                return(0);
            }
            You could instead have a bit of a header with something like:
            Code:
            #include "bam.h"
            int main(int argc, char *argv[]) {
                BGZF *fp = bgzf_open("foo.bam", "w");
                bam_header_t *header = bam_header_init();
                char *text = malloc(sizeof(char) * 128);
            
                //Add a chromosome to the header
                header->n_targets=1;
                header->target_name = malloc(sizeof(char *));
                header->target_name[0] = malloc(5*sizeof(char));
                header->target_name[0] = strcpy(header->target_name[0], "chr1");
                header->target_len = malloc(sizeof(uint32_t));
                header->target_len[0] = 1000;
                //The @HD line
                text = strcpy(text, "@HD\tVN:1.0\tSO:unsorted\n");
                header->text = text;
                header->l_text = strlen(text);
            
                bam_header_write(fp,header);
                bgzf_close(fp);
                bam_header_destroy(header);
                return(0);
            }
            You can also add the @SQ lines to header->text, though it's not strictly required.

            Comment

            • darked89
              Member
              • Jun 2009
              • 38

              #7
              Originally posted by dpryan View Post
              @granzanimo: In case you're still not getting things to work, this would be the absolute minimum BAM file:

              Code:
              #include "bam.h"
              int main(int argc, char *argv[]) {
                  BGZF *fp = bgzf_open("foo.bam", "w");
                  bam_header_t *header = bam_header_init();
                  bam_header_write(fp,header);
                  bgzf_close(fp);
                  bam_header_destroy(header);
                  return(0);
              }
              You could instead have a bit of a header with something like:
              Code:
              #include "bam.h"
              int main(int argc, char *argv[]) {
                  BGZF *fp = bgzf_open("foo.bam", "w");
                  bam_header_t *header = bam_header_init();
                  char *text = malloc(sizeof(char) * 128);
              
                  //Add a chromosome to the header
                  header->n_targets=1;
                  header->target_name = malloc(sizeof(char *));
                  header->target_name[0] = malloc(5*sizeof(char));
                  header->target_name[0] = strcpy(header->target_name[0], "chr1");
                  header->target_len = malloc(sizeof(uint32_t));
                  header->target_len[0] = 1000;
                  //The @HD line
                  text = strcpy(text, "@HD\tVN:1.0\tSO:unsorted\n");
                  header->text = text;
                  header->l_text = strlen(text);
              
                  bam_header_write(fp,header);
                  bgzf_close(fp);
                  bam_header_destroy(header);
                  return(0);
              }
              You can also add the @SQ lines to header->text, though it's not strictly required.

              Just a short tip taken from dpryan earlier post, but somehow not popping up in google results on top how to compile the above code snippets:
              Code:
              gcc -Wall -o  mysnippet  mysnippet.c -lbam -lz -lpthread -I/path/2/samtools_src 
              -L//path/2/samtools_src
              Tested with samtools_0.1.19 and gcc 4.9.2

              Hope it helps.

              Darek

              Comment

              • dpryan
                Devon Ryan
                • Jul 2011
                • 3478

                #8
                It's good that you mentioned that! I have the unfortunate habit of assuming that everyone else knows how to compile C code manually...which is understandably not the case.

                Want to convert that code to work with htslib too (it's probably just modifying the included headers)?

                Comment

                • darked89
                  Member
                  • Jun 2009
                  • 38

                  #9
                  Originally posted by dpryan View Post
                  It's good that you mentioned that! I have the unfortunate habit of assuming that everyone else knows how to compile C code manually...which is understandably not the case.

                  Want to convert that code to work with htslib too (it's probably just modifying the included headers)?
                  Well, catching up with htslib is one thing, but going from test BAM with
                  Code:
                  @HD	VN:1.0	SO:unsorted
                  @SQ	SN:chr1	LN:1000
                  towards one where one can:
                  * create BAM with real world header taken from conventionally created BAM file (samtools view -H real.bam > real.header.txt; read the real.header.txt and create a test1.bam)
                  * add even a tiny chunk with 10 unpaired but mapped reads
                  * if possible, add another minimal chunk and get say test3.bam
                  * create bam with a real world header plus two already sorted SAM files taken from 2 different chromosomes from real.bam

                  If for time being these steps are doable only using old branch of samtools, so be it. As long as the resulting BAMs are readable by the htslib, it is not a software archaeology

                  Anyway, many thanks for posting the code. I think it may be worthy to start putting it in say github repo bam_hacks_for_nonC_ppl

                  Darek

                  Comment

                  Latest Articles

                  Collapse

                  • SEQadmin2
                    Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                    by SEQadmin2



                    Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                    ...
                    07-09-2026, 11:10 AM
                  • SEQadmin2
                    Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                    by SEQadmin2



                    Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                    There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                    07-08-2026, 05:17 AM
                  • GATTACAT
                    Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                    by GATTACAT
                    Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                    07-01-2026, 11:43 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, 07-09-2026, 10:04 AM
                  0 responses
                  23 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-08-2026, 10:08 AM
                  0 responses
                  15 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-07-2026, 11:05 AM
                  0 responses
                  33 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-02-2026, 11:08 AM
                  0 responses
                  31 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...