Debian Codegraph @

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