Imported Upstream version 0.5
[pysam.git] / samtools / bam2bcf.c.pysam.c
1 #include "pysam.h"
2
3 #include <math.h>
4 #include <stdint.h>
5 #include "bam.h"
6 #include "kstring.h"
7 #include "bam2bcf.h"
8 #include "errmod.h"
9 #include "bcftools/bcf.h"
10
11 extern  void ks_introsort_uint32_t(size_t n, uint32_t a[]);
12
13 #define CALL_ETA 0.03f
14 #define CALL_MAX 256
15 #define CALL_DEFTHETA 0.83f
16 #define DEF_MAPQ 20
17
18 #define CAP_DIST 25
19
20 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ)
21 {
22         bcf_callaux_t *bca;
23         if (theta <= 0.) theta = CALL_DEFTHETA;
24         bca = calloc(1, sizeof(bcf_callaux_t));
25         bca->capQ = 60;
26         bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 100;
27         bca->min_baseQ = min_baseQ;
28         bca->e = errmod_init(1. - theta);
29         bca->min_frac = 0.002;
30         bca->min_support = 1;
31         return bca;
32 }
33
34 void bcf_call_destroy(bcf_callaux_t *bca)
35 {
36         if (bca == 0) return;
37         errmod_destroy(bca->e);
38         free(bca->bases); free(bca->inscns); free(bca);
39 }
40 /* ref_base is the 4-bit representation of the reference base. It is
41  * negative if we are looking at an indel. */
42 int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r)
43 {
44         int i, n, ref4, is_indel, ori_depth = 0;
45         memset(r, 0, sizeof(bcf_callret1_t));
46         if (ref_base >= 0) {
47                 ref4 = bam_nt16_nt4_table[ref_base];
48                 is_indel = 0;
49         } else ref4 = 4, is_indel = 1;
50         if (_n == 0) return -1;
51         // enlarge the bases array if necessary
52         if (bca->max_bases < _n) {
53                 bca->max_bases = _n;
54                 kroundup32(bca->max_bases);
55                 bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
56         }
57         // fill the bases array
58         memset(r, 0, sizeof(bcf_callret1_t));
59         for (i = n = 0; i < _n; ++i) {
60                 const bam_pileup1_t *p = pl + i;
61                 int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
62                 // set base
63                 if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
64                 ++ori_depth;
65                 baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality
66                 seqQ = is_indel? (p->aux>>8&0xff) : 99;
67                 if (q < bca->min_baseQ) continue;
68                 if (q > seqQ) q = seqQ;
69                 mapQ = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
70                 mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
71                 if (q > mapQ) q = mapQ;
72                 if (q > 63) q = 63;
73                 if (q < 4) q = 4;
74                 if (!is_indel) {
75                         b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
76                         b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
77                         is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
78                 } else {
79                         b = p->aux>>16&0x3f;
80                         is_diff = (b != 0);
81                 }
82                 bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
83                 // collect annotations
84                 if (b < 4) r->qsum[b] += q;
85                 ++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
86                 min_dist = p->b->core.l_qseq - 1 - p->qpos;
87                 if (min_dist > p->qpos) min_dist = p->qpos;
88                 if (min_dist > CAP_DIST) min_dist = CAP_DIST;
89                 r->anno[1<<2|is_diff<<1|0] += baseQ;
90                 r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
91                 r->anno[2<<2|is_diff<<1|0] += mapQ;
92                 r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
93                 r->anno[3<<2|is_diff<<1|0] += min_dist;
94                 r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
95         }
96         r->depth = n; r->ori_depth = ori_depth;
97         // glfgen
98         errmod_cal(bca->e, n, 5, bca->bases, r->p);
99         return r->depth;
100 }
101
102 int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, bcf_call_t *call)
103 {
104         int ref4, i, j, qsum[4];
105         int64_t tmp;
106         if (ref_base >= 0) {
107                 call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
108                 if (ref4 > 4) ref4 = 4;
109         } else call->ori_ref = -1, ref4 = 0;
110         // calculate qsum
111         memset(qsum, 0, 4 * sizeof(int));
112         for (i = 0; i < n; ++i)
113                 for (j = 0; j < 4; ++j)
114                         qsum[j] += calls[i].qsum[j];
115         for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
116         // find the top 2 alleles
117         for (i = 1; i < 4; ++i) // insertion sort
118                 for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
119                         tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
120         // set the reference allele and alternative allele(s)
121         for (i = 0; i < 5; ++i) call->a[i] = -1;
122         call->unseen = -1;
123         call->a[0] = ref4;
124         for (i = 3, j = 1; i >= 0; --i) {
125                 if ((qsum[i]&3) != ref4) {
126                         if (qsum[i]>>2 != 0) call->a[j++] = qsum[i]&3;
127                         else break;
128                 }
129         }
130         if (ref_base >= 0) { // for SNPs, find the "unseen" base
131                 if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
132                         call->unseen = j, call->a[j++] = qsum[i]&3;
133                 call->n_alleles = j;
134         } else {
135                 call->n_alleles = j;
136                 if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
137         }
138         // set the PL array
139         if (call->n < n) {
140                 call->n = n;
141                 call->PL = realloc(call->PL, 15 * n);
142         }
143         {
144                 int x, g[15], z;
145                 double sum_min = 0.;
146                 x = call->n_alleles * (call->n_alleles + 1) / 2;
147                 // get the possible genotypes
148                 for (i = z = 0; i < call->n_alleles; ++i)
149                         for (j = 0; j <= i; ++j)
150                                 g[z++] = call->a[j] * 5 + call->a[i];
151                 for (i = 0; i < n; ++i) {
152                         uint8_t *PL = call->PL + x * i;
153                         const bcf_callret1_t *r = calls + i;
154                         float min = 1e37;
155                         for (j = 0; j < x; ++j)
156                                 if (min > r->p[g[j]]) min = r->p[g[j]];
157                         sum_min += min;
158                         for (j = 0; j < x; ++j) {
159                                 int y;
160                                 y = (int)(r->p[g[j]] - min + .499);
161                                 if (y > 255) y = 255;
162                                 PL[j] = y;
163                         }
164                 }
165 //              if (ref_base < 0) fprintf(pysamerr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
166                 call->shift = (int)(sum_min + .499);
167         }
168         // combine annotations
169         memset(call->anno, 0, 16 * sizeof(int));
170         for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
171                 call->depth += calls[i].depth;
172                 call->ori_depth += calls[i].ori_depth;
173                 for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
174         }
175         return 0;
176 }
177
178 int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP,
179                                  const bcf_callaux_t *bca, const char *ref)
180 {
181         extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
182         kstring_t s;
183         int i, j;
184         b->n_smpl = bc->n;
185         b->tid = tid; b->pos = pos; b->qual = 0;
186         s.s = b->str; s.m = b->m_str; s.l = 0;
187         kputc('\0', &s);
188         if (bc->ori_ref < 0) { // an indel
189                 // write REF
190                 kputc(ref[pos], &s);
191                 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
192                 kputc('\0', &s);
193                 // write ALT
194                 kputc(ref[pos], &s);
195                 for (i = 1; i < 4; ++i) {
196                         if (bc->a[i] < 0) break;
197                         if (i > 1) {
198                                 kputc(',', &s); kputc(ref[pos], &s);
199                         }
200                         if (bca->indel_types[bc->a[i]] < 0) { // deletion
201                                 for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
202                                         kputc(ref[pos+1+j], &s);
203                         } else { // insertion; cannot be a reference unless a bug
204                                 char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
205                                 for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
206                                         kputc("ACGTN"[(int)inscns[j]], &s);
207                                 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
208                         }
209                 }
210                 kputc('\0', &s);
211         } else { // a SNP
212                 kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
213                 for (i = 1; i < 5; ++i) {
214                         if (bc->a[i] < 0) break;
215                         if (i > 1) kputc(',', &s);
216                         kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
217                 }
218                 kputc('\0', &s);
219         }
220         kputc('\0', &s);
221         // INFO
222         if (bc->ori_ref < 0) kputs("INDEL;", &s);
223         kputs("DP=", &s); kputw(bc->ori_depth, &s); kputs(";I16=", &s);
224         for (i = 0; i < 16; ++i) {
225                 if (i) kputc(',', &s);
226                 kputw(bc->anno[i], &s);
227         }
228         kputc('\0', &s);
229         // FMT
230         kputs("PL", &s);
231         if (bcr) {
232                 kputs(":DP", &s);
233                 if (is_SP) kputs(":SP", &s);
234         }
235         kputc('\0', &s);
236         b->m_str = s.m; b->str = s.s; b->l_str = s.l;
237         bcf_sync(b);
238         memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
239         if (bcr) {
240                 uint16_t *dp = (uint16_t*)b->gi[1].data;
241                 uint8_t *sp = is_SP? b->gi[2].data : 0;
242                 for (i = 0; i < bc->n; ++i) {
243                         bcf_callret1_t *p = bcr + i;
244                         dp[i] = p->depth < 0xffff? p->depth : 0xffff;
245                         if (is_SP) {
246                                 if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2
247                                         || p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2)
248                                 {
249                                         sp[i] = 0;
250                                 } else {
251                                         double left, right, two;
252                                         int x;
253                                         kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two);
254                                         x = (int)(-4.343 * log(two) + .499);
255                                         if (x > 255) x = 255;
256                                         sp[i] = x;
257                                 }
258                         }
259                 }
260         }
261         return 0;
262 }