| 1 |
#include <jni.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include "icecream_IceCreamAlignerJNI.h" |
| 4 |
|
| 5 |
#define pointsMatch 1 |
| 6 |
#define pointsSub -1 |
| 7 |
#define pointsDel -2 |
| 8 |
#define pointsIns -2 |
| 9 |
|
| 10 |
void alignForwardJNI(const jint * query, const jint * ref, jint * retArray, |
| 11 |
const jint qlen, const jint rlen, const jint minScore, const float minRatio) { |
| 12 |
|
| 13 |
const int arrayLength=rlen; |
| 14 |
const int arrayLength2=rlen+1; |
| 15 |
|
| 16 |
|
| 17 |
int array1[arrayLength2]; |
| 18 |
int array2[arrayLength2]; |
| 19 |
|
| 20 |
int * prev=array1; |
| 21 |
int * next=array2; |
| 22 |
int maxScore=-999999; |
| 23 |
int maxQpos=-1; |
| 24 |
int maxRpos=-1; |
| 25 |
int iters=0; |
| 26 |
retArray[0]=-999999; |
| 27 |
|
| 28 |
const int minPassingScore=(int)(qlen*minRatio*pointsMatch); |
| 29 |
const int minPassingScore3=minPassingScore-qlen*pointsMatch; |
| 30 |
|
| 31 |
for(int i=0; i<=arrayLength-qlen; i++){next[i]=prev[i]=0;} |
| 32 |
for(int i=arrayLength-qlen, score=0; i<=arrayLength; i++, score+=pointsDel) { |
| 33 |
prev[i]=score; |
| 34 |
next[i]=0; |
| 35 |
} |
| 36 |
|
| 37 |
for(int qpos=0; qpos<qlen; qpos++){ |
| 38 |
prev[0]=pointsIns*qpos; |
| 39 |
|
| 40 |
const int q=query[qpos]; |
| 41 |
int maxScoreThisPass=-999999; |
| 42 |
const int remainingBases=(qlen-qpos); |
| 43 |
const int remainingPoints=remainingBases*pointsMatch; |
| 44 |
const int minViableScore=minPassingScore3-remainingPoints; |
| 45 |
|
| 46 |
for(int rpos=0, apos=1; rpos<rlen; rpos++, apos++){ |
| 47 |
const int r=ref[rpos]; |
| 48 |
const int match=(q==r); |
| 49 |
const int vScore=prev[apos]+pointsIns; |
| 50 |
const int dScore=(match ? pointsMatch : pointsSub)+prev[apos-1]; |
| 51 |
|
| 52 |
|
| 53 |
int score=(dScore>=vScore ? dScore : vScore); |
| 54 |
next[apos]=score; |
| 55 |
} |
| 56 |
|
| 57 |
for(int apos=1; apos<arrayLength2; apos++){ |
| 58 |
const int hScore=next[apos-1]+pointsDel; |
| 59 |
|
| 60 |
|
| 61 |
int score=next[apos]; |
| 62 |
score=(hScore>score ? hScore : score); |
| 63 |
next[apos]=score; |
| 64 |
maxScoreThisPass=(score>maxScoreThisPass ? score : maxScoreThisPass); |
| 65 |
} |
| 66 |
iters+=arrayLength; |
| 67 |
|
| 68 |
|
| 69 |
if(maxScoreThisPass<minScore){return;} |
| 70 |
|
| 71 |
|
| 72 |
if(maxScoreThisPass<minViableScore){return;} |
| 73 |
|
| 74 |
int * temp=prev; |
| 75 |
prev=next; |
| 76 |
next=temp; |
| 77 |
} |
| 78 |
|
| 79 |
maxScore=-999999; |
| 80 |
for(int apos=1; apos<arrayLength2; apos++){ |
| 81 |
int score=prev[apos]; |
| 82 |
if(score>=maxScore){ |
| 83 |
maxScore=score; |
| 84 |
maxQpos=qlen; |
| 85 |
maxRpos=apos-1; |
| 86 |
} |
| 87 |
} |
| 88 |
retArray[0]=maxScore; |
| 89 |
retArray[1]=maxQpos; |
| 90 |
retArray[2]=maxRpos; |
| 91 |
retArray[3]=iters; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
void alignForward16JNI(const jshort * query, const jshort * ref, jint * retArray, |
| 96 |
const jshort qlen, const jshort rlen, const jshort minScore, const float minRatio) { |
| 97 |
|
| 98 |
const jshort arrayLength=rlen; |
| 99 |
const jshort arrayLength2=rlen+1; |
| 100 |
|
| 101 |
|
| 102 |
jshort array1[arrayLength2]; |
| 103 |
jshort array2[arrayLength2]; |
| 104 |
|
| 105 |
jshort * prev=array1; |
| 106 |
jshort * next=array2; |
| 107 |
jshort maxScore=-32000; |
| 108 |
jshort maxQpos=-1; |
| 109 |
jshort maxRpos=-1; |
| 110 |
jint iters=0; |
| 111 |
retArray[0]=-32000; |
| 112 |
|
| 113 |
const jshort minPassingScore=(int)(qlen*minRatio*pointsMatch); |
| 114 |
const jshort minPassingScore3=minPassingScore-qlen*pointsMatch; |
| 115 |
|
| 116 |
for(jshort i=0; i<=arrayLength-qlen; i++){next[i]=prev[i]=0;} |
| 117 |
for(jshort i=arrayLength-qlen, score=0; i<=arrayLength; i++, score+=pointsDel) { |
| 118 |
prev[i]=score; |
| 119 |
next[i]=0; |
| 120 |
} |
| 121 |
|
| 122 |
for(jshort qpos=0; qpos<qlen; qpos++){ |
| 123 |
prev[0]=pointsIns*qpos; |
| 124 |
|
| 125 |
const jshort q=query[qpos]; |
| 126 |
jshort maxScoreThisPass=-32000; |
| 127 |
const jshort remainingBases=(qlen-qpos); |
| 128 |
const jshort remainingPoints=remainingBases*pointsMatch; |
| 129 |
const jshort minViableScore=minPassingScore3-remainingPoints; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
for(jshort rpos=0, apos=1; rpos<rlen; rpos++, apos++){ |
| 146 |
const jshort r=ref[rpos]; |
| 147 |
const jshort match=(q==r); |
| 148 |
const jshort vScore=prev[apos]+pointsIns; |
| 149 |
const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1]; |
| 150 |
|
| 151 |
|
| 152 |
jshort score=(dScore>=vScore ? dScore : vScore); |
| 153 |
next[apos]=score; |
| 154 |
} |
| 155 |
|
| 156 |
for(jshort apos=1; apos<arrayLength2; apos++){ |
| 157 |
const jshort hScore=next[apos-1]+pointsDel; |
| 158 |
|
| 159 |
|
| 160 |
jshort score=next[apos]; |
| 161 |
score=(hScore>score ? hScore : score); |
| 162 |
next[apos]=score; |
| 163 |
maxScoreThisPass=(score>maxScoreThisPass ? score : maxScoreThisPass); |
| 164 |
} |
| 165 |
iters+=arrayLength; |
| 166 |
|
| 167 |
|
| 168 |
if(maxScoreThisPass<minScore){return;} |
| 169 |
|
| 170 |
|
| 171 |
if(maxScoreThisPass<minViableScore){return;} |
| 172 |
|
| 173 |
jshort * temp=prev; |
| 174 |
prev=next; |
| 175 |
next=temp; |
| 176 |
} |
| 177 |
|
| 178 |
maxScore=-32000; |
| 179 |
for(jshort apos=1; apos<arrayLength2; apos++){ |
| 180 |
jshort score=prev[apos]; |
| 181 |
if(score>=maxScore){ |
| 182 |
maxScore=score; |
| 183 |
maxQpos=qlen; |
| 184 |
maxRpos=apos-1; |
| 185 |
} |
| 186 |
} |
| 187 |
retArray[0]=maxScore; |
| 188 |
retArray[1]=maxQpos; |
| 189 |
retArray[2]=maxRpos; |
| 190 |
retArray[3]=iters; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
void alignForwardShortJNI(jint * query, jint * ref, jint * retArray, const jint qlen, const jint rlen) { |
| 195 |
|
| 196 |
const jint arrayLength=qlen; |
| 197 |
const jint arrayLength2=qlen+1; |
| 198 |
|
| 199 |
|
| 200 |
jint array1[arrayLength2]; |
| 201 |
jint array2[arrayLength2]; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
jint * prev=array1; |
| 207 |
jint * next=array2; |
| 208 |
jint maxScore=-999999; |
| 209 |
jint maxQpos=-1; |
| 210 |
jint maxRpos=-1; |
| 211 |
jint itersShort=0; |
| 212 |
|
| 213 |
for(jint i=0; i<arrayLength2; i++) { |
| 214 |
prev[i]=pointsIns*i; |
| 215 |
next[i]=0; |
| 216 |
} |
| 217 |
|
| 218 |
for(jint rpos=0; rpos<rlen; rpos++){ |
| 219 |
if(rlen-rpos<arrayLength){prev[0]=next[0]+pointsDel;} |
| 220 |
|
| 221 |
const jint r=ref[rpos]; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
for(jint qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){ |
| 239 |
const jint q=query[qpos]; |
| 240 |
const jboolean match=(q==r); |
| 241 |
const jint vScore=prev[apos]+pointsIns; |
| 242 |
const jint dScore=(match ? pointsMatch : pointsSub)+prev[apos-1]; |
| 243 |
|
| 244 |
const jint score=(dScore>=vScore ? dScore : vScore); |
| 245 |
|
| 246 |
next[apos]=score; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
for(jint qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){ |
| 251 |
const jint hScore=next[apos-1]+pointsDel; |
| 252 |
|
| 253 |
jint score=next[apos]; |
| 254 |
score=(hScore>score ? hScore : score); |
| 255 |
|
| 256 |
next[apos]=score; |
| 257 |
} |
| 258 |
|
| 259 |
itersShort+=arrayLength; |
| 260 |
|
| 261 |
jint score=next[arrayLength]; |
| 262 |
if(score>=maxScore){ |
| 263 |
maxScore=score; |
| 264 |
maxQpos=arrayLength-1; |
| 265 |
maxRpos=rpos; |
| 266 |
} |
| 267 |
|
| 268 |
jint * temp=prev; |
| 269 |
prev=next; |
| 270 |
next=temp; |
| 271 |
} |
| 272 |
retArray[0]=maxScore; |
| 273 |
retArray[1]=maxQpos; |
| 274 |
retArray[2]=maxRpos; |
| 275 |
retArray[3]=itersShort; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
void alignForwardShort16JNI(jshort * query, jshort * ref, jint * retArray, const jshort qlen, const jshort rlen) { |
| 283 |
|
| 284 |
const jshort arrayLength=qlen; |
| 285 |
const jshort arrayLength2=qlen+1; |
| 286 |
|
| 287 |
|
| 288 |
jshort array1[arrayLength2]; |
| 289 |
jshort array2[arrayLength2]; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
jshort * prev=array1; |
| 295 |
jshort * next=array2; |
| 296 |
jshort maxScore=-32000; |
| 297 |
jshort maxQpos=-1; |
| 298 |
jshort maxRpos=-1; |
| 299 |
jint itersShort=0; |
| 300 |
|
| 301 |
for(jshort i=0; i<arrayLength2; i++) { |
| 302 |
prev[i]=pointsIns*i; |
| 303 |
next[i]=0; |
| 304 |
} |
| 305 |
|
| 306 |
for(jshort rpos=0; rpos<rlen; rpos++){ |
| 307 |
if(rlen-rpos<arrayLength){prev[0]=next[0]+pointsDel;} |
| 308 |
|
| 309 |
const jshort r=ref[rpos]; |
| 310 |
jshort score=0; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){ |
| 328 |
const jshort q=query[qpos]; |
| 329 |
const jboolean match=(q==r); |
| 330 |
const jshort vScore=prev[apos]+pointsIns; |
| 331 |
const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1]; |
| 332 |
|
| 333 |
score=(dScore>=vScore ? dScore : vScore); |
| 334 |
|
| 335 |
next[apos]=score; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){ |
| 340 |
const jshort hScore=next[apos-1]+pointsDel; |
| 341 |
|
| 342 |
score=next[apos]; |
| 343 |
score=(hScore>score ? hScore : score); |
| 344 |
|
| 345 |
next[apos]=score; |
| 346 |
} |
| 347 |
|
| 348 |
itersShort+=arrayLength; |
| 349 |
|
| 350 |
if(score>=maxScore){ |
| 351 |
maxScore=score; |
| 352 |
maxQpos=arrayLength-1; |
| 353 |
maxRpos=rpos; |
| 354 |
} |
| 355 |
|
| 356 |
jshort * temp=prev; |
| 357 |
prev=next; |
| 358 |
next=temp; |
| 359 |
} |
| 360 |
retArray[0]=maxScore; |
| 361 |
retArray[1]=maxQpos; |
| 362 |
retArray[2]=maxRpos; |
| 363 |
retArray[3]=itersShort; |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
void alignForwardShort16JNI_352(jshort * query, jshort * ref, jint * retArray, const jshort rlen) { |
| 371 |
|
| 372 |
const jshort arrayLength=352; |
| 373 |
const jshort arrayLength2=352+1; |
| 374 |
|
| 375 |
|
| 376 |
jshort array1[arrayLength2]; |
| 377 |
jshort array2[arrayLength2]; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
jshort * prev=array1; |
| 383 |
jshort * next=array2; |
| 384 |
jshort maxScore=-16000; |
| 385 |
jshort maxQpos=-1; |
| 386 |
jshort maxRpos=-1; |
| 387 |
jint itersShort=0; |
| 388 |
|
| 389 |
for(jshort i=0; i<arrayLength2; i++) { |
| 390 |
prev[i]=pointsIns*i; |
| 391 |
next[i]=0; |
| 392 |
} |
| 393 |
|
| 394 |
for(jshort rpos=0; rpos<rlen; rpos++){ |
| 395 |
if(rlen-rpos<arrayLength){prev[0]=next[0]+pointsDel;} |
| 396 |
|
| 397 |
const jshort r=ref[rpos]; |
| 398 |
jshort score=0; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){ |
| 416 |
const jshort q=query[qpos]; |
| 417 |
const jboolean match=(q==r); |
| 418 |
const jshort vScore=prev[apos]+pointsIns; |
| 419 |
const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1]; |
| 420 |
|
| 421 |
score=(dScore>=vScore ? dScore : vScore); |
| 422 |
|
| 423 |
next[apos]=score; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){ |
| 428 |
const jshort hScore=next[apos-1]+pointsDel; |
| 429 |
|
| 430 |
score=next[apos]; |
| 431 |
score=(hScore>score ? hScore : score); |
| 432 |
|
| 433 |
next[apos]=score; |
| 434 |
} |
| 435 |
|
| 436 |
itersShort+=arrayLength; |
| 437 |
|
| 438 |
if(score>=maxScore){ |
| 439 |
maxScore=score; |
| 440 |
maxQpos=arrayLength-1; |
| 441 |
maxRpos=rpos; |
| 442 |
} |
| 443 |
|
| 444 |
jshort * temp=prev; |
| 445 |
prev=next; |
| 446 |
next=temp; |
| 447 |
} |
| 448 |
retArray[0]=maxScore; |
| 449 |
retArray[1]=maxQpos; |
| 450 |
retArray[2]=maxRpos; |
| 451 |
retArray[3]=itersShort; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForwardJNI |
| 458 |
(JNIEnv *env, jobject obj, jintArray query, jintArray ref, jintArray rvector, jint qlen, jint rlen, jint minScore, jfloat minRatio){ |
| 459 |
|
| 460 |
|
| 461 |
jint * jquery = (jint*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 462 |
jint * jref = (jint*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 463 |
jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL); |
| 464 |
|
| 465 |
alignForwardJNI(jquery, jref, jrvector, qlen, rlen, minScore, minRatio); |
| 466 |
|
| 467 |
|
| 468 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 469 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 470 |
(*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0); |
| 471 |
} |
| 472 |
|
| 473 |
JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForward16JNI |
| 474 |
(JNIEnv *env, jobject obj, jshortArray query, jshortArray ref, jintArray rvector, jshort qlen, jshort rlen, jshort minScore, jfloat minRatio){ |
| 475 |
|
| 476 |
|
| 477 |
jshort * jquery = (jshort*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 478 |
jshort * jref = (jshort*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 479 |
jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL); |
| 480 |
|
| 481 |
alignForward16JNI(jquery, jref, jrvector, qlen, rlen, minScore, minRatio); |
| 482 |
|
| 483 |
|
| 484 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 485 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 486 |
(*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0); |
| 487 |
} |
| 488 |
|
| 489 |
JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForwardShortJNI |
| 490 |
(JNIEnv *env, jobject obj, jintArray query, jintArray ref, jintArray rvector, jint qlen, jint rlen){ |
| 491 |
|
| 492 |
|
| 493 |
jint * jquery = (jint*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 494 |
jint * jref = (jint*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 495 |
jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL); |
| 496 |
|
| 497 |
alignForwardShortJNI(jquery, jref, jrvector, qlen, rlen); |
| 498 |
|
| 499 |
|
| 500 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 501 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 502 |
(*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0); |
| 503 |
} |
| 504 |
|
| 505 |
JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForwardShort16JNI |
| 506 |
(JNIEnv *env, jobject obj, jshortArray query, jshortArray ref, jintArray rvector, jshort qlen, jshort rlen){ |
| 507 |
|
| 508 |
|
| 509 |
jshort * jquery = (jshort*)(*env)->GetPrimitiveArrayCritical(env, query, NULL); |
| 510 |
jshort * jref = (jshort*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL); |
| 511 |
jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL); |
| 512 |
|
| 513 |
if(qlen==352){ |
| 514 |
alignForwardShort16JNI_352(jquery, jref, jrvector, rlen); |
| 515 |
}else{ |
| 516 |
alignForwardShort16JNI(jquery, jref, jrvector, qlen, rlen); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
(*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT); |
| 521 |
(*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT); |
| 522 |
(*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0); |
| 523 |
} |