minibwa 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +119 -0
- data/ext/minibwa/extconf.rb +105 -0
- data/ext/minibwa/mb_buffer.c +113 -0
- data/ext/minibwa/mb_hit.c +192 -0
- data/ext/minibwa/mb_index.c +462 -0
- data/ext/minibwa/mb_index_build.c +174 -0
- data/ext/minibwa/mb_options.c +301 -0
- data/ext/minibwa/minibwa/LICENSE.txt +37 -0
- data/ext/minibwa/minibwa/align.c +930 -0
- data/ext/minibwa/minibwa/bseq.h +45 -0
- data/ext/minibwa/minibwa/bwt.c +715 -0
- data/ext/minibwa/minibwa/bwt.h +86 -0
- data/ext/minibwa/minibwa/cs.c +161 -0
- data/ext/minibwa/minibwa/format.c +356 -0
- data/ext/minibwa/minibwa/index.c +342 -0
- data/ext/minibwa/minibwa/kalloc.c +224 -0
- data/ext/minibwa/minibwa/kalloc.h +54 -0
- data/ext/minibwa/minibwa/ketopt.h +123 -0
- data/ext/minibwa/minibwa/kommon.c +374 -0
- data/ext/minibwa/minibwa/kommon.h +85 -0
- data/ext/minibwa/minibwa/kseq.h +256 -0
- data/ext/minibwa/minibwa/ksort.h +163 -0
- data/ext/minibwa/minibwa/ksw2.h +220 -0
- data/ext/minibwa/minibwa/ksw2_extd2_sse.c +403 -0
- data/ext/minibwa/minibwa/ksw2_extz2_sse.c +296 -0
- data/ext/minibwa/minibwa/ksw2_ll_sse.c +341 -0
- data/ext/minibwa/minibwa/kthread.h +15 -0
- data/ext/minibwa/minibwa/l2bit.c +479 -0
- data/ext/minibwa/minibwa/l2bit.h +72 -0
- data/ext/minibwa/minibwa/lchain.c +231 -0
- data/ext/minibwa/minibwa/libsais.c +6985 -0
- data/ext/minibwa/minibwa/libsais.h +106 -0
- data/ext/minibwa/minibwa/libsais64.c +7064 -0
- data/ext/minibwa/minibwa/libsais64.h +81 -0
- data/ext/minibwa/minibwa/map-algo.c +769 -0
- data/ext/minibwa/minibwa/mbpriv.h +148 -0
- data/ext/minibwa/minibwa/minibwa.h +176 -0
- data/ext/minibwa/minibwa/options.c +116 -0
- data/ext/minibwa/minibwa/pe.c +559 -0
- data/ext/minibwa/minibwa/s2n-lite.h +59 -0
- data/ext/minibwa/minibwa/seed.c +354 -0
- data/ext/minibwa/minibwa.c +67 -0
- data/ext/minibwa/minibwa.h +51 -0
- data/lib/minibwa/hit.rb +110 -0
- data/lib/minibwa/index.rb +77 -0
- data/lib/minibwa/options.rb +235 -0
- data/lib/minibwa/sam.rb +85 -0
- data/lib/minibwa/version.rb +6 -0
- data/lib/minibwa.rb +11 -0
- metadata +88 -0
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
#include <assert.h>
|
|
5
|
+
#include <math.h>
|
|
6
|
+
#include "mbpriv.h"
|
|
7
|
+
#include "kalloc.h"
|
|
8
|
+
#include "ksw2.h"
|
|
9
|
+
|
|
10
|
+
static inline int mb_insert_dir(const mb_hit_t *h0, const mb_hit_t *h1, int64_t *dist)
|
|
11
|
+
{
|
|
12
|
+
int64_t p0, p1;
|
|
13
|
+
p0 = h0->rev? h0->te : h0->ts;
|
|
14
|
+
p1 = h1->rev? h1->te : h1->ts;
|
|
15
|
+
*dist = p0 > p1? p0 - p1 : p1 - p0;
|
|
16
|
+
return ((int32_t)h0->rev << 1 | (int32_t)h1->rev) ^ (p0 < p1? 0 : 3);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static inline double mb_pair_score(const mb_hit_t *h0, const mb_hit_t *h1, const mb_pestat_t pes[4], int32_t match_sc)
|
|
20
|
+
{
|
|
21
|
+
const double MB_SQRT1_2 = 0.707106781186547524401;
|
|
22
|
+
int32_t dir;
|
|
23
|
+
int64_t dist;
|
|
24
|
+
dir = mb_insert_dir(h0, h1, &dist);
|
|
25
|
+
if (!pes[dir].failed && dist >= pes[dir].lo && dist <= pes[dir].hi) {
|
|
26
|
+
double ns = (dist - pes[dir].avg) / pes[dir].std; // normalized score
|
|
27
|
+
return h0->p->dp_max + h1->p->dp_max + .721 * log(2. * erfc(fabs(ns) * MB_SQRT1_2)) * match_sc; // .721 = 1/log(4)
|
|
28
|
+
}
|
|
29
|
+
return -1.0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static const mb_hit_t *mb_select_unique_se(int32_t n_hit, const mb_hit_t *hit)
|
|
33
|
+
{
|
|
34
|
+
int32_t j, n_pri = 0, mapq = 0, k = -1;
|
|
35
|
+
for (j = 0; j < n_hit; ++j)
|
|
36
|
+
if (hit[j].id == hit[j].parent)
|
|
37
|
+
++n_pri, mapq = hit[j].mapq, k = j;
|
|
38
|
+
return n_pri == 1 && mapq >= 10? &hit[k] : 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static int32_t mb_hit_sum_score(void *km, int32_t n_hit, const mb_hit_t *hit)
|
|
42
|
+
{
|
|
43
|
+
static const int32_t max_cnt_heap = 8;
|
|
44
|
+
int32_t i, k, n_pri, sc, qe;
|
|
45
|
+
uint64_t *a, aa[max_cnt_heap];
|
|
46
|
+
for (i = 0, n_pri = sc = 0; i < n_hit; ++i) // precalculate size
|
|
47
|
+
if (hit[i].id == hit[i].parent && hit[i].p)
|
|
48
|
+
++n_pri, sc = hit[i].p->dp_max; // NB: requiring base alignment
|
|
49
|
+
if (n_pri == 0) return 0;
|
|
50
|
+
if (n_pri == 1) return sc;
|
|
51
|
+
a = n_pri < max_cnt_heap? aa : Kmalloc(km, uint64_t, n_pri);
|
|
52
|
+
for (i = k = 0; i < n_hit; ++i)
|
|
53
|
+
if (hit[i].id == hit[i].parent && hit[i].p)
|
|
54
|
+
a[k++] = (uint64_t)hit[i].qs << 32 | i;
|
|
55
|
+
radix_sort_mb64(a, a + k);
|
|
56
|
+
for (i = 0, sc = qe = 0; i < k; ++i) {
|
|
57
|
+
const mb_hit_t *h = &hit[(uint32_t)a[i]];
|
|
58
|
+
if (h->qe <= qe) continue;
|
|
59
|
+
if (h->qs < qe)
|
|
60
|
+
sc += (int32_t)((double)(h->qe - qe) / (h->qe - h->qs) * h->p->dp_max + .499);
|
|
61
|
+
else sc += h->p->dp_max;
|
|
62
|
+
qe = h->qe;
|
|
63
|
+
}
|
|
64
|
+
if (a != aa) kfree(km, a);
|
|
65
|
+
return sc;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
void mb_pestat(void *km, const mb_opt_t *opt, int32_t n_frag, const int32_t *seg_off, const int32_t *seg_cnt, const int32_t *n_hit, mb_hit_t *const *hit, mb_pestat_t pes[4])
|
|
69
|
+
{
|
|
70
|
+
const int MIN_DIR_CNT = 20;
|
|
71
|
+
const double MIN_DIR_RATIO = 0.05, OUTLIER_BOUND = 2.0, MAPPING_BOUND = 3.0, MAX_STDDEV = 4.0;
|
|
72
|
+
int32_t i, d, max;
|
|
73
|
+
struct { int32_t n, m; uint64_t *a; } is[4], *q;
|
|
74
|
+
memset(is, 0, sizeof(is[0]) * 4);
|
|
75
|
+
memset(pes, 0, sizeof(pes[0]) * 4);
|
|
76
|
+
for (i = 0; i < n_frag; ++i) {
|
|
77
|
+
const mb_hit_t *r[2];
|
|
78
|
+
int32_t off, dir;
|
|
79
|
+
int64_t dist;
|
|
80
|
+
if (seg_cnt[i] != 2) continue;
|
|
81
|
+
off = seg_off[i];
|
|
82
|
+
r[0] = mb_select_unique_se(n_hit[off + 0], hit[off + 0]);
|
|
83
|
+
r[1] = mb_select_unique_se(n_hit[off + 1], hit[off + 1]);
|
|
84
|
+
if (r[0] == 0 || r[1] == 0) continue;
|
|
85
|
+
if (r[0]->tid != r[1]->tid) continue; // not on the same contig
|
|
86
|
+
dir = mb_insert_dir(r[0], r[1], &dist);
|
|
87
|
+
if (dist < opt->max_pe_ins) {
|
|
88
|
+
if (is[dir].n == is[dir].m)
|
|
89
|
+
Kgrow(km, uint64_t, is[dir].a, is[dir].n, is[dir].m);
|
|
90
|
+
is[dir].a[is[dir].n++] = dist;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (kom_verbose >= 3)
|
|
94
|
+
fprintf(stderr, "[M::%s] # candidate unique pairs for (FF, FR, RF, RR): (%d, %d, %d, %d)\n", __func__, is[0].n, is[1].n, is[2].n, is[3].n);
|
|
95
|
+
for (d = 0, max = 0; d < 4; ++d)
|
|
96
|
+
max = max > is[d].n? max : is[d].n;
|
|
97
|
+
for (d = 0; d < 4; ++d) {
|
|
98
|
+
mb_pestat_t *r = &pes[d];
|
|
99
|
+
q = &is[d];
|
|
100
|
+
int p25, p50, p75, x;
|
|
101
|
+
if (q->n < MIN_DIR_CNT || q->n < max * MIN_DIR_RATIO) {
|
|
102
|
+
r->failed = 1;
|
|
103
|
+
kfree(km, q->a);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
radix_sort_mb64(q->a, q->a + q->n);
|
|
107
|
+
p25 = q->a[(int)(.25 * q->n + .499)];
|
|
108
|
+
p50 = q->a[(int)(.50 * q->n + .499)];
|
|
109
|
+
p75 = q->a[(int)(.75 * q->n + .499)];
|
|
110
|
+
r->lo = (int)(p25 - OUTLIER_BOUND * (p75 - p25) + .499);
|
|
111
|
+
if (r->lo < 1) r->lo = 1;
|
|
112
|
+
r->hi = (int)(p75 + OUTLIER_BOUND * (p75 - p25) + .499);
|
|
113
|
+
for (i = x = 0, r->avg = 0; i < q->n; ++i)
|
|
114
|
+
if (q->a[i] >= r->lo && q->a[i] <= r->hi)
|
|
115
|
+
r->avg += q->a[i], ++x;
|
|
116
|
+
r->avg /= x;
|
|
117
|
+
for (i = 0, r->std = 0; i < q->n; ++i)
|
|
118
|
+
if (q->a[i] >= r->lo && q->a[i] <= r->hi)
|
|
119
|
+
r->std += (q->a[i] - r->avg) * (q->a[i] - r->avg);
|
|
120
|
+
r->std = sqrt(r->std / x);
|
|
121
|
+
if (kom_verbose >= 3)
|
|
122
|
+
fprintf(stderr, "[M::%s::%c%c] (25, 50, 75) percentile: (%d, %d, %d); mean and std.dev: (%.2f, %.2f)\n",
|
|
123
|
+
__func__, "FR"[d>>1&1], "FR"[d&1], p25, p50, p75, r->avg, r->std);
|
|
124
|
+
r->lo = (int)(p25 - MAPPING_BOUND * (p75 - p25) + .499);
|
|
125
|
+
r->hi = (int)(p75 + MAPPING_BOUND * (p75 - p25) + .499);
|
|
126
|
+
if (r->lo > r->avg - MAX_STDDEV * r->std) r->lo = (int)(r->avg - MAX_STDDEV * r->std + .499);
|
|
127
|
+
if (r->hi < r->avg + MAX_STDDEV * r->std) r->hi = (int)(r->avg + MAX_STDDEV * r->std + .499);
|
|
128
|
+
if (r->lo < 1) r->lo = 1;
|
|
129
|
+
if (kom_verbose >= 3)
|
|
130
|
+
fprintf(stderr, "[M::%s::%c%c] low and high boundaries for proper pairs: (%d, %d)\n", __func__, "FR"[d>>1&1], "FR"[d&1], r->lo, r->hi);
|
|
131
|
+
kfree(km, q->a);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
typedef struct {
|
|
136
|
+
int32_t score, sub_sc, n_sub, n_pp;
|
|
137
|
+
int32_t i[2];
|
|
138
|
+
} mb_pairaux_t;
|
|
139
|
+
|
|
140
|
+
static void mb_pair_hits(void *km, const mb_opt_t *opt, const l2b_t *l2b, int32_t n_hit[2], mb_hit_t *hit[2], const mb_pestat_t pes[4], mb_pairaux_t *ret)
|
|
141
|
+
{
|
|
142
|
+
int32_t r, i, k, n_pa, y[4], n_pp = 0, m_pp = 0;
|
|
143
|
+
mb128_t *pa, *pp = 0; // pp: proper pairs
|
|
144
|
+
|
|
145
|
+
ret->i[0] = ret->i[1] = ret->score = ret->sub_sc = -1, ret->n_sub = ret->n_pp = 0;
|
|
146
|
+
if (n_hit[0] == 0 || n_hit[1] == 0) return;
|
|
147
|
+
pa = Kcalloc(km, mb128_t, n_hit[0] + n_hit[1]);
|
|
148
|
+
for (r = n_pa = 0; r < 2; ++r) {
|
|
149
|
+
for (i = 0; i < n_hit[r]; ++i) {
|
|
150
|
+
mb128_t *p = &pa[n_pa++];
|
|
151
|
+
mb_hit_t *h = &hit[r][i];
|
|
152
|
+
h->proper_pair = 0;
|
|
153
|
+
p->x = l2b->ctg[h->tid].off + (h->rev? h->te : h->ts);
|
|
154
|
+
p->y = (uint64_t)i << 2 | (uint64_t)h->rev << 1 | r;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
radix_sort_mb128x(pa, pa + n_pa);
|
|
158
|
+
|
|
159
|
+
y[0] = y[1] = y[2] = y[3] = -1;
|
|
160
|
+
for (i = 0; i < n_pa; ++i) {
|
|
161
|
+
mb128_t *pi = &pa[i];
|
|
162
|
+
mb_hit_t *hi = &hit[pi->y&1][pi->y>>2];
|
|
163
|
+
for (r = 0; r < 2; ++r) {
|
|
164
|
+
int which, dir = r << 1 | (pi->y>>1&1);
|
|
165
|
+
if (pes[dir].failed) continue; // invalid orientation
|
|
166
|
+
which = r << 1 | ((pi->y&1) ^ 1);
|
|
167
|
+
if (y[which] < 0) continue; // no previous hit
|
|
168
|
+
for (k = y[which]; k >= 0; --k) {
|
|
169
|
+
mb128_t *pk = &pa[k], *q;
|
|
170
|
+
mb_hit_t *hk = &hit[pk->y&1][pk->y>>2];
|
|
171
|
+
int64_t dist;
|
|
172
|
+
double s;
|
|
173
|
+
if ((pk->y&3) != which) continue;
|
|
174
|
+
if (hi->tid != hk->tid) break;
|
|
175
|
+
dist = pi->x - pk->x;
|
|
176
|
+
if (dist > pes[dir].hi) break;
|
|
177
|
+
if (dist < pes[dir].lo) continue;
|
|
178
|
+
hk->proper_pair = hi->proper_pair = 1; // paired
|
|
179
|
+
s = mb_pair_score(hk, hi, pes, opt->a);
|
|
180
|
+
if (s < 0.) s = 0.;
|
|
181
|
+
if (n_pp == m_pp) Kgrow(km, mb128_t, pp, n_pp, m_pp);
|
|
182
|
+
q = &pp[n_pp++];
|
|
183
|
+
q->y = (pk->y&1) == 0? (uint64_t)(pk->y>>2) << 32 | (pi->y>>2) : (uint64_t)(pi->y>>2) << 32 | (pk->y>>2); // upper bits: index to read[0]
|
|
184
|
+
q->x = (uint64_t)(s + .499) << 32 | (hk->hash ^ hi->hash);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
y[pi->y&3] = i;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
ret->n_pp = n_pp;
|
|
191
|
+
if (n_pp > 0) {
|
|
192
|
+
uint64_t max = 0, max2 = 0;
|
|
193
|
+
int32_t tmp = opt->a + opt->b > opt->q + opt->e? opt->a + opt->b : opt->q + opt->e;
|
|
194
|
+
for (i = 0; i < n_pp; ++i) { // find max and max2
|
|
195
|
+
mb128_t *q = &pp[i];
|
|
196
|
+
if (q->x > max) max2 = max, max = q->x, ret->i[0] = q->y>>32, ret->i[1] = (uint32_t)q->y;
|
|
197
|
+
else if (q->x > max2) max2 = q->x;
|
|
198
|
+
}
|
|
199
|
+
assert(ret->i[0] < n_hit[0] && ret->i[1] < n_hit[1]);
|
|
200
|
+
ret->score = max>>32, ret->sub_sc = max2>>32;
|
|
201
|
+
for (i = 0; i < n_pp; ++i)
|
|
202
|
+
if (pp[i].x>>32 >= ret->score - tmp)
|
|
203
|
+
ret->n_sub++;
|
|
204
|
+
}
|
|
205
|
+
kfree(km, pp);
|
|
206
|
+
kfree(km, pa);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static int32_t mb_ungap(void *km, int32_t qlen, const uint8_t *qseq, int32_t tlen, const uint8_t *tseq, int32_t kmer, l2b_meth_t mt, int32_t *max_i, int32_t *n_good, int32_t *n_kmer)
|
|
210
|
+
{ // a linear algorithm to find ungapped alignment
|
|
211
|
+
static uint8_t c2t[4] = { 0, 3, 2, 3 };
|
|
212
|
+
static uint8_t g2a[4] = { 0, 1, 0, 3 };
|
|
213
|
+
int32_t i, l, cap = 1 << kmer*2, mask = cap - 1, max, *a;
|
|
214
|
+
uint16_t *h, x;
|
|
215
|
+
*max_i = -1, *n_good = *n_kmer = 0;
|
|
216
|
+
if (qlen >= UINT16_MAX) return 0;
|
|
217
|
+
a = Kcalloc(km, int32_t, tlen);
|
|
218
|
+
h = Kcalloc(km, uint16_t, cap);
|
|
219
|
+
for (i = l = 0, x = 0; i < qlen; ++i) {
|
|
220
|
+
if (qseq[i] < 4) {
|
|
221
|
+
uint8_t c = mt == L2B_METH_C2T? c2t[qseq[i]] : mt == L2B_METH_G2A? g2a[qseq[i]] : qseq[i];
|
|
222
|
+
x = (x << 2 | c) & mask;
|
|
223
|
+
if (++l >= kmer) {
|
|
224
|
+
if (h[x] == 0) ++*n_kmer; // n_kmer is the number of distinct k-mers on the query sequence
|
|
225
|
+
h[x] = i;
|
|
226
|
+
}
|
|
227
|
+
} else x = 0, l = 0;
|
|
228
|
+
}
|
|
229
|
+
for (i = l = 0, x = 0; i < tlen; ++i) {
|
|
230
|
+
if (tseq[i] < 4) {
|
|
231
|
+
uint8_t c = mt == L2B_METH_C2T? c2t[tseq[i]] : mt == L2B_METH_G2A? g2a[tseq[i]] : tseq[i];
|
|
232
|
+
x = (x << 2 | c) & mask;
|
|
233
|
+
if (++l >= kmer) {
|
|
234
|
+
if (h[x] > 0 && i >= h[x])
|
|
235
|
+
++a[i - h[x]]; // inspired by the Hough transform for line finding
|
|
236
|
+
}
|
|
237
|
+
} else x = 0, l = 0;
|
|
238
|
+
}
|
|
239
|
+
for (i = 0, max = 0; i < tlen; ++i)
|
|
240
|
+
if (max < a[i])
|
|
241
|
+
max = a[i], *max_i = i;
|
|
242
|
+
for (i = 0, *n_good = 0; i < tlen; ++i)
|
|
243
|
+
if (a[i] > max>>1) ++*n_good;
|
|
244
|
+
kfree(km, h);
|
|
245
|
+
kfree(km, a);
|
|
246
|
+
return max;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static void mb_matesw_align(void *km, const mb_opt_t *opt, int32_t qlen, uint8_t *qseq, int32_t tlen, uint8_t *tseq, mb_hit_t *h, int32_t min_sc, l2b_meth_t mt, ksw_extz_t *ez)
|
|
250
|
+
{
|
|
251
|
+
int8_t mat[25];
|
|
252
|
+
int32_t max_sc = qlen < tlen? qlen : tlen;
|
|
253
|
+
int32_t b_mm = (opt->b + opt->a - 1) / opt->a;
|
|
254
|
+
int32_t b_ts = (opt->b_ts + opt->a - 1) / opt->a;
|
|
255
|
+
int32_t b_ambi = (opt->b_ambi + opt->a - 1) / opt->a;
|
|
256
|
+
int32_t gapo = (opt->q + opt->a - 1) / opt->a;
|
|
257
|
+
int32_t gape = (opt->e + opt->a - 1) / opt->a;
|
|
258
|
+
int32_t sz, xtra;
|
|
259
|
+
void *qp;
|
|
260
|
+
ksw_llrst_t rst;
|
|
261
|
+
|
|
262
|
+
memset(h, 0, sizeof(*h));
|
|
263
|
+
if (max_sc >= 32767) return;
|
|
264
|
+
ksw_gen_nt4_mat(mat, 1, b_mm, b_ts, b_ambi, (int)mt);
|
|
265
|
+
sz = max_sc < 255 - b_mm? 1 : 2;
|
|
266
|
+
qp = ksw_ll_qinit(km, sz, qlen, qseq, 5, mat);
|
|
267
|
+
xtra = KSW_LL_SUBO | opt->min_len;
|
|
268
|
+
if (sz == 1)
|
|
269
|
+
rst = ksw_ll_u8_core(qp, tlen, tseq, gapo, gape, xtra);
|
|
270
|
+
else
|
|
271
|
+
rst = ksw_ll_i16_core(qp, tlen, tseq, gapo, gape, xtra);
|
|
272
|
+
if (kom_dbg_flag & MB_DBG_ALN_PE) {
|
|
273
|
+
int i;
|
|
274
|
+
fprintf(stderr, "===> qlen=%d; tlen=%d; score=%d; qe=%d; te=%d <===\n", qlen, tlen, rst.score, rst.qe + 1, rst.te + 1);
|
|
275
|
+
for (i = 0; i < qlen; ++i) fputc("ACGTN"[qseq[i]], stderr);
|
|
276
|
+
fputc('\n', stderr);
|
|
277
|
+
for (i = 0; i < tlen; ++i) fputc("ACGTN"[tseq[i]], stderr);
|
|
278
|
+
fputc('\n', stderr);
|
|
279
|
+
}
|
|
280
|
+
kfree(km, qp);
|
|
281
|
+
if (rst.score >= opt->min_dp_max && rst.score >= min_sc) { // min_sc is already divided by opt->a
|
|
282
|
+
int32_t te = rst.te + 1, qe = rst.qe + 1, ksw_flag = KSW_EZ_EXTZ_ONLY|KSW_EZ_RIGHT|KSW_EZ_REV_CIGAR;
|
|
283
|
+
mb_seq_rev(qe, qseq);
|
|
284
|
+
mb_seq_rev(te, tseq);
|
|
285
|
+
ksw_gen_nt4_mat(mat, opt->a, opt->b, opt->b_ts, opt->b_ambi, (int)mt);
|
|
286
|
+
if (mt != L2B_METH_NONE) ksw_flag |= KSW_EZ_GENERIC_SC;
|
|
287
|
+
ksw_extz2_sse(km, qe, qseq, te, tseq, 5, mat, opt->q, opt->e, opt->bw, opt->zdrop, opt->end_bonus, ksw_flag, ez);
|
|
288
|
+
mb_seq_rev(qe, qseq);
|
|
289
|
+
mb_seq_rev(te, tseq);
|
|
290
|
+
if (ez->n_cigar > 0 && ez->max >= opt->min_dp_max * opt->a) {
|
|
291
|
+
mb_append_cigar(h, ez->n_cigar, ez->cigar);
|
|
292
|
+
h->rescued = 1;
|
|
293
|
+
h->qe = qe, h->te = te;
|
|
294
|
+
h->ts = te - (ez->reach_end? ez->mqe_t + 1 : ez->max_t + 1);
|
|
295
|
+
h->qs = qe - (ez->reach_end? qe : ez->max_q + 1);
|
|
296
|
+
h->p->dp_max = h->p->dp_score = ez->max;
|
|
297
|
+
h->p->dp_max2 = (int32_t)((double)ez->max / rst.score * rst.score2 + .499);
|
|
298
|
+
if (h->p->dp_max2 < 0) h->p->dp_max2 = 0;
|
|
299
|
+
h->score = h->score0 = rst.score;
|
|
300
|
+
h->subsc = rst.score2;
|
|
301
|
+
h->cnt = 0, h->as = -1;
|
|
302
|
+
h->parent = MB_PARENT_UNSET;
|
|
303
|
+
if (kom_dbg_flag & MB_DBG_ALN_PE) {
|
|
304
|
+
int i;
|
|
305
|
+
fprintf(stderr, "max=%d; ts=%ld; qs=%d; reach_end=%d; cigar=", ez->max, (long)h->ts, h->qs, ez->reach_end);
|
|
306
|
+
for (i = 0; i < ez->n_cigar; ++i) fprintf(stderr, "%d%c", ez->cigar[i]>>4, MB_CIGAR_STR[ez->cigar[i]&0xf]);
|
|
307
|
+
fputc('\n', stderr);
|
|
308
|
+
}
|
|
309
|
+
mb_update_extra(km, h, &qseq[h->qs], &tseq[h->ts], mat, opt->q, opt->e, opt->flag, 0);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
typedef struct {
|
|
315
|
+
int32_t n, m;
|
|
316
|
+
mb_hit_t *a;
|
|
317
|
+
} mb_hit_v;
|
|
318
|
+
|
|
319
|
+
static const mb_hit_t *mb_matesw_core(void *km, const mb_opt_t *opt, const l2b_t *l2b, const mb_pestat_t pes[4], const mb_hit_t *h0, int32_t r0,
|
|
320
|
+
int32_t len, uint8_t *seq[2], l2b_meth_t mt0, mb_hit_v *h1, int32_t min_sc, ksw_extz_t *ez)
|
|
321
|
+
{
|
|
322
|
+
int32_t dir, skip[4];
|
|
323
|
+
int64_t pos5;
|
|
324
|
+
const mb_hit_t *ret = 0;
|
|
325
|
+
// find permitted orientation
|
|
326
|
+
for (dir = 0; dir < 4; ++dir) skip[dir] = !!pes[dir].failed;
|
|
327
|
+
if (skip[0] + skip[1] + skip[2] + skip[3] == 4) return 0; // no need to perform SW
|
|
328
|
+
// perform SW
|
|
329
|
+
pos5 = h0->rev? h0->te : h0->ts;
|
|
330
|
+
for (dir = 0; dir < 4; ++dir) {
|
|
331
|
+
int is_rev, is_larger;
|
|
332
|
+
int64_t ts, te;
|
|
333
|
+
if (skip[dir]) continue;
|
|
334
|
+
is_rev = (dir>>1 != (dir&1)) ^ h0->rev; // whether to reverse complement the mate
|
|
335
|
+
if (dir>>1 != (dir&1)) is_larger = dir>>1 ^ is_rev; // whether the mate has larger coordinate (FR or RF)
|
|
336
|
+
else is_larger = dir>>1 ^ r0 ^ h0->rev; // FF or RR
|
|
337
|
+
ts = (is_larger? pos5 + pes[dir].lo : pos5 - pes[dir].hi) - (!is_rev? 0 : len);
|
|
338
|
+
te = (is_larger? pos5 + pes[dir].hi : pos5 - pes[dir].lo) + (!is_rev? len : 0);
|
|
339
|
+
if (ts < 0) ts = 0;
|
|
340
|
+
if (te > l2b->ctg[h0->tid].len) te = l2b->ctg[h0->tid].len;
|
|
341
|
+
if (te - ts > len) {
|
|
342
|
+
int64_t ts2 = ts, te2 = te;
|
|
343
|
+
int32_t max_ug, max_i, n_good, n_kmer;
|
|
344
|
+
uint8_t *ref;
|
|
345
|
+
l2b_meth_t mt = mt0;
|
|
346
|
+
mb_hit_t ht;
|
|
347
|
+
ht.p = 0;
|
|
348
|
+
ref = Kmalloc(km, uint8_t, te - ts);
|
|
349
|
+
if (is_rev) mt = l2b_meth_rev(mt0);
|
|
350
|
+
l2b_getseq(l2b, h0->tid, ts, te, ref);
|
|
351
|
+
max_ug = mb_ungap(km, len, seq[is_rev], te - ts, ref, 7, mt, &max_i, &n_good, &n_kmer);
|
|
352
|
+
if (max_ug >= 10 && max_ug >= len>>1 && n_good == 1) {
|
|
353
|
+
ts2 = ts + max_i - len / 2;
|
|
354
|
+
te2 = ts2 + len * 2;
|
|
355
|
+
if (ts2 < ts) ts2 = ts;
|
|
356
|
+
if (te2 > te) te2 = te;
|
|
357
|
+
}
|
|
358
|
+
if (max_ug >= 10 || max_ug >= n_kmer * 0.33)
|
|
359
|
+
mb_matesw_align(km, opt, len, seq[is_rev], te2 - ts2, &ref[ts2 - ts], &ht, min_sc, mt, ez);
|
|
360
|
+
if (ht.p) { // a good hit found
|
|
361
|
+
ht.tid = h0->tid;
|
|
362
|
+
ht.ts += ts2, ht.te += ts2;
|
|
363
|
+
ht.rev = is_rev;
|
|
364
|
+
if (is_rev) {
|
|
365
|
+
int32_t qt = ht.qs;
|
|
366
|
+
ht.qs = len - ht.qe;
|
|
367
|
+
ht.qe = len - qt;
|
|
368
|
+
}
|
|
369
|
+
//fprintf(stderr, "X\t%s\tsc0=%d\tnew_sc=%d\tnew_ts=%ld\tn_cigar=%d\tcigar[0]=%d\n", l2b->ctg[ht.tid].name, h0->p->dp_max, ht.p->dp_max, (long)ht.ts, ht.p->n_cigar, ht.p->cigar[0]>>4);
|
|
370
|
+
if (h1->n == h1->m) kom_grow(mb_hit_t, h1->a, h1->n, h1->m);
|
|
371
|
+
h1->a[h1->n++] = ht;
|
|
372
|
+
ret = &h1->a[h1->n - 1];
|
|
373
|
+
}
|
|
374
|
+
kfree(km, ref);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return ret;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
static int32_t mb_matesw(void *km, const mb_opt_t *opt, const l2b_t *l2b, int32_t n_hit[2], mb_hit_t *hit[2], const mb_pestat_t pes[4], const mb_pairaux_t *paux0,
|
|
381
|
+
int32_t qlen[2], char *const qseq[2], int32_t is_meth)
|
|
382
|
+
{
|
|
383
|
+
int32_t i, r, n_add, n_res, max[2], max2[2], skip[2], min_sc[2];
|
|
384
|
+
mb_hit_v ha[2];
|
|
385
|
+
ksw_extz_t ez;
|
|
386
|
+
mb128_t *a;
|
|
387
|
+
uint8_t *qs[2][2];
|
|
388
|
+
if (opt->max_rescue == 0) return 0;
|
|
389
|
+
// precalculate the number of rescue candidates
|
|
390
|
+
for (r = 0, n_res = 0; r < 2; ++r) {
|
|
391
|
+
const mb_hit_t *h0 = hit[r];
|
|
392
|
+
int32_t m, n = n_hit[r];
|
|
393
|
+
for (i = 0, m = 0; i < n && m < opt->max_rescue; ++i)
|
|
394
|
+
if (h0[i].proper_pair == 0 && h0[i].p->dp_max >= h0[0].p->dp_max - opt->pen_unpair * opt->a)
|
|
395
|
+
++m;
|
|
396
|
+
n_res += m;
|
|
397
|
+
}
|
|
398
|
+
if (n_res == 0) return 0;
|
|
399
|
+
// collect rescue candidates; MUST match the loop above
|
|
400
|
+
a = Kcalloc(km, mb128_t, n_res);
|
|
401
|
+
for (r = 0, n_res = 0; r < 2; ++r) {
|
|
402
|
+
const mb_hit_t *h0 = hit[r];
|
|
403
|
+
int32_t m, n = n_hit[r];
|
|
404
|
+
for (i = 0, m = 0; i < n && m < opt->max_rescue; ++i) {
|
|
405
|
+
if (h0[i].proper_pair == 0 && h0[i].p->dp_max >= h0[0].p->dp_max - opt->pen_unpair * opt->a) {
|
|
406
|
+
mb128_t *p = &a[n_res++];
|
|
407
|
+
p->x = (uint64_t)h0[i].p->dp_max << 32 | h0[i].hash;
|
|
408
|
+
p->y = i << 1 | r;
|
|
409
|
+
++m;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
radix_sort_mb128x(a, a + n_res);
|
|
414
|
+
|
|
415
|
+
// prepare sequences for alignment
|
|
416
|
+
qs[0][0] = Kcalloc(km, uint8_t, (qlen[0] + qlen[1]) * 2);
|
|
417
|
+
qs[0][1] = qs[0][0] + qlen[0];
|
|
418
|
+
qs[1][0] = qs[0][1] + qlen[0];
|
|
419
|
+
qs[1][1] = qs[1][0] + qlen[1];
|
|
420
|
+
for (r = 0; r < 2; ++r) {
|
|
421
|
+
ha[r].n = ha[r].m = n_hit[r];
|
|
422
|
+
ha[r].a = hit[r];
|
|
423
|
+
for (i = 0; i < qlen[r]; ++i) {
|
|
424
|
+
int32_t c = kom_nt4_table[(uint8_t)qseq[r][i]];
|
|
425
|
+
qs[r][0][i] = c;
|
|
426
|
+
qs[r][1][qlen[r] - 1 - i] = c < 4? 3 - c : 4;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
min_sc[0] = mb_hit_sum_score(km, n_hit[0], hit[0]) / opt->a - opt->pen_unpair;
|
|
430
|
+
min_sc[1] = mb_hit_sum_score(km, n_hit[1], hit[1]) / opt->a - opt->pen_unpair;
|
|
431
|
+
|
|
432
|
+
// do alignment
|
|
433
|
+
max[0] = paux0->score, max2[0] = paux0->sub_sc, skip[0] = 0;
|
|
434
|
+
max[1] = paux0->score, max2[1] = paux0->sub_sc, skip[1] = 0;
|
|
435
|
+
memset(&ez, 0, sizeof(ez));
|
|
436
|
+
ez.m_cigar = 16;
|
|
437
|
+
ez.cigar = Kmalloc(km, uint32_t, ez.m_cigar);
|
|
438
|
+
for (i = n_res - 1; i >= 0; --i) {
|
|
439
|
+
int32_t sc, r = a[i].y&1, j = a[i].y>>1;
|
|
440
|
+
const mb_hit_t *h0 = &ha[r].a[j], *h1;
|
|
441
|
+
l2b_meth_t mt = !is_meth? L2B_METH_NONE : r == 0? L2B_METH_G2A : L2B_METH_C2T; // rescuing mate: r=0 rescues R2(G2A), r=1 rescues R1(C2T)
|
|
442
|
+
if (skip[r]) continue;
|
|
443
|
+
h1 = mb_matesw_core(km, opt, l2b, pes, h0, r, qlen[!r], qs[!r], mt, &ha[!r], min_sc[!r], &ez);
|
|
444
|
+
if (h1) { // rescue successful
|
|
445
|
+
sc = mb_pair_score(h0, h1, pes, opt->a);
|
|
446
|
+
if (sc > max[r]) max2[r] = max[r], max[r] = sc;
|
|
447
|
+
else if (sc > max2[r]) max2[r] = sc;
|
|
448
|
+
if (max[r] == max2[r]) skip[r] = 1;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
kfree(km, ez.cigar);
|
|
453
|
+
kfree(km, qs[0][0]);
|
|
454
|
+
kfree(km, a);
|
|
455
|
+
n_add = (ha[0].n - n_hit[0]) + (ha[1].n - n_hit[1]);
|
|
456
|
+
for (r = 0; r < 2; ++r)
|
|
457
|
+
n_hit[r] = ha[r].n, hit[r] = ha[r].a;
|
|
458
|
+
return n_add;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
void mb_pair(void *km, const mb_opt_t *opt, const l2b_t *l2b, int32_t n_hit[2], mb_hit_t *hit[2], const mb_pestat_t pes[4], int32_t qlen[2], char *const qseq[2])
|
|
462
|
+
{
|
|
463
|
+
const int32_t pe_bonus = 4;
|
|
464
|
+
int32_t r, i, dp_max_se[2], score_se, dp_max_se2[2], score_se2, do_matesw, is_meth = !!(opt->flag & MB_F_METH);
|
|
465
|
+
mb_pairaux_t paux;
|
|
466
|
+
int32_t seed_ratio[2], min_seed_ratio;
|
|
467
|
+
|
|
468
|
+
if (n_hit[0] == 0 && n_hit[1] == 0) return;
|
|
469
|
+
seed_ratio[0] = n_hit[0] > 0? hit[0][0].seed_ratio : 255;
|
|
470
|
+
seed_ratio[1] = n_hit[1] > 0? hit[1][0].seed_ratio : 255;
|
|
471
|
+
min_seed_ratio = seed_ratio[0] < seed_ratio[1]? seed_ratio[0] : seed_ratio[1];
|
|
472
|
+
mb_pair_hits(km, opt, l2b, n_hit, hit, pes, &paux);
|
|
473
|
+
do_matesw = paux.n_pp > 0 && paux.score == paux.sub_sc? 0 : 1; // skip mate rescue if we see two equally best pairs
|
|
474
|
+
if (do_matesw && opt->max_rescue > 0) {
|
|
475
|
+
int32_t sub_diff = opt->a + opt->b > opt->q + opt->e? opt->a + opt->b : opt->q + opt->e;
|
|
476
|
+
if (mb_matesw(km, opt, l2b, n_hit, hit, pes, &paux, qlen, qseq, is_meth) > 0) {
|
|
477
|
+
for (r = 0; r < 2; ++r) {
|
|
478
|
+
for (i = 0; i < n_hit[r]; ++i) {
|
|
479
|
+
mb_hit_t *h = &hit[r][i];
|
|
480
|
+
if (!h->rescued)
|
|
481
|
+
h->n_sub = h->subsc = h->p->dp_max2 = 0;
|
|
482
|
+
}
|
|
483
|
+
mb_hit_sort(km, &n_hit[r], hit[r]);
|
|
484
|
+
mb_set_parent(km, opt->mask_level, opt->mask_len, n_hit[r], hit[r], sub_diff, 0);
|
|
485
|
+
mb_set_mapq(km, qlen[r], n_hit[r], hit[r], opt->min_chain_score, opt->a, mb_is_sr_mode(opt, qlen[r]), opt->max_sr_len);
|
|
486
|
+
}
|
|
487
|
+
mb_pair_hits(km, opt, l2b, n_hit, hit, pes, &paux); // pair again if new hits rescued
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (paux.n_pp == 0) goto end_pairing;
|
|
491
|
+
|
|
492
|
+
for (r = 0; r < 2; ++r) {
|
|
493
|
+
for (dp_max_se[r] = dp_max_se2[r] = 0, i = 0; i < n_hit[r]; ++i) {
|
|
494
|
+
if (dp_max_se[r] < hit[r][i].p->dp_max)
|
|
495
|
+
dp_max_se2[r] = dp_max_se[r], dp_max_se[r] = hit[r][i].p->dp_max;
|
|
496
|
+
else if (dp_max_se2[r] < hit[r][i].p->dp_max)
|
|
497
|
+
dp_max_se2[r] = hit[r][i].p->dp_max;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
score_se = dp_max_se[0] + dp_max_se[1];
|
|
501
|
+
score_se2 = dp_max_se2[0] + dp_max_se2[1];
|
|
502
|
+
|
|
503
|
+
mb_sync_high_cov(n_hit[0], hit[0]);
|
|
504
|
+
mb_sync_high_cov(n_hit[1], hit[1]);
|
|
505
|
+
if (paux.score >= score_se - opt->pen_unpair * opt->a) { // choose the paired hits
|
|
506
|
+
int32_t mapq_pe, score2 = paux.sub_sc, diff;
|
|
507
|
+
double identity;
|
|
508
|
+
mb_hit_t *h[2];
|
|
509
|
+
h[0] = &hit[0][paux.i[0]];
|
|
510
|
+
h[1] = &hit[1][paux.i[1]];
|
|
511
|
+
assert(n_hit[0] > 0 && n_hit[1] > 0);
|
|
512
|
+
identity = (double)(h[0]->mlen + h[1]->mlen) / (h[0]->blen + h[1]->blen);
|
|
513
|
+
if ((h[0]->id != h[0]->parent || h[1]->id != h[1]->parent) && score2 < score_se - opt->pen_unpair * opt->a)
|
|
514
|
+
score2 = score_se - opt->pen_unpair * opt->a;
|
|
515
|
+
diff = paux.score - score2;
|
|
516
|
+
if (diff > paux.score + pe_bonus * opt->a - score_se2) // inspired by dragmap
|
|
517
|
+
diff = paux.score + pe_bonus * opt->a - score_se2;
|
|
518
|
+
mapq_pe = (int)(6.02 * identity * identity * diff / opt->a - 4.343 * log(paux.n_sub + 1) + .499);
|
|
519
|
+
if (mapq_pe < 0) mapq_pe = 0;
|
|
520
|
+
mapq_pe = (int)(mapq_pe * (1. - .5 * (h[0]->frac_high / 255. + h[1]->frac_high / 255.)) + .499);
|
|
521
|
+
if (min_seed_ratio < 50) mapq_pe *= (double)min_seed_ratio * min_seed_ratio / 2500.0;
|
|
522
|
+
if (mapq_pe > 60) mapq_pe = 60;
|
|
523
|
+
if (mapq_pe <= 0 && paux.score > score2) mapq_pe = 1;
|
|
524
|
+
for (r = 0; r < 2; ++r) {
|
|
525
|
+
if (n_hit[r] == 1) // if this read only has one hit, take max(mapq_se,mapq_pe)
|
|
526
|
+
h[r]->mapq = h[r]->mapq > mapq_pe? h[r]->mapq : mapq_pe;
|
|
527
|
+
else // if this read has multiple hits, cap mapq and penalize if mapq_se is small
|
|
528
|
+
h[r]->mapq = h[r]->mapq > mapq_pe? mapq_pe : (int32_t)(.2 * h[r]->mapq + .8 * mapq_pe + .499);
|
|
529
|
+
if (h[r]->id != h[r]->parent) { // then lift the paired hit to primary and update parent
|
|
530
|
+
mb_hit_t *p = &hit[r][h[r]->parent];
|
|
531
|
+
for (i = 0; i < n_hit[r]; ++i)
|
|
532
|
+
if (hit[r][i].parent == p->id) // h[r]->parent is always set to h[r]->id
|
|
533
|
+
hit[r][i].parent = h[r]->id;
|
|
534
|
+
p->mapq = 0;
|
|
535
|
+
}
|
|
536
|
+
for (i = 0; i < n_hit[r]; ++i) { // handle other chimeric hits
|
|
537
|
+
mb_hit_t *p = &hit[r][i], *q = h[r];
|
|
538
|
+
if (q != p && p->id == p->parent) { // p is a chimeric hit that is not h[r]
|
|
539
|
+
int32_t j, ol = p->qe <= q->qs || p->qs >= q->qe? 0 : (p->qe < q->qe? p->qe : q->qe) - (p->qs > q->qs? p->qs : q->qs);
|
|
540
|
+
if (ol > opt->mask_level * (p->qe - p->qs)) { // if p overlaps with h[r] a lot, make it a secondary hit
|
|
541
|
+
for (j = 0; j < n_hit[r]; ++j) // FIXME: quadratic time complexity, but almost never an issue on real data
|
|
542
|
+
if (hit[r][j].parent == p->id)
|
|
543
|
+
hit[r][j].parent = q->id;
|
|
544
|
+
p->mapq = 0;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
} else { // choose the unpaired hits
|
|
550
|
+
int32_t diff = score_se - opt->pen_unpair * opt->a - paux.score;
|
|
551
|
+
int32_t mapq_pe = 6 * diff / opt->a;
|
|
552
|
+
for (r = 0; r < 2; ++r)
|
|
553
|
+
for (i = 0; i < n_hit[r]; ++i)
|
|
554
|
+
hit[r][i].mapq = hit[r][i].mapq < mapq_pe? hit[r][i].mapq : mapq_pe;
|
|
555
|
+
}
|
|
556
|
+
end_pairing:
|
|
557
|
+
mb_set_sam_pri(n_hit[0], hit[0], !!(opt->flag & MB_F_PRIMARY5));
|
|
558
|
+
mb_set_sam_pri(n_hit[1], hit[1], !!(opt->flag & MB_F_PRIMARY5));
|
|
559
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#ifndef S2N_LITE_H
|
|
2
|
+
#define S2N_LITE_H
|
|
3
|
+
|
|
4
|
+
#include <arm_neon.h>
|
|
5
|
+
|
|
6
|
+
typedef uint8x16_t __m128i;
|
|
7
|
+
|
|
8
|
+
static inline __m128i _mm_load_si128(const __m128i *ptr) { return vld1q_u8((const uint8_t*)ptr); }
|
|
9
|
+
static inline __m128i _mm_loadu_si128(const __m128i *ptr) { return vld1q_u8((const uint8_t*)ptr); }
|
|
10
|
+
static inline void _mm_store_si128(__m128i *ptr, __m128i a) { vst1q_u8((uint8_t*)ptr, a); }
|
|
11
|
+
static inline void _mm_storeu_si128(__m128i *ptr, __m128i a) { vst1q_u8((uint8_t*)ptr, a); }
|
|
12
|
+
static inline __m128i _mm_setzero_si128(void) { return vdupq_n_u8(0); }
|
|
13
|
+
static inline __m128i _mm_or_si128(__m128i a, __m128i b) { return vorrq_u8(a, b); }
|
|
14
|
+
static inline __m128i _mm_and_si128(__m128i a, __m128i b) { return vandq_u8(a, b); }
|
|
15
|
+
static inline __m128i _mm_andnot_si128(__m128i a, __m128i b) { return vbicq_u8(b, a); }
|
|
16
|
+
|
|
17
|
+
#define _mm_slli_si128(a, imm8) vextq_u8(_mm_setzero_si128(), (a), 16 - (imm8))
|
|
18
|
+
#define _mm_srli_si128(a, imm8) vextq_u8((a), _mm_setzero_si128(), (imm8))
|
|
19
|
+
|
|
20
|
+
static inline __m128i _mm_blendv_epi8(__m128i a, __m128i b, __m128i mask) { return vbslq_u8(vreinterpretq_u8_s8(vshrq_n_s8(vreinterpretq_s8_u8(mask), 7)), b, a); }
|
|
21
|
+
|
|
22
|
+
static inline __m128i _mm_set1_epi8(int a) { return vdupq_n_u8(a); }
|
|
23
|
+
static inline __m128i _mm_add_epi8(__m128i a, __m128i b) { return vaddq_u8(a, b); }
|
|
24
|
+
static inline __m128i _mm_adds_epu8(__m128i a, __m128i b) { return vqaddq_u8(a, b); }
|
|
25
|
+
static inline __m128i _mm_sub_epi8(__m128i a, __m128i b) { return vsubq_u8(a, b); }
|
|
26
|
+
static inline __m128i _mm_subs_epu8(__m128i a, __m128i b) { return vqsubq_u8(a, b); }
|
|
27
|
+
static inline __m128i _mm_cmpeq_epi8(__m128i a, __m128i b) { return vceqq_u8(a, b); }
|
|
28
|
+
static inline __m128i _mm_cmpgt_epi8(__m128i a, __m128i b) { return vcgtq_s8(vreinterpretq_s8_u8(a), vreinterpretq_s8_u8(b)); }
|
|
29
|
+
static inline __m128i _mm_max_epi8(__m128i a, __m128i b) { return vreinterpretq_u8_s8(vmaxq_s8(vreinterpretq_s8_u8(a), vreinterpretq_s8_u8(b))); }
|
|
30
|
+
static inline __m128i _mm_min_epi8(__m128i a, __m128i b) { return vreinterpretq_u8_s8(vminq_s8(vreinterpretq_s8_u8(a), vreinterpretq_s8_u8(b))); }
|
|
31
|
+
static inline __m128i _mm_max_epu8(__m128i a, __m128i b) { return vmaxq_u8(a, b); }
|
|
32
|
+
static inline __m128i _mm_min_epu8(__m128i a, __m128i b) { return vminq_u8(a, b); }
|
|
33
|
+
|
|
34
|
+
static inline __m128i _mm_set1_epi16(int a) { return vreinterpretq_u8_s16(vdupq_n_s16(a)); }
|
|
35
|
+
static inline __m128i _mm_cmpgt_epi16(__m128i a, __m128i b) { return vreinterpretq_u8_u16(vcgtq_s16(vreinterpretq_s16_u8(a), vreinterpretq_s16_u8(b))); }
|
|
36
|
+
static inline __m128i _mm_max_epi16(__m128i a, __m128i b) { return vreinterpretq_u8_s16(vmaxq_s16(vreinterpretq_s16_u8(a), vreinterpretq_s16_u8(b))); }
|
|
37
|
+
static inline __m128i _mm_adds_epi16(__m128i a, __m128i b) { return vreinterpretq_u8_s16(vqaddq_s16(vreinterpretq_s16_u8(a), vreinterpretq_s16_u8(b))); }
|
|
38
|
+
static inline __m128i _mm_subs_epi16(__m128i a, __m128i b) { return vreinterpretq_u8_s16(vqsubq_s16(vreinterpretq_s16_u8(a), vreinterpretq_s16_u8(b))); }
|
|
39
|
+
static inline __m128i _mm_subs_epu16(__m128i a, __m128i b) { return vreinterpretq_u8_u16(vqsubq_u16(vreinterpretq_u16_u8(a), vreinterpretq_u16_u8(b))); }
|
|
40
|
+
|
|
41
|
+
#define _mm_extract_epi16(a, imm8) vgetq_lane_s16(vreinterpretq_s16_u8(a), (imm8))
|
|
42
|
+
#define _mm_insert_epi16(a, b, imm8) vreinterpretq_u8_s16(vsetq_lane_s16((b), vreinterpretq_s16_u8(a), (imm8)))
|
|
43
|
+
|
|
44
|
+
static inline __m128i _mm_set1_epi32(int a) { return vreinterpretq_u8_s32(vdupq_n_s32(a)); }
|
|
45
|
+
static inline __m128i _mm_cvtsi32_si128(int a) { return vreinterpretq_u8_s32(vsetq_lane_s32(a, vdupq_n_s32(0), 0)); }
|
|
46
|
+
static inline __m128i _mm_setr_epi32(int a, int b, int c, int d) {
|
|
47
|
+
int32_t x[4] = {a, b, c, d};
|
|
48
|
+
return vld1q_u8((const uint8_t*)x);
|
|
49
|
+
}
|
|
50
|
+
static inline __m128i _mm_cmpgt_epi32(__m128i a, __m128i b) { return vreinterpretq_u8_u32(vcgtq_s32(vreinterpretq_s32_u8(a), vreinterpretq_s32_u8(b))); }
|
|
51
|
+
static inline __m128i _mm_max_epi32(__m128i a, __m128i b) { return vreinterpretq_u8_s32(vmaxq_s32(vreinterpretq_s32_u8(a), vreinterpretq_s32_u8(b))); }
|
|
52
|
+
static inline __m128i _mm_add_epi32(__m128i a, __m128i b) { return vreinterpretq_u8_s32(vaddq_s32(vreinterpretq_s32_u8(a), vreinterpretq_s32_u8(b))); }
|
|
53
|
+
static inline __m128i _mm_sub_epi32(__m128i a, __m128i b) { return vreinterpretq_u8_s32(vsubq_s32(vreinterpretq_s32_u8(a), vreinterpretq_s32_u8(b))); }
|
|
54
|
+
|
|
55
|
+
#define _mm_insert_epi32(a, b, imm8) vreinterpretq_u8_s32(vsetq_lane_s32((b), vreinterpretq_s32_u8(a), (imm8)))
|
|
56
|
+
|
|
57
|
+
#define _mm_prefetch(p, i) __builtin_prefetch(p, 0, (i))
|
|
58
|
+
|
|
59
|
+
#endif
|