annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/vcf.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <zlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include "kseq.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 KSTREAM_INIT(gzFile, gzread, 4096)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 gzFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 FILE *fpout;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 kstream_t *ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 void *refhash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 kstring_t line;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 int max_ref;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 } vcf_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 bcf_hdr_t *vcf_hdr_read(bcf_t *bp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 kstring_t meta, smpl;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 int dret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 vcf_t *v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 bcf_hdr_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 if (!bp->is_vcf) return bcf_hdr_read(bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 h = calloc(1, sizeof(bcf_hdr_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 v = (vcf_t*)bp->v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 v->line.l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 memset(&meta, 0, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 memset(&smpl, 0, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 while (ks_getuntil(v->ks, '\n', &v->line, &dret) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 if (v->line.l < 2) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 if (v->line.s[0] != '#') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 free(meta.s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 free(smpl.s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 free(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 return 0; // no sample line
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 if (v->line.s[0] == '#' && v->line.s[1] == '#') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 kputsn(v->line.s, v->line.l, &meta); kputc('\n', &meta);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 } else if (v->line.s[0] == '#') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 int k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 ks_tokaux_t aux;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 char *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 for (p = kstrtok(v->line.s, "\t\n", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 if (k >= 9) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 kputsn(p, aux.p - p, &smpl);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 kputc('\0', &smpl);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 kputc('\0', &meta);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 h->name = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 h->sname = smpl.s; h->l_smpl = smpl.l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 h->txt = meta.s; h->l_txt = meta.l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 bcf_hdr_sync(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 return h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 bcf_t *vcf_open(const char *fn, const char *mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 bcf_t *bp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 vcf_t *v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 if (strchr(mode, 'b')) return bcf_open(fn, mode);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 bp = calloc(1, sizeof(bcf_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 v = calloc(1, sizeof(vcf_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 bp->is_vcf = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 bp->v = v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 v->refhash = bcf_str2id_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 if (strchr(mode, 'r')) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 v->fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 v->ks = ks_init(v->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 } else if (strchr(mode, 'w'))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 v->fpout = strcmp(fn, "-")? fopen(fn, "w") : stdout;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 return bp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 int vcf_dictread(bcf_t *bp, bcf_hdr_t *h, const char *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 vcf_t *v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 gzFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 kstream_t *ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 kstring_t s, rn;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 int dret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 if (bp == 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 if (!bp->is_vcf) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 s.l = s.m = 0; s.s = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 rn.m = rn.l = h->l_nm; rn.s = h->name;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 v = (vcf_t*)bp->v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 fp = gzopen(fn, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 ks = ks_init(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 while (ks_getuntil(ks, 0, &s, &dret) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 bcf_str2id_add(v->refhash, strdup(s.s));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 kputs(s.s, &rn); kputc('\0', &rn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 ks_destroy(ks);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 gzclose(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 h->l_nm = rn.l; h->name = rn.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 bcf_hdr_sync(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 free(s.s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 int vcf_close(bcf_t *bp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 vcf_t *v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 if (bp == 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 if (!bp->is_vcf) return bcf_close(bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 v = (vcf_t*)bp->v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 if (v->fp) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 ks_destroy(v->ks);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 gzclose(v->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 if (v->fpout) fclose(v->fpout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 free(v->line.s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 bcf_str2id_thorough_destroy(v->refhash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 free(v);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 free(bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 vcf_t *v = (vcf_t*)bp->v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 int i, has_ver = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 if (!bp->is_vcf) return bcf_hdr_write(bp, h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 if (h->l_txt > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 if (strstr(h->txt, "##fileformat=")) has_ver = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.1\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 fwrite(h->txt, 1, h->l_txt - 1, v->fpout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 if (h->l_txt == 0) fprintf(v->fpout, "##fileformat=VCFv4.1\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 fprintf(v->fpout, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 for (i = 0; i < h->n_smpl; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 fprintf(v->fpout, "\t%s", h->sns[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 fputc('\n', v->fpout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 int vcf_write(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 vcf_t *v = (vcf_t*)bp->v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 extern void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 if (!bp->is_vcf) return bcf_write(bp, h, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 bcf_fmt_core(h, b, &v->line);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 fwrite(v->line.s, 1, v->line.l, v->fpout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 fputc('\n', v->fpout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 return v->line.l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 int dret, k, i, sync = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 vcf_t *v = (vcf_t*)bp->v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 char *p, *q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 kstring_t str, rn;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 ks_tokaux_t aux, a2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 if (!bp->is_vcf) return bcf_read(bp, h, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 v->line.l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 str.l = 0; str.m = b->m_str; str.s = b->str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 rn.l = rn.m = h->l_nm; rn.s = h->name;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 if (ks_getuntil(v->ks, '\n', &v->line, &dret) < 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 b->n_smpl = h->n_smpl;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 for (p = kstrtok(v->line.s, "\t", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 *(char*)aux.p = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 if (k == 0) { // ref
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 int tid = bcf_str2id(v->refhash, p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 if (tid < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 tid = bcf_str2id_add(v->refhash, strdup(p));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 kputs(p, &rn); kputc('\0', &rn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 sync = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 b->tid = tid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 } else if (k == 1) { // pos
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 b->pos = atoi(p) - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 } else if (k == 5) { // qual
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 b->qual = (p[0] >= '0' && p[0] <= '9')? atof(p) : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 } else if (k <= 8) { // variable length strings
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 kputs(p, &str); kputc('\0', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 b->l_str = str.l; b->m_str = str.m; b->str = str.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 if (k == 8) bcf_sync(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 } else { // k > 9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 if (strncmp(p, "./.", 3) == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 for (i = 0; i < b->n_gi; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 ((uint8_t*)b->gi[i].data)[k-9] = 1<<7;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 ((uint8_t*)b->gi[i].data)[k-9] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 ((int32_t*)b->gi[i].data)[k-9] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 ((uint16_t*)b->gi[i].data)[k-9] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 } else if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 int y = b->n_alleles * (b->n_alleles + 1) / 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 memset((uint8_t*)b->gi[i].data + (k - 9) * y, 0, y);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 int y = b->n_alleles * (b->n_alleles + 1) / 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 memset((float*)b->gi[i].data + (k - 9) * y, 0, y * 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 goto endblock;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 for (q = kstrtok(p, ":", &a2), i = 0; q && i < b->n_gi; q = kstrtok(0, 0, &a2), ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 ((uint8_t*)b->gi[i].data)[k-9] = (q[0] - '0')<<3 | (q[2] - '0') | (q[1] == '/'? 0 : 1) << 6;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 double _x = strtod(q, &q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 int x = (int)(_x + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 if (x > 255) x = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 ((uint8_t*)b->gi[i].data)[k-9] = x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 int x = strtol(q, &q, 10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 if (x > 0xffff) x = 0xffff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 ((uint32_t*)b->gi[i].data)[k-9] = x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 int x = strtol(q, &q, 10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 if (x > 0xffff) x = 0xffff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 ((uint16_t*)b->gi[i].data)[k-9] = x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 } else if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 int x, y, j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 uint8_t *data = (uint8_t*)b->gi[i].data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 y = b->n_alleles * (b->n_alleles + 1) / 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 for (j = 0; j < y; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 x = strtol(q, &q, 10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 if (x > 255) x = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 data[(k-9) * y + j] = x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 ++q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 int j, y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 float x, *data = (float*)b->gi[i].data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 y = b->n_alleles * (b->n_alleles + 1) / 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 for (j = 0; j < y; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 x = strtod(q, &q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 data[(k-9) * y + j] = x > 0? -x/10. : x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 ++q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 endblock: i = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 h->l_nm = rn.l; h->name = rn.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 if (sync) bcf_hdr_sync(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 return v->line.l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 }