9 #define MINUS_CONST 0x10000000
10 #define INDEL_WINDOW_SIZE 50
11 #define INDEL_BAD_SCORE 10000
13 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
15 int k, x = c->pos, y = 0, last_y = 0;
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;
24 return y + (tpos - x);
28 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
29 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
31 *_tpos = is_left? x : x + l;
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)
44 q = bca->openQ + bca->extQ * (abs(l) - 1);
45 qh = l_run >= 3? (int)(bca->tandemQ * (double)l / l_run + .499) : 1000;
46 return q < qh? q : qh;
49 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref)
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 = 0; s < n; ++s) {
57 for (i = 0; i < n_plp[s]; ++i)
58 if (plp[s][i].indel != 0) break;
59 if (i < n_plp[s]) break;
61 if (s == n) return -1; // there is no indel at this position.
62 { // find out how many types of indels are present
65 aux = calloc(n + 1, 4);
67 aux[m++] = MINUS_CONST; // zero indel is always a type
68 for (s = N = 0; s < n; ++s) {
69 N += n_plp[s]; // N is the total number of reads
70 for (i = 0; i < n_plp[s]; ++i) {
71 const bam_pileup1_t *p = plp[s] + i;
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;
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));
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;
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)
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;
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;
108 for (i = pos + 2; ref[i]; ++i)
109 if (bam_nt16_table[(int)ref[i]] != c) break;
111 for (i = pos; i >= 0; --i)
112 if (bam_nt16_table[(int)ref[i]] != c) break;
116 // construct the consensus sequence
117 max_ins = types[n_types - 1]; // max_ins is at least 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) {
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];
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)
144 max = ia[k], max_k = k;
145 inscns[t*max_ins + j] = max? max_k : 4;
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) {
156 ka_param2_t ap = ka_param2_qual;
157 ap.band_width = abs(types[t]) + 3;
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)
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 assert(tbeg >= left);
182 // write the query sequence
183 for (l = qbeg; l < qend; ++l)
184 query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
185 // do alignment; this takes most of computing time for indel calling
187 for (sc = 0; sc < tend - tbeg + types[t]; ++sc)
188 fputc("ACGTN"[(int)ref2[tbeg-left+sc]], stderr);
190 for (sc = 0; sc < qend - qbeg; ++sc) fputc("ACGTN"[(int)query[qbeg + sc]], stderr);
193 sc = ka_global_score((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
194 (uint8_t*)query + qbeg, qend - qbeg, &ap);
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);
198 score[K*n_types + t] = -sc;
202 free(ref2); free(query);
203 { // choose the top 4 indel types; reference must be included
207 sc = alloca(n_types * sizeof(int));
208 sumq = alloca(n_types * sizeof(int));
209 memset(sumq, 0, sizeof(int) * n_types);
210 for (s = K = 0; s < n; ++s) {
211 for (i = 0; i < n_plp[s]; ++i, ++K) {
212 bam_pileup1_t *p = plp[s] + i;
213 int *sct = &score[K*n_types], indelQ;
214 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
215 for (t = 1; t < n_types; ++t) // insertion sort
216 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
217 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
218 /* errmod_cal() assumes that if the call is wrong, the
219 * likelihoods of other events are equal. This is about
220 * right for substitutions, but is not desired for
221 * indels. To reuse errmod_cal(), I have to make
222 * compromise for multi-allelic indels.
224 if ((sc[0]&0x3f) == ref_type) {
225 indelQ = (sc[1]>>6) - (sc[0]>>6);
226 tmp = est_seqQ(bca, types[sc[1]&0x3f], l_run);
228 for (t = 0; t < n_types; ++t) // look for the reference type
229 if ((sc[t]&0x3f) == ref_type) break;
230 indelQ = (sc[t]>>6) - (sc[0]>>6);
231 tmp = est_seqQ(bca, types[sc[0]&0x3f], l_run);
233 if (indelQ > tmp) indelQ = tmp;
234 if (indelQ > p->b->core.qual) indelQ = p->b->core.qual;
235 if (indelQ > bca->capQ) indelQ = bca->capQ;
236 p->aux = (sc[0]&0x3f)<<8 | indelQ;
237 sumq[sc[0]&0x3f] += indelQ;
240 // determine bca->indel_types[]
241 for (t = 0; t < n_types; ++t)
242 sumq[t] = sumq[t]<<6 | t;
243 for (t = 1; t < n_types; ++t) // insertion sort
244 for (j = t; j > 0 && sumq[j] < sumq[j-1]; --j)
245 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
246 for (t = 0; t < n_types; ++t) // look for the reference type
247 if ((sumq[t]&0x3f) == ref_type) break;
248 if (t) { // then move the reference type to the first
250 for (; t > 0; --t) sumq[t] = sumq[t-1];
253 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
254 for (t = 0; t < 4 && t < n_types; ++t)
255 bca->indel_types[t] = types[sumq[t]&0x3f];
257 // FIXME: to set the inserted sequence
260 free(types); free(inscns);