annotate pyPRADA_1.2/tools/bwa-0.5.7-mh/bwtaln.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 <unistd.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include <math.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #include <time.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 #include <stdint.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 #ifdef HAVE_CONFIG_H
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 #include "config.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 #include "bwtaln.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 #include "bwtgap.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 #include "utils.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 #ifdef HAVE_PTHREAD
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 #define THREAD_BLOCK_SIZE 1024
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 #include <pthread.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 static pthread_mutex_t g_seq_lock = PTHREAD_MUTEX_INITIALIZER;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 gap_opt_t *gap_init_opt()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 gap_opt_t *o;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 o = (gap_opt_t*)calloc(1, sizeof(gap_opt_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 /* IMPORTANT: s_mm*10 should be about the average base error
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 rate. Voilating this requirement will break pairing! */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 o->s_mm = 3; o->s_gapo = 11; o->s_gape = 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 o->max_diff = -1; o->max_gapo = 1; o->max_gape = 6;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 o->indel_end_skip = 5; o->max_del_occ = 10; o->max_entries = 2000000;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 o->mode = BWA_MODE_GAPE | BWA_MODE_COMPREAD;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 o->seed_len = 32; o->max_seed_diff = 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 o->fnr = 0.04;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 o->n_threads = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 o->max_top2 = 30;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 o->trim_qual = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 return o;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 int bwa_cal_maxdiff(int l, double err, double thres)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 double elambda = exp(-l * err);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 double sum, y = 1.0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 int k, x = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 for (k = 1, sum = elambda; k < 1000; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 y *= l * err;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 x *= k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 sum += elambda * y / x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 if (1.0 - sum < thres) return k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 return 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 // width must be filled as zero
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 static int bwt_cal_width(const bwt_t *rbwt, int len, const ubyte_t *str, bwt_width_t *width)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 bwtint_t k, l, ok, ol;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 int i, bid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 bid = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 k = 0; l = rbwt->seq_len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 for (i = 0; i < len; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 ubyte_t c = str[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 if (c < 4) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 bwt_2occ(rbwt, k - 1, l, c, &ok, &ol);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 k = rbwt->L2[c] + ok + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 l = rbwt->L2[c] + ol;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 if (k > l || c > 3) { // then restart
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 k = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 l = rbwt->seq_len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 ++bid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 width[i].w = l - k + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 width[i].bid = bid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 width[len].w = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 width[len].bid = ++bid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 return bid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 void bwa_cal_sa_reg_gap(int tid, bwt_t *const bwt[2], int n_seqs, bwa_seq_t *seqs, const gap_opt_t *opt)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 int i, max_l = 0, max_len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 gap_stack_t *stack;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 bwt_width_t *w[2], *seed_w[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 const ubyte_t *seq[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 gap_opt_t local_opt = *opt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 // initiate priority stack
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 for (i = max_len = 0; i != n_seqs; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 if (seqs[i].len > max_len) max_len = seqs[i].len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 if (opt->fnr > 0.0) local_opt.max_diff = bwa_cal_maxdiff(max_len, BWA_AVG_ERR, opt->fnr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 if (local_opt.max_diff < local_opt.max_gapo) local_opt.max_gapo = local_opt.max_diff;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 stack = gap_init_stack(local_opt.max_diff, local_opt.max_gapo, local_opt.max_gape, &local_opt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 seed_w[0] = (bwt_width_t*)calloc(opt->seed_len+1, sizeof(bwt_width_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 seed_w[1] = (bwt_width_t*)calloc(opt->seed_len+1, sizeof(bwt_width_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 w[0] = w[1] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 for (i = 0; i != n_seqs; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 bwa_seq_t *p = seqs + i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 #ifdef HAVE_PTHREAD
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 if (opt->n_threads > 1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 pthread_mutex_lock(&g_seq_lock);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 if (p->tid < 0) { // unassigned
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 int j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 for (j = i; j < n_seqs && j < i + THREAD_BLOCK_SIZE; ++j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 seqs[j].tid = tid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 } else if (p->tid != tid) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 pthread_mutex_unlock(&g_seq_lock);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 pthread_mutex_unlock(&g_seq_lock);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 p->sa = 0; p->type = BWA_TYPE_NO_MATCH; p->c1 = p->c2 = 0; p->n_aln = 0; p->aln = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 seq[0] = p->seq; seq[1] = p->rseq;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 if (max_l < p->len) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 max_l = p->len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 w[0] = (bwt_width_t*)calloc(max_l + 1, sizeof(bwt_width_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 w[1] = (bwt_width_t*)calloc(max_l + 1, sizeof(bwt_width_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 bwt_cal_width(bwt[0], p->len, seq[0], w[0]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 bwt_cal_width(bwt[1], p->len, seq[1], w[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 if (opt->fnr > 0.0) local_opt.max_diff = bwa_cal_maxdiff(p->len, BWA_AVG_ERR, opt->fnr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 local_opt.seed_len = opt->seed_len < p->len? opt->seed_len : 0x7fffffff;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 if (p->len > opt->seed_len) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 bwt_cal_width(bwt[0], opt->seed_len, seq[0] + (p->len - opt->seed_len), seed_w[0]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 bwt_cal_width(bwt[1], opt->seed_len, seq[1] + (p->len - opt->seed_len), seed_w[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 // core function
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 p->aln = bwt_match_gap(bwt, p->len, seq, w, p->len <= opt->seed_len? 0 : seed_w, &local_opt, &p->n_aln, stack);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 // store the alignment
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 free(p->name); free(p->seq); free(p->rseq); free(p->qual);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 p->name = 0; p->seq = p->rseq = p->qual = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 free(seed_w[0]); free(seed_w[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 free(w[0]); free(w[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 gap_destroy_stack(stack);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 #ifdef HAVE_PTHREAD
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 int tid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 bwt_t *bwt[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 int n_seqs;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 bwa_seq_t *seqs;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 const gap_opt_t *opt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 } thread_aux_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 static void *worker(void *data)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 thread_aux_t *d = (thread_aux_t*)data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 bwa_cal_sa_reg_gap(d->tid, d->bwt, d->n_seqs, d->seqs, d->opt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 void bwa_aln_core(const char *prefix, const char *fn_fa, const gap_opt_t *opt)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 int i, n_seqs, tot_seqs = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 bwa_seq_t *seqs;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 bwa_seqio_t *ks;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 clock_t t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 bwt_t *bwt[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 // initialization
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 ks = bwa_seq_open(fn_fa);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 { // load BWT
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 char *str = (char*)calloc(strlen(prefix) + 10, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 strcpy(str, prefix); strcat(str, ".bwt"); bwt[0] = bwt_restore_bwt(str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 strcpy(str, prefix); strcat(str, ".rbwt"); bwt[1] = bwt_restore_bwt(str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 free(str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 // core loop
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 fwrite(opt, sizeof(gap_opt_t), 1, stdout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 while ((seqs = bwa_read_seq(ks, 0x40000, &n_seqs, opt->mode & BWA_MODE_COMPREAD, opt->trim_qual)) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 tot_seqs += n_seqs;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 t = clock();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 fprintf(stderr, "[bwa_aln_core] calculate SA coordinate... ");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 #ifdef HAVE_PTHREAD
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 if (opt->n_threads <= 1) { // no multi-threading at all
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 bwa_cal_sa_reg_gap(0, bwt, n_seqs, seqs, opt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 pthread_t *tid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 pthread_attr_t attr;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 thread_aux_t *data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 int j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 pthread_attr_init(&attr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 data = (thread_aux_t*)calloc(opt->n_threads, sizeof(thread_aux_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 for (j = 0; j < opt->n_threads; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 data[j].tid = j; data[j].bwt[0] = bwt[0]; data[j].bwt[1] = bwt[1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 data[j].n_seqs = n_seqs; data[j].seqs = seqs; data[j].opt = opt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 pthread_create(&tid[j], &attr, worker, data + j);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 for (j = 0; j < opt->n_threads; ++j) pthread_join(tid[j], 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 free(data); free(tid);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 bwa_cal_sa_reg_gap(0, bwt, n_seqs, seqs, opt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); t = clock();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 t = clock();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 fprintf(stderr, "[bwa_aln_core] write to the disk... ");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211 for (i = 0; i < n_seqs; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 bwa_seq_t *p = seqs + i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 fwrite(&p->n_aln, 4, 1, stdout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 if (p->n_aln) fwrite(p->aln, sizeof(bwt_aln1_t), p->n_aln, stdout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); t = clock();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 bwa_free_read_seq(n_seqs, seqs);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 fprintf(stderr, "[bwa_aln_core] %d sequences have been processed.\n", tot_seqs);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 // destroy
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 bwt_destroy(bwt[0]); bwt_destroy(bwt[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 bwa_seq_close(ks);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227 int bwa_aln(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 int c, opte = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 gap_opt_t *opt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 opt = gap_init_opt();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 while ((c = getopt(argc, argv, "n:o:e:i:d:l:k:cLR:m:t:NM:O:E:q:f:")) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234 switch (c) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 case 'n':
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 if (strstr(optarg, ".")) opt->fnr = atof(optarg), opt->max_diff = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 else opt->max_diff = atoi(optarg), opt->fnr = -1.0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239 case 'o': opt->max_gapo = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 case 'e': opte = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241 case 'M': opt->s_mm = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 case 'O': opt->s_gapo = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 case 'E': opt->s_gape = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244 case 'd': opt->max_del_occ = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 case 'i': opt->indel_end_skip = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246 case 'l': opt->seed_len = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247 case 'k': opt->max_seed_diff = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 case 'm': opt->max_entries = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 case 't': opt->n_threads = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 case 'L': opt->mode |= BWA_MODE_LOGGAP; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251 case 'R': opt->max_top2 = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 case 'q': opt->trim_qual = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253 case 'c': opt->mode &= ~BWA_MODE_COMPREAD; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 case 'N': opt->mode |= BWA_MODE_NONSTOP; opt->max_top2 = 0x7fffffff; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 case 'f': freopen(optarg, "wb", stdout); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 default: return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 if (opte > 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 opt->max_gape = opte;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 opt->mode &= ~BWA_MODE_GAPE;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 if (optind + 2 > argc) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 fprintf(stderr, "\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 fprintf(stderr, "Usage: bwa aln [options] <prefix> <in.fq>\n\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 fprintf(stderr, "Options: -n NUM max #diff (int) or missing prob under %.2f err rate (float) [%.2f]\n",
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
268 BWA_AVG_ERR, opt->fnr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
269 fprintf(stderr, " -o INT maximum number or fraction of gap opens [%d]\n", opt->max_gapo);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
270 fprintf(stderr, " -e INT maximum number of gap extensions, -1 for disabling long gaps [-1]\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
271 fprintf(stderr, " -i INT do not put an indel within INT bp towards the ends [%d]\n", opt->indel_end_skip);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
272 fprintf(stderr, " -d INT maximum occurrences for extending a long deletion [%d]\n", opt->max_del_occ);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
273 fprintf(stderr, " -l INT seed length [%d]\n", opt->seed_len);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
274 fprintf(stderr, " -k INT maximum differences in the seed [%d]\n", opt->max_seed_diff);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
275 fprintf(stderr, " -m INT maximum entries in the queue [%d]\n", opt->max_entries);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
276 fprintf(stderr, " -t INT number of threads [%d]\n", opt->n_threads);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
277 fprintf(stderr, " -M INT mismatch penalty [%d]\n", opt->s_mm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
278 fprintf(stderr, " -O INT gap open penalty [%d]\n", opt->s_gapo);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
279 fprintf(stderr, " -E INT gap extension penalty [%d]\n", opt->s_gape);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
280 fprintf(stderr, " -R INT stop searching when there are >INT equally best hits [%d]\n", opt->max_top2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
281 fprintf(stderr, " -q INT quality threshold for read trimming down to %dbp [%d]\n", BWA_MIN_RDLEN, opt->trim_qual);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
282 fprintf(stderr, " -c input sequences are in the color space\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
283 fprintf(stderr, " -L log-scaled gap penalty for long deletions\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
284 fprintf(stderr, " -N non-iterative mode: search for all n-difference hits (slooow)\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
285 fprintf(stderr, " -f FILE file to write output to instead of stdout\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
286 fprintf(stderr, "\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
287 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
288 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
289 if (opt->fnr > 0.0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
290 int i, k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
291 for (i = 17, k = 0; i <= 250; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
292 int l = bwa_cal_maxdiff(i, BWA_AVG_ERR, opt->fnr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
293 if (l != k) fprintf(stderr, "[bwa_aln] %dbp reads: max_diff = %d\n", i, l);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
294 k = l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
295 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
296 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
297 bwa_aln_core(argv[optind], argv[optind+1], opt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
298 free(opt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
299 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
300 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
301
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
302 /* rgoya: Temporary clone of aln_path2cigar to accomodate for bwa_cigar_t,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
303 __cigar_op and __cigar_len while keeping stdaln stand alone */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
304 bwa_cigar_t *bwa_aln_path2cigar(const path_t *path, int path_len, int *n_cigar)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
305 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
306 uint32_t *cigar32;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
307 bwa_cigar_t *cigar;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
308 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
309 cigar32 = aln_path2cigar32((path_t*) path, path_len, n_cigar);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
310 cigar = (bwa_cigar_t*)cigar32;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
311 for (i = 0; i < *n_cigar; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
312 cigar[i] = __cigar_create( (cigar32[i]&0xf), (cigar32[i]>>4) );
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
313 return cigar;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
314 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
315