<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>

CC BY-SA 3.0
2018-02-28T14:10:01.030Z
2018-02-28T14:10:01.030Z
user172818
<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>

CC BY-SA 3.0
2018-02-28T10:49:35.237Z
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.
Konrad Rudolph
CC BY-SA 3.0
2018-02-28T11:03:29.523Z
@KonradRudolph Can I use it externally properly? Without hacking?
SmallChess
CC BY-SA 3.0
2018-02-28T11:13:50.893Z
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.
Konrad Rudolph
CC BY-SA 3.0
2018-02-28T11:14:46.537Z
@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.
SmallChess
<c++><htslib>
How to use C++ htslib to read VCF contig name and size?
# 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=
##contig=

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**: @KonradRudolph 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**: @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.  

---

## 1 answers

### Answer by username_2 on 2018-02-28. Score: 4 | Accepted answer

#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 \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.


---

