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