Debian Codegraph @

current/aligner/SingleStateAlignerFlat2.java

raw symbols SCIP
1 package aligner;
2
3 import dna.AminoAcid;
4 import shared.KillSwitch;
5 import shared.Tools;
6
7 /**
8 * Based on SSAFlat, but with previous state pointers removed. */
9 public final class SingleStateAlignerFlat2 implements Aligner {
10
11
12 public SingleStateAlignerFlat2(){}
13
14 private void prefillTopRow(){
15 final int[] header=packed[0];
16 final int qlen=rows;
17 for(int i=0; i<=columns; i++){
18 int x=columns-i+1;
19 int qbases=qlen-x;
20
21 //Minimal points to prefer a leftmost alignment
22 header[i]=qbases<=0 ? 0 : -qbases;
23
24 //Forces consumption of query, but does not allow for insertions...
25 // header[i]=qbases<=0 ? 0 : calcDelScoreOffset(qbases);
26 }
27 }
28
29 private void prefillLeftColumnStartingAt(int i){
30 packed[0][0]=MODE_MATCH;
31 i=Tools.max(1, i);
32 for(int score=MODE_INS+(POINTS_INS*i); i<=maxRows; i++){//Fill column 0 with insertions
33 score+=POINTS_INS;
34 packed[i][0]=score;
35 }
36 }
37
38 private void initialize(int rows_, int columns_){
39 rows=rows_;
40 columns=columns_;
41 if(rows<=maxRows && columns<=maxColumns){
42 prefillTopRow();
43 // prefillLeftColumn();
44 return;
45 }
46
47 final int maxRows0=maxRows;
48 final int maxColumns0=maxColumns;
49 final int[][] packed0=packed;
50
51 //Monotonic increase
52 maxRows=Tools.max(maxRows, rows+10);
53 maxColumns=Tools.max(maxColumns, columns+10);
54
55 if(packed==null || maxColumns>maxColumns0){//Make a new matrix
56 packed=KillSwitch.allocInt2D(maxRows+1, maxColumns+1);
57 prefillLeftColumnStartingAt(1);
58 }else{//Copy old rows
59 assert(maxRows0>0 && maxColumns0>0);
60 assert(maxRows>maxRows0 && maxColumns<=maxColumns0) : "rows="+rows+",maxRows="+maxRows+
61 ",maxRows0="+maxRows0+",columns="+columns+",maxColumns="+maxColumns+",maxColumns0="+maxColumns0;
62 packed=KillSwitch.allocInt2D(maxRows+1);
63 for(int i=0; i<packed.length; i++){
64 if(i<packed0.length){
65 packed[i]=packed0[i];
66 }else{
67 packed[i]=KillSwitch.allocInt1D(maxColumns+1);
68 }
69 }
70 //Fill column 0 with insertions
71 prefillLeftColumnStartingAt(maxRows0);
72 }
73 prefillTopRow();
74 }
75
76 /** return new int[] {rows, maxCol, maxState, maxScore, maxStart};
77 * Will not fill areas that cannot match minScore */
78 @Override
79 public final int[] fillLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
80 return fillUnlimited(read, ref, refStartLoc, refEndLoc, minScore);
81 }
82
83 @Override
84 public final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc){
85 return fillUnlimited(read, ref, refStartLoc, refEndLoc, -999999);
86 }
87
88 /** return new int[] {rows, maxCol, maxState, maxScore, maxStart};
89 * Min score is optional */
90 @Override
91 public final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
92 initialize(read.length, refEndLoc-refStartLoc+1);
93
94 //temporary, for finding a bug
95 if(rows>maxRows || columns>maxColumns){
96 throw new RuntimeException("rows="+rows+", maxRows="+maxRows+", cols="+columns+", maxCols="+maxColumns+"\n"+new String(read)+"\n");
97 }
98
99 assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows;
100 assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns;
101
102 assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc;
103 assert(refEndLoc<ref.length) : "Check that values are in-bounds before calling this function: "+refEndLoc+", "+ref.length;
104
105 final int refOffset=refStartLoc-1;
106 for(int row=1; row<=rows; row++){
107
108 final byte qBase=read[row-1];
109 for(int col=1; col<=columns; col++){
110
111 final byte rBase=ref[refOffset+col];
112
113 final boolean match=(qBase==rBase);
114 final boolean defined=(qBase!='N' && rBase!='N');
115
116 final int scoreFromDiag=packed[row-1][col-1];
117 final int scoreFromDel=packed[row][col-1];
118 final int scoreFromIns=packed[row-1][col];
119
120 final int diagScoreM=POINTS_MATCH;
121 final int diagScoreS=POINTS_SUB;
122 final int delScore=scoreFromDel+POINTS_DEL;
123 final int insScore=scoreFromIns+POINTS_INS;
124
125 // final int diagScore=scoreFromDiag+(defined ? (match ? diagScoreM : diagScoreS) : POINTS_NOREF);
126 int diagScore=(match ? diagScoreM : diagScoreS);
127 diagScore=scoreFromDiag+(defined ? diagScore : POINTS_NOREF);
128
129 int score=diagScore>=delScore ? diagScore : delScore;
130 score=score>=insScore ? score : insScore;
131
132 packed[row][col]=score;
133 }
134 //iterationsUnlimited+=columns;
135 }
136
137
138 int maxCol=-1;
139 int maxState=-1;
140 int maxStart=-1;
141 int maxScore=Integer.MIN_VALUE;
142
143 for(int col=1; col<=columns; col++){
144 int x=packed[rows][col];
145 if(x>maxScore){
146 maxScore=x;
147 maxCol=col;
148
149 // assert(rows-1<read.length) : (rows-1)+", "+read.length;
150 // assert(refOffset+col<ref.length) : refOffset+", "+col+", "+ref.length;
151 maxState=getState(rows, col, read[rows-1], ref[refOffset+col]);
152 maxStart=x;
153 }
154 }
155
156 // System.err.println("Returning "+rows+", "+maxCol+", "+maxState+", "+maxScore+"; minScore="+minScore);
157 return maxScore<minScore ? null : new int[] {rows, maxCol, maxState, maxScore, maxStart};
158 }
159
160 int getState(int row, int col, byte q, byte r){//zxvzxcv TODO: Fix - needs to find max
161 final boolean match=(q==r);
162 final boolean defined=(q!='N' && r!='N');
163
164 final int scoreFromDiag=packed[row-1][col-1];
165 final int scoreFromDel=packed[row][col-1];
166 final int scoreFromIns=packed[row-1][col];
167 // final int score=packed[row][col];
168
169 final int diagScoreM=POINTS_MATCH;
170 final int diagScoreS=POINTS_SUB;
171 final int delScore=scoreFromDel+POINTS_DEL;
172 final int insScore=scoreFromIns+POINTS_INS;
173
174 final int diagScore=scoreFromDiag+(defined ? (match ? diagScoreM : diagScoreS) : POINTS_NOREF);
175
176 // int score2=diagScore>=delScore ? diagScore : delScore;
177 // score2=score>=insScore ? score : insScore;
178
179 // assert(score==score2) : score+", "+score2;
180
181 if(diagScore>=delScore && diagScore>=insScore){
182 return defined ? match ? MODE_MATCH : MODE_SUB : MODE_N;
183 }else if(delScore>=insScore){
184 return MODE_DEL;
185 }
186 return MODE_INS;
187 }
188
189
190 /** Generates the match string.
191 * State is NOT used. */
192 @Override
193 public final byte[] traceback(byte[] query, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state){
194 // assert(false);
195 assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc;
196 assert(row==rows);
197
198 byte[] out=new byte[row+col-1]; //TODO if an out of bound crash occurs, try removing the "-1".
199 int outPos=0;
200
201 // assert(state==(packed[row][col]&MODEMASK));
202
203 while(row>0 && col>0){
204 byte q=query[row-1];
205 byte r=ref[refStartLoc+col-1];
206 boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r));
207 state=getState(row, col, q, r);
208 if(state==MODE_MATCH){
209 col--;
210 row--;
211 out[outPos]=defined ? (byte)'m' : (byte)'N';
212 }else if(state==MODE_SUB){
213 col--;
214 row--;
215 out[outPos]=defined ? (byte)'S' : (byte)'N';
216 }else if(state==MODE_N){
217 col--;
218 row--;
219 out[outPos]='N';
220 }else if(state==MODE_DEL){
221 col--;
222 out[outPos]='D';
223 }else if(state==MODE_INS){
224 row--;
225 // out[outPos]='I';
226 // out[outPos]=(col<0 || col>=columns ? (byte)'C' : (byte)'I');
227 if(col>=0 && col<columns){
228 out[outPos]='I';
229 }else{
230 out[outPos]='C';
231 col--;
232 }
233 }else{
234 assert(false) : state;
235 }
236 outPos++;
237 }
238
239 assert(row==0 || col==0);
240 if(col!=row){
241 while(row>0){
242 out[outPos]='C';
243 outPos++;
244 row--;
245 col--;
246 }
247 if(col>0){
248 //do nothing
249 }
250 }
251
252 //Shrink and reverse the string
253 byte[] out2=new byte[outPos];
254 for(int i=0; i<outPos; i++){
255 out2[i]=out[outPos-i-1];
256 }
257 out=null;
258
259 return out2;
260 }
261
262 @Override
263 /** Generates identity;
264 * fills 'extra' with {match, sub, del, ins, N, clip} if present */
265 public float tracebackIdentity(byte[] query, byte[] ref, int refStartLoc, int refEndLoc,
266 int row, int col, int state, int[] extra){
267
268 // assert(false);
269 assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc;
270 assert(row==rows);
271
272 // assert(state==(packed[row][col]&MODEMASK));
273 int match=0, sub=0, del=0, ins=0, noref=0, clip=0;
274
275 while(row>0 && col>0){
276 byte q=query[row-1];
277 byte r=ref[refStartLoc+col-1];
278 boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r));
279 state=getState(row, col, q, r);
280 if(state==MODE_MATCH){
281 col--;
282 row--;
283 match+=(defined ? 1 : 0);
284 noref+=(defined ? 0 : 1);
285 }else if(state==MODE_SUB){
286 col--;
287 row--;
288 sub+=(defined ? 1 : 0);
289 noref+=(defined ? 0 : 1);
290 }else if(state==MODE_N){
291 col--;
292 row--;
293 noref++;
294 }else if(state==MODE_DEL){
295 col--;
296 del++;
297 }else if(state==MODE_INS){
298 row--;
299 boolean edge=(col<=1 || col>=columns);
300 ins+=(edge ? 0 : 1);
301 clip+=(edge ? 1 : 0);
302 }else{
303 assert(false) : state;
304 }
305 }
306
307 assert(row==0 || col==0);
308 if(col!=row){//Not sure what this is doing
309 while(row>0){
310 clip++;
311 row--;
312 col--;
313 }
314 if(col>0){
315 //do nothing
316 }
317 }
318
319 if(extra!=null){
320 assert(extra.length==5);
321 extra[0]=match;
322 extra[1]=sub;
323 extra[2]=del;
324 extra[3]=ins;
325 extra[4]=noref;
326 extra[5]=clip;
327 }
328
329 float len=match+sub+ins+del+noref*0.1f;
330 float id=match/Tools.max(1.0f, len);
331 return id;
332 }
333
334 /** @return {score, bestRefStart, bestRefStop} */
335 @Override
336 public final int[] score(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc,
337 final int maxRow, final int maxCol, final int maxState/*, final int maxScore, final int maxStart*/){
338
339 int row=maxRow;
340 int col=maxCol;
341 int state=maxState;
342
343 assert(maxState>=0 && maxState<packed.length) :
344 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
345 assert(maxRow>=0 && maxRow<packed.length) :
346 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
347 assert(maxCol>=0 && maxCol<packed[0].length) :
348 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
349
350 int score=packed[maxRow][maxCol]; //Or zero, if it is to be recalculated
351
352 if(row<rows){
353 int difR=rows-row;
354 int difC=columns-col;
355
356 while(difR>difC){
357 score+=POINTS_NOREF;
358 difR--;
359 }
360
361 row+=difR;
362 col+=difR;
363
364 }
365
366 assert(refStartLoc<=refEndLoc);
367 assert(row==rows);
368
369
370 final int bestRefStop=refStartLoc+col-1;
371
372 while(row>0 && col>0){
373 final byte q=read[row-1];
374 final byte r=ref[refStartLoc+col-1];
375 // final boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r));
376 state=getState(row, col, q, r);
377 if(state==MODE_MATCH){
378 col--;
379 row--;
380 }else if(state==MODE_SUB){
381 col--;
382 row--;
383 }else if(state==MODE_N){
384 col--;
385 row--;
386 }else if(state==MODE_DEL){
387 col--;
388 }else if(state==MODE_INS){
389 row--;
390 }else{
391 assert(false) : state;
392 }
393 }
394 // assert(false) : row+", "+col;
395 if(row>col){
396 col-=row;
397 }
398
399 final int bestRefStart=refStartLoc+col;
400
401 // System.err.println("t2\t"+score+", "+maxScore+", "+maxStart+", "+bestRefStart);
402 int[] rvec;
403 if(bestRefStart<refStartLoc || bestRefStop>refEndLoc){ //Suggest extra padding in cases of overflow
404 int padLeft=Tools.max(0, refStartLoc-bestRefStart);
405 int padRight=Tools.max(0, bestRefStop-refEndLoc);
406 rvec=new int[] {score, bestRefStart, bestRefStop, padLeft, padRight};
407 }else{
408 rvec=new int[] {score, bestRefStart, bestRefStop};
409 }
410 return rvec;
411 }
412
413
414 /** Will not fill areas that cannot match minScore.
415 * @return {score, bestRefStart, bestRefStop} */
416 @Override
417 public final int[] fillAndScoreLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
418 int a=Tools.max(0, refStartLoc);
419 int b=Tools.min(ref.length-1, refEndLoc);
420 assert(b>=a);
421
422 if(b-a>=maxColumns){
423 System.err.println("Warning: Max alignment columns exceeded; restricting range. "+(b-a+1)+" > "+maxColumns);
424 assert(false) : refStartLoc+", "+refEndLoc;
425 b=Tools.min(ref.length-1, a+maxColumns-1);
426 }
427 int[] max=fillLimited(read, ref, a, b, minScore);
428 // return max==null ? null : new int[] {max[3], 0, max[1]};
429
430 int[] score=(max==null ? null : score(read, ref, a, b, max[0], max[1], max[2]/*, max[3], max[4]*/));
431
432 return score;
433 }
434
435 public static final String toString(byte[] ref, int startLoc, int stopLoc){
436 StringBuilder sb=new StringBuilder(stopLoc-startLoc+1);
437 for(int i=startLoc; i<=stopLoc; i++){sb.append((char)ref[i]);}
438 return sb.toString();
439 }
440
441 // public static int calcDelScore(int len){
442 // if(len<=0){return 0;}
443 // int score=POINTS_DEL;
444 // if(len>1){
445 // score+=(len-1)*POINTS_DEL2;
446 // }
447 // return score;
448 // }
449
450 // public int maxScoreByIdentity(int len, float identity){
451 // assert(identity>=0 && identity<=1);
452 // return (int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB));
453 // }
454
455 @Override
456 public int minScoreByIdentity(int len, float identity){
457 assert(identity>=0 && identity<=1);
458
459 int a=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB));
460 int b=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_INS));
461 int c=(int)(len*(1*POINTS_MATCH+((1/(Tools.max(identity, 0.000001f)))-1)*POINTS_DEL));
462 return Tools.min(a, b, c);
463 }
464
465 private static int calcDelScore(int len){
466 if(len<=0){return 0;}
467 int score=POINTS_DEL*len;
468 return score;
469 }
470 //
471 // public static int calcInsScore(int len){
472 // if(len<=0){return 0;}
473 // int score=POINTS_INS;
474 //
475 // if(len>1){
476 // score+=(len-1)*POINTS_INS2;
477 // }
478 // return score;
479 // }
480 //
481 // private static int calcInsScoreOffset(int len){
482 // if(len<=0){return 0;}
483 // int score=POINTS_INS;
484 //
485 // if(len>1){
486 // score+=(len-1)*POINTS_INS2;
487 // }
488 // return score;
489 // }
490
491 @Override
492 public int rows(){return rows;}
493 @Override
494 public int columns(){return columns;}
495
496
497 private int maxRows;
498 private int maxColumns;
499
500 private int[][] packed;
501
502 public static final int MAX_SCORE=Integer.MAX_VALUE-2000;
503 public static final int MIN_SCORE=0-MAX_SCORE; //Keeps it 1 point above "BAD".
504
505 //For some reason changing MODE_DEL from 1 to 0 breaks everything
506 private static final byte MODE_DEL=1;
507 private static final byte MODE_INS=2;
508 private static final byte MODE_SUB=3;
509 private static final byte MODE_MATCH=4;
510 private static final byte MODE_N=5;
511
512 public static final int POINTS_NOREF=-20;
513 public static final int POINTS_MATCH=100;
514 public static final int POINTS_SUB=-50;
515 public static final int POINTS_INS=-121;
516 public static final int POINTS_DEL=-111;
517
518 // public static final int POINTS_NOREF=-150000;
519 // public static final int POINTS_MATCH=100;
520 // public static final int POINTS_SUB=-100;
521 // public static final int POINTS_INS=-100;
522 // public static final int POINTS_DEL=-100;
523
524 public static final int BAD=MIN_SCORE-1;
525
526 private int rows;
527 private int columns;
528
529 // public long iterationsLimited=0;
530 // public long iterationsUnlimited=0;
531
532 public boolean verbose=false;
533 public boolean verbose2=false;
534
535 }