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