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 SSAFlat, but with previous state pointers removed. */
|
jpayne@68
|
9 public final class SingleStateAlignerFlat2Amino implements Aligner {
|
jpayne@68
|
10
|
jpayne@68
|
11
|
jpayne@68
|
12 public SingleStateAlignerFlat2Amino(){}
|
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);
|
jpayne@68
|
61 packed=KillSwitch.allocInt2D(maxRows+1);
|
jpayne@68
|
62 for(int i=0; i<packed.length; i++){
|
jpayne@68
|
63 if(i<packed0.length){
|
jpayne@68
|
64 packed[i]=packed0[i];
|
jpayne@68
|
65 }else{
|
jpayne@68
|
66 packed[i]=KillSwitch.allocInt1D(maxColumns+1);
|
jpayne@68
|
67 }
|
jpayne@68
|
68 }
|
jpayne@68
|
69 //Fill column 0 with insertions
|
jpayne@68
|
70 prefillLeftColumnStartingAt(maxRows0);
|
jpayne@68
|
71 }
|
jpayne@68
|
72 prefillTopRow();
|
jpayne@68
|
73 }
|
jpayne@68
|
74
|
jpayne@68
|
75 /** return new int[] {rows, maxCol, maxState, maxScore, maxStart};
|
jpayne@68
|
76 * Will not fill areas that cannot match minScore */
|
jpayne@68
|
77 @Override
|
jpayne@68
|
78 public final int[] fillLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
|
jpayne@68
|
79 return fillUnlimited(read, ref, refStartLoc, refEndLoc, minScore);
|
jpayne@68
|
80 }
|
jpayne@68
|
81
|
jpayne@68
|
82 @Override
|
jpayne@68
|
83 public final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc){
|
jpayne@68
|
84 return fillUnlimited(read, ref, refStartLoc, refEndLoc, -999999);
|
jpayne@68
|
85 }
|
jpayne@68
|
86
|
jpayne@68
|
87 /** return new int[] {rows, maxCol, maxState, maxScore, maxStart};
|
jpayne@68
|
88 * Min score is optional */
|
jpayne@68
|
89 @Override
|
jpayne@68
|
90 public final int[] fillUnlimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
|
jpayne@68
|
91 initialize(read.length, refEndLoc-refStartLoc+1);
|
jpayne@68
|
92
|
jpayne@68
|
93 //temporary, for finding a bug
|
jpayne@68
|
94 if(rows>maxRows || columns>maxColumns){
|
jpayne@68
|
95 throw new RuntimeException("rows="+rows+", maxRows="+maxRows+", cols="+columns+", maxCols="+maxColumns+"\n"+new String(read)+"\n");
|
jpayne@68
|
96 }
|
jpayne@68
|
97
|
jpayne@68
|
98 assert(rows<=maxRows) : "Check that values are in-bounds before calling this function: "+rows+", "+maxRows;
|
jpayne@68
|
99 assert(columns<=maxColumns) : "Check that values are in-bounds before calling this function: "+columns+", "+maxColumns;
|
jpayne@68
|
100
|
jpayne@68
|
101 assert(refStartLoc>=0) : "Check that values are in-bounds before calling this function: "+refStartLoc;
|
jpayne@68
|
102 assert(refEndLoc<ref.length) : "Check that values are in-bounds before calling this function: "+refEndLoc+", "+ref.length;
|
jpayne@68
|
103
|
jpayne@68
|
104 final int refOffset=refStartLoc-1;
|
jpayne@68
|
105 for(int row=1; row<=rows; row++){
|
jpayne@68
|
106
|
jpayne@68
|
107 final byte qBase=read[row-1];
|
jpayne@68
|
108 for(int col=1; col<=columns; col++){
|
jpayne@68
|
109
|
jpayne@68
|
110 final byte rBase=ref[refOffset+col];
|
jpayne@68
|
111
|
jpayne@68
|
112 final boolean match=(qBase==rBase);
|
jpayne@68
|
113 final boolean defined=(AminoAcid.isFullyDefinedAA(qBase) && AminoAcid.isFullyDefinedAA(rBase));
|
jpayne@68
|
114
|
jpayne@68
|
115 final int scoreFromDiag=packed[row-1][col-1];
|
jpayne@68
|
116 final int scoreFromDel=packed[row][col-1];
|
jpayne@68
|
117 final int scoreFromIns=packed[row-1][col];
|
jpayne@68
|
118
|
jpayne@68
|
119 final int diagScoreM=POINTS_MATCH;
|
jpayne@68
|
120 final int diagScoreS=POINTS_SUB;
|
jpayne@68
|
121 final int delScore=scoreFromDel+POINTS_DEL;
|
jpayne@68
|
122 final int insScore=scoreFromIns+POINTS_INS;
|
jpayne@68
|
123
|
jpayne@68
|
124 // final int diagScore=scoreFromDiag+(defined ? (match ? diagScoreM : diagScoreS) : POINTS_NOREF);
|
jpayne@68
|
125 int diagScore=(match ? diagScoreM : diagScoreS);
|
jpayne@68
|
126 diagScore=scoreFromDiag+(defined ? diagScore : POINTS_NOREF);
|
jpayne@68
|
127
|
jpayne@68
|
128 int score=diagScore>=delScore ? diagScore : delScore;
|
jpayne@68
|
129 score=score>=insScore ? score : insScore;
|
jpayne@68
|
130
|
jpayne@68
|
131 packed[row][col]=score;
|
jpayne@68
|
132 }
|
jpayne@68
|
133 //iterationsUnlimited+=columns;
|
jpayne@68
|
134 }
|
jpayne@68
|
135
|
jpayne@68
|
136
|
jpayne@68
|
137 int maxCol=-1;
|
jpayne@68
|
138 int maxState=-1;
|
jpayne@68
|
139 int maxStart=-1;
|
jpayne@68
|
140 int maxScore=Integer.MIN_VALUE;
|
jpayne@68
|
141
|
jpayne@68
|
142 for(int col=1; col<=columns; col++){
|
jpayne@68
|
143 int x=packed[rows][col];
|
jpayne@68
|
144 if(x>maxScore){
|
jpayne@68
|
145 maxScore=x;
|
jpayne@68
|
146 maxCol=col;
|
jpayne@68
|
147
|
jpayne@68
|
148 // assert(rows-1<read.length) : (rows-1)+", "+read.length;
|
jpayne@68
|
149 // assert(refOffset+col<ref.length) : refOffset+", "+col+", "+ref.length;
|
jpayne@68
|
150 maxState=getState(rows, col, read[rows-1], ref[refOffset+col]);
|
jpayne@68
|
151 maxStart=x;
|
jpayne@68
|
152 }
|
jpayne@68
|
153 }
|
jpayne@68
|
154
|
jpayne@68
|
155 // System.err.println("Returning "+rows+", "+maxCol+", "+maxState+", "+maxScore+"; minScore="+minScore);
|
jpayne@68
|
156 return maxScore<minScore ? null : new int[] {rows, maxCol, maxState, maxScore, maxStart};
|
jpayne@68
|
157 }
|
jpayne@68
|
158
|
jpayne@68
|
159 int getState(int row, int col, byte q, byte r){//zxvzxcv TODO: Fix - needs to find max
|
jpayne@68
|
160 final boolean match=(q==r);
|
jpayne@68
|
161 final boolean defined=(AminoAcid.isFullyDefinedAA(q) && AminoAcid.isFullyDefinedAA(r));
|
jpayne@68
|
162
|
jpayne@68
|
163 final int scoreFromDiag=packed[row-1][col-1];
|
jpayne@68
|
164 final int scoreFromDel=packed[row][col-1];
|
jpayne@68
|
165 final int scoreFromIns=packed[row-1][col];
|
jpayne@68
|
166 // final int score=packed[row][col];
|
jpayne@68
|
167
|
jpayne@68
|
168 final int diagScoreM=POINTS_MATCH;
|
jpayne@68
|
169 final int diagScoreS=POINTS_SUB;
|
jpayne@68
|
170 final int delScore=scoreFromDel+POINTS_DEL;
|
jpayne@68
|
171 final int insScore=scoreFromIns+POINTS_INS;
|
jpayne@68
|
172
|
jpayne@68
|
173 final int diagScore=scoreFromDiag+(defined ? (match ? diagScoreM : diagScoreS) : POINTS_NOREF);
|
jpayne@68
|
174
|
jpayne@68
|
175 // int score2=diagScore>=delScore ? diagScore : delScore;
|
jpayne@68
|
176 // score2=score>=insScore ? score : insScore;
|
jpayne@68
|
177
|
jpayne@68
|
178 // assert(score==score2) : score+", "+score2;
|
jpayne@68
|
179
|
jpayne@68
|
180 if(diagScore>=delScore && diagScore>=insScore){
|
jpayne@68
|
181 return defined ? match ? MODE_MATCH : MODE_SUB : MODE_N;
|
jpayne@68
|
182 }else if(delScore>=insScore){
|
jpayne@68
|
183 return MODE_DEL;
|
jpayne@68
|
184 }
|
jpayne@68
|
185 return MODE_INS;
|
jpayne@68
|
186 }
|
jpayne@68
|
187
|
jpayne@68
|
188 /** Generates the match string */
|
jpayne@68
|
189 @Override
|
jpayne@68
|
190 public final byte[] traceback(byte[] query, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state){
|
jpayne@68
|
191 // assert(false);
|
jpayne@68
|
192 assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc;
|
jpayne@68
|
193 assert(row==rows);
|
jpayne@68
|
194
|
jpayne@68
|
195 byte[] out=new byte[row+col-1]; //TODO if an out of bound crash occurs, try removing the "-1".
|
jpayne@68
|
196 int outPos=0;
|
jpayne@68
|
197
|
jpayne@68
|
198 // assert(state==(packed[row][col]&MODEMASK));
|
jpayne@68
|
199
|
jpayne@68
|
200 while(row>0 && col>0){
|
jpayne@68
|
201 byte q=query[row-1];
|
jpayne@68
|
202 byte r=ref[refStartLoc+col-1];
|
jpayne@68
|
203 boolean defined=(AminoAcid.isFullyDefinedAA(q) && AminoAcid.isFullyDefinedAA(r));
|
jpayne@68
|
204 state=getState(row, col, q, r);
|
jpayne@68
|
205 // assert(defined) : state+", "+(int)q+", "+(int)r+", "+new String(query);
|
jpayne@68
|
206 // assert(state!=MODE_N) : state+", "+Character.toString(q)+", "+Character.toString(r)+", "+new String(query);
|
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.isFullyDefinedAA(q) && AminoAcid.isFullyDefinedAA(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 /** Generates identity;
|
jpayne@68
|
332 * fills 'extra' with {match, sub, del, ins, N, clip} if present */
|
jpayne@68
|
333 public float tracebackIdentityAmino(byte[] query, byte[] ref, int refStartLoc, int refEndLoc, int row, int col, int state, int[] extra){
|
jpayne@68
|
334
|
jpayne@68
|
335 // assert(false);
|
jpayne@68
|
336 assert(refStartLoc<=refEndLoc) : refStartLoc+", "+refEndLoc;
|
jpayne@68
|
337 assert(row==rows);
|
jpayne@68
|
338
|
jpayne@68
|
339 // assert(state==(packed[row][col]&MODEMASK));
|
jpayne@68
|
340 int match=0, sub=0, del=0, ins=0, noref=0, clip=0;
|
jpayne@68
|
341
|
jpayne@68
|
342 while(row>0 && col>0){
|
jpayne@68
|
343 byte q=query[row-1];
|
jpayne@68
|
344 byte r=ref[refStartLoc+col-1];
|
jpayne@68
|
345 boolean defined=(AminoAcid.isFullyDefinedAA(q) && AminoAcid.isFullyDefinedAA(r));
|
jpayne@68
|
346 state=getState(row, col, q, r);
|
jpayne@68
|
347 if(state==MODE_MATCH){
|
jpayne@68
|
348 col--;
|
jpayne@68
|
349 row--;
|
jpayne@68
|
350 match+=(defined ? 1 : 0);
|
jpayne@68
|
351 noref+=(defined ? 0 : 1);
|
jpayne@68
|
352 }else if(state==MODE_SUB){
|
jpayne@68
|
353 col--;
|
jpayne@68
|
354 row--;
|
jpayne@68
|
355 sub+=(defined ? 1 : 0);
|
jpayne@68
|
356 noref+=(defined ? 0 : 1);
|
jpayne@68
|
357 }else if(state==MODE_N){
|
jpayne@68
|
358 col--;
|
jpayne@68
|
359 row--;
|
jpayne@68
|
360 noref++;
|
jpayne@68
|
361 }else if(state==MODE_DEL){
|
jpayne@68
|
362 col--;
|
jpayne@68
|
363 del++;
|
jpayne@68
|
364 }else if(state==MODE_INS){
|
jpayne@68
|
365 row--;
|
jpayne@68
|
366 boolean edge=(col<=1 || col>=columns);
|
jpayne@68
|
367 ins+=(edge ? 0 : 1);
|
jpayne@68
|
368 clip+=(edge ? 1 : 0);
|
jpayne@68
|
369 }else{
|
jpayne@68
|
370 assert(false) : state;
|
jpayne@68
|
371 }
|
jpayne@68
|
372 }
|
jpayne@68
|
373
|
jpayne@68
|
374 assert(row==0 || col==0);
|
jpayne@68
|
375 if(col!=row){//Not sure what this is doing
|
jpayne@68
|
376 while(row>0){
|
jpayne@68
|
377 clip++;
|
jpayne@68
|
378 row--;
|
jpayne@68
|
379 col--;
|
jpayne@68
|
380 }
|
jpayne@68
|
381 if(col>0){
|
jpayne@68
|
382 //do nothing
|
jpayne@68
|
383 }
|
jpayne@68
|
384 }
|
jpayne@68
|
385
|
jpayne@68
|
386 if(extra!=null){
|
jpayne@68
|
387 assert(extra.length==5);
|
jpayne@68
|
388 extra[0]=match;
|
jpayne@68
|
389 extra[1]=sub;
|
jpayne@68
|
390 extra[2]=del;
|
jpayne@68
|
391 extra[3]=ins;
|
jpayne@68
|
392 extra[4]=noref;
|
jpayne@68
|
393 extra[5]=clip;
|
jpayne@68
|
394 }
|
jpayne@68
|
395
|
jpayne@68
|
396 float len=match+sub+ins+del+noref*0.1f;
|
jpayne@68
|
397 float id=match/Tools.max(1.0f, len);
|
jpayne@68
|
398 return id;
|
jpayne@68
|
399 }
|
jpayne@68
|
400
|
jpayne@68
|
401 /** @return {score, bestRefStart, bestRefStop} */
|
jpayne@68
|
402 @Override
|
jpayne@68
|
403 public final int[] score(final byte[] read, final byte[] ref, final int refStartLoc, final int refEndLoc,
|
jpayne@68
|
404 final int maxRow, final int maxCol, final int maxState/*, final int maxScore, final int maxStart*/){
|
jpayne@68
|
405
|
jpayne@68
|
406 int row=maxRow;
|
jpayne@68
|
407 int col=maxCol;
|
jpayne@68
|
408 int state=maxState;
|
jpayne@68
|
409
|
jpayne@68
|
410 assert(maxState>=0 && maxState<packed.length) :
|
jpayne@68
|
411 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
|
jpayne@68
|
412 assert(maxRow>=0 && maxRow<packed.length) :
|
jpayne@68
|
413 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
|
jpayne@68
|
414 assert(maxCol>=0 && maxCol<packed[0].length) :
|
jpayne@68
|
415 maxState+", "+maxRow+", "+maxCol+"\n"+new String(read)+"\n"+toString(ref, refStartLoc, refEndLoc);
|
jpayne@68
|
416
|
jpayne@68
|
417 int score=packed[maxRow][maxCol]; //Or zero, if it is to be recalculated
|
jpayne@68
|
418
|
jpayne@68
|
419 if(row<rows){
|
jpayne@68
|
420 int difR=rows-row;
|
jpayne@68
|
421 int difC=columns-col;
|
jpayne@68
|
422
|
jpayne@68
|
423 while(difR>difC){
|
jpayne@68
|
424 score+=POINTS_NOREF;
|
jpayne@68
|
425 difR--;
|
jpayne@68
|
426 }
|
jpayne@68
|
427
|
jpayne@68
|
428 row+=difR;
|
jpayne@68
|
429 col+=difR;
|
jpayne@68
|
430
|
jpayne@68
|
431 }
|
jpayne@68
|
432
|
jpayne@68
|
433 assert(refStartLoc<=refEndLoc);
|
jpayne@68
|
434 assert(row==rows);
|
jpayne@68
|
435
|
jpayne@68
|
436
|
jpayne@68
|
437 final int bestRefStop=refStartLoc+col-1;
|
jpayne@68
|
438
|
jpayne@68
|
439 while(row>0 && col>0){
|
jpayne@68
|
440 final byte q=read[row-1];
|
jpayne@68
|
441 final byte r=ref[refStartLoc+col-1];
|
jpayne@68
|
442 // final boolean defined=(AminoAcid.isFullyDefinedAA(q) && AminoAcid.isFullyDefinedAA(r));
|
jpayne@68
|
443 state=getState(row, col, q, r);
|
jpayne@68
|
444 if(state==MODE_MATCH){
|
jpayne@68
|
445 col--;
|
jpayne@68
|
446 row--;
|
jpayne@68
|
447 }else if(state==MODE_SUB){
|
jpayne@68
|
448 col--;
|
jpayne@68
|
449 row--;
|
jpayne@68
|
450 }else if(state==MODE_N){
|
jpayne@68
|
451 col--;
|
jpayne@68
|
452 row--;
|
jpayne@68
|
453 }else if(state==MODE_DEL){
|
jpayne@68
|
454 col--;
|
jpayne@68
|
455 }else if(state==MODE_INS){
|
jpayne@68
|
456 row--;
|
jpayne@68
|
457 }else{
|
jpayne@68
|
458 assert(false) : state;
|
jpayne@68
|
459 }
|
jpayne@68
|
460 }
|
jpayne@68
|
461 // assert(false) : row+", "+col;
|
jpayne@68
|
462 if(row>col){
|
jpayne@68
|
463 col-=row;
|
jpayne@68
|
464 }
|
jpayne@68
|
465
|
jpayne@68
|
466 final int bestRefStart=refStartLoc+col;
|
jpayne@68
|
467
|
jpayne@68
|
468 // System.err.println("t2\t"+score+", "+maxScore+", "+maxStart+", "+bestRefStart);
|
jpayne@68
|
469 int[] rvec;
|
jpayne@68
|
470 if(bestRefStart<refStartLoc || bestRefStop>refEndLoc){ //Suggest extra padding in cases of overflow
|
jpayne@68
|
471 int padLeft=Tools.max(0, refStartLoc-bestRefStart);
|
jpayne@68
|
472 int padRight=Tools.max(0, bestRefStop-refEndLoc);
|
jpayne@68
|
473 rvec=new int[] {score, bestRefStart, bestRefStop, padLeft, padRight};
|
jpayne@68
|
474 }else{
|
jpayne@68
|
475 rvec=new int[] {score, bestRefStart, bestRefStop};
|
jpayne@68
|
476 }
|
jpayne@68
|
477 return rvec;
|
jpayne@68
|
478 }
|
jpayne@68
|
479
|
jpayne@68
|
480
|
jpayne@68
|
481 /** Will not fill areas that cannot match minScore.
|
jpayne@68
|
482 * @return {score, bestRefStart, bestRefStop} */
|
jpayne@68
|
483 @Override
|
jpayne@68
|
484 public final int[] fillAndScoreLimited(byte[] read, byte[] ref, int refStartLoc, int refEndLoc, int minScore){
|
jpayne@68
|
485 int a=Tools.max(0, refStartLoc);
|
jpayne@68
|
486 int b=Tools.min(ref.length-1, refEndLoc);
|
jpayne@68
|
487 assert(b>=a);
|
jpayne@68
|
488
|
jpayne@68
|
489 if(b-a>=maxColumns){
|
jpayne@68
|
490 System.err.println("Warning: Max alignment columns exceeded; restricting range. "+(b-a+1)+" > "+maxColumns);
|
jpayne@68
|
491 assert(false) : refStartLoc+", "+refEndLoc;
|
jpayne@68
|
492 b=Tools.min(ref.length-1, a+maxColumns-1);
|
jpayne@68
|
493 }
|
jpayne@68
|
494 int[] max=fillLimited(read, ref, a, b, minScore);
|
jpayne@68
|
495 // return max==null ? null : new int[] {max[3], 0, max[1]};
|
jpayne@68
|
496
|
jpayne@68
|
497 int[] score=(max==null ? null : score(read, ref, a, b, max[0], max[1], max[2]/*, max[3], max[4]*/));
|
jpayne@68
|
498
|
jpayne@68
|
499 return score;
|
jpayne@68
|
500 }
|
jpayne@68
|
501
|
jpayne@68
|
502 public static final String toString(byte[] ref, int startLoc, int stopLoc){
|
jpayne@68
|
503 StringBuilder sb=new StringBuilder(stopLoc-startLoc+1);
|
jpayne@68
|
504 for(int i=startLoc; i<=stopLoc; i++){sb.append((char)ref[i]);}
|
jpayne@68
|
505 return sb.toString();
|
jpayne@68
|
506 }
|
jpayne@68
|
507
|
jpayne@68
|
508 // public static int calcDelScore(int len){
|
jpayne@68
|
509 // if(len<=0){return 0;}
|
jpayne@68
|
510 // int score=POINTS_DEL;
|
jpayne@68
|
511 // if(len>1){
|
jpayne@68
|
512 // score+=(len-1)*POINTS_DEL2;
|
jpayne@68
|
513 // }
|
jpayne@68
|
514 // return score;
|
jpayne@68
|
515 // }
|
jpayne@68
|
516
|
jpayne@68
|
517 // public int maxScoreByIdentity(int len, float identity){
|
jpayne@68
|
518 // assert(identity>=0 && identity<=1);
|
jpayne@68
|
519 // return (int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB));
|
jpayne@68
|
520 // }
|
jpayne@68
|
521
|
jpayne@68
|
522 @Override
|
jpayne@68
|
523 public int minScoreByIdentity(int len, float identity){
|
jpayne@68
|
524 assert(identity>=0 && identity<=1);
|
jpayne@68
|
525
|
jpayne@68
|
526 int a=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_SUB));
|
jpayne@68
|
527 int b=(int)(len*(identity*POINTS_MATCH+(1-identity)*POINTS_INS));
|
jpayne@68
|
528 int c=(int)(len*(1*POINTS_MATCH+((1/(Tools.max(identity, 0.000001f)))-1)*POINTS_DEL));
|
jpayne@68
|
529 return Tools.min(a, b, c);
|
jpayne@68
|
530 }
|
jpayne@68
|
531
|
jpayne@68
|
532 private static int calcDelScore(int len){
|
jpayne@68
|
533 if(len<=0){return 0;}
|
jpayne@68
|
534 int score=POINTS_DEL*len;
|
jpayne@68
|
535 return score;
|
jpayne@68
|
536 }
|
jpayne@68
|
537
|
jpayne@68
|
538 @Override
|
jpayne@68
|
539 public int rows(){return rows;}
|
jpayne@68
|
540 @Override
|
jpayne@68
|
541 public int columns(){return columns;}
|
jpayne@68
|
542
|
jpayne@68
|
543
|
jpayne@68
|
544 private int maxRows;
|
jpayne@68
|
545 private int maxColumns;
|
jpayne@68
|
546
|
jpayne@68
|
547 private int[][] packed;
|
jpayne@68
|
548
|
jpayne@68
|
549 public static final int MAX_SCORE=Integer.MAX_VALUE-2000;
|
jpayne@68
|
550 public static final int MIN_SCORE=0-MAX_SCORE; //Keeps it 1 point above "BAD".
|
jpayne@68
|
551
|
jpayne@68
|
552 //For some reason changing MODE_DEL from 1 to 0 breaks everything
|
jpayne@68
|
553 private static final byte MODE_DEL=1;
|
jpayne@68
|
554 private static final byte MODE_INS=2;
|
jpayne@68
|
555 private static final byte MODE_SUB=3;
|
jpayne@68
|
556 private static final byte MODE_MATCH=4;
|
jpayne@68
|
557 private static final byte MODE_N=5;
|
jpayne@68
|
558
|
jpayne@68
|
559 public static final int POINTS_NOREF=-15;
|
jpayne@68
|
560 public static final int POINTS_MATCH=100;
|
jpayne@68
|
561 public static final int POINTS_SUB=-50;
|
jpayne@68
|
562 public static final int POINTS_INS=-121;
|
jpayne@68
|
563 public static final int POINTS_DEL=-111;
|
jpayne@68
|
564
|
jpayne@68
|
565 // public static final int POINTS_NOREF=-100000;
|
jpayne@68
|
566 // public static final int POINTS_MATCH=100;
|
jpayne@68
|
567 // public static final int POINTS_SUB=-100;
|
jpayne@68
|
568 // public static final int POINTS_INS=-100;
|
jpayne@68
|
569 // public static final int POINTS_DEL=-100;
|
jpayne@68
|
570
|
jpayne@68
|
571 public static final int BAD=MIN_SCORE-1;
|
jpayne@68
|
572
|
jpayne@68
|
573 private int rows;
|
jpayne@68
|
574 private int columns;
|
jpayne@68
|
575
|
jpayne@68
|
576 // public long iterationsLimited=0;
|
jpayne@68
|
577 // public long iterationsUnlimited=0;
|
jpayne@68
|
578
|
jpayne@68
|
579 public boolean verbose=false;
|
jpayne@68
|
580 public boolean verbose2=false;
|
jpayne@68
|
581
|
jpayne@68
|
582 }
|