comparison pyPRADA_1.2/tools/bwa-0.5.7-mh/bwtgap.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:acc2ca1a3ba4
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "bwtgap.h"
5 #include "bwtaln.h"
6
7 #define STATE_M 0
8 #define STATE_I 1
9 #define STATE_D 2
10
11 #define aln_score(m,o,e,p) ((m)*(p)->s_mm + (o)*(p)->s_gapo + (e)*(p)->s_gape)
12
13 gap_stack_t *gap_init_stack(int max_mm, int max_gapo, int max_gape, const gap_opt_t *opt)
14 {
15 int i;
16 gap_stack_t *stack;
17 stack = (gap_stack_t*)calloc(1, sizeof(gap_stack_t));
18 stack->n_stacks = aln_score(max_mm+1, max_gapo+1, max_gape+1, opt);
19 stack->stacks = (gap_stack1_t*)calloc(stack->n_stacks, sizeof(gap_stack1_t));
20 for (i = 0; i != stack->n_stacks; ++i) {
21 gap_stack1_t *p = stack->stacks + i;
22 p->m_entries = 4;
23 p->stack = (gap_entry_t*)calloc(p->m_entries, sizeof(gap_entry_t));
24 }
25 return stack;
26 }
27
28 void gap_destroy_stack(gap_stack_t *stack)
29 {
30 int i;
31 for (i = 0; i != stack->n_stacks; ++i) free(stack->stacks[i].stack);
32 free(stack->stacks);
33 free(stack);
34 }
35
36 static void gap_reset_stack(gap_stack_t *stack)
37 {
38 int i;
39 for (i = 0; i != stack->n_stacks; ++i)
40 stack->stacks[i].n_entries = 0;
41 stack->best = stack->n_stacks;
42 stack->n_entries = 0;
43 }
44
45 static inline void gap_push(gap_stack_t *stack, int a, int i, bwtint_t k, bwtint_t l, int n_mm, int n_gapo, int n_gape,
46 int state, int is_diff, const gap_opt_t *opt)
47 {
48 int score;
49 gap_entry_t *p;
50 gap_stack1_t *q;
51 score = aln_score(n_mm, n_gapo, n_gape, opt);
52 q = stack->stacks + score;
53 if (q->n_entries == q->m_entries) {
54 q->m_entries <<= 1;
55 q->stack = (gap_entry_t*)realloc(q->stack, sizeof(gap_entry_t) * q->m_entries);
56 }
57 p = q->stack + q->n_entries;
58 p->info = (u_int32_t)score<<21 | a<<20 | i; p->k = k; p->l = l;
59 p->n_mm = n_mm; p->n_gapo = n_gapo; p->n_gape = n_gape; p->state = state;
60 if (is_diff) p->last_diff_pos = i;
61 ++(q->n_entries);
62 ++(stack->n_entries);
63 if (stack->best > score) stack->best = score;
64 }
65
66 static inline void gap_pop(gap_stack_t *stack, gap_entry_t *e)
67 {
68 gap_stack1_t *q;
69 q = stack->stacks + stack->best;
70 *e = q->stack[q->n_entries - 1];
71 --(q->n_entries);
72 --(stack->n_entries);
73 if (q->n_entries == 0 && stack->n_entries) { // reset best
74 int i;
75 for (i = stack->best + 1; i < stack->n_stacks; ++i)
76 if (stack->stacks[i].n_entries != 0) break;
77 stack->best = i;
78 } else if (stack->n_entries == 0) stack->best = stack->n_stacks;
79 }
80
81 static inline void gap_shadow(int x, int len, bwtint_t max, int last_diff_pos, bwt_width_t *w)
82 {
83 int i, j;
84 for (i = j = 0; i < last_diff_pos; ++i) {
85 if (w[i].w > x) w[i].w -= x;
86 else if (w[i].w == x) {
87 w[i].bid = 1;
88 w[i].w = max - (++j);
89 } // else should not happen
90 }
91 }
92
93 static inline int int_log2(uint32_t v)
94 {
95 int c = 0;
96 if (v & 0xffff0000u) { v >>= 16; c |= 16; }
97 if (v & 0xff00) { v >>= 8; c |= 8; }
98 if (v & 0xf0) { v >>= 4; c |= 4; }
99 if (v & 0xc) { v >>= 2; c |= 2; }
100 if (v & 0x2) c |= 1;
101 return c;
102 }
103
104 bwt_aln1_t *bwt_match_gap(bwt_t *const bwts[2], int len, const ubyte_t *seq[2], bwt_width_t *w[2],
105 bwt_width_t *seed_w[2], const gap_opt_t *opt, int *_n_aln, gap_stack_t *stack)
106 {
107 int best_score = aln_score(opt->max_diff+1, opt->max_gapo+1, opt->max_gape+1, opt);
108 int best_diff = opt->max_diff + 1, max_diff = opt->max_diff;
109 int best_cnt = 0;
110 int max_entries = 0, j, _j, n_aln, m_aln;
111 bwt_aln1_t *aln;
112
113 m_aln = 4; n_aln = 0;
114 aln = (bwt_aln1_t*)calloc(m_aln, sizeof(bwt_aln1_t));
115
116 // check whether there are too many N
117 for (j = _j = 0; j < len; ++j)
118 if (seq[0][j] > 3) ++_j;
119 if (_j > max_diff) {
120 *_n_aln = n_aln;
121 return aln;
122 }
123
124 //for (j = 0; j != len; ++j) printf("#0 %d: [%d,%u]\t[%d,%u]\n", j, w[0][j].bid, w[0][j].w, w[1][j].bid, w[1][j].w);
125 gap_reset_stack(stack); // reset stack
126 gap_push(stack, 0, len, 0, bwts[0]->seq_len, 0, 0, 0, 0, 0, opt);
127 gap_push(stack, 1, len, 0, bwts[0]->seq_len, 0, 0, 0, 0, 0, opt);
128
129 while (stack->n_entries) {
130 gap_entry_t e;
131 int a, i, m, m_seed = 0, hit_found, allow_diff, allow_M, tmp;
132 bwtint_t k, l, cnt_k[4], cnt_l[4], occ;
133 const bwt_t *bwt;
134 const ubyte_t *str;
135 const bwt_width_t *seed_width = 0;
136 bwt_width_t *width;
137
138 if (max_entries < stack->n_entries) max_entries = stack->n_entries;
139 if (stack->n_entries > opt->max_entries) break;
140 gap_pop(stack, &e); // get the best entry
141 k = e.k; l = e.l; // SA interval
142 a = e.info>>20&1; i = e.info&0xffff; // strand, length
143 if (!(opt->mode & BWA_MODE_NONSTOP) && e.info>>21 > best_score + opt->s_mm) break; // no need to proceed
144
145 m = max_diff - (e.n_mm + e.n_gapo);
146 if (opt->mode & BWA_MODE_GAPE) m -= e.n_gape;
147 if (m < 0) continue;
148 bwt = bwts[1-a]; str = seq[a]; width = w[a];
149 if (seed_w) { // apply seeding
150 seed_width = seed_w[a];
151 m_seed = opt->max_seed_diff - (e.n_mm + e.n_gapo);
152 if (opt->mode & BWA_MODE_GAPE) m_seed -= e.n_gape;
153 }
154 //printf("#1\t[%d,%d,%d,%c]\t[%d,%d,%d]\t[%u,%u]\t[%u,%u]\t%d\n", stack->n_entries, a, i, "MID"[e.state], e.n_mm, e.n_gapo, e.n_gape, width[i-1].bid, width[i-1].w, k, l, e.last_diff_pos);
155 if (i > 0 && m < width[i-1].bid) continue;
156
157 // check whether a hit is found
158 hit_found = 0;
159 if (i == 0) hit_found = 1;
160 else if (m == 0 && (e.state == STATE_M || (opt->mode&BWA_MODE_GAPE) || e.n_gape == opt->max_gape)) { // no diff allowed
161 if (bwt_match_exact_alt(bwt, i, str, &k, &l)) hit_found = 1;
162 else continue; // no hit, skip
163 }
164
165 if (hit_found) { // action for found hits
166 int score = aln_score(e.n_mm, e.n_gapo, e.n_gape, opt);
167 int do_add = 1;
168 //printf("#2 hits found: %d:(%u,%u)\n", e.n_mm+e.n_gapo, k, l);
169 if (n_aln == 0) {
170 best_score = score;
171 best_diff = e.n_mm + e.n_gapo;
172 if (opt->mode & BWA_MODE_GAPE) best_diff += e.n_gape;
173 if (!(opt->mode & BWA_MODE_NONSTOP))
174 max_diff = (best_diff + 1 > opt->max_diff)? opt->max_diff : best_diff + 1; // top2 behaviour
175 }
176 if (score == best_score) best_cnt += l - k + 1;
177 else if (best_cnt > opt->max_top2) break; // top2b behaviour
178 if (e.n_gapo) { // check whether the hit has been found. this may happen when a gap occurs in a tandem repeat
179 for (j = 0; j != n_aln; ++j)
180 if (aln[j].k == k && aln[j].l == l) break;
181 if (j < n_aln) do_add = 0;
182 }
183 if (do_add) { // append
184 bwt_aln1_t *p;
185 gap_shadow(l - k + 1, len, bwt->seq_len, e.last_diff_pos, width);
186 if (n_aln == m_aln) {
187 m_aln <<= 1;
188 aln = (bwt_aln1_t*)realloc(aln, m_aln * sizeof(bwt_aln1_t));
189 memset(aln + m_aln/2, 0, m_aln/2*sizeof(bwt_aln1_t));
190 }
191 p = aln + n_aln;
192 p->n_mm = e.n_mm; p->n_gapo = e.n_gapo; p->n_gape = e.n_gape; p->a = a;
193 p->k = k; p->l = l;
194 p->score = score;
195 ++n_aln;
196 }
197 continue;
198 }
199
200 --i;
201 bwt_2occ4(bwt, k - 1, l, cnt_k, cnt_l); // retrieve Occ values
202 occ = l - k + 1;
203 // test whether diff is allowed
204 allow_diff = allow_M = 1;
205 if (i > 0) {
206 int ii = i - (len - opt->seed_len);
207 if (width[i-1].bid > m-1) allow_diff = 0;
208 else if (width[i-1].bid == m-1 && width[i].bid == m-1 && width[i-1].w == width[i].w) allow_M = 0;
209 if (seed_w && ii > 0) {
210 if (seed_width[ii-1].bid > m_seed-1) allow_diff = 0;
211 else if (seed_width[ii-1].bid == m_seed-1 && seed_width[ii].bid == m_seed-1
212 && seed_width[ii-1].w == seed_width[ii].w) allow_M = 0;
213 }
214 }
215 // indels
216 tmp = (opt->mode & BWA_MODE_LOGGAP)? int_log2(e.n_gape + e.n_gapo)/2+1 : e.n_gapo + e.n_gape;
217 if (allow_diff && i >= opt->indel_end_skip + tmp && len - i >= opt->indel_end_skip + tmp) {
218 if (e.state == STATE_M) { // gap open
219 if (e.n_gapo < opt->max_gapo) { // gap open is allowed
220 // insertion
221 gap_push(stack, a, i, k, l, e.n_mm, e.n_gapo + 1, e.n_gape, STATE_I, 1, opt);
222 // deletion
223 for (j = 0; j != 4; ++j) {
224 k = bwt->L2[j] + cnt_k[j] + 1;
225 l = bwt->L2[j] + cnt_l[j];
226 if (k <= l) gap_push(stack, a, i + 1, k, l, e.n_mm, e.n_gapo + 1, e.n_gape, STATE_D, 1, opt);
227 }
228 }
229 } else if (e.state == STATE_I) { // extention of an insertion
230 if (e.n_gape < opt->max_gape) // gap extention is allowed
231 gap_push(stack, a, i, k, l, e.n_mm, e.n_gapo, e.n_gape + 1, STATE_I, 1, opt);
232 } else if (e.state == STATE_D) { // extention of a deletion
233 if (e.n_gape < opt->max_gape) { // gap extention is allowed
234 if (e.n_gape + e.n_gapo < max_diff || occ < opt->max_del_occ) {
235 for (j = 0; j != 4; ++j) {
236 k = bwt->L2[j] + cnt_k[j] + 1;
237 l = bwt->L2[j] + cnt_l[j];
238 if (k <= l) gap_push(stack, a, i + 1, k, l, e.n_mm, e.n_gapo, e.n_gape + 1, STATE_D, 1, opt);
239 }
240 }
241 }
242 }
243 }
244 // mismatches
245 if (allow_diff && allow_M) { // mismatch is allowed
246 for (j = 1; j <= 4; ++j) {
247 int c = (str[i] + j) & 3;
248 int is_mm = (j != 4 || str[i] > 3);
249 k = bwt->L2[c] + cnt_k[c] + 1;
250 l = bwt->L2[c] + cnt_l[c];
251 if (k <= l) gap_push(stack, a, i, k, l, e.n_mm + is_mm, e.n_gapo, e.n_gape, STATE_M, is_mm, opt);
252 }
253 } else if (str[i] < 4) { // try exact match only
254 int c = str[i] & 3;
255 k = bwt->L2[c] + cnt_k[c] + 1;
256 l = bwt->L2[c] + cnt_l[c];
257 if (k <= l) gap_push(stack, a, i, k, l, e.n_mm, e.n_gapo, e.n_gape, STATE_M, 0, opt);
258 }
259 }
260
261 *_n_aln = n_aln;
262 //fprintf(stderr, "max_entries = %d\n", max_entries);
263 return aln;
264 }