comparison bwa-0.6.2/bwtsw2_pair.c @ 2:a294fbfcb1db draft default tip

Uploaded BWA
author ashvark
date Fri, 18 Jul 2014 07:55:59 -0400
parents dd1186b11b3b
children
comparison
equal deleted inserted replaced
1:a9636dc1e99a 2:a294fbfcb1db
1 #include <math.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "bwt.h"
6 #include "bntseq.h"
7 #include "bwtsw2.h"
8 #include "kstring.h"
9 #ifndef _NO_SSE2
10 #include "ksw.h"
11 #else
12 #include "stdaln.h"
13 #endif
14
15 #define MIN_RATIO 0.8
16 #define OUTLIER_BOUND 2.0
17 #define MAX_STDDEV 4.0
18 #define EXT_STDDEV 4.0
19
20 typedef struct {
21 int low, high, failed;
22 double avg, std;
23 } bsw2pestat_t;
24
25 bsw2pestat_t bsw2_stat(int n, bwtsw2_t **buf, kstring_t *msg, int max_ins)
26 {
27 extern void ks_introsort_uint64_t(size_t n, uint64_t *a);
28 int i, k, x, p25, p50, p75, tmp, max_len = 0;
29 uint64_t *isize;
30 bsw2pestat_t r;
31
32 memset(&r, 0, sizeof(bsw2pestat_t));
33 isize = calloc(n, 8);
34 for (i = k = 0; i < n; i += 2) {
35 bsw2hit_t *t[2];
36 int l;
37 if (buf[i] == 0 || buf[i]->n != 1 || buf[i+1]->n != 1) continue; // more than 1 hits
38 t[0] = &buf[i]->hits[0]; t[1] = &buf[i+1]->hits[0];
39 if (t[0]->G2 > 0.8 * t[0]->G) continue; // the best hit is not good enough
40 if (t[1]->G2 > 0.8 * t[1]->G) continue; // the best hit is not good enough
41 l = t[0]->k > t[1]->k? t[0]->k - t[1]->k + t[1]->len : t[1]->k - t[0]->k + t[0]->len;
42 if (l >= max_ins) continue; // skip pairs with excessively large insert
43 max_len = max_len > t[0]->end - t[0]->beg? max_len : t[0]->end - t[0]->beg;
44 max_len = max_len > t[1]->end - t[1]->beg? max_len : t[1]->end - t[1]->beg;
45 isize[k++] = l;
46 }
47 ks_introsort_uint64_t(k, isize);
48 p25 = isize[(int)(.25 * k + .499)];
49 p50 = isize[(int)(.50 * k + .499)];
50 p75 = isize[(int)(.75 * k + .499)];
51 ksprintf(msg, "[%s] infer the insert size distribution from %d high-quality pairs.\n", __func__, k);
52 if (k < 8) {
53 ksprintf(msg, "[%s] fail to infer the insert size distribution.\n", __func__);
54 free(isize);
55 r.failed = 1;
56 return r;
57 }
58 tmp = (int)(p25 - OUTLIER_BOUND * (p75 - p25) + .499);
59 r.low = tmp > max_len? tmp : max_len;
60 if (r.low < 1) r.low = 1;
61 r.high = (int)(p75 + OUTLIER_BOUND * (p75 - p25) + .499);
62 ksprintf(msg, "[%s] (25, 50, 75) percentile: (%d, %d, %d)\n", __func__, p25, p50, p75);
63 ksprintf(msg, "[%s] low and high boundaries for computing mean and std.dev: (%d, %d)\n", __func__, r.low, r.high);
64 for (i = x = 0, r.avg = 0; i < k; ++i)
65 if (isize[i] >= r.low && isize[i] <= r.high)
66 r.avg += isize[i], ++x;
67 r.avg /= x;
68 for (i = 0, r.std = 0; i < k; ++i)
69 if (isize[i] >= r.low && isize[i] <= r.high)
70 r.std += (isize[i] - r.avg) * (isize[i] - r.avg);
71 r.std = sqrt(r.std / x);
72 ksprintf(msg, "[%s] mean and std.dev: (%.2f, %.2f)\n", __func__, r.avg, r.std);
73 tmp = (int)(p25 - 3. * (p75 - p25) + .499);
74 r.low = tmp > max_len? tmp : max_len;
75 if (r.low < 1) r.low = 1;
76 r.high = (int)(p75 + 3. * (p75 - p25) + .499);
77 if (r.low > r.avg - MAX_STDDEV * 4.) r.low = (int)(r.avg - MAX_STDDEV * 4. + .499);
78 r.low = tmp > max_len? tmp : max_len;
79 if (r.high < r.avg - MAX_STDDEV * 4.) r.high = (int)(r.avg + MAX_STDDEV * 4. + .499);
80 ksprintf(msg, "[%s] low and high boundaries for proper pairs: (%d, %d)\n", __func__, r.low, r.high);
81 free(isize);
82 return r;
83 }
84
85 typedef struct {
86 int n_cigar, beg, end, len;
87 int64_t pos;
88 uint32_t *cigar;
89 } pairaux_t;
90
91 extern unsigned char nst_nt4_table[256];
92
93 void bsw2_pair1(const bsw2opt_t *opt, int64_t l_pac, const uint8_t *pac, const bsw2pestat_t *st, const bsw2hit_t *h, int l_mseq, const char *mseq, bsw2hit_t *a, int8_t g_mat[25])
94 {
95 extern void seq_reverse(int len, ubyte_t *seq, int is_comp);
96 int64_t k, beg, end;
97 uint8_t *seq, *ref;
98 int i;
99 // compute the region start and end
100 a->n_seeds = 1; a->flag |= BSW2_FLAG_MATESW; // before calling this routine, *a has been cleared with memset(0); the flag is set with 1<<6/7
101 if (h->is_rev == 0) {
102 beg = (int64_t)(h->k + st->avg - EXT_STDDEV * st->std - l_mseq + .499);
103 if (beg < h->k) beg = h->k;
104 end = (int64_t)(h->k + st->avg + EXT_STDDEV * st->std + .499);
105 a->is_rev = 1; a->flag |= 16;
106 } else {
107 beg = (int64_t)(h->k + h->end - h->beg - st->avg - EXT_STDDEV * st->std + .499);
108 end = (int64_t)(h->k + h->end - h->beg - st->avg + EXT_STDDEV * st->std + l_mseq + .499);
109 if (end > h->k + (h->end - h->beg)) end = h->k + (h->end - h->beg);
110 a->is_rev = 0;
111 }
112 if (beg < 1) beg = 1;
113 if (end > l_pac) end = l_pac;
114 if (end - beg < l_mseq) return;
115 // generate the sequence
116 seq = malloc(l_mseq + (end - beg));
117 ref = seq + l_mseq;
118 for (k = beg; k < end; ++k)
119 ref[k - beg] = pac[k>>2] >> ((~k&3)<<1) & 0x3;
120 if (h->is_rev == 0) {
121 for (i = 0; i < l_mseq; ++i) { // on the reverse strand
122 int c = nst_nt4_table[(int)mseq[i]];
123 seq[l_mseq - 1 - i] = c > 3? 4 : 3 - c;
124 }
125 } else {
126 for (i = 0; i < l_mseq; ++i) // on the forward strand
127 seq[i] = nst_nt4_table[(int)mseq[i]];
128 }
129 #ifndef _NO_SSE2
130 {
131 ksw_query_t *q;
132 ksw_aux_t aux[2];
133 // forward Smith-Waterman
134 aux[0].T = opt->t; aux[0].gapo = opt->q; aux[0].gape = opt->r; aux[1] = aux[0];
135 q = ksw_qinit(l_mseq * g_mat[0] < 250? 1 : 2, l_mseq, seq, 5, g_mat);
136 ksw_sse2(q, end - beg, ref, &aux[0]);
137 free(q);
138 if (aux[0].score < opt->t) {
139 free(seq);
140 return;
141 }
142 ++aux[0].qe; ++aux[0].te;
143 // reverse Smith-Waterman
144 seq_reverse(aux[0].qe, seq, 0);
145 seq_reverse(aux[0].te, ref, 0);
146 q = ksw_qinit(aux[0].qe * g_mat[0] < 250? 1 : 2, aux[0].qe, seq, 5, g_mat);
147 ksw_sse2(q, aux[0].te, ref, &aux[1]);
148 free(q);
149 ++aux[1].qe; ++aux[1].te;
150 // write output
151 a->G = aux[0].score;
152 a->G2 = aux[0].score2 > aux[1].score2? aux[0].score2 : aux[1].score2;
153 if (a->G2 < opt->t) a->G2 = 0;
154 if (a->G2) a->flag |= BSW2_FLAG_TANDEM;
155 a->k = beg + (aux[0].te - aux[1].te);
156 a->len = aux[1].te;
157 a->beg = aux[0].qe - aux[1].qe;
158 a->end = aux[0].qe;
159 }
160 #else
161 {
162 AlnParam ap;
163 path_t path[2];
164 int matrix[25];
165 for (i = 0; i < 25; ++i) matrix[i] = g_mat[i];
166 ap.gap_open = opt->q; ap.gap_ext = opt->r; ap.gap_end = opt->r;
167 ap.matrix = matrix; ap.row = 5; ap.band_width = 50;
168 a->G = aln_local_core(ref, end - beg, seq, l_mseq, &ap, path, 0, opt->t, &a->G2);
169 if (a->G < opt->t) a->G = 0;
170 if (a->G2 < opt->t) a->G2 = 0;
171 a->k = beg + path[0].i - 1;
172 a->len = path[1].i - path[0].i + 1;
173 a->beg = path[0].j - 1;
174 a->end = path[1].j;
175 }
176 #endif
177 if (a->is_rev) i = a->beg, a->beg = l_mseq - a->end, a->end = l_mseq - i;
178 free(seq);
179 }
180
181 void bsw2_pair(const bsw2opt_t *opt, int64_t l_pac, const uint8_t *pac, int n, bsw2seq1_t *seq, bwtsw2_t **hits)
182 {
183 extern int bsw2_resolve_duphits(const bntseq_t *bns, const bwt_t *bwt, bwtsw2_t *b, int IS);
184 bsw2pestat_t pes;
185 int i, j, k, n_rescued = 0, n_moved = 0, n_fixed = 0;
186 int8_t g_mat[25];
187 kstring_t msg;
188 memset(&msg, 0, sizeof(kstring_t));
189 pes = bsw2_stat(n, hits, &msg, opt->max_ins);
190 for (i = k = 0; i < 5; ++i) {
191 for (j = 0; j < 4; ++j)
192 g_mat[k++] = i == j? opt->a : -opt->b;
193 g_mat[k++] = 0;
194 }
195 for (i = 0; i < n; i += 2) {
196 bsw2hit_t a[2];
197 memset(&a, 0, sizeof(bsw2hit_t) * 2);
198 a[0].flag = 1<<6; a[1].flag = 1<<7;
199 for (j = 0; j < 2; ++j) { // set the read1/2 flag
200 if (hits[i+j] == 0) continue;
201 for (k = 0; k < hits[i+j]->n; ++k) {
202 bsw2hit_t *p = &hits[i+j]->hits[k];
203 p->flag |= 1<<(6+j);
204 }
205 }
206 if (pes.failed) continue;
207 if (hits[i] == 0 || hits[i+1] == 0) continue; // one end has excessive N
208 if (hits[i]->n != 1 && hits[i+1]->n != 1) continue; // no end has exactly one hit
209 if (hits[i]->n > 1 || hits[i+1]->n > 1) continue; // one read has more than one hit
210 if (!opt->skip_sw) {
211 if (hits[i+0]->n == 1) bsw2_pair1(opt, l_pac, pac, &pes, &hits[i+0]->hits[0], seq[i+1].l, seq[i+1].seq, &a[1], g_mat);
212 if (hits[i+1]->n == 1) bsw2_pair1(opt, l_pac, pac, &pes, &hits[i+1]->hits[0], seq[i+0].l, seq[i+0].seq, &a[0], g_mat);
213 } // else a[0].G == a[1].G == a[0].G2 == a[1].G2 == 0
214 // the following enumerate all possibilities. It is tedious but necessary...
215 if (hits[i]->n + hits[i+1]->n == 1) { // one end mapped; the other not;
216 bwtsw2_t *p[2];
217 int which;
218 if (hits[i]->n == 1) p[0] = hits[i], p[1] = hits[i+1], which = 1;
219 else p[0] = hits[i+1], p[1] = hits[i], which = 0;
220 if (a[which].G == 0) continue;
221 a[which].flag |= BSW2_FLAG_RESCUED;
222 if (p[1]->max == 0) {
223 p[1]->max = 1;
224 p[1]->hits = malloc(sizeof(bsw2hit_t));
225 }
226 p[1]->hits[0] = a[which];
227 p[1]->n = 1;
228 p[0]->hits[0].flag |= 2;
229 p[1]->hits[0].flag |= 2;
230 ++n_rescued;
231 } else { // then both ends mapped
232 int is_fixed = 0;
233 //fprintf(stderr, "%d; %lld,%lld; %d,%d\n", a[0].is_rev, hits[i]->hits[0].k, a[0].k, hits[i]->hits[0].end, a[0].end);
234 for (j = 0; j < 2; ++j) { // fix wrong mappings and wrong suboptimal alignment score
235 bsw2hit_t *p = &hits[i+j]->hits[0];
236 if (p->G < a[j].G) { // the orginal mapping is suboptimal
237 a[j].G2 = a[j].G2 > p->G? a[j].G2 : p->G; // FIXME: reset BSW2_FLAG_TANDEM?
238 *p = a[j];
239 ++n_fixed;
240 is_fixed = 1;
241 } else if (p->k != a[j].k && p->G2 < a[j].G) {
242 p->G2 = a[j].G;
243 } else if (p->k == a[j].k && p->G2 < a[j].G2) {
244 p->G2 = a[j].G2;
245 }
246 }
247 if (hits[i]->hits[0].k == a[0].k && hits[i+1]->hits[0].k == a[1].k) { // properly paired and no ends need to be moved
248 for (j = 0; j < 2; ++j)
249 hits[i+j]->hits[0].flag |= 2 | (a[j].flag & BSW2_FLAG_TANDEM);
250 } else if (hits[i]->hits[0].k == a[0].k || hits[i+1]->hits[0].k == a[1].k) { // a tandem match
251 for (j = 0; j < 2; ++j) {
252 hits[i+j]->hits[0].flag |= 2;
253 if (hits[i+j]->hits[0].k != a[j].k)
254 hits[i+j]->hits[0].flag |= BSW2_FLAG_TANDEM;
255 }
256 } else if (!is_fixed && (a[0].G || a[1].G)) { // it is possible to move one end
257 if (a[0].G && a[1].G) { // now we have two "proper pairs"
258 int G[2];
259 double diff;
260 G[0] = hits[i]->hits[0].G + a[1].G;
261 G[1] = hits[i+1]->hits[0].G + a[0].G;
262 diff = fabs(G[0] - G[1]) / (opt->a + opt->b) / ((hits[i]->hits[0].len + a[1].len + hits[i+1]->hits[0].len + a[0].len) / 2.);
263 if (diff > 0.05) a[G[0] > G[1]? 0 : 1].G = 0;
264 }
265 if (a[0].G == 0 || a[1].G == 0) { // one proper pair only
266 bsw2hit_t *p[2]; // p[0] points the unchanged hit; p[1] to the hit to be moved
267 int which, isize;
268 double dev, diff;
269 if (a[0].G) p[0] = &hits[i+1]->hits[0], p[1] = &hits[i]->hits[0], which = 0;
270 else p[0] = &hits[i]->hits[0], p[1] = &hits[i+1]->hits[0], which = 1;
271 isize = p[0]->is_rev? p[0]->k + p[0]->len - a[which].k : a[which].k + a[which].len - p[0]->k;
272 dev = fabs(isize - pes.avg) / pes.std;
273 diff = (double)(p[1]->G - a[which].G) / (opt->a + opt->b) / (p[1]->end - p[1]->beg) * 100.0;
274 if (diff < dev * 2.) { // then move (heuristic)
275 a[which].G2 = a[which].G;
276 p[1][0] = a[which];
277 p[1]->flag |= BSW2_FLAG_MOVED | 2;
278 p[0]->flag |= 2;
279 ++n_moved;
280 }
281 }
282 } else if (is_fixed) {
283 hits[i+0]->hits[0].flag |= 2;
284 hits[i+1]->hits[0].flag |= 2;
285 }
286 }
287 }
288 ksprintf(&msg, "[%s] #fixed=%d, #rescued=%d, #moved=%d\n", __func__, n_fixed, n_rescued, n_moved);
289 fputs(msg.s, stderr);
290 free(msg.s);
291 }