]> git.donarmstrong.com Git - samtools.git/blobdiff - bcftools/vcf.c
* write a little more VCF header
[samtools.git] / bcftools / vcf.c
index 50a73566a6d6e45dca212e909ca36d10580dcd0e..81c7061c9cbc4462ade1d36825100b5b760c5975 100644 (file)
@@ -93,9 +93,22 @@ int vcf_close(bcf_t *bp)
 int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h)
 {
        vcf_t *v = (vcf_t*)bp->v;
-       int i;
+       int i, has_ref = 0, has_ver = 0;
        if (!bp->is_vcf) return bcf_hdr_write(bp, h);
-       if (h->l_txt > 0) fwrite(h->txt, 1, h->l_txt - 1, v->fpout);
+       if (h->l_txt > 0) {
+               if (strstr(h->txt, "##fileformat=")) has_ver = 1;
+               if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.0\n");
+               fwrite(h->txt, 1, h->l_txt - 1, v->fpout);
+               if (strstr(h->txt, "##SQ=")) has_ref = 1;
+       }
+       if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.0\n");
+       if (!has_ref) {
+               fprintf(v->fpout, "##SQ=");
+               for (i = 0; i < h->n_ref; ++i) {
+                       fprintf(v->fpout, "%s", h->ns[i]);
+                       fputc(i == h->n_ref - 1? '\n' : ',', v->fpout);
+               }
+       }
        fprintf(v->fpout, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT");
        for (i = 0; i < h->n_smpl; ++i)
                fprintf(v->fpout, "\t%s", h->sns[i]);
@@ -132,7 +145,7 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
                        int tid = bcf_str2id(v->refhash, p);
                        if (tid < 0) {
                                tid = bcf_str2id_add(v->refhash, p);
-                               kputs(p, &rn);
+                               kputs(p, &rn); kputc('\0', &rn);
                                sync = 1;
                        }
                        b->tid = tid;
@@ -156,6 +169,15 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
                                        int x = strtol(q, &q, 10);
                                        if (x > 0xffff) x = 0xffff;
                                        ((uint16_t*)b->gi[i].data)[k-9] = x;
+                               } else if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
+                                       int x, j;
+                                       uint8_t *data = (uint8_t*)b->gi[i].data;
+                                       for (j = 0; j < b->gi[i].len; ++j) {
+                                               x = strtol(q, &q, 10);
+                                               if (x > 255) x = 255;
+                                               data[i * b->gi[i].len + j] = x;
+                                               ++q;
+                                       }
                                }
                        }
                }