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