Answers.Body: <p>see the code in <a href="https://github.com/samtools/htslib/blob/develop/vcf.c#L3104" rel="nofollow noreferrer">https://github.com/samtools/htslib/blob/develop/vcf.c#L3104</a></p>
  <pre><code> bcf_unpack((bcf1_t*)v, BCF_UN_ALL);// if you need genotypes too, might use another constant to avoid decoding all the structure
   (...)
   kputc('\t', s); // ALT
   if (v-&gt;n_allele &gt; 1) {
          for (i = 1; i &lt; v-&gt;n_allele; ++i) {
              if (i &gt; 1) kputc(',', s);
              kputs(v-&gt;d.allele[i], s);
          }
      }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T12:36:18.523Z
Answers.Text: Thanks for reply, i'll try that!.
Answers.UserDisplayName: RAHenriksen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T13:20:28.927Z
Answers.Text: Okay, so as far as i understand based on this function, s is a kstring_t* which you can create using mem "kstring_t *s = (kstring_t*)&test_header->mem;", and then all the information is added to the pointer "s". But how are we then able to print or access the information in "s" ? I can see its a pointer declared with a reference, but then i should be able to print the values with *s. Again thanks for your time.
Answers.UserDisplayName: RAHenriksen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T14:51:16.933Z
Answers.Text: kstring_t is just the implementation of a dynamic string. you could just use ` cout << v->d.allele[i] << endl; `
Answers.UserDisplayName: Pierre
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T14:59:18.073Z
Answers.Text: okay, that was my initial try, i just looked into it, and the problem is my n_allele for some reason is 0. So ill play around with it some more. Thanks again.
Answers.UserDisplayName: RAHenriksen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T11:40:29.370Z
Answers.LastActivityDate: 2020-11-24T11:40:29.370Z
Answers.UserDisplayName: Pierre
Body: <p>I would like to print the alternative allele from a vcf file which i load in with htslib.
  The description of some functions:
  <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></p>
  <p>I have used bcf1_t objects by creating a header and reading the information.</p>
  <pre><code>htsFile *test_vcf = NULL;
  
    // creates header
    bcf_hdr_t *test_header = NULL;
    
    // initialize and allocate bcf1_t object
    bcf1_t *test_record = bcf_init();
  
    test_vcf = vcf_open(&quot;Testfile.vcf&quot;, &quot;r&quot;);
  
    // returning a bcf_hdr_t struct 
    test_header = bcf_hdr_read(test_vcf);
    
    while(bcf_read(test_vcf, test_header, test_record) == 0){
      std::cout &lt;&lt; &quot;chr&quot; &lt;&lt; bcf_hdr_id2name(test_header, test_record-&gt;rid) &lt;&lt; &quot;:&quot; &lt;&lt;test_record-&gt;pos+1 &lt;&lt; std::endl;
    }
    bcf_hdr_destroy(test_header);
    bcf_destroy(test_record); 
    bcf_close(test_vcf);
  </code></pre>
  <p>But im not able to extract the reference or alternative allele information using this structure bcf1_t.</p>
  <p>As far as i can read its perhaps possible with the structure and function:</p>
  <pre><code>bcf_dec_t
  bcf_unpack()
  </code></pre>
  <p>But i am not able to get it to work since all the other functions seems limited to the bcf1_t struct.</p>
  <p>Can anyone help with extracting the REF and ALT sequence?
  Thanks for your time</p>
  
Tags: <vcf><c++><htslib>
Title: How to print alternative allele from VCF with htslib c++
ThreadText: # How to print alternative allele from VCF with htslib c++
  
  (Asked by: username_0 on 2020-11-24)
  
  I would like to print the alternative allele from a vcf file which i load in with htslib.
  The description of some functions:
  <https://github.com/samtools/htslib/blob/develop/htslib/vcf.h>
  
  I have used bcf1\_t objects by creating a header and reading the information.
  
  ```
  htsFile *test_vcf = NULL;
  
    // creates header
    bcf_hdr_t *test_header = NULL;
    
    // initialize and allocate bcf1_t object
    bcf1_t *test_record = bcf_init();
  
    test_vcf = vcf_open("Testfile.vcf", "r");
  
    // returning a bcf_hdr_t struct 
    test_header = bcf_hdr_read(test_vcf);
    
    while(bcf_read(test_vcf, test_header, test_record) == 0){
      std::cout << "chr" << bcf_hdr_id2name(test_header, test_record->rid) << ":" <<test_record->pos+1 << std::endl;
    }
    bcf_hdr_destroy(test_header);
    bcf_destroy(test_record); 
    bcf_close(test_vcf);
  
  ```
  
  But im not able to extract the reference or alternative allele information using this structure bcf1\_t.
  
  As far as i can read its perhaps possible with the structure and function:
  
  ```
  bcf_dec_t
  bcf_unpack()
  
  ```
  
  But i am not able to get it to work since all the other functions seems limited to the bcf1\_t struct.
  
  Can anyone help with extracting the REF and ALT sequence?
  Thanks for your time
  
  **Tags:** vcf, c++, htslib
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-11-24. Score: 1
  
  see the code in <https://github.com/samtools/htslib/blob/develop/vcf.c#L3104>
  
  ```
   bcf_unpack((bcf1_t*)v, BCF_UN_ALL);// if you need genotypes too, might use another constant to avoid decoding all the structure
   (...)
   kputc('\t', s); // ALT
   if (v->n_allele > 1) {
          for (i = 1; i < v->n_allele; ++i) {
              if (i > 1) kputc(',', s);
              kputs(v->d.allele[i], s);
          }
      }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for reply, i'll try that!.  
  > **username_0**: Okay, so as far as i understand based on this function, s is a kstring_t* which you can create using mem "kstring_t *s = (kstring_t*)&test_header->mem;", and then all the information is added to the pointer "s". But how are we then able to print or access the information in "s" ? I can see its a pointer declared with a reference, but then i should be able to print the values with *s. Again thanks for your time.  
  > **username_1**: kstring_t is just the implementation of a dynamic string. you could just use ` cout << v->d.allele[i] << endl; `  
  > **username_0**: okay, that was my initial try, i just looked into it, and the problem is my n_allele for some reason is 0. So ill play around with it some more. Thanks again.  
  
  ---
  
  