annotate bwa-0.6.2/bwape.c @ 2:a294fbfcb1db draft default tip

Uploaded BWA
author ashvark
date Fri, 18 Jul 2014 07:55:59 -0400
parents dd1186b11b3b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
1 #include <unistd.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
2 #include <math.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
3 #include <stdlib.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
4 #include <time.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
5 #include <stdio.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
6 #include <string.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
7 #include "bwtaln.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
8 #include "kvec.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
9 #include "bntseq.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
10 #include "utils.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
11 #include "stdaln.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
12 #include "bwase.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
13
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
14 typedef struct {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
15 int n;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
16 bwtint_t *a;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
17 } poslist_t;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
18
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
19 typedef struct {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
20 double avg, std, ap_prior;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
21 bwtint_t low, high, high_bayesian;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
22 } isize_info_t;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
23
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
24 typedef struct {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
25 uint64_t x, y;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
26 } b128_t;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
27
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
28 #define b128_lt(a, b) ((a).x < (b).x)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
29 #define b128_eq(a, b) ((a).x == (b).x && (a).y == (b).y)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
30 #define b128_hash(a) ((uint32_t)(a).x)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
31
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
32 #include "khash.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
33 KHASH_INIT(b128, b128_t, poslist_t, 1, b128_hash, b128_eq)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
34
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
35 #include "ksort.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
36 KSORT_INIT(b128, b128_t, b128_lt)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
37 KSORT_INIT_GENERIC(uint64_t)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
38
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
39 typedef struct {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
40 kvec_t(b128_t) arr;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
41 kvec_t(b128_t) pos[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
42 kvec_t(bwt_aln1_t) aln[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
43 } pe_data_t;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
44
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
45 #define MIN_HASH_WIDTH 1000
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
46
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
47 extern int g_log_n[256]; // in bwase.c
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
48 static kh_b128_t *g_hash;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
49
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
50 void bwa_aln2seq_core(int n_aln, const bwt_aln1_t *aln, bwa_seq_t *s, int set_main, int n_multi);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
51 void bwa_aln2seq(int n_aln, const bwt_aln1_t *aln, bwa_seq_t *s);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
52 int bwa_approx_mapQ(const bwa_seq_t *p, int mm);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
53 void bwa_print_sam1(const bntseq_t *bns, bwa_seq_t *p, const bwa_seq_t *mate, int mode, int max_top2);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
54 bntseq_t *bwa_open_nt(const char *prefix);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
55 void bwa_print_sam_SQ(const bntseq_t *bns);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
56 void bwa_print_sam_PG();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
57
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
58 pe_opt_t *bwa_init_pe_opt()
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
59 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
60 pe_opt_t *po;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
61 po = (pe_opt_t*)calloc(1, sizeof(pe_opt_t));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
62 po->max_isize = 500;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
63 po->force_isize = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
64 po->max_occ = 100000;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
65 po->n_multi = 3;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
66 po->N_multi = 10;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
67 po->type = BWA_PET_STD;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
68 po->is_sw = 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
69 po->ap_prior = 1e-5;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
70 return po;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
71 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
72
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
73 static inline uint64_t hash_64(uint64_t key)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
74 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
75 key += ~(key << 32);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
76 key ^= (key >> 22);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
77 key += ~(key << 13);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
78 key ^= (key >> 8);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
79 key += (key << 3);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
80 key ^= (key >> 15);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
81 key += ~(key << 27);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
82 key ^= (key >> 31);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
83 return key;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
84 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
85 /*
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
86 static double ierfc(double x) // inverse erfc(); iphi(x) = M_SQRT2 *ierfc(2 * x);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
87 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
88 const double a = 0.140012;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
89 double b, c;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
90 b = log(x * (2 - x));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
91 c = 2./M_PI/a + b / 2.;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
92 return sqrt(sqrt(c * c - b / a) - c);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
93 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
94 */
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
95
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
96 // for normal distribution, this is about 3std
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
97 #define OUTLIER_BOUND 2.0
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
98
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
99 static int infer_isize(int n_seqs, bwa_seq_t *seqs[2], isize_info_t *ii, double ap_prior, int64_t L)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
100 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
101 uint64_t x, *isizes, n_ap = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
102 int n, i, tot, p25, p75, p50, max_len = 1, tmp;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
103 double skewness = 0.0, kurtosis = 0.0, y;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
104
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
105 ii->avg = ii->std = -1.0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
106 ii->low = ii->high = ii->high_bayesian = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
107 isizes = (uint64_t*)calloc(n_seqs, 8);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
108 for (i = 0, tot = 0; i != n_seqs; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
109 bwa_seq_t *p[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
110 p[0] = seqs[0] + i; p[1] = seqs[1] + i;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
111 if (p[0]->mapQ >= 20 && p[1]->mapQ >= 20) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
112 x = (p[0]->pos < p[1]->pos)? p[1]->pos + p[1]->len - p[0]->pos : p[0]->pos + p[0]->len - p[1]->pos;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
113 if (x < 100000) isizes[tot++] = x;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
114 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
115 if (p[0]->len > max_len) max_len = p[0]->len;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
116 if (p[1]->len > max_len) max_len = p[1]->len;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
117 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
118 if (tot < 20) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
119 fprintf(stderr, "[infer_isize] fail to infer insert size: too few good pairs\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
120 free(isizes);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
121 return -1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
122 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
123 ks_introsort(uint64_t, tot, isizes);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
124 p25 = isizes[(int)(tot*0.25 + 0.5)];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
125 p50 = isizes[(int)(tot*0.50 + 0.5)];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
126 p75 = isizes[(int)(tot*0.75 + 0.5)];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
127 tmp = (int)(p25 - OUTLIER_BOUND * (p75 - p25) + .499);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
128 ii->low = tmp > max_len? tmp : max_len; // ii->low is unsigned
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
129 ii->high = (int)(p75 + OUTLIER_BOUND * (p75 - p25) + .499);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
130 for (i = 0, x = n = 0; i < tot; ++i)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
131 if (isizes[i] >= ii->low && isizes[i] <= ii->high)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
132 ++n, x += isizes[i];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
133 ii->avg = (double)x / n;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
134 for (i = 0; i < tot; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
135 if (isizes[i] >= ii->low && isizes[i] <= ii->high) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
136 double tmp = (isizes[i] - ii->avg) * (isizes[i] - ii->avg);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
137 ii->std += tmp;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
138 skewness += tmp * (isizes[i] - ii->avg);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
139 kurtosis += tmp * tmp;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
140 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
141 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
142 kurtosis = kurtosis/n / (ii->std / n * ii->std / n) - 3;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
143 ii->std = sqrt(ii->std / n); // it would be better as n-1, but n is usually very large
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
144 skewness = skewness / n / (ii->std * ii->std * ii->std);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
145 for (y = 1.0; y < 10.0; y += 0.01)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
146 if (.5 * erfc(y / M_SQRT2) < ap_prior / L * (y * ii->std + ii->avg)) break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
147 ii->high_bayesian = (bwtint_t)(y * ii->std + ii->avg + .499);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
148 for (i = 0; i < tot; ++i)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
149 if (isizes[i] > ii->high_bayesian) ++n_ap;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
150 ii->ap_prior = .01 * (n_ap + .01) / tot;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
151 if (ii->ap_prior < ap_prior) ii->ap_prior = ap_prior;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
152 free(isizes);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
153 fprintf(stderr, "[infer_isize] (25, 50, 75) percentile: (%d, %d, %d)\n", p25, p50, p75);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
154 if (isnan(ii->std) || p75 > 100000) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
155 ii->low = ii->high = ii->high_bayesian = 0; ii->avg = ii->std = -1.0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
156 fprintf(stderr, "[infer_isize] fail to infer insert size: weird pairing\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
157 return -1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
158 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
159 for (y = 1.0; y < 10.0; y += 0.01)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
160 if (.5 * erfc(y / M_SQRT2) < ap_prior / L * (y * ii->std + ii->avg)) break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
161 ii->high_bayesian = (bwtint_t)(y * ii->std + ii->avg + .499);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
162 fprintf(stderr, "[infer_isize] low and high boundaries: %ld and %ld for estimating avg and std\n", (long)ii->low, (long)ii->high);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
163 fprintf(stderr, "[infer_isize] inferred external isize from %d pairs: %.3lf +/- %.3lf\n", n, ii->avg, ii->std);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
164 fprintf(stderr, "[infer_isize] skewness: %.3lf; kurtosis: %.3lf; ap_prior: %.2e\n", skewness, kurtosis, ii->ap_prior);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
165 fprintf(stderr, "[infer_isize] inferred maximum insert size: %ld (%.2lf sigma)\n", (long)ii->high_bayesian, y);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
166 return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
167 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
168
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
169 static int pairing(bwa_seq_t *p[2], pe_data_t *d, const pe_opt_t *opt, int s_mm, const isize_info_t *ii)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
170 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
171 int i, j, o_n, subo_n, cnt_chg = 0, low_bound = ii->low, max_len;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
172 uint64_t o_score, subo_score;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
173 b128_t last_pos[2][2], o_pos[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
174 max_len = p[0]->full_len;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
175 if (max_len < p[1]->full_len) max_len = p[1]->full_len;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
176 if (low_bound < max_len) low_bound = max_len;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
177
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
178 // here v>=u. When ii is set, we check insert size with ii; otherwise with opt->max_isize
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
179 #define __pairing_aux(u,v) do { \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
180 bwtint_t l = (v).x + p[(v).y&1]->len - ((u).x); \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
181 if ((u).x != (uint64_t)-1 && (v).x > (u).x && l >= max_len \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
182 && ((ii->high && l <= ii->high_bayesian) || (ii->high == 0 && l <= opt->max_isize))) \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
183 { \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
184 uint64_t s = d->aln[(v).y&1].a[(v).y>>2].score + d->aln[(u).y&1].a[(u).y>>2].score; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
185 s *= 10; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
186 if (ii->high) s += (int)(-4.343 * log(.5 * erfc(M_SQRT1_2 * fabs(l - ii->avg) / ii->std)) + .499); \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
187 s = s<<32 | (uint32_t)hash_64((u).x<<32 | (v).x); \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
188 if (s>>32 == o_score>>32) ++o_n; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
189 else if (s>>32 < o_score>>32) { subo_n += o_n; o_n = 1; } \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
190 else ++subo_n; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
191 if (s < o_score) subo_score = o_score, o_score = s, o_pos[(u).y&1] = (u), o_pos[(v).y&1] = (v); \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
192 else if (s < subo_score) subo_score = s; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
193 } \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
194 } while (0)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
195
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
196 #define __pairing_aux2(q, w) do { \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
197 const bwt_aln1_t *r = d->aln[(w).y&1].a + ((w).y>>2); \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
198 (q)->extra_flag |= SAM_FPP; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
199 if ((q)->pos != (w).x || (q)->strand != ((w).y>>1&1)) { \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
200 (q)->n_mm = r->n_mm; (q)->n_gapo = r->n_gapo; (q)->n_gape = r->n_gape; (q)->strand = (w).y>>1&1; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
201 (q)->score = r->score; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
202 (q)->pos = (w).x; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
203 if ((q)->mapQ > 0) ++cnt_chg; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
204 } \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
205 } while (0)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
206
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
207 o_score = subo_score = (uint64_t)-1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
208 o_n = subo_n = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
209 ks_introsort(b128, d->arr.n, d->arr.a);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
210 for (j = 0; j < 2; ++j) last_pos[j][0].x = last_pos[j][0].y = last_pos[j][1].x = last_pos[j][1].y = (uint64_t)-1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
211 if (opt->type == BWA_PET_STD) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
212 for (i = 0; i < d->arr.n; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
213 b128_t x = d->arr.a[i];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
214 int strand = x.y>>1&1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
215 if (strand == 1) { // reverse strand, then check
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
216 int y = 1 - (x.y&1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
217 __pairing_aux(last_pos[y][1], x);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
218 __pairing_aux(last_pos[y][0], x);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
219 } else { // forward strand, then push
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
220 last_pos[x.y&1][0] = last_pos[x.y&1][1];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
221 last_pos[x.y&1][1] = x;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
222 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
223 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
224 } else if (opt->type == BWA_PET_SOLID) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
225 for (i = 0; i < d->arr.n; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
226 b128_t x = d->arr.a[i];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
227 int strand = x.y>>1&1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
228 if ((strand^x.y)&1) { // push
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
229 int y = 1 - (x.y&1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
230 __pairing_aux(last_pos[y][1], x);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
231 __pairing_aux(last_pos[y][0], x);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
232 } else { // check
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
233 last_pos[x.y&1][0] = last_pos[x.y&1][1];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
234 last_pos[x.y&1][1] = x;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
235 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
236 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
237 } else {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
238 fprintf(stderr, "[paring] not implemented yet!\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
239 exit(1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
240 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
241 // set pairing
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
242 //fprintf(stderr, "[%ld, %d, %d, %d]\n", d->arr.n, (int)(o_score>>32), (int)(subo_score>>32), o_n);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
243 if (o_score != (uint64_t)-1) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
244 int mapQ_p = 0; // this is the maximum mapping quality when one end is moved
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
245 //fprintf(stderr, "%d, %d\n", o_n, subo_n);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
246 if (o_n == 1) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
247 if (subo_score == (uint64_t)-1) mapQ_p = 29; // no sub-optimal pair
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
248 else if ((subo_score>>32) - (o_score>>32) > s_mm * 10) mapQ_p = 23; // poor sub-optimal pair
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
249 else {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
250 int n = subo_n > 255? 255 : subo_n;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
251 mapQ_p = ((subo_score>>32) - (o_score>>32)) / 2 - g_log_n[n];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
252 if (mapQ_p < 0) mapQ_p = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
253 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
254 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
255 if ((p[0]->pos == o_pos[0].x && p[0]->strand == (o_pos[0].y>>1&1)) && (p[1]->pos == o_pos[1].x && p[1]->strand == (o_pos[1].y>>1&1))) { // both ends not moved
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
256 if (p[0]->mapQ > 0 && p[1]->mapQ > 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
257 int mapQ = p[0]->mapQ + p[1]->mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
258 if (mapQ > 60) mapQ = 60;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
259 p[0]->mapQ = p[1]->mapQ = mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
260 } else {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
261 if (p[0]->mapQ == 0) p[0]->mapQ = (mapQ_p + 7 < p[1]->mapQ)? mapQ_p + 7 : p[1]->mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
262 if (p[1]->mapQ == 0) p[1]->mapQ = (mapQ_p + 7 < p[0]->mapQ)? mapQ_p + 7 : p[0]->mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
263 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
264 } else if (p[0]->pos == o_pos[0].x && p[0]->strand == (o_pos[0].y>>1&1)) { // [1] moved
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
265 p[1]->seQ = 0; p[1]->mapQ = p[0]->mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
266 if (p[1]->mapQ > mapQ_p) p[1]->mapQ = mapQ_p;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
267 } else if (p[1]->pos == o_pos[1].x && p[1]->strand == (o_pos[1].y>>1&1)) { // [0] moved
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
268 p[0]->seQ = 0; p[0]->mapQ = p[1]->mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
269 if (p[0]->mapQ > mapQ_p) p[0]->mapQ = mapQ_p;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
270 } else { // both ends moved
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
271 p[0]->seQ = p[1]->seQ = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
272 mapQ_p -= 20;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
273 if (mapQ_p < 0) mapQ_p = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
274 p[0]->mapQ = p[1]->mapQ = mapQ_p;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
275 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
276 __pairing_aux2(p[0], o_pos[0]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
277 __pairing_aux2(p[1], o_pos[1]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
278 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
279 return cnt_chg;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
280 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
281
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
282 typedef struct {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
283 kvec_t(bwt_aln1_t) aln;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
284 } aln_buf_t;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
285
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
286 int bwa_cal_pac_pos_pe(const bntseq_t *bns, const char *prefix, bwt_t *const _bwt, int n_seqs, bwa_seq_t *seqs[2], FILE *fp_sa[2], isize_info_t *ii,
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
287 const pe_opt_t *opt, const gap_opt_t *gopt, const isize_info_t *last_ii)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
288 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
289 int i, j, cnt_chg = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
290 char str[1024];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
291 bwt_t *bwt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
292 pe_data_t *d;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
293 aln_buf_t *buf[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
294
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
295 d = (pe_data_t*)calloc(1, sizeof(pe_data_t));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
296 buf[0] = (aln_buf_t*)calloc(n_seqs, sizeof(aln_buf_t));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
297 buf[1] = (aln_buf_t*)calloc(n_seqs, sizeof(aln_buf_t));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
298
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
299 if (_bwt == 0) { // load forward SA
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
300 strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
301 strcpy(str, prefix); strcat(str, ".sa"); bwt_restore_sa(str, bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
302 } else bwt = _bwt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
303
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
304 // SE
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
305 for (i = 0; i != n_seqs; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
306 bwa_seq_t *p[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
307 for (j = 0; j < 2; ++j) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
308 int n_aln;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
309 p[j] = seqs[j] + i;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
310 p[j]->n_multi = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
311 p[j]->extra_flag |= SAM_FPD | (j == 0? SAM_FR1 : SAM_FR2);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
312 fread(&n_aln, 4, 1, fp_sa[j]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
313 if (n_aln > kv_max(d->aln[j]))
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
314 kv_resize(bwt_aln1_t, d->aln[j], n_aln);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
315 d->aln[j].n = n_aln;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
316 fread(d->aln[j].a, sizeof(bwt_aln1_t), n_aln, fp_sa[j]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
317 kv_copy(bwt_aln1_t, buf[j][i].aln, d->aln[j]); // backup d->aln[j]
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
318 // generate SE alignment and mapping quality
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
319 bwa_aln2seq(n_aln, d->aln[j].a, p[j]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
320 if (p[j]->type == BWA_TYPE_UNIQUE || p[j]->type == BWA_TYPE_REPEAT) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
321 int strand;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
322 int max_diff = gopt->fnr > 0.0? bwa_cal_maxdiff(p[j]->len, BWA_AVG_ERR, gopt->fnr) : gopt->max_diff;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
323 p[j]->seQ = p[j]->mapQ = bwa_approx_mapQ(p[j], max_diff);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
324 p[j]->pos = bwa_sa2pos(bns, bwt, p[j]->sa, p[j]->len, &strand);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
325 p[j]->strand = strand;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
326 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
327 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
328 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
329
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
330 // infer isize
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
331 infer_isize(n_seqs, seqs, ii, opt->ap_prior, bwt->seq_len/2);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
332 if (ii->avg < 0.0 && last_ii->avg > 0.0) *ii = *last_ii;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
333 if (opt->force_isize) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
334 fprintf(stderr, "[%s] discard insert size estimate as user's request.\n", __func__);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
335 ii->low = ii->high = 0; ii->avg = ii->std = -1.0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
336 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
337
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
338 // PE
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
339 for (i = 0; i != n_seqs; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
340 bwa_seq_t *p[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
341 for (j = 0; j < 2; ++j) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
342 p[j] = seqs[j] + i;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
343 kv_copy(bwt_aln1_t, d->aln[j], buf[j][i].aln);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
344 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
345 if ((p[0]->type == BWA_TYPE_UNIQUE || p[0]->type == BWA_TYPE_REPEAT)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
346 && (p[1]->type == BWA_TYPE_UNIQUE || p[1]->type == BWA_TYPE_REPEAT))
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
347 { // only when both ends mapped
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
348 b128_t x;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
349 int j, k;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
350 long long n_occ[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
351 for (j = 0; j < 2; ++j) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
352 n_occ[j] = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
353 for (k = 0; k < d->aln[j].n; ++k)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
354 n_occ[j] += d->aln[j].a[k].l - d->aln[j].a[k].k + 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
355 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
356 if (n_occ[0] > opt->max_occ || n_occ[1] > opt->max_occ) continue;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
357 d->arr.n = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
358 for (j = 0; j < 2; ++j) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
359 for (k = 0; k < d->aln[j].n; ++k) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
360 bwt_aln1_t *r = d->aln[j].a + k;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
361 bwtint_t l;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
362 if (0 && r->l - r->k + 1 >= MIN_HASH_WIDTH) { // then check hash table
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
363 b128_t key;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
364 int ret;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
365 key.x = r->k; key.y = r->l;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
366 khint_t iter = kh_put(b128, g_hash, key, &ret);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
367 if (ret) { // not in the hash table; ret must equal 1 as we never remove elements
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
368 poslist_t *z = &kh_val(g_hash, iter);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
369 z->n = r->l - r->k + 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
370 z->a = (bwtint_t*)malloc(sizeof(bwtint_t) * z->n);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
371 for (l = r->k; l <= r->l; ++l) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
372 int strand;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
373 z->a[l - r->k] = bwa_sa2pos(bns, bwt, l, p[j]->len, &strand)<<1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
374 z->a[l - r->k] |= strand;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
375 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
376 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
377 for (l = 0; l < kh_val(g_hash, iter).n; ++l) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
378 x.x = kh_val(g_hash, iter).a[l]>>1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
379 x.y = k<<2 | (kh_val(g_hash, iter).a[l]&1)<<1 | j;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
380 kv_push(b128_t, d->arr, x);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
381 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
382 } else { // then calculate on the fly
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
383 for (l = r->k; l <= r->l; ++l) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
384 int strand;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
385 x.x = bwa_sa2pos(bns, bwt, l, p[j]->len, &strand);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
386 x.y = k<<2 | strand<<1 | j;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
387 kv_push(b128_t, d->arr, x);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
388 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
389 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
390 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
391 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
392 cnt_chg += pairing(p, d, opt, gopt->s_mm, ii);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
393 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
394
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
395 if (opt->N_multi || opt->n_multi) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
396 for (j = 0; j < 2; ++j) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
397 if (p[j]->type != BWA_TYPE_NO_MATCH) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
398 int k, n_multi;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
399 if (!(p[j]->extra_flag&SAM_FPP) && p[1-j]->type != BWA_TYPE_NO_MATCH) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
400 bwa_aln2seq_core(d->aln[j].n, d->aln[j].a, p[j], 0, p[j]->c1+p[j]->c2-1 > opt->N_multi? opt->n_multi : opt->N_multi);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
401 } else bwa_aln2seq_core(d->aln[j].n, d->aln[j].a, p[j], 0, opt->n_multi);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
402 for (k = 0, n_multi = 0; k < p[j]->n_multi; ++k) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
403 int strand;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
404 bwt_multi1_t *q = p[j]->multi + k;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
405 q->pos = bwa_sa2pos(bns, bwt, q->pos, p[j]->len, &strand);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
406 q->strand = strand;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
407 if (q->pos != p[j]->pos)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
408 p[j]->multi[n_multi++] = *q;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
409 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
410 p[j]->n_multi = n_multi;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
411 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
412 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
413 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
414 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
415
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
416 // free
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
417 for (i = 0; i < n_seqs; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
418 kv_destroy(buf[0][i].aln);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
419 kv_destroy(buf[1][i].aln);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
420 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
421 free(buf[0]); free(buf[1]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
422 if (_bwt == 0) bwt_destroy(bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
423 kv_destroy(d->arr);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
424 kv_destroy(d->pos[0]); kv_destroy(d->pos[1]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
425 kv_destroy(d->aln[0]); kv_destroy(d->aln[1]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
426 free(d);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
427 return cnt_chg;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
428 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
429
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
430 #define SW_MIN_MATCH_LEN 20
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
431 #define SW_MIN_MAPQ 17
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
432
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
433 // cnt = n_mm<<16 | n_gapo<<8 | n_gape
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
434 bwa_cigar_t *bwa_sw_core(bwtint_t l_pac, const ubyte_t *pacseq, int len, const ubyte_t *seq, int64_t *beg, int reglen,
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
435 int *n_cigar, uint32_t *_cnt)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
436 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
437 bwa_cigar_t *cigar = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
438 ubyte_t *ref_seq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
439 bwtint_t k, x, y, l;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
440 int path_len, ret, subo;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
441 AlnParam ap = aln_param_bwa;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
442 path_t *path, *p;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
443
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
444 // check whether there are too many N's
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
445 if (reglen < SW_MIN_MATCH_LEN || (int64_t)l_pac - *beg < len) return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
446 for (k = 0, x = 0; k < len; ++k)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
447 if (seq[k] >= 4) ++x;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
448 if ((float)x/len >= 0.25 || len - x < SW_MIN_MATCH_LEN) return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
449
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
450 // get reference subsequence
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
451 ref_seq = (ubyte_t*)calloc(reglen, 1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
452 for (k = *beg, l = 0; l < reglen && k < l_pac; ++k)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
453 ref_seq[l++] = pacseq[k>>2] >> ((~k&3)<<1) & 3;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
454 path = (path_t*)calloc(l+len, sizeof(path_t));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
455
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
456 // do alignment
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
457 ret = aln_local_core(ref_seq, l, (ubyte_t*)seq, len, &ap, path, &path_len, 1, &subo);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
458 if (ret < 0 || subo == ret) { // no hit or tandem hits
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
459 free(path); free(cigar); free(ref_seq); *n_cigar = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
460 return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
461 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
462 cigar = bwa_aln_path2cigar(path, path_len, n_cigar);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
463
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
464 // check whether the alignment is good enough
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
465 for (k = 0, x = y = 0; k < *n_cigar; ++k) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
466 bwa_cigar_t c = cigar[k];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
467 if (__cigar_op(c) == FROM_M) x += __cigar_len(c), y += __cigar_len(c);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
468 else if (__cigar_op(c) == FROM_D) x += __cigar_len(c);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
469 else y += __cigar_len(c);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
470 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
471 if (x < SW_MIN_MATCH_LEN || y < SW_MIN_MATCH_LEN) { // not good enough
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
472 free(path); free(cigar); free(ref_seq);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
473 *n_cigar = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
474 return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
475 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
476
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
477 { // update cigar and coordinate;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
478 int start, end;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
479 p = path + path_len - 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
480 *beg += (p->i? p->i : 1) - 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
481 start = (p->j? p->j : 1) - 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
482 end = path->j;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
483 cigar = (bwa_cigar_t*)realloc(cigar, sizeof(bwa_cigar_t) * (*n_cigar + 2));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
484 if (start) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
485 memmove(cigar + 1, cigar, sizeof(bwa_cigar_t) * (*n_cigar));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
486 cigar[0] = __cigar_create(3, start);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
487 ++(*n_cigar);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
488 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
489 if (end < len) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
490 /*cigar[*n_cigar] = 3<<14 | (len - end);*/
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
491 cigar[*n_cigar] = __cigar_create(3, (len - end));
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
492 ++(*n_cigar);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
493 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
494 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
495
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
496 { // set *cnt
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
497 int n_mm, n_gapo, n_gape;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
498 n_mm = n_gapo = n_gape = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
499 p = path + path_len - 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
500 x = p->i? p->i - 1 : 0; y = p->j? p->j - 1 : 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
501 for (k = 0; k < *n_cigar; ++k) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
502 bwa_cigar_t c = cigar[k];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
503 if (__cigar_op(c) == FROM_M) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
504 for (l = 0; l < (__cigar_len(c)); ++l)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
505 if (ref_seq[x+l] < 4 && seq[y+l] < 4 && ref_seq[x+l] != seq[y+l]) ++n_mm;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
506 x += __cigar_len(c), y += __cigar_len(c);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
507 } else if (__cigar_op(c) == FROM_D) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
508 x += __cigar_len(c), ++n_gapo, n_gape += (__cigar_len(c)) - 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
509 } else if (__cigar_op(c) == FROM_I) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
510 y += __cigar_len(c), ++n_gapo, n_gape += (__cigar_len(c)) - 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
511 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
512 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
513 *_cnt = (uint32_t)n_mm<<16 | n_gapo<<8 | n_gape;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
514 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
515
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
516 free(ref_seq); free(path);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
517 return cigar;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
518 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
519
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
520 ubyte_t *bwa_paired_sw(const bntseq_t *bns, const ubyte_t *_pacseq, int n_seqs, bwa_seq_t *seqs[2], const pe_opt_t *popt, const isize_info_t *ii)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
521 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
522 ubyte_t *pacseq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
523 int i;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
524 uint64_t n_tot[2], n_mapped[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
525
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
526 // load reference sequence
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
527 if (_pacseq == 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
528 pacseq = (ubyte_t*)calloc(bns->l_pac/4+1, 1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
529 rewind(bns->fp_pac);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
530 fread(pacseq, 1, bns->l_pac/4+1, bns->fp_pac);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
531 } else pacseq = (ubyte_t*)_pacseq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
532 if (!popt->is_sw || ii->avg < 0.0) return pacseq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
533
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
534 // perform mate alignment
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
535 n_tot[0] = n_tot[1] = n_mapped[0] = n_mapped[1] = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
536 for (i = 0; i != n_seqs; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
537 bwa_seq_t *p[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
538 p[0] = seqs[0] + i; p[1] = seqs[1] + i;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
539 if ((p[0]->mapQ >= SW_MIN_MAPQ || p[1]->mapQ >= SW_MIN_MAPQ) && (p[0]->extra_flag&SAM_FPP) == 0) { // unpaired and one read has high mapQ
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
540 int k, n_cigar[2], is_singleton, mapQ = 0, mq_adjust[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
541 int64_t beg[2], end[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
542 bwa_cigar_t *cigar[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
543 uint32_t cnt[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
544
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
545 /* In the following, _pref points to the reference read
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
546 * which must be aligned; _pmate points to its mate which is
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
547 * considered to be modified. */
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
548
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
549 #define __set_rght_coor(_a, _b, _pref, _pmate) do { \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
550 (_a) = (int64_t)_pref->pos + ii->avg - 3 * ii->std - _pmate->len * 1.5; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
551 (_b) = (_a) + 6 * ii->std + 2 * _pmate->len; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
552 if ((_a) < (int64_t)_pref->pos + _pref->len) (_a) = _pref->pos + _pref->len; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
553 if ((_b) > bns->l_pac) (_b) = bns->l_pac; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
554 } while (0)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
555
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
556 #define __set_left_coor(_a, _b, _pref, _pmate) do { \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
557 (_a) = (int64_t)_pref->pos + _pref->len - ii->avg - 3 * ii->std - _pmate->len * 0.5; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
558 (_b) = (_a) + 6 * ii->std + 2 * _pmate->len; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
559 if ((_a) < 0) (_a) = 0; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
560 if ((_b) > _pref->pos) (_b) = _pref->pos; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
561 } while (0)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
562
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
563 #define __set_fixed(_pref, _pmate, _beg, _cnt) do { \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
564 _pmate->type = BWA_TYPE_MATESW; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
565 _pmate->pos = _beg; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
566 _pmate->seQ = _pref->seQ; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
567 _pmate->strand = (popt->type == BWA_PET_STD)? 1 - _pref->strand : _pref->strand; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
568 _pmate->n_mm = _cnt>>16; _pmate->n_gapo = _cnt>>8&0xff; _pmate->n_gape = _cnt&0xff; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
569 _pmate->extra_flag |= SAM_FPP; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
570 _pref->extra_flag |= SAM_FPP; \
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
571 } while (0)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
572
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
573 mq_adjust[0] = mq_adjust[1] = 255; // not effective
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
574 is_singleton = (p[0]->type == BWA_TYPE_NO_MATCH || p[1]->type == BWA_TYPE_NO_MATCH)? 1 : 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
575
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
576 ++n_tot[is_singleton];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
577 cigar[0] = cigar[1] = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
578 n_cigar[0] = n_cigar[1] = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
579 if (popt->type != BWA_PET_STD && popt->type != BWA_PET_SOLID) continue; // other types of pairing is not considered
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
580 for (k = 0; k < 2; ++k) { // p[1-k] is the reference read and p[k] is the read considered to be modified
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
581 ubyte_t *seq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
582 if (p[1-k]->type == BWA_TYPE_NO_MATCH) continue; // if p[1-k] is unmapped, skip
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
583 if (popt->type == BWA_PET_STD) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
584 if (p[1-k]->strand == 0) { // then the mate is on the reverse strand and has larger coordinate
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
585 __set_rght_coor(beg[k], end[k], p[1-k], p[k]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
586 seq = p[k]->rseq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
587 } else { // then the mate is on forward stand and has smaller coordinate
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
588 __set_left_coor(beg[k], end[k], p[1-k], p[k]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
589 seq = p[k]->seq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
590 seq_reverse(p[k]->len, seq, 0); // because ->seq is reversed; this will reversed back shortly
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
591 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
592 } else { // BWA_PET_SOLID
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
593 if (p[1-k]->strand == 0) { // R3-F3 pairing
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
594 if (k == 0) __set_left_coor(beg[k], end[k], p[1-k], p[k]); // p[k] is R3
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
595 else __set_rght_coor(beg[k], end[k], p[1-k], p[k]); // p[k] is F3
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
596 seq = p[k]->rseq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
597 seq_reverse(p[k]->len, seq, 0); // because ->seq is reversed
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
598 } else { // F3-R3 pairing
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
599 if (k == 0) __set_rght_coor(beg[k], end[k], p[1-k], p[k]); // p[k] is R3
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
600 else __set_left_coor(beg[k], end[k], p[1-k], p[k]); // p[k] is F3
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
601 seq = p[k]->seq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
602 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
603 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
604 // perform SW alignment
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
605 cigar[k] = bwa_sw_core(bns->l_pac, pacseq, p[k]->len, seq, &beg[k], end[k] - beg[k], &n_cigar[k], &cnt[k]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
606 if (cigar[k] && p[k]->type != BWA_TYPE_NO_MATCH) { // re-evaluate cigar[k]
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
607 int s_old, clip = 0, s_new;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
608 if (__cigar_op(cigar[k][0]) == 3) clip += __cigar_len(cigar[k][0]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
609 if (__cigar_op(cigar[k][n_cigar[k]-1]) == 3) clip += __cigar_len(cigar[k][n_cigar[k]-1]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
610 s_old = (int)((p[k]->n_mm * 9 + p[k]->n_gapo * 13 + p[k]->n_gape * 2) / 3. * 8. + .499);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
611 s_new = (int)(((cnt[k]>>16) * 9 + (cnt[k]>>8&0xff) * 13 + (cnt[k]&0xff) * 2 + clip * 3) / 3. * 8. + .499);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
612 s_old += -4.343 * log(ii->ap_prior / bns->l_pac);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
613 s_new += (int)(-4.343 * log(.5 * erfc(M_SQRT1_2 * 1.5) + .499)); // assume the mapped isize is 1.5\sigma
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
614 if (s_old < s_new) { // reject SW alignment
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
615 mq_adjust[k] = s_new - s_old;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
616 free(cigar[k]); cigar[k] = 0; n_cigar[k] = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
617 } else mq_adjust[k] = s_old - s_new;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
618 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
619 // now revserse sequence back such that p[*]->seq looks untouched
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
620 if (popt->type == BWA_PET_STD) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
621 if (p[1-k]->strand == 1) seq_reverse(p[k]->len, seq, 0);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
622 } else {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
623 if (p[1-k]->strand == 0) seq_reverse(p[k]->len, seq, 0);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
624 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
625 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
626 k = -1; // no read to be changed
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
627 if (cigar[0] && cigar[1]) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
628 k = p[0]->mapQ < p[1]->mapQ? 0 : 1; // p[k] to be fixed
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
629 mapQ = abs(p[1]->mapQ - p[0]->mapQ);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
630 } else if (cigar[0]) k = 0, mapQ = p[1]->mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
631 else if (cigar[1]) k = 1, mapQ = p[0]->mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
632 if (k >= 0 && p[k]->pos != beg[k]) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
633 ++n_mapped[is_singleton];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
634 { // recalculate mapping quality
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
635 int tmp = (int)p[1-k]->mapQ - p[k]->mapQ/2 - 8;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
636 if (tmp <= 0) tmp = 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
637 if (mapQ > tmp) mapQ = tmp;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
638 p[k]->mapQ = p[1-k]->mapQ = mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
639 p[k]->seQ = p[1-k]->seQ = p[1-k]->seQ < mapQ? p[1-k]->seQ : mapQ;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
640 if (p[k]->mapQ > mq_adjust[k]) p[k]->mapQ = mq_adjust[k];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
641 if (p[k]->seQ > mq_adjust[k]) p[k]->seQ = mq_adjust[k];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
642 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
643 // update CIGAR
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
644 free(p[k]->cigar); p[k]->cigar = cigar[k]; cigar[k] = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
645 p[k]->n_cigar = n_cigar[k];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
646 // update the rest of information
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
647 __set_fixed(p[1-k], p[k], beg[k], cnt[k]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
648 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
649 free(cigar[0]); free(cigar[1]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
650 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
651 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
652 fprintf(stderr, "[bwa_paired_sw] %lld out of %lld Q%d singletons are mated.\n",
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
653 (long long)n_mapped[1], (long long)n_tot[1], SW_MIN_MAPQ);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
654 fprintf(stderr, "[bwa_paired_sw] %lld out of %lld Q%d discordant pairs are fixed.\n",
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
655 (long long)n_mapped[0], (long long)n_tot[0], SW_MIN_MAPQ);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
656 return pacseq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
657 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
658
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
659 void bwa_sai2sam_pe_core(const char *prefix, char *const fn_sa[2], char *const fn_fa[2], pe_opt_t *popt)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
660 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
661 extern bwa_seqio_t *bwa_open_reads(int mode, const char *fn_fa);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
662 int i, j, n_seqs, tot_seqs = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
663 bwa_seq_t *seqs[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
664 bwa_seqio_t *ks[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
665 clock_t t;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
666 bntseq_t *bns, *ntbns = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
667 FILE *fp_sa[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
668 gap_opt_t opt, opt0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
669 khint_t iter;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
670 isize_info_t last_ii; // this is for the last batch of reads
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
671 char str[1024];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
672 bwt_t *bwt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
673 uint8_t *pac;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
674
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
675 // initialization
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
676 bwase_initialize(); // initialize g_log_n[] in bwase.c
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
677 pac = 0; bwt = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
678 for (i = 1; i != 256; ++i) g_log_n[i] = (int)(4.343 * log(i) + 0.5);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
679 bns = bns_restore(prefix);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
680 srand48(bns->seed);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
681 fp_sa[0] = xopen(fn_sa[0], "r");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
682 fp_sa[1] = xopen(fn_sa[1], "r");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
683 g_hash = kh_init(b128);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
684 last_ii.avg = -1.0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
685
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
686 fread(&opt, sizeof(gap_opt_t), 1, fp_sa[0]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
687 ks[0] = bwa_open_reads(opt.mode, fn_fa[0]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
688 opt0 = opt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
689 fread(&opt, sizeof(gap_opt_t), 1, fp_sa[1]); // overwritten!
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
690 ks[1] = bwa_open_reads(opt.mode, fn_fa[1]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
691 if (!(opt.mode & BWA_MODE_COMPREAD)) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
692 popt->type = BWA_PET_SOLID;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
693 ntbns = bwa_open_nt(prefix);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
694 } else { // for Illumina alignment only
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
695 if (popt->is_preload) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
696 strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
697 strcpy(str, prefix); strcat(str, ".sa"); bwt_restore_sa(str, bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
698 pac = (ubyte_t*)calloc(bns->l_pac/4+1, 1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
699 rewind(bns->fp_pac);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
700 fread(pac, 1, bns->l_pac/4+1, bns->fp_pac);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
701 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
702 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
703
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
704 // core loop
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
705 bwa_print_sam_SQ(bns);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
706 bwa_print_sam_PG();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
707 while ((seqs[0] = bwa_read_seq(ks[0], 0x40000, &n_seqs, opt0.mode, opt0.trim_qual)) != 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
708 int cnt_chg;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
709 isize_info_t ii;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
710 ubyte_t *pacseq;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
711
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
712 seqs[1] = bwa_read_seq(ks[1], 0x40000, &n_seqs, opt.mode, opt.trim_qual);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
713 tot_seqs += n_seqs;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
714 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
715
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
716 fprintf(stderr, "[bwa_sai2sam_pe_core] convert to sequence coordinate... \n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
717 cnt_chg = bwa_cal_pac_pos_pe(bns, prefix, bwt, n_seqs, seqs, fp_sa, &ii, popt, &opt, &last_ii);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
718 fprintf(stderr, "[bwa_sai2sam_pe_core] time elapses: %.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
719 fprintf(stderr, "[bwa_sai2sam_pe_core] changing coordinates of %d alignments.\n", cnt_chg);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
720
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
721 fprintf(stderr, "[bwa_sai2sam_pe_core] align unmapped mate...\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
722 pacseq = bwa_paired_sw(bns, pac, n_seqs, seqs, popt, &ii);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
723 fprintf(stderr, "[bwa_sai2sam_pe_core] time elapses: %.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
724
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
725 fprintf(stderr, "[bwa_sai2sam_pe_core] refine gapped alignments... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
726 for (j = 0; j < 2; ++j)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
727 bwa_refine_gapped(bns, n_seqs, seqs[j], pacseq, ntbns);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
728 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
729 if (pac == 0) free(pacseq);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
730
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
731 fprintf(stderr, "[bwa_sai2sam_pe_core] print alignments... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
732 for (i = 0; i < n_seqs; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
733 bwa_seq_t *p[2];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
734 p[0] = seqs[0] + i; p[1] = seqs[1] + i;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
735 if (p[0]->bc[0] || p[1]->bc[0]) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
736 strcat(p[0]->bc, p[1]->bc);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
737 strcpy(p[1]->bc, p[0]->bc);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
738 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
739 bwa_print_sam1(bns, p[0], p[1], opt.mode, opt.max_top2);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
740 bwa_print_sam1(bns, p[1], p[0], opt.mode, opt.max_top2);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
741 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
742 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
743
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
744 for (j = 0; j < 2; ++j)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
745 bwa_free_read_seq(n_seqs, seqs[j]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
746 fprintf(stderr, "[bwa_sai2sam_pe_core] %d sequences have been processed.\n", tot_seqs);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
747 last_ii = ii;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
748 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
749
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
750 // destroy
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
751 bns_destroy(bns);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
752 if (ntbns) bns_destroy(ntbns);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
753 for (i = 0; i < 2; ++i) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
754 bwa_seq_close(ks[i]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
755 fclose(fp_sa[i]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
756 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
757 for (iter = kh_begin(g_hash); iter != kh_end(g_hash); ++iter)
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
758 if (kh_exist(g_hash, iter)) free(kh_val(g_hash, iter).a);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
759 kh_destroy(b128, g_hash);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
760 if (pac) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
761 free(pac); bwt_destroy(bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
762 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
763 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
764
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
765 int bwa_sai2sam_pe(int argc, char *argv[])
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
766 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
767 extern char *bwa_rg_line, *bwa_rg_id;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
768 extern int bwa_set_rg(const char *s);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
769 extern char *bwa_infer_prefix(const char *hint);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
770 int c;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
771 pe_opt_t *popt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
772 char *prefix;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
773
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
774 popt = bwa_init_pe_opt();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
775 while ((c = getopt(argc, argv, "a:o:sPn:N:c:f:Ar:")) >= 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
776 switch (c) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
777 case 'r':
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
778 if (bwa_set_rg(optarg) < 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
779 fprintf(stderr, "[%s] malformated @RG line\n", __func__);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
780 return 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
781 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
782 break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
783 case 'a': popt->max_isize = atoi(optarg); break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
784 case 'o': popt->max_occ = atoi(optarg); break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
785 case 's': popt->is_sw = 0; break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
786 case 'P': popt->is_preload = 1; break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
787 case 'n': popt->n_multi = atoi(optarg); break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
788 case 'N': popt->N_multi = atoi(optarg); break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
789 case 'c': popt->ap_prior = atof(optarg); break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
790 case 'f': xreopen(optarg, "w", stdout); break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
791 case 'A': popt->force_isize = 1; break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
792 default: return 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
793 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
794 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
795
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
796 if (optind + 5 > argc) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
797 fprintf(stderr, "\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
798 fprintf(stderr, "Usage: bwa sampe [options] <prefix> <in1.sai> <in2.sai> <in1.fq> <in2.fq>\n\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
799 fprintf(stderr, "Options: -a INT maximum insert size [%d]\n", popt->max_isize);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
800 fprintf(stderr, " -o INT maximum occurrences for one end [%d]\n", popt->max_occ);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
801 fprintf(stderr, " -n INT maximum hits to output for paired reads [%d]\n", popt->n_multi);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
802 fprintf(stderr, " -N INT maximum hits to output for discordant pairs [%d]\n", popt->N_multi);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
803 fprintf(stderr, " -c FLOAT prior of chimeric rate (lower bound) [%.1le]\n", popt->ap_prior);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
804 fprintf(stderr, " -f FILE sam file to output results to [stdout]\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
805 fprintf(stderr, " -r STR read group header line such as `@RG\\tID:foo\\tSM:bar' [null]\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
806 fprintf(stderr, " -P preload index into memory (for base-space reads only)\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
807 fprintf(stderr, " -s disable Smith-Waterman for the unmapped mate\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
808 fprintf(stderr, " -A disable insert size estimate (force -s)\n\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
809 fprintf(stderr, "Notes: 1. For SOLiD reads, <in1.fq> corresponds R3 reads and <in2.fq> to F3.\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
810 fprintf(stderr, " 2. For reads shorter than 30bp, applying a smaller -o is recommended to\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
811 fprintf(stderr, " to get a sensible speed at the cost of pairing accuracy.\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
812 fprintf(stderr, "\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
813 return 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
814 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
815 if ((prefix = bwa_infer_prefix(argv[optind])) == 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
816 fprintf(stderr, "[%s] fail to locate the index\n", __func__);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
817 free(bwa_rg_line); free(bwa_rg_id);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
818 return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
819 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
820 bwa_sai2sam_pe_core(prefix, argv + optind + 1, argv + optind+3, popt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
821 free(bwa_rg_line); free(bwa_rg_id); free(prefix);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
822 free(popt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
823 return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
824 }