ksw.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /* The MIT License
  2. Copyright (c) 2011 by Attractive Chaos <attractor@live.co.uk>
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  16. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  17. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <emmintrin.h>
  24. #include "ksw.h"
  25. #ifdef __GNUC__
  26. #define LIKELY(x) __builtin_expect((x),1)
  27. #define UNLIKELY(x) __builtin_expect((x),0)
  28. #else
  29. #define LIKELY(x) (x)
  30. #define UNLIKELY(x) (x)
  31. #endif
  32. const kswr_t g_defr = { 0, -1, -1, -1, -1, -1, -1 };
  33. struct _kswq_t {
  34. int qlen, slen;
  35. uint8_t shift, mdiff, max, size;
  36. __m128i *qp, *H0, *H1, *E, *Hmax;
  37. };
  38. /**
  39. * Initialize the query data structure
  40. *
  41. * @param size Number of bytes used to store a score; valid valures are 1 or 2
  42. * @param qlen Length of the query sequence
  43. * @param query Query sequence
  44. * @param m Size of the alphabet
  45. * @param mat Scoring matrix in a one-dimension array
  46. *
  47. * @return Query data structure
  48. */
  49. kswq_t *ksw_qinit(int size, int qlen, const uint8_t *query, int m, const int8_t *mat)
  50. {
  51. kswq_t *q;
  52. int slen, a, tmp, p;
  53. size = size > 1? 2 : 1;
  54. p = 8 * (3 - size); // # values per __m128i
  55. slen = (qlen + p - 1) / p; // segmented length
  56. q = (kswq_t*)malloc(sizeof(kswq_t) + 256 + 16 * slen * (m + 4)); // a single block of memory
  57. q->qp = (__m128i*)(((size_t)q + sizeof(kswq_t) + 15) >> 4 << 4); // align memory
  58. q->H0 = q->qp + slen * m;
  59. q->H1 = q->H0 + slen;
  60. q->E = q->H1 + slen;
  61. q->Hmax = q->E + slen;
  62. q->slen = slen; q->qlen = qlen; q->size = size;
  63. // compute shift
  64. tmp = m * m;
  65. for (a = 0, q->shift = 127, q->mdiff = 0; a < tmp; ++a) { // find the minimum and maximum score
  66. if (mat[a] < (int8_t)q->shift) q->shift = mat[a];
  67. if (mat[a] > (int8_t)q->mdiff) q->mdiff = mat[a];
  68. }
  69. q->max = q->mdiff;
  70. q->shift = 256 - q->shift; // NB: q->shift is uint8_t
  71. q->mdiff += q->shift; // this is the difference between the min and max scores
  72. // An example: p=8, qlen=19, slen=3 and segmentation:
  73. // {{0,3,6,9,12,15,18,-1},{1,4,7,10,13,16,-1,-1},{2,5,8,11,14,17,-1,-1}}
  74. if (size == 1) {
  75. int8_t *t = (int8_t*)q->qp;
  76. for (a = 0; a < m; ++a) {
  77. int i, k, nlen = slen * p;
  78. const int8_t *ma = mat + a * m;
  79. for (i = 0; i < slen; ++i)
  80. for (k = i; k < nlen; k += slen) // p iterations
  81. *t++ = (k >= qlen? 0 : ma[query[k]]) + q->shift;
  82. }
  83. } else {
  84. int16_t *t = (int16_t*)q->qp;
  85. for (a = 0; a < m; ++a) {
  86. int i, k, nlen = slen * p;
  87. const int8_t *ma = mat + a * m;
  88. for (i = 0; i < slen; ++i)
  89. for (k = i; k < nlen; k += slen) // p iterations
  90. *t++ = (k >= qlen? 0 : ma[query[k]]);
  91. }
  92. }
  93. return q;
  94. }
  95. kswr_t ksw_u8(kswq_t *q, int tlen, const uint8_t *target, int _gapo, int _gape, int xtra) // the first gap costs -(_o+_e)
  96. {
  97. int slen, i, m_b, n_b, te = -1, gmax = 0, minsc, endsc;
  98. uint64_t *b;
  99. __m128i zero, gapoe, gape, shift, *H0, *H1, *E, *Hmax;
  100. kswr_t r;
  101. #define __max_16(ret, xx) do { \
  102. (xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 8)); \
  103. (xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 4)); \
  104. (xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 2)); \
  105. (xx) = _mm_max_epu8((xx), _mm_srli_si128((xx), 1)); \
  106. (ret) = _mm_extract_epi16((xx), 0) & 0x00ff; \
  107. } while (0)
  108. // initialization
  109. r = g_defr;
  110. minsc = (xtra&KSW_XSUBO)? xtra&0xffff : 0x10000;
  111. endsc = (xtra&KSW_XSTOP)? xtra&0xffff : 0x10000;
  112. m_b = n_b = 0; b = 0;
  113. zero = _mm_set1_epi32(0);
  114. gapoe = _mm_set1_epi8(_gapo + _gape);
  115. gape = _mm_set1_epi8(_gape);
  116. shift = _mm_set1_epi8(q->shift);
  117. H0 = q->H0; H1 = q->H1; E = q->E; Hmax = q->Hmax;
  118. slen = q->slen;
  119. for (i = 0; i < slen; ++i) {
  120. _mm_store_si128(E + i, zero);
  121. _mm_store_si128(H0 + i, zero);
  122. _mm_store_si128(Hmax + i, zero);
  123. }
  124. // the core loop
  125. for (i = 0; i < tlen; ++i) {
  126. int j, k, cmp, imax;
  127. __m128i e, h, f = zero, max = zero, *S = q->qp + target[i] * slen; // s is the 1st score vector
  128. h = _mm_load_si128(H0 + slen - 1); // h={2,5,8,11,14,17,-1,-1} in the above example
  129. h = _mm_slli_si128(h, 1); // h=H(i-1,-1); << instead of >> because x64 is little-endian
  130. for (j = 0; LIKELY(j < slen); ++j) {
  131. /* SW cells are computed in the following order:
  132. * H(i,j) = max{H(i-1,j-1)+S(i,j), E(i,j), F(i,j)}
  133. * E(i+1,j) = max{H(i,j)-q, E(i,j)-r}
  134. * F(i,j+1) = max{H(i,j)-q, F(i,j)-r}
  135. */
  136. // compute H'(i,j); note that at the beginning, h=H'(i-1,j-1)
  137. h = _mm_adds_epu8(h, _mm_load_si128(S + j));
  138. h = _mm_subs_epu8(h, shift); // h=H'(i-1,j-1)+S(i,j)
  139. e = _mm_load_si128(E + j); // e=E'(i,j)
  140. h = _mm_max_epu8(h, e);
  141. h = _mm_max_epu8(h, f); // h=H'(i,j)
  142. max = _mm_max_epu8(max, h); // set max
  143. _mm_store_si128(H1 + j, h); // save to H'(i,j)
  144. // now compute E'(i+1,j)
  145. h = _mm_subs_epu8(h, gapoe); // h=H'(i,j)-gapo
  146. e = _mm_subs_epu8(e, gape); // e=E'(i,j)-gape
  147. e = _mm_max_epu8(e, h); // e=E'(i+1,j)
  148. _mm_store_si128(E + j, e); // save to E'(i+1,j)
  149. // now compute F'(i,j+1)
  150. f = _mm_subs_epu8(f, gape);
  151. f = _mm_max_epu8(f, h);
  152. // get H'(i-1,j) and prepare for the next j
  153. h = _mm_load_si128(H0 + j); // h=H'(i-1,j)
  154. }
  155. // NB: we do not need to set E(i,j) as we disallow adjecent insertion and then deletion
  156. for (k = 0; LIKELY(k < 16); ++k) { // this block mimics SWPS3; NB: H(i,j) updated in the lazy-F loop cannot exceed max
  157. f = _mm_slli_si128(f, 1);
  158. for (j = 0; LIKELY(j < slen); ++j) {
  159. h = _mm_load_si128(H1 + j);
  160. h = _mm_max_epu8(h, f); // h=H'(i,j)
  161. _mm_store_si128(H1 + j, h);
  162. h = _mm_subs_epu8(h, gapoe);
  163. f = _mm_subs_epu8(f, gape);
  164. cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(_mm_subs_epu8(f, h), zero));
  165. if (UNLIKELY(cmp == 0xffff)) goto end_loop16;
  166. }
  167. }
  168. end_loop16:
  169. //int k;for (k=0;k<16;++k)printf("%d ", ((uint8_t*)&max)[k]);printf("\n");
  170. __max_16(imax, max); // imax is the maximum number in max
  171. if (imax >= minsc) { // write the b array; this condition adds branching unfornately
  172. if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) { // then append
  173. if (n_b == m_b) {
  174. m_b = m_b? m_b<<1 : 8;
  175. b = (uint64_t*)realloc(b, 8 * m_b);
  176. }
  177. b[n_b++] = (uint64_t)imax<<32 | i;
  178. } else if ((int)(b[n_b-1]>>32) < imax) b[n_b-1] = (uint64_t)imax<<32 | i; // modify the last
  179. }
  180. if (imax > gmax) {
  181. gmax = imax; te = i; // te is the end position on the target
  182. for (j = 0; LIKELY(j < slen); ++j) // keep the H1 vector
  183. _mm_store_si128(Hmax + j, _mm_load_si128(H1 + j));
  184. if (gmax + q->shift >= 255 || gmax >= endsc) break;
  185. }
  186. S = H1; H1 = H0; H0 = S; // swap H0 and H1
  187. }
  188. r.score = gmax + q->shift < 255? gmax : 255;
  189. r.te = te;
  190. if (r.score != 255) { // get a->qe, the end of query match; find the 2nd best score
  191. int max = -1, low, high, qlen = slen * 16;
  192. uint8_t *t = (uint8_t*)Hmax;
  193. for (i = 0; i < qlen; ++i, ++t)
  194. if ((int)*t > max) max = *t, r.qe = i / 16 + i % 16 * slen;
  195. //printf("%d,%d\n", max, gmax);
  196. if (b) {
  197. i = (r.score + q->max - 1) / q->max;
  198. low = te - i; high = te + i;
  199. for (i = 0; i < n_b; ++i) {
  200. int e = (int32_t)b[i];
  201. if ((e < low || e > high) && (int)(b[i]>>32) > r.score2)
  202. r.score2 = b[i]>>32, r.te2 = e;
  203. }
  204. }
  205. }
  206. free(b);
  207. return r;
  208. }
  209. kswr_t ksw_i16(kswq_t *q, int tlen, const uint8_t *target, int _gapo, int _gape, int xtra) // the first gap costs -(_o+_e)
  210. {
  211. int slen, i, m_b, n_b, te = -1, gmax = 0, minsc, endsc;
  212. uint64_t *b;
  213. __m128i zero, gapoe, gape, *H0, *H1, *E, *Hmax;
  214. kswr_t r;
  215. #define __max_8(ret, xx) do { \
  216. (xx) = _mm_max_epi16((xx), _mm_srli_si128((xx), 8)); \
  217. (xx) = _mm_max_epi16((xx), _mm_srli_si128((xx), 4)); \
  218. (xx) = _mm_max_epi16((xx), _mm_srli_si128((xx), 2)); \
  219. (ret) = _mm_extract_epi16((xx), 0); \
  220. } while (0)
  221. // initialization
  222. r = g_defr;
  223. minsc = (xtra&KSW_XSUBO)? xtra&0xffff : 0x10000;
  224. endsc = (xtra&KSW_XSTOP)? xtra&0xffff : 0x10000;
  225. m_b = n_b = 0; b = 0;
  226. zero = _mm_set1_epi32(0);
  227. gapoe = _mm_set1_epi16(_gapo + _gape);
  228. gape = _mm_set1_epi16(_gape);
  229. H0 = q->H0; H1 = q->H1; E = q->E; Hmax = q->Hmax;
  230. slen = q->slen;
  231. for (i = 0; i < slen; ++i) {
  232. _mm_store_si128(E + i, zero);
  233. _mm_store_si128(H0 + i, zero);
  234. _mm_store_si128(Hmax + i, zero);
  235. }
  236. // the core loop
  237. for (i = 0; i < tlen; ++i) {
  238. int j, k, imax;
  239. __m128i e, h, f = zero, max = zero, *S = q->qp + target[i] * slen; // s is the 1st score vector
  240. h = _mm_load_si128(H0 + slen - 1); // h={2,5,8,11,14,17,-1,-1} in the above example
  241. h = _mm_slli_si128(h, 2);
  242. for (j = 0; LIKELY(j < slen); ++j) {
  243. h = _mm_adds_epi16(h, *S++);
  244. e = _mm_load_si128(E + j);
  245. h = _mm_max_epi16(h, e);
  246. h = _mm_max_epi16(h, f);
  247. max = _mm_max_epi16(max, h);
  248. _mm_store_si128(H1 + j, h);
  249. h = _mm_subs_epu16(h, gapoe);
  250. e = _mm_subs_epu16(e, gape);
  251. e = _mm_max_epi16(e, h);
  252. _mm_store_si128(E + j, e);
  253. f = _mm_subs_epu16(f, gape);
  254. f = _mm_max_epi16(f, h);
  255. h = _mm_load_si128(H0 + j);
  256. }
  257. for (k = 0; LIKELY(k < 16); ++k) {
  258. f = _mm_slli_si128(f, 2);
  259. for (j = 0; LIKELY(j < slen); ++j) {
  260. h = _mm_load_si128(H1 + j);
  261. h = _mm_max_epi16(h, f);
  262. _mm_store_si128(H1 + j, h);
  263. h = _mm_subs_epu16(h, gapoe);
  264. f = _mm_subs_epu16(f, gape);
  265. if(UNLIKELY(!_mm_movemask_epi8(_mm_cmpgt_epi16(f, h)))) goto end_loop8;
  266. }
  267. }
  268. end_loop8:
  269. __max_8(imax, max);
  270. if (imax >= minsc) {
  271. if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) {
  272. if (n_b == m_b) {
  273. m_b = m_b? m_b<<1 : 8;
  274. b = (uint64_t*)realloc(b, 8 * m_b);
  275. }
  276. b[n_b++] = (uint64_t)imax<<32 | i;
  277. } else if ((int)(b[n_b-1]>>32) < imax) b[n_b-1] = (uint64_t)imax<<32 | i; // modify the last
  278. }
  279. if (imax > gmax) {
  280. gmax = imax; te = i;
  281. for (j = 0; LIKELY(j < slen); ++j)
  282. _mm_store_si128(Hmax + j, _mm_load_si128(H1 + j));
  283. if (gmax >= endsc) break;
  284. }
  285. S = H1; H1 = H0; H0 = S;
  286. }
  287. r.score = gmax; r.te = te;
  288. {
  289. int max = -1, low, high, qlen = slen * 8;
  290. uint16_t *t = (uint16_t*)Hmax;
  291. for (i = 0, r.qe = -1; i < qlen; ++i, ++t)
  292. if ((int)*t > max) max = *t, r.qe = i / 8 + i % 8 * slen;
  293. if (b) {
  294. i = (r.score + q->max - 1) / q->max;
  295. low = te - i; high = te + i;
  296. for (i = 0; i < n_b; ++i) {
  297. int e = (int32_t)b[i];
  298. if ((e < low || e > high) && (int)(b[i]>>32) > r.score2)
  299. r.score2 = b[i]>>32, r.te2 = e;
  300. }
  301. }
  302. }
  303. free(b);
  304. return r;
  305. }
  306. static void revseq(int l, uint8_t *s)
  307. {
  308. int i, t;
  309. for (i = 0; i < l>>1; ++i)
  310. t = s[i], s[i] = s[l - 1 - i], s[l - 1 - i] = t;
  311. }
  312. kswr_t ksw_align(int qlen, uint8_t *query, int tlen, uint8_t *target, int m, const int8_t *mat, int gapo, int gape, int xtra, kswq_t **qry)
  313. {
  314. int size;
  315. kswq_t *q;
  316. kswr_t r, rr;
  317. kswr_t (*func)(kswq_t*, int, const uint8_t*, int, int, int);
  318. q = (qry && *qry)? *qry : ksw_qinit((xtra&KSW_XBYTE)? 1 : 2, qlen, query, m, mat);
  319. if (qry && *qry == 0) *qry = q;
  320. func = q->size == 2? ksw_i16 : ksw_u8;
  321. size = q->size;
  322. r = func(q, tlen, target, gapo, gape, xtra);
  323. if (qry == 0) free(q);
  324. if ((xtra&KSW_XSTART) == 0 || ((xtra&KSW_XSUBO) && r.score < (xtra&0xffff))) return r;
  325. revseq(r.qe + 1, query); revseq(r.te + 1, target); // +1 because qe/te points to the exact end, not the position after the end
  326. q = ksw_qinit(size, r.qe + 1, query, m, mat);
  327. rr = func(q, tlen, target, gapo, gape, KSW_XSTOP | r.score);
  328. revseq(r.qe + 1, query); revseq(r.te + 1, target);
  329. free(q);
  330. if (r.score == rr.score)
  331. r.tb = r.te - rr.te, r.qb = r.qe - rr.qe;
  332. return r;
  333. }
  334. /********************
  335. *** SW extension ***
  336. ********************/
  337. typedef struct {
  338. int32_t h, e;
  339. } eh_t;
  340. int ksw_extend(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int m, const int8_t *mat, int gapo, int gape, int w, int h0, int *_qle, int *_tle)
  341. {
  342. eh_t *eh; // score array
  343. int8_t *qp; // query profile
  344. int i, j, k, gapoe = gapo + gape, beg, end, max, max_i, max_j, max_gap;
  345. if (h0 < 0) h0 = 0;
  346. // allocate memory
  347. qp = malloc(qlen * m);
  348. eh = calloc(qlen + 1, 8);
  349. // generate the query profile
  350. for (k = i = 0; k < m; ++k) {
  351. const int8_t *p = &mat[k * m];
  352. for (j = 0; j < qlen; ++j) qp[i++] = p[query[j]];
  353. }
  354. // fill the first row
  355. eh[0].h = h0; eh[1].h = h0 > gapoe? h0 - gapoe : 0;
  356. for (j = 2; j <= qlen && eh[j-1].h > gape; ++j)
  357. eh[j].h = eh[j-1].h - gape;
  358. // adjust $w if it is too large
  359. k = m * m;
  360. for (i = 0, max = 0; i < k; ++i) // get the max score
  361. max = max > mat[i]? max : mat[i];
  362. max_gap = (int)((double)(qlen * max - gapo) / gape + 1.);
  363. max_gap = max_gap > 1? max_gap : 1;
  364. w = w < max_gap? w : max_gap;
  365. // DP loop
  366. max = h0, max_i = max_j = -1;
  367. beg = 0, end = qlen;
  368. for (i = 0; LIKELY(i < tlen); ++i) {
  369. int f = 0, h1, m = 0, mj = -1;
  370. int8_t *q = &qp[target[i] * qlen];
  371. // compute the first column
  372. h1 = h0 - (gapo + gape * (i + 1));
  373. if (h1 < 0) h1 = 0;
  374. // apply the band and the constraint (if provided)
  375. if (beg < i - w) beg = i - w;
  376. if (end > i + w + 1) end = i + w + 1;
  377. if (end > qlen) end = qlen;
  378. for (j = beg; LIKELY(j < end); ++j) {
  379. // At the beginning of the loop: eh[j] = { H(i-1,j-1), E(i,j) }, f = F(i,j) and h1 = H(i,j-1)
  380. // Similar to SSE2-SW, cells are computed in the following order:
  381. // H(i,j) = max{H(i-1,j-1)+S(i,j), E(i,j), F(i,j)}
  382. // E(i+1,j) = max{H(i,j)-gapo, E(i,j)} - gape
  383. // F(i,j+1) = max{H(i,j)-gapo, F(i,j)} - gape
  384. eh_t *p = &eh[j];
  385. int h = p->h, e = p->e; // get H(i-1,j-1) and E(i-1,j)
  386. p->h = h1; // set H(i,j-1) for the next row
  387. h += q[j];
  388. h = h > e? h : e;
  389. h = h > f? h : f;
  390. h1 = h; // save H(i,j) to h1 for the next column
  391. mj = m > h? mj : j;
  392. m = m > h? m : h; // m is stored at eh[mj+1]
  393. h -= gapoe;
  394. h = h > 0? h : 0;
  395. e -= gape;
  396. e = e > h? e : h; // computed E(i+1,j)
  397. p->e = e; // save E(i+1,j) for the next row
  398. f -= gape;
  399. f = f > h? f : h; // computed F(i,j+1)
  400. }
  401. eh[end].h = h1; eh[end].e = 0;
  402. if (m == 0) break;
  403. if (m > max) max = m, max_i = i, max_j = mj;
  404. // update beg and end for the next round
  405. for (j = mj; j >= beg && eh[j].h; --j);
  406. beg = j + 1;
  407. for (j = mj + 2; j <= end && eh[j].h; ++j);
  408. end = j;
  409. //beg = 0; end = qlen; // uncomment this line for debugging
  410. }
  411. free(eh); free(qp);
  412. if (_qle) *_qle = max_j + 1;
  413. if (_tle) *_tle = max_i + 1;
  414. return max;
  415. }
  416. /********************
  417. * Global alignment *
  418. ********************/
  419. #define MINUS_INF -0x40000000
  420. static inline uint32_t *push_cigar(int *n_cigar, int *m_cigar, uint32_t *cigar, int op, int len)
  421. {
  422. if (*n_cigar == 0 || op != (cigar[(*n_cigar) - 1]&0xf)) {
  423. if (*n_cigar == *m_cigar) {
  424. *m_cigar = *m_cigar? (*m_cigar)<<1 : 4;
  425. cigar = realloc(cigar, (*m_cigar) << 2);
  426. }
  427. cigar[(*n_cigar)++] = len<<4 | op;
  428. } else cigar[(*n_cigar)-1] += len<<4;
  429. return cigar;
  430. }
  431. int ksw_global(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int m, const int8_t *mat, int gapo, int gape, int w, int *n_cigar_, uint32_t **cigar_)
  432. {
  433. eh_t *eh;
  434. int8_t *qp; // query profile
  435. int i, j, k, gapoe = gapo + gape, score, n_col;
  436. uint8_t *z; // backtrack matrix; in each cell: f<<4|e<<2|h; in principle, we can halve the memory, but backtrack will be a little more complex
  437. if (n_cigar_) *n_cigar_ = 0;
  438. // allocate memory
  439. n_col = qlen < 2*w+1? qlen : 2*w+1; // maximum #columns of the backtrack matrix
  440. z = malloc(n_col * tlen);
  441. qp = malloc(qlen * m);
  442. eh = calloc(qlen + 1, 8);
  443. // generate the query profile
  444. for (k = i = 0; k < m; ++k) {
  445. const int8_t *p = &mat[k * m];
  446. for (j = 0; j < qlen; ++j) qp[i++] = p[query[j]];
  447. }
  448. // fill the first row
  449. eh[0].h = 0; eh[0].e = MINUS_INF;
  450. for (j = 1; j <= qlen && j <= w; ++j)
  451. eh[j].h = -(gapo + gape * j), eh[j].e = MINUS_INF;
  452. for (; j <= qlen; ++j) eh[j].h = eh[j].e = MINUS_INF; // everything is -inf outside the band
  453. // DP loop
  454. for (i = 0; LIKELY(i < tlen); ++i) { // target sequence is in the outer loop
  455. int32_t f = MINUS_INF, h1, beg, end;
  456. int8_t *q = &qp[target[i] * qlen];
  457. uint8_t *zi = &z[i * n_col];
  458. beg = i > w? i - w : 0;
  459. end = i + w + 1 < qlen? i + w + 1 : qlen; // only loop through [beg,end) of the query sequence
  460. h1 = beg == 0? -(gapo + gape * (i + 1)) : MINUS_INF;
  461. for (j = beg; LIKELY(j < end); ++j) {
  462. // This loop is organized in a similar way to ksw_extend() and ksw_sse2(), except:
  463. // 1) not checking h>0; 2) recording direction for backtracking
  464. eh_t *p = &eh[j];
  465. int32_t h = p->h, e = p->e;
  466. uint8_t d; // direction
  467. p->h = h1;
  468. h += q[j];
  469. d = h > e? 0 : 1;
  470. h = h > e? h : e;
  471. d = h > f? d : 2;
  472. h = h > f? h : f;
  473. h1 = h;
  474. h -= gapoe;
  475. e -= gape;
  476. d |= e > h? 1<<2 : 0;
  477. e = e > h? e : h;
  478. p->e = e;
  479. f -= gape;
  480. d |= f > h? 2<<4 : 0; // if we want to halve the memory, use one bit only, instead of two
  481. f = f > h? f : h;
  482. zi[j - beg] = d; // z[i,j] keeps h for the current cell and e/f for the next cell
  483. }
  484. eh[end].h = h1; eh[end].e = MINUS_INF;
  485. }
  486. score = eh[qlen].h;
  487. if (n_cigar_ && cigar_) { // backtrack
  488. int n_cigar = 0, m_cigar = 0, which = 0;
  489. uint32_t *cigar = 0, tmp;
  490. i = tlen - 1; k = (i + w + 1 < qlen? i + w + 1 : qlen) - 1; // (i,k) points to the last cell
  491. while (i >= 0 && k >= 0) {
  492. which = z[i * n_col + (k - (i > w? i - w : 0))] >> (which<<1) & 3;
  493. if (which == 0) cigar = push_cigar(&n_cigar, &m_cigar, cigar, 0, 1), --i, --k;
  494. else if (which == 1) cigar = push_cigar(&n_cigar, &m_cigar, cigar, 2, 1), --i;
  495. else cigar = push_cigar(&n_cigar, &m_cigar, cigar, 1, 1), --k;
  496. }
  497. if (i >= 0) cigar = push_cigar(&n_cigar, &m_cigar, cigar, 2, i + 1);
  498. if (k >= 0) cigar = push_cigar(&n_cigar, &m_cigar, cigar, 1, k + 1);
  499. for (i = 0; i < n_cigar>>1; ++i) // reverse CIGAR
  500. tmp = cigar[i], cigar[i] = cigar[n_cigar-1-i], cigar[n_cigar-1-i] = tmp;
  501. *n_cigar_ = n_cigar, *cigar_ = cigar;
  502. }
  503. free(eh); free(qp); free(z);
  504. return score;
  505. }
  506. /*******************************************
  507. * Main function (not compiled by default) *
  508. *******************************************/
  509. #ifdef _KSW_MAIN
  510. #include <unistd.h>
  511. #include <stdio.h>
  512. #include <zlib.h>
  513. #include "kseq.h"
  514. KSEQ_INIT(gzFile, gzread)
  515. unsigned char seq_nt4_table[256] = {
  516. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  517. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  518. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  519. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  520. 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
  521. 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  522. 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
  523. 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  524. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  525. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  526. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  527. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  528. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  529. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  530. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  531. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  532. };
  533. int main(int argc, char *argv[])
  534. {
  535. int c, sa = 1, sb = 3, i, j, k, forward_only = 0, max_rseq = 0;
  536. int8_t mat[25];
  537. int gapo = 5, gape = 2, minsc = 0, xtra = KSW_XSTART;
  538. uint8_t *rseq = 0;
  539. gzFile fpt, fpq;
  540. kseq_t *kst, *ksq;
  541. // parse command line
  542. while ((c = getopt(argc, argv, "a:b:q:r:ft:1")) >= 0) {
  543. switch (c) {
  544. case 'a': sa = atoi(optarg); break;
  545. case 'b': sb = atoi(optarg); break;
  546. case 'q': gapo = atoi(optarg); break;
  547. case 'r': gape = atoi(optarg); break;
  548. case 't': minsc = atoi(optarg); break;
  549. case 'f': forward_only = 1; break;
  550. case '1': xtra |= KSW_XBYTE; break;
  551. }
  552. }
  553. if (optind + 2 > argc) {
  554. fprintf(stderr, "Usage: ksw [-1] [-f] [-a%d] [-b%d] [-q%d] [-r%d] [-t%d] <target.fa> <query.fa>\n", sa, sb, gapo, gape, minsc);
  555. return 1;
  556. }
  557. if (minsc > 0xffff) minsc = 0xffff;
  558. xtra |= KSW_XSUBO | minsc;
  559. // initialize scoring matrix
  560. for (i = k = 0; i < 4; ++i) {
  561. for (j = 0; j < 4; ++j)
  562. mat[k++] = i == j? sa : -sb;
  563. mat[k++] = 0; // ambiguous base
  564. }
  565. for (j = 0; j < 5; ++j) mat[k++] = 0;
  566. // open file
  567. fpt = gzopen(argv[optind], "r"); kst = kseq_init(fpt);
  568. fpq = gzopen(argv[optind+1], "r"); ksq = kseq_init(fpq);
  569. // all-pair alignment
  570. while (kseq_read(ksq) > 0) {
  571. kswq_t *q[2] = {0, 0};
  572. kswr_t r;
  573. for (i = 0; i < (int)ksq->seq.l; ++i) ksq->seq.s[i] = seq_nt4_table[(int)ksq->seq.s[i]];
  574. if (!forward_only) { // reverse
  575. if ((int)ksq->seq.m > max_rseq) {
  576. max_rseq = ksq->seq.m;
  577. rseq = (uint8_t*)realloc(rseq, max_rseq);
  578. }
  579. for (i = 0, j = ksq->seq.l - 1; i < (int)ksq->seq.l; ++i, --j)
  580. rseq[j] = ksq->seq.s[i] == 4? 4 : 3 - ksq->seq.s[i];
  581. }
  582. gzrewind(fpt); kseq_rewind(kst);
  583. while (kseq_read(kst) > 0) {
  584. for (i = 0; i < (int)kst->seq.l; ++i) kst->seq.s[i] = seq_nt4_table[(int)kst->seq.s[i]];
  585. r = ksw_align(ksq->seq.l, (uint8_t*)ksq->seq.s, kst->seq.l, (uint8_t*)kst->seq.s, 5, mat, gapo, gape, xtra, &q[0]);
  586. if (r.score >= minsc)
  587. printf("%s\t%d\t%d\t%s\t%d\t%d\t%d\t%d\t%d\n", kst->name.s, r.tb, r.te+1, ksq->name.s, r.qb, r.qe+1, r.score, r.score2, r.te2);
  588. if (rseq) {
  589. r = ksw_align(ksq->seq.l, rseq, kst->seq.l, (uint8_t*)kst->seq.s, 5, mat, gapo, gape, xtra, &q[1]);
  590. if (r.score >= minsc)
  591. printf("%s\t%d\t%d\t%s\t%d\t%d\t%d\t%d\t%d\n", kst->name.s, r.tb, r.te+1, ksq->name.s, (int)ksq->seq.l - r.qb, (int)ksq->seq.l - 1 - r.qe, r.score, r.score2, r.te2);
  592. }
  593. }
  594. free(q[0]); free(q[1]);
  595. }
  596. free(rseq);
  597. kseq_destroy(kst); gzclose(fpt);
  598. kseq_destroy(ksq); gzclose(fpq);
  599. return 0;
  600. }
  601. #endif