| 1 |
package assemble; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 5 |
|
| 6 |
import dna.AminoAcid; |
| 7 |
import kmer.AbstractKmerTableSet; |
| 8 |
import shared.Tools; |
| 9 |
import structures.ByteBuilder; |
| 10 |
import ukmer.AbstractKmerTableU; |
| 11 |
import ukmer.HashArrayU1D; |
| 12 |
import ukmer.HashForestU; |
| 13 |
import ukmer.Kmer; |
| 14 |
import ukmer.KmerNodeU; |
| 15 |
import ukmer.KmerTableSetU; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class Shaver2 extends Shaver { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public Shaver2(KmerTableSetU tables_, int threads_){ |
| 32 |
this(tables_, threads_, 1, 1, 1, 1, 3, 100, 100, true, true); |
| 33 |
} |
| 34 |
|
| 35 |
public Shaver2(KmerTableSetU tables_, int threads_, |
| 36 |
int minCount_, int maxCount_, int minSeed_, int minCountExtend_, float branchMult2_, int maxLengthToDiscard_, int maxDistanceToExplore_, |
| 37 |
boolean removeHair_, boolean removeBubbles_){ |
| 38 |
super(tables_, threads_, minCount_, maxCount_, minSeed_, minCountExtend_, branchMult2_, maxLengthToDiscard_, maxDistanceToExplore_, removeHair_, removeBubbles_); |
| 39 |
tables=tables_; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@Override |
| 48 |
final AbstractExploreThread makeExploreThread(int id_){return new ExploreThread(id_);} |
| 49 |
@Override |
| 50 |
final AbstractShaveThread makeShaveThread(int id_){return new ShaveThread(id_);} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public boolean exploreAndMark(Kmer kmer, ByteBuilder bb, int[] leftCounts, int[] rightCounts, int minCount, int maxCount, |
| 79 |
int maxLengthToDiscard, int maxDistanceToExplore, boolean prune, |
| 80 |
long[][] countMatrixT, long[][] removeMatrixT){ |
| 81 |
bb.clear(); |
| 82 |
assert(kmer.len>=kmer.kbig); |
| 83 |
if(findOwner(kmer)>STATUS_UNEXPLORED){return false;} |
| 84 |
|
| 85 |
assert(countWithinLimits(kmer)) : "count="+getCount(kmer)+", minCount="+minCount+", maxCount="+maxCount+"\n"+kmer.toString(); |
| 86 |
|
| 87 |
bb.appendKmer(kmer); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
final int rightCode=explore(kmer, bb, leftCounts, rightCounts, minCount, maxCount, maxDistanceToExplore); |
| 92 |
|
| 93 |
|
| 94 |
bb.reverseComplementInPlace(); |
| 95 |
|
| 96 |
kmer=tables.rightmostKmer(bb, kmer); |
| 97 |
assert(getCount(kmer)>0) : "count="+getCount(kmer)+", minCount="+minCount+", maxCount="+maxCount+", rightCode="+rightCode+"\n"+kmer.toString(); |
| 98 |
|
| 99 |
final int leftCode=explore(kmer, bb, leftCounts, rightCounts, minCount, maxCount, maxDistanceToExplore); |
| 100 |
|
| 101 |
|
| 102 |
kmer=tables.rightmostKmer(bb, kmer); |
| 103 |
assert(getCount(kmer)>0) : "count="+getCount(kmer)+", minCount="+minCount+", maxCount="+maxCount+", leftCode="+leftCode+", rightCode="+rightCode+"\n"+kmer.toString(); |
| 104 |
|
| 105 |
final int min=Tools.min(rightCode, leftCode); |
| 106 |
final int max=Tools.max(rightCode, leftCode); |
| 107 |
|
| 108 |
countMatrixT[min][max]++; |
| 109 |
|
| 110 |
if(rightCode==TOO_LONG || rightCode==TOO_DEEP || rightCode==LOOP || rightCode==F_BRANCH){ |
| 111 |
claim(bb, STATUS_EXPLORED, false, kmer); |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
if(leftCode==TOO_LONG || leftCode==TOO_DEEP || leftCode==LOOP || leftCode==F_BRANCH){ |
| 116 |
claim(bb, STATUS_EXPLORED, false, kmer); |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
if(bb.length()-kbig>maxLengthToDiscard){ |
| 121 |
claim(bb, STATUS_EXPLORED, false, kmer); |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
if(removeHair && min==DEAD_END){ |
| 126 |
if(max==DEAD_END || max==B_BRANCH){ |
| 127 |
removeMatrixT[min][max]++; |
| 128 |
boolean success=claim(bb, STATUS_REMOVE, false, kmer); |
| 129 |
if(verbose || verbose2){System.err.println("Claiming ("+rightCode+","+leftCode+") length "+bb.length()+": "+bb);} |
| 130 |
assert(success); |
| 131 |
return true; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if(removeBubbles){ |
| 136 |
if(rightCode==B_BRANCH && leftCode==B_BRANCH){ |
| 137 |
removeMatrixT[min][max]++; |
| 138 |
boolean success=claim(bb, STATUS_REMOVE, false, kmer); |
| 139 |
if(verbose || verbose2){System.err.println("Claiming ("+rightCode+","+leftCode+") length "+bb.length()+": "+bb);} |
| 140 |
assert(success); |
| 141 |
return true; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
claim(bb, STATUS_EXPLORED, false, kmer); |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public int explore(Kmer kmer, ByteBuilder bb, int[] leftCounts, int[] rightCounts, int minCount, int maxCount, int maxLength0){ |
| 160 |
if(verbose){outstream.println("Entering explore with bb.length()="+bb.length());} |
| 161 |
if(bb.length()==0){bb.appendKmer(kmer);} |
| 162 |
assert(bb.length()>=kmer.kbig && kmer.len>=kmer.kbig) : bb.length()+", "+kmer.len+", "+kmer.kbig; |
| 163 |
|
| 164 |
|
| 165 |
final int initialLength=bb.length(); |
| 166 |
final int maxLength=maxLength0+kbig; |
| 167 |
|
| 168 |
final long firstKey=kmer.xor(); |
| 169 |
HashArrayU1D table=tables.getTable(kmer); |
| 170 |
int count=table.getValue(kmer); |
| 171 |
|
| 172 |
|
| 173 |
assert(count>=minCount && count<=maxCount) : count+", "+Kmer.MASK_CORE+", "+kmer.verify(false)+", "+kmer.verify(true); |
| 174 |
|
| 175 |
int nextRightMaxPos=fillRightCounts(kmer, rightCounts); |
| 176 |
int nextRightMax=rightCounts[nextRightMaxPos]; |
| 177 |
if(nextRightMax<minCount){ |
| 178 |
if(verbose){outstream.println("Returning DEAD_END: rightMax="+nextRightMax);} |
| 179 |
return DEAD_END; |
| 180 |
} |
| 181 |
|
| 182 |
while(bb.length()<=maxLength){ |
| 183 |
|
| 184 |
final int rightMaxPos=nextRightMaxPos; |
| 185 |
final int rightMax=rightCounts[rightMaxPos]; |
| 186 |
final int rightSecondPos=Tools.secondHighestPosition(rightCounts); |
| 187 |
final int rightSecond=rightCounts[rightSecondPos]; |
| 188 |
|
| 189 |
if(verbose){ |
| 190 |
outstream.println("kmer: "+toText(kmer)); |
| 191 |
outstream.println("Right counts: "+count+", "+Arrays.toString(rightCounts)); |
| 192 |
outstream.println("rightMaxPos="+rightMaxPos); |
| 193 |
outstream.println("rightMax="+rightMax); |
| 194 |
outstream.println("rightSecondPos="+rightSecondPos); |
| 195 |
outstream.println("rightSecond="+rightSecond); |
| 196 |
} |
| 197 |
assert(count>0); |
| 198 |
|
| 199 |
|
| 200 |
final int prevCount=count; |
| 201 |
|
| 202 |
|
| 203 |
final byte b=AminoAcid.numberToBase[rightMaxPos]; |
| 204 |
final long x=rightMaxPos; |
| 205 |
long evicted=kmer.addRightNumeric(x); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if(kmer.xor()==firstKey){ |
| 211 |
if(verbose){outstream.println("Returning LOOP");} |
| 212 |
return LOOP; |
| 213 |
} |
| 214 |
table=tables.getTable(kmer); |
| 215 |
|
| 216 |
assert(table.getValue(kmer)==rightMax || rightMax==0); |
| 217 |
count=rightMax; |
| 218 |
assert(count>0); |
| 219 |
|
| 220 |
|
| 221 |
{ |
| 222 |
nextRightMaxPos=fillRightCounts(kmer, rightCounts); |
| 223 |
nextRightMax=rightCounts[nextRightMaxPos]; |
| 224 |
if(nextRightMax<minCount){ |
| 225 |
if(verbose){outstream.println("Returning DEAD_END: rightMax="+rightMax);} |
| 226 |
return DEAD_END; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
{ |
| 232 |
final int leftMaxPos=fillLeftCounts(kmer, leftCounts); |
| 233 |
final int leftMax=leftCounts[leftMaxPos]; |
| 234 |
final int leftSecondPos=Tools.secondHighestPosition(leftCounts); |
| 235 |
final int leftSecond=leftCounts[leftSecondPos]; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if(verbose){ |
| 240 |
outstream.println("Left counts: "+count+", "+Arrays.toString(leftCounts)); |
| 241 |
outstream.println("leftMaxPos="+leftMaxPos); |
| 242 |
outstream.println("leftMax="+leftMax); |
| 243 |
outstream.println("leftSecondPos="+leftSecondPos); |
| 244 |
outstream.println("leftSecond="+leftSecond); |
| 245 |
} |
| 246 |
|
| 247 |
if(leftSecond>=minCount || leftMax>prevCount){ |
| 248 |
|
| 249 |
if(leftMax>prevCount){ |
| 250 |
if(verbose){outstream.println("Returning B_BRANCH_LOWER: " + |
| 251 |
"count="+count+", prevCount="+prevCount+", leftMax="+leftMax+", leftSecond="+leftSecond);} |
| 252 |
return B_BRANCH; |
| 253 |
}else{ |
| 254 |
assert(leftMax==prevCount); |
| 255 |
if(leftMax>=2*leftSecond){ |
| 256 |
|
| 257 |
|
| 258 |
}else{ |
| 259 |
if(verbose){outstream.println("Returning B_BRANCH_SIMILAR: " + |
| 260 |
"count="+count+", prevCount="+prevCount+", leftMax="+leftMax+", leftSecond="+leftSecond);} |
| 261 |
return B_BRANCH; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
if(rightSecond>=minCount){ |
| 270 |
if(verbose){outstream.println("Returning F_BRANCH: rightSecond="+rightSecond);} |
| 271 |
return F_BRANCH; |
| 272 |
} |
| 273 |
|
| 274 |
if(count>maxCount){ |
| 275 |
if(verbose){outstream.println("Returning TOO_DEEP: rightMax="+rightMax);} |
| 276 |
return TOO_DEEP; |
| 277 |
} |
| 278 |
|
| 279 |
bb.append(b); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if(verbose){outstream.println("Added base "+(char)b);} |
| 290 |
} |
| 291 |
|
| 292 |
assert(bb.length()>maxLength); |
| 293 |
if(verbose){outstream.println("Returning TOO_LONG: length="+bb.length());} |
| 294 |
return TOO_LONG; |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
class ExploreThread extends AbstractExploreThread{ |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public ExploreThread(int id_){ |
| 315 |
super(id_, kbig); |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
boolean processNextTable(final Kmer kmer, Kmer temp){ |
| 320 |
final int tnum=nextTable.getAndAdd(1); |
| 321 |
if(tnum>=tables.ways){return false;} |
| 322 |
final HashArrayU1D table=tables.getTable(tnum); |
| 323 |
final int[] counts=table.values(); |
| 324 |
final int max=counts.length; |
| 325 |
|
| 326 |
if(startFromHighCounts){ |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
for(int cell=0; cell<max; cell++){ |
| 332 |
final int count=counts[cell]; |
| 333 |
if(count>maxCount){ |
| 334 |
int x=processCell_high(table, cell, kmer, temp, count); |
| 335 |
deadEndsFoundT+=x; |
| 336 |
} |
| 337 |
} |
| 338 |
}else{ |
| 339 |
for(int cell=0; cell<max; cell++){ |
| 340 |
int x=processCell_low(table, cell, kmer, temp); |
| 341 |
deadEndsFoundT+=x; |
| 342 |
} |
| 343 |
} |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
@Override |
| 348 |
boolean processNextVictims(final Kmer kmer, Kmer temp){ |
| 349 |
final int tnum=nextVictims.getAndAdd(1); |
| 350 |
if(tnum>=tables.ways){return false;} |
| 351 |
final HashArrayU1D table=tables.getTable(tnum); |
| 352 |
final HashForestU forest=table.victims(); |
| 353 |
final int max=forest.arrayLength(); |
| 354 |
if(startFromHighCounts){ |
| 355 |
for(int cell=0; cell<max; cell++){ |
| 356 |
KmerNodeU kn=forest.getNode(cell); |
| 357 |
int x=traverseKmerNodeU_high(kn, kmer, temp); |
| 358 |
deadEndsFoundT+=x; |
| 359 |
} |
| 360 |
}else{ |
| 361 |
for(int cell=0; cell<max; cell++){ |
| 362 |
KmerNodeU kn=forest.getNode(cell); |
| 363 |
int x=traverseKmerNodeU_low(kn, kmer, temp); |
| 364 |
deadEndsFoundT+=x; |
| 365 |
} |
| 366 |
} |
| 367 |
return true; |
| 368 |
} |
| 369 |
|
| 370 |
private int traverseKmerNodeU_high(KmerNodeU kn, Kmer kmer, Kmer temp){ |
| 371 |
int sum=0; |
| 372 |
if(kn!=null){ |
| 373 |
sum+=processKmerNodeU_high(kn, kmer, temp); |
| 374 |
if(kn.left()!=null){ |
| 375 |
sum+=traverseKmerNodeU_high(kn.left(), kmer, temp); |
| 376 |
} |
| 377 |
if(kn.right()!=null){ |
| 378 |
sum+=traverseKmerNodeU_high(kn.right(), kmer, temp); |
| 379 |
} |
| 380 |
} |
| 381 |
return sum; |
| 382 |
} |
| 383 |
|
| 384 |
private int traverseKmerNodeU_low(KmerNodeU kn, Kmer kmer, Kmer temp){ |
| 385 |
int sum=0; |
| 386 |
if(kn!=null){ |
| 387 |
sum+=processKmerNodeU_low(kn, kmer, temp); |
| 388 |
if(kn.left()!=null){ |
| 389 |
sum+=traverseKmerNodeU_low(kn.left(), kmer, temp); |
| 390 |
} |
| 391 |
if(kn.right()!=null){ |
| 392 |
sum+=traverseKmerNodeU_low(kn.right(), kmer, temp); |
| 393 |
} |
| 394 |
} |
| 395 |
return sum; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
private int processCell_low(HashArrayU1D table, int cell, Kmer kmer0, Kmer temp){ |
| 402 |
int count=table.readCellValue(cell); |
| 403 |
if(count<minSeed || count>maxCount){return 0;} |
| 404 |
int owner=table.getCellOwner(cell); |
| 405 |
if(owner>STATUS_UNEXPLORED){return 0;} |
| 406 |
Kmer kmer=table.fillKmer(cell, kmer0); |
| 407 |
if(kmer==null){return 0;} |
| 408 |
if(verbose){outstream.println("id="+id+" processing cell "+cell+"; \tkmer="+kmer);} |
| 409 |
|
| 410 |
return processKmer_low(kmer, temp); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
private int processKmerNodeU_low(KmerNodeU kn, Kmer kmer, Kmer temp){ |
| 415 |
kmer.setFrom(kn.pivot()); |
| 416 |
final int count=kn.getValue(kmer); |
| 417 |
if(count<minSeed || count>maxCount){return 0;} |
| 418 |
int owner=kn.getOwner(kmer); |
| 419 |
if(owner>STATUS_UNEXPLORED){return 0;} |
| 420 |
|
| 421 |
return processKmer_low(kmer, temp); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
private int processKmer_low(Kmer original, Kmer temp){ |
| 426 |
kmersTestedT++; |
| 427 |
boolean b=exploreAndMark(original, builderT, leftCounts, rightCounts, minCount, maxCount, maxLengthToDiscard, maxDistanceToExplore, true |
| 428 |
, countMatrixT, removeMatrixT |
| 429 |
); |
| 430 |
return b ? 1 : 0; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
private int processCell_high(HashArrayU1D table, int cell, Kmer kmer0, Kmer temp, int count){ |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
if(count<=maxCount){return 0;} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
Kmer kmer=table.fillKmer(cell, kmer0); |
| 448 |
if(kmer==null){return 0;} |
| 449 |
if(verbose){outstream.println("id="+id+" processing cell "+cell+"; \tkmer="+kmer);} |
| 450 |
|
| 451 |
|
| 452 |
return processKmer_high(kmer, temp, count); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
private int processKmerNodeU_high(KmerNodeU kn, Kmer kmer, Kmer temp){ |
| 457 |
kmer.setFrom(kn.pivot()); |
| 458 |
final int count=kn.getValue(kmer); |
| 459 |
if(count<=maxCount){return 0;} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
return processKmer_high(kmer, temp, count); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
private int processKmer_high(final Kmer original, Kmer kmer, final int count0){ |
| 469 |
int sum=0; |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
assert(original.len()==kbig); |
| 487 |
|
| 488 |
|
| 489 |
sum+=processKmer_high_leftLoop(original, kmer, count0); |
| 490 |
|
| 491 |
original.rcomp(); |
| 492 |
assert(original.len()==kbig); |
| 493 |
sum+=processKmer_high_leftLoop(original, kmer, count0); |
| 494 |
return sum; |
| 495 |
} |
| 496 |
|
| 497 |
private int processKmer_high_leftLoop(final Kmer original, Kmer kmer, final int count0){ |
| 498 |
if(shaveVFast){ |
| 499 |
int inf=Integer.MAX_VALUE; |
| 500 |
int maxHighBranch=-1, minHighBranch=inf, highBranches=0; |
| 501 |
int maxLowBranch=-1, minLowBranch=inf, lowBranches=0; |
| 502 |
for(long i=0; i<4; i++){ |
| 503 |
kmer.setFrom(original); |
| 504 |
long old=kmer.addLeftNumeric(i); |
| 505 |
assert(kmer.len()>=kbig) : kmer.len()+", "+kbig; |
| 506 |
HashArrayU1D table=(HashArrayU1D) tables.getTable(kmer); |
| 507 |
int count=table.getValue(kmer); |
| 508 |
if(count>0){ |
| 509 |
if(count<=maxCount){ |
| 510 |
minLowBranch=Tools.min(count, minLowBranch); |
| 511 |
maxLowBranch=Tools.max(count, maxLowBranch); |
| 512 |
lowBranches++; |
| 513 |
}else{ |
| 514 |
minHighBranch=Tools.min(count, minHighBranch); |
| 515 |
maxHighBranch=Tools.max(count, maxHighBranch); |
| 516 |
highBranches++; |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if(maxLowBranch<0){return 0;} |
| 522 |
|
| 523 |
|
| 524 |
if(highBranches+lowBranches<2){return 0;} |
| 525 |
|
| 526 |
} |
| 527 |
|
| 528 |
int sum=0; |
| 529 |
for(long i=0; i<4; i++){ |
| 530 |
kmer.setFrom(original); |
| 531 |
long old=kmer.addLeftNumeric(i); |
| 532 |
assert(kmer.len()>=kbig) : kmer.len()+", "+kbig; |
| 533 |
HashArrayU1D table=(HashArrayU1D) tables.getTable(kmer); |
| 534 |
int count=table.getValue(kmer); |
| 535 |
if(count>0 && count<=maxCount && table.getOwner(kmer)<=STATUS_UNEXPLORED){ |
| 536 |
kmersTestedT++; |
| 537 |
boolean b=exploreAndMark(kmer, builderT, leftCounts, rightCounts, |
| 538 |
minCount, maxCount, maxLengthToDiscard, maxDistanceToExplore, true, |
| 539 |
countMatrixT, removeMatrixT); |
| 540 |
if(b){sum++;} |
| 541 |
} |
| 542 |
} |
| 543 |
return sum; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
private class ShaveThread extends AbstractShaveThread{ |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
public ShaveThread(int id_){ |
| 560 |
super(id_); |
| 561 |
} |
| 562 |
|
| 563 |
@Override |
| 564 |
boolean processNextTable(){ |
| 565 |
final int tnum=nextTable.getAndAdd(1); |
| 566 |
if(tnum>=tables.ways){return false;} |
| 567 |
|
| 568 |
final HashArrayU1D table=tables.getTable(tnum); |
| 569 |
final AtomicIntegerArray owners=table.owners(); |
| 570 |
final int[] values=table.values(); |
| 571 |
final int max=table.arrayLength(); |
| 572 |
for(int cell=0; cell<max; cell++){ |
| 573 |
if(owners.get(cell)==STATUS_REMOVE){ |
| 574 |
|
| 575 |
values[cell]=0; |
| 576 |
} |
| 577 |
} |
| 578 |
for(KmerNodeU kn : table.victims().array()){ |
| 579 |
if(kn!=null){traverseKmerNodeU(kn);} |
| 580 |
} |
| 581 |
|
| 582 |
table.clearOwnership(); |
| 583 |
kmersRemovedT+=table.regenerate(0); |
| 584 |
|
| 585 |
return true; |
| 586 |
} |
| 587 |
|
| 588 |
private void traverseKmerNodeU(KmerNodeU kn){ |
| 589 |
if(kn==null){return;} |
| 590 |
if(kn.owner()==STATUS_REMOVE){kn.set(0);} |
| 591 |
traverseKmerNodeU(kn.left()); |
| 592 |
traverseKmerNodeU(kn.right()); |
| 593 |
} |
| 594 |
|
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public boolean claim(final ByteBuilder bb, final int id, final boolean exitEarly, Kmer kmer){ |
| 603 |
return claim(bb.array, bb.length(), id, exitEarly, kmer); |
| 604 |
} |
| 605 |
|
| 606 |
public boolean claim(final byte[] bases, final int blen, final int id, boolean exitEarly, final Kmer kmer){ |
| 607 |
if(blen<kbig){return false;} |
| 608 |
if(verbose){outstream.println("Thread "+id+" claim start.");} |
| 609 |
int len=0; |
| 610 |
kmer.clear(); |
| 611 |
boolean success=true; |
| 612 |
|
| 613 |
for(int i=0; i<blen && success; i++){ |
| 614 |
final byte b=bases[i]; |
| 615 |
final long x=AminoAcid.baseToNumber[b]; |
| 616 |
kmer.addRight(b); |
| 617 |
|
| 618 |
if(x<0){len=0;} |
| 619 |
else{len++;} |
| 620 |
assert(len==kmer.len); |
| 621 |
|
| 622 |
if(len>=kbig){ |
| 623 |
assert(countWithinLimits(kmer)) : "count="+getCount(kmer)+", minCount="+minCount+", maxCount="+maxCount+"\n" |
| 624 |
+ "len="+len+", i="+i+", blen="+blen+"\n" |
| 625 |
+ ""+kmer.toString(); |
| 626 |
success=claim(kmer, id); |
| 627 |
success=(success || !exitEarly); |
| 628 |
} |
| 629 |
} |
| 630 |
return success; |
| 631 |
} |
| 632 |
|
| 633 |
final boolean countWithinLimits(Kmer kmer){ |
| 634 |
int count=getCount(kmer); |
| 635 |
return count>=minCount && count<=maxCount; |
| 636 |
} |
| 637 |
|
| 638 |
int getCount(Kmer kmer){return tables.getCount(kmer);} |
| 639 |
boolean claim(Kmer kmer, int id){return tables.claim(kmer, id);} |
| 640 |
boolean doubleClaim(ByteBuilder bb, int id, Kmer kmer){return tables.doubleClaim(bb, id, kmer);} |
| 641 |
|
| 642 |
|
| 643 |
int findOwner(Kmer kmer){return tables.findOwner(kmer);} |
| 644 |
int findOwner(ByteBuilder bb, int id, Kmer kmer){return tables.findOwner(bb, id, kmer);} |
| 645 |
int findOwner(byte[] array, int len, int id, Kmer kmer){return tables.findOwner(array, len, id, kmer);} |
| 646 |
void release(ByteBuilder bb, int id, Kmer kmer){tables.release(bb, id, kmer);} |
| 647 |
void release(byte[] array, int len, int id, Kmer kmer){tables.release(array, len, id, kmer);} |
| 648 |
int fillRightCounts(Kmer kmer, int[] counts){return tables.fillRightCounts(kmer, counts);} |
| 649 |
int fillLeftCounts(Kmer kmer, int[] counts){return tables.fillLeftCounts(kmer, counts);} |
| 650 |
static StringBuilder toText(Kmer kmer){return AbstractKmerTableU.toText(kmer);} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
@Override |
| 661 |
AbstractKmerTableSet tables(){return tables;} |
| 662 |
|
| 663 |
final KmerTableSetU tables; |
| 664 |
|
| 665 |
} |