]> git.donarmstrong.com Git - samtools.git/blob - bcftools/vcf.c
* fixed a bug in kstrtok@kstring.c
[samtools.git] / bcftools / vcf.c
1 #include <zlib.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "bcf.h"
6 #include "kstring.h"
7 #include "kseq.h"
8 KSTREAM_INIT(gzFile, gzread, 4096)
9
10 typedef struct {
11         gzFile fp;
12         FILE *fpout;
13         kstream_t *ks;
14         void *refhash;
15         kstring_t line;
16         int max_ref;
17 } vcf_t;
18
19 bcf_hdr_t *vcf_hdr_read(bcf_t *bp)
20 {
21         kstring_t meta, smpl;
22         int dret;
23         vcf_t *v;
24         bcf_hdr_t *h;
25         if (!bp->is_vcf) return bcf_hdr_read(bp);
26         h = calloc(1, sizeof(bcf_hdr_t));
27         v = (vcf_t*)bp->v;
28         v->line.l = 0;
29         memset(&meta, 0, sizeof(kstring_t));
30         memset(&smpl, 0, sizeof(kstring_t));
31         while (ks_getuntil(v->ks, '\n', &v->line, &dret) >= 0) {
32                 if (v->line.l < 2) continue;
33                 if (v->line.s[0] != '#') return 0; // no sample line
34                 if (v->line.s[0] == '#' && v->line.s[1] == '#') {
35                         kputsn(v->line.s, v->line.l, &meta); kputc('\n', &meta);
36                 } else if (v->line.s[0] == '#') {
37                         int k;
38                         ks_tokaux_t aux;
39                         char *p;
40                         for (p = kstrtok(v->line.s, "\t\n", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
41                                 if (k >= 9) {
42                                         kputsn(p, aux.p - p, &smpl);
43                                         kputc('\0', &smpl);
44                                 }
45                         }
46                         break;
47                 }
48         }
49         kputc('\0', &meta);
50         h->name = 0;
51         h->sname = smpl.s; h->l_smpl = smpl.l;
52         h->txt = meta.s; h->l_txt = meta.l;
53         bcf_hdr_sync(h);
54         return h;
55 }
56
57 bcf_t *vcf_open(const char *fn, const char *mode)
58 {
59         bcf_t *bp;
60         vcf_t *v;
61         if (strchr(mode, 'b')) return bcf_open(fn, mode);
62         bp = calloc(1, sizeof(bcf_t));
63         v = calloc(1, sizeof(vcf_t));
64         bp->is_vcf = 1;
65         bp->v = v;
66         v->refhash = bcf_str2id_init();
67         if (strchr(mode, 'r')) {
68                 v->fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
69                 v->ks = ks_init(v->fp);
70         } else if (strchr(mode, 'w'))
71                 v->fpout = strcmp(fn, "-")? fopen(fn, "w") : stdout;
72         return bp;
73 }
74
75 int vcf_close(bcf_t *bp)
76 {
77         vcf_t *v;
78         if (bp == 0) return -1;
79         if (!bp->is_vcf) return bcf_close(bp);
80         v = (vcf_t*)bp->v;
81         if (v->fp) {
82                 ks_destroy(v->ks);
83                 gzclose(v->fp);
84         }
85         if (v->fpout) fclose(v->fpout);
86         free(v->line.s);
87         bcf_str2id_destroy(v->refhash);
88         free(v);
89         free(bp);
90         return 0;
91 }
92
93 int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h)
94 {
95         vcf_t *v = (vcf_t*)bp->v;
96         int i;
97         if (!bp->is_vcf) return bcf_hdr_write(bp, h);
98         if (h->l_txt > 0) fwrite(h->txt, 1, h->l_txt - 1, v->fpout);
99         fprintf(v->fpout, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT");
100         for (i = 0; i < h->n_smpl; ++i)
101                 fprintf(v->fpout, "\t%s", h->sns[i]);
102         fputc('\n', v->fpout);
103         return 0;
104 }
105
106 int vcf_write(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
107 {
108         vcf_t *v = (vcf_t*)bp->v;
109         extern void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s);
110         if (!bp->is_vcf) return bcf_write(bp, h, b);
111         bcf_fmt_core(h, b, &v->line);
112         fwrite(v->line.s, 1, v->line.l, v->fpout);
113         fputc('\n', v->fpout);
114         return v->line.l + 1;
115 }
116
117 int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
118 {
119         int dret, k, i, sync = 0;
120         vcf_t *v = (vcf_t*)bp->v;
121         char *p, *q;
122         kstring_t str, rn;
123         ks_tokaux_t aux, a2;
124         if (!bp->is_vcf) return bcf_read(bp, h, b);
125         v->line.l = 0;
126         str.l = 0; str.m = b->m_str; str.s = b->str;
127         rn.l = rn.m = h->l_nm; rn.s = h->name;
128         if (ks_getuntil(v->ks, '\n', &v->line, &dret) < 0) return -1;
129         for (p = kstrtok(v->line.s, "\t", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
130                 *(char*)aux.p = 0;
131                 if (k == 0) { // ref
132                         int tid = bcf_str2id(v->refhash, p);
133                         if (tid < 0) {
134                                 tid = bcf_str2id_add(v->refhash, p);
135                                 kputs(p, &rn);
136                                 sync = 1;
137                         }
138                         b->tid = tid;
139                 } else if (k == 1) { // pos
140                         b->pos = atoi(p);
141                 } else if (k == 5) { // qual
142                         b->qual = (p[0] >= '0' && p[0] <= '9')? atoi(p) : 0;
143                 } else if (k <= 8) { // variable length strings
144                         kputs(p, &str); kputc('\0', &str);
145                         b->l_str = str.l; b->m_str = str.m; b->str = str.s;
146                         if (k == 8) bcf_sync(h->n_smpl, b);
147                 } else {
148                         for (q = kstrtok(p, ":", &a2), i = 0; q && i < b->n_gi; q = kstrtok(0, 0, &a2), ++i) {
149                                 if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
150                                         ((uint8_t*)b->gi[i].data)[k-9] = (q[0] - '0')<<3 | (q[2] - '0') | (q[1] == '/'? 0 : 1) << 6;
151                                 } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
152                                         int x = strtol(q, &q, 10);
153                                         if (x > 255) x = 255;
154                                         ((uint8_t*)b->gi[i].data)[k-9] = x;
155                                 } else if (b->gi[i].fmt == bcf_str2int("DP", 2)) {
156                                         int x = strtol(q, &q, 10);
157                                         if (x > 0xffff) x = 0xffff;
158                                         ((uint16_t*)b->gi[i].data)[k-9] = x;
159                                 }
160                         }
161                 }
162         }
163         h->l_nm = rn.l; h->name = rn.s;
164         if (sync) bcf_hdr_sync(h);
165         return v->line.l + 1;
166 }