Debian Codegraph @

current/align2/MultiStateAligner11ts.java

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