comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/aligner/SingleStateAlignerFlat3.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package aligner;
2
3 import dna.AminoAcid;
4 import shared.KillSwitch;
5 import shared.Tools;
6
7 /**
8 * Based on SSAFlat2, but with neutral weights. */
9 public final class SingleStateAlignerFlat3 implements Aligner {
10
11
12 public SingleStateAlignerFlat3(){}
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 @Override
192 public final byte[] traceback(byte[] query, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state){
193 // assert(false);
194 assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc;
195 assert(row==rows);
196
197 byte[] out=new byte[row+col-1]; //TODO if an out of bound crash occurs, try removing the "-1".
198 int outPos=0;
199
200 // assert(state==(packed[row][col]&MODEMASK));
201
202 while(row>0 && col>0){
203 byte q=query[row-1];
204 byte r=ref[refStartLoc+col-1];
205 boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r));
206 state=getState(row, col, q, r);
207 if(state==MODE_MATCH){
208 col--;
209 row--;
210 out[outPos]=defined ? (byte)'m' : (byte)'N';
211 }else if(state==MODE_SUB){
212 col--;
213 row--;
214 out[outPos]=defined ? (byte)'S' : (byte)'N';
215 }else if(state==MODE_N){
216 col--;
217 row--;
218 out[outPos]='N';
219 }else if(state==MODE_DEL){
220 col--;
221 out[outPos]='D';
222 }else if(state==MODE_INS){
223 row--;
224 // out[outPos]='I';
225 if(col>=0 && col<columns){
226 out[outPos]='I';
227 }else{
228 out[outPos]='C';
229 col--;
230 }
231 }else{
232 assert(false) : state;
233 }
234 outPos++;
235 }
236
237 assert(row==0 || col==0);
238 if(col!=row){//Not sure what this is doing
239 while(row>0){
240 out[outPos]='C';
241 outPos++;
242 row--;
243 col--;
244 }
245 if(col>0){
246 //do nothing
247 }
248 }
249
250 //Shrink and reverse the string
251 byte[] out2=new byte[outPos];
252 for(int i=0; i<outPos; i++){
253 out2[i]=out[outPos-i-1];
254 }
255 out=null;
256
257 return out2;
258 }
259
260 @Override
261 /** Generates identity;
262 * fills 'extra' with {match, sub, del, ins, N, clip} if present */
263 public float tracebackIdentity(byte[] query, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state, int[] extra){
264
265 // assert(false);
266 assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc;
267 assert(row==rows);
268
269 // assert(state==(packed[row][col]&MODEMASK));
270 int match=0, sub=0, del=0, ins=0, noref=0, clip=0;
271
272 while(row>0 && col>0){
273 byte q=query[row-1];
274 byte r=ref[refStartLoc+col-1];
275 boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r));
276 state=getState(row, col, q, r);
277 if(state==MODE_MATCH){
278 col--;
279 row--;
280 match+=(defined ? 1 : 0);
281 noref+=(defined ? 0 : 1);
282 }else if(state==MODE_SUB){
283 col--;
284 row--;
285 sub+=(defined ? 1 : 0);
286 noref+=(defined ? 0 : 1);
287 }else if(state==MODE_N){
288 col--;
289 row--;
290 noref++;
291 }else if(state==MODE_DEL){
292 col--;
293 del++;
294 }else if(state==MODE_INS){
295 row--;
296 boolean edge=(col<=1 || col>=columns);
297 ins+=(edge ? 0 : 1);
298 clip+=(edge ? 1 : 0);
299 }else{
300 assert(false) : state;
301 }
302 }
303
304 assert(row==0 || col==0);
305 if(col!=row){//Not sure what this is doing
306 while(row>0){
307 clip++;
308 row--;
309 col--;
310 }
311 if(col>0){
312 //do nothing
313 }
314 }
315
316 if(extra!=null){
317 assert(extra.length==5);
318 extra[0]=match;
319 extra[1]=sub;
320 extra[2]=del;
321 extra[3]=ins;
322 extra[4]=noref;
323 extra[5]=clip;
324 }
325
326 float len=match+sub+ins+del+noref*0.1f;
327 float id=match/Tools.max(1.0f, len);
328 return id;
329 }
330
331 /** @return {score, bestRefStart, bestRefStop} */
332 @Override
333 public final int[] score(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc,
334 final int maxRow, final int maxCol, final int maxState/*, final int maxScore, final int maxStart*/){
335
336 int row=maxRow;
337 int col=maxCol;
338 int state=maxState;
339
340 assert(maxState>=0 && maxState<packed.length) :
341 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
342 assert(maxRow>=0 && maxRow<packed.length) :
343 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
344 assert(maxCol>=0 && maxCol<packed[0].length) :
345 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
346
347 int score=packed[maxRow][maxCol]; //Or zero, if it is to be recalculated
348
349 if(row<rows){
350 int difR=rows-row;
351 int difC=columns-col;
352
353 while(difR>difC){
354 score+=POINTS_NOREF;
355 difR--;
356 }
357
358 row+=difR;
359 col+=difR;
360
361 }
362
363 assert(refStartLoc<=refEndLoc);
364 assert(row==rows);
365
366
367 final int bestRefStop=refStartLoc+col-1;
368
369 while(row>0 && col>0){
370 final byte q=read[row-1];
371 final byte r=ref[refStartLoc+col-1];
372 // final boolean defined=(AminoAcid.isFullyDefined(q) && AminoAcid.isFullyDefined(r));
373 state=getState(row, col, q, r);
374 if(state==MODE_MATCH){
375 col--;
376 row--;
377 }else if(state==MODE_SUB){
378 col--;
379 row--;
380 }else if(state==MODE_N){
381 col--;
382 row--;
383 }else if(state==MODE_DEL){
384 col--;
385 }else if(state==MODE_INS){
386 row--;
387 }else{
388 assert(false) : state;
389 }
390 }
391 // assert(false) : row+", "+col;
392 if(row>col){
393 col-=row;
394 }
395
396 final int bestRefStart=refStartLoc+col;
397
398 // System.err.println("t2\t"+score+", "+maxScore+", "+maxStart+", "+bestRefStart);
399 int[] rvec;
400 if(bestRefStart<refStartLoc || bestRefStop>refEndLoc){ //Suggest extra padding in cases of overflow
401 int padLeft=Tools.max(0, refStartLoc-bestRefStart);
402 int padRight=Tools.max(0, bestRefStop-refEndLoc);
403 rvec=new int[] {score, bestRefStart, bestRefStop, padLeft, padRight};
404 }else{
405 rvec=new int[] {score, bestRefStart, bestRefStop};
406 }
407 return rvec;
408 }
409
410
411 /** Will not fill areas that cannot match minScore.
412 * @return {score, bestRefStart, bestRefStop} */
413 @Override
414 public final int[] fillAndScoreLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
415 int a=Tools.max(0, refStartLoc);
416 int b=Tools.min(ref.length-1, refEndLoc);
417 assert(b>=a);
418
419 if(b-a>=maxColumns){
420 System.err.println("Warning: Max alignment columns exceeded; restricting range. "+(b-a+1)+" > "+maxColumns);
421 assert(false) : refStartLoc+", "+refEndLoc;
422 b=Tools.min(ref.length-1, a+maxColumns-1);
423 }
424 int[] max=fillLimited(read, ref, a, b, minScore);
425 // return max==null ? null : new int[] {max[3], 0, max[1]};
426
427 int[] score=(max==null ? null : score(read, ref, a, b, max[0], max[1], max[2]/*, max[3], max[4]*/));
428
429 return score;
430 }
431
432 public static final String toString(byte[] ref, int startLoc, int stopLoc){
433 StringBuilder sb=new StringBuilder(stopLoc-startLoc+1);
434 for(int i=startLoc; i<=stopLoc; i++){sb.append((char)ref[i]);}
435 return sb.toString();
436 }
437
438 // public static int calcDelScore(int len){
439 // if(len<=0){return 0;}
440 // int score=POINTS_DEL;
441 // if(len>1){
442 // score+=(len-1)*POINTS_DEL2;
443 // }
444 // return score;
445 // }
446
447 // public int maxScoreByIdentity(int len, float identity){
448 // assert(identity>=0 && identity<=1);
449 // return (int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB));
450 // }
451
452 @Override
453 public int minScoreByIdentity(int len, float identity){
454 assert(identity>=0 && identity<=1);
455
456 int a=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB));
457 int b=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_INS));
458 int c=(int)(len*(1*POINTS_MATCH+((1/(Tools.max(identity, 0.000001f)))-1)*POINTS_DEL));
459 return Tools.min(a, b, c);
460 }
461
462 private static int calcDelScore(int len){
463 if(len<=0){return 0;}
464 int score=POINTS_DEL*len;
465 return score;
466 }
467 //
468 // public static int calcInsScore(int len){
469 // if(len<=0){return 0;}
470 // int score=POINTS_INS;
471 //
472 // if(len>1){
473 // score+=(len-1)*POINTS_INS2;
474 // }
475 // return score;
476 // }
477 //
478 // private static int calcInsScoreOffset(int len){
479 // if(len<=0){return 0;}
480 // int score=POINTS_INS;
481 //
482 // if(len>1){
483 // score+=(len-1)*POINTS_INS2;
484 // }
485 // return score;
486 // }
487
488 @Override
489 public int rows(){return rows;}
490 @Override
491 public int columns(){return columns;}
492
493
494 private int maxRows;
495 private int maxColumns;
496
497 private int[][] packed;
498
499 public static final int MAX_SCORE=Integer.MAX_VALUE-2000;
500 public static final int MIN_SCORE=0-MAX_SCORE; //Keeps it 1 point above "BAD".
501
502 //For some reason changing MODE_DEL from 1 to 0 breaks everything
503 private static final byte MODE_DEL=1;
504 private static final byte MODE_INS=2;
505 private static final byte MODE_SUB=3;
506 private static final byte MODE_MATCH=4;
507 private static final byte MODE_N=5;
508
509 public static final int POINTS_NOREF=-2;
510 public static final int POINTS_MATCH=10;
511 public static final int POINTS_SUB=-10;
512 public static final int POINTS_INS=-10;
513 public static final int POINTS_DEL=-10;
514
515 public static final int BAD=MIN_SCORE-1;
516
517 private int rows;
518 private int columns;
519
520 public boolean verbose=false;
521 public boolean verbose2=false;
522
523 }