| 1 |
package align2; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import shared.KillSwitch; |
| 7 |
import shared.Tools; |
| 8 |
import stream.SiteScore; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public final class MultiStateAligner9ts extends MSA{ |
| 17 |
|
| 18 |
|
| 19 |
public static void main(String[] args){ |
| 20 |
byte[] read=args[0].getBytes(); |
| 21 |
byte[] ref=args[1].getBytes(); |
| 22 |
|
| 23 |
byte[] original=ref; |
| 24 |
|
| 25 |
MultiStateAligner9ts msa=new MultiStateAligner9ts(read.length, ref.length); |
| 26 |
|
| 27 |
System.out.println("Initial: "); |
| 28 |
printMatrix(msa.packed, read.length, ref.length, TIMEMASK, SCOREOFFSET); |
| 29 |
|
| 30 |
int[] max=msa.fillLimited(read, ref, 0, ref.length-1, 0, null); |
| 31 |
|
| 32 |
System.out.println("Max: "+Arrays.toString(max)); |
| 33 |
|
| 34 |
System.out.println("Final: "); |
| 35 |
printMatrix(msa.packed, read.length, ref.length, TIMEMASK, SCOREOFFSET); |
| 36 |
|
| 37 |
byte[] out=msa.traceback(read, ref, 0, ref.length-1, max[0], max[1], max[2], false); |
| 38 |
|
| 39 |
int[] score=null; |
| 40 |
score=msa.score(read, ref, 0, ref.length-1, max[0], max[1], max[2], false); |
| 41 |
|
| 42 |
System.out.println(new String(ref)); |
| 43 |
System.out.println(new String(read)); |
| 44 |
System.out.println(new String(out)); |
| 45 |
System.out.println("Score: "+Arrays.toString(score)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
public MultiStateAligner9ts(int maxRows_, int maxColumns_){ |
| 50 |
super(maxRows_, maxColumns_); |
| 51 |
|
| 52 |
packed=KillSwitch.allocInt3D(3, maxRows+1, maxColumns+1); |
| 53 |
grefbuffer=KillSwitch.allocByte1D(maxColumns+2); |
| 54 |
vertLimit=KillSwitch.allocInt1D(maxRows+1); |
| 55 |
horizLimit=KillSwitch.allocInt1D(maxColumns+1); |
| 56 |
|
| 57 |
Arrays.fill(vertLimit, BADoff); |
| 58 |
Arrays.fill(horizLimit, BADoff); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
for(int matrix=0; matrix<packed.length; matrix++){ |
| 65 |
for(int i=1; i<=maxRows; i++){ |
| 66 |
for(int j=0; j<packed[matrix][i].length; j++){ |
| 67 |
packed[matrix][i][j]|=BADoff; |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
for(int i=0; i<=maxRows; i++){ |
| 75 |
|
| 76 |
int prevScore=(i<2 ? 0 : packed[matrix][i-1][0]); |
| 77 |
int score=(i<2 ? (i*POINTSoff_INS) : |
| 78 |
(i<LIMIT_FOR_COST_3 ? prevScore+POINTSoff_INS2 : |
| 79 |
(i<LIMIT_FOR_COST_4 ? prevScore+POINTSoff_INS3 : prevScore+POINTSoff_INS4))); |
| 80 |
|
| 81 |
packed[matrix][i][0]=score; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public final int[] fillLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore, int[] gaps){ |
| 94 |
if(gaps==null){return fillLimitedX(read, ref, refStartLoc, refEndLoc, minScore);} |
| 95 |
else{ |
| 96 |
byte[] gref=makeGref(ref, gaps, refStartLoc, refEndLoc); |
| 97 |
assert(gref!=null) : "Excessively long read:\n"+new String(read); |
| 98 |
return fillLimitedX(read, gref, 0, greflimit, minScore); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private final int[] fillLimitedX(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){ |
| 106 |
|
| 107 |
|
| 108 |
rows=read.length; |
| 109 |
columns=refEndLoc-refStartLoc+1; |
| 110 |
|
| 111 |
final int halfband=(bandwidth<1 && bandwidthRatio<=0) ? 0 : |
| 112 |
Tools.max(Tools.min(bandwidth<1 ? 9999999 : bandwidth, bandwidthRatio<=0 ? 9999999 : 8+(int)(rows*bandwidthRatio)), (columns-rows+8))/2; |
| 113 |
|
| 114 |
if(minScore<1 || (columns+rows<90) || ((halfband<1 || halfband*3>columns) && (columns>read.length+Tools.min(170, read.length+20)))){ |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
return fillUnlimited(read, ref, refStartLoc, refEndLoc); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
final int BARRIER_I2=rows-BARRIER_I1, BARRIER_I2b=columns-1; |
| 123 |
final int BARRIER_D2=rows-BARRIER_D1; |
| 124 |
|
| 125 |
minScore-=MIN_SCORE_ADJUST; |
| 126 |
|
| 127 |
assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows+"\n"+ |
| 128 |
refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n"; |
| 129 |
assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns+"\n"+ |
| 130 |
refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n"; |
| 131 |
|
| 132 |
assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc+"\n"+ |
| 133 |
refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n"; |
| 134 |
assert(refEndLoc<ref.length) : "Check that values are in-bounds before calling this function: "+refEndLoc+", "+ref.length+"\n"+ |
| 135 |
refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n"; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
for(int x=0; x<packed.length; x++){ |
| 143 |
|
| 144 |
|
| 145 |
Arrays.fill(packed[x][rows], 1, columns+1, BADoff); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
int minGoodCol=1; |
| 152 |
int maxGoodCol=columns; |
| 153 |
|
| 154 |
final int minScore_off=(minScore<<SCOREOFFSET); |
| 155 |
final int maxGain=(read.length-1)*POINTSoff_MATCH2+POINTSoff_MATCH; |
| 156 |
final int floor=minScore_off-maxGain; |
| 157 |
|
| 158 |
final int subfloor=floor-5*POINTSoff_MATCH2; |
| 159 |
assert(subfloor>BADoff); |
| 160 |
assert(subfloor<minScore_off) : minScore_off+", "+floor+", "+BADoff+", "+subfloor; |
| 161 |
|
| 162 |
if(verbose2){ |
| 163 |
System.out.println(); |
| 164 |
System.out.println("minScore="+minScore+"\t"+minScore_off); |
| 165 |
System.out.println("maxGain="+(maxGain>>SCOREOFFSET)+"\t"+(maxGain)); |
| 166 |
System.out.println("floor="+(floor>>SCOREOFFSET)+"\t"+(floor)); |
| 167 |
System.out.println("subfloor="+(subfloor>>SCOREOFFSET)+"\t"+(subfloor)); |
| 168 |
System.out.println("BADoff="+(BADoff>>SCOREOFFSET)+"\t"+(BADoff)); |
| 169 |
System.out.println("maxGain="+(maxGain>>SCOREOFFSET)+"\t"+(maxGain)); |
| 170 |
System.out.println(); |
| 171 |
} |
| 172 |
|
| 173 |
vertLimit[rows]=minScore_off; |
| 174 |
boolean prevDefined=false; |
| 175 |
for(int i=rows-1; i>=0; i--){ |
| 176 |
byte c=read[i]; |
| 177 |
if(AminoAcid.isFullyDefined(c)){ |
| 178 |
vertLimit[i]=Tools.max(vertLimit[i+1]-(prevDefined ? POINTSoff_MATCH2 : POINTSoff_MATCH), floor); |
| 179 |
prevDefined=true; |
| 180 |
}else{ |
| 181 |
vertLimit[i]=Tools.max(vertLimit[i+1]-POINTSoff_NOCALL, floor); |
| 182 |
prevDefined=false; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
horizLimit[columns]=minScore_off; |
| 187 |
prevDefined=false; |
| 188 |
for(int i=columns-1; i>=0; i--){ |
| 189 |
byte c=ref[refStartLoc+i]; |
| 190 |
if(AminoAcid.isFullyDefined(c)){ |
| 191 |
horizLimit[i]=Tools.max(horizLimit[i+1]-(prevDefined ? POINTSoff_MATCH2 : POINTSoff_MATCH), floor); |
| 192 |
prevDefined=true; |
| 193 |
}else{ |
| 194 |
horizLimit[i]=Tools.max(horizLimit[i+1]-(prevDefined && c==GAPC ? POINTSoff_DEL : POINTSoff_NOREF), floor); |
| 195 |
prevDefined=false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
for(int row=1; row<=rows; row++){ |
| 210 |
|
| 211 |
final int colStart=(halfband<1 ? minGoodCol : Tools.max(minGoodCol, row-halfband)); |
| 212 |
final int colStop=(halfband<1 ? maxGoodCol : Tools.min(maxGoodCol, row+halfband*2-1)); |
| 213 |
|
| 214 |
minGoodCol=-1; |
| 215 |
maxGoodCol=-2; |
| 216 |
|
| 217 |
final int vlimit=vertLimit[row]; |
| 218 |
|
| 219 |
if(verbose2){ |
| 220 |
System.out.println(); |
| 221 |
System.out.println("row="+row); |
| 222 |
System.out.println("colStart="+colStart); |
| 223 |
System.out.println("colStop="+colStop); |
| 224 |
System.out.println("vlimit="+(vlimit>>SCOREOFFSET)+"\t"+(vlimit)); |
| 225 |
} |
| 226 |
|
| 227 |
if(colStart<0 || colStop<colStart){break;} |
| 228 |
|
| 229 |
|
| 230 |
if(colStart>1){ |
| 231 |
assert(row>0); |
| 232 |
packed[MODE_MS][row][colStart-1]=subfloor; |
| 233 |
packed[MODE_INS][row][colStart-1]=subfloor; |
| 234 |
packed[MODE_DEL][row][colStart-1]=subfloor; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
for(int col=colStart; col<=columns; col++){ |
| 239 |
|
| 240 |
|
| 241 |
if(verbose2){ |
| 242 |
System.out.println("\ncol "+col); |
| 243 |
} |
| 244 |
|
| 245 |
final byte call0=(row<2 ? (byte)'?' : read[row-2]); |
| 246 |
final byte call1=read[row-1]; |
| 247 |
final byte ref0=(col<2 ? (byte)'!' : ref[refStartLoc+col-2]); |
| 248 |
final byte ref1=ref[refStartLoc+col-1]; |
| 249 |
|
| 250 |
final boolean gap=(ref1==GAPC); |
| 251 |
assert(call1!=GAPC); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
final boolean match=(call1==ref1 && ref1!='N'); |
| 256 |
final boolean prevMatch=(call0==ref0 && ref0!='N'); |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
iterationsLimited++; |
| 261 |
final int limit=Tools.max(vlimit, horizLimit[col]); |
| 262 |
final int limit3=Tools.max(floor, (match ? limit-POINTSoff_MATCH2 : limit-POINTSoff_SUB3)); |
| 263 |
|
| 264 |
final int delNeeded=Tools.max(0, row-col-1); |
| 265 |
final int insNeeded=Tools.max(0, (rows-row)-(columns-col)-1); |
| 266 |
|
| 267 |
final int delPenalty=calcDelScoreOffset(delNeeded); |
| 268 |
final int insPenalty=calcInsScoreOffset(insNeeded); |
| 269 |
|
| 270 |
|
| 271 |
final int scoreFromDiag_MS=packed[MODE_MS][row-1][col-1]&SCOREMASK; |
| 272 |
final int scoreFromDel_MS=packed[MODE_DEL][row-1][col-1]&SCOREMASK; |
| 273 |
final int scoreFromIns_MS=packed[MODE_INS][row-1][col-1]&SCOREMASK; |
| 274 |
|
| 275 |
final int scoreFromDiag_DEL=packed[MODE_MS][row][col-1]&SCOREMASK; |
| 276 |
final int scoreFromDel_DEL=packed[MODE_DEL][row][col-1]&SCOREMASK; |
| 277 |
|
| 278 |
final int scoreFromDiag_INS=packed[MODE_MS][row-1][col]&SCOREMASK; |
| 279 |
final int scoreFromIns_INS=packed[MODE_INS][row-1][col]&SCOREMASK; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
if(gap || (scoreFromDiag_MS<=limit3 && scoreFromDel_MS<=limit3 && scoreFromIns_MS<=limit3)){ |
| 287 |
packed[MODE_MS][row][col]=subfloor; |
| 288 |
}else{ |
| 289 |
final int streak=(packed[MODE_MS][row-1][col-1]&TIMEMASK); |
| 290 |
|
| 291 |
{ |
| 292 |
|
| 293 |
int score; |
| 294 |
int time; |
| 295 |
byte prevState; |
| 296 |
|
| 297 |
if(match){ |
| 298 |
|
| 299 |
int scoreMS=scoreFromDiag_MS+(prevMatch ? POINTSoff_MATCH2 : POINTSoff_MATCH); |
| 300 |
int scoreD=scoreFromDel_MS+POINTSoff_MATCH; |
| 301 |
int scoreI=scoreFromIns_MS+POINTSoff_MATCH; |
| 302 |
|
| 303 |
|
| 304 |
if(scoreMS>=scoreD && scoreMS>=scoreI){ |
| 305 |
score=scoreMS; |
| 306 |
time=(prevMatch ? streak+1 : 1); |
| 307 |
prevState=MODE_MS; |
| 308 |
}else if(scoreD>=scoreI){ |
| 309 |
score=scoreD; |
| 310 |
time=1; |
| 311 |
prevState=MODE_DEL; |
| 312 |
}else{ |
| 313 |
score=scoreI; |
| 314 |
time=1; |
| 315 |
prevState=MODE_INS; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
}else{ |
| 328 |
|
| 329 |
int scoreMS; |
| 330 |
if(ref1!='N' && call1!='N'){ |
| 331 |
scoreMS=scoreFromDiag_MS+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : |
| 332 |
(streak==0 ? POINTSoff_SUB : streak<LIMIT_FOR_COST_3 ? POINTSoff_SUB2 : POINTSoff_SUB3)); |
| 333 |
}else{ |
| 334 |
scoreMS=scoreFromDiag_MS+POINTSoff_NOCALL; |
| 335 |
} |
| 336 |
|
| 337 |
int scoreD=scoreFromDel_MS+POINTSoff_SUB; |
| 338 |
int scoreI=scoreFromIns_MS+POINTSoff_SUB; |
| 339 |
|
| 340 |
if(scoreMS>=scoreD && scoreMS>=scoreI){ |
| 341 |
score=scoreMS; |
| 342 |
time=(prevMatch ? 1 : streak+1); |
| 343 |
|
| 344 |
prevState=MODE_MS; |
| 345 |
}else if(scoreD>=scoreI){ |
| 346 |
score=scoreD; |
| 347 |
time=1; |
| 348 |
prevState=MODE_DEL; |
| 349 |
}else{ |
| 350 |
score=scoreI; |
| 351 |
time=1; |
| 352 |
prevState=MODE_INS; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
final int limit2; |
| 366 |
if(delNeeded>0){ |
| 367 |
limit2=limit-delPenalty; |
| 368 |
}else if(insNeeded>0){ |
| 369 |
limit2=limit-insPenalty; |
| 370 |
}else{ |
| 371 |
limit2=limit; |
| 372 |
} |
| 373 |
assert(limit2>=limit); |
| 374 |
|
| 375 |
if(verbose2){System.err.println("MS: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));} |
| 376 |
|
| 377 |
if(score>=limit2){ |
| 378 |
maxGoodCol=col; |
| 379 |
if(minGoodCol<0){minGoodCol=col;} |
| 380 |
}else{ |
| 381 |
score=subfloor; |
| 382 |
} |
| 383 |
|
| 384 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 385 |
assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead"; |
| 386 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 387 |
|
| 388 |
packed[MODE_MS][row][col]=(score|time); |
| 389 |
assert((score&SCOREMASK)==score); |
| 390 |
|
| 391 |
assert((time&TIMEMASK)==time); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if((scoreFromDiag_DEL<=limit && scoreFromDel_DEL<=limit) || row<BARRIER_D1 || row>BARRIER_D2){ |
| 396 |
|
| 397 |
packed[MODE_DEL][row][col]=subfloor; |
| 398 |
}else{ |
| 399 |
|
| 400 |
final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK; |
| 401 |
|
| 402 |
int scoreMS=scoreFromDiag_DEL+POINTSoff_DEL; |
| 403 |
int scoreD=scoreFromDel_DEL+(streak==0 ? POINTSoff_DEL : |
| 404 |
streak<LIMIT_FOR_COST_3 ? POINTSoff_DEL2 : |
| 405 |
streak<LIMIT_FOR_COST_4 ? POINTSoff_DEL3 : |
| 406 |
streak<LIMIT_FOR_COST_5 ? POINTSoff_DEL4 : |
| 407 |
((streak&MASK5)==0 ? POINTSoff_DEL5 : 0)); |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
if(ref1=='N'){ |
| 412 |
scoreMS+=POINTSoff_DEL_REF_N; |
| 413 |
scoreD+=POINTSoff_DEL_REF_N; |
| 414 |
}else if(gap){ |
| 415 |
scoreMS+=POINTSoff_GAP; |
| 416 |
scoreD+=POINTSoff_GAP; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
int score; |
| 422 |
int time; |
| 423 |
byte prevState; |
| 424 |
if(scoreMS>=scoreD){ |
| 425 |
score=scoreMS; |
| 426 |
time=1; |
| 427 |
prevState=MODE_MS; |
| 428 |
}else{ |
| 429 |
score=scoreD; |
| 430 |
time=streak+1; |
| 431 |
prevState=MODE_DEL; |
| 432 |
} |
| 433 |
|
| 434 |
final int limit2; |
| 435 |
if(insNeeded>0){ |
| 436 |
limit2=limit-insPenalty; |
| 437 |
}else if(delNeeded>0){ |
| 438 |
limit2=limit-calcDelScoreOffset(time+delNeeded)+calcDelScoreOffset(time); |
| 439 |
}else{ |
| 440 |
limit2=limit; |
| 441 |
} |
| 442 |
assert(limit2>=limit); |
| 443 |
if(verbose2){System.err.println("DEL: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));} |
| 444 |
|
| 445 |
if(score>=limit2){ |
| 446 |
maxGoodCol=col; |
| 447 |
if(minGoodCol<0){minGoodCol=col;} |
| 448 |
}else{ |
| 449 |
score=subfloor; |
| 450 |
} |
| 451 |
|
| 452 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 453 |
assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead"; |
| 454 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 455 |
|
| 456 |
packed[MODE_DEL][row][col]=(score|time); |
| 457 |
assert((score&SCOREMASK)==score); |
| 458 |
|
| 459 |
assert((time&TIMEMASK)==time); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
if(gap || (scoreFromDiag_INS<=limit && scoreFromIns_INS<=limit) || (row<BARRIER_I1 && col>1) || (row>BARRIER_I2 && col<BARRIER_I2b)){ |
| 464 |
packed[MODE_INS][row][col]=subfloor; |
| 465 |
}else{ |
| 466 |
|
| 467 |
final int streak=packed[MODE_INS][row-1][col]&TIMEMASK; |
| 468 |
|
| 469 |
int scoreMS=scoreFromDiag_INS+POINTSoff_INS; |
| 470 |
|
| 471 |
int scoreI=scoreFromIns_INS+(streak==0 ? POINTSoff_INS : |
| 472 |
streak<LIMIT_FOR_COST_3 ? POINTSoff_INS2 : |
| 473 |
streak<LIMIT_FOR_COST_4 ? POINTSoff_INS3 : POINTSoff_INS4); |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
int score; |
| 484 |
int time; |
| 485 |
byte prevState; |
| 486 |
if(scoreMS>=scoreI){ |
| 487 |
score=scoreMS; |
| 488 |
time=1; |
| 489 |
prevState=MODE_MS; |
| 490 |
}else{ |
| 491 |
score=scoreI; |
| 492 |
time=streak+1; |
| 493 |
prevState=MODE_INS; |
| 494 |
} |
| 495 |
|
| 496 |
final int limit2; |
| 497 |
if(delNeeded>0){ |
| 498 |
limit2=limit-delPenalty; |
| 499 |
}else if(insNeeded>0){ |
| 500 |
limit2=limit-calcInsScoreOffset(time+insNeeded)+calcInsScoreOffset(time); |
| 501 |
}else{ |
| 502 |
limit2=limit; |
| 503 |
} |
| 504 |
assert(limit2>=limit); |
| 505 |
|
| 506 |
if(verbose2){System.err.println("INS: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));} |
| 507 |
if(score>=limit2){ |
| 508 |
maxGoodCol=col; |
| 509 |
if(minGoodCol<0){minGoodCol=col;} |
| 510 |
}else{ |
| 511 |
score=subfloor; |
| 512 |
} |
| 513 |
|
| 514 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 515 |
assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead"; |
| 516 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 517 |
|
| 518 |
packed[MODE_INS][row][col]=(score|time); |
| 519 |
assert((score&SCOREMASK)==score); |
| 520 |
|
| 521 |
assert((time&TIMEMASK)==time); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
if(col>=colStop){ |
| 526 |
if(col>colStop && (maxGoodCol<col || halfband>0)){break;} |
| 527 |
if(row>1){ |
| 528 |
packed[MODE_MS][row-1][col+1]=subfloor; |
| 529 |
packed[MODE_INS][row-1][col+1]=subfloor; |
| 530 |
packed[MODE_DEL][row-1][col+1]=subfloor; |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
int maxCol=-1; |
| 538 |
int maxState=-1; |
| 539 |
int maxScore=Integer.MIN_VALUE; |
| 540 |
|
| 541 |
for(int state=0; state<packed.length; state++){ |
| 542 |
for(int col=1; col<=columns; col++){ |
| 543 |
int x=packed[state][rows][col]&SCOREMASK; |
| 544 |
if(x>maxScore){ |
| 545 |
maxScore=x; |
| 546 |
maxCol=col; |
| 547 |
maxState=state; |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
assert(maxScore>=BADoff); |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
if(maxScore<minScore_off){ |
| 560 |
return null; |
| 561 |
} |
| 562 |
|
| 563 |
maxScore>>=SCOREOFFSET; |
| 564 |
|
| 565 |
|
| 566 |
return new int[] {rows, maxCol, maxState, maxScore}; |
| 567 |
} |
| 568 |
|
| 569 |
@Override |
| 570 |
public final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int[] gaps){ |
| 571 |
if(gaps==null){return fillUnlimited(read, ref, refStartLoc, refEndLoc);} |
| 572 |
else{ |
| 573 |
byte[] gref=makeGref(ref, gaps, refStartLoc, refEndLoc); |
| 574 |
assert(gref!=null) : "Excessively long read:\n"+new String(read); |
| 575 |
return fillUnlimited(read, gref, 0, greflimit); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
private final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc){ |
| 583 |
rows=read.length; |
| 584 |
columns=refEndLoc-refStartLoc+1; |
| 585 |
|
| 586 |
final int maxGain=(read.length-1)*POINTSoff_MATCH2+POINTSoff_MATCH; |
| 587 |
final int subfloor=0-2*maxGain; |
| 588 |
assert(subfloor>BADoff && subfloor*2>BADoff) : (read.length-1)+", "+maxGain+", "+subfloor+", "+(subfloor*2)+", "+BADoff+"\n" |
| 589 |
+rows+", "+columns+", "+POINTSoff_MATCH2+", "+SCOREOFFSET+"\n"+new String(read)+"\n"; |
| 590 |
|
| 591 |
final int BARRIER_I2=rows-BARRIER_I1, BARRIER_I2b=columns-1; |
| 592 |
final int BARRIER_D2=rows-BARRIER_D1; |
| 593 |
|
| 594 |
|
| 595 |
if(rows>maxRows || columns>maxColumns){ |
| 596 |
throw new RuntimeException("rows="+rows+", maxRows="+maxRows+", cols="+columns+", maxCols="+maxColumns+"\n"+new String(read)+"\n"); |
| 597 |
} |
| 598 |
|
| 599 |
assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows; |
| 600 |
assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns; |
| 601 |
|
| 602 |
assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc; |
| 603 |
assert(refEndLoc<ref.length) : "Check that values are in-bounds before calling this function: "+refEndLoc+", "+ref.length; |
| 604 |
|
| 605 |
for(int row=1; row<=rows; row++){ |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
for(int col=1; col<=columns; col++){ |
| 611 |
iterationsUnlimited++; |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
final byte call0=(row<2 ? (byte)'?' : read[row-2]); |
| 617 |
final byte call1=read[row-1]; |
| 618 |
final byte ref0=(col<2 ? (byte)'!' : ref[refStartLoc+col-2]); |
| 619 |
final byte ref1=ref[refStartLoc+col-1]; |
| 620 |
|
| 621 |
final boolean match=(call1==ref1 && ref1!='N'); |
| 622 |
final boolean prevMatch=(call0==ref0 && ref0!='N'); |
| 623 |
|
| 624 |
final boolean gap=(ref1==GAPC); |
| 625 |
assert(call1!=GAPC); |
| 626 |
|
| 627 |
if(gap){ |
| 628 |
packed[MODE_MS][row][col]=subfloor; |
| 629 |
}else{ |
| 630 |
|
| 631 |
final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK; |
| 632 |
final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK; |
| 633 |
final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK; |
| 634 |
final int streak=(packed[MODE_MS][row-1][col-1]&TIMEMASK); |
| 635 |
|
| 636 |
{ |
| 637 |
|
| 638 |
if(match){ |
| 639 |
|
| 640 |
int scoreMS=scoreFromDiag+(prevMatch ? POINTSoff_MATCH2 : POINTSoff_MATCH); |
| 641 |
int scoreD=scoreFromDel+POINTSoff_MATCH; |
| 642 |
int scoreI=scoreFromIns+POINTSoff_MATCH; |
| 643 |
|
| 644 |
int score; |
| 645 |
int time; |
| 646 |
|
| 647 |
if(scoreMS>=scoreD && scoreMS>=scoreI){ |
| 648 |
score=scoreMS; |
| 649 |
time=(prevMatch ? streak+1 : 1); |
| 650 |
|
| 651 |
}else if(scoreD>=scoreI){ |
| 652 |
score=scoreD; |
| 653 |
time=1; |
| 654 |
|
| 655 |
}else{ |
| 656 |
score=scoreI; |
| 657 |
time=1; |
| 658 |
|
| 659 |
} |
| 660 |
|
| 661 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 662 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 663 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 664 |
|
| 665 |
packed[MODE_MS][row][col]=(score|time); |
| 666 |
assert((score&SCOREMASK)==score); |
| 667 |
|
| 668 |
assert((time&TIMEMASK)==time); |
| 669 |
|
| 670 |
}else{ |
| 671 |
|
| 672 |
int scoreMS; |
| 673 |
if(ref1!='N' && call1!='N'){ |
| 674 |
scoreMS=scoreFromDiag+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : |
| 675 |
(streak==0 ? POINTSoff_SUB : streak<LIMIT_FOR_COST_3 ? POINTSoff_SUB2 : POINTSoff_SUB3)); |
| 676 |
}else{ |
| 677 |
scoreMS=scoreFromDiag+POINTSoff_NOCALL; |
| 678 |
} |
| 679 |
|
| 680 |
int scoreD=scoreFromDel+POINTSoff_SUB; |
| 681 |
int scoreI=scoreFromIns+POINTSoff_SUB; |
| 682 |
|
| 683 |
int score; |
| 684 |
int time; |
| 685 |
byte prevState; |
| 686 |
if(scoreMS>=scoreD && scoreMS>=scoreI){ |
| 687 |
score=scoreMS; |
| 688 |
time=(prevMatch ? 1 : streak+1); |
| 689 |
|
| 690 |
prevState=MODE_MS; |
| 691 |
}else if(scoreD>=scoreI){ |
| 692 |
score=scoreD; |
| 693 |
time=1; |
| 694 |
prevState=MODE_DEL; |
| 695 |
}else{ |
| 696 |
score=scoreI; |
| 697 |
time=1; |
| 698 |
prevState=MODE_INS; |
| 699 |
} |
| 700 |
|
| 701 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 702 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 703 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 704 |
|
| 705 |
packed[MODE_MS][row][col]=(score|time); |
| 706 |
assert((score&SCOREMASK)==score); |
| 707 |
|
| 708 |
assert((time&TIMEMASK)==time); |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
if(row<BARRIER_D1 || row>BARRIER_D2){ |
| 714 |
packed[MODE_DEL][row][col]=subfloor; |
| 715 |
}else{ |
| 716 |
|
| 717 |
final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK; |
| 718 |
|
| 719 |
final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; |
| 720 |
final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; |
| 721 |
|
| 722 |
int scoreMS=scoreFromDiag+POINTSoff_DEL; |
| 723 |
int scoreD=scoreFromDel+(streak==0 ? POINTSoff_DEL : |
| 724 |
streak<LIMIT_FOR_COST_3 ? POINTSoff_DEL2 : |
| 725 |
streak<LIMIT_FOR_COST_4 ? POINTSoff_DEL3 : |
| 726 |
streak<LIMIT_FOR_COST_5 ? POINTSoff_DEL4 : |
| 727 |
((streak&MASK5)==0 ? POINTSoff_DEL5 : 0)); |
| 728 |
|
| 729 |
|
| 730 |
if(ref1=='N'){ |
| 731 |
scoreMS+=POINTSoff_DEL_REF_N; |
| 732 |
scoreD+=POINTSoff_DEL_REF_N; |
| 733 |
}else if(gap){ |
| 734 |
scoreMS+=POINTSoff_GAP; |
| 735 |
scoreD+=POINTSoff_GAP; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
int score; |
| 741 |
int time; |
| 742 |
byte prevState; |
| 743 |
if(scoreMS>=scoreD){ |
| 744 |
score=scoreMS; |
| 745 |
time=1; |
| 746 |
prevState=MODE_MS; |
| 747 |
}else{ |
| 748 |
score=scoreD; |
| 749 |
time=streak+1; |
| 750 |
prevState=MODE_DEL; |
| 751 |
} |
| 752 |
|
| 753 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 754 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 755 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 756 |
|
| 757 |
packed[MODE_DEL][row][col]=(score|time); |
| 758 |
assert((score&SCOREMASK)==score); |
| 759 |
|
| 760 |
assert((time&TIMEMASK)==time); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
if(gap || (row<BARRIER_I1 && col>1) || (row>BARRIER_I2 && col<BARRIER_I2b)){ |
| 766 |
packed[MODE_INS][row][col]=subfloor; |
| 767 |
}else{ |
| 768 |
|
| 769 |
final int streak=packed[MODE_INS][row-1][col]&TIMEMASK; |
| 770 |
|
| 771 |
final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK; |
| 772 |
final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK; |
| 773 |
|
| 774 |
int scoreMS=scoreFromDiag+POINTSoff_INS; |
| 775 |
|
| 776 |
int scoreI=scoreFromIns+(streak==0 ? POINTSoff_INS : |
| 777 |
streak<LIMIT_FOR_COST_3 ? POINTSoff_INS2 : |
| 778 |
streak<LIMIT_FOR_COST_4 ? POINTSoff_INS3 : POINTSoff_INS4); |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
int score; |
| 788 |
int time; |
| 789 |
byte prevState; |
| 790 |
if(scoreMS>=scoreI){ |
| 791 |
score=scoreMS; |
| 792 |
time=1; |
| 793 |
prevState=MODE_MS; |
| 794 |
}else{ |
| 795 |
score=scoreI; |
| 796 |
time=streak+1; |
| 797 |
prevState=MODE_INS; |
| 798 |
} |
| 799 |
|
| 800 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 801 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 802 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 803 |
|
| 804 |
packed[MODE_INS][row][col]=(score|time); |
| 805 |
assert((score&SCOREMASK)==score); |
| 806 |
|
| 807 |
assert((time&TIMEMASK)==time); |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
int maxCol=-1; |
| 814 |
int maxState=-1; |
| 815 |
int maxScore=Integer.MIN_VALUE; |
| 816 |
|
| 817 |
for(int state=0; state<packed.length; state++){ |
| 818 |
for(int col=1; col<=columns; col++){ |
| 819 |
int x=packed[state][rows][col]&SCOREMASK; |
| 820 |
if(x>maxScore){ |
| 821 |
maxScore=x; |
| 822 |
maxCol=col; |
| 823 |
maxState=state; |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
maxScore>>=SCOREOFFSET; |
| 828 |
|
| 829 |
|
| 830 |
return new int[] {rows, maxCol, maxState, maxScore}; |
| 831 |
} |
| 832 |
|
| 833 |
@Override |
| 834 |
@Deprecated |
| 835 |
|
| 836 |
public final int[] fillQ(byte[] read, byte[] ref, byte[] baseScores, int refStartLoc, int refEndLoc){ |
| 837 |
assert(false) : "Needs to be redone to work with score cutoffs. Not difficult."; |
| 838 |
rows=read.length; |
| 839 |
columns=refEndLoc-refStartLoc+1; |
| 840 |
|
| 841 |
assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows; |
| 842 |
assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns; |
| 843 |
|
| 844 |
assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc; |
| 845 |
assert(refEndLoc<ref.length) : "Check that values are in-bounds before calling this function: "+refEndLoc+", "+ref.length; |
| 846 |
|
| 847 |
for(int row=1; row<=rows; row++){ |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
for(int col=1; col<=columns; col++){ |
| 853 |
|
| 854 |
final boolean match=(read[row-1]==ref[refStartLoc+col-1]); |
| 855 |
final boolean prevMatch=(row<2 || col<2 ? false : read[row-2]==ref[refStartLoc+col-2]); |
| 856 |
|
| 857 |
{ |
| 858 |
|
| 859 |
final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK; |
| 860 |
final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK; |
| 861 |
final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK; |
| 862 |
final int streak=(packed[MODE_MS][row-1][col-1]&TIMEMASK); |
| 863 |
|
| 864 |
{ |
| 865 |
|
| 866 |
if(match){ |
| 867 |
|
| 868 |
int scoreMS=scoreFromDiag+(prevMatch ? POINTSoff_MATCH2 : POINTSoff_MATCH); |
| 869 |
int scoreD=scoreFromDel+POINTSoff_MATCH; |
| 870 |
int scoreI=scoreFromIns+POINTSoff_MATCH; |
| 871 |
|
| 872 |
int score; |
| 873 |
int time; |
| 874 |
|
| 875 |
if(scoreMS>=scoreD && scoreMS>=scoreI){ |
| 876 |
score=scoreMS; |
| 877 |
time=(prevMatch ? streak+1 : 1); |
| 878 |
|
| 879 |
}else if(scoreD>=scoreI){ |
| 880 |
score=scoreD; |
| 881 |
time=1; |
| 882 |
|
| 883 |
}else{ |
| 884 |
score=scoreI; |
| 885 |
time=1; |
| 886 |
|
| 887 |
} |
| 888 |
score+=(((int)baseScores[row-1])<<SCOREOFFSET); |
| 889 |
|
| 890 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 891 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 892 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 893 |
|
| 894 |
packed[MODE_MS][row][col]=(score|time); |
| 895 |
assert((score&SCOREMASK)==score); |
| 896 |
|
| 897 |
assert((time&TIMEMASK)==time); |
| 898 |
|
| 899 |
}else{ |
| 900 |
|
| 901 |
int scoreMS=scoreFromDiag+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : |
| 902 |
(streak==0 ? POINTSoff_SUB : streak<LIMIT_FOR_COST_3 ? POINTSoff_SUB2 : POINTSoff_SUB3)); |
| 903 |
int scoreD=scoreFromDel+POINTSoff_SUB; |
| 904 |
int scoreI=scoreFromIns+POINTSoff_SUB; |
| 905 |
|
| 906 |
int score; |
| 907 |
int time; |
| 908 |
byte prevState; |
| 909 |
if(scoreMS>=scoreD && scoreMS>=scoreI){ |
| 910 |
score=scoreMS; |
| 911 |
time=(prevMatch ? 1 : streak+1); |
| 912 |
|
| 913 |
prevState=MODE_MS; |
| 914 |
}else if(scoreD>=scoreI){ |
| 915 |
score=scoreD; |
| 916 |
time=1; |
| 917 |
prevState=MODE_DEL; |
| 918 |
}else{ |
| 919 |
score=scoreI; |
| 920 |
time=1; |
| 921 |
prevState=MODE_INS; |
| 922 |
} |
| 923 |
|
| 924 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 925 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 926 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 927 |
|
| 928 |
packed[MODE_MS][row][col]=(score|time); |
| 929 |
assert((score&SCOREMASK)==score); |
| 930 |
|
| 931 |
assert((time&TIMEMASK)==time); |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
{ |
| 937 |
|
| 938 |
final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK; |
| 939 |
|
| 940 |
final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; |
| 941 |
final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; |
| 942 |
|
| 943 |
int scoreMS=scoreFromDiag+POINTSoff_DEL; |
| 944 |
int scoreD=scoreFromDel+(streak==0 ? POINTSoff_DEL : |
| 945 |
streak<LIMIT_FOR_COST_3 ? POINTSoff_DEL2 : |
| 946 |
streak<LIMIT_FOR_COST_4 ? POINTSoff_DEL3 : |
| 947 |
streak<LIMIT_FOR_COST_5 ? POINTSoff_DEL4 : |
| 948 |
((streak&MASK5)==0 ? POINTSoff_DEL5 : 0)); |
| 949 |
|
| 950 |
|
| 951 |
int score; |
| 952 |
int time; |
| 953 |
byte prevState; |
| 954 |
if(scoreMS>=scoreD){ |
| 955 |
score=scoreMS; |
| 956 |
time=1; |
| 957 |
prevState=MODE_MS; |
| 958 |
}else{ |
| 959 |
score=scoreD; |
| 960 |
time=streak+1; |
| 961 |
prevState=MODE_DEL; |
| 962 |
} |
| 963 |
|
| 964 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 965 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 966 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 967 |
|
| 968 |
packed[MODE_DEL][row][col]=(score|time); |
| 969 |
assert((score&SCOREMASK)==score); |
| 970 |
|
| 971 |
assert((time&TIMEMASK)==time); |
| 972 |
} |
| 973 |
|
| 974 |
{ |
| 975 |
|
| 976 |
final int streak=packed[MODE_INS][row-1][col]&TIMEMASK; |
| 977 |
|
| 978 |
final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK; |
| 979 |
final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK; |
| 980 |
|
| 981 |
int scoreMS=scoreFromDiag+POINTSoff_INS; |
| 982 |
|
| 983 |
int scoreI=scoreFromIns+(streak==0 ? POINTSoff_INS : |
| 984 |
streak<LIMIT_FOR_COST_3 ? POINTSoff_INS2 : |
| 985 |
streak<LIMIT_FOR_COST_4 ? POINTSoff_INS3 : POINTSoff_INS4); |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
int score; |
| 993 |
int time; |
| 994 |
byte prevState; |
| 995 |
if(scoreMS>=scoreI){ |
| 996 |
score=scoreMS; |
| 997 |
time=1; |
| 998 |
prevState=MODE_MS; |
| 999 |
}else{ |
| 1000 |
score=scoreI; |
| 1001 |
time=streak+1; |
| 1002 |
prevState=MODE_INS; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if(time>MAX_TIME){time=MAX_TIME-MASK5;} |
| 1006 |
assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 1007 |
assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead"; |
| 1008 |
|
| 1009 |
packed[MODE_INS][row][col]=(score|time); |
| 1010 |
assert((score&SCOREMASK)==score); |
| 1011 |
|
| 1012 |
assert((time&TIMEMASK)==time); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
int maxCol=-1; |
| 1019 |
int maxState=-1; |
| 1020 |
int maxScore=Integer.MIN_VALUE; |
| 1021 |
|
| 1022 |
for(int state=0; state<packed.length; state++){ |
| 1023 |
for(int col=1; col<=columns; col++){ |
| 1024 |
int x=packed[state][rows][col]&SCOREMASK; |
| 1025 |
if(x>maxScore){ |
| 1026 |
maxScore=x; |
| 1027 |
maxCol=col; |
| 1028 |
maxState=state; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
maxScore>>=SCOREOFFSET; |
| 1033 |
|
| 1034 |
|
| 1035 |
return new int[] {rows, maxCol, maxState, maxScore}; |
| 1036 |
} |
| 1037 |
|
| 1038 |
@Override |
| 1039 |
|
| 1040 |
|
| 1041 |
public final byte[] traceback(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state, boolean gapped){ |
| 1042 |
if(gapped){ |
| 1043 |
final byte[] gref=grefbuffer; |
| 1044 |
int gstart=translateToGappedCoordinate(refStartLoc, gref); |
| 1045 |
int gstop=translateToGappedCoordinate(refEndLoc, gref); |
| 1046 |
byte[] out=traceback2(read, gref, gstart, gstop, row, col, state); |
| 1047 |
return out; |
| 1048 |
}else{ |
| 1049 |
return traceback2(read, ref, refStartLoc, refEndLoc, row, col, state); |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
@Override |
| 1054 |
|
| 1055 |
public final byte[] traceback2(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state){ |
| 1056 |
|
| 1057 |
assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc; |
| 1058 |
assert(row==rows); |
| 1059 |
|
| 1060 |
byte[] out=new byte[row+col-1]; |
| 1061 |
int outPos=0; |
| 1062 |
|
| 1063 |
int gaps=0; |
| 1064 |
|
| 1065 |
if(state==MODE_INS){ |
| 1066 |
|
| 1067 |
} |
| 1068 |
|
| 1069 |
while(row>0 && col>0){ |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
final int time=packed[state][row][col]&TIMEMASK; |
| 1074 |
final byte prev; |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
if(state==MODE_MS){ |
| 1079 |
if(time>1){prev=(byte)state;} |
| 1080 |
else{ |
| 1081 |
final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK; |
| 1082 |
final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK; |
| 1083 |
final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK; |
| 1084 |
if(scoreFromDiag>=scoreFromDel && scoreFromDiag>=scoreFromIns){prev=MODE_MS;} |
| 1085 |
else if(scoreFromDel>=scoreFromIns){prev=MODE_DEL;} |
| 1086 |
else{prev=MODE_INS;} |
| 1087 |
} |
| 1088 |
|
| 1089 |
byte c=read[row-1]; |
| 1090 |
byte r=ref[refStartLoc+col-1]; |
| 1091 |
if(c==r){ |
| 1092 |
out[outPos]='m'; |
| 1093 |
}else{ |
| 1094 |
if(!AminoAcid.isFullyDefined(c)){ |
| 1095 |
out[outPos]='N'; |
| 1096 |
}else if(!AminoAcid.isFullyDefined(r)){ |
| 1097 |
|
| 1098 |
out[outPos]='N'; |
| 1099 |
}else{ |
| 1100 |
out[outPos]='S'; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
row--; |
| 1105 |
col--; |
| 1106 |
}else if(state==MODE_DEL){ |
| 1107 |
if(time>1){prev=(byte)state;} |
| 1108 |
else{ |
| 1109 |
final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; |
| 1110 |
final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; |
| 1111 |
if(scoreFromDiag>=scoreFromDel){prev=MODE_MS;} |
| 1112 |
else{prev=MODE_DEL;} |
| 1113 |
} |
| 1114 |
|
| 1115 |
byte r=ref[refStartLoc+col-1]; |
| 1116 |
if(r==GAPC){ |
| 1117 |
out[outPos]='-'; |
| 1118 |
gaps++; |
| 1119 |
}else{ |
| 1120 |
out[outPos]='D'; |
| 1121 |
} |
| 1122 |
col--; |
| 1123 |
}else{ |
| 1124 |
if(time>1){prev=(byte)state;} |
| 1125 |
else{ |
| 1126 |
final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK; |
| 1127 |
final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK; |
| 1128 |
if(scoreFromDiag>=scoreFromIns){prev=MODE_MS;} |
| 1129 |
else{prev=MODE_INS;} |
| 1130 |
} |
| 1131 |
|
| 1132 |
assert(state==MODE_INS) : state; |
| 1133 |
if(col==0){ |
| 1134 |
out[outPos]='X'; |
| 1135 |
}else if(col>=columns){ |
| 1136 |
out[outPos]='Y'; |
| 1137 |
}else{ |
| 1138 |
out[outPos]='I'; |
| 1139 |
} |
| 1140 |
row--; |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
state=prev; |
| 1145 |
outPos++; |
| 1146 |
} |
| 1147 |
|
| 1148 |
assert(row==0 || col==0); |
| 1149 |
if(col!=row){ |
| 1150 |
while(row>0){ |
| 1151 |
out[outPos]='X'; |
| 1152 |
outPos++; |
| 1153 |
row--; |
| 1154 |
col--; |
| 1155 |
} |
| 1156 |
if(col>0){ |
| 1157 |
|
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
byte[] out2=new byte[outPos]; |
| 1164 |
for(int i=0; i<outPos; i++){ |
| 1165 |
out2[i]=out[outPos-i-1]; |
| 1166 |
} |
| 1167 |
out=null; |
| 1168 |
|
| 1169 |
if(gaps==0){return out2;} |
| 1170 |
|
| 1171 |
|
| 1172 |
byte[] out3=new byte[out2.length+gaps*(GAPLEN-1)]; |
| 1173 |
for(int i=0, j=0; i<out2.length; i++){ |
| 1174 |
byte c=out2[i]; |
| 1175 |
if(c!=GAPC){ |
| 1176 |
out3[j]=c; |
| 1177 |
j++; |
| 1178 |
}else{ |
| 1179 |
int lim=j+GAPLEN; |
| 1180 |
for(; j<lim; j++){ |
| 1181 |
out3[j]='D'; |
| 1182 |
} |
| 1183 |
} |
| 1184 |
} |
| 1185 |
return out3; |
| 1186 |
} |
| 1187 |
|
| 1188 |
@Override |
| 1189 |
|
| 1190 |
public final int[] score(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc, |
| 1191 |
final int maxRow, final int maxCol, final int maxState, boolean gapped){ |
| 1192 |
if(gapped){ |
| 1193 |
if(verbose){ |
| 1194 |
System.err.println("score():"); |
| 1195 |
System.err.println("origin="+grefRefOrigin+", "+refStartLoc+", "+refEndLoc+", "+maxRow+", "+maxCol); |
| 1196 |
} |
| 1197 |
final byte[] gref=grefbuffer; |
| 1198 |
int gstart=translateToGappedCoordinate(refStartLoc, gref); |
| 1199 |
int gstop=translateToGappedCoordinate(refEndLoc, gref); |
| 1200 |
|
| 1201 |
assert(translateFromGappedCoordinate(gstart, gref)==refStartLoc); |
| 1202 |
assert(translateFromGappedCoordinate(gstop, gref)==refEndLoc); |
| 1203 |
|
| 1204 |
assert(gstart==0) : gstart; |
| 1205 |
|
| 1206 |
if(verbose){System.err.println("gstart, gstop: "+gstart+", "+gstop);} |
| 1207 |
int[] out=score2(read, gref, gstart, gstop, maxRow, maxCol, maxState); |
| 1208 |
if(verbose){System.err.println("got score "+Arrays.toString(out));} |
| 1209 |
|
| 1210 |
assert(out[1]==translateToGappedCoordinate(translateFromGappedCoordinate(out[1], gref), gref)) : |
| 1211 |
"Verifying: "+out[1]+" -> "+translateFromGappedCoordinate(out[1], gref)+" -> "+ |
| 1212 |
translateToGappedCoordinate(translateFromGappedCoordinate(out[1], gref), gref); |
| 1213 |
assert(out[2]==translateToGappedCoordinate(translateFromGappedCoordinate(out[2], gref), gref)); |
| 1214 |
|
| 1215 |
out[1]=translateFromGappedCoordinate(out[1], gref); |
| 1216 |
out[2]=translateFromGappedCoordinate(out[2], gref); |
| 1217 |
if(verbose){System.err.println("returning score "+Arrays.toString(out));} |
| 1218 |
return out; |
| 1219 |
}else{ |
| 1220 |
return score2(read, ref, refStartLoc, refEndLoc, maxRow, maxCol, maxState); |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
@Override |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
public final int[] score2(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc, |
| 1229 |
final int maxRow, final int maxCol, final int maxState){ |
| 1230 |
|
| 1231 |
int row=maxRow; |
| 1232 |
int col=maxCol; |
| 1233 |
int state=maxState; |
| 1234 |
|
| 1235 |
assert(maxState>=0 && maxState<packed.length) : |
| 1236 |
maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc); |
| 1237 |
assert(maxRow>=0 && maxRow<packed[0].length) : |
| 1238 |
maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc); |
| 1239 |
assert(maxCol>=0 && maxCol<packed[0][0].length) : |
| 1240 |
maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc); |
| 1241 |
|
| 1242 |
int score=packed[maxState][maxRow][maxCol]&SCOREMASK; |
| 1243 |
|
| 1244 |
if(row<rows){ |
| 1245 |
int difR=rows-row; |
| 1246 |
int difC=columns-col; |
| 1247 |
|
| 1248 |
while(difR>difC){ |
| 1249 |
score+=POINTSoff_NOREF; |
| 1250 |
difR--; |
| 1251 |
} |
| 1252 |
|
| 1253 |
row+=difR; |
| 1254 |
col+=difR; |
| 1255 |
|
| 1256 |
} |
| 1257 |
|
| 1258 |
assert(refStartLoc<=refEndLoc); |
| 1259 |
assert(row==rows); |
| 1260 |
|
| 1261 |
|
| 1262 |
final int bestRefStop=refStartLoc+col-1; |
| 1263 |
|
| 1264 |
while(row>0 && col>0){ |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
final int time=packed[state][row][col]&TIMEMASK; |
| 1272 |
final byte prev; |
| 1273 |
|
| 1274 |
if(state==MODE_MS){ |
| 1275 |
if(time>1){prev=(byte)state;} |
| 1276 |
else{ |
| 1277 |
final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK; |
| 1278 |
final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK; |
| 1279 |
final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK; |
| 1280 |
if(scoreFromDiag>=scoreFromDel && scoreFromDiag>=scoreFromIns){prev=MODE_MS;} |
| 1281 |
else if(scoreFromDel>=scoreFromIns){prev=MODE_DEL;} |
| 1282 |
else{prev=MODE_INS;} |
| 1283 |
} |
| 1284 |
row--; |
| 1285 |
col--; |
| 1286 |
}else if(state==MODE_DEL){ |
| 1287 |
if(time>1){prev=(byte)state;} |
| 1288 |
else{ |
| 1289 |
final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK; |
| 1290 |
final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK; |
| 1291 |
if(scoreFromDiag>=scoreFromDel){prev=MODE_MS;} |
| 1292 |
else{prev=MODE_DEL;} |
| 1293 |
} |
| 1294 |
col--; |
| 1295 |
}else{ |
| 1296 |
assert(state==MODE_INS); |
| 1297 |
if(time>1){prev=(byte)state;} |
| 1298 |
else{ |
| 1299 |
final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK; |
| 1300 |
final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK; |
| 1301 |
if(scoreFromDiag>=scoreFromIns){prev=MODE_MS;} |
| 1302 |
else{prev=MODE_INS;} |
| 1303 |
} |
| 1304 |
row--; |
| 1305 |
} |
| 1306 |
|
| 1307 |
if(col<0){ |
| 1308 |
System.err.println(row); |
| 1309 |
break; |
| 1310 |
|
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
state=prev; |
| 1315 |
|
| 1316 |
|
| 1317 |
} |
| 1318 |
|
| 1319 |
if(row>col){ |
| 1320 |
col-=row; |
| 1321 |
} |
| 1322 |
|
| 1323 |
final int bestRefStart=refStartLoc+col; |
| 1324 |
|
| 1325 |
score>>=SCOREOFFSET; |
| 1326 |
int[] rvec; |
| 1327 |
if(bestRefStart<refStartLoc || bestRefStop>refEndLoc){ |
| 1328 |
int padLeft=Tools.max(0, refStartLoc-bestRefStart); |
| 1329 |
int padRight=Tools.max(0, bestRefStop-refEndLoc); |
| 1330 |
rvec=new int[] {score, bestRefStart, bestRefStop, maxRow, maxCol, maxState, padLeft, padRight}; |
| 1331 |
}else{ |
| 1332 |
rvec=new int[] {score, bestRefStart, bestRefStop, maxRow, maxCol, maxState}; |
| 1333 |
} |
| 1334 |
return rvec; |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
private final byte[] makeGref(byte[] ref, int[] gaps, int refStartLoc, int refEndLoc){ |
| 1346 |
assert(gaps!=null && gaps.length>0); |
| 1347 |
|
| 1348 |
assert(refStartLoc<=gaps[0]) : refStartLoc+", "+refEndLoc+", "+Arrays.toString(gaps); |
| 1349 |
assert(refEndLoc>=gaps[gaps.length-1]); |
| 1350 |
|
| 1351 |
final int g0_old=gaps[0]; |
| 1352 |
final int gN_old=gaps[gaps.length-1]; |
| 1353 |
gaps[0]=Tools.min(gaps[0], refStartLoc); |
| 1354 |
gaps[gaps.length-1]=Tools.max(gN_old, refEndLoc); |
| 1355 |
grefRefOrigin=gaps[0]; |
| 1356 |
|
| 1357 |
if(verbose){System.err.println("\ngaps2: "+Arrays.toString(gaps));} |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
byte[] gref=grefbuffer; |
| 1384 |
|
| 1385 |
int gpos=0; |
| 1386 |
for(int i=0; i<gaps.length; i+=2){ |
| 1387 |
int x=gaps[i]; |
| 1388 |
int y=gaps[i+1]; |
| 1389 |
|
| 1390 |
for(int r=x; r<=y; r++, gpos++){ |
| 1391 |
|
| 1392 |
|
| 1393 |
gref[gpos]=ref[r]; |
| 1394 |
} |
| 1395 |
|
| 1396 |
if(i+2<gaps.length){ |
| 1397 |
int z=gaps[i+2]; |
| 1398 |
assert(z>y); |
| 1399 |
int gap=z-y-1; |
| 1400 |
assert(gap>=MINGAP) : gap+"\t"+MINGAP; |
| 1401 |
if(gap<MINGAP){ |
| 1402 |
assert(false) : "TODO - just fill in normally"; |
| 1403 |
}else{ |
| 1404 |
int rem=gap%GAPLEN; |
| 1405 |
int lim=y+GAPBUFFER+rem; |
| 1406 |
|
| 1407 |
int div=(gap-GAPBUFFER2)/GAPLEN; |
| 1408 |
if(verbose){ |
| 1409 |
System.err.println("div = "+div); |
| 1410 |
} |
| 1411 |
assert(div>0); |
| 1412 |
|
| 1413 |
for(int r=y+1; r<=lim; r++, gpos++){ |
| 1414 |
gref[gpos]=ref[r]; |
| 1415 |
} |
| 1416 |
for(int g=0; g<div; g++, gpos++){ |
| 1417 |
gref[gpos]=GAPC; |
| 1418 |
} |
| 1419 |
for(int r=z-GAPBUFFER; r<z; r++, gpos++){ |
| 1420 |
gref[gpos]=ref[r]; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
greflimit=gpos; |
| 1427 |
|
| 1428 |
assert(gref[gpos-1]==ref[refEndLoc]); |
| 1429 |
|
| 1430 |
|
| 1431 |
{ |
| 1432 |
final int lim=Tools.min(gref.length, greflimit+GREFLIMIT2_CUSHION); |
| 1433 |
if(lim>gref.length){ |
| 1434 |
System.err.println("gref buffer overflow: "+lim+" > "+gref.length); |
| 1435 |
return null; |
| 1436 |
} |
| 1437 |
for(int i=greflimit, r=refEndLoc+1; i<lim; i++, r++){ |
| 1438 |
gref[i]=(r<ref.length ? ref[r] : (byte)'N'); |
| 1439 |
greflimit2=i; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
if(verbose){ |
| 1444 |
System.err.println("gref:\n"+new String(gref)); |
| 1445 |
} |
| 1446 |
|
| 1447 |
gaps[0]=g0_old; |
| 1448 |
gaps[gaps.length-1]=gN_old; |
| 1449 |
|
| 1450 |
if(verbose){ |
| 1451 |
System.err.println("\ngaps3: "+Arrays.toString(gaps)); |
| 1452 |
} |
| 1453 |
|
| 1454 |
return gref; |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
private final int translateFromGappedCoordinate(int point, byte[] gref){ |
| 1484 |
if(verbose){System.err.println("translateFromGappedCoordinate("+point+"), gro="+grefRefOrigin+", grl="+greflimit);} |
| 1485 |
if(point<=0){return grefRefOrigin+point;} |
| 1486 |
for(int i=0, j=grefRefOrigin; i<greflimit2; i++){ |
| 1487 |
|
| 1488 |
byte c=gref[i]; |
| 1489 |
assert(point>=i) : "\n"+grefRefOrigin+"\n"+point+"\n"+new String(gref)+"\n"; |
| 1490 |
|
| 1491 |
if(i==point){ |
| 1492 |
if(verbose){System.err.println(" -> "+j);} |
| 1493 |
return j; |
| 1494 |
} |
| 1495 |
|
| 1496 |
j+=(c==GAPC ? GAPLEN : 1); |
| 1497 |
|
| 1498 |
|
| 1499 |
} |
| 1500 |
|
| 1501 |
System.err.println(grefRefOrigin); |
| 1502 |
System.err.println(point); |
| 1503 |
System.err.println(new String(gref)); |
| 1504 |
|
| 1505 |
throw new RuntimeException("Out of bounds."); |
| 1506 |
} |
| 1507 |
|
| 1508 |
private final int translateToGappedCoordinate(int point, byte[] gref){ |
| 1509 |
if(verbose){System.err.println("translateToGappedCoordinate("+point+"), gro="+grefRefOrigin+", grl="+greflimit);} |
| 1510 |
if(point<=grefRefOrigin){return point-grefRefOrigin;} |
| 1511 |
for(int i=0, j=grefRefOrigin; i<greflimit2; i++){ |
| 1512 |
|
| 1513 |
assert(point>=j) : "\n"+grefRefOrigin+"\n"+point+"\n"+new String(gref)+"\n"; |
| 1514 |
byte c=gref[i]; |
| 1515 |
|
| 1516 |
if(j==point){ |
| 1517 |
if(verbose){System.err.println(" -> "+i);} |
| 1518 |
return i; |
| 1519 |
} |
| 1520 |
|
| 1521 |
j+=(c==GAPC ? GAPLEN : 1); |
| 1522 |
|
| 1523 |
|
| 1524 |
} |
| 1525 |
|
| 1526 |
System.err.println(grefRefOrigin); |
| 1527 |
System.err.println(point); |
| 1528 |
System.err.println(new String(gref)); |
| 1529 |
|
| 1530 |
throw new RuntimeException("Out of bounds."); |
| 1531 |
} |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
private final int calcAffineScore(int[] locArray){ |
| 1536 |
int score=0; |
| 1537 |
int lastLoc=-2; |
| 1538 |
int lastValue=-1; |
| 1539 |
int timeInMode=0; |
| 1540 |
|
| 1541 |
for(int i=0; i<locArray.length; i++){ |
| 1542 |
int loc=locArray[i]; |
| 1543 |
|
| 1544 |
if(loc>0){ |
| 1545 |
if(loc==lastValue){ |
| 1546 |
score+=POINTS_MATCH2; |
| 1547 |
}else if(loc==lastLoc || lastLoc<0){ |
| 1548 |
score+=POINTS_MATCH; |
| 1549 |
}else if(loc<lastLoc){ |
| 1550 |
assert(lastLoc>=0); |
| 1551 |
score+=POINTS_MATCH; |
| 1552 |
score+=POINTS_DEL; |
| 1553 |
int dif=lastLoc-loc+1; |
| 1554 |
if(dif>MINGAP){ |
| 1555 |
int rem=dif%GAPLEN; |
| 1556 |
int div=(dif-GAPBUFFER2)/GAPLEN; |
| 1557 |
score+=(div*POINTS_GAP); |
| 1558 |
assert(rem+GAPBUFFER2<dif); |
| 1559 |
dif=rem+GAPBUFFER2; |
| 1560 |
assert(dif>LIMIT_FOR_COST_4); |
| 1561 |
|
| 1562 |
} |
| 1563 |
if(dif>LIMIT_FOR_COST_5){ |
| 1564 |
score+=((dif-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTS_DEL5; |
| 1565 |
dif=LIMIT_FOR_COST_5; |
| 1566 |
} |
| 1567 |
if(dif>LIMIT_FOR_COST_4){ |
| 1568 |
score+=(dif-LIMIT_FOR_COST_4)*POINTS_DEL4; |
| 1569 |
dif=LIMIT_FOR_COST_4; |
| 1570 |
} |
| 1571 |
if(dif>LIMIT_FOR_COST_3){ |
| 1572 |
score+=(dif-LIMIT_FOR_COST_3)*POINTS_DEL3; |
| 1573 |
dif=LIMIT_FOR_COST_3; |
| 1574 |
} |
| 1575 |
if(dif>1){ |
| 1576 |
score+=(dif-1)*POINTS_DEL2; |
| 1577 |
} |
| 1578 |
timeInMode=1; |
| 1579 |
}else if(loc>lastLoc){ |
| 1580 |
assert(lastLoc>=0); |
| 1581 |
score+=POINTS_MATCH; |
| 1582 |
score+=POINTS_INS; |
| 1583 |
int dif=Tools.min(loc-lastLoc+1, 5); |
| 1584 |
assert(dif>0); |
| 1585 |
if(dif>LIMIT_FOR_COST_4){ |
| 1586 |
score+=(dif-LIMIT_FOR_COST_4)*POINTS_INS4; |
| 1587 |
dif=LIMIT_FOR_COST_4; |
| 1588 |
} |
| 1589 |
if(dif>LIMIT_FOR_COST_3){ |
| 1590 |
score+=(dif-LIMIT_FOR_COST_3)*POINTS_INS3; |
| 1591 |
dif=LIMIT_FOR_COST_3; |
| 1592 |
} |
| 1593 |
if(dif>1){ |
| 1594 |
score+=(dif-1)*POINTS_INS2; |
| 1595 |
} |
| 1596 |
timeInMode=1; |
| 1597 |
}else{ |
| 1598 |
assert(false); |
| 1599 |
} |
| 1600 |
lastLoc=loc; |
| 1601 |
}else{ |
| 1602 |
if(lastValue<0 && timeInMode>0){ |
| 1603 |
if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;} |
| 1604 |
else{score+=POINTS_SUB3;} |
| 1605 |
timeInMode++; |
| 1606 |
}else{ |
| 1607 |
score+=POINTS_SUB; |
| 1608 |
timeInMode=1; |
| 1609 |
} |
| 1610 |
} |
| 1611 |
lastValue=loc; |
| 1612 |
} |
| 1613 |
assert(score<=maxQuality(locArray.length)); |
| 1614 |
return score; |
| 1615 |
} |
| 1616 |
|
| 1617 |
@Override |
| 1618 |
public final int calcAffineScore(final int[] locArray, final byte[] baseScores, final byte bases[]){ |
| 1619 |
int score=0; |
| 1620 |
int lastLoc=-3; |
| 1621 |
int lastValue=-1; |
| 1622 |
int timeInMode=0; |
| 1623 |
|
| 1624 |
for(int i=0; i<locArray.length; i++){ |
| 1625 |
final int loc=locArray[i]; |
| 1626 |
|
| 1627 |
if(loc>0){ |
| 1628 |
if(loc==lastValue){ |
| 1629 |
score+=(POINTS_MATCH2+baseScores[i]); |
| 1630 |
}else if(loc==lastLoc || lastLoc<0){ |
| 1631 |
score+=(POINTS_MATCH+baseScores[i]); |
| 1632 |
}else if(loc<lastLoc){ |
| 1633 |
assert(lastLoc>=0); |
| 1634 |
score+=(POINTS_MATCH+baseScores[i]); |
| 1635 |
score+=POINTS_DEL; |
| 1636 |
int dif=lastLoc-loc+1; |
| 1637 |
if(dif>MINGAP){ |
| 1638 |
int rem=dif%GAPLEN; |
| 1639 |
int div=(dif-GAPBUFFER2)/GAPLEN; |
| 1640 |
score+=(div*POINTS_GAP); |
| 1641 |
assert(rem+GAPBUFFER2<dif); |
| 1642 |
dif=rem+GAPBUFFER2; |
| 1643 |
assert(dif>LIMIT_FOR_COST_4); |
| 1644 |
|
| 1645 |
} |
| 1646 |
if(dif>LIMIT_FOR_COST_5){ |
| 1647 |
score+=((dif-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTS_DEL5; |
| 1648 |
dif=LIMIT_FOR_COST_5; |
| 1649 |
} |
| 1650 |
if(dif>LIMIT_FOR_COST_4){ |
| 1651 |
score+=(dif-LIMIT_FOR_COST_4)*POINTS_DEL4; |
| 1652 |
dif=LIMIT_FOR_COST_4; |
| 1653 |
} |
| 1654 |
if(dif>LIMIT_FOR_COST_3){ |
| 1655 |
score+=(dif-LIMIT_FOR_COST_3)*POINTS_DEL3; |
| 1656 |
dif=LIMIT_FOR_COST_3; |
| 1657 |
} |
| 1658 |
if(dif>1){ |
| 1659 |
score+=(dif-1)*POINTS_DEL2; |
| 1660 |
} |
| 1661 |
timeInMode=1; |
| 1662 |
}else if(loc>lastLoc){ |
| 1663 |
assert(lastLoc>=0); |
| 1664 |
score+=(POINTS_MATCH+baseScores[i]); |
| 1665 |
score+=POINTS_INS; |
| 1666 |
int dif=Tools.min(loc-lastLoc+1, 5); |
| 1667 |
assert(dif>0); |
| 1668 |
if(dif>LIMIT_FOR_COST_4){ |
| 1669 |
score+=(dif-LIMIT_FOR_COST_4)*POINTS_INS4; |
| 1670 |
dif=LIMIT_FOR_COST_4; |
| 1671 |
} |
| 1672 |
if(dif>LIMIT_FOR_COST_3){ |
| 1673 |
score+=(dif-LIMIT_FOR_COST_3)*POINTS_INS3; |
| 1674 |
dif=LIMIT_FOR_COST_3; |
| 1675 |
} |
| 1676 |
if(dif>1){ |
| 1677 |
score+=(dif-1)*POINTS_INS2; |
| 1678 |
} |
| 1679 |
timeInMode=1; |
| 1680 |
}else{ |
| 1681 |
assert(false); |
| 1682 |
} |
| 1683 |
lastLoc=loc; |
| 1684 |
}else if(loc==-1){ |
| 1685 |
if(lastValue<0 && timeInMode>0){ |
| 1686 |
if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;} |
| 1687 |
else{score+=POINTS_SUB3;} |
| 1688 |
timeInMode++; |
| 1689 |
}else{ |
| 1690 |
score+=POINTS_SUB; |
| 1691 |
timeInMode=1; |
| 1692 |
} |
| 1693 |
}else{ |
| 1694 |
assert(loc==-2) : "\ni="+i+", loc="+loc+", score="+score+", lastLoc="+lastLoc+", lastValue="+lastValue |
| 1695 |
+", time="+timeInMode+", length="+locArray.length+"\nbases=\n"+new String(bases) |
| 1696 |
+"\nlocs[]=\n"+Arrays.toString(locArray)+"\n"+new String(bases)+"\n" |
| 1697 |
+"If this happens please ensure that the reference has a startpad of Ns longer than readlength."; |
| 1698 |
timeInMode=0; |
| 1699 |
score+=POINTS_NOCALL; |
| 1700 |
} |
| 1701 |
lastValue=loc; |
| 1702 |
} |
| 1703 |
assert(score<=maxQuality(locArray.length)); |
| 1704 |
return score; |
| 1705 |
} |
| 1706 |
|
| 1707 |
@Override |
| 1708 |
public final int calcAffineScore(int[] locArray, byte[] baseScores, byte[] bases, int minContig){ |
| 1709 |
assert(minContig>1) : minContig; |
| 1710 |
|
| 1711 |
int contig=0; |
| 1712 |
int maxContig=0; |
| 1713 |
|
| 1714 |
int score=0; |
| 1715 |
int lastLoc=-3; |
| 1716 |
int lastValue=-1; |
| 1717 |
int timeInMode=0; |
| 1718 |
|
| 1719 |
for(int i=0; i<locArray.length; i++){ |
| 1720 |
int loc=locArray[i]; |
| 1721 |
|
| 1722 |
if(loc>0){ |
| 1723 |
if(loc==lastValue){ |
| 1724 |
contig++; |
| 1725 |
score+=(POINTS_MATCH2+baseScores[i]); |
| 1726 |
}else if(loc==lastLoc || lastLoc<0){ |
| 1727 |
maxContig=Tools.max(maxContig, contig); |
| 1728 |
contig=1; |
| 1729 |
score+=(POINTS_MATCH+baseScores[i]); |
| 1730 |
}else if(loc<lastLoc){ |
| 1731 |
maxContig=Tools.max(maxContig, contig); |
| 1732 |
contig=0; |
| 1733 |
assert(lastLoc>=0); |
| 1734 |
score+=(POINTS_MATCH+baseScores[i]); |
| 1735 |
score+=POINTS_DEL; |
| 1736 |
int dif=lastLoc-loc+1; |
| 1737 |
if(dif>MINGAP){ |
| 1738 |
int rem=dif%GAPLEN; |
| 1739 |
int div=(dif-GAPBUFFER2)/GAPLEN; |
| 1740 |
score+=(div*POINTS_GAP); |
| 1741 |
assert(rem+GAPBUFFER2<dif); |
| 1742 |
dif=rem+GAPBUFFER2; |
| 1743 |
assert(dif>LIMIT_FOR_COST_4); |
| 1744 |
|
| 1745 |
} |
| 1746 |
if(dif>LIMIT_FOR_COST_5){ |
| 1747 |
score+=((dif-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTS_DEL5; |
| 1748 |
dif=LIMIT_FOR_COST_5; |
| 1749 |
} |
| 1750 |
if(dif>LIMIT_FOR_COST_4){ |
| 1751 |
score+=(dif-LIMIT_FOR_COST_4)*POINTS_DEL4; |
| 1752 |
dif=LIMIT_FOR_COST_4; |
| 1753 |
} |
| 1754 |
if(dif>LIMIT_FOR_COST_3){ |
| 1755 |
score+=(dif-LIMIT_FOR_COST_3)*POINTS_DEL3; |
| 1756 |
dif=LIMIT_FOR_COST_3; |
| 1757 |
} |
| 1758 |
if(dif>1){ |
| 1759 |
score+=(dif-1)*POINTS_DEL2; |
| 1760 |
} |
| 1761 |
timeInMode=1; |
| 1762 |
}else if(loc>lastLoc){ |
| 1763 |
maxContig=Tools.max(maxContig, contig); |
| 1764 |
contig=0; |
| 1765 |
assert(lastLoc>=0); |
| 1766 |
score+=(POINTS_MATCH+baseScores[i]); |
| 1767 |
score+=POINTS_INS; |
| 1768 |
int dif=Tools.min(loc-lastLoc+1, 5); |
| 1769 |
assert(dif>0); |
| 1770 |
if(dif>LIMIT_FOR_COST_4){ |
| 1771 |
score+=(dif-LIMIT_FOR_COST_4)*POINTS_INS4; |
| 1772 |
dif=LIMIT_FOR_COST_4; |
| 1773 |
} |
| 1774 |
if(dif>LIMIT_FOR_COST_3){ |
| 1775 |
score+=(dif-LIMIT_FOR_COST_3)*POINTS_INS3; |
| 1776 |
dif=LIMIT_FOR_COST_3; |
| 1777 |
} |
| 1778 |
if(dif>1){ |
| 1779 |
score+=(dif-1)*POINTS_INS2; |
| 1780 |
} |
| 1781 |
timeInMode=1; |
| 1782 |
}else{ |
| 1783 |
assert(false); |
| 1784 |
} |
| 1785 |
lastLoc=loc; |
| 1786 |
}else if(loc==-1){ |
| 1787 |
if(lastValue<0 && timeInMode>0){ |
| 1788 |
if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;} |
| 1789 |
else{score+=POINTS_SUB3;} |
| 1790 |
timeInMode++; |
| 1791 |
}else{ |
| 1792 |
score+=POINTS_SUB; |
| 1793 |
timeInMode=1; |
| 1794 |
} |
| 1795 |
}else{ |
| 1796 |
assert(loc==-2) : loc+"\n"+Arrays.toString(locArray)+"\n"+Arrays.toString(baseScores)+"\n"+new String(bases)+"\n" |
| 1797 |
+"If this happens please ensure that the reference has a startpad of Ns longer than readlength."; |
| 1798 |
timeInMode=0; |
| 1799 |
score+=POINTS_NOCALL; |
| 1800 |
} |
| 1801 |
lastValue=loc; |
| 1802 |
} |
| 1803 |
assert(score<=maxQuality(locArray.length)); |
| 1804 |
if(Tools.max(contig, maxContig)<minContig){score=Tools.min(score, -50*locArray.length);} |
| 1805 |
return score; |
| 1806 |
} |
| 1807 |
|
| 1808 |
@Override |
| 1809 |
public final int scoreNoIndels(byte[] read, byte[] ref, final int refStart){ |
| 1810 |
return scoreNoIndels(read, ref, refStart, null); |
| 1811 |
} |
| 1812 |
@Override |
| 1813 |
public final int scoreNoIndels(byte[] read, byte[] ref, final int refStart, final SiteScore ss){ |
| 1814 |
|
| 1815 |
int score=0; |
| 1816 |
int mode=-1; |
| 1817 |
int timeInMode=0; |
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
int readStart=0; |
| 1822 |
int readStop=read.length; |
| 1823 |
final int refStop=refStart+read.length; |
| 1824 |
boolean semiperfect=true; |
| 1825 |
int norefs=0; |
| 1826 |
|
| 1827 |
if(refStart<0){ |
| 1828 |
readStart=0-refStart; |
| 1829 |
score+=POINTS_NOREF*readStart; |
| 1830 |
norefs+=readStart; |
| 1831 |
} |
| 1832 |
if(refStop>ref.length){ |
| 1833 |
int dif=(refStop-ref.length); |
| 1834 |
readStop-=dif; |
| 1835 |
score+=POINTS_NOREF*dif; |
| 1836 |
norefs+=dif; |
| 1837 |
} |
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
for(int i=readStart; i<readStop; i++){ |
| 1842 |
byte c=read[i]; |
| 1843 |
byte r=ref[refStart+i]; |
| 1844 |
|
| 1845 |
if(c==r && c!='N'){ |
| 1846 |
if(mode==MODE_MS){ |
| 1847 |
timeInMode++; |
| 1848 |
score+=POINTS_MATCH2; |
| 1849 |
}else{ |
| 1850 |
timeInMode=0; |
| 1851 |
score+=POINTS_MATCH; |
| 1852 |
} |
| 1853 |
mode=MODE_MS; |
| 1854 |
}else if(c<0 || c=='N'){ |
| 1855 |
score+=POINTS_NOCALL; |
| 1856 |
semiperfect=false; |
| 1857 |
}else if(r<0 || r=='N'){ |
| 1858 |
score+=POINTS_NOREF; |
| 1859 |
norefs++; |
| 1860 |
}else{ |
| 1861 |
if(mode==MODE_SUB){timeInMode++;} |
| 1862 |
else{timeInMode=0;} |
| 1863 |
|
| 1864 |
if(timeInMode==0){score+=POINTS_SUB;} |
| 1865 |
else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;} |
| 1866 |
else{score+=POINTS_SUB3;} |
| 1867 |
mode=MODE_SUB; |
| 1868 |
semiperfect=false; |
| 1869 |
} |
| 1870 |
} |
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
return score; |
| 1876 |
} |
| 1877 |
|
| 1878 |
@Override |
| 1879 |
public final byte[] genMatchNoIndels(byte[] read, byte[] ref, final int refStart){ |
| 1880 |
if(read==null || ref==null){return null;} |
| 1881 |
|
| 1882 |
final byte[] match=new byte[read.length]; |
| 1883 |
|
| 1884 |
for(int i=0, j=refStart; i<read.length; i++, j++){ |
| 1885 |
byte c=read[i]; |
| 1886 |
byte r=(j<0 || j>=ref.length) ? (byte)'N' : ref[j]; |
| 1887 |
|
| 1888 |
if(c=='N' || r=='N'){match[i]='N';} |
| 1889 |
else if(c==r){match[i]='m';} |
| 1890 |
else{match[i]='S';} |
| 1891 |
|
| 1892 |
} |
| 1893 |
|
| 1894 |
return match; |
| 1895 |
} |
| 1896 |
|
| 1897 |
@Override |
| 1898 |
public final int scoreNoIndels(byte[] read, byte[] ref, byte[] baseScores, final int refStart){ |
| 1899 |
return scoreNoIndels(read, ref, baseScores, refStart, null); |
| 1900 |
} |
| 1901 |
@Override |
| 1902 |
public final int scoreNoIndels(byte[] read, byte[] ref, byte[] baseScores, final int refStart, SiteScore ss){ |
| 1903 |
|
| 1904 |
int score=0; |
| 1905 |
int mode=-1; |
| 1906 |
int timeInMode=0; |
| 1907 |
int norefs=0; |
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
int readStart=0; |
| 1912 |
int readStop=read.length; |
| 1913 |
final int refStop=refStart+read.length; |
| 1914 |
boolean semiperfect=true; |
| 1915 |
|
| 1916 |
if(refStart<0){ |
| 1917 |
readStart=0-refStart; |
| 1918 |
score+=POINTS_NOREF*readStart; |
| 1919 |
norefs+=readStart; |
| 1920 |
} |
| 1921 |
if(refStop>ref.length){ |
| 1922 |
int dif=(refStop-ref.length); |
| 1923 |
readStop-=dif; |
| 1924 |
score+=POINTS_NOREF*dif; |
| 1925 |
norefs+=dif; |
| 1926 |
} |
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
for(int i=readStart; i<readStop; i++){ |
| 1931 |
byte c=read[i]; |
| 1932 |
byte r=ref[refStart+i]; |
| 1933 |
|
| 1934 |
if(c==r && c!='N'){ |
| 1935 |
if(mode==MODE_MS){ |
| 1936 |
timeInMode++; |
| 1937 |
score+=POINTS_MATCH2; |
| 1938 |
}else{ |
| 1939 |
timeInMode=0; |
| 1940 |
score+=POINTS_MATCH; |
| 1941 |
} |
| 1942 |
score+=baseScores[i]; |
| 1943 |
mode=MODE_MS; |
| 1944 |
}else if(c<0 || c=='N'){ |
| 1945 |
score+=POINTS_NOCALL; |
| 1946 |
semiperfect=false; |
| 1947 |
}else if(r<0 || r=='N'){ |
| 1948 |
score+=POINTS_NOREF; |
| 1949 |
norefs++; |
| 1950 |
}else{ |
| 1951 |
if(mode==MODE_SUB){timeInMode++;} |
| 1952 |
else{timeInMode=0;} |
| 1953 |
|
| 1954 |
if(timeInMode==0){score+=POINTS_SUB;} |
| 1955 |
else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;} |
| 1956 |
else{score+=POINTS_SUB3;} |
| 1957 |
mode=MODE_SUB; |
| 1958 |
semiperfect=false; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
return score; |
| 1967 |
} |
| 1968 |
|
| 1969 |
@Override |
| 1970 |
public final int scoreNoIndelsAndMakeMatchString(byte[] read, byte[] ref, byte[] baseScores, final int refStart, byte[][] matchReturn){ |
| 1971 |
int score=0; |
| 1972 |
int mode=-1; |
| 1973 |
int timeInMode=0; |
| 1974 |
|
| 1975 |
assert(refStart<=ref.length) : refStart+", "+ref.length; |
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
int readStart=0; |
| 1980 |
int readStop=read.length; |
| 1981 |
final int refStop=refStart+read.length; |
| 1982 |
if(refStart<0 || refStop>ref.length){return -99999;} |
| 1983 |
if(refStart<0){ |
| 1984 |
readStart=0-refStart; |
| 1985 |
score+=POINTS_NOREF*readStart; |
| 1986 |
} |
| 1987 |
if(refStop>ref.length){ |
| 1988 |
int dif=(refStop-ref.length); |
| 1989 |
System.err.println(new String(read)+"\ndif="+dif+", ref.length="+ref.length+", refStop="+refStop); |
| 1990 |
readStop-=dif; |
| 1991 |
score+=POINTS_NOREF*dif; |
| 1992 |
} |
| 1993 |
assert(refStart+readStop<=ref.length) : "readStart="+readStart+", readStop="+readStop+ |
| 1994 |
", refStart="+refStart+", refStop="+refStop+", ref.length="+ref.length+", read.length="+read.length; |
| 1995 |
|
| 1996 |
assert(matchReturn!=null); |
| 1997 |
assert(matchReturn.length==1); |
| 1998 |
if(matchReturn[0]==null || matchReturn[0].length!=read.length){ |
| 1999 |
assert(matchReturn[0]==null || matchReturn[0].length<read.length) : matchReturn[0].length+"!="+read.length; |
| 2000 |
matchReturn[0]=new byte[read.length]; |
| 2001 |
} |
| 2002 |
final byte[] match=matchReturn[0]; |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
for(int i=readStart; i<readStop; i++){ |
| 2007 |
byte c=read[i]; |
| 2008 |
byte r=ref[refStart+i]; |
| 2009 |
|
| 2010 |
assert(r!='.' && c!='.'); |
| 2011 |
|
| 2012 |
if(c==r && c!='N'){ |
| 2013 |
if(mode==MODE_MS){ |
| 2014 |
timeInMode++; |
| 2015 |
score+=POINTS_MATCH2; |
| 2016 |
}else{ |
| 2017 |
timeInMode=0; |
| 2018 |
score+=POINTS_MATCH; |
| 2019 |
} |
| 2020 |
score+=baseScores[i]; |
| 2021 |
match[i]='m'; |
| 2022 |
mode=MODE_MS; |
| 2023 |
}else if(c<0 || c=='N'){ |
| 2024 |
score+=POINTS_NOCALL; |
| 2025 |
match[i]='N'; |
| 2026 |
}else if(r<0 || r=='N'){ |
| 2027 |
score+=POINTS_NOREF; |
| 2028 |
|
| 2029 |
match[i]='N'; |
| 2030 |
}else{ |
| 2031 |
match[i]='S'; |
| 2032 |
if(mode==MODE_SUB){timeInMode++;} |
| 2033 |
else{timeInMode=0;} |
| 2034 |
|
| 2035 |
if(timeInMode==0){score+=POINTS_SUB;} |
| 2036 |
else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;} |
| 2037 |
else{score+=POINTS_SUB3;} |
| 2038 |
mode=MODE_SUB; |
| 2039 |
} |
| 2040 |
} |
| 2041 |
|
| 2042 |
return score; |
| 2043 |
} |
| 2044 |
|
| 2045 |
@Override |
| 2046 |
public final int scoreNoIndelsAndMakeMatchString(byte[] read, byte[] ref, final int refStart, byte[][] matchReturn){ |
| 2047 |
int score=0; |
| 2048 |
int mode=-1; |
| 2049 |
int timeInMode=0; |
| 2050 |
|
| 2051 |
assert(refStart<=ref.length) : refStart+", "+ref.length; |
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
int readStart=0; |
| 2056 |
int readStop=read.length; |
| 2057 |
final int refStop=refStart+read.length; |
| 2058 |
if(refStart<0 || refStop>ref.length){return -99999;} |
| 2059 |
if(refStart<0){ |
| 2060 |
readStart=0-refStart; |
| 2061 |
score+=POINTS_NOREF*readStart; |
| 2062 |
} |
| 2063 |
if(refStop>ref.length){ |
| 2064 |
int dif=(refStop-ref.length); |
| 2065 |
System.err.println(new String(read)+"\ndif="+dif+", ref.length="+ref.length+", refStop="+refStop); |
| 2066 |
readStop-=dif; |
| 2067 |
score+=POINTS_NOREF*dif; |
| 2068 |
} |
| 2069 |
assert(refStart+readStop<=ref.length) : "readStart="+readStart+", readStop="+readStop+ |
| 2070 |
", refStart="+refStart+", refStop="+refStop+", ref.length="+ref.length+", read.length="+read.length; |
| 2071 |
|
| 2072 |
assert(matchReturn!=null); |
| 2073 |
assert(matchReturn.length==1); |
| 2074 |
if(matchReturn[0]==null || matchReturn[0].length!=read.length){ |
| 2075 |
assert(matchReturn[0]==null || matchReturn[0].length<read.length) : matchReturn[0].length+"!="+read.length; |
| 2076 |
matchReturn[0]=new byte[read.length]; |
| 2077 |
} |
| 2078 |
final byte[] match=matchReturn[0]; |
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
for(int i=readStart; i<readStop; i++){ |
| 2083 |
byte c=read[i]; |
| 2084 |
byte r=ref[refStart+i]; |
| 2085 |
|
| 2086 |
assert(r!='.' && c!='.'); |
| 2087 |
|
| 2088 |
if(c==r && c!='N'){ |
| 2089 |
if(mode==MODE_MS){ |
| 2090 |
timeInMode++; |
| 2091 |
score+=POINTS_MATCH2; |
| 2092 |
}else{ |
| 2093 |
timeInMode=0; |
| 2094 |
score+=POINTS_MATCH; |
| 2095 |
} |
| 2096 |
match[i]='m'; |
| 2097 |
mode=MODE_MS; |
| 2098 |
}else if(c<0 || c=='N'){ |
| 2099 |
score+=POINTS_NOCALL; |
| 2100 |
match[i]='N'; |
| 2101 |
}else if(r<0 || r=='N'){ |
| 2102 |
score+=POINTS_NOREF; |
| 2103 |
|
| 2104 |
match[i]='N'; |
| 2105 |
}else{ |
| 2106 |
match[i]='S'; |
| 2107 |
if(mode==MODE_SUB){timeInMode++;} |
| 2108 |
else{timeInMode=0;} |
| 2109 |
|
| 2110 |
if(timeInMode==0){score+=POINTS_SUB;} |
| 2111 |
else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;} |
| 2112 |
else{score+=POINTS_SUB3;} |
| 2113 |
mode=MODE_SUB; |
| 2114 |
} |
| 2115 |
} |
| 2116 |
|
| 2117 |
return score; |
| 2118 |
} |
| 2119 |
|
| 2120 |
@Override |
| 2121 |
public final int maxQuality(int numBases){ |
| 2122 |
return POINTS_MATCH+(numBases-1)*(POINTS_MATCH2); |
| 2123 |
} |
| 2124 |
|
| 2125 |
@Override |
| 2126 |
public final int maxQuality(byte[] baseScores){ |
| 2127 |
return POINTS_MATCH+(baseScores.length-1)*(POINTS_MATCH2)+Tools.sumInt(baseScores); |
| 2128 |
} |
| 2129 |
|
| 2130 |
@Override |
| 2131 |
public final int maxImperfectScore(int numBases){ |
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
int maxQ=maxQuality(numBases); |
| 2139 |
int maxI=maxQ+Tools.min(POINTS_DEL, POINTS_INS-POINTS_MATCH2); |
| 2140 |
assert(maxI<(maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB))); |
| 2141 |
return maxI; |
| 2142 |
} |
| 2143 |
|
| 2144 |
@Override |
| 2145 |
public final int maxImperfectScore(byte[] baseScores){ |
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
int maxQ=maxQuality(baseScores); |
| 2153 |
int maxI=maxQ+Tools.min(POINTS_DEL, POINTS_INS-POINTS_MATCH2); |
| 2154 |
assert(maxI<(maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB))); |
| 2155 |
return maxI; |
| 2156 |
} |
| 2157 |
|
| 2158 |
@Override |
| 2159 |
public int calcDelScore(int len, boolean approximateGaps){ |
| 2160 |
if(len<=0){return 0;} |
| 2161 |
int score=POINTS_DEL; |
| 2162 |
|
| 2163 |
if(approximateGaps && len>MINGAP){ |
| 2164 |
int rem=len%GAPLEN; |
| 2165 |
int div=(len-GAPBUFFER2)/GAPLEN; |
| 2166 |
score+=(div*POINTS_GAP); |
| 2167 |
assert(rem+GAPBUFFER2<len); |
| 2168 |
len=rem+GAPBUFFER2; |
| 2169 |
assert(len>LIMIT_FOR_COST_4); |
| 2170 |
|
| 2171 |
} |
| 2172 |
|
| 2173 |
if(len>LIMIT_FOR_COST_5){ |
| 2174 |
score+=((len-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTS_DEL5; |
| 2175 |
len=LIMIT_FOR_COST_5; |
| 2176 |
} |
| 2177 |
if(len>LIMIT_FOR_COST_4){ |
| 2178 |
score+=(len-LIMIT_FOR_COST_4)*POINTS_DEL4; |
| 2179 |
len=LIMIT_FOR_COST_4; |
| 2180 |
} |
| 2181 |
if(len>LIMIT_FOR_COST_3){ |
| 2182 |
score+=(len-LIMIT_FOR_COST_3)*POINTS_DEL3; |
| 2183 |
len=LIMIT_FOR_COST_3; |
| 2184 |
} |
| 2185 |
if(len>1){ |
| 2186 |
score+=(len-1)*POINTS_DEL2; |
| 2187 |
} |
| 2188 |
return score; |
| 2189 |
} |
| 2190 |
|
| 2191 |
private static int calcDelScoreOffset(int len){ |
| 2192 |
if(len<=0){return 0;} |
| 2193 |
int score=POINTSoff_DEL; |
| 2194 |
|
| 2195 |
if(len>LIMIT_FOR_COST_5){ |
| 2196 |
score+=((len-LIMIT_FOR_COST_5+MASK5)/TIMESLIP)*POINTSoff_DEL5; |
| 2197 |
len=LIMIT_FOR_COST_5; |
| 2198 |
} |
| 2199 |
if(len>LIMIT_FOR_COST_4){ |
| 2200 |
score+=(len-LIMIT_FOR_COST_4)*POINTSoff_DEL4; |
| 2201 |
len=LIMIT_FOR_COST_4; |
| 2202 |
} |
| 2203 |
if(len>LIMIT_FOR_COST_3){ |
| 2204 |
score+=(len-LIMIT_FOR_COST_3)*POINTSoff_DEL3; |
| 2205 |
len=LIMIT_FOR_COST_3; |
| 2206 |
} |
| 2207 |
if(len>1){ |
| 2208 |
score+=(len-1)*POINTSoff_DEL2; |
| 2209 |
} |
| 2210 |
return score; |
| 2211 |
} |
| 2212 |
|
| 2213 |
@Override |
| 2214 |
public int calcInsScore(int len){ |
| 2215 |
if(len<=0){return 0;} |
| 2216 |
int score=POINTS_INS; |
| 2217 |
|
| 2218 |
if(len>LIMIT_FOR_COST_4){ |
| 2219 |
score+=(len-LIMIT_FOR_COST_4)*POINTS_INS4; |
| 2220 |
len=LIMIT_FOR_COST_4; |
| 2221 |
} |
| 2222 |
if(len>LIMIT_FOR_COST_3){ |
| 2223 |
score+=(len-LIMIT_FOR_COST_3)*POINTS_INS3; |
| 2224 |
len=LIMIT_FOR_COST_3; |
| 2225 |
} |
| 2226 |
if(len>1){ |
| 2227 |
score+=(len-1)*POINTS_INS2; |
| 2228 |
} |
| 2229 |
return score; |
| 2230 |
} |
| 2231 |
|
| 2232 |
private static int calcInsScoreOffset(int len){ |
| 2233 |
if(len<=0){return 0;} |
| 2234 |
int score=POINTSoff_INS; |
| 2235 |
if(len>LIMIT_FOR_COST_4){ |
| 2236 |
score+=(len-LIMIT_FOR_COST_4)*POINTSoff_INS4; |
| 2237 |
len=LIMIT_FOR_COST_4; |
| 2238 |
} |
| 2239 |
if(len>LIMIT_FOR_COST_3){ |
| 2240 |
score+=(len-LIMIT_FOR_COST_3)*POINTSoff_INS3; |
| 2241 |
len=LIMIT_FOR_COST_3; |
| 2242 |
} |
| 2243 |
if(len>1){ |
| 2244 |
score+=(len-1)*POINTSoff_INS2; |
| 2245 |
} |
| 2246 |
return score; |
| 2247 |
} |
| 2248 |
|
| 2249 |
|
| 2250 |
private final int[][][] packed; |
| 2251 |
private final byte[] grefbuffer; |
| 2252 |
private int greflimit=-1; |
| 2253 |
private int greflimit2=-1; |
| 2254 |
private int grefRefOrigin=-1; |
| 2255 |
|
| 2256 |
|
| 2257 |
@Override |
| 2258 |
|
| 2259 |
public final byte[] getGrefbuffer(){ |
| 2260 |
return grefbuffer; |
| 2261 |
} |
| 2262 |
|
| 2263 |
public final int[] vertLimit; |
| 2264 |
public final int[] horizLimit; |
| 2265 |
|
| 2266 |
@Override |
| 2267 |
public CharSequence showVertLimit(){ |
| 2268 |
StringBuilder sb=new StringBuilder(); |
| 2269 |
for(int i=0; i<=rows; i++){sb.append(vertLimit[i]>>SCOREOFFSET).append(",");} |
| 2270 |
return sb; |
| 2271 |
} |
| 2272 |
|
| 2273 |
@Override |
| 2274 |
public CharSequence showHorizLimit(){ |
| 2275 |
StringBuilder sb=new StringBuilder(); |
| 2276 |
for(int i=0; i<=columns; i++){sb.append(horizLimit[i]>>SCOREOFFSET).append(",");} |
| 2277 |
return sb; |
| 2278 |
} |
| 2279 |
|
| 2280 |
public static float minIdToMinRatio(double minid){ |
| 2281 |
if(minid>1){minid=minid/100;} |
| 2282 |
assert(minid>0 && minid<=1) : "Min identity should be between 0 and 1. Values above 1 will be assumed to be percent and divided by 100."; |
| 2283 |
double matchdif=POINTS_MATCH-POINTS_MATCH2; |
| 2284 |
double match=POINTS_MATCH2; |
| 2285 |
double sub=-POINTS_MATCH2+0.5*(matchdif+POINTS_SUB)+0.5*POINTS_SUB2; |
| 2286 |
double del=0.1*(matchdif+POINTS_DEL)+0.2*POINTS_DEL2+0.4*POINTS_DEL3+0.3*POINTS_DEL4; |
| 2287 |
double ins=-POINTS_MATCH2+0.4*(matchdif+POINTS_INS)+0.3*(POINTS_INS2)+0.3*(POINTS_INS3); |
| 2288 |
double badAvg=.7*sub+.2*del+.1*ins; |
| 2289 |
double badFraction=1-minid; |
| 2290 |
double minratio=(match+badFraction*badAvg)/match; |
| 2291 |
assert(minratio<=1); |
| 2292 |
minratio=Tools.max(0.1, minratio); |
| 2293 |
return (float)minratio; |
| 2294 |
} |
| 2295 |
|
| 2296 |
public static final int TIMEBITS=11; |
| 2297 |
public static final int SCOREBITS=32-TIMEBITS; |
| 2298 |
public static final int MAX_TIME=((1<<TIMEBITS)-1); |
| 2299 |
public static final int MAX_SCORE=((1<<(SCOREBITS-1))-1)-2000; |
| 2300 |
public static final int MIN_SCORE=0-MAX_SCORE; |
| 2301 |
|
| 2302 |
public static final int SCOREOFFSET=TIMEBITS; |
| 2303 |
|
| 2304 |
public static final int TIMEMASK=~((-1)<<TIMEBITS); |
| 2305 |
public static final int SCOREMASK=(~((-1)<<SCOREBITS))<<SCOREOFFSET; |
| 2306 |
|
| 2307 |
public static final int POINTS_NOREF=0; |
| 2308 |
public static final int POINTS_NOCALL=0; |
| 2309 |
public static final int POINTS_MATCH=70; |
| 2310 |
public static final int POINTS_MATCH2=100; |
| 2311 |
public static final int POINTS_COMPATIBLE=50; |
| 2312 |
public static final int POINTS_SUB=-127; |
| 2313 |
public static final int POINTS_SUBR=-147; |
| 2314 |
public static final int POINTS_SUB2=-51; |
| 2315 |
public static final int POINTS_SUB3=-25; |
| 2316 |
public static final int POINTS_MATCHSUB=-10; |
| 2317 |
public static final int POINTS_INS=-395; |
| 2318 |
public static final int POINTS_INS2=-39; |
| 2319 |
public static final int POINTS_INS3=-23; |
| 2320 |
public static final int POINTS_INS4=-8; |
| 2321 |
public static final int POINTS_DEL=-472; |
| 2322 |
public static final int POINTS_DEL2=-33; |
| 2323 |
public static final int POINTS_DEL3=-9; |
| 2324 |
public static final int POINTS_DEL4=-1; |
| 2325 |
public static final int POINTS_DEL5=-1; |
| 2326 |
public static final int POINTS_DEL_REF_N=-10; |
| 2327 |
public static final int POINTS_GAP=0-GAPCOST; |
| 2328 |
|
| 2329 |
public static final int TIMESLIP=4; |
| 2330 |
public static final int MASK5=TIMESLIP-1; |
| 2331 |
static{assert(Integer.bitCount(TIMESLIP)==1);} |
| 2332 |
|
| 2333 |
|
| 2334 |
private static final int BARRIER_I1=2; |
| 2335 |
private static final int BARRIER_D1=3; |
| 2336 |
|
| 2337 |
|
| 2338 |
public static final int LIMIT_FOR_COST_3=5; |
| 2339 |
public static final int LIMIT_FOR_COST_4=20; |
| 2340 |
public static final int LIMIT_FOR_COST_5=80; |
| 2341 |
|
| 2342 |
public static final int BAD=MIN_SCORE-1; |
| 2343 |
|
| 2344 |
|
| 2345 |
public static final int POINTSoff_NOREF=(POINTS_NOREF<<SCOREOFFSET); |
| 2346 |
public static final int POINTSoff_NOCALL=(POINTS_NOCALL<<SCOREOFFSET); |
| 2347 |
public static final int POINTSoff_MATCH=(POINTS_MATCH<<SCOREOFFSET); |
| 2348 |
public static final int POINTSoff_MATCH2=(POINTS_MATCH2<<SCOREOFFSET); |
| 2349 |
public static final int POINTSoff_COMPATIBLE=(POINTS_COMPATIBLE<<SCOREOFFSET); |
| 2350 |
public static final int POINTSoff_SUB=(POINTS_SUB<<SCOREOFFSET); |
| 2351 |
public static final int POINTSoff_SUBR=(POINTS_SUBR<<SCOREOFFSET); |
| 2352 |
public static final int POINTSoff_SUB2=(POINTS_SUB2<<SCOREOFFSET); |
| 2353 |
public static final int POINTSoff_SUB3=(POINTS_SUB3<<SCOREOFFSET); |
| 2354 |
public static final int POINTSoff_MATCHSUB=(POINTS_MATCHSUB<<SCOREOFFSET); |
| 2355 |
public static final int POINTSoff_INS=(POINTS_INS<<SCOREOFFSET); |
| 2356 |
public static final int POINTSoff_INS2=(POINTS_INS2<<SCOREOFFSET); |
| 2357 |
public static final int POINTSoff_INS3=(POINTS_INS3<<SCOREOFFSET); |
| 2358 |
public static final int POINTSoff_INS4=(POINTS_INS4<<SCOREOFFSET); |
| 2359 |
public static final int POINTSoff_DEL=(POINTS_DEL<<SCOREOFFSET); |
| 2360 |
public static final int POINTSoff_DEL2=(POINTS_DEL2<<SCOREOFFSET); |
| 2361 |
public static final int POINTSoff_DEL3=(POINTS_DEL3<<SCOREOFFSET); |
| 2362 |
public static final int POINTSoff_DEL4=(POINTS_DEL4<<SCOREOFFSET); |
| 2363 |
public static final int POINTSoff_DEL5=(POINTS_DEL5<<SCOREOFFSET); |
| 2364 |
public static final int POINTSoff_GAP=(POINTS_GAP<<SCOREOFFSET); |
| 2365 |
public static final int POINTSoff_DEL_REF_N=(POINTS_DEL_REF_N<<SCOREOFFSET); |
| 2366 |
public static final int BADoff=(BAD<<SCOREOFFSET); |
| 2367 |
public static final int MAXoff_SCORE=MAX_SCORE<<SCOREOFFSET; |
| 2368 |
public static final int MINoff_SCORE=MIN_SCORE<<SCOREOFFSET; |
| 2369 |
|
| 2370 |
|
| 2371 |
@Override |
| 2372 |
public final int POINTS_NOREF(){return POINTS_NOREF;} |
| 2373 |
@Override |
| 2374 |
public final int POINTS_NOCALL(){return POINTS_NOCALL;} |
| 2375 |
@Override |
| 2376 |
public final int POINTS_MATCH(){return POINTS_MATCH;} |
| 2377 |
@Override |
| 2378 |
public final int POINTS_MATCH2(){return POINTS_MATCH2;} |
| 2379 |
@Override |
| 2380 |
public final int POINTS_COMPATIBLE(){return POINTS_COMPATIBLE;} |
| 2381 |
@Override |
| 2382 |
public final int POINTS_SUB(){return POINTS_SUB;} |
| 2383 |
@Override |
| 2384 |
public final int POINTS_SUBR(){return POINTS_SUBR;} |
| 2385 |
@Override |
| 2386 |
public final int POINTS_SUB2(){return POINTS_SUB2;} |
| 2387 |
@Override |
| 2388 |
public final int POINTS_SUB3(){return POINTS_SUB3;} |
| 2389 |
@Override |
| 2390 |
public final int POINTS_MATCHSUB(){return POINTS_MATCHSUB;} |
| 2391 |
@Override |
| 2392 |
public final int POINTS_INS(){return POINTS_INS;} |
| 2393 |
@Override |
| 2394 |
public final int POINTS_INS2(){return POINTS_INS2;} |
| 2395 |
@Override |
| 2396 |
public final int POINTS_INS3(){return POINTS_INS3;} |
| 2397 |
@Override |
| 2398 |
public final int POINTS_INS4(){return POINTS_INS4;} |
| 2399 |
@Override |
| 2400 |
public final int POINTS_DEL(){return POINTS_DEL;} |
| 2401 |
@Override |
| 2402 |
public final int POINTS_DEL2(){return POINTS_DEL2;} |
| 2403 |
@Override |
| 2404 |
public final int POINTS_DEL3(){return POINTS_DEL3;} |
| 2405 |
@Override |
| 2406 |
public final int POINTS_DEL4(){return POINTS_DEL4;} |
| 2407 |
@Override |
| 2408 |
public final int POINTS_DEL5(){return POINTS_DEL5;} |
| 2409 |
@Override |
| 2410 |
public final int POINTS_DEL_REF_N(){return POINTS_DEL_REF_N;} |
| 2411 |
@Override |
| 2412 |
public final int POINTS_GAP(){return POINTS_GAP;} |
| 2413 |
|
| 2414 |
@Override |
| 2415 |
public final int TIMESLIP(){return TIMESLIP;} |
| 2416 |
@Override |
| 2417 |
public final int MASK5(){return MASK5;} |
| 2418 |
@Override |
| 2419 |
public final int SCOREOFFSET(){return SCOREOFFSET();} |
| 2420 |
|
| 2421 |
@Override |
| 2422 |
final int BARRIER_I1(){return BARRIER_I1;} |
| 2423 |
@Override |
| 2424 |
final int BARRIER_D1(){return BARRIER_D1;} |
| 2425 |
|
| 2426 |
@Override |
| 2427 |
public final int LIMIT_FOR_COST_3(){return LIMIT_FOR_COST_3;} |
| 2428 |
@Override |
| 2429 |
public final int LIMIT_FOR_COST_4(){return LIMIT_FOR_COST_4;} |
| 2430 |
@Override |
| 2431 |
public final int LIMIT_FOR_COST_5(){return LIMIT_FOR_COST_5;} |
| 2432 |
|
| 2433 |
@Override |
| 2434 |
public final int BAD(){return BAD;} |
| 2435 |
|
| 2436 |
|
| 2437 |
private int rows; |
| 2438 |
private int columns; |
| 2439 |
|
| 2440 |
} |