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