updated main loop to not duplicate hash key
[samtools.git] / phase.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <math.h>
6 #include <zlib.h>
7 #include "bam.h"
8 #include "errmod.h"
9
10 #include "kseq.h"
11 KSTREAM_INIT(gzFile, gzread, 16384)
12
13 #define MAX_VARS 256
14 #define FLIP_PENALTY 2
15 #define FLIP_THRES 4
16 #define MASK_THRES 3
17
18 #define FLAG_FIX_CHIMERA 0x1
19 #define FLAG_LIST_EXCL   0x4
20 #define FLAG_DROP_AMBI   0x8
21
22 typedef struct {
23         // configurations, initialized in the main function
24         int flag, k, min_baseQ, min_varLOD, max_depth;
25         // other global variables
26         int vpos_shift;
27         bamFile fp;
28         char *pre;
29         bamFile out[3];
30         // alignment queue
31         int n, m;
32         bam1_t **b;
33 } phaseg_t;
34
35 typedef struct {
36         int8_t seq[MAX_VARS]; // TODO: change to dynamic memory allocation!
37         int vpos, beg, end;
38         uint32_t vlen:16, single:1, flip:1, phase:1, phased:1, ambig:1;
39         uint32_t in:16, out:16; // in-phase and out-phase
40 } frag_t, *frag_p;
41
42 #define rseq_lt(a,b) ((a)->vpos < (b)->vpos)
43
44 #include "khash.h"
45 KHASH_SET_INIT_INT64(set64)
46 KHASH_MAP_INIT_INT64(64, frag_t)
47
48 typedef khash_t(64) nseq_t;
49
50 #include "ksort.h"
51 KSORT_INIT(rseq, frag_p, rseq_lt)
52
53 static char nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
54
55 static inline uint64_t X31_hash_string(const char *s)
56 {
57         uint64_t h = *s;
58         if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
59         return h;
60 }
61
62 static void count1(int l, const uint8_t *seq, int *cnt)
63 {
64         int i, j, n_ambi;
65         uint32_t z, x;
66         if (seq[l-1] == 0) return; // do nothing is the last base is ambiguous
67         for (i = n_ambi = 0; i < l; ++i) // collect ambiguous bases
68                 if (seq[i] == 0) ++n_ambi;
69         if (l - n_ambi <= 1) return; // only one SNP
70         for (x = 0; x < 1u<<n_ambi; ++x) { // count
71                 for (i = j = 0, z = 0; i < l; ++i) {
72                         int c;
73                         if (seq[i]) c = seq[i] - 1;
74                         else {
75                                 c = x>>j&1;
76                                 ++j;
77                         }
78                         z = z<<1 | c;
79                 }
80                 ++cnt[z];
81         }
82 }
83
84 static int **count_all(int l, int vpos, nseq_t *hash)
85 {
86         khint_t k;
87         int i, j, **cnt;
88         uint8_t *seq;
89         seq = calloc(l, 1);
90         cnt = calloc(vpos, sizeof(void*));
91         for (i = 0; i < vpos; ++i) cnt[i] = calloc(1<<l, sizeof(int));
92         for (k = 0; k < kh_end(hash); ++k) {
93                 if (kh_exist(hash, k)) {
94                         frag_t *f = &kh_val(hash, k);
95                         if (f->vpos >= vpos || f->single) continue; // out of region; or singleton
96                         if (f->vlen == 1) { // such reads should be flagged as deleted previously if everything is right
97                                 f->single = 1;
98                                 continue;
99                         }
100                         for (j = 1; j < f->vlen; ++j) {
101                                 for (i = 0; i < l; ++i)
102                                         seq[i] = j < l - 1 - i? 0 : f->seq[j - (l - 1 - i)];
103                                 count1(l, seq, cnt[f->vpos + j]);
104                         }
105                 }
106         }
107         free(seq);
108         return cnt;
109 }
110
111 // phasing
112 static int8_t *dynaprog(int l, int vpos, int **w)
113 {
114         int *f[2], *curr, *prev, max, i;
115         int8_t **b, *h = 0;
116         uint32_t x, z = 1u<<(l-1), mask = (1u<<l) - 1;
117         f[0] = calloc(z, sizeof(int));
118         f[1] = calloc(z, sizeof(int));
119         b = calloc(vpos, sizeof(void*));
120         prev = f[0]; curr = f[1];
121         // fill the backtrack matrix
122         for (i = 0; i < vpos; ++i) {
123                 int *wi = w[i], *tmp;
124                 int8_t *bi;
125                 bi = b[i] = calloc(z, 1);
126                 /* In the following, x is the current state, which is the
127                  * lexicographically smaller local haplotype. xc is the complement of
128                  * x, or the larger local haplotype; y0 and y1 are the two predecessors
129                  * of x. */
130                 for (x = 0; x < z; ++x) { // x0 is the smaller 
131                         uint32_t y0, y1, xc;
132                         int c0, c1;
133                         xc = ~x&mask; y0 = x>>1; y1 = xc>>1;
134                         c0 = prev[y0] + wi[x] + wi[xc];
135                         c1 = prev[y1] + wi[x] + wi[xc];
136                         if (c0 > c1) bi[x] = 0, curr[x] = c0;
137                         else bi[x] = 1, curr[x] = c1;
138                 }
139                 tmp = prev; prev = curr; curr = tmp; // swap
140         }
141         { // backtrack
142                 uint32_t max_x = 0;
143                 int which = 0;
144                 h = calloc(vpos, 1);
145                 for (x = 0, max = 0, max_x = 0; x < z; ++x)
146                         if (prev[x] > max) max = prev[x], max_x = x;
147                 for (i = vpos - 1, x = max_x; i >= 0; --i) {
148                         h[i] = which? (~x&1) : (x&1);
149                         which = b[i][x]? !which : which;
150                         x = b[i][x]? (~x&mask)>>1 : x>>1;
151                 }
152         }
153         // free
154         for (i = 0; i < vpos; ++i) free(b[i]);
155         free(f[0]); free(f[1]); free(b);
156         return h;
157 }
158
159 // phase each fragment
160 static uint64_t *fragphase(int vpos, const int8_t *path, nseq_t *hash, int flip)
161 {
162         khint_t k;
163         uint64_t *pcnt;
164         uint32_t *left, *rght, max;
165         left = rght = 0; max = 0;
166         pcnt = calloc(vpos, 8);
167         for (k = 0; k < kh_end(hash); ++k) {
168                 if (kh_exist(hash, k)) {
169                         int i, c[2];
170                         frag_t *f = &kh_val(hash, k);
171                         if (f->vpos >= vpos) continue;
172                         // get the phase
173                         c[0] = c[1] = 0;
174                         for (i = 0; i < f->vlen; ++i) {
175                                 if (f->seq[i] == 0) continue;
176                                 ++c[f->seq[i] == path[f->vpos + i] + 1? 0 : 1];
177                         }
178                         f->phase = c[0] > c[1]? 0 : 1;
179                         f->in = c[f->phase]; f->out = c[1 - f->phase];
180                         f->phased = f->in == f->out? 0 : 1;
181                         f->ambig = (f->in && f->out && f->out < 3 && f->in <= f->out + 1)? 1 : 0;
182                         // fix chimera
183                         f->flip = 0;
184                         if (flip && c[0] >= 3 && c[1] >= 3) {
185                                 int sum[2], m, mi, md;
186                                 if (f->vlen > max) { // enlarge the array
187                                         max = f->vlen;
188                                         kroundup32(max);
189                                         left = realloc(left, max * 4);
190                                         rght = realloc(rght, max * 4);
191                                 }
192                                 for (i = 0, sum[0] = sum[1] = 0; i < f->vlen; ++i) { // get left counts
193                                         if (f->seq[i]) {
194                                                 int c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
195                                                 ++sum[c == path[f->vpos + i]? 0 : 1];
196                                         }
197                                         left[i] = sum[1]<<16 | sum[0];
198                                 }
199                                 for (i = f->vlen - 1, sum[0] = sum[1] = 0; i >= 0; --i) { // get right counts
200                                         if (f->seq[i]) {
201                                                 int c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
202                                                 ++sum[c == path[f->vpos + i]? 0 : 1];
203                                         }
204                                         rght[i] = sum[1]<<16 | sum[0];
205                                 }
206                                 // find the best flip point
207                                 for (i = m = 0, mi = -1, md = -1; i < f->vlen - 1; ++i) {
208                                         int a[2];
209                                         a[0] = (left[i]&0xffff) + (rght[i+1]>>16&0xffff) - (rght[i+1]&0xffff) * FLIP_PENALTY;
210                                         a[1] = (left[i]>>16&0xffff) + (rght[i+1]&0xffff) - (rght[i+1]>>16&0xffff) * FLIP_PENALTY;
211                                         if (a[0] > a[1]) {
212                                                 if (a[0] > m) m = a[0], md = 0, mi = i;
213                                         } else {
214                                                 if (a[1] > m) m = a[1], md = 1, mi = i;
215                                         }
216                                 }
217                                 if (m - c[0] >= FLIP_THRES && m - c[1] >= FLIP_THRES) { // then flip
218                                         f->flip = 1;
219                                         if (md == 0) { // flip the tail
220                                                 for (i = mi + 1; i < f->vlen; ++i)
221                                                         if (f->seq[i] == 1) f->seq[i] = 2;
222                                                         else if (f->seq[i] == 2) f->seq[i] = 1;
223                                         } else { // flip the head
224                                                 for (i = 0; i <= mi; ++i)
225                                                         if (f->seq[i] == 1) f->seq[i] = 2;
226                                                         else if (f->seq[i] == 2) f->seq[i] = 1;
227                                         }
228                                 }
229                         }
230                         // update pcnt[]
231                         if (!f->single) {
232                                 for (i = 0; i < f->vlen; ++i) {
233                                         int c;
234                                         if (f->seq[i] == 0) continue;
235                                         c = f->phase? 2 - f->seq[i] : f->seq[i] - 1;
236                                         if (c == path[f->vpos + i]) {
237                                                 if (f->phase == 0) ++pcnt[f->vpos + i];
238                                                 else pcnt[f->vpos + i] += 1ull<<32;
239                                         } else {
240                                                 if (f->phase == 0) pcnt[f->vpos + i] += 1<<16;
241                                                 else pcnt[f->vpos + i] += 1ull<<48;
242                                         }
243                                 }
244                         }
245                 }
246         }
247         free(left); free(rght);
248         return pcnt;
249 }
250
251 static uint64_t *genmask(int vpos, const uint64_t *pcnt, int *_n)
252 {
253         int i, max = 0, max_i = -1, m = 0, n = 0, beg = 0, score = 0;
254         uint64_t *list = 0;
255         for (i = 0; i < vpos; ++i) {
256                 uint64_t x = pcnt[i];
257                 int c[4], pre = score, s;
258                 c[0] = x&0xffff; c[1] = x>>16&0xffff; c[2] = x>>32&0xffff; c[3] = x>>48&0xffff;
259                 s = (c[1] + c[3] == 0)? -(c[0] + c[2]) : (c[1] + c[3] - 1);
260                 if (c[3] > c[2]) s += c[3] - c[2];
261                 if (c[1] > c[0]) s += c[1] - c[0];
262                 score += s;
263                 if (score < 0) score = 0;
264                 if (pre == 0 && score > 0) beg = i; // change from zero to non-zero
265                 if ((i == vpos - 1 || score == 0) && max >= MASK_THRES) {
266                         if (n == m) {
267                                 m = m? m<<1 : 4;
268                                 list = realloc(list, m * 8);
269                         }
270                         list[n++] = (uint64_t)beg<<32 | max_i;
271                         i = max_i; // reset i to max_i
272                         score = 0;
273                 } else if (score > max) max = score, max_i = i;
274                 if (score == 0) max = 0;
275         }
276         *_n = n;
277         return list;
278 }
279
280 // trim heading and tailing ambiguous bases; mark deleted and remove sequence
281 static int clean_seqs(int vpos, nseq_t *hash)
282 {
283         khint_t k;
284         int ret = 0;
285         for (k = 0; k < kh_end(hash); ++k) {
286                 if (kh_exist(hash, k)) {
287                         frag_t *f = &kh_val(hash, k);
288                         int beg, end, i;
289                         if (f->vpos >= vpos) {
290                                 ret = 1;
291                                 continue;
292                         }
293                         for (i = 0; i < f->vlen; ++i)
294                                 if (f->seq[i] != 0) break;
295                         beg = i;
296                         for (i = f->vlen - 1; i >= 0; --i)
297                                 if (f->seq[i] != 0) break;
298                         end = i + 1;
299                         if (end - beg <= 0) kh_del(64, hash, k);
300                         else {
301                                 if (beg != 0) memmove(f->seq, f->seq + beg, end - beg);
302                                 f->vpos += beg; f->vlen = end - beg;
303                                 f->single = f->vlen == 1? 1 : 0;
304                         }
305                 }
306         }
307         return ret;
308 }
309
310 static void dump_aln(phaseg_t *g, int min_pos, const nseq_t *hash)
311 {
312         int i, is_flip, drop_ambi;
313         drop_ambi = g->flag & FLAG_DROP_AMBI;
314         is_flip = (drand48() < 0.5);
315         for (i = 0; i < g->n; ++i) {
316                 int end, which;
317                 uint64_t key;
318                 khint_t k;
319                 bam1_t *b = g->b[i];
320                 key = X31_hash_string(bam1_qname(b));
321                 end = bam_calend(&b->core, bam1_cigar(b));
322                 if (end > min_pos) break;
323                 k = kh_get(64, hash, key);
324                 if (k == kh_end(hash)) which = 3;
325                 else {
326                         frag_t *f = &kh_val(hash, k);
327                         if (f->ambig) which = drop_ambi? 2 : 3;
328                         else if (f->phased && f->flip) which = 2;
329                         else if (f->phased == 0) which = 3;
330                         else { // phased and not flipped
331                                 char c = 'Y';
332                                 which = f->phase;
333                                 bam_aux_append(b, "ZP", 'A', 1, (uint8_t*)&c);
334                         }
335                         if (which < 2 && is_flip) which = 1 - which; // increase the randomness
336                 }
337                 if (which == 3) which = (drand48() < 0.5);
338                 bam_write1(g->out[which], b);
339                 bam_destroy1(b);
340                 g->b[i] = 0;
341         }
342         memmove(g->b, g->b + i, (g->n - i) * sizeof(void*));
343         g->n -= i;
344 }
345
346 static int phase(phaseg_t *g, const char *chr, int vpos, uint64_t *cns, nseq_t *hash)
347 {
348         int i, j, n_seqs = kh_size(hash), n_masked = 0, min_pos;
349         khint_t k;
350         frag_t **seqs;
351         int8_t *path, *sitemask;
352         uint64_t *pcnt, *regmask;
353
354         if (vpos == 0) return 0;
355         i = clean_seqs(vpos, hash); // i is true if hash has an element with its vpos >= vpos
356         min_pos = i? cns[vpos]>>32 : 0x7fffffff;
357         if (vpos == 1) {
358                 printf("PS\t%s\t%d\t%d\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[0]>>32) + 1);
359                 printf("M0\t%s\t%d\t%d\t%c\t%c\t%d\t0\t0\t0\t0\n//\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[0]>>32) + 1,
360                         "ACGTX"[cns[0]&3], "ACGTX"[cns[0]>>16&3], g->vpos_shift + 1);
361                 for (k = 0; k < kh_end(hash); ++k) {
362                         if (kh_exist(hash, k)) {
363                                 frag_t *f = &kh_val(hash, k);
364                                 if (f->vpos) continue;
365                                 f->flip = 0;
366                                 if (f->seq[0] == 0) f->phased = 0;
367                                 else f->phased = 1, f->phase = f->seq[0] - 1;
368                         }
369                 }
370                 dump_aln(g, min_pos, hash);
371                 ++g->vpos_shift;
372                 return 1;
373         }
374         { // phase
375                 int **cnt;
376                 uint64_t *mask;
377                 printf("PS\t%s\t%d\t%d\n", chr, (int)(cns[0]>>32) + 1, (int)(cns[vpos-1]>>32) + 1);
378                 sitemask = calloc(vpos, 1);
379                 cnt = count_all(g->k, vpos, hash);
380                 path = dynaprog(g->k, vpos, cnt);
381                 for (i = 0; i < vpos; ++i) free(cnt[i]);
382                 free(cnt);
383                 pcnt = fragphase(vpos, path, hash, 0); // do not fix chimeras when masking
384                 mask = genmask(vpos, pcnt, &n_masked);
385                 regmask = calloc(n_masked, 8);
386                 for (i = 0; i < n_masked; ++i) {
387                         regmask[i] = cns[mask[i]>>32]>>32<<32 | cns[(uint32_t)mask[i]]>>32;
388                         for (j = mask[i]>>32; j <= (int32_t)mask[i]; ++j)
389                                 sitemask[j] = 1;
390                 }
391                 free(mask);
392                 if (g->flag & FLAG_FIX_CHIMERA) {
393                         free(pcnt);
394                         pcnt = fragphase(vpos, path, hash, 1);
395                 }
396         }
397         for (i = 0; i < n_masked; ++i)
398                 printf("FL\t%s\t%d\t%d\n", chr, (int)(regmask[i]>>32) + 1, (int)regmask[i] + 1);
399         for (i = 0; i < vpos; ++i) {
400                 uint64_t x = pcnt[i];
401                 int8_t c[2];
402                 c[0] = (cns[i]&0xffff)>>2 == 0? 4 : (cns[i]&3);
403                 c[1] = (cns[i]>>16&0xffff)>>2 == 0? 4 : (cns[i]>>16&3);
404                 printf("M%d\t%s\t%d\t%d\t%c\t%c\t%d\t%d\t%d\t%d\t%d\n", sitemask[i]+1, chr, (int)(cns[0]>>32) + 1, (int)(cns[i]>>32) + 1, "ACGTX"[c[path[i]]], "ACGTX"[c[1-path[i]]],
405                         i + g->vpos_shift + 1, (int)(x&0xffff), (int)(x>>16&0xffff), (int)(x>>32&0xffff), (int)(x>>48&0xffff));
406         }
407         free(path); free(pcnt); free(regmask); free(sitemask);
408         seqs = calloc(n_seqs, sizeof(void*));
409         for (k = 0, i = 0; k < kh_end(hash); ++k) 
410                 if (kh_exist(hash, k) && kh_val(hash, k).vpos < vpos && !kh_val(hash, k).single)
411                         seqs[i++] = &kh_val(hash, k);
412         n_seqs = i;
413         ks_introsort_rseq(n_seqs, seqs);
414         for (i = 0; i < n_seqs; ++i) {
415                 frag_t *f = seqs[i];
416                 printf("EV\t0\t%s\t%d\t40\t%dM\t*\t0\t0\t", chr, f->vpos + 1 + g->vpos_shift, f->vlen);
417                 for (j = 0; j < f->vlen; ++j) {
418                         uint32_t c = cns[f->vpos + j];
419                         if (f->seq[j] == 0) putchar('N');
420                         else putchar("ACGT"[f->seq[j] == 1? (c&3) : (c>>16&3)]);
421                 }
422                 printf("\t*\tYP:i:%d\tYF:i:%d\tYI:i:%d\tYO:i:%d\tYS:i:%d\n", f->phase, f->flip, f->in, f->out, f->beg+1);
423         }
424         free(seqs);
425         printf("//\n");
426         fflush(stdout);
427         g->vpos_shift += vpos;
428         dump_aln(g, min_pos, hash);
429         return vpos;
430 }
431
432 static void update_vpos(int vpos, nseq_t *hash)
433 {
434         khint_t k;
435         for (k = 0; k < kh_end(hash); ++k) {
436                 if (kh_exist(hash, k)) {
437                         frag_t *f = &kh_val(hash, k);
438                         if (f->vpos < vpos) kh_del(64, hash, k); // TODO: if frag_t::seq is allocated dynamically, free it
439                         else f->vpos -= vpos;
440                 }
441         }
442 }
443
444 static nseq_t *shrink_hash(nseq_t *hash) // TODO: to implement
445 {
446         return hash;
447 }
448
449 static int readaln(void *data, bam1_t *b)
450 {
451         phaseg_t *g = (phaseg_t*)data;
452         int ret;
453         ret = bam_read1(g->fp, b);
454         if (ret < 0) return ret;
455         if (!(b->core.flag & (BAM_FUNMAP|BAM_FSECONDARY|BAM_FQCFAIL|BAM_FDUP)) && g->pre) {
456                 if (g->n == g->m) {
457                         g->m = g->m? g->m<<1 : 16;
458                         g->b = realloc(g->b, g->m * sizeof(void*));
459                 }
460                 g->b[g->n++] = bam_dup1(b);
461         }
462         return ret;
463 }
464
465 static khash_t(set64) *loadpos(const char *fn, bam_header_t *h)
466 {
467         gzFile fp;
468         kstream_t *ks;
469         int ret, dret;
470         kstring_t *str;
471         khash_t(set64) *hash;
472
473         hash = kh_init(set64);
474         str = calloc(1, sizeof(kstring_t));
475         fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
476         ks = ks_init(fp);
477         while (ks_getuntil(ks, 0, str, &dret) >= 0) {
478                 int tid = bam_get_tid(h, str->s);
479                 if (tid >= 0 && dret != '\n') {
480                         if (ks_getuntil(ks, 0, str, &dret) >= 0) {
481                                 uint64_t x = (uint64_t)tid<<32 | (atoi(str->s) - 1);
482                                 kh_put(set64, hash, x, &ret);
483                         } else break;
484                 }
485                 if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
486                 if (dret < 0) break;
487         }
488         ks_destroy(ks);
489         gzclose(fp);
490         free(str->s); free(str);
491         return hash;
492 }
493
494 static int gl2cns(float q[16])
495 {
496         int i, j, min_ij;
497         float min, min2;
498         min = min2 = 1e30; min_ij = -1;
499         for (i = 0; i < 4; ++i) {
500                 for (j = i; j < 4; ++j) {
501                         if (q[i<<2|j] < min) min_ij = i<<2|j, min2 = min, min = q[i<<2|j];
502                         else if (q[i<<2|j] < min2) min2 = q[i<<2|j];
503                 }
504         }
505         return (min_ij>>2&3) == (min_ij&3)? 0 : 1<<18 | (min_ij>>2&3)<<16 | (min_ij&3) | (int)(min2 - min + .499) << 2;
506 }
507
508 int main_phase(int argc, char *argv[])
509 {
510         extern void bam_init_header_hash(bam_header_t *header);
511         int c, tid, pos, vpos = 0, n, lasttid = -1, max_vpos = 0;
512         const bam_pileup1_t *plp;
513         bam_plp_t iter;
514         bam_header_t *h;
515         nseq_t *seqs;
516         uint64_t *cns = 0;
517         phaseg_t g;
518         char *fn_list = 0;
519         khash_t(set64) *set = 0;
520         errmod_t *em;
521         uint16_t *bases;
522
523         memset(&g, 0, sizeof(phaseg_t));
524         g.flag = FLAG_FIX_CHIMERA;
525         g.min_varLOD = 37; g.k = 13; g.min_baseQ = 13; g.max_depth = 256;
526         while ((c = getopt(argc, argv, "Q:eFq:k:b:l:D:A:")) >= 0) {
527                 switch (c) {
528                         case 'D': g.max_depth = atoi(optarg); break;
529                         case 'q': g.min_varLOD = atoi(optarg); break;
530                         case 'Q': g.min_baseQ = atoi(optarg); break;
531                         case 'k': g.k = atoi(optarg); break;
532                         case 'F': g.flag &= ~FLAG_FIX_CHIMERA; break;
533                         case 'e': g.flag |= FLAG_LIST_EXCL; break;
534                         case 'A': g.flag |= FLAG_DROP_AMBI; break;
535                         case 'b': g.pre = strdup(optarg); break;
536                         case 'l': fn_list = strdup(optarg); break;
537                 }
538         }
539         if (argc == optind) {
540                 fprintf(stderr, "\n");
541                 fprintf(stderr, "Usage:   samtools phase [options] <in.bam>\n\n");
542                 fprintf(stderr, "Options: -k INT    block length [%d]\n", g.k);
543                 fprintf(stderr, "         -b STR    prefix of BAMs to output [null]\n");
544                 fprintf(stderr, "         -q INT    min het phred-LOD [%d]\n", g.min_varLOD);
545                 fprintf(stderr, "         -Q INT    min base quality in het calling [%d]\n", g.min_baseQ);
546                 fprintf(stderr, "         -D INT    max read depth [%d]\n", g.max_depth);
547 //              fprintf(stderr, "         -l FILE   list of sites to phase [null]\n");
548                 fprintf(stderr, "         -F        do not attempt to fix chimeras\n");
549                 fprintf(stderr, "         -A        drop reads with ambiguous phase\n");
550 //              fprintf(stderr, "         -e        do not discover SNPs (effective with -l)\n");
551                 fprintf(stderr, "\n");
552                 return 1;
553         }
554         g.fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
555         h = bam_header_read(g.fp);
556         if (fn_list) { // read the list of sites to phase
557                 bam_init_header_hash(h);
558                 set = loadpos(fn_list, h);
559                 free(fn_list);
560         } else g.flag &= ~FLAG_LIST_EXCL;
561         if (g.pre) { // open BAMs to write
562                 char *s = malloc(strlen(g.pre) + 20);
563                 strcpy(s, g.pre); strcat(s, ".0.bam"); g.out[0] = bam_open(s, "w");
564                 strcpy(s, g.pre); strcat(s, ".1.bam"); g.out[1] = bam_open(s, "w");
565                 strcpy(s, g.pre); strcat(s, ".chimera.bam"); g.out[2] = bam_open(s, "w");
566                 for (c = 0; c <= 2; ++c) bam_header_write(g.out[c], h);
567                 free(s);
568         }
569
570         iter = bam_plp_init(readaln, &g);
571         g.vpos_shift = 0;
572         seqs = kh_init(64);
573         em = errmod_init(1. - 0.83);
574         bases = calloc(g.max_depth, 2);
575         printf("CC\n");
576         printf("CC\tDescriptions:\nCC\n");
577         printf("CC\t  CC      comments\n");
578         printf("CC\t  PS      start of a phase set\n");
579         printf("CC\t  FL      filtered region\n");
580         printf("CC\t  M[012]  markers; 0 for singletons, 1 for phased and 2 for filtered\n");
581         printf("CC\t  EV      supporting reads; SAM format\n");
582         printf("CC\t  //      end of a phase set\nCC\n");
583         printf("CC\tFormats of PS, FL and M[012] lines (1-based coordinates):\nCC\n");
584         printf("CC\t  PS  chr  phaseSetStart  phaseSetEnd\n");
585         printf("CC\t  FL  chr  filterStart    filterEnd\n");
586         printf("CC\t  M?  chr  PS  pos  allele0  allele1  hetIndex  #supports0  #errors0  #supp1  #err1\n");
587         printf("CC\nCC\n");
588         fflush(stdout);
589         while ((plp = bam_plp_auto(iter, &tid, &pos, &n)) != 0) {
590                 int i, k, c, tmp, dophase = 1, in_set = 0;
591                 float q[16];
592                 if (tid < 0) break;
593                 if (tid != lasttid) { // change of chromosome
594                         g.vpos_shift = 0;
595                         if (lasttid >= 0) {
596                                 seqs = shrink_hash(seqs);
597                                 phase(&g, h->target_name[lasttid], vpos, cns, seqs);
598                                 update_vpos(0x7fffffff, seqs);
599                         }
600                         lasttid = tid;
601                         vpos = 0;
602                 }
603                 if (set && kh_get(set64, set, (uint64_t)tid<<32 | pos) != kh_end(set)) in_set = 1;
604                 if (n > g.max_depth) continue; // do not proceed if the depth is too high
605                 // fill the bases array and check if there is a variant
606                 for (i = k = 0; i < n; ++i) {
607                         const bam_pileup1_t *p = plp + i;
608                         uint8_t *seq;
609                         int q, baseQ, b;
610                         if (p->is_del || p->is_refskip) continue;
611                         baseQ = bam1_qual(p->b)[p->qpos];
612                         if (baseQ < g.min_baseQ) continue;
613                         seq = bam1_seq(p->b);
614                         b = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos)];
615                         if (b > 3) continue;
616                         q = baseQ < p->b->core.qual? baseQ : p->b->core.qual;
617                         if (q < 4) q = 4;
618                         if (q > 63) q = 63;
619                         bases[k++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
620                 }
621                 if (k == 0) continue;
622                 errmod_cal(em, k, 4, bases, q); // compute genotype likelihood
623                 c = gl2cns(q); // get the consensus
624                 // tell if to proceed
625                 if (set && (g.flag&FLAG_LIST_EXCL) && !in_set) continue; // not in the list
626                 if (!in_set && (c&0xffff)>>2 < g.min_varLOD) continue; // not a variant
627                 // add the variant
628                 if (vpos == max_vpos) {
629                         max_vpos = max_vpos? max_vpos<<1 : 128;
630                         cns = realloc(cns, max_vpos * 8);
631                 }
632                 cns[vpos] = (uint64_t)pos<<32 | c;
633                 for (i = 0; i < n; ++i) {
634                         const bam_pileup1_t *p = plp + i;
635                         uint64_t key;
636                         khint_t k;
637                         uint8_t *seq = bam1_seq(p->b);
638                         frag_t *f;
639                         if (p->is_del || p->is_refskip) continue;
640                         if (p->b->core.qual == 0) continue;
641                         // get the base code
642                         c = nt16_nt4_table[(int)bam1_seqi(seq, p->qpos)];
643                         if (c == (cns[vpos]&3)) c = 1;
644                         else if (c == (cns[vpos]>>16&3)) c = 2;
645                         else c = 0;
646                         // write to seqs
647                         key = X31_hash_string(bam1_qname(p->b));
648                         k = kh_put(64, seqs, key, &tmp);
649                         f = &kh_val(seqs, k);
650                         if (tmp == 0) { // present in the hash table
651                                 if (vpos - f->vpos + 1 < MAX_VARS) {
652                                         f->vlen = vpos - f->vpos + 1;
653                                         f->seq[f->vlen-1] = c;
654                                         f->end = bam_calend(&p->b->core, bam1_cigar(p->b));
655                                 }
656                                 dophase = 0;
657                         } else { // absent
658                                 memset(f->seq, 0, MAX_VARS);
659                                 f->beg = p->b->core.pos;
660                                 f->end = bam_calend(&p->b->core, bam1_cigar(p->b));
661                                 f->vpos = vpos, f->vlen = 1, f->seq[0] = c, f->single = f->phased = f->flip = f->ambig = 0;
662                         }
663                 }
664                 if (dophase) {
665                         seqs = shrink_hash(seqs);
666                         phase(&g, h->target_name[tid], vpos, cns, seqs);
667                         update_vpos(vpos, seqs);
668                         cns[0] = cns[vpos];
669                         vpos = 0;
670                 }
671                 ++vpos;
672         }
673         if (tid >= 0) phase(&g, h->target_name[tid], vpos, cns, seqs);
674         bam_header_destroy(h);
675         bam_plp_destroy(iter);
676         bam_close(g.fp);
677         kh_destroy(64, seqs);
678         kh_destroy(set64, set);
679         free(cns);
680         errmod_destroy(em);
681         free(bases);
682         if (g.pre) {
683                 for (c = 0; c <= 2; ++c) bam_close(g.out[c]);
684                 free(g.pre); free(g.b);
685         }
686         return 0;
687 }