| 1 |
#include <jni.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include "align2_BandedAlignerJNI.h" |
| 5 |
|
| 6 |
|
| 7 |
#define max(a,b) \ |
| 8 |
({ __typeof__ (a) _a = (a); \ |
| 9 |
__typeof__ (b) _b = (b); \ |
| 10 |
_a > _b ? _a : _b; }) |
| 11 |
|
| 12 |
#define min(a,b) \ |
| 13 |
({ __typeof__ (a) _a = (a); \ |
| 14 |
__typeof__ (b) _b = (b); \ |
| 15 |
_a < _b ? _a : _b; }) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define penalizeOffCenter 1 |
| 21 |
|
| 22 |
|
| 23 |
jint alignForward( |
| 24 |
jbyte * query, |
| 25 |
jbyte * ref, |
| 26 |
jint query_length, |
| 27 |
jint ref_length, |
| 28 |
jint qstart, |
| 29 |
jint rstart, |
| 30 |
jint maxEdits, |
| 31 |
jboolean exact, |
| 32 |
jint * lastQueryLoc, |
| 33 |
jint * lastRefLoc, |
| 34 |
jint * lastRow, |
| 35 |
jint * lastEdits, |
| 36 |
jint * lastOffset, |
| 37 |
jint maxWidth, |
| 38 |
jbyte * baseToNumber |
| 39 |
); |
| 40 |
|
| 41 |
jint alignForwardRC( |
| 42 |
jbyte * query, |
| 43 |
jbyte * ref, |
| 44 |
jint query_length, |
| 45 |
jint ref_length, |
| 46 |
jint qstart, |
| 47 |
jint rstart, |
| 48 |
jint maxEdits, |
| 49 |
jboolean exact, |
| 50 |
jint * lastQueryLoc, |
| 51 |
jint * lastRefLoc, |
| 52 |
jint * lastRow, |
| 53 |
jint * lastEdits, |
| 54 |
jint * lastOffset, |
| 55 |
jint maxWidth, |
| 56 |
jbyte * baseToNumber, |
| 57 |
jbyte * baseToComplementExtended |
| 58 |
); |
| 59 |
|
| 60 |
jint alignReverse( |
| 61 |
jbyte * query, |
| 62 |
jbyte * ref, |
| 63 |
jint query_length, |
| 64 |
jint ref_length, |
| 65 |
jint qstart, |
| 66 |
jint rstart, |
| 67 |
jint maxEdits, |
| 68 |
jboolean exact, |
| 69 |
jint * lastQueryLoc, |
| 70 |
jint * lastRefLoc, |
| 71 |
jint * lastRow, |
| 72 |
jint * lastEdits, |
| 73 |
jint * lastOffset, |
| 74 |
jint maxWidth, |
| 75 |
jbyte * baseToNumber |
| 76 |
); |
| 77 |
|
| 78 |
jint alignReverseRC( |
| 79 |
jbyte * query, |
| 80 |
jbyte * ref, |
| 81 |
jint query_length, |
| 82 |
jint ref_length, |
| 83 |
jint qstart, |
| 84 |
jint rstart, |
| 85 |
jint maxEdits, |
| 86 |
jboolean exact, |
| 87 |
jint * lastQueryLoc, |
| 88 |
jint * lastRefLoc, |
| 89 |
jint * lastRow, |
| 90 |
jint * lastEdits, |
| 91 |
jint * lastOffset, |
| 92 |
jint maxWidth, |
| 93 |
jbyte * baseToNumber, |
| 94 |
jbyte * baseToComplementExtended |
| 95 |
); |
| 96 |
|
| 97 |
jint lastOffsetFunc(jint * array, jint halfWidth){ |
| 98 |
const jint center=halfWidth+1; |
| 99 |
jint minLoc=center; |
| 100 |
for(jint i=1; i<=halfWidth; i++){ |
| 101 |
if(array[center+i]<array[minLoc]){ |
| 102 |
minLoc=center+i; |
| 103 |
} |
| 104 |
if(array[center-i]<array[minLoc]){ |
| 105 |
minLoc=center-i; |
| 106 |
} |
| 107 |
} |
| 108 |
return center-minLoc; |
| 109 |
} |
| 110 |
|
| 111 |
jint penalizeOffCenterFunc(jint * array, jint halfWidth, jint big){ |
| 112 |
const jint center=halfWidth+1; |
| 113 |
jint edits=array[center]; |
| 114 |
for(jint i=1; i<=halfWidth; i++){ |
| 115 |
array[center+i]=min(big, array[center+i]+i); |
| 116 |
edits=min(edits, array[center+i]); |
| 117 |
array[center-i]=min(big, array[center-i]+i); |
| 118 |
edits=min(edits, array[center-i]); |
| 119 |
} |
| 120 |
return edits; |
| 121 |
} |
| 122 |
|
| 123 |
jint alignForward( |
| 124 |
jbyte * query, |
| 125 |
jbyte * ref, |
| 126 |
jint query_length, |
| 127 |
jint ref_length, |
| 128 |
jint qstart, |
| 129 |
jint rstart, |
| 130 |
jint maxEdits, |
| 131 |
jboolean exact, |
| 132 |
jint * lastQueryLoc, |
| 133 |
jint * lastRefLoc, |
| 134 |
jint * lastRow, |
| 135 |
jint * lastEdits, |
| 136 |
jint * lastOffset, |
| 137 |
jint maxWidth, |
| 138 |
jbyte * baseToNumber |
| 139 |
) { |
| 140 |
|
| 141 |
if(query_length-qstart>ref_length-rstart){ |
| 142 |
jint x=alignForward(ref, query, ref_length, query_length, rstart, |
| 143 |
qstart, maxEdits, exact, lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, baseToNumber); |
| 144 |
const jint temp = *lastQueryLoc; |
| 145 |
*lastQueryLoc = *lastRefLoc; |
| 146 |
*lastRefLoc = temp; |
| 147 |
return x; |
| 148 |
} |
| 149 |
|
| 150 |
const jint big=999; |
| 151 |
jint edits=0; |
| 152 |
jint row=0; |
| 153 |
*lastRow=-1; |
| 154 |
*lastEdits=0; |
| 155 |
*lastOffset=0; |
| 156 |
|
| 157 |
const jint width=min(maxWidth, (maxEdits*2)+1); |
| 158 |
const jint halfWidth=width/2; |
| 159 |
const jboolean inexact=!exact; |
| 160 |
|
| 161 |
jint qloc=qstart; |
| 162 |
jint rsloc=rstart-halfWidth; |
| 163 |
const jint xlines=query_length-qstart; |
| 164 |
const jint ylines=ref_length-rstart; |
| 165 |
const jint len=min(xlines, ylines); |
| 166 |
|
| 167 |
if(len<1){ |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
jint arrayCurrentArray[maxWidth+2]; |
| 172 |
jint arrayPrevArray[maxWidth+2]; |
| 173 |
jint * arrayCurrent=arrayCurrentArray; |
| 174 |
jint * arrayPrev=arrayPrevArray; |
| 175 |
jint * arrayTemp; |
| 176 |
|
| 177 |
for(jint i=0; i<maxWidth+2; i++) { |
| 178 |
arrayCurrent[i]=big; |
| 179 |
arrayPrev[i]=big; |
| 180 |
} |
| 181 |
|
| 182 |
{ |
| 183 |
const jbyte q=query[qloc]; |
| 184 |
const jint colStart=max(0, rsloc); |
| 185 |
const jint colLimit=min(rsloc+width, ref_length); |
| 186 |
edits=big; |
| 187 |
jint mloc=1+(colStart-rsloc); |
| 188 |
for(jint col=colStart; col<colLimit; mloc++, col++){ |
| 189 |
const jbyte r=ref[col]; |
| 190 |
const jint score=(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 191 |
arrayCurrent[mloc]=score; |
| 192 |
edits=min(edits, score); |
| 193 |
} |
| 194 |
row++; qloc++; rsloc++; |
| 195 |
} |
| 196 |
if(penalizeOffCenter){ |
| 197 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 198 |
} |
| 199 |
|
| 200 |
for(row=1; row<len; row++, qloc++, rsloc++){ |
| 201 |
arrayTemp=arrayCurrent; |
| 202 |
arrayCurrent=arrayPrev; |
| 203 |
arrayPrev=arrayTemp; |
| 204 |
const jbyte q=query[qloc]; |
| 205 |
const jint colStart=max(0, rsloc); |
| 206 |
const jint colLimit=min(rsloc+width, ref_length); |
| 207 |
for(jint i=0; i<maxWidth+2; i++) { |
| 208 |
arrayCurrent[i]=big; |
| 209 |
} |
| 210 |
edits=big; |
| 211 |
jint mloc=1+(colStart-rsloc); |
| 212 |
const jint forceDiag=(row==len-1); |
| 213 |
for(jint col=colStart; col<colLimit; mloc++, col++){ |
| 214 |
const jbyte r=ref[col]; |
| 215 |
const jint scoreUp=arrayPrev[mloc+1]+1; |
| 216 |
const jint scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 217 |
const jint scoreLeft=arrayCurrent[mloc-1]+1; |
| 218 |
const jint score=(forceDiag || col==ref_length-1) ? scoreDiag : min(scoreUp, min(scoreDiag, scoreLeft)); |
| 219 |
arrayCurrent[mloc]=score; |
| 220 |
edits=min(edits, score); |
| 221 |
} |
| 222 |
if(edits>maxEdits){ |
| 223 |
row++; |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
if(penalizeOffCenter){ |
| 228 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 229 |
} |
| 230 |
|
| 231 |
*lastRow=row-1; |
| 232 |
*lastEdits=edits; |
| 233 |
*lastQueryLoc=qloc-1; |
| 234 |
*lastOffset=lastOffsetFunc(arrayCurrent, halfWidth); |
| 235 |
*lastRefLoc=rsloc+halfWidth-(*lastOffset)-1; |
| 236 |
while((*lastRefLoc)>=ref_length || (*lastQueryLoc)>=query_length){(*lastRefLoc)--;(*lastQueryLoc)--;} |
| 237 |
|
| 238 |
return edits; |
| 239 |
} |
| 240 |
|
| 241 |
jint alignForwardRC( |
| 242 |
jbyte * query, |
| 243 |
jbyte * ref, |
| 244 |
jint query_length, |
| 245 |
jint ref_length, |
| 246 |
jint qstart, |
| 247 |
jint rstart, |
| 248 |
jint maxEdits, |
| 249 |
jboolean exact, |
| 250 |
jint * lastQueryLoc, |
| 251 |
jint * lastRefLoc, |
| 252 |
jint * lastRow, |
| 253 |
jint * lastEdits, |
| 254 |
jint * lastOffset, |
| 255 |
jint maxWidth, |
| 256 |
jbyte * baseToNumber, |
| 257 |
jbyte * baseToComplementExtended |
| 258 |
) { |
| 259 |
|
| 260 |
if(qstart+1>ref_length-rstart){ |
| 261 |
jint x=alignReverseRC(ref, query, ref_length, query_length, rstart, |
| 262 |
qstart, maxEdits, exact, lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, baseToNumber, baseToComplementExtended); |
| 263 |
const jint temp = *lastQueryLoc; |
| 264 |
*lastQueryLoc = *lastRefLoc; |
| 265 |
*lastRefLoc = temp; |
| 266 |
return x; |
| 267 |
} |
| 268 |
|
| 269 |
const jint big=999; |
| 270 |
jint edits=0; |
| 271 |
jint row=0; |
| 272 |
*lastRow=-1; |
| 273 |
*lastEdits=0; |
| 274 |
*lastOffset=0; |
| 275 |
|
| 276 |
const jint width=min(maxWidth, (maxEdits*2)+1); |
| 277 |
const jint halfWidth=width/2; |
| 278 |
const jboolean inexact=!exact; |
| 279 |
|
| 280 |
jint qloc=qstart; |
| 281 |
jint rsloc=rstart-halfWidth; |
| 282 |
const jint xlines=qstart+1; |
| 283 |
const jint ylines=ref_length-rstart; |
| 284 |
const jint len=min(xlines, ylines); |
| 285 |
|
| 286 |
if(len<1){ |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
jint arrayCurrentArray[maxWidth+2]; |
| 291 |
jint arrayPrevArray[maxWidth+2]; |
| 292 |
jint * arrayCurrent=arrayCurrentArray; |
| 293 |
jint * arrayPrev=arrayPrevArray; |
| 294 |
jint * arrayTemp; |
| 295 |
|
| 296 |
for(jint i=0; i<maxWidth+2; i++) { |
| 297 |
arrayCurrent[i]=big; |
| 298 |
arrayPrev[i]=big; |
| 299 |
} |
| 300 |
|
| 301 |
{ |
| 302 |
const jbyte q=baseToComplementExtended[query[qloc]]; |
| 303 |
const jint colStart=max(0, rsloc); |
| 304 |
const jint colLimit=min(rsloc+width, ref_length); |
| 305 |
edits=big; |
| 306 |
jint mloc=1+(colStart-rsloc); |
| 307 |
for(jint col=colStart; col<colLimit; mloc++, col++){ |
| 308 |
const jbyte r=ref[col]; |
| 309 |
const jint score=(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 310 |
arrayCurrent[mloc]=score; |
| 311 |
edits=min(edits, score); |
| 312 |
} |
| 313 |
row++; qloc--; rsloc++; |
| 314 |
} |
| 315 |
if(penalizeOffCenter){ |
| 316 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 317 |
} |
| 318 |
|
| 319 |
for(row=1; row<len; row++, qloc--, rsloc++){ |
| 320 |
arrayTemp=arrayCurrent; |
| 321 |
arrayCurrent=arrayPrev; |
| 322 |
arrayPrev=arrayTemp; |
| 323 |
const jbyte q=baseToComplementExtended[query[qloc]]; |
| 324 |
const jint colStart=max(0, rsloc); |
| 325 |
const jint colLimit=min(rsloc+width, ref_length); |
| 326 |
for(jint i=0; i<maxWidth+2; i++) { |
| 327 |
arrayCurrent[i]=big; |
| 328 |
} |
| 329 |
edits=big; |
| 330 |
jint mloc=1+(colStart-rsloc); |
| 331 |
const jint forceDiag=(row==len-1); |
| 332 |
for(jint col=colStart; col<colLimit; mloc++, col++){ |
| 333 |
const jbyte r=ref[col]; |
| 334 |
const jint scoreUp=arrayPrev[mloc+1]+1; |
| 335 |
const jint scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 336 |
const jint scoreLeft=arrayCurrent[mloc-1]+1; |
| 337 |
const jint score=(forceDiag || col==ref_length-1) ? scoreDiag : min(scoreUp, min(scoreDiag, scoreLeft)); |
| 338 |
arrayCurrent[mloc]=score; |
| 339 |
edits=min(edits, score); |
| 340 |
} |
| 341 |
if(edits>maxEdits){row++; break;} |
| 342 |
} |
| 343 |
if(penalizeOffCenter){ |
| 344 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 345 |
} |
| 346 |
|
| 347 |
*lastRow=row-1; |
| 348 |
*lastEdits=edits; |
| 349 |
*lastOffset=lastOffsetFunc(arrayCurrent, halfWidth); |
| 350 |
*lastQueryLoc=qloc+1; |
| 351 |
*lastRefLoc=rsloc+halfWidth-(*lastOffset)-1; |
| 352 |
while((*lastRefLoc)>=ref_length || (*lastQueryLoc)<0){(*lastRefLoc)--; (*lastQueryLoc)++;} |
| 353 |
|
| 354 |
return edits; |
| 355 |
} |
| 356 |
|
| 357 |
jint alignReverse( |
| 358 |
jbyte * query, |
| 359 |
jbyte * ref, |
| 360 |
jint query_length, |
| 361 |
jint ref_length, |
| 362 |
jint qstart, |
| 363 |
jint rstart, |
| 364 |
jint maxEdits, |
| 365 |
jboolean exact, |
| 366 |
jint * lastQueryLoc, |
| 367 |
jint * lastRefLoc, |
| 368 |
jint * lastRow, |
| 369 |
jint * lastEdits, |
| 370 |
jint * lastOffset, |
| 371 |
jint maxWidth, |
| 372 |
jbyte * baseToNumber |
| 373 |
) { |
| 374 |
|
| 375 |
if(qstart>rstart){ |
| 376 |
jint x=alignReverse(ref, query, ref_length, query_length, rstart, |
| 377 |
qstart, maxEdits, exact, lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, baseToNumber); |
| 378 |
const jint temp = *lastQueryLoc; |
| 379 |
*lastQueryLoc = *lastRefLoc; |
| 380 |
*lastRefLoc = temp; |
| 381 |
return x; |
| 382 |
} |
| 383 |
|
| 384 |
const jint big=999; |
| 385 |
jint edits=0; |
| 386 |
jint row=0; |
| 387 |
*lastRow=-1; |
| 388 |
*lastEdits=0; |
| 389 |
*lastOffset=0; |
| 390 |
|
| 391 |
const jint width=min(maxWidth, (maxEdits*2)+1); |
| 392 |
const jint halfWidth=width/2; |
| 393 |
const jboolean inexact=!exact; |
| 394 |
|
| 395 |
jint qloc=qstart; |
| 396 |
jint rsloc=rstart-halfWidth; |
| 397 |
const jint xlines=qstart+1; |
| 398 |
const jint ylines=rstart+1; |
| 399 |
const jint len=min(xlines, ylines); |
| 400 |
|
| 401 |
if(len<1){ |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
jint arrayCurrentArray[maxWidth+2]; |
| 406 |
jint arrayPrevArray[maxWidth+2]; |
| 407 |
jint * arrayCurrent=arrayCurrentArray; |
| 408 |
jint * arrayPrev=arrayPrevArray; |
| 409 |
jint * arrayTemp; |
| 410 |
|
| 411 |
for(jint i=0; i<maxWidth+2; i++) { |
| 412 |
arrayCurrent[i]=big; |
| 413 |
arrayPrev[i]=big; |
| 414 |
} |
| 415 |
|
| 416 |
{ |
| 417 |
const jbyte q=query[qloc]; |
| 418 |
const jint colStart=max(0, rsloc); |
| 419 |
const jint colLimit=min(rsloc+width, ref_length); |
| 420 |
edits=big; |
| 421 |
jint mloc=1+width-(colLimit-rsloc); |
| 422 |
for(jint col=colLimit-1; col>=colStart; mloc++, col--){ |
| 423 |
const jbyte r=ref[col]; |
| 424 |
const jint score=(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 425 |
arrayCurrent[mloc]=score; |
| 426 |
edits=min(edits, score); |
| 427 |
} |
| 428 |
row++; qloc--; rsloc--; |
| 429 |
} |
| 430 |
if(penalizeOffCenter){ |
| 431 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 432 |
} |
| 433 |
|
| 434 |
for(row=1; row<len; row++, qloc--, rsloc--){ |
| 435 |
arrayTemp=arrayCurrent; |
| 436 |
arrayCurrent=arrayPrev; |
| 437 |
arrayPrev=arrayTemp; |
| 438 |
const jbyte q=query[qloc]; |
| 439 |
const jint colStart=max(0, rsloc); |
| 440 |
const jint colLimit=min(rsloc+width, ref_length); |
| 441 |
for(jint i=0; i<maxWidth+2; i++) { |
| 442 |
arrayCurrent[i]=big; |
| 443 |
} |
| 444 |
edits=big; |
| 445 |
jint mloc=1+width-(colLimit-rsloc); |
| 446 |
const jint forceDiag=(row==len-1); |
| 447 |
for(jint col=colLimit-1; col>=colStart; mloc++, col--){ |
| 448 |
const jbyte r=ref[col]; |
| 449 |
const jint scoreUp=arrayPrev[mloc+1]+1; |
| 450 |
const jint scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 451 |
const jint scoreLeft=arrayCurrent[mloc-1]+1; |
| 452 |
const jint score=(forceDiag || col==0) ? scoreDiag : min(scoreUp, min(scoreDiag, scoreLeft)); |
| 453 |
arrayCurrent[mloc]=score; |
| 454 |
edits=min(edits, score); |
| 455 |
} |
| 456 |
if(edits>maxEdits){row++; break;} |
| 457 |
} |
| 458 |
if(penalizeOffCenter){ |
| 459 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 460 |
} |
| 461 |
|
| 462 |
*lastRow=row-1; |
| 463 |
*lastEdits=edits; |
| 464 |
*lastOffset=lastOffsetFunc(arrayCurrent, halfWidth); |
| 465 |
*lastQueryLoc=qloc+1; |
| 466 |
*lastRefLoc=rsloc+halfWidth+(*lastOffset)+1; |
| 467 |
while((*lastRefLoc)<0 || (*lastQueryLoc)<0){(*lastRefLoc)++; (*lastQueryLoc)++;} |
| 468 |
|
| 469 |
return edits; |
| 470 |
} |
| 471 |
|
| 472 |
jint alignReverseRC( |
| 473 |
jbyte * query, |
| 474 |
jbyte * ref, |
| 475 |
jint query_length, |
| 476 |
jint ref_length, |
| 477 |
jint qstart, |
| 478 |
jint rstart, |
| 479 |
jint maxEdits, |
| 480 |
jboolean exact, |
| 481 |
jint * lastQueryLoc, |
| 482 |
jint * lastRefLoc, |
| 483 |
jint * lastRow, |
| 484 |
jint * lastEdits, |
| 485 |
jint * lastOffset, |
| 486 |
jint maxWidth, |
| 487 |
jbyte * baseToNumber, |
| 488 |
jbyte * baseToComplementExtended |
| 489 |
) { |
| 490 |
|
| 491 |
if(query_length-qstart>rstart+1){ |
| 492 |
jint x=alignForwardRC(ref, query, ref_length, query_length, rstart, |
| 493 |
qstart, maxEdits, exact, lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, baseToNumber, baseToComplementExtended); |
| 494 |
const jint temp = *lastQueryLoc; |
| 495 |
*lastQueryLoc = *lastRefLoc; |
| 496 |
*lastRefLoc = temp; |
| 497 |
return x; |
| 498 |
} |
| 499 |
|
| 500 |
const jint big=999; |
| 501 |
jint edits=0; |
| 502 |
jint row=0; |
| 503 |
*lastRow=-1; |
| 504 |
*lastEdits=0; |
| 505 |
*lastOffset=0; |
| 506 |
|
| 507 |
const jint width=min(maxWidth, (maxEdits*2)+1); |
| 508 |
const jint halfWidth=width/2; |
| 509 |
const jboolean inexact=!exact; |
| 510 |
|
| 511 |
jint qloc=qstart; |
| 512 |
jint rsloc=rstart-halfWidth; |
| 513 |
const jint xlines=query_length-qstart; |
| 514 |
const jint ylines=rstart+1; |
| 515 |
const jint len=min(xlines, ylines); |
| 516 |
|
| 517 |
if(len<1){ |
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
jint arrayCurrentArray[maxWidth+2]; |
| 522 |
jint arrayPrevArray[maxWidth+2]; |
| 523 |
jint * arrayCurrent=arrayCurrentArray; |
| 524 |
jint * arrayPrev=arrayPrevArray; |
| 525 |
jint * arrayTemp; |
| 526 |
|
| 527 |
for(jint i=0; i<maxWidth+2; i++) { |
| 528 |
arrayCurrent[i]=big; |
| 529 |
arrayPrev[i]=big; |
| 530 |
} |
| 531 |
|
| 532 |
{ |
| 533 |
const jbyte q=baseToComplementExtended[query[qloc]]; |
| 534 |
const jint colStart=max(0, rsloc); |
| 535 |
const jint colLimit=min(rsloc+width, ref_length); |
| 536 |
edits=big; |
| 537 |
jint mloc=1+width-(colLimit-rsloc); |
| 538 |
for(jint col=colLimit-1; col>=colStart; mloc++, col--){ |
| 539 |
const jbyte r=ref[col]; |
| 540 |
const jint score=(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 541 |
arrayCurrent[mloc]=score; |
| 542 |
edits=min(edits, score); |
| 543 |
} |
| 544 |
row++; qloc++; rsloc--; |
| 545 |
} |
| 546 |
if(penalizeOffCenter){ |
| 547 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 548 |
} |
| 549 |
|
| 550 |
for(row=1; row<len; row++, qloc++, rsloc--){ |
| 551 |
arrayTemp=arrayCurrent; |
| 552 |
arrayCurrent=arrayPrev; |
| 553 |
arrayPrev=arrayTemp; |
| 554 |
const jbyte q=baseToComplementExtended[query[qloc]]; |
| 555 |
const jint colStart=max(0, rsloc); |
| 556 |
const jint colLimit=min(rsloc+width, ref_length); |
| 557 |
for(jint i=0; i<maxWidth+2; i++) { |
| 558 |
arrayCurrent[i]=big; |
| 559 |
} |
| 560 |
edits=big; |
| 561 |
jint mloc=1+width-(colLimit-rsloc); |
| 562 |
const jint forceDiag=(row==len-1); |
| 563 |
for(jint col=colLimit-1; col>=colStart; mloc++, col--){ |
| 564 |
const jbyte r=ref[col]; |
| 565 |
const jint scoreUp=arrayPrev[mloc+1]+1; |
| 566 |
const jint scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!(baseToNumber[q]>=0) || !(baseToNumber[r]>=0))) ? 0 : 1); |
| 567 |
const jint scoreLeft=arrayCurrent[mloc-1]+1; |
| 568 |
const jint score=(forceDiag || col==0) ? scoreDiag : min(scoreUp, min(scoreDiag, scoreLeft)); |
| 569 |
arrayCurrent[mloc]=score; |
| 570 |
edits=min(edits, score); |
| 571 |
} |
| 572 |
if(edits>maxEdits){row++; break;} |
| 573 |
} |
| 574 |
if(penalizeOffCenter){ |
| 575 |
edits=penalizeOffCenterFunc(arrayCurrent, halfWidth, big); |
| 576 |
} |
| 577 |
|
| 578 |
*lastRow=row-1; |
| 579 |
*lastEdits=edits; |
| 580 |
*lastOffset=lastOffsetFunc(arrayCurrent, halfWidth); |
| 581 |
*lastQueryLoc=qloc-1; |
| 582 |
*lastRefLoc=rsloc+halfWidth+(*lastOffset)+1; |
| 583 |
while((*lastRefLoc)<0 || (*lastQueryLoc)>=query_length){(*lastRefLoc)++; (*lastQueryLoc)--;} |
| 584 |
return edits; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
JNIEXPORT jint JNICALL Java_align2_BandedAlignerJNI_alignForwardJNI( |
| 589 |
JNIEnv *env, |
| 590 |
jobject obj, |
| 591 |
jbyteArray query, |
| 592 |
jbyteArray ref, |
| 593 |
jint qstart, |
| 594 |
jint rstart, |
| 595 |
jint maxEdits, |
| 596 |
jboolean exact, |
| 597 |
jint maxWidth, |
| 598 |
jbyteArray baseToNumber, |
| 599 |
jintArray returnVals |
| 600 |
) { |
| 601 |
jint edits = 0; |
| 602 |
|
| 603 |
|
| 604 |
jint ref_length = (*env)->GetArrayLength(env, ref); |
| 605 |
jint query_length = (*env)->GetArrayLength(env, query); |
| 606 |
|
| 607 |
|
| 608 |
jbyte * jbaseToNumber = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, baseToNumber, NULL); |
| 609 |
jbyte * jref = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 610 |
jbyte * jquery = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 611 |
jint * jreturnVals = (jint*)(*env)->GetPrimitiveArrayCritical(env, returnVals, NULL); |
| 612 |
|
| 613 |
|
| 614 |
jint * lastQueryLoc = &jreturnVals[0]; |
| 615 |
jint * lastRefLoc = &jreturnVals[1]; |
| 616 |
jint * lastRow = &jreturnVals[2]; |
| 617 |
jint * lastEdits = &jreturnVals[3]; |
| 618 |
jint * lastOffset = &jreturnVals[4]; |
| 619 |
|
| 620 |
|
| 621 |
edits = alignForward(jquery, jref, query_length, ref_length, qstart, rstart, maxEdits, exact, |
| 622 |
lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, jbaseToNumber); |
| 623 |
|
| 624 |
|
| 625 |
(*env)->ReleasePrimitiveArrayCritical(env, baseToNumber, jbaseToNumber, JNI_ABORT); |
| 626 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 627 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 628 |
(*env)->ReleasePrimitiveArrayCritical(env, returnVals, jreturnVals, 0); |
| 629 |
|
| 630 |
return edits; |
| 631 |
} |
| 632 |
|
| 633 |
JNIEXPORT jint JNICALL Java_align2_BandedAlignerJNI_alignForwardRCJNI( |
| 634 |
JNIEnv *env, |
| 635 |
jobject obj, |
| 636 |
jbyteArray query, |
| 637 |
jbyteArray ref, |
| 638 |
jint qstart, |
| 639 |
jint rstart, |
| 640 |
jint maxEdits, |
| 641 |
jboolean exact, |
| 642 |
jint maxWidth, |
| 643 |
jbyteArray baseToNumber, |
| 644 |
jbyteArray baseToComplementExtended, |
| 645 |
jintArray returnVals |
| 646 |
) { |
| 647 |
jint edits = 0; |
| 648 |
|
| 649 |
jint ref_length = (*env)->GetArrayLength(env, ref); |
| 650 |
jint query_length = (*env)->GetArrayLength(env, query); |
| 651 |
|
| 652 |
jbyte * jbaseToComplementExtended = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, baseToComplementExtended, NULL); |
| 653 |
jbyte * jbaseToNumber = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, baseToNumber, NULL); |
| 654 |
jbyte * jref = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 655 |
jbyte * jquery = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 656 |
jint * jreturnVals = (jint*)(*env)->GetPrimitiveArrayCritical(env, returnVals, NULL); |
| 657 |
|
| 658 |
jint * lastQueryLoc = &jreturnVals[0]; |
| 659 |
jint * lastRefLoc = &jreturnVals[1]; |
| 660 |
jint * lastRow = &jreturnVals[2]; |
| 661 |
jint * lastEdits = &jreturnVals[3]; |
| 662 |
jint * lastOffset = &jreturnVals[4]; |
| 663 |
|
| 664 |
edits = alignForwardRC(jquery, jref, query_length, ref_length, qstart, rstart, maxEdits, exact, |
| 665 |
lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, jbaseToNumber, jbaseToComplementExtended); |
| 666 |
|
| 667 |
(*env)->ReleasePrimitiveArrayCritical(env, baseToComplementExtended, jbaseToComplementExtended, JNI_ABORT); |
| 668 |
(*env)->ReleasePrimitiveArrayCritical(env, baseToNumber, jbaseToNumber, JNI_ABORT); |
| 669 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 670 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 671 |
(*env)->ReleasePrimitiveArrayCritical(env, returnVals, jreturnVals, 0); |
| 672 |
|
| 673 |
return edits; |
| 674 |
} |
| 675 |
|
| 676 |
JNIEXPORT jint JNICALL Java_align2_BandedAlignerJNI_alignReverseJNI( |
| 677 |
JNIEnv *env, |
| 678 |
jobject obj, |
| 679 |
jbyteArray query, |
| 680 |
jbyteArray ref, |
| 681 |
jint qstart, |
| 682 |
jint rstart, |
| 683 |
jint maxEdits, |
| 684 |
jboolean exact, |
| 685 |
jint maxWidth, |
| 686 |
jbyteArray baseToNumber, |
| 687 |
jintArray returnVals |
| 688 |
) { |
| 689 |
jint edits = 0; |
| 690 |
|
| 691 |
jint ref_length = (*env)->GetArrayLength(env, ref); |
| 692 |
jint query_length = (*env)->GetArrayLength(env, query); |
| 693 |
|
| 694 |
jbyte * jbaseToNumber = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, baseToNumber, NULL); |
| 695 |
jbyte * jref = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 696 |
jbyte * jquery = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 697 |
jint * jreturnVals = (jint*)(*env)->GetPrimitiveArrayCritical(env, returnVals, NULL); |
| 698 |
|
| 699 |
jint * lastQueryLoc = &jreturnVals[0]; |
| 700 |
jint * lastRefLoc = &jreturnVals[1]; |
| 701 |
jint * lastRow = &jreturnVals[2]; |
| 702 |
jint * lastEdits = &jreturnVals[3]; |
| 703 |
jint * lastOffset = &jreturnVals[4]; |
| 704 |
|
| 705 |
edits = alignReverse(jquery, jref, query_length, ref_length, qstart, rstart, maxEdits, exact, |
| 706 |
lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, jbaseToNumber); |
| 707 |
|
| 708 |
(*env)->ReleasePrimitiveArrayCritical(env, baseToNumber, jbaseToNumber, JNI_ABORT); |
| 709 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 710 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 711 |
(*env)->ReleasePrimitiveArrayCritical(env, returnVals, jreturnVals, 0); |
| 712 |
|
| 713 |
return edits; |
| 714 |
} |
| 715 |
|
| 716 |
JNIEXPORT jint JNICALL Java_align2_BandedAlignerJNI_alignReverseRCJNI( |
| 717 |
JNIEnv *env, |
| 718 |
jobject obj, |
| 719 |
jbyteArray query, |
| 720 |
jbyteArray ref, |
| 721 |
jint qstart, |
| 722 |
jint rstart, |
| 723 |
jint maxEdits, |
| 724 |
jboolean exact, |
| 725 |
jint maxWidth, |
| 726 |
jbyteArray baseToNumber, |
| 727 |
jbyteArray baseToComplementExtended, |
| 728 |
jintArray returnVals |
| 729 |
) { |
| 730 |
jint edits = 0; |
| 731 |
|
| 732 |
jint ref_length = (*env)->GetArrayLength(env, ref); |
| 733 |
jint query_length = (*env)->GetArrayLength(env, query); |
| 734 |
|
| 735 |
jbyte * jbaseToComplementExtended = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, baseToComplementExtended, NULL); |
| 736 |
jbyte * jbaseToNumber = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, baseToNumber, NULL); |
| 737 |
jbyte * jref = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 738 |
jbyte * jquery = (jbyte*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 739 |
jint * jreturnVals = (jint*)(*env)->GetPrimitiveArrayCritical(env, returnVals, NULL); |
| 740 |
|
| 741 |
jint * lastQueryLoc = &jreturnVals[0]; |
| 742 |
jint * lastRefLoc = &jreturnVals[1]; |
| 743 |
jint * lastRow = &jreturnVals[2]; |
| 744 |
jint * lastEdits = &jreturnVals[3]; |
| 745 |
jint * lastOffset = &jreturnVals[4]; |
| 746 |
|
| 747 |
edits = alignReverseRC(jquery, jref, query_length, ref_length, qstart, rstart, maxEdits, exact, |
| 748 |
lastQueryLoc, lastRefLoc, lastRow, lastEdits, lastOffset, maxWidth, jbaseToNumber, jbaseToComplementExtended); |
| 749 |
|
| 750 |
(*env)->ReleasePrimitiveArrayCritical(env, baseToComplementExtended, jbaseToComplementExtended, JNI_ABORT); |
| 751 |
(*env)->ReleasePrimitiveArrayCritical(env, baseToNumber, jbaseToNumber, JNI_ABORT); |
| 752 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 753 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 754 |
(*env)->ReleasePrimitiveArrayCritical(env, returnVals, jreturnVals, 0); |
| 755 |
|
| 756 |
return edits; |
| 757 |
} |
| 758 |
|