11 #define MINUS_CONST 0x10000000
12 #define INDEL_WINDOW_SIZE 50
15 void *bcf_call_add_rg(void *_hash, const char *hdtext, const char *list)
17 const char *s, *p, *q, *r, *t;
19 if (list == 0 || hdtext == 0) return _hash;
20 if (_hash == 0) _hash = kh_init(rg);
21 hash = (khash_t(rg)*)_hash;
22 if ((s = strstr(hdtext, "@RG\t")) == 0) return hash;
24 t = strstr(s + 4, "@RG\t"); // the next @RG
25 if ((p = strstr(s, "\tID:")) != 0) p += 4;
26 if ((q = strstr(s, "\tPL:")) != 0) q += 4;
27 if (p && q && (t == 0 || (p < t && q < t))) { // ID and PL are both present
30 for (r = p; *r && *r != '\t' && *r != '\n'; ++r); lp = r - p;
31 for (r = q; *r && *r != '\t' && *r != '\n'; ++r); lq = r - q;
32 x = calloc((lp > lq? lp : lq) + 1, 1);
33 for (r = q; *r && *r != '\t' && *r != '\n'; ++r) x[r-q] = *r;
34 if (strstr(list, x)) { // insert ID to the hash table
37 for (r = p; *r && *r != '\t' && *r != '\n'; ++r) x[r-p] = *r;
39 k = kh_get(rg, hash, x);
40 if (k == kh_end(hash)) k = kh_put(rg, hash, x, &ret);
49 void bcf_call_del_rghash(void *_hash)
52 khash_t(rg) *hash = (khash_t(rg)*)_hash;
53 if (hash == 0) return;
54 for (k = kh_begin(hash); k < kh_end(hash); ++k)
55 if (kh_exist(hash, k))
56 free((char*)kh_key(hash, k));
60 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
62 int k, x = c->pos, y = 0, last_y = 0;
64 for (k = 0; k < c->n_cigar; ++k) {
65 int op = cigar[k] & BAM_CIGAR_MASK;
66 int l = cigar[k] >> BAM_CIGAR_SHIFT;
67 if (op == BAM_CMATCH) {
68 if (c->pos > tpos) return y;
71 return y + (tpos - x);
75 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
76 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
78 *_tpos = is_left? x : x + l;
87 // FIXME: check if the inserted sequence is consistent with the homopolymer run
88 // l is the relative gap length and l_run is the length of the homopolymer on the reference
89 static inline int est_seqQ(const bcf_callaux_t *bca, int l, int l_run)
92 q = bca->openQ + bca->extQ * (abs(l) - 1);
93 qh = l_run >= 3? (int)(bca->tandemQ * (double)abs(l) / l_run + .499) : 1000;
94 return q < qh? q : qh;
97 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
99 int i, j, max = 0, max_i = pos, score = 0;
101 for (i = pos + 1, j = 0; ref[i]; ++i, ++j) {
102 if (ins4) score += (toupper(ref[i]) != "ACGTN"[(int)ins4[j%l]])? -10 : 1;
103 else score += (toupper(ref[i]) != toupper(ref[pos+1+j%l]))? -10 : 1;
104 if (score < 0) break;
105 if (max < score) max = score, max_i = i;
110 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
113 extern void ks_introsort_uint32_t(int, uint32_t*);
114 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;
115 char *inscns = 0, *ref2, *query;
116 khash_t(rg) *hash = (khash_t(rg)*)rghash;
117 if (ref == 0 || bca == 0) return -1;
118 // mark filtered reads
121 for (s = N = 0; s < n; ++s) {
122 for (i = 0; i < n_plp[s]; ++i) {
123 bam_pileup1_t *p = plp[s] + i;
124 const uint8_t *rg = bam_aux_get(p->b, "RG");
125 p->aux = 1; // filtered by default
127 khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
128 if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
132 if (N == 0) return -1; // no reads left
134 // determine if there is a gap
135 for (s = N = 0; s < n; ++s) {
136 for (i = 0; i < n_plp[s]; ++i)
137 if (plp[s][i].indel != 0) break;
138 if (i < n_plp[s]) break;
140 if (s == n) return -1; // there is no indel at this position.
141 for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
142 { // find out how many types of indels are present
145 aux = calloc(N + 1, 4);
147 aux[m++] = MINUS_CONST; // zero indel is always a type
148 for (s = 0; s < n; ++s) {
149 for (i = 0; i < n_plp[s]; ++i) {
150 const bam_pileup1_t *p = plp[s] + i;
151 if (p->indel != 0 && (rghash == 0 || p->aux == 0))
152 aux[m++] = MINUS_CONST + p->indel;
153 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
154 if (j > max_rd_len) max_rd_len = j;
157 ks_introsort(uint32_t, m, aux);
158 // squeeze out identical types
159 for (i = 1, n_types = 1; i < m; ++i)
160 if (aux[i] != aux[i-1]) ++n_types;
161 if (n_types == 1) { // no indels
162 free(aux); return -1;
164 types = (int*)calloc(n_types, sizeof(int));
166 types[t++] = aux[0] - MINUS_CONST;
167 for (i = 1; i < m; ++i)
168 if (aux[i] != aux[i-1])
169 types[t++] = aux[i] - MINUS_CONST;
171 for (t = 0; t < n_types; ++t)
172 if (types[t] == 0) break;
173 ref_type = t; // the index of the reference type (0)
174 assert(n_types < 64);
176 { // calculate left and right boundary
177 left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
178 right = pos + INDEL_WINDOW_SIZE;
179 if (types[0] < 0) right -= types[0];
180 // in case the alignments stand out the reference
181 for (i = pos; i < right; ++i)
182 if (ref[i] == 0) break;
185 { // the length of the homopolymer run around the current position
186 int c = bam_nt16_table[(int)ref[pos + 1]];
187 if (c == 15) l_run = 1;
189 for (i = pos + 2; ref[i]; ++i)
190 if (bam_nt16_table[(int)ref[i]] != c) break;
192 for (i = pos; i >= 0; --i)
193 if (bam_nt16_table[(int)ref[i]] != c) break;
197 // construct the consensus sequence
198 max_ins = types[n_types - 1]; // max_ins is at least 0
200 int *inscns_aux = calloc(4 * n_types * max_ins, sizeof(int));
201 // count the number of occurrences of each base at each position for each type of insertion
202 for (t = 0; t < n_types; ++t) {
204 for (s = 0; s < n; ++s) {
205 for (i = 0; i < n_plp[s]; ++i) {
206 bam_pileup1_t *p = plp[s] + i;
207 if (p->indel == types[t]) {
208 uint8_t *seq = bam1_seq(p->b);
209 for (k = 1; k <= p->indel; ++k) {
210 int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
211 if (c < 4) ++inscns_aux[(t*max_ins+(k-1))*4 + c];
218 // use the majority rule to construct the consensus
219 inscns = calloc(n_types * max_ins, 1);
220 for (t = 0; t < n_types; ++t) {
221 for (j = 0; j < types[t]; ++j) {
222 int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*4];
223 for (k = 0; k < 4; ++k)
225 max = ia[k], max_k = k;
226 inscns[t*max_ins + j] = max? max_k : 4;
231 // compute the likelihood given each type of indel for each read
232 ref2 = calloc(right - left + max_ins + 2, 1);
233 query = calloc(right - left + max_rd_len + max_ins + 2, 1);
234 score = calloc(N * n_types, sizeof(int));
236 for (t = 0; t < n_types; ++t) {
238 ka_param2_t ap = ka_param2_qual;
239 ap.band_width = abs(types[t]) + 3;
241 if (types[t] == 0) ir = 0;
242 else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
243 else ir = est_indelreg(pos, ref, -types[t], 0);
244 if (ir > bca->indelreg) bca->indelreg = ir;
245 // fprintf(stderr, "%d, %d, %d\n", pos, types[t], ir);
247 for (k = 0, j = left; j <= pos; ++j)
248 ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
249 if (types[t] <= 0) j += -types[t];
250 else for (l = 0; l < types[t]; ++l)
251 ref2[k++] = inscns[t*max_ins + l];
252 if (types[0] < 0) { // mask deleted sequences to avoid a particular error in the model.
253 int jj, tmp = types[t] >= 0? -types[0] : -types[0] + types[t];
254 for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j)
257 for (; j < right && ref[j]; ++j)
258 ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
259 if (j < right) right = j;
260 // align each read to ref2
261 for (s = K = 0; s < n; ++s) {
262 for (i = 0; i < n_plp[s]; ++i, ++K) {
263 bam_pileup1_t *p = plp[s] + i;
264 int qbeg, qend, tbeg, tend, sc;
265 uint8_t *seq = bam1_seq(p->b);
266 // determine the start and end of sequences for alignment
267 qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left, 0, &tbeg);
268 qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
271 tbeg = tbeg - l > left? tbeg - l : left;
273 // write the query sequence
274 for (l = qbeg; l < qend; ++l)
275 query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
276 // do alignment; this takes most of computing time for indel calling
277 sc = ka_global_score((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
278 (uint8_t*)query, qend - qbeg, &ap);
279 score[K*n_types + t] = -sc;
281 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
282 fputc("ACGTN"[(int)ref2[tbeg-left+l]], stderr);
284 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], stderr);
286 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);
291 free(ref2); free(query);
294 sc = alloca(n_types * sizeof(int));
295 sumq = alloca(n_types * sizeof(int));
296 memset(sumq, 0, sizeof(int) * n_types);
297 for (s = K = 0; s < n; ++s) {
298 for (i = 0; i < n_plp[s]; ++i, ++K) {
299 bam_pileup1_t *p = plp[s] + i;
300 int *sct = &score[K*n_types], indelQ, seqQ;
301 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
302 for (t = 1; t < n_types; ++t) // insertion sort
303 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
304 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
305 /* errmod_cal() assumes that if the call is wrong, the
306 * likelihoods of other events are equal. This is about
307 * right for substitutions, but is not desired for
308 * indels. To reuse errmod_cal(), I have to make
309 * compromise for multi-allelic indels.
311 if ((sc[0]&0x3f) == ref_type) {
312 indelQ = (sc[1]>>6) - (sc[0]>>6);
313 seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
315 for (t = 0; t < n_types; ++t) // look for the reference type
316 if ((sc[t]&0x3f) == ref_type) break;
317 indelQ = (sc[t]>>6) - (sc[0]>>6);
318 seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
320 if (sc[0]>>6 > MAX_SCORE) indelQ = 0; // too many mismatches; something bad possibly happened
321 p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ;
322 sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
323 // 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);
326 // determine bca->indel_types[] and bca->inscns
327 bca->maxins = max_ins;
328 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
329 for (t = 0; t < n_types; ++t)
330 sumq[t] = sumq[t]<<6 | t;
331 for (t = 1; t < n_types; ++t) // insertion sort
332 for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
333 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
334 for (t = 0; t < n_types; ++t) // look for the reference type
335 if ((sumq[t]&0x3f) == ref_type) break;
336 if (t) { // then move the reference type to the first
338 for (; t > 0; --t) sumq[t] = sumq[t-1];
341 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
342 for (t = 0; t < 4 && t < n_types; ++t) {
343 bca->indel_types[t] = types[sumq[t]&0x3f];
344 memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
347 for (s = n_alt = 0; s < n; ++s) {
348 for (i = 0; i < n_plp[s]; ++i) {
349 bam_pileup1_t *p = plp[s] + i;
350 int x = types[p->aux>>16&0x3f];
351 for (j = 0; j < 4; ++j)
352 if (x == bca->indel_types[j]) break;
353 p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
354 if ((p->aux>>16&0x3f) > 0) ++n_alt;
355 // fprintf(stderr, "X pos=%d read=%d:%d name=%s call=%d type=%d q=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), p->aux>>16&63, bca->indel_types[p->aux>>16&63], p->aux&0xff, p->aux>>8&0xff);
361 free(types); free(inscns);
362 return n_alt > 0? 0 : -1;