annotate pyPRADA_1.2/tools/bwa-0.5.7-mh/bwtsw2_chain.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include "bwtsw2.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 uint32_t tbeg, tend;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 int qbeg, qend;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 uint32_t flag:1, idx:31;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 int chain; // also reuse as a counter
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 } hsaip_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 #define _hsaip_lt(a, b) ((a).qbeg < (b).qbeg)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 #include "ksort.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 KSORT_INIT(hsaip, hsaip_t, _hsaip_lt)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 static int chaining(const bsw2opt_t *opt, int shift, int n, hsaip_t *z, hsaip_t *chain)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 int j, k, m = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 ks_introsort(hsaip, n, z);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 for (j = 0; j < n; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 hsaip_t *p = z + j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 for (k = m - 1; k >= 0; --k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 hsaip_t *q = chain + k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 int x = p->qbeg - q->qbeg; // always positive
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 int y = p->tbeg - q->tbeg;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 if (y > 0 && x - y <= opt->bw && y - x <= opt->bw) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 if (p->qend > q->qend) q->qend = p->qend;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 if (p->tend > q->tend) q->tend = p->tend;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 ++q->chain;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 p->chain = shift + k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 if (k < 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 chain[m] = *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 chain[m].chain = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 chain[m].idx = p->chain = shift + m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 ++m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 return m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 void bsw2_chain_filter(const bsw2opt_t *opt, int len, bwtsw2_t *b[2])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 hsaip_t *z[2], *chain[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 int i, j, k, n[2], m[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 char *flag;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 // initialization
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 n[0] = b[0]->n; n[1] = b[1]->n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 z[0] = calloc(n[0] + n[1], sizeof(hsaip_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 z[1] = z[0] + n[0];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 chain[0] = calloc(n[0] + n[1], sizeof(hsaip_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 for (k = j = 0; k < 2; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 for (i = 0; i < b[k]->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 bsw2hit_t *p = b[k]->hits + i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 hsaip_t *q = z[k] + i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 q->flag = k; q->idx = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 q->tbeg = p->k; q->tend = p->k + p->len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 q->chain = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 q->qbeg = p->beg; q->qend = p->end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 // chaining
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 m[0] = chaining(opt, 0, n[0], z[0], chain[0]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 chain[1] = chain[0] + m[0];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 m[1] = chaining(opt, m[0], n[1], z[1], chain[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 // change query coordinate on the reverse strand
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 for (k = 0; k < m[1]; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 hsaip_t *p = chain[1] + k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 int tmp = p->qbeg;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 p->qbeg = len - p->qend; p->qend = len - tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 // filtering
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 flag = calloc(m[0] + m[1], 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 ks_introsort(hsaip, m[0] + m[1], chain[0]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 for (k = 1; k < m[0] + m[1]; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 hsaip_t *p = chain[0] + k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 for (j = 0; j < k; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 hsaip_t *q = chain[0] + j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 if (flag[q->idx]) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 if (q->qend >= p->qend && q->chain > p->chain * opt->t_seeds * 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 flag[p->idx] = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 for (k = 0; k < n[0] + n[1]; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 hsaip_t *p = z[0] + k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 if (flag[p->chain])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 b[p->flag]->hits[p->idx].G = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 free(flag);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 // squeeze out filtered elements in b[2]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 for (k = 0; k < 2; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 for (j = i = 0; j < n[k]; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 bsw2hit_t *p = b[k]->hits + j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 if (p->G) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 if (i != j) b[k]->hits[i++] = *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 else ++i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 b[k]->n = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 // free
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 free(z[0]); free(chain[0]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 }