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