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

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:903fc43d6227
1 #include <string.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include "kstring.h"
5 #include "bcf.h"
6
7 bcf_t *bcf_open(const char *fn, const char *mode)
8 {
9 bcf_t *b;
10 b = calloc(1, sizeof(bcf_t));
11 if (strchr(mode, 'w')) {
12 b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdout), mode);
13 } else {
14 b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdin), mode);
15 }
16 return b;
17 }
18
19 int bcf_close(bcf_t *b)
20 {
21 int ret;
22 if (b == 0) return 0;
23 ret = bgzf_close(b->fp);
24 free(b);
25 return ret;
26 }
27
28 int bcf_hdr_write(bcf_t *b, const bcf_hdr_t *h)
29 {
30 if (b == 0 || h == 0) return -1;
31 bgzf_write(b->fp, "BCF\4", 4);
32 bgzf_write(b->fp, &h->l_nm, 4);
33 bgzf_write(b->fp, h->name, h->l_nm);
34 bgzf_write(b->fp, &h->l_smpl, 4);
35 bgzf_write(b->fp, h->sname, h->l_smpl);
36 bgzf_write(b->fp, &h->l_txt, 4);
37 bgzf_write(b->fp, h->txt, h->l_txt);
38 bgzf_flush(b->fp);
39 return 16 + h->l_nm + h->l_smpl + h->l_txt;
40 }
41
42 bcf_hdr_t *bcf_hdr_read(bcf_t *b)
43 {
44 uint8_t magic[4];
45 bcf_hdr_t *h;
46 if (b == 0) return 0;
47 h = calloc(1, sizeof(bcf_hdr_t));
48 bgzf_read(b->fp, magic, 4);
49 bgzf_read(b->fp, &h->l_nm, 4);
50 h->name = malloc(h->l_nm);
51 bgzf_read(b->fp, h->name, h->l_nm);
52 bgzf_read(b->fp, &h->l_smpl, 4);
53 h->sname = malloc(h->l_smpl);
54 bgzf_read(b->fp, h->sname, h->l_smpl);
55 bgzf_read(b->fp, &h->l_txt, 4);
56 h->txt = malloc(h->l_txt);
57 bgzf_read(b->fp, h->txt, h->l_txt);
58 bcf_hdr_sync(h);
59 return h;
60 }
61
62 void bcf_hdr_destroy(bcf_hdr_t *h)
63 {
64 if (h == 0) return;
65 free(h->name); free(h->sname); free(h->txt); free(h->ns); free(h->sns);
66 free(h);
67 }
68
69 static inline char **cnt_null(int l, char *str, int *_n)
70 {
71 int n = 0;
72 char *p, **list;
73 *_n = 0;
74 if (l == 0 || str == 0) return 0;
75 for (p = str; p != str + l; ++p)
76 if (*p == 0) ++n;
77 *_n = n;
78 list = calloc(n, sizeof(void*));
79 list[0] = str;
80 for (p = str, n = 1; p < str + l - 1; ++p)
81 if (*p == 0) list[n++] = p + 1;
82 return list;
83 }
84
85 int bcf_hdr_sync(bcf_hdr_t *b)
86 {
87 if (b == 0) return -1;
88 if (b->ns) free(b->ns);
89 if (b->sns) free(b->sns);
90 if (b->l_nm) b->ns = cnt_null(b->l_nm, b->name, &b->n_ref);
91 else b->ns = 0, b->n_ref = 0;
92 b->sns = cnt_null(b->l_smpl, b->sname, &b->n_smpl);
93 return 0;
94 }
95
96 int bcf_sync(bcf1_t *b)
97 {
98 char *p, *tmp[5];
99 int i, n, n_smpl = b->n_smpl;
100 ks_tokaux_t aux;
101 // set ref, alt, flt, info, fmt
102 b->ref = b->alt = b->flt = b->info = b->fmt = 0;
103 for (p = b->str, n = 0; p < b->str + b->l_str; ++p) {
104 if (*p == 0 && p+1 != b->str + b->l_str) {
105 if (n == 5) {
106 ++n;
107 break;
108 } else tmp[n++] = p + 1;
109 }
110 }
111 if (n != 5) {
112 fprintf(stderr, "[%s] incorrect number of fields (%d != 5) at %d:%d\n", __func__, n, b->tid, b->pos);
113 return -1;
114 }
115 b->ref = tmp[0]; b->alt = tmp[1]; b->flt = tmp[2]; b->info = tmp[3]; b->fmt = tmp[4];
116 // set n_alleles
117 if (*b->alt == 0) b->n_alleles = 1;
118 else {
119 for (p = b->alt, n = 1; *p; ++p)
120 if (*p == ',') ++n;
121 b->n_alleles = n + 1;
122 }
123 // set n_gi and gi[i].fmt
124 for (p = b->fmt, n = 1; *p; ++p)
125 if (*p == ':') ++n;
126 if (n > b->m_gi) {
127 int old_m = b->m_gi;
128 b->m_gi = n;
129 kroundup32(b->m_gi);
130 b->gi = realloc(b->gi, b->m_gi * sizeof(bcf_ginfo_t));
131 memset(b->gi + old_m, 0, (b->m_gi - old_m) * sizeof(bcf_ginfo_t));
132 }
133 b->n_gi = n;
134 for (p = kstrtok(b->fmt, ":", &aux), n = 0; p; p = kstrtok(0, 0, &aux))
135 b->gi[n++].fmt = bcf_str2int(p, aux.p - p);
136 // set gi[i].len
137 for (i = 0; i < b->n_gi; ++i) {
138 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
139 b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2;
140 } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("HQ", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
141 b->gi[i].len = 2;
142 } else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("GT", 2)) {
143 b->gi[i].len = 1;
144 } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
145 b->gi[i].len = 4;
146 } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
147 b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2 * 4;
148 }
149 b->gi[i].data = realloc(b->gi[i].data, n_smpl * b->gi[i].len);
150 }
151 return 0;
152 }
153
154 int bcf_write(bcf_t *bp, const bcf_hdr_t *h, const bcf1_t *b)
155 {
156 int i, l = 0;
157 if (b == 0) return -1;
158 bgzf_write(bp->fp, &b->tid, 4);
159 bgzf_write(bp->fp, &b->pos, 4);
160 bgzf_write(bp->fp, &b->qual, 4);
161 bgzf_write(bp->fp, &b->l_str, 4);
162 bgzf_write(bp->fp, b->str, b->l_str);
163 l = 12 + b->l_str;
164 for (i = 0; i < b->n_gi; ++i) {
165 bgzf_write(bp->fp, b->gi[i].data, b->gi[i].len * h->n_smpl);
166 l += b->gi[i].len * h->n_smpl;
167 }
168 return l;
169 }
170
171 int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b)
172 {
173 int i, l = 0;
174 if (b == 0) return -1;
175 if (bgzf_read(bp->fp, &b->tid, 4) == 0) return -1;
176 b->n_smpl = h->n_smpl;
177 bgzf_read(bp->fp, &b->pos, 4);
178 bgzf_read(bp->fp, &b->qual, 4);
179 bgzf_read(bp->fp, &b->l_str, 4);
180 if (b->l_str > b->m_str) {
181 b->m_str = b->l_str;
182 kroundup32(b->m_str);
183 b->str = realloc(b->str, b->m_str);
184 }
185 bgzf_read(bp->fp, b->str, b->l_str);
186 l = 12 + b->l_str;
187 if (bcf_sync(b) < 0) return -2;
188 for (i = 0; i < b->n_gi; ++i) {
189 bgzf_read(bp->fp, b->gi[i].data, b->gi[i].len * h->n_smpl);
190 l += b->gi[i].len * h->n_smpl;
191 }
192 return l;
193 }
194
195 int bcf_destroy(bcf1_t *b)
196 {
197 int i;
198 if (b == 0) return -1;
199 free(b->str);
200 for (i = 0; i < b->m_gi; ++i)
201 free(b->gi[i].data);
202 free(b->gi);
203 free(b);
204 return 0;
205 }
206
207 static inline void fmt_str(const char *p, kstring_t *s)
208 {
209 if (*p == 0) kputc('.', s);
210 else kputs(p, s);
211 }
212
213 void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s)
214 {
215 int i, j, x;
216 s->l = 0;
217 if (h->n_ref) kputs(h->ns[b->tid], s);
218 else kputw(b->tid, s);
219 kputc('\t', s);
220 kputw(b->pos + 1, s); kputc('\t', s);
221 fmt_str(b->str, s); kputc('\t', s);
222 fmt_str(b->ref, s); kputc('\t', s);
223 fmt_str(b->alt, s); kputc('\t', s);
224 ksprintf(s, "%.3g", b->qual); kputc('\t', s);
225 fmt_str(b->flt, s); kputc('\t', s);
226 fmt_str(b->info, s);
227 if (b->fmt[0]) {
228 kputc('\t', s);
229 fmt_str(b->fmt, s);
230 }
231 x = b->n_alleles * (b->n_alleles + 1) / 2;
232 if (b->n_gi == 0) return;
233 int iPL = -1;
234 if ( b->n_alleles > 2 ) {
235 for (i=0; i<b->n_gi; i++) {
236 if ( b->gi[i].fmt == bcf_str2int("PL", 2) ) {
237 iPL = i;
238 break;
239 }
240 }
241 }
242 for (j = 0; j < h->n_smpl; ++j) {
243 int ploidy = b->ploidy ? b->ploidy[j] : 2;
244 kputc('\t', s);
245 for (i = 0; i < b->n_gi; ++i) {
246 if (i) kputc(':', s);
247 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
248 uint8_t *d = (uint8_t*)b->gi[i].data + j * x;
249 int k;
250 if ( ploidy==1 )
251 for (k=0; k<b->n_alleles; k++)
252 {
253 if (k>0) kputc(',', s);
254 kputw(d[(k+1)*(k+2)/2-1], s);
255 }
256 else
257 for (k = 0; k < x; ++k) {
258 if (k > 0) kputc(',', s);
259 kputw(d[k], s);
260 }
261 } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
262 kputw(((uint16_t*)b->gi[i].data)[j], s);
263 } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
264 kputw(((uint8_t*)b->gi[i].data)[j], s);
265 } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
266 kputw(((int32_t*)b->gi[i].data)[j], s);
267 } else if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
268 int y = ((uint8_t*)b->gi[i].data)[j];
269 if ( ploidy==1 )
270 {
271 if ( y>>7&1 )
272 kputc('.', s);
273 else
274 kputc('0' + (y>>3&7), s);
275 }
276 else
277 {
278 if ( y>>7&1 )
279 kputsn("./.", 3, s);
280 else {
281 kputc('0' + (y>>3&7), s);
282 kputc("/|"[y>>6&1], s);
283 kputc('0' + (y&7), s);
284 }
285 }
286 } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
287 float *d = (float*)b->gi[i].data + j * x;
288 int k;
289 //printf("- %lx\n", d);
290 for (k = 0; k < x; ++k) {
291 if (k > 0) kputc(',', s);
292 ksprintf(s, "%.2f", d[k]);
293 }
294 } else kputc('.', s); // custom fields
295 }
296 }
297 }
298
299 char *bcf_fmt(const bcf_hdr_t *h, bcf1_t *b)
300 {
301 kstring_t s;
302 s.l = s.m = 0; s.s = 0;
303 bcf_fmt_core(h, b, &s);
304 return s.s;
305 }
306
307 int bcf_append_info(bcf1_t *b, const char *info, int l)
308 {
309 int shift = b->fmt - b->str;
310 int l_fmt = b->l_str - shift;
311 char *ori = b->str;
312 if (b->l_str + l > b->m_str) { // enlarge if necessary
313 b->m_str = b->l_str + l;
314 kroundup32(b->m_str);
315 b->str = realloc(b->str, b->m_str);
316 }
317 memmove(b->str + shift + l, b->str + shift, l_fmt); // move the FORMAT field
318 memcpy(b->str + shift - 1, info, l); // append to the INFO field
319 b->str[shift + l - 1] = '\0';
320 b->fmt = b->str + shift + l;
321 b->l_str += l;
322 if (ori != b->str) bcf_sync(b); // synchronize when realloc changes the pointer
323 return 0;
324 }
325
326 int remove_tag(char *str, const char *tag, char delim)
327 {
328 char *tmp = str, *p;
329 int len_diff = 0, ori_len = strlen(str);
330 while ( *tmp && (p = strstr(tmp,tag)) )
331 {
332 if ( p>str )
333 {
334 if ( *(p-1)!=delim ) { tmp=p+1; continue; } // shared substring
335 p--;
336 }
337 char *q=p+1;
338 while ( *q && *q!=delim ) q++;
339 if ( p==str && *q ) q++; // the tag is first, don't move the delim char
340 len_diff += q-p;
341 if ( ! *q ) { *p = 0; break; } // the tag was last, no delim follows
342 else
343 memmove(p,q,ori_len-(int)(p-str)-(int)(q-p)); // *q==delim
344 }
345 if ( len_diff==ori_len )
346 str[0]='.', str[1]=0, len_diff--;
347
348 return len_diff;
349 }
350
351
352 void rm_info(kstring_t *s, const char *key)
353 {
354 char *p = s->s;
355 int n = 0;
356 while ( n<4 )
357 {
358 if ( !*p ) n++;
359 p++;
360 }
361 char *q = p+1;
362 while ( *q && q-s->s<s->l ) q++;
363
364 int nrm = remove_tag(p, key, ';');
365 if ( nrm )
366 memmove(q-nrm, q, s->s+s->l-q+1);
367 s->l -= nrm;
368 }
369
370 int bcf_cpy(bcf1_t *r, const bcf1_t *b)
371 {
372 char *t1 = r->str;
373 bcf_ginfo_t *t2 = r->gi;
374 int i, t3 = r->m_str, t4 = r->m_gi;
375 *r = *b;
376 r->str = t1; r->gi = t2; r->m_str = t3; r->m_gi = t4;
377 if (r->m_str < b->m_str) {
378 r->m_str = b->m_str;
379 r->str = realloc(r->str, r->m_str);
380 }
381 memcpy(r->str, b->str, r->m_str);
382 bcf_sync(r); // calling bcf_sync() is simple but inefficient
383 for (i = 0; i < r->n_gi; ++i)
384 memcpy(r->gi[i].data, b->gi[i].data, r->n_smpl * r->gi[i].len);
385 return 0;
386 }
387
388 int bcf_is_indel(const bcf1_t *b)
389 {
390 char *p;
391 if (strlen(b->ref) > 1) return 1;
392 for (p = b->alt; *p; ++p)
393 if (*p != ',' && p[1] != ',' && p[1] != '\0')
394 return 1;
395 return 0;
396 }