8 #define MINUS_CONST 0x10000000
9 #define INDEL_WINDOW_SIZE 50
11 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
13 int k, x = c->pos, y = 0, last_y = 0;
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;
22 return y + (tpos - x);
26 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
27 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
29 *_tpos = is_left? x : x + l;
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)
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;
48 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
50 int i, j, max = 0, max_i = pos, score = 0;
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;
56 if (max < score) max = score, max_i = i;
61 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref)
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 for (i = 0; i < n_plp[s]; ++i)
70 if (plp[s][i].indel != 0) break;
71 if (i < n_plp[s]) break;
73 if (s == n) return -1; // there is no indel at this position.
74 for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
75 { // find out how many types of indels are present
78 aux = calloc(N + 1, 4);
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;
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;
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));
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;
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);
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;
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;
120 for (i = pos + 2; ref[i]; ++i)
121 if (bam_nt16_table[(int)ref[i]] != c) break;
123 for (i = pos; i >= 0; --i)
124 if (bam_nt16_table[(int)ref[i]] != c) break;
128 // construct the consensus sequence
129 max_ins = types[n_types - 1]; // max_ins is at least 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) {
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];
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)
156 max = ia[k], max_k = k;
157 inscns[t*max_ins + j] = max? max_k : 4;
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));
167 for (t = 0; t < n_types; ++t) {
169 ka_param2_t ap = ka_param2_qual;
170 ap.band_width = abs(types[t]) + 3;
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);
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)
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);
202 tbeg = tbeg - l > left? tbeg - l : left;
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;
212 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
213 fputc("ACGTN"[(int)ref2[tbeg-left+l]], stderr);
215 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], 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);
222 free(ref2); free(query);
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, seqQ;
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.
242 if ((sc[0]&0x3f) == ref_type) {
243 indelQ = (sc[1]>>6) - (sc[0]>>6);
244 seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
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 seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
251 p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ;
252 sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
253 // 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);
256 // determine bca->indel_types[] and bca->inscns
257 bca->maxins = max_ins;
258 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
259 for (t = 0; t < n_types; ++t)
260 sumq[t] = sumq[t]<<6 | t;
261 for (t = 1; t < n_types; ++t) // insertion sort
262 for (j = t; j > 0 && sumq[j] < sumq[j-1]; --j)
263 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
264 for (t = 0; t < n_types; ++t) // look for the reference type
265 if ((sumq[t]&0x3f) == ref_type) break;
266 if (t) { // then move the reference type to the first
268 for (; t > 0; --t) sumq[t] = sumq[t-1];
271 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
272 for (t = 0; t < 4 && t < n_types; ++t) {
273 bca->indel_types[t] = types[sumq[t]&0x3f];
274 memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
277 for (s = K = 0; s < n; ++s) {
278 for (i = 0; i < n_plp[s]; ++i, ++K) {
279 bam_pileup1_t *p = plp[s] + i;
280 int x = types[p->aux>>16&0x3f];
281 for (j = 0; j < 4; ++j)
282 if (x == bca->indel_types[j]) break;
283 p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
284 // 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);
290 free(types); free(inscns);