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