Answers.Body: <pre class="lang-c prettyprint-override"><code>#include "htslib/vcf.h"
  
  int main(int argc, char *argv[])
  {
      htsFile *fp;
      bcf_hdr_t *hdr;
      bcf_idpair_t *ctg;
      int i;
      if (argc == 1) {
          fprintf(stderr, "Usage: print-ctg &lt;in.vcf&gt;\n");
          return 1;
      }
      fp = vcf_open(argv[1], "r");
      hdr = vcf_hdr_read(fp);
      ctg = hdr-&gt;id[BCF_DT_CTG];
      for (i = 0; i &lt; hdr-&gt;n[BCF_DT_CTG]; ++i)
          printf("%s\t%d\n", ctg[i].key, ctg[i].val-&gt;info[0]);
      bcf_hdr_destroy(hdr);
      vcf_close(fp);
      return 0;
  }
  </code></pre>
  
  <p>On stability: this use has been in htslib forever. In general, functions/structs/variables in the public headers are meant to be stable. However, there is no guarantee that future versions will always keep the same APIs.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-28T14:10:01.030Z
Answers.LastActivityDate: 2018-02-28T14:10:01.030Z
Answers.UserDisplayName: user172818
Body: <p>A typical VCF file has:</p>
  
  <pre><code>##contig=&lt;ID=chr1,length=248956422&gt;
  ##contig=&lt;ID=chr2,length=242193529&gt;
  </code></pre>
  
  <p>I would like to use <code>htslib</code> in C++ to read it. My attempt:</p>
  
  <pre><code>htsFile *fp = bcf_open("my.vcf", "r");
  bcf_hdr_t *hdr = bcf_hdr_read(fp); 
  </code></pre>
  
  <p>In <a href="https://github.com/samtools/htslib/blob/develop/htslib/vcf.h" rel="nofollow noreferrer">https://github.com/samtools/htslib/blob/develop/htslib/vcf.h</a>, I'm not able to find a function that can do that for me.</p>
  
  <p>How to read <code>chr1</code> and <code>248956422</code> in C++?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T10:49:35.237Z
Comments.Text: As far as I can tell, the (internal) function `bcf_hdr_parse_line` does that. It returns an object x of type `bcf_hrec_t*`, for which `strcmp(x->key, "contig") == 0)` for your lines. You can then access the `x->keys` and `x->vals`. Untested.
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T11:03:29.523Z
Comments.Text: @KonradRudolph Can I use it externally properly? Without hacking?
Comments.UserDisplayName: SmallChess
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T11:13:50.893Z
Comments.Text: Depends on what you mean by “properly”. Since the API isn’t properly documented anyway, the distinction between external and internal functions is somewhat fluid. It might break with future versions of htslib, if that’s what you’re concerned about.
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T11:14:46.537Z
Comments.Text: @KonradRudolph Yeah. Breaking with future versions is my concern. If I see don't any other alternative, I might go with that or just parse the file myself.
Comments.UserDisplayName: SmallChess
Tags: <c++><htslib>
Title: How to use C++ htslib to read VCF contig name and size?
ThreadText: # How to use C++ htslib to read VCF contig name and size?
  
  (Asked by: username_0 on 2018-02-28)
  
  A typical VCF file has:
  
  ```
  ##contig=<ID=chr1,length=248956422>
  ##contig=<ID=chr2,length=242193529>
  
  ```
  
  I would like to use `htslib` in C++ to read it. My attempt:
  
  ```
  htsFile *fp = bcf_open("my.vcf", "r");
  bcf_hdr_t *hdr = bcf_hdr_read(fp); 
  
  ```
  
  In <https://github.com/samtools/htslib/blob/develop/htslib/vcf.h>, I'm not able to find a function that can do that for me.
  
  How to read `chr1` and `248956422` in C++?
  
  **Tags:** c++, htslib
  
  > **Comments:**
  > **username_1**: As far as I can tell, the (internal) function `bcf_hdr_parse_line` does that. It returns an object x of type `bcf_hrec_t*`, for which `strcmp(x->key, "contig") == 0)` for your lines. You can then access the `x->keys` and `x->vals`. Untested.  
  > **username_0**: @username_1 Can I use it externally properly? Without hacking?  
  > **username_1**: Depends on what you mean by “properly”. Since the API isn’t properly documented anyway, the distinction between external and internal functions is somewhat fluid. It might break with future versions of htslib, if that’s what you’re concerned about.  
  > **username_0**: @username_1 Yeah. Breaking with future versions is my concern. If I see don't any other alternative, I might go with that or just parse the file myself.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-02-28. Score: 4 | Accepted answer
  
  ```c
  #include "htslib/vcf.h"
  
  int main(int argc, char *argv[])
  {
      htsFile *fp;
      bcf_hdr_t *hdr;
      bcf_idpair_t *ctg;
      int i;
      if (argc == 1) {
          fprintf(stderr, "Usage: print-ctg <in.vcf>\n");
          return 1;
      }
      fp = vcf_open(argv[1], "r");
      hdr = vcf_hdr_read(fp);
      ctg = hdr->id[BCF_DT_CTG];
      for (i = 0; i < hdr->n[BCF_DT_CTG]; ++i)
          printf("%s\t%d\n", ctg[i].key, ctg[i].val->info[0]);
      bcf_hdr_destroy(hdr);
      vcf_close(fp);
      return 0;
  }
  
  ```
  
  On stability: this use has been in htslib forever. In general, functions/structs/variables in the public headers are meant to be stable. However, there is no guarantee that future versions will always keep the same APIs.
  
  
  ---
  
  