| 1 |
package align2; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import shared.Tools; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
public class BandedAlignerConcrete extends BandedAligner{ |
| 14 |
|
| 15 |
|
| 16 |
public static void main(String[] args){ |
| 17 |
byte[] query=args[0].getBytes(); |
| 18 |
byte[] ref=(args[1].equals(".") ? args[0].getBytes() : args[1].getBytes()); |
| 19 |
int qstart=-1; |
| 20 |
int rstart=-1; |
| 21 |
int maxedits=big-1; |
| 22 |
int width=5; |
| 23 |
if(args.length>2){qstart=Integer.parseInt(args[2]);} |
| 24 |
if(args.length>3){rstart=Integer.parseInt(args[3]);} |
| 25 |
if(args.length>4){maxedits=Integer.parseInt(args[4]);} |
| 26 |
if(args.length>5){width=Integer.parseInt(args[5]);} |
| 27 |
|
| 28 |
BandedAlignerConcrete ba=new BandedAlignerConcrete(width); |
| 29 |
|
| 30 |
int edits; |
| 31 |
|
| 32 |
penalizeOffCenter=true; |
| 33 |
edits=ba.alignForward(query, ref, (qstart==-1 ? 0 : qstart), (rstart==-1 ? 0 : rstart), maxedits, true); |
| 34 |
System.out.println("Forward: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 35 |
System.out.println("***********************\n"); |
| 36 |
|
| 37 |
penalizeOffCenter=false; |
| 38 |
edits=ba.alignForward(query, ref, (qstart==-1 ? 0 : qstart), (rstart==-1 ? 0 : rstart), maxedits, true); |
| 39 |
System.out.println("Forward2: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 40 |
System.out.println("***********************\n"); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
penalizeOffCenter=true; |
| 47 |
edits=ba.alignReverse(query, ref, (qstart==-1 ? query.length-1 : qstart), (rstart==-1 ? ref.length-1 : rstart), maxedits, true); |
| 48 |
System.out.println("Reverse: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 49 |
System.out.println("***********************\n"); |
| 50 |
|
| 51 |
penalizeOffCenter=false; |
| 52 |
edits=ba.alignReverse(query, ref, (qstart==-1 ? query.length-1 : qstart), (rstart==-1 ? ref.length-1 : rstart), maxedits, true); |
| 53 |
System.out.println("Reverse2: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 54 |
System.out.println("***********************\n"); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
penalizeOffCenter=true; |
| 61 |
edits=ba.alignQuadruple(query, ref, maxedits, true); |
| 62 |
System.out.println("Quadruple: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 63 |
System.out.println("***********************\n"); |
| 64 |
|
| 65 |
penalizeOffCenter=false; |
| 66 |
edits=ba.alignQuadruple(query, ref, maxedits, true); |
| 67 |
System.out.println("Quadruple2: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 68 |
System.out.println("***********************\n"); |
| 69 |
|
| 70 |
penalizeOffCenter=true; |
| 71 |
edits=ba.alignDouble(query, ref, maxedits, true); |
| 72 |
System.out.println("Double: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 73 |
System.out.println("***********************\n"); |
| 74 |
|
| 75 |
penalizeOffCenter=false; |
| 76 |
edits=ba.alignDouble(query, ref, maxedits, true); |
| 77 |
System.out.println("Double2: \tedits="+edits+", lastRow="+ba.lastRow+", score="+ba.score()); |
| 78 |
System.out.println("***********************\n"); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public BandedAlignerConcrete(int width_){ |
| 83 |
super(width_); |
| 84 |
array1=new int[maxWidth+2]; |
| 85 |
array2=new int[maxWidth+2]; |
| 86 |
Arrays.fill(array1, big); |
| 87 |
Arrays.fill(array2, big); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
assert(big>maxWidth/2); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
@Override |
| 102 |
public int alignForward(final byte[] query, final byte[] ref, final int qstart, final int rstart, final int maxEdits, final boolean exact){ |
| 103 |
assert(big>maxEdits); |
| 104 |
if(verbose){System.err.println("alignForward("+new String(query)+", "+new String(ref)+", "+qstart+", "+rstart+", "+maxEdits+")");} |
| 105 |
if(query.length-qstart>ref.length-rstart){ |
| 106 |
int x=alignForward(ref, query, rstart, qstart, maxEdits, exact); |
| 107 |
int temp=lastQueryLoc; |
| 108 |
lastQueryLoc=lastRefLoc; |
| 109 |
lastRefLoc=temp; |
| 110 |
if(verbose){ |
| 111 |
System.out.println("Reversed."); |
| 112 |
System.out.println("Final state: lastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 113 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+"\n"); |
| 114 |
} |
| 115 |
return x; |
| 116 |
} |
| 117 |
int edits=0, row=0; |
| 118 |
lastRow=-1; |
| 119 |
lastEdits=0; |
| 120 |
lastOffset=0; |
| 121 |
|
| 122 |
final int width=Tools.min(maxWidth, (maxEdits*2)+1, Tools.max(query.length, ref.length)*2+2)|1; |
| 123 |
final int halfWidth=width/2; |
| 124 |
final boolean inexact=!exact; |
| 125 |
|
| 126 |
int qloc=qstart; |
| 127 |
int rsloc=rstart-halfWidth; |
| 128 |
final int xlines=query.length-qstart; |
| 129 |
final int ylines=ref.length-rstart; |
| 130 |
final int len=Tools.min(xlines, ylines); |
| 131 |
if(verbose){System.err.println("xlines="+xlines+", ylines="+ylines+", len="+len);} |
| 132 |
if(len<1){ |
| 133 |
if(false){ |
| 134 |
throw new RuntimeException("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 135 |
} |
| 136 |
assert(false) : ("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
Arrays.fill(array1, 0, Tools.min(width, maxWidth)+1, big); |
| 141 |
Arrays.fill(array2, 0, Tools.min(width, maxWidth)+1, big); |
| 142 |
arrayCurrent=array1; |
| 143 |
arrayPrev=array2; |
| 144 |
{ |
| 145 |
if(verbose){System.err.println("\nFirst row.");} |
| 146 |
final byte q=query[qloc]; |
| 147 |
final int colStart=Tools.max(0, rsloc); |
| 148 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 149 |
edits=big; |
| 150 |
int mloc=1+(colStart-rsloc); |
| 151 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 152 |
|
| 153 |
for(int col=colStart; col<colLimit; mloc++, col++){ |
| 154 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 155 |
final byte r=ref[col]; |
| 156 |
final int score=(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 157 |
arrayCurrent[mloc]=score; |
| 158 |
edits=Tools.min(edits, score); |
| 159 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; prev=0; score="+score+"; scores = "+Arrays.toString(arrayCurrent));} |
| 160 |
} |
| 161 |
row++; qloc++; rsloc++; |
| 162 |
} |
| 163 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 164 |
|
| 165 |
for(row=1; row<len; row++, qloc++, rsloc++){ |
| 166 |
|
| 167 |
arrayTemp=arrayCurrent; |
| 168 |
arrayCurrent=arrayPrev; |
| 169 |
arrayPrev=arrayTemp; |
| 170 |
if(verbose){System.err.println("\nNew row, prev="+Arrays.toString(arrayPrev)+", current="+Arrays.toString(arrayCurrent));} |
| 171 |
final byte q=query[qloc]; |
| 172 |
final int colStart=Tools.max(0, rsloc); |
| 173 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 174 |
Arrays.fill(arrayCurrent, big); |
| 175 |
edits=big; |
| 176 |
int mloc=1+(colStart-rsloc); |
| 177 |
boolean forceDiag=(row==len-1); |
| 178 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 179 |
for(int col=colStart; col<colLimit; mloc++, col++){ |
| 180 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 181 |
final byte r=ref[col]; |
| 182 |
final int scoreUp=arrayPrev[mloc+1]+1; |
| 183 |
final int scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 184 |
final int scoreLeft=arrayCurrent[mloc-1]+1; |
| 185 |
final int score=(forceDiag || col==ref.length-1) ? scoreDiag : Tools.min(scoreUp, scoreDiag, scoreLeft); |
| 186 |
if(verbose){System.err.println("prev=min(s["+(mloc-1)+"]="+arrayCurrent[mloc-1]+", p["+(mloc)+"]="+arrayPrev[mloc]+", p["+(mloc+1)+"]="+arrayPrev[mloc+1]+")");} |
| 187 |
arrayCurrent[mloc]=score; |
| 188 |
edits=Tools.min(edits, score); |
| 189 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; up="+scoreUp+"; diag="+scoreDiag+"; left="+scoreLeft+"; scores = "+Arrays.toString(arrayCurrent));} |
| 190 |
} |
| 191 |
if(edits>maxEdits){row++; break;} |
| 192 |
} |
| 193 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 194 |
|
| 195 |
lastRow=row-1; |
| 196 |
lastEdits=edits; |
| 197 |
lastQueryLoc=qloc-1; |
| 198 |
lastOffset=lastOffset(arrayCurrent, halfWidth); |
| 199 |
lastRefLoc=rsloc+halfWidth-lastOffset-1; |
| 200 |
while(lastRefLoc>=ref.length || lastQueryLoc>=query.length){lastRefLoc--; lastQueryLoc--;} |
| 201 |
if(verbose){ |
| 202 |
System.out.println("\nFinal state: arrayCurrent="+Arrays.toString(arrayCurrent)+"\nlastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 203 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+"\n"); |
| 204 |
} |
| 205 |
return edits; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
@Override |
| 216 |
public int alignForwardRC(final byte[] query, final byte[] ref, final int qstart, final int rstart, final int maxEdits, final boolean exact){ |
| 217 |
assert(big>maxEdits); |
| 218 |
if(verbose){System.err.println("alignForwardRC("+new String(query)+", "+new String(ref)+", "+qstart+", "+rstart+", "+maxEdits+")");} |
| 219 |
if(qstart+1>ref.length-rstart){ |
| 220 |
int x=alignReverseRC(ref, query, rstart, qstart, maxEdits, exact); |
| 221 |
int temp=lastQueryLoc; |
| 222 |
lastQueryLoc=lastRefLoc; |
| 223 |
lastRefLoc=temp; |
| 224 |
if(verbose){ |
| 225 |
System.out.println("Reversed."); |
| 226 |
System.out.println("Final state: lastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 227 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+"\n"); |
| 228 |
} |
| 229 |
return x; |
| 230 |
} |
| 231 |
int edits=0, row=0; |
| 232 |
lastRow=-1; |
| 233 |
lastEdits=0; |
| 234 |
lastOffset=0; |
| 235 |
|
| 236 |
final int width=Tools.min(maxWidth, (maxEdits*2)+1, Tools.max(query.length, ref.length)*2+2)|1; |
| 237 |
final int halfWidth=width/2; |
| 238 |
final boolean inexact=!exact; |
| 239 |
|
| 240 |
int qloc=qstart; |
| 241 |
int rsloc=rstart-halfWidth; |
| 242 |
final int xlines=qstart+1; |
| 243 |
final int ylines=ref.length-rstart; |
| 244 |
final int len=Tools.min(xlines, ylines); |
| 245 |
if(verbose){System.err.println("xlines="+xlines+", ylines="+ylines+", len="+len);} |
| 246 |
if(len<1){ |
| 247 |
if(false){ |
| 248 |
throw new RuntimeException("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 249 |
} |
| 250 |
assert(false) : ("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
Arrays.fill(array1, 0, Tools.min(width, maxWidth)+1, big); |
| 255 |
Arrays.fill(array2, 0, Tools.min(width, maxWidth)+1, big); |
| 256 |
arrayCurrent=array1; |
| 257 |
arrayPrev=array2; |
| 258 |
|
| 259 |
{ |
| 260 |
if(verbose){System.err.println("\nFirst row.");} |
| 261 |
final byte q=AminoAcid.baseToComplementExtended[query[qloc]]; |
| 262 |
final int colStart=Tools.max(0, rsloc); |
| 263 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 264 |
edits=big; |
| 265 |
int mloc=1+(colStart-rsloc); |
| 266 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 267 |
for(int col=colStart; col<colLimit; mloc++, col++){ |
| 268 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 269 |
final byte r=ref[col]; |
| 270 |
final int score=(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 271 |
arrayCurrent[mloc]=score; |
| 272 |
edits=Tools.min(edits, score); |
| 273 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; prev=0; score="+score+"; scores = "+Arrays.toString(arrayCurrent));} |
| 274 |
} |
| 275 |
row++; qloc--; rsloc++; |
| 276 |
} |
| 277 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 278 |
|
| 279 |
for(row=1; row<len; row++, qloc--, rsloc++){ |
| 280 |
|
| 281 |
arrayTemp=arrayCurrent; |
| 282 |
arrayCurrent=arrayPrev; |
| 283 |
arrayPrev=arrayTemp; |
| 284 |
if(verbose){System.err.println("\nNew row, prev="+Arrays.toString(arrayPrev)+", current="+Arrays.toString(arrayCurrent));} |
| 285 |
final byte q=AminoAcid.baseToComplementExtended[query[qloc]]; |
| 286 |
final int colStart=Tools.max(0, rsloc); |
| 287 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 288 |
Arrays.fill(arrayCurrent, big); |
| 289 |
edits=big; |
| 290 |
int mloc=1+(colStart-rsloc); |
| 291 |
boolean forceDiag=(row==len-1); |
| 292 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 293 |
for(int col=colStart; col<colLimit; mloc++, col++){ |
| 294 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 295 |
final byte r=ref[col]; |
| 296 |
final int scoreUp=arrayPrev[mloc+1]+1; |
| 297 |
final int scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 298 |
final int scoreLeft=arrayCurrent[mloc-1]+1; |
| 299 |
final int score=(forceDiag || col==ref.length-1) ? scoreDiag : Tools.min(scoreUp, scoreDiag, scoreLeft); |
| 300 |
if(verbose){System.err.println("prev=min(s["+(mloc-1)+"]="+arrayCurrent[mloc-1]+", p["+(mloc)+"]="+arrayPrev[mloc]+", p["+(mloc+1)+"]="+arrayPrev[mloc+1]+")");} |
| 301 |
arrayCurrent[mloc]=score; |
| 302 |
edits=Tools.min(edits, score); |
| 303 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; up="+scoreUp+"; diag="+scoreDiag+"; left="+scoreLeft+"; scores = "+Arrays.toString(arrayCurrent));} |
| 304 |
} |
| 305 |
if(edits>maxEdits){row++; break;} |
| 306 |
} |
| 307 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 308 |
|
| 309 |
lastRow=row-1; |
| 310 |
lastEdits=edits; |
| 311 |
lastOffset=lastOffset(arrayCurrent, halfWidth); |
| 312 |
lastQueryLoc=qloc+1; |
| 313 |
lastRefLoc=rsloc+halfWidth-lastOffset-1; |
| 314 |
while(lastRefLoc>=ref.length || lastQueryLoc<0){lastRefLoc--; lastQueryLoc++;} |
| 315 |
if(verbose){ |
| 316 |
System.out.println("\nFinal state: arrayCurrent="+Arrays.toString(arrayCurrent)+"\nlastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 317 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+", qloc="+qloc+"\n"); |
| 318 |
} |
| 319 |
return edits; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
@Override |
| 330 |
public int alignReverse(final byte[] query, final byte[] ref, final int qstart, final int rstart, final int maxEdits, final boolean exact){ |
| 331 |
assert(big>maxEdits); |
| 332 |
if(verbose){System.err.println("alignReverse("+new String(query)+", "+new String(ref)+", "+qstart+", "+rstart+", "+maxEdits+")");} |
| 333 |
if(qstart>rstart){ |
| 334 |
int x=alignReverse(ref, query, rstart, qstart, maxEdits, exact); |
| 335 |
int temp=lastQueryLoc; |
| 336 |
lastQueryLoc=lastRefLoc; |
| 337 |
lastRefLoc=temp; |
| 338 |
if(verbose){ |
| 339 |
System.out.println("Reversed."); |
| 340 |
System.out.println("Final state: lastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 341 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+"\n"); |
| 342 |
} |
| 343 |
return x; |
| 344 |
} |
| 345 |
|
| 346 |
int edits=0, row=0; |
| 347 |
lastRow=-1; |
| 348 |
lastEdits=0; |
| 349 |
lastOffset=0; |
| 350 |
|
| 351 |
final int width=Tools.min(maxWidth, (maxEdits*2)+1, Tools.max(query.length, ref.length)*2+2)|1; |
| 352 |
final int halfWidth=width/2; |
| 353 |
final boolean inexact=!exact; |
| 354 |
|
| 355 |
int qloc=qstart; |
| 356 |
int rsloc=rstart-halfWidth; |
| 357 |
final int xlines=qstart+1; |
| 358 |
final int ylines=rstart+1; |
| 359 |
final int len=Tools.min(xlines, ylines); |
| 360 |
if(verbose){System.err.println("xlines="+xlines+", ylines="+ylines+", len="+len);} |
| 361 |
if(len<1){ |
| 362 |
if(false){ |
| 363 |
throw new RuntimeException("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 364 |
} |
| 365 |
assert(false) : ("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
Arrays.fill(array1, 0, Tools.min(width, maxWidth)+1, big); |
| 370 |
Arrays.fill(array2, 0, Tools.min(width, maxWidth)+1, big); |
| 371 |
arrayCurrent=array1; |
| 372 |
arrayPrev=array2; |
| 373 |
|
| 374 |
{ |
| 375 |
if(verbose){System.err.println("\nFirst row.");} |
| 376 |
final byte q=query[qloc]; |
| 377 |
final int colStart=Tools.max(0, rsloc); |
| 378 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 379 |
edits=big; |
| 380 |
int mloc=1+width-(colLimit-rsloc); |
| 381 |
|
| 382 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 383 |
for(int col=colLimit-1; col>=colStart; mloc++, col--){ |
| 384 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 385 |
final byte r=ref[col]; |
| 386 |
final int score=(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 387 |
arrayCurrent[mloc]=score; |
| 388 |
edits=Tools.min(edits, score); |
| 389 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; prev=0; score="+score+"; scores = "+Arrays.toString(arrayCurrent));} |
| 390 |
} |
| 391 |
row++; qloc--; rsloc--; |
| 392 |
} |
| 393 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 394 |
|
| 395 |
for(row=1; row<len; row++, qloc--, rsloc--){ |
| 396 |
|
| 397 |
arrayTemp=arrayCurrent; |
| 398 |
arrayCurrent=arrayPrev; |
| 399 |
arrayPrev=arrayTemp; |
| 400 |
if(verbose){System.err.println("\nNew row, prev="+Arrays.toString(arrayPrev)+", current="+Arrays.toString(arrayCurrent));} |
| 401 |
final byte q=query[qloc]; |
| 402 |
final int colStart=Tools.max(0, rsloc); |
| 403 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 404 |
Arrays.fill(arrayCurrent, big); |
| 405 |
edits=big; |
| 406 |
int mloc=1+width-(colLimit-rsloc); |
| 407 |
boolean forceDiag=(row==len-1); |
| 408 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 409 |
for(int col=colLimit-1; col>=colStart; mloc++, col--){ |
| 410 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 411 |
final byte r=ref[col]; |
| 412 |
final int scoreUp=arrayPrev[mloc+1]+1; |
| 413 |
final int scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 414 |
final int scoreLeft=arrayCurrent[mloc-1]+1; |
| 415 |
final int score=(forceDiag || col==0) ? scoreDiag : Tools.min(scoreUp, scoreDiag, scoreLeft); |
| 416 |
if(verbose){System.err.println("prev=min(s["+(mloc-1)+"]="+arrayCurrent[mloc-1]+", p["+(mloc)+"]="+arrayPrev[mloc]+", p["+(mloc+1)+"]="+arrayPrev[mloc+1]+")");} |
| 417 |
arrayCurrent[mloc]=score; |
| 418 |
edits=Tools.min(edits, score); |
| 419 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; up="+scoreUp+"; diag="+scoreDiag+"; left="+scoreLeft+"; scores = "+Arrays.toString(arrayCurrent));} |
| 420 |
} |
| 421 |
if(edits>maxEdits){row++; break;} |
| 422 |
} |
| 423 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 424 |
|
| 425 |
lastRow=row-1; |
| 426 |
lastEdits=edits; |
| 427 |
lastOffset=lastOffset(arrayCurrent, halfWidth); |
| 428 |
lastQueryLoc=qloc+1; |
| 429 |
lastRefLoc=rsloc+halfWidth+lastOffset+1; |
| 430 |
while(lastRefLoc<0 || lastQueryLoc<0){lastRefLoc++; lastQueryLoc++;} |
| 431 |
if(verbose){ |
| 432 |
System.out.println("\nFinal state: arrayCurrent="+Arrays.toString(arrayCurrent)+"\nlastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 433 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+", qloc="+qloc+", rsloc="+rsloc+"\n"); |
| 434 |
} |
| 435 |
return edits; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
@Override |
| 446 |
public int alignReverseRC(final byte[] query, final byte[] ref, final int qstart, final int rstart, final int maxEdits, final boolean exact){ |
| 447 |
assert(big>maxEdits); |
| 448 |
if(verbose){System.err.println("alignReverseRC("+new String(query)+", "+new String(ref)+", "+qstart+", "+rstart+", "+maxEdits+")");} |
| 449 |
if(query.length-qstart>rstart+1){ |
| 450 |
int x=alignForwardRC(ref, query, rstart, qstart, maxEdits, exact); |
| 451 |
int temp=lastQueryLoc; |
| 452 |
lastQueryLoc=lastRefLoc; |
| 453 |
lastRefLoc=temp; |
| 454 |
if(verbose){ |
| 455 |
System.out.println("Reversed."); |
| 456 |
System.out.println("Final state: lastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 457 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+"\n"); |
| 458 |
} |
| 459 |
return x; |
| 460 |
} |
| 461 |
int edits=0, row=0; |
| 462 |
lastRow=-1; |
| 463 |
lastEdits=0; |
| 464 |
lastOffset=0; |
| 465 |
|
| 466 |
final int width=Tools.min(maxWidth, (maxEdits*2)+1, Tools.max(query.length, ref.length)*2+2)|1; |
| 467 |
final int halfWidth=width/2; |
| 468 |
final boolean inexact=!exact; |
| 469 |
|
| 470 |
int qloc=qstart; |
| 471 |
int rsloc=rstart-halfWidth; |
| 472 |
final int xlines=query.length-qstart; |
| 473 |
final int ylines=rstart+1; |
| 474 |
final int len=Tools.min(xlines, ylines); |
| 475 |
if(verbose){System.err.println("xlines="+xlines+", ylines="+ylines+", len="+len);} |
| 476 |
if(len<1){ |
| 477 |
if(false){ |
| 478 |
throw new RuntimeException("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 479 |
} |
| 480 |
assert(false) : ("No overlap: qstart="+qstart+", rstart="+rstart+", qlen="+query.length+", rlen="+ref.length); |
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
Arrays.fill(array1, 0, Tools.min(width, maxWidth)+1, big); |
| 485 |
Arrays.fill(array2, 0, Tools.min(width, maxWidth)+1, big); |
| 486 |
arrayCurrent=array1; |
| 487 |
arrayPrev=array2; |
| 488 |
|
| 489 |
{ |
| 490 |
if(verbose){System.err.println("\nFirst row.");} |
| 491 |
final byte q=AminoAcid.baseToComplementExtended[query[qloc]]; |
| 492 |
final int colStart=Tools.max(0, rsloc); |
| 493 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 494 |
edits=big; |
| 495 |
int mloc=1+width-(colLimit-rsloc); |
| 496 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 497 |
for(int col=colLimit-1; col>=colStart; mloc++, col--){ |
| 498 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 499 |
final byte r=ref[col]; |
| 500 |
final int score=(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 501 |
arrayCurrent[mloc]=score; |
| 502 |
edits=Tools.min(edits, score); |
| 503 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; prev=0; score="+score+"; scores = "+Arrays.toString(arrayCurrent));} |
| 504 |
} |
| 505 |
row++; qloc++; rsloc--; |
| 506 |
} |
| 507 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 508 |
|
| 509 |
for(row=1; row<len; row++, qloc++, rsloc--){ |
| 510 |
|
| 511 |
arrayTemp=arrayCurrent; |
| 512 |
arrayCurrent=arrayPrev; |
| 513 |
arrayPrev=arrayTemp; |
| 514 |
if(verbose){System.err.println("\nNew row, prev="+Arrays.toString(arrayPrev)+", current="+Arrays.toString(arrayCurrent));} |
| 515 |
final byte q=AminoAcid.baseToComplementExtended[query[qloc]]; |
| 516 |
final int colStart=Tools.max(0, rsloc); |
| 517 |
final int colLimit=Tools.min(rsloc+width, ref.length); |
| 518 |
Arrays.fill(arrayCurrent, big); |
| 519 |
edits=big; |
| 520 |
int mloc=1+width-(colLimit-rsloc); |
| 521 |
boolean forceDiag=(row==len-1); |
| 522 |
if(verbose){System.err.println("q="+(char)q+", qloc="+qloc+", rsloc="+rsloc+", colStart="+colStart+", colLimit="+colLimit+", mloc="+mloc);} |
| 523 |
for(int col=colLimit-1; col>=colStart; mloc++, col--){ |
| 524 |
if(verbose){System.err.println("col="+col+", mloc="+mloc);} |
| 525 |
final byte r=ref[col]; |
| 526 |
final int scoreUp=arrayPrev[mloc+1]+1; |
| 527 |
final int scoreDiag=arrayPrev[mloc]+(q==r || (inexact && (!AminoAcid.isFullyDefined(q) || !AminoAcid.isFullyDefined(r))) ? 0 : 1); |
| 528 |
final int scoreLeft=arrayCurrent[mloc-1]+1; |
| 529 |
final int score=(forceDiag || col==0) ? scoreDiag : Tools.min(scoreUp, scoreDiag, scoreLeft); |
| 530 |
if(verbose){System.err.println("prev=min(s["+(mloc-1)+"]="+arrayCurrent[mloc-1]+", p["+(mloc)+"]="+arrayPrev[mloc]+", p["+(mloc+1)+"]="+arrayPrev[mloc+1]+")");} |
| 531 |
arrayCurrent[mloc]=score; |
| 532 |
edits=Tools.min(edits, score); |
| 533 |
if(verbose){System.err.println("Comparing "+(char)q+" to "+(char)r+"; up="+scoreUp+"; diag="+scoreDiag+"; left="+scoreLeft+"; scores = "+Arrays.toString(arrayCurrent));} |
| 534 |
} |
| 535 |
if(edits>maxEdits){row++; break;} |
| 536 |
} |
| 537 |
if(penalizeOffCenter){edits=penalizeOffCenter(arrayCurrent, halfWidth);} |
| 538 |
|
| 539 |
lastRow=row-1; |
| 540 |
lastEdits=edits; |
| 541 |
lastOffset=lastOffset(arrayCurrent, halfWidth); |
| 542 |
lastQueryLoc=qloc-1; |
| 543 |
lastRefLoc=rsloc+halfWidth+lastOffset+1; |
| 544 |
while(lastRefLoc<0 || lastQueryLoc>=query.length){lastRefLoc++; lastQueryLoc--;} |
| 545 |
if(verbose){ |
| 546 |
System.out.println("\nFinal state: arrayCurrent="+Arrays.toString(arrayCurrent)+"\nlastRow="+lastRow+", lastEdits="+lastEdits+", lastOffset="+lastOffset+ |
| 547 |
", lastQueryLoc="+lastQueryLoc+", lastRefLoc="+lastRefLoc+(query.length<30 ? ", query="+new String(query)+", ref="+new String(ref) : "")+"\n"); |
| 548 |
} |
| 549 |
return edits; |
| 550 |
} |
| 551 |
|
| 552 |
private final int[] array1; |
| 553 |
private final int[] array2; |
| 554 |
private int[] arrayCurrent, arrayPrev, arrayTemp; |
| 555 |
|
| 556 |
} |