Debian Codegraph @

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