]> git.donarmstrong.com Git - samtools.git/blob - bam2bcf_indel.c
fixed another silly bug in mpileup's indel caller
[samtools.git] / bam2bcf_indel.c
1 #include <assert.h>
2 #include <ctype.h>
3 #include "bam.h"
4 #include "bam2bcf.h"
5 #include "ksort.h"
6 #include "kaln.h"
7
8 #define MINUS_CONST 0x10000000
9 #define INDEL_WINDOW_SIZE 50
10
11 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
12 {
13         int k, x = c->pos, y = 0, last_y = 0;
14         *_tpos = c->pos;
15         for (k = 0; k < c->n_cigar; ++k) {
16                 int op = cigar[k] & BAM_CIGAR_MASK;
17                 int l = cigar[k] >> BAM_CIGAR_SHIFT;
18                 if (op == BAM_CMATCH) {
19                         if (c->pos > tpos) return y;
20                         if (x + l > tpos) {
21                                 *_tpos = tpos;
22                                 return y + (tpos - x);
23                         }
24                         x += l; y += l;
25                         last_y = y;
26                 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
27                 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
28                         if (x + l > tpos) {
29                                 *_tpos = is_left? x : x + l;
30                                 return y;
31                         }
32                         x += l;
33                 }
34         }
35         *_tpos = x;
36         return last_y;
37 }
38 // FIXME: check if the inserted sequence is consistent with the homopolymer run
39 // l is the relative gap length and l_run is the length of the homopolymer on the reference
40 static inline int est_seqQ(const bcf_callaux_t *bca, int l, int l_run)
41 {
42         int q, qh;
43         q = bca->openQ + bca->extQ * (abs(l) - 1);
44         qh = l_run >= 3? (int)(bca->tandemQ * (double)abs(l) / l_run + .499) : 1000;
45         return q < qh? q : qh;
46 }
47
48 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
49 {
50         int i, j, max = 0, max_i = pos, score = 0;
51         l = abs(l);
52         for (i = pos + 1, j = 0; ref[i]; ++i, ++j) {
53                 if (ins4) score += (toupper(ref[i]) != "ACGTN"[(int)ins4[j%l]])? -10 : 1;
54                 else score += (toupper(ref[i]) != toupper(ref[pos+1+j%l]))? -10 : 1;
55                 if (score < 0) break;
56                 if (max < score) max = score, max_i = i;
57         }
58         return max_i - pos;
59 }
60
61 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref)
62 {
63         extern void ks_introsort_uint32_t(int, uint32_t*);
64         int i, s, j, k, t, n_types, *types, max_rd_len, left, right, max_ins, *score, N, K, l_run, ref_type;
65         char *inscns = 0, *ref2, *query;
66         if (ref == 0 || bca == 0) return -1;
67         // determine if there is a gap
68         for (s = N = 0; s < n; ++s) {
69                 N += n_plp[s]; // N is the total number of reads
70                 for (i = 0; i < n_plp[s]; ++i)
71                         if (plp[s][i].indel != 0) break;
72                 if (i < n_plp[s]) break;
73         }
74         if (s == n) return -1; // there is no indel at this position.
75         { // find out how many types of indels are present
76                 int m;
77                 uint32_t *aux;
78                 aux = calloc(N + 1, 4);
79                 m = max_rd_len = 0;
80                 aux[m++] = MINUS_CONST; // zero indel is always a type
81                 for (s = 0; s < n; ++s) {
82                         for (i = 0; i < n_plp[s]; ++i) {
83                                 const bam_pileup1_t *p = plp[s] + i;
84                                 if (p->indel != 0)
85                                         aux[m++] = MINUS_CONST + p->indel;
86                                 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
87                                 if (j > max_rd_len) max_rd_len = j;
88                         }
89                 }
90                 ks_introsort(uint32_t, m, aux);
91                 // squeeze out identical types
92                 for (i = 1, n_types = 1; i < m; ++i)
93                         if (aux[i] != aux[i-1]) ++n_types;
94                 assert(n_types > 1); // there must at least one type of non-reference indel
95                 types = (int*)calloc(n_types, sizeof(int));
96                 t = 0;
97                 types[t++] = aux[0] - MINUS_CONST; 
98                 for (i = 1; i < m; ++i)
99                         if (aux[i] != aux[i-1])
100                                 types[t++] = aux[i] - MINUS_CONST;
101                 free(aux);
102                 for (t = 0; t < n_types; ++t)
103                         if (types[t] == 0) break;
104                 ref_type = t; // the index of the reference type (0)
105                 assert(n_types < 64);
106         }
107         { // calculate left and right boundary
108                 left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
109                 right = pos + INDEL_WINDOW_SIZE;
110                 if (types[0] < 0) right -= types[0];
111                 // in case the alignments stand out the reference
112                 for (i = pos; i < right; ++i)
113                         if (ref[i] == 0) break;
114                 right = i;
115         }
116         { // the length of the homopolymer run around the current position
117                 int c = bam_nt16_table[(int)ref[pos + 1]];
118                 if (c == 15) l_run = 1;
119                 else {
120                         for (i = pos + 2; ref[i]; ++i)
121                                 if (bam_nt16_table[(int)ref[i]] != c) break;
122                         l_run = i;
123                         for (i = pos; i >= 0; --i)
124                                 if (bam_nt16_table[(int)ref[i]] != c) break;
125                         l_run -= i + 1;
126                 }
127         }
128         // construct the consensus sequence
129         max_ins = types[n_types - 1]; // max_ins is at least 0
130         if (max_ins > 0) {
131                 int *inscns_aux = calloc(4 * n_types * max_ins, sizeof(int));
132                 // count the number of occurrences of each base at each position for each type of insertion
133                 for (t = 0; t < n_types; ++t) {
134                         if (types[t] > 0) {
135                                 for (s = 0; s < n; ++s) {
136                                         for (i = 0; i < n_plp[s]; ++i) {
137                                                 bam_pileup1_t *p = plp[s] + i;
138                                                 if (p->indel == types[t]) {
139                                                         uint8_t *seq = bam1_seq(p->b);
140                                                         for (k = 1; k <= p->indel; ++k) {
141                                                                 int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
142                                                                 if (c < 4) ++inscns_aux[(t*max_ins+(k-1))*4 + c];
143                                                         }
144                                                 }
145                                         }
146                                 }
147                         }
148                 }
149                 // use the majority rule to construct the consensus
150                 inscns = calloc(n_types * max_ins, 1);
151                 for (t = 0; t < n_types; ++t) {
152                         for (j = 0; j < types[t]; ++j) {
153                                 int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*4];
154                                 for (k = 0; k < 4; ++k)
155                                         if (ia[k] > max)
156                                                 max = ia[k], max_k = k;
157                                 inscns[t*max_ins + j] = max? max_k : 4;
158                         }
159                 }
160                 free(inscns_aux);
161         }
162         // compute the likelihood given each type of indel for each read
163         ref2  = calloc(right - left + max_ins + 2, 1);
164         query = calloc(right - left + max_rd_len + max_ins + 2, 1);
165         score = calloc(N * n_types, sizeof(int));
166         bca->indelreg = 0;
167         for (t = 0; t < n_types; ++t) {
168                 int l, ir;
169                 ka_param2_t ap = ka_param2_qual;
170                 ap.band_width = abs(types[t]) + 3;
171                 // compute indelreg
172                 if (types[t] == 0) ir = 0;
173                 else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
174                 else ir = est_indelreg(pos, ref, -types[t], 0);
175                 if (ir > bca->indelreg) bca->indelreg = ir;
176 //              fprintf(stderr, "%d, %d, %d\n", pos, types[t], ir);
177                 // write ref2
178                 for (k = 0, j = left; j <= pos; ++j)
179                         ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
180                 if (types[t] <= 0) j += -types[t];
181                 else for (l = 0; l < types[t]; ++l)
182                                  ref2[k++] = inscns[t*max_ins + l];
183                 if (types[0] < 0) { // mask deleted sequences to avoid a particular error in the model.
184                         int jj, tmp = types[t] >= 0? -types[0] : -types[0] + types[t];
185                         for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j)
186                                 ref2[k++] = 4;
187                 }
188                 for (; j < right && ref[j]; ++j)
189                         ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
190                 if (j < right) right = j;
191                 // align each read to ref2
192                 for (s = K = 0; s < n; ++s) {
193                         for (i = 0; i < n_plp[s]; ++i, ++K) {
194                                 bam_pileup1_t *p = plp[s] + i;
195                                 int qbeg, qend, tbeg, tend, sc;
196                                 uint8_t *seq = bam1_seq(p->b);
197                                 // determine the start and end of sequences for alignment
198                                 qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left,  0, &tbeg);
199                                 qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
200                                 if (types[t] < 0) {
201                                         int l = -types[t];
202                                         tbeg = tbeg - l > left?  tbeg - l : left;
203                                 }
204                                 // write the query sequence
205                                 for (l = qbeg; l < qend; ++l)
206                                         query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
207                                 // do alignment; this takes most of computing time for indel calling
208                                 sc = ka_global_score((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
209                                                                          (uint8_t*)query, qend - qbeg, &ap);
210                                 score[K*n_types + t] = -sc;
211 /*
212                                 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
213                                         fputc("ACGTN"[(int)ref2[tbeg-left+l]], stderr);
214                                 fputc('\n', stderr);
215                                 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], stderr);
216                                 fputc('\n', stderr);
217                                 fprintf(stderr, "pos=%d type=%d read=%d:%d name=%s qbeg=%d tbeg=%d score=%d\n", pos, types[t], s, i, bam1_qname(p->b), qbeg, tbeg, sc);
218 */
219                         }
220                 }
221         }
222         free(ref2); free(query);
223         { // compute indelQ
224                 int *sc, tmp, *sumq;
225                 sc   = alloca(n_types * sizeof(int));
226                 sumq = alloca(n_types * sizeof(int));
227                 memset(sumq, 0, sizeof(int) * n_types);
228                 for (s = K = 0; s < n; ++s) {
229                         for (i = 0; i < n_plp[s]; ++i, ++K) {
230                                 bam_pileup1_t *p = plp[s] + i;
231                                 int *sct = &score[K*n_types], indelQ;
232                                 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
233                                 for (t = 1; t < n_types; ++t) // insertion sort
234                                         for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
235                                                 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
236                                 /* errmod_cal() assumes that if the call is wrong, the
237                                  * likelihoods of other events are equal. This is about
238                                  * right for substitutions, but is not desired for
239                                  * indels. To reuse errmod_cal(), I have to make
240                                  * compromise for multi-allelic indels.
241                                  */
242                                 if ((sc[0]&0x3f) == ref_type) {
243                                         indelQ = (sc[1]>>6) - (sc[0]>>6);
244                                         tmp = est_seqQ(bca, types[sc[1]&0x3f], l_run);
245                                 } else {
246                                         for (t = 0; t < n_types; ++t) // look for the reference type
247                                                 if ((sc[t]&0x3f) == ref_type) break;
248                                         indelQ = (sc[t]>>6) - (sc[0]>>6);
249                                         tmp = est_seqQ(bca, types[sc[0]&0x3f], l_run);
250                                 }
251                                 if (indelQ > tmp) indelQ = tmp;
252                                 p->aux = (sc[0]&0x3f)<<8 | indelQ;
253                                 sumq[sc[0]&0x3f] += indelQ;
254 //                              fprintf(stderr, "pos=%d read=%d:%d name=%s call=%d q=%d\n", pos, s, i, bam1_qname(p->b), types[sc[0]&0x3f], indelQ);
255                         }
256                 }
257                 // determine bca->indel_types[] and bca->inscns
258                 bca->maxins = max_ins;
259                 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
260                 for (t = 0; t < n_types; ++t)
261                         sumq[t] = sumq[t]<<6 | t;
262                 for (t = 1; t < n_types; ++t) // insertion sort
263                         for (j = t; j > 0 && sumq[j] < sumq[j-1]; --j)
264                                 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
265                 for (t = 0; t < n_types; ++t) // look for the reference type
266                         if ((sumq[t]&0x3f) == ref_type) break;
267                 if (t) { // then move the reference type to the first
268                         tmp = sumq[t];
269                         for (; t > 0; --t) sumq[t] = sumq[t-1];
270                         sumq[0] = tmp;
271                 }
272                 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
273                 for (t = 0; t < 4 && t < n_types; ++t) {
274                         bca->indel_types[t] = types[sumq[t]&0x3f];
275                         memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
276                 }
277                 // update p->aux
278                 for (s = K = 0; s < n; ++s) {
279                         for (i = 0; i < n_plp[s]; ++i, ++K) {
280                                 bam_pileup1_t *p = plp[s] + i;
281                                 int x = types[p->aux>>8&0x3f];
282                                 for (j = 0; j < 4; ++j)
283                                         if (x == bca->indel_types[j]) break;
284                                 p->aux = j<<8 | (j == 4? 0 : (p->aux&0xff));
285 //                              fprintf(stderr, "pos=%d read=%d:%d name=%s call=%d q=%d\n", pos, s, i, bam1_qname(p->b), p->aux>>8&63, p->aux&0xff);
286                         }
287                 }               
288         }
289         free(score);
290         // free
291         free(types); free(inscns);
292         return 0;
293 }