Debian Codegraph @

current/aligner/MultiStateAligner9PacBioAdapter.java

raw symbols SCIP
1 package aligner;
2
3 import java.util.Arrays;
4
5 import dna.AminoAcid;
6 import dna.ChromosomeArray;
7 import dna.Data;
8 import shared.Tools;
9 import stream.Read;
10 import stream.SiteScore;
11
12 /**
13 * Based on MSA9ts, with transform scores tweaked for PacBio. */
14 public final class MultiStateAligner9PacBioAdapter {
15
16
17 public MultiStateAligner9PacBioAdapter(int maxRows_, int maxColumns_){
18 // assert(maxColumns_>=200);
19 // assert(maxRows_>=200);
20 maxRows=maxRows_;
21 maxColumns=maxColumns_;
22 packed=new int[3][maxRows+1][maxColumns+1];
23
24 insScoreArray=new int[maxRows+1];
25 delScoreArray=new int[maxColumns+1];
26 matchScoreArray=new int[maxRows+1];
27 subScoreArray=new int[maxRows+1];
28 for(int i=0; i<insScoreArray.length; i++){
29 insScoreArray[i]=(i==0 ? POINTSoff_INS :
30 i<LIMIT_FOR_COST_3 ? POINTSoff_INS2 : POINTSoff_INS3);
31 }
32 for(int i=0; i<delScoreArray.length; i++){
33 delScoreArray[i]=(i==0 ? POINTSoff_DEL :
34 i<LIMIT_FOR_COST_3 ? POINTSoff_DEL2 : POINTSoff_DEL3);
35 }
36 for(int i=0; i<matchScoreArray.length; i++){
37 matchScoreArray[i]=(i==0 ? POINTSoff_MATCH2 : POINTSoff_MATCH);
38 }
39 for(int i=0; i<subScoreArray.length; i++){
40 subScoreArray[i]=(i==0 ? POINTSoff_SUB : i<LIMIT_FOR_COST_3 ? POINTSoff_SUB2 : POINTSoff_SUB3);
41 }
42
43 vertLimit=new int[maxRows+1];
44 horizLimit=new int[maxColumns+1];
45 Arrays.fill(vertLimit, BADoff);
46 Arrays.fill(horizLimit, BADoff);
47
48 // for(int i=0; i<maxColumns+1; i++){
49 // scores[0][i]=0-i;
50 // }
51
52 for(int matrix=0; matrix<packed.length; matrix++){
53 for(int i=1; i<=maxRows; i++){
54 for(int j=0; j<packed[matrix][i].length; j++){
55 packed[matrix][i][j]|=BADoff;
56 }
57 // packed[matrix][i][0]|=MODE_INS;
58 }
59 // for(int i=0; i<maxRows+1; i++){
60 // scores[matrix][i][0]=(i*POINTSoff_NOREF);
61 // }
62 for(int i=0; i<=maxRows; i++){
63
64 int prevScore=(i<2 ? 0 : packed[matrix][i-1][0]);
65 int score=(i<2 ? (i*POINTSoff_INS) :
66 (i<LIMIT_FOR_COST_3 ? prevScore+POINTSoff_INS2 : prevScore+POINTSoff_INS3));
67
68 packed[matrix][i][0]=score;
69 }
70 // for(int i=1; i<maxColumns+1; i++){
71 // prevState[matrix][0][i]=MODE_DEL;
72 // }
73 // for(int i=0; i<=maxColumns; i++){
74 // packed[matrix][0][i]|=MODE_MS;
75 // }
76 }
77 }
78
79
80 /** return new int[] {rows, maxC, maxS, max};
81 * Will not fill areas that cannot match minScore */
82 public final int[] fillLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
83 return fillLimitedX(read, ref, refStartLoc, refEndLoc, minScore);
84 }
85
86
87 /** return new int[] {rows, maxC, maxS, max};
88 * Will not fill areas that cannot match minScore */
89 private final int[] fillLimitedX(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
90 // minScore=0;
91 // assert(minScore>0);
92 rows=read.length;
93 columns=refEndLoc-refStartLoc+1;
94
95 if(/*read.length<40 || */false || minScore<=0 || columns>read.length+Tools.min(100, read.length)){
96 // assert(false) : minScore;
97 return fillUnlimited(read, ref, refStartLoc, refEndLoc, minScore);
98 }
99
100 minScore-=100; //Increases quality trivially
101
102 assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows+"\n"+
103 refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n";
104 assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns+"\n"+
105 refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n";
106
107 assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc+"\n"+
108 refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n";
109 assert(refEndLoc<ref.length) : "Check that values are in-bounds before calling this function: "+refEndLoc+", "+ref.length+"\n"+
110 refStartLoc+", "+refEndLoc+", "+rows+", "+maxRows+", "+columns+", "+maxColumns+"\n"+new String(read)+"\n";
111
112 // for(int x=0; x<packed.length; x++){
113 // for(int y=1; y<rows+1; y++){
114 // Arrays.fill(packed[x][y], 1, columns+1, BADoff);
115 // }
116 // }
117 for(int x=0; x<packed.length; x++){
118
119 // Arrays.fill(packed[x][1], 1, columns+1, BADoff);
120 Arrays.fill(packed[x][rows], 1, columns+1, BADoff);
121 // for(int y=1; y<rows+1; y++){
122 // Arrays.fill(packed[x][y], 1, columns+1, BADoff);
123 // }
124 }
125
126 int minGoodCol=1;
127 int maxGoodCol=columns;
128
129 final int minScore_off=(minScore<<SCOREOFFSET);
130 final int maxGain=(read.length-1)*POINTSoff_MATCH2+POINTSoff_MATCH;
131 final int floor=minScore_off-maxGain;
132 // final int subfloor=Tools.max(BADoff, floor-200*POINTSoff_MATCH2);
133 final int subfloor=floor-5*POINTSoff_MATCH2;
134 assert(subfloor>BADoff); //TODO: Actually, it needs to be substantially more.
135 assert(subfloor<minScore_off) : minScore_off+", "+floor+", "+BADoff+", "+subfloor;
136
137 if(verbose2){
138 System.out.println();
139 System.out.println("minScore="+minScore+"\t"+minScore_off);
140 System.out.println("maxGain="+(maxGain>>SCOREOFFSET)+"\t"+(maxGain));
141 System.out.println("floor="+(floor>>SCOREOFFSET)+"\t"+(floor));
142 System.out.println("subfloor="+(subfloor>>SCOREOFFSET)+"\t"+(subfloor));
143 System.out.println("BADoff="+(BADoff>>SCOREOFFSET)+"\t"+(BADoff));
144 System.out.println("maxGain="+(maxGain>>SCOREOFFSET)+"\t"+(maxGain));
145 System.out.println();
146 }
147
148 vertLimit[rows]=minScore_off;
149 for(int i=rows-1; i>=0; i--){
150 vertLimit[i]=Tools.max(vertLimit[i+1]-POINTSoff_MATCH2, floor);
151 }
152
153 horizLimit[columns]=minScore_off;
154 for(int i=columns-1; i>=0; i--){
155 horizLimit[i]=Tools.max(horizLimit[i+1]-POINTSoff_MATCH2, floor);
156 }
157
158 for(int row=1; row<=rows; row++){
159
160 int colStart=minGoodCol;
161 int colStop=maxGoodCol;
162
163 minGoodCol=-1;
164 maxGoodCol=-2;
165
166 final int vlimit=vertLimit[row];
167
168 if(verbose2){
169 System.out.println();
170 System.out.println("row="+row);
171 System.out.println("colStart="+colStart);
172 System.out.println("colStop="+colStop);
173 System.out.println("vlimit="+(vlimit>>SCOREOFFSET)+"\t"+(vlimit));
174 }
175
176 if(colStart<0 || colStop<colStart){break;}
177
178
179 if(colStart>1){
180 assert(row>0);
181 packed[MODE_MS][row][colStart-1]=subfloor;
182 packed[MODE_INS][row][colStart-1]=subfloor;
183 packed[MODE_DEL][row][colStart-1]=subfloor;
184 }
185
186
187 for(int col=colStart; col<=columns; col++){
188
189
190 if(verbose2){
191 System.out.println("\ncol "+col);
192 }
193
194 final byte call0=(row<2 ? (byte)'?' : read[row-2]);
195 final byte call1=read[row-1];
196 final byte ref0=(col<2 ? (byte)'!' : ref[refStartLoc+col-2]);
197 final byte ref1=ref[refStartLoc+col-1];
198
199 // final boolean match=(read[row-1]==ref[refStartLoc+col-1]);
200 // final boolean prevMatch=(row<2 || col<2 ? false : read[row-2]==ref[refStartLoc+col-2]);
201 final boolean match=(call1==ref1);
202 final boolean prevMatch=(call0==ref0);
203
204 // System.err.println("")
205
206 iterationsLimited++;
207 final int limit=Tools.max(vlimit, horizLimit[col]);
208 final int limit3=Tools.max(floor, (match ? limit-POINTSoff_MATCH2 : limit-POINTSoff_SUB3));
209
210 final int delNeeded=Tools.max(0, row-col-1);
211 final int insNeeded=Tools.max(0, (rows-row)-(columns-col)-1);
212
213 final int delPenalty=calcDelScoreOffset(delNeeded);
214 final int insPenalty=calcInsScoreOffset(insNeeded);
215
216
217 final int scoreFromDiag_MS=packed[MODE_MS][row-1][col-1]&SCOREMASK;
218 final int scoreFromDel_MS=packed[MODE_DEL][row-1][col-1]&SCOREMASK;
219 final int scoreFromIns_MS=packed[MODE_INS][row-1][col-1]&SCOREMASK;
220
221 final int scoreFromDiag_DEL=packed[MODE_MS][row][col-1]&SCOREMASK;
222 final int scoreFromDel_DEL=packed[MODE_DEL][row][col-1]&SCOREMASK;
223
224 final int scoreFromDiag_INS=packed[MODE_MS][row-1][col]&SCOREMASK;
225 final int scoreFromIns_INS=packed[MODE_INS][row-1][col]&SCOREMASK;
226
227 // if(scoreFromDiag_MS<limit3 && scoreFromDel_MS<limit3 && scoreFromIns_MS<limit3
228 // && scoreFromDiag_DEL<limit && scoreFromDel_DEL<limit && scoreFromDiag_INS<limit && scoreFromIns_INS<limit){
229 // iterationsLimited--; //A "fast" iteration
230 // }
231
232 if((scoreFromDiag_MS<=limit3 && scoreFromDel_MS<=limit3 && scoreFromIns_MS<=limit3)){
233 packed[MODE_MS][row][col]=subfloor;
234 }else{//Calculate match and sub scores
235 final int streak=(packed[MODE_MS][row-1][col-1]&TIMEMASK);
236
237 {//Calculate match/sub score
238
239 int score;
240 int time;
241 byte prevState;
242
243 if(match){
244
245 int scoreMS=scoreFromDiag_MS+(prevMatch ? POINTSoff_MATCH2 : POINTSoff_MATCH);
246 int scoreD=scoreFromDel_MS+POINTSoff_MATCH;
247 int scoreI=scoreFromIns_MS+POINTSoff_MATCH;
248
249 // byte prevState;
250 if(scoreMS>=scoreD && scoreMS>=scoreI){
251 score=scoreMS;
252 time=(prevMatch ? streak+1 : 1);
253 prevState=MODE_MS;
254 }else if(scoreD>=scoreI){
255 score=scoreD;
256 time=1;
257 prevState=MODE_DEL;
258 }else{
259 score=scoreI;
260 time=1;
261 prevState=MODE_INS;
262 }
263
264 // assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
265 // assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead";
266 // assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
267 //// packed[MODE_MS][row][col]=(score|prevState|time);
268 // packed[MODE_MS][row][col]=(score|time);
269 // assert((score&SCOREMASK)==score);
270 //// assert((prevState&MODEMASK)==prevState);
271 // assert((time&TIMEMASK)==time);
272
273 }else{
274
275 int scoreMS;
276 if(ref1!='N'){
277 scoreMS=scoreFromDiag_MS+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : subScoreArray[streak]);
278 }else{
279 scoreMS=scoreFromDiag_MS+POINTSoff_NOCALL;
280 }
281
282 int scoreD=scoreFromDel_MS+POINTSoff_SUB; //+2 to move it as close as possible to the deletion / insertion
283 int scoreI=scoreFromIns_MS+POINTSoff_SUB;
284
285 if(scoreMS>=scoreD && scoreMS>=scoreI){
286 score=scoreMS;
287 time=(prevMatch ? 1 : streak+1);
288 // time=(prevMatch ? (streak==1 ? 3 : 1) : streak+1);
289 prevState=MODE_MS;
290 }else if(scoreD>=scoreI){
291 score=scoreD;
292 time=1;
293 prevState=MODE_DEL;
294 }else{
295 score=scoreI;
296 time=1;
297 prevState=MODE_INS;
298 }
299
300 // assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
301 // assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead";
302 // assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
303 //// packed[MODE_MS][row][col]=(score|prevState|time);
304 // packed[MODE_MS][row][col]=(score|time);
305 // assert((score&SCOREMASK)==score);
306 //// assert((prevState&MODEMASK)==prevState);
307 // assert((time&TIMEMASK)==time);
308 }
309
310 final int limit2;
311 if(delNeeded>0){
312 limit2=limit-delPenalty;
313 }else if(insNeeded>0){
314 limit2=limit-insPenalty;
315 }else{
316 limit2=limit;
317 }
318 assert(limit2>=limit);
319
320 if(verbose2){System.err.println("MS: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));}
321
322 if(score>=limit2){
323 maxGoodCol=col;
324 if(minGoodCol<0){minGoodCol=col;}
325 }else{
326 score=subfloor;
327 }
328
329 assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
330 assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead";
331 assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
332 // packed[MODE_MS][row][col]=(score|prevState|time);
333 packed[MODE_MS][row][col]=(score|time);
334 assert((score&SCOREMASK)==score);
335 // assert((prevState&MODEMASK)==prevState);
336 assert((time&TIMEMASK)==time);
337 }
338 }
339
340 if((scoreFromDiag_DEL<=limit && scoreFromDel_DEL<=limit)){
341 // assert((scoreFromDiag_DEL<=limit && scoreFromDel_DEL<=limit)) : scoreFromDiag_DEL+", "+row;
342 packed[MODE_DEL][row][col]=subfloor;
343 }else{//Calculate DEL score
344
345 final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK;
346
347 int scoreMS=scoreFromDiag_DEL+POINTSoff_DEL;
348 int scoreD=scoreFromDel_DEL+delScoreArray[streak];
349 // int scoreI=scoreFromIns+POINTSoff_DEL;
350
351 int score;
352 int time;
353 byte prevState;
354 if(scoreMS>=scoreD){
355 score=scoreMS;
356 time=1;
357 prevState=MODE_MS;
358 }else{
359 score=scoreD;
360 time=streak+1;
361 prevState=MODE_DEL;
362 }
363
364 final int limit2;
365 if(insNeeded>0){
366 limit2=limit-insPenalty;
367 }else if(delNeeded>0){
368 limit2=limit-calcDelScoreOffset(time+delNeeded)+calcDelScoreOffset(time);
369 }else{
370 limit2=limit;
371 }
372 assert(limit2>=limit);
373 if(verbose2){System.err.println("DEL: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));}
374
375 if(score>=limit2){
376 maxGoodCol=col;
377 if(minGoodCol<0){minGoodCol=col;}
378 }else{
379 score=subfloor;
380 }
381
382 assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
383 assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead";
384 assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
385 // packed[MODE_DEL][row][col]=(score|prevState|time);
386 packed[MODE_DEL][row][col]=(score|time);
387 assert((score&SCOREMASK)==score);
388 // assert((prevState&MODEMASK)==prevState);
389 assert((time&TIMEMASK)==time);
390 }
391
392 if(scoreFromDiag_INS<=limit && scoreFromIns_INS<=limit){
393 packed[MODE_INS][row][col]=subfloor;
394 }else{//Calculate INS score
395
396 final int streak=packed[MODE_INS][row-1][col]&TIMEMASK;
397
398 int scoreMS=scoreFromDiag_INS+POINTSoff_INS;
399 // int scoreD=scoreFromDel+POINTSoff_INS;
400 int scoreI=scoreFromIns_INS+insScoreArray[streak];
401
402
403 // System.err.println("("+row+","+col+")\t"+scoreFromDiag+"+"+POINTSoff_INS+"="+scoreM+", "+
404 // scoreFromSub+"+"+POINTSoff_INS+"="+scoreS+", "
405 // +scoreD+", "+scoreFromIns+"+"+
406 // (streak==0 ? POINTSoff_INS : streak<LIMIT_FOR_COST_3 ? POINTSoff_INS2 : POINTSoff_INS3)+"="+scoreI);
407
408 //if(match){scoreMS=subfloor;}
409
410 int score;
411 int time;
412 byte prevState;
413 if(scoreMS>=scoreI){
414 score=scoreMS;
415 time=1;
416 prevState=MODE_MS;
417 }else{
418 score=scoreI;
419 time=streak+1;
420 prevState=MODE_INS;
421 }
422
423 final int limit2;
424 if(delNeeded>0){
425 limit2=limit-delPenalty;
426 }else if(insNeeded>0){
427 limit2=limit-calcInsScoreOffset(time+insNeeded)+calcInsScoreOffset(time);
428 }else{
429 limit2=limit;
430 }
431 assert(limit2>=limit);
432
433 if(verbose2){System.err.println("INS: \tlimit2="+(limit2>>SCOREOFFSET)+"\t, score="+(score>>SCOREOFFSET));}
434 if(score>=limit2){
435 maxGoodCol=col;
436 if(minGoodCol<0){minGoodCol=col;}
437 }else{
438 score=subfloor;
439 }
440
441 assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
442 assert(score>=MINoff_SCORE || score==BADoff) : "Score overflow - use MSA2 instead";
443 assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
444 // packed[MODE_INS][row][col]=(score|prevState|time);
445 packed[MODE_INS][row][col]=(score|time);
446 assert((score&SCOREMASK)==score);
447 // assert((prevState&MODEMASK)==prevState);
448 assert((time&TIMEMASK)==time);
449 }
450
451
452 if(col>=colStop){
453 if(col>colStop && maxGoodCol<col){break;}
454 if(row>1){
455 packed[MODE_MS][row-1][col+1]=subfloor;
456 packed[MODE_INS][row-1][col+1]=subfloor;
457 packed[MODE_DEL][row-1][col+1]=subfloor;
458 }
459 }
460 }
461 }
462
463
464 int maxCol=-1;
465 int maxState=-1;
466 int maxScore=Integer.MIN_VALUE;
467
468 for(int state=0; state<packed.length; state++){
469 for(int col=1; col<=columns; col++){
470 int x=packed[state][rows][col]&SCOREMASK;
471 if(x>maxScore){
472 maxScore=x;
473 maxCol=col;
474 maxState=state;
475 }
476 }
477 }
478
479 assert(maxScore>=BADoff);
480 // if(maxScore==BADoff){
481 // return null;
482 // }
483 // if(maxScore<floor){
484 // return null;
485 // }
486 if(maxScore<minScore_off){
487 return null;
488 }
489
490 maxScore>>=SCOREOFFSET;
491
492 // System.err.println("Returning "+rows+", "+maxCol+", "+maxState+", "+maxScore);
493 return maxScore<minScore ? null : new int[] {rows, maxCol, maxState, maxScore};
494 }
495
496
497 /** return new int[] {rows, maxC, maxS, max};
498 * Does not require a min score (ie, same as old method) */
499 private final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, final int minScore){
500 rows=read.length;
501 columns=refEndLoc-refStartLoc+1;
502
503 final int maxGain=(read.length-1)*POINTSoff_MATCH2+POINTSoff_MATCH;
504 final int subfloor=0-2*maxGain;
505 assert(subfloor>BADoff && subfloor*2>BADoff); //TODO: Actually, it needs to be substantially more.
506
507 //temporary, for finding a bug
508 if(rows>maxRows || columns>maxColumns){
509 throw new RuntimeException("rows="+rows+", maxRows="+maxRows+", cols="+columns+", maxCols="+maxColumns+"\n"+new String(read)+"\n");
510 }
511
512 assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows;
513 assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns;
514
515 assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc;
516 assert(refEndLoc<ref.length) : "Check that values are in-bounds before calling this function: "+refEndLoc+", "+ref.length;
517
518 for(int row=1; row<=rows; row++){
519
520 // int minc=max(1, row-20);
521 // int maxc=min(columns, row+20);
522
523 for(int col=1; col<=columns; col++){
524 iterationsUnlimited++;
525
526 // final boolean match=(read[row-1]==ref[refStartLoc+col-1]);
527 // final boolean prevMatch=(row<2 || col<2 ? false : read[row-2]==ref[refStartLoc+col-2]);
528
529 final byte call0=(row<2 ? (byte)'?' : read[row-2]);
530 final byte call1=read[row-1];
531 final byte ref0=(col<2 ? (byte)'!' : ref[refStartLoc+col-2]);
532 final byte ref1=ref[refStartLoc+col-1];
533
534 final boolean match=(call1==ref1);
535 final boolean prevMatch=(call0==ref0);
536
537 {//Calculate match and sub scores
538
539 final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK;
540 final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK;
541 final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK;
542 final int streak=(packed[MODE_MS][row-1][col-1]&TIMEMASK);
543
544 {//Calculate match/sub score
545
546 if(match){
547
548 int scoreMS=scoreFromDiag+(prevMatch ? POINTSoff_MATCH2 : POINTSoff_MATCH);
549 int scoreD=scoreFromDel+POINTSoff_MATCH;
550 int scoreI=scoreFromIns+POINTSoff_MATCH;
551
552 int score;
553 int time;
554 // byte prevState;
555 if(scoreMS>=scoreD && scoreMS>=scoreI){
556 score=scoreMS;
557 time=(prevMatch ? streak+1 : 1);
558 // prevState=MODE_MS;
559 }else if(scoreD>=scoreI){
560 score=scoreD;
561 time=1;
562 // prevState=MODE_DEL;
563 }else{
564 score=scoreI;
565 time=1;
566 // prevState=MODE_INS;
567 }
568
569 assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
570 assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead";
571 assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
572 // packed[MODE_MS][row][col]=(score|prevState|time);
573 packed[MODE_MS][row][col]=(score|time);
574 assert((score&SCOREMASK)==score);
575 // assert((prevState&MODEMASK)==prevState);
576 assert((time&TIMEMASK)==time);
577
578 }else{
579
580 int scoreMS;
581 if(ref1!='N'){
582 scoreMS=scoreFromDiag+(prevMatch ? (streak<=1 ? POINTSoff_SUBR : POINTSoff_SUB) : subScoreArray[streak]);
583 }else{
584 scoreMS=scoreFromDiag+POINTSoff_NOCALL;
585 }
586
587 int scoreD=scoreFromDel+POINTSoff_SUB; //+2 to move it as close as possible to the deletion / insertion
588 int scoreI=scoreFromIns+POINTSoff_SUB;
589
590 int score;
591 int time;
592 byte prevState;
593 if(scoreMS>=scoreD && scoreMS>=scoreI){
594 score=scoreMS;
595 time=(prevMatch ? 1 : streak+1);
596 // time=(prevMatch ? (streak==1 ? 3 : 1) : streak+1);
597 prevState=MODE_MS;
598 }else if(scoreD>=scoreI){
599 score=scoreD;
600 time=1;
601 prevState=MODE_DEL;
602 }else{
603 score=scoreI;
604 time=1;
605 prevState=MODE_INS;
606 }
607
608 assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
609 assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead";
610 assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
611 // packed[MODE_MS][row][col]=(score|prevState|time);
612 packed[MODE_MS][row][col]=(score|time);
613 assert((score&SCOREMASK)==score);
614 // assert((prevState&MODEMASK)==prevState);
615 assert((time&TIMEMASK)==time);
616 }
617 }
618 }
619
620 {//Calculate DEL score
621
622 final int streak=packed[MODE_DEL][row][col-1]&TIMEMASK;
623
624 final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK;
625 final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK;
626
627 int scoreMS=scoreFromDiag+POINTSoff_DEL;
628 int scoreD=scoreFromDel+delScoreArray[streak];
629 // int scoreI=scoreFromIns+POINTSoff_DEL;
630
631 int score;
632 int time;
633 byte prevState;
634 if(scoreMS>=scoreD){
635 score=scoreMS;
636 time=1;
637 prevState=MODE_MS;
638 }else{
639 score=scoreD;
640 time=streak+1;
641 prevState=MODE_DEL;
642 }
643
644 assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
645 assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead";
646 assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
647 // packed[MODE_DEL][row][col]=(score|prevState|time);
648 packed[MODE_DEL][row][col]=(score|time);
649 assert((score&SCOREMASK)==score);
650 // assert((prevState&MODEMASK)==prevState);
651 assert((time&TIMEMASK)==time);
652 }
653
654 {//Calculate INS score
655
656 final int streak=packed[MODE_INS][row-1][col]&TIMEMASK;
657
658 final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK;
659 final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK;
660
661 int scoreMS=scoreFromDiag+POINTSoff_INS;
662 // int scoreD=scoreFromDel+POINTSoff_INS;
663 int scoreI=scoreFromIns+insScoreArray[streak];
664
665 // System.err.println("("+row+","+col+")\t"+scoreFromDiag+"+"+POINTSoff_INS+"="+scoreM+", "+
666 // scoreFromSub+"+"+POINTSoff_INS+"="+scoreS+", "
667 // +scoreD+", "+scoreFromIns+"+"+
668 // (streak==0 ? POINTSoff_INS : streak<LIMIT_FOR_COST_3 ? POINTSoff_INS2 : POINTSoff_INS3)+"="+scoreI);
669
670 //if(match){scoreMS=subfloor;}
671
672 int score;
673 int time;
674 byte prevState;
675 if(scoreMS>=scoreI){
676 score=scoreMS;
677 time=1;
678 prevState=MODE_MS;
679 }else{
680 score=scoreI;
681 time=streak+1;
682 prevState=MODE_INS;
683 }
684
685 assert(time<=MAX_TIME);//if(time>MAX_TIME){time=MAX_TIME-3;}
686 assert(score>=MINoff_SCORE) : "Score overflow - use MSA2 instead";
687 assert(score<=MAXoff_SCORE) : "Score overflow - use MSA2 instead";
688 // packed[MODE_INS][row][col]=(score|prevState|time);
689 packed[MODE_INS][row][col]=(score|time);
690 assert((score&SCOREMASK)==score);
691 // assert((prevState&MODEMASK)==prevState);
692 assert((time&TIMEMASK)==time);
693 }
694 }
695 }
696
697
698 int maxCol=-1;
699 int maxState=-1;
700 int maxScore=Integer.MIN_VALUE;
701
702 for(int state=0; state<packed.length; state++){
703 for(int col=1; col<=columns; col++){
704 int x=packed[state][rows][col]&SCOREMASK;
705 if(x>maxScore){
706 maxScore=x;
707 maxCol=col;
708 maxState=state;
709 }
710 }
711 }
712 maxScore>>=SCOREOFFSET;
713
714 // System.err.println("Returning "+rows+", "+maxCol+", "+maxState+", "+maxScore);
715 return maxScore<minScore ? null : new int[] {rows, maxCol, maxState, maxScore};
716 }
717
718
719
720 /** Generates the match string */
721 public final byte[] traceback(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state){
722 // assert(false);
723 assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc;
724 assert(row==rows);
725
726 byte[] out=new byte[row+col-1]; //TODO if an out of bound crash occurs, try removing the "-1".
727 int outPos=0;
728
729 if(state==MODE_INS){
730 //TODO ? Maybe not needed.
731 }
732
733 while(row>0 && col>0){
734
735 // byte prev0=(byte)(packed[state][row][col]&MODEMASK);
736
737 final int time=packed[state][row][col]&TIMEMASK;
738 final byte prev;
739
740 // System.err.println("state="+state+", prev="+prev+", row="+row+", col="+col+", score="+scores[state][row][col]);
741
742 if(state==MODE_MS){
743 if(time>1){prev=(byte)state;}
744 else{
745 final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK;
746 final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK;
747 final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK;
748 if(scoreFromDiag>=scoreFromDel && scoreFromDiag>=scoreFromIns){prev=MODE_MS;}
749 else if(scoreFromDel>=scoreFromIns){prev=MODE_DEL;}
750 else{prev=MODE_INS;}
751 }
752
753 byte c=read[row-1];
754 byte r=ref[refStartLoc+col-1];
755 if(c==r){
756 out[outPos]='m';
757 }else{
758 if(!AminoAcid.isFullyDefined(c)){
759 out[outPos]='N';
760 }else if(!AminoAcid.isFullyDefined(r)){
761 // out[outPos]='X';
762 out[outPos]='N';
763 }else{
764 out[outPos]='S';
765 }
766 }
767
768 row--;
769 col--;
770 }else if(state==MODE_DEL){
771 if(time>1){prev=(byte)state;}
772 else{
773 final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK;
774 final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK;
775 if(scoreFromDiag>=scoreFromDel){prev=MODE_MS;}
776 else{prev=MODE_DEL;}
777 }
778
779 byte r=ref[refStartLoc+col-1];
780 out[outPos]='D';
781
782 col--;
783 }else{
784 if(time>1){prev=(byte)state;}
785 else{
786 final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK;
787 final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK;
788 if(scoreFromDiag>=scoreFromIns){prev=MODE_MS;}
789 else{prev=MODE_INS;}
790 }
791
792 assert(state==MODE_INS) : state;
793 if(col==0){
794 out[outPos]='X';
795 }else if(col>=columns){
796 out[outPos]='Y';
797 }else{
798 out[outPos]='I';
799 }
800 row--;
801 }
802
803 // assert(prev==prev0);
804 state=prev;
805 outPos++;
806 }
807
808 assert(row==0 || col==0);
809 if(col!=row){
810 while(row>0){
811 out[outPos]='X';
812 outPos++;
813 row--;
814 col--;
815 }
816 if(col>0){
817 //do nothing
818 }
819 }
820
821
822 //Shrink and reverse the string
823 byte[] out2=new byte[outPos];
824 for(int i=0; i<outPos; i++){
825 out2[i]=out[outPos-i-1];
826 }
827 out=null;
828
829 return out2;
830 }
831
832 /** @return {score, bestRefStart, bestRefStop} */
833 public final int[] score(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc,
834 final int maxRow, final int maxCol, final int maxState){
835
836 int row=maxRow;
837 int col=maxCol;
838 int state=maxState;
839
840 assert(maxState>=0 && maxState<packed.length) :
841 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
842 assert(maxRow>=0 && maxRow<packed[0].length) :
843 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
844 assert(maxCol>=0 && maxCol<packed[0][0].length) :
845 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
846
847 int score=packed[maxState][maxRow][maxCol]&SCOREMASK; //Or zero, if it is to be recalculated
848
849 if(row<rows){
850 int difR=rows-row;
851 int difC=columns-col;
852
853 while(difR>difC){
854 score+=POINTSoff_NOREF;
855 difR--;
856 }
857
858 row+=difR;
859 col+=difR;
860
861 }
862
863 assert(refStartLoc<=refEndLoc);
864 assert(row==rows);
865
866
867 final int bestRefStop=refStartLoc+col-1;
868
869 while(row>0 && col>0){
870 // System.err.println("state="+state+", row="+row+", col="+col);
871
872
873
874 // byte prev0=(byte)(packed[state][row][col]&MODEMASK);
875
876 final int time=packed[state][row][col]&TIMEMASK;
877 final byte prev;
878
879 if(state==MODE_MS){
880 if(time>1){prev=(byte)state;}
881 else{
882 final int scoreFromDiag=packed[MODE_MS][row-1][col-1]&SCOREMASK;
883 final int scoreFromDel=packed[MODE_DEL][row-1][col-1]&SCOREMASK;
884 final int scoreFromIns=packed[MODE_INS][row-1][col-1]&SCOREMASK;
885 if(scoreFromDiag>=scoreFromDel && scoreFromDiag>=scoreFromIns){prev=MODE_MS;}
886 else if(scoreFromDel>=scoreFromIns){prev=MODE_DEL;}
887 else{prev=MODE_INS;}
888 }
889 row--;
890 col--;
891 }else if(state==MODE_DEL){
892 if(time>1){prev=(byte)state;}
893 else{
894 final int scoreFromDiag=packed[MODE_MS][row][col-1]&SCOREMASK;
895 final int scoreFromDel=packed[MODE_DEL][row][col-1]&SCOREMASK;
896 if(scoreFromDiag>=scoreFromDel){prev=MODE_MS;}
897 else{prev=MODE_DEL;}
898 }
899 col--;
900 }else{
901 assert(state==MODE_INS);
902 if(time>1){prev=(byte)state;}
903 else{
904 final int scoreFromDiag=packed[MODE_MS][row-1][col]&SCOREMASK;
905 final int scoreFromIns=packed[MODE_INS][row-1][col]&SCOREMASK;
906 if(scoreFromDiag>=scoreFromIns){prev=MODE_MS;}
907 else{prev=MODE_INS;}
908 }
909 row--;
910 }
911
912 if(col<0){
913 System.err.println(row);
914 break; //prevents an out of bounds access
915
916 }
917
918 // assert(prev==prev0);
919 state=prev;
920
921 // System.err.println("state2="+state+", row2="+row+", col2="+col+"\n");
922 }
923 // assert(false) : row+", "+col;
924 if(row>col){
925 col-=row;
926 }
927
928 final int bestRefStart=refStartLoc+col;
929
930 score>>=SCOREOFFSET;
931 int[] rvec;
932 if(bestRefStart<refStartLoc || bestRefStop>refEndLoc){ //Suggest extra padding in cases of overflow
933 int padLeft=Tools.max(0, refStartLoc-bestRefStart);
934 int padRight=Tools.max(0, bestRefStop-refEndLoc);
935 rvec=new int[] {score, bestRefStart, bestRefStop, padLeft, padRight};
936 }else{
937 rvec=new int[] {score, bestRefStart, bestRefStop};
938 }
939 return rvec;
940 }
941
942
943 /** Will not fill areas that cannot match minScore.
944 * @return {score, bestRefStart, bestRefStop} */
945 public final int[] fillAndScoreLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
946 int a=Tools.max(0, refStartLoc);
947 int b=Tools.min(ref.length-1, refEndLoc);
948 assert(b>=a);
949
950 int[] score;
951
952 if(b-a>=maxColumns){
953 System.err.println("Warning: Max alignment columns exceeded; restricting range. "+(b-a+1)+" > "+maxColumns);
954 assert(false) : refStartLoc+", "+refEndLoc;
955 b=Tools.min(ref.length-1, a+maxColumns-1);
956 }
957 int[] max=fillLimited(read, ref, a, b, minScore);
958 score=(max==null ? null : score(read, ref, a, b, max[0], max[1], max[2]));
959
960 return score;
961 }
962
963
964
965 public final static int scoreNoIndels(byte[] read, SiteScore ss){
966 ChromosomeArray cha=Data.getChromosome(ss.chrom);
967 return scoreNoIndels(read, cha.array, ss.start, ss);
968 }
969
970 public final static int scoreNoIndels(byte[] read, final int chrom, final int refStart){
971 ChromosomeArray cha=Data.getChromosome(chrom);
972 return scoreNoIndels(read, cha.array, refStart, null);
973 }
974
975 public final static int scoreNoIndels(byte[] read, SiteScore ss, byte[] baseScores){
976 ChromosomeArray cha=Data.getChromosome(ss.chrom);
977 return scoreNoIndels(read, cha.array, baseScores, ss.start, ss);
978 }
979
980 public final static int scoreNoIndels(byte[] read, final int chrom, final int refStart, byte[] baseScores){
981 ChromosomeArray cha=Data.getChromosome(chrom);
982 return scoreNoIndels(read, cha.array, baseScores, refStart, null);
983 }
984
985 public final static int scoreNoIndels(byte[] read, byte[] ref, final int refStart, final SiteScore ss){
986
987 int score=0;
988 int mode=-1;
989 int timeInMode=0;
990
991 //This block handles cases where the read runs outside the reference
992 //Of course, padding the reference with 'N' would be better, but...
993 int readStart=0;
994 int readStop=read.length;
995 final int refStop=refStart+read.length;
996 boolean semiperfect=true;
997 int norefs=0;
998
999 if(refStart<0){
1000 readStart=0-refStart;
1001 score+=POINTS_NOREF*readStart;
1002 norefs+=readStart;
1003 }
1004 if(refStop>ref.length){
1005 int dif=(refStop-ref.length);
1006 readStop-=dif;
1007 score+=POINTS_NOREF*dif;
1008 norefs+=dif;
1009 }
1010
1011 // if(refStart<0 || refStart+read.length>ref.length){return -99999;} //No longer needed.
1012
1013 for(int i=readStart; i<readStop; i++){
1014 byte c=read[i];
1015 byte r=ref[refStart+i];
1016
1017 if(c==r && c!='N'){
1018 if(mode==MODE_MS){
1019 timeInMode++;
1020 score+=POINTS_MATCH2;
1021 }else{
1022 timeInMode=0;
1023 score+=POINTS_MATCH;
1024 }
1025 mode=MODE_MS;
1026 }else if(c<0 || c=='N'){
1027 score+=POINTS_NOCALL;
1028 semiperfect=false;
1029 }else if(r<0 || r=='N'){
1030 score+=POINTS_NOREF;
1031 norefs++;
1032 }else{
1033 if(mode==MODE_SUB){timeInMode++;}
1034 else{timeInMode=0;}
1035
1036 if(timeInMode==0){score+=POINTS_SUB;}
1037 else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;}
1038 else{score+=POINTS_SUB3;}
1039 mode=MODE_SUB;
1040 semiperfect=false;
1041 }
1042 }
1043
1044 if(semiperfect && ss!=null){ss.semiperfect=((ss.stop==ss.start+read.length-1) && (norefs<=read.length/2));}
1045
1046 return score;
1047 }
1048
1049
1050 public final static int scoreNoIndels(byte[] read, byte[] ref, byte[] baseScores, final int refStart, SiteScore ss){
1051
1052 int score=0;
1053 int mode=-1;
1054 int timeInMode=0;
1055 int norefs=0;
1056
1057 //This block handles cases where the read runs outside the reference
1058 //Of course, padding the reference with 'N' would be better, but...
1059 int readStart=0;
1060 int readStop=read.length;
1061 final int refStop=refStart+read.length;
1062 boolean semiperfect=true;
1063
1064 if(refStart<0){
1065 readStart=0-refStart;
1066 score+=POINTS_NOREF*readStart;
1067 norefs+=readStart;
1068 }
1069 if(refStop>ref.length){
1070 int dif=(refStop-ref.length);
1071 readStop-=dif;
1072 score+=POINTS_NOREF*dif;
1073 norefs+=dif;
1074 }
1075
1076 // if(refStart<0 || refStart+read.length>ref.length){return -99999;} //No longer needed.
1077
1078 for(int i=readStart; i<readStop; i++){
1079 byte c=read[i];
1080 byte r=ref[refStart+i];
1081
1082 if(c==r && c!='N'){
1083 if(mode==MODE_MS){
1084 timeInMode++;
1085 score+=POINTS_MATCH2;
1086 }else{
1087 timeInMode=0;
1088 score+=POINTS_MATCH;
1089 }
1090 score+=baseScores[i];
1091 mode=MODE_MS;
1092 }else if(c<0 || c=='N'){
1093 score+=POINTS_NOCALL;
1094 semiperfect=false;
1095 }else if(r<0 || r=='N'){
1096 score+=POINTS_NOREF;
1097 norefs++;
1098 }else{
1099 if(mode==MODE_SUB){timeInMode++;}
1100 else{timeInMode=0;}
1101
1102 if(timeInMode==0){score+=POINTS_SUB;}
1103 else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;}
1104 else{score+=POINTS_SUB3;}
1105 mode=MODE_SUB;
1106 semiperfect=false;
1107 }
1108 }
1109
1110 if(semiperfect && ss!=null){ss.semiperfect=((ss.stop==ss.start+read.length-1) && (norefs<=read.length/2));}
1111 assert(Read.CHECKSITE(ss, read, -1));
1112
1113 return score;
1114 }
1115
1116
1117 public final static int scoreNoIndelsAndMakeMatchString(byte[] read, byte[] ref, byte[] baseScores, final int refStart, byte[][] matchReturn){
1118 int score=0;
1119 int mode=-1;
1120 int timeInMode=0;
1121
1122 assert(refStart<=ref.length) : refStart+", "+ref.length;
1123
1124 //This block handles cases where the read runs outside the reference
1125 //Of course, padding the reference with 'N' would be better, but...
1126 int readStart=0;
1127 int readStop=read.length;
1128 final int refStop=refStart+read.length;
1129 if(refStart<0){
1130 readStart=0-refStart;
1131 score+=POINTS_NOREF*readStart;
1132 }
1133 if(refStop>ref.length){
1134 int dif=(refStop-ref.length);
1135 System.err.println("dif="+dif+", ref.length="+ref.length+", refStop="+refStop);
1136 readStop-=dif;
1137 score+=POINTS_NOREF*dif;
1138 }
1139 assert(refStart+readStop<=ref.length) : "readStart="+readStart+", readStop="+readStop+
1140 ", refStart="+refStart+", refStop="+refStop+", ref.length="+ref.length+", read.length="+read.length;
1141
1142 assert(matchReturn!=null);
1143 assert(matchReturn.length==1);
1144 if(matchReturn[0]==null || matchReturn[0].length!=read.length){
1145 assert(matchReturn[0]==null || matchReturn[0].length<read.length) : matchReturn[0].length+"!="+read.length;
1146 matchReturn[0]=new byte[read.length];
1147 }
1148 final byte[] match=matchReturn[0];
1149
1150 // if(refStart<0 || refStart+read.length>ref.length){return -99999;} //No longer needed.
1151
1152 for(int i=readStart; i<readStop; i++){
1153 byte c=read[i];
1154 byte r=ref[refStart+i];
1155
1156 assert(r!='.' && c!='.');
1157
1158 if(c==r && c!='N'){
1159 if(mode==MODE_MS){
1160 timeInMode++;
1161 score+=POINTS_MATCH2;
1162 }else{
1163 timeInMode=0;
1164 score+=POINTS_MATCH;
1165 }
1166 score+=baseScores[i];
1167 match[i]='m';
1168 mode=MODE_MS;
1169 }else if(c<0 || c=='N'){
1170 score+=POINTS_NOCALL;
1171 match[i]='N';
1172 }else if(r<0 || r=='N'){
1173 score+=POINTS_NOREF;
1174 // match[i]='m';
1175 match[i]='N';
1176 }else{
1177 match[i]='S';
1178 if(mode==MODE_SUB){timeInMode++;}
1179 else{timeInMode=0;}
1180
1181 if(timeInMode==0){score+=POINTS_SUB;}
1182 else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;}
1183 else{score+=POINTS_SUB3;}
1184 mode=MODE_SUB;
1185 }
1186 }
1187
1188 return score;
1189 }
1190
1191
1192 public final static int scoreNoIndelsAndMakeMatchString(byte[] read, byte[] ref, final int refStart, byte[][] matchReturn){
1193 int score=0;
1194 int mode=-1;
1195 int timeInMode=0;
1196
1197 assert(refStart<=ref.length) : refStart+", "+ref.length;
1198
1199 //This block handles cases where the read runs outside the reference
1200 //Of course, padding the reference with 'N' would be better, but...
1201 int readStart=0;
1202 int readStop=read.length;
1203 final int refStop=refStart+read.length;
1204 if(refStart<0){
1205 readStart=0-refStart;
1206 score+=POINTS_NOREF*readStart;
1207 }
1208 if(refStop>ref.length){
1209 int dif=(refStop-ref.length);
1210 System.err.println("dif="+dif+", ref.length="+ref.length+", refStop="+refStop);
1211 readStop-=dif;
1212 score+=POINTS_NOREF*dif;
1213 }
1214 assert(refStart+readStop<=ref.length) : "readStart="+readStart+", readStop="+readStop+
1215 ", refStart="+refStart+", refStop="+refStop+", ref.length="+ref.length+", read.length="+read.length;
1216
1217 assert(matchReturn!=null);
1218 assert(matchReturn.length==1);
1219 if(matchReturn[0]==null || matchReturn[0].length!=read.length){
1220 assert(matchReturn[0]==null || matchReturn[0].length<read.length) : matchReturn[0].length+"!="+read.length;
1221 matchReturn[0]=new byte[read.length];
1222 }
1223 final byte[] match=matchReturn[0];
1224
1225 // if(refStart<0 || refStart+read.length>ref.length){return -99999;} //No longer needed.
1226
1227 for(int i=readStart; i<readStop; i++){
1228 byte c=read[i];
1229 byte r=ref[refStart+i];
1230
1231 assert(r!='.' && c!='.');
1232
1233 if(c==r && c!='N'){
1234 if(mode==MODE_MS){
1235 timeInMode++;
1236 score+=POINTS_MATCH2;
1237 }else{
1238 timeInMode=0;
1239 score+=POINTS_MATCH;
1240 }
1241 match[i]='m';
1242 mode=MODE_MS;
1243 }else if(c<0 || c=='N'){
1244 score+=POINTS_NOCALL;
1245 match[i]='N';
1246 }else if(r<0 || r=='N'){
1247 score+=POINTS_NOREF;
1248 // match[i]='m';
1249 match[i]='N';
1250 }else{
1251 match[i]='S';
1252 if(mode==MODE_SUB){timeInMode++;}
1253 else{timeInMode=0;}
1254
1255 if(timeInMode==0){score+=POINTS_SUB;}
1256 else if(timeInMode<LIMIT_FOR_COST_3){score+=POINTS_SUB2;}
1257 else{score+=POINTS_SUB3;}
1258 mode=MODE_SUB;
1259 }
1260 }
1261
1262 return score;
1263 }
1264
1265 public static final int maxQuality(int numBases){
1266 return POINTS_MATCH+(numBases-1)*(POINTS_MATCH2);
1267 }
1268
1269 public static final int maxQuality(byte[] baseScores){
1270 return POINTS_MATCH+(baseScores.length-1)*(POINTS_MATCH2)+Tools.sumInt(baseScores);
1271 }
1272
1273 public static final int maxImperfectScore(int numBases){
1274 // int maxQ=maxQuality(numBases);
1275 //// maxImperfectSwScore=maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB);
1276 // int maxI=maxQ+POINTS_DEL;
1277 // maxI=Tools.max(maxI, maxQ+POINTS_INS-POINTS_MATCH2);
1278 // maxI=Tools.min(maxI, maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB));
1279
1280 int maxQ=maxQuality(numBases);
1281 int maxI=maxQ+Tools.min(POINTS_DEL, POINTS_INS-POINTS_MATCH2);
1282 assert(maxI<(maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB)));
1283 return maxI;
1284 }
1285
1286 public static final int maxImperfectScore(byte[] baseScores){
1287 // int maxQ=maxQuality(numBases);
1288 //// maxImperfectSwScore=maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB);
1289 // int maxI=maxQ+POINTS_DEL;
1290 // maxI=Tools.max(maxI, maxQ+POINTS_INS-POINTS_MATCH2);
1291 // maxI=Tools.min(maxI, maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB));
1292
1293 int maxQ=maxQuality(baseScores);
1294 int maxI=maxQ+Tools.min(POINTS_DEL, POINTS_INS-POINTS_MATCH2);
1295 assert(maxI<(maxQ-(POINTS_MATCH2+POINTS_MATCH2)+(POINTS_MATCH+POINTS_SUB)));
1296 return maxI;
1297 }
1298
1299 public static final String toString(int[] a){
1300
1301 int width=7;
1302
1303 StringBuilder sb=new StringBuilder((a.length+1)*width+2);
1304 for(int num : a){
1305 String s=" "+num;
1306 int spaces=width-s.length();
1307 assert(spaces>=0) : width+", "+s.length()+", "+s+", "+num+", "+spaces;
1308 for(int i=0; i<spaces; i++){sb.append(' ');}
1309 sb.append(s);
1310 }
1311
1312 return sb.toString();
1313 }
1314
1315 public static final String toTimePacked(int[] a){
1316 int width=7;
1317
1318 StringBuilder sb=new StringBuilder((a.length+1)*width+2);
1319 for(int num_ : a){
1320 int num=num_&TIMEMASK;
1321 String s=" "+num;
1322 int spaces=width-s.length();
1323 assert(spaces>=0) : width+", "+s.length()+", "+s+", "+num+", "+spaces;
1324 for(int i=0; i<spaces; i++){sb.append(' ');}
1325 sb.append(s);
1326 }
1327
1328 return sb.toString();
1329 }
1330
1331 public static final String toScorePacked(int[] a){
1332 int width=7;
1333
1334 String minString=" -";
1335 String maxString=" ";
1336 while(minString.length()<width){minString+='9';}
1337 while(maxString.length()<width){maxString+='9';}
1338
1339 StringBuilder sb=new StringBuilder((a.length+1)*width+2);
1340 for(int num_ : a){
1341 int num=num_>>SCOREOFFSET;
1342 String s=" "+num;
1343 if(s.length()>width){s=num>0 ? maxString : minString;}
1344 int spaces=width-s.length();
1345 assert(spaces>=0) : width+", "+s.length()+", "+s+", "+num+", "+spaces;
1346 for(int i=0; i<spaces; i++){sb.append(' ');}
1347 sb.append(s);
1348 }
1349
1350 return sb.toString();
1351 }
1352
1353 public static final String toString(byte[] a){
1354
1355 int width=6;
1356
1357 StringBuilder sb=new StringBuilder((a.length+1)*width+2);
1358 for(int num : a){
1359 String s=" "+num;
1360 int spaces=width-s.length();
1361 assert(spaces>=0);
1362 for(int i=0; i<spaces; i++){sb.append(' ');}
1363 sb.append(s);
1364 }
1365
1366 return sb.toString();
1367 }
1368
1369 public static final String toString(byte[] ref, int startLoc, int stopLoc){
1370 StringBuilder sb=new StringBuilder(stopLoc-startLoc+1);
1371 for(int i=startLoc; i<=stopLoc; i++){sb.append((char)ref[i]);}
1372 return sb.toString();
1373 }
1374
1375 public static int calcDelScore(int len){
1376 if(len<=0){return 0;}
1377 int score=POINTS_DEL;
1378
1379 if(len>LIMIT_FOR_COST_3){
1380 score+=(len-LIMIT_FOR_COST_3)*POINTS_DEL3;
1381 len=LIMIT_FOR_COST_3;
1382 }
1383 if(len>1){
1384 score+=(len-1)*POINTS_DEL2;
1385 }
1386 return score;
1387 }
1388
1389 private static int calcDelScoreOffset(int len){
1390 if(len<=0){return 0;}
1391 int score=POINTSoff_DEL;
1392
1393 if(len>LIMIT_FOR_COST_3){
1394 score+=(len-LIMIT_FOR_COST_3)*POINTSoff_DEL3;
1395 len=LIMIT_FOR_COST_3;
1396 }
1397 if(len>1){
1398 score+=(len-1)*POINTSoff_DEL2;
1399 }
1400 return score;
1401 }
1402
1403 public static int calcInsScore(int len){
1404 if(len<=0){return 0;}
1405 int score=POINTS_INS;
1406
1407 if(len>LIMIT_FOR_COST_3){
1408 score+=(len-LIMIT_FOR_COST_3)*POINTS_INS3;
1409 len=LIMIT_FOR_COST_3;
1410 }
1411 if(len>1){
1412 score+=(len-1)*POINTS_INS2;
1413 }
1414 return score;
1415 }
1416
1417 private static int calcInsScoreOffset(int len){
1418 if(len<=0){return 0;}
1419 int score=POINTSoff_INS;
1420
1421 if(len>LIMIT_FOR_COST_3){
1422 score+=(len-LIMIT_FOR_COST_3)*POINTSoff_INS3;
1423 len=LIMIT_FOR_COST_3;
1424 }
1425 if(len>1){
1426 score+=(len-1)*POINTSoff_INS2;
1427 }
1428 return score;
1429 }
1430
1431
1432 private final int maxRows;
1433 private final int maxColumns;
1434
1435 private final int[][][] packed;
1436
1437 private final int[] vertLimit;
1438 private final int[] horizLimit;
1439
1440 private final int[] insScoreArray;
1441 private final int[] delScoreArray;
1442 private final int[] matchScoreArray;
1443 private final int[] subScoreArray;
1444
1445 CharSequence showVertLimit(){
1446 StringBuilder sb=new StringBuilder();
1447 for(int i=0; i<=rows; i++){sb.append(vertLimit[i]>>SCOREOFFSET).append(",");}
1448 return sb;
1449 }
1450 CharSequence showHorizLimit(){
1451 StringBuilder sb=new StringBuilder();
1452 for(int i=0; i<=columns; i++){sb.append(horizLimit[i]>>SCOREOFFSET).append(",");}
1453 return sb;
1454 }
1455
1456 // public static final int MODEBITS=2;
1457 public static final int TIMEBITS=12;
1458 public static final int SCOREBITS=32-TIMEBITS;
1459 public static final int MAX_TIME=((1<<TIMEBITS)-1);
1460 public static final int MAX_SCORE=((1<<(SCOREBITS-1))-1)-2000;
1461 public static final int MIN_SCORE=0-MAX_SCORE; //Keeps it 1 point above "BAD".
1462
1463 // public static final int MODEOFFSET=0; //Always zero.
1464 // public static final int TIMEOFFSET=0;
1465 public static final int SCOREOFFSET=TIMEBITS;
1466
1467 // public static final int MODEMASK=~((-1)<<MODEBITS);
1468 // public static final int TIMEMASK=(~((-1)<<TIMEBITS))<<TIMEOFFSET;
1469 public static final int TIMEMASK=~((-1)<<TIMEBITS);
1470 public static final int SCOREMASK=(~((-1)<<SCOREBITS))<<SCOREOFFSET;
1471
1472 private static final byte MODE_MS=0;
1473 private static final byte MODE_DEL=1;
1474 private static final byte MODE_INS=2;
1475 private static final byte MODE_SUB=3;
1476
1477 public static final int POINTS_NOREF=-10;
1478 public static final int POINTS_NOCALL=-10;
1479 public static final int POINTS_MATCH=90;
1480 public static final int POINTS_MATCH2=100; //Note: Changing to 90 substantially reduces false positives
1481 public static final int POINTS_SUB=-143;
1482 public static final int POINTS_SUBR=-161; //increased penalty if prior match streak was at most 1
1483 public static final int POINTS_SUB2=-54;
1484 public static final int POINTS_SUB3=-35;
1485 public static final int POINTS_INS=-207;
1486 public static final int POINTS_INS2=-51;
1487 public static final int POINTS_INS3=-37;
1488 public static final int POINTS_INS4=-15;
1489 public static final int POINTS_DEL=-273;
1490 public static final int POINTS_DEL2=-38;
1491 public static final int POINTS_DEL3=-27;
1492
1493
1494 public static final int LIMIT_FOR_COST_3=5;
1495
1496 public static final int BAD=MIN_SCORE-1;
1497
1498
1499 public static final int POINTSoff_NOREF=(POINTS_NOREF<<SCOREOFFSET);
1500 public static final int POINTSoff_NOCALL=(POINTS_NOCALL<<SCOREOFFSET);
1501 public static final int POINTSoff_MATCH=(POINTS_MATCH<<SCOREOFFSET);
1502 public static final int POINTSoff_MATCH2=(POINTS_MATCH2<<SCOREOFFSET);
1503 public static final int POINTSoff_SUB=(POINTS_SUB<<SCOREOFFSET);
1504 public static final int POINTSoff_SUBR=(POINTS_SUBR<<SCOREOFFSET);
1505 public static final int POINTSoff_SUB2=(POINTS_SUB2<<SCOREOFFSET);
1506 public static final int POINTSoff_SUB3=(POINTS_SUB3<<SCOREOFFSET);
1507 public static final int POINTSoff_INS=(POINTS_INS<<SCOREOFFSET);
1508 public static final int POINTSoff_INS2=(POINTS_INS2<<SCOREOFFSET);
1509 public static final int POINTSoff_INS3=(POINTS_INS3<<SCOREOFFSET);
1510 public static final int POINTSoff_INS4=(POINTS_INS4<<SCOREOFFSET);
1511 public static final int POINTSoff_DEL=(POINTS_DEL<<SCOREOFFSET);
1512 public static final int POINTSoff_DEL2=(POINTS_DEL2<<SCOREOFFSET);
1513 public static final int POINTSoff_DEL3=(POINTS_DEL3<<SCOREOFFSET);
1514 public static final int BADoff=(BAD<<SCOREOFFSET);
1515 public static final int MAXoff_SCORE=MAX_SCORE<<SCOREOFFSET;
1516 public static final int MINoff_SCORE=MIN_SCORE<<SCOREOFFSET;
1517
1518 private int rows;
1519 private int columns;
1520
1521 public long iterationsLimited=0;
1522 public long iterationsUnlimited=0;
1523
1524 public boolean verbose=false;
1525 public boolean verbose2=false;
1526
1527 }