| 1 |
package ukmer; |
| 2 |
|
| 3 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 4 |
import java.util.concurrent.atomic.AtomicLong; |
| 5 |
import java.util.concurrent.locks.Lock; |
| 6 |
|
| 7 |
import dna.AminoAcid; |
| 8 |
import fileIO.ByteStreamWriter; |
| 9 |
import fileIO.TextStreamWriter; |
| 10 |
import shared.KillSwitch; |
| 11 |
import shared.Shared; |
| 12 |
import shared.Tools; |
| 13 |
import structures.ByteBuilder; |
| 14 |
import structures.SuperLongList; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public abstract class AbstractKmerTableU { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public abstract int increment(Kmer kmer); |
| 29 |
|
| 30 |
|
| 31 |
public abstract int incrementAndReturnNumCreated(final Kmer kmer); |
| 32 |
|
| 33 |
public abstract int set(Kmer kmer, int value); |
| 34 |
|
| 35 |
public abstract int set(Kmer kmer, int[] vals); |
| 36 |
|
| 37 |
|
| 38 |
public abstract int setIfNotPresent(Kmer kmer, int value); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public abstract int getValue(Kmer kmer); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public abstract int[] getValues(Kmer kmer, int[] singleton); |
| 54 |
|
| 55 |
public abstract boolean contains(Kmer kmer); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static final int compare(long[] key1, long[] key2){ |
| 64 |
for(int i=0; i<key1.length; i++){ |
| 65 |
long dif=key1[i]-key2[i]; |
| 66 |
if(dif!=0){return (int)Tools.mid(-1, dif, 1);} |
| 67 |
} |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
public static final boolean equals(long[] key1, long[] key2){ |
| 72 |
return compare(key1, key2)==0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public abstract int getValue(long[] key, long xor); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public final boolean contains(Kmer kmer, int v){ |
| 112 |
assert(TESTMODE); |
| 113 |
int[] set=getValues(kmer, new int[] {-1}); |
| 114 |
if(set==null){return false;} |
| 115 |
for(int s : set){ |
| 116 |
if(s==-1){break;} |
| 117 |
if(s==v){return true;} |
| 118 |
} |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
public final boolean contains(Kmer kmer, int[] vals){ |
| 123 |
assert(TESTMODE); |
| 124 |
int[] set=getValues(kmer, new int[] {-1}); |
| 125 |
if(set==null){return false;} |
| 126 |
boolean success=true; |
| 127 |
for(int v : vals){ |
| 128 |
if(v==-1){break;} |
| 129 |
success=false; |
| 130 |
for(int s : set){ |
| 131 |
if(s==v){ |
| 132 |
success=true; |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
if(!success){break;} |
| 137 |
} |
| 138 |
return success; |
| 139 |
} |
| 140 |
|
| 141 |
public abstract void rebalance(); |
| 142 |
|
| 143 |
public abstract long size(); |
| 144 |
public abstract int arrayLength(); |
| 145 |
public abstract boolean canRebalance(); |
| 146 |
|
| 147 |
public abstract boolean dumpKmersAsText(TextStreamWriter tsw, int k, int mincount, int maxcount); |
| 148 |
public abstract boolean dumpKmersAsBytes(ByteStreamWriter bsw, int k, int mincount, int maxcount, AtomicLong remaining); |
| 149 |
public abstract boolean dumpKmersAsBytes_MT(final ByteStreamWriter bsw, final ByteBuilder bb, final int k, final int mincount, final int maxcount, AtomicLong remaining); |
| 150 |
|
| 151 |
public abstract void fillHistogram(long[] ca, int max); |
| 152 |
public abstract void fillHistogram(SuperLongList sll); |
| 153 |
public abstract void countGC(long[] gcCounts, int max); |
| 154 |
|
| 155 |
public static final int gc(long kmer){ |
| 156 |
int gc=0; |
| 157 |
while(kmer>0){ |
| 158 |
long x=kmer&3; |
| 159 |
kmer>>>=2; |
| 160 |
if(x==1 || x==2){gc++;} |
| 161 |
} |
| 162 |
return gc; |
| 163 |
} |
| 164 |
|
| 165 |
Object get(Kmer kmer){return get(kmer.key());} |
| 166 |
abstract Object get(long[] key); |
| 167 |
abstract void resize(); |
| 168 |
abstract boolean canResize(); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
abstract long regenerate(final int limit); |
| 178 |
|
| 179 |
final void lock(){getLock().lock();} |
| 180 |
final void unlock(){getLock().unlock();} |
| 181 |
final boolean tryLock(){return getLock().tryLock();} |
| 182 |
Lock getLock(){ |
| 183 |
throw new RuntimeException("Unimplemented."); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
final static AtomicIntegerArray allocAtomicInt(int len){ |
| 191 |
return KillSwitch.allocAtomicInt(len); |
| 192 |
} |
| 193 |
|
| 194 |
final static long[] allocLong1D(int len){ |
| 195 |
return KillSwitch.allocLong1D(len); |
| 196 |
} |
| 197 |
|
| 198 |
final static long[][] allocLong2D(int mult, int len){ |
| 199 |
return KillSwitch.allocLong2D(mult, len); |
| 200 |
} |
| 201 |
|
| 202 |
final static int[] allocInt1D(int len){ |
| 203 |
return KillSwitch.allocInt1D(len); |
| 204 |
} |
| 205 |
|
| 206 |
final static int[][] allocInt2D(int len){ |
| 207 |
return KillSwitch.allocInt2D(len); |
| 208 |
} |
| 209 |
|
| 210 |
final static KmerNodeU[] allocKmerNodeArray(int len){ |
| 211 |
KmerNodeU[] ret=null; |
| 212 |
try { |
| 213 |
ret=new KmerNodeU[len]; |
| 214 |
} catch (OutOfMemoryError e) { |
| 215 |
synchronized(killMessage){ |
| 216 |
e.printStackTrace(); |
| 217 |
System.err.println(killMessage); |
| 218 |
KillSwitch.killSilent(); |
| 219 |
} |
| 220 |
} |
| 221 |
return ret; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public abstract int setOwner(Kmer kmer, int newOwner); |
| 231 |
|
| 232 |
|
| 233 |
public abstract boolean clearOwner(Kmer kmer, int owner); |
| 234 |
|
| 235 |
|
| 236 |
public abstract int getOwner(Kmer kmer); |
| 237 |
|
| 238 |
|
| 239 |
public abstract void initializeOwnership(); |
| 240 |
|
| 241 |
|
| 242 |
public abstract void clearOwnership(); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static final StringBuilder toText(Kmer kmer){ |
| 249 |
return toText(kmer.key(), kmer.k); |
| 250 |
} |
| 251 |
|
| 252 |
public static final StringBuilder toText(long[] array, int k){ |
| 253 |
StringBuilder sb=new StringBuilder(k*array.length); |
| 254 |
for(int pos=0; pos<array.length; pos++){ |
| 255 |
long kmer=array[pos]; |
| 256 |
for(int i=k-1; i>=0; i--){ |
| 257 |
int x=(int)((kmer>>(2*i))&3); |
| 258 |
sb.append((char)AminoAcid.numberToBase[x]); |
| 259 |
} |
| 260 |
} |
| 261 |
return sb; |
| 262 |
} |
| 263 |
|
| 264 |
static final StringBuilder toText(long[] array, int count, int k){ |
| 265 |
StringBuilder sb=new StringBuilder(k+10); |
| 266 |
return toText(array, count, k, sb); |
| 267 |
} |
| 268 |
|
| 269 |
static final ByteBuilder toBytes(long[] array, int count, int k){ |
| 270 |
ByteBuilder bb=new ByteBuilder(k+10); |
| 271 |
return toBytes(array, count, k, bb); |
| 272 |
} |
| 273 |
|
| 274 |
static final StringBuilder toText(long[] array, int[] values, int k){ |
| 275 |
StringBuilder sb=new StringBuilder(k+10); |
| 276 |
return toText(array, values, k, sb); |
| 277 |
} |
| 278 |
|
| 279 |
static final ByteBuilder toBytes(long[] array, int[] values, int k){ |
| 280 |
ByteBuilder bb=new ByteBuilder(k+10); |
| 281 |
return toBytes(array, values, k, bb); |
| 282 |
} |
| 283 |
|
| 284 |
static final StringBuilder toText(long[] array, int count, int k, StringBuilder sb){ |
| 285 |
if(FASTA_DUMP){ |
| 286 |
sb.append('>'); |
| 287 |
sb.append(count); |
| 288 |
sb.append('\n'); |
| 289 |
for(int i=0; i<array.length; i++){ |
| 290 |
append(array[i], k, sb); |
| 291 |
} |
| 292 |
}else{ |
| 293 |
for(int i=0; i<array.length; i++){ |
| 294 |
append(array[i], k, sb); |
| 295 |
} |
| 296 |
sb.append('\t'); |
| 297 |
sb.append(count); |
| 298 |
} |
| 299 |
return sb; |
| 300 |
} |
| 301 |
|
| 302 |
static final StringBuilder toText(long[] array, int[] values, int k, StringBuilder sb){ |
| 303 |
if(FASTA_DUMP){ |
| 304 |
sb.append('>'); |
| 305 |
for(int i=0; i<values.length; i++){ |
| 306 |
int x=values[i]; |
| 307 |
if(x==-1){break;} |
| 308 |
if(i>0){sb.append(',');} |
| 309 |
sb.append(x); |
| 310 |
} |
| 311 |
sb.append('\n'); |
| 312 |
for(int i=0; i<array.length; i++){ |
| 313 |
append(array[i], k, sb); |
| 314 |
} |
| 315 |
}else{ |
| 316 |
for(int i=0; i<array.length; i++){ |
| 317 |
append(array[i], k, sb); |
| 318 |
} |
| 319 |
sb.append('\t'); |
| 320 |
for(int i=0; i<values.length; i++){ |
| 321 |
int x=values[i]; |
| 322 |
if(x==-1){break;} |
| 323 |
if(i>0){sb.append(',');} |
| 324 |
sb.append(x); |
| 325 |
} |
| 326 |
} |
| 327 |
return sb; |
| 328 |
} |
| 329 |
|
| 330 |
private static final void append(long kmer, int k, StringBuilder sb){ |
| 331 |
for(int i=k-1; i>=0; i--){ |
| 332 |
int x=(int)((kmer>>(2*i))&3); |
| 333 |
sb.append((char)AminoAcid.numberToBase[x]); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
public static final ByteBuilder toBytes(long[] array, long count, int k, ByteBuilder sb){ |
| 338 |
if(FASTA_DUMP){ |
| 339 |
sb.append('>'); |
| 340 |
sb.append(count); |
| 341 |
sb.append('\n'); |
| 342 |
for(int i=0; i<array.length; i++){ |
| 343 |
append(array[i], k, sb); |
| 344 |
} |
| 345 |
}else{ |
| 346 |
for(int i=0; i<array.length; i++){ |
| 347 |
append(array[i], k, sb); |
| 348 |
} |
| 349 |
sb.append('\t'); |
| 350 |
sb.append(count); |
| 351 |
} |
| 352 |
return sb; |
| 353 |
} |
| 354 |
|
| 355 |
public static final ByteBuilder toBytes(long[] array, int[] values, int k, ByteBuilder sb){ |
| 356 |
if(FASTA_DUMP){ |
| 357 |
sb.append('>'); |
| 358 |
for(int i=0; i<values.length; i++){ |
| 359 |
int x=values[i]; |
| 360 |
if(x==-1){break;} |
| 361 |
if(i>0){sb.append(',');} |
| 362 |
sb.append(x); |
| 363 |
} |
| 364 |
sb.append('\n'); |
| 365 |
for(int i=0; i<array.length; i++){ |
| 366 |
append(array[i], k, sb); |
| 367 |
} |
| 368 |
}else{ |
| 369 |
for(int i=0; i<array.length; i++){ |
| 370 |
append(array[i], k, sb); |
| 371 |
} |
| 372 |
sb.append('\t'); |
| 373 |
for(int i=0; i<values.length; i++){ |
| 374 |
int x=values[i]; |
| 375 |
if(x==-1){break;} |
| 376 |
if(i>0){sb.append(',');} |
| 377 |
sb.append(x); |
| 378 |
} |
| 379 |
} |
| 380 |
return sb; |
| 381 |
} |
| 382 |
|
| 383 |
private static final void append(long kmer, int k, ByteBuilder sb){ |
| 384 |
for(int i=k-1; i>=0; i--){ |
| 385 |
int x=(int)((kmer>>(2*i))&3); |
| 386 |
sb.append((char)AminoAcid.numberToBase[x]); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
static void appendKmerText(long[] array, int count, int k, ByteBuilder bb){ |
| 398 |
bb.setLength(0); |
| 399 |
toBytes(array, count, k, bb); |
| 400 |
bb.nl(); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
long flush(){ |
| 406 |
throw new RuntimeException("Unsupported."); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public static final AbstractKmerTableU[] preallocate(int ways, int tableType, int[] schedule, int k, int kbig){ |
| 417 |
|
| 418 |
final AbstractKmerTableU[] tables=new AbstractKmerTableU[ways]; |
| 419 |
|
| 420 |
{ |
| 421 |
final int t=Tools.max(1, Tools.min(Shared.threads(), 2, ways)); |
| 422 |
final AllocThread[] allocators=new AllocThread[t]; |
| 423 |
for(int i=0; i<t; i++){ |
| 424 |
allocators[i]=new AllocThread(tableType, schedule, i, t, k, kbig, tables); |
| 425 |
} |
| 426 |
for(AllocThread at : allocators){at.start();} |
| 427 |
for(AllocThread at : allocators){ |
| 428 |
while(at.getState()!=Thread.State.TERMINATED){ |
| 429 |
try { |
| 430 |
at.join(); |
| 431 |
} catch (InterruptedException e) { |
| 432 |
|
| 433 |
e.printStackTrace(); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
synchronized(tables){ |
| 440 |
for(int i=0; i<tables.length; i++){ |
| 441 |
final AbstractKmerTableU akt=tables[i]; |
| 442 |
if(akt==null){ |
| 443 |
throw new RuntimeException("KmerTable allocation failed, probably due to lack of RAM: "+i+", "+tables.length); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
return tables; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
private static class AllocThread extends Thread{ |
| 456 |
|
| 457 |
AllocThread(int type_, int[] schedule_, int mod_, int div_, |
| 458 |
int k_, int kbig_, AbstractKmerTableU[] tables_){ |
| 459 |
type=type_; |
| 460 |
schedule=schedule_; |
| 461 |
size=schedule[0]; |
| 462 |
mod=mod_; |
| 463 |
div=div_; |
| 464 |
growable=schedule.length>1; |
| 465 |
tables=tables_; |
| 466 |
k=k_; |
| 467 |
kbig=kbig_; |
| 468 |
} |
| 469 |
|
| 470 |
@Override |
| 471 |
public void run(){ |
| 472 |
for(int i=mod; i<tables.length; i+=div){ |
| 473 |
|
| 474 |
final AbstractKmerTableU akt; |
| 475 |
if(type==FOREST1D){ |
| 476 |
akt=new HashForestU(size, k, growable, false); |
| 477 |
}else if(type==ARRAY1D){ |
| 478 |
akt=new HashArrayU1D(schedule, k, kbig); |
| 479 |
|
| 480 |
}else if(type==NODE1D){ |
| 481 |
throw new RuntimeException("Must use forest, table, or array data structure. Type="+type); |
| 482 |
|
| 483 |
}else if(type==FOREST2D){ |
| 484 |
akt=new HashForestU(size, k, growable, true); |
| 485 |
}else if(type==ARRAY2D){ |
| 486 |
akt=new HashArrayU2D(schedule, k, kbig); |
| 487 |
|
| 488 |
}else if(type==NODE2D){ |
| 489 |
throw new RuntimeException("Must use forest, table, or array data structure. Type="+type); |
| 490 |
|
| 491 |
}else if(type==ARRAYH){ |
| 492 |
akt=new HashArrayUHybrid(schedule, k, kbig); |
| 493 |
|
| 494 |
}else{ |
| 495 |
throw new RuntimeException("Must use forest, table, or array data structure. Type="+type); |
| 496 |
} |
| 497 |
synchronized(tables){ |
| 498 |
tables[i]=akt; |
| 499 |
} |
| 500 |
|
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
private final int type; |
| 505 |
private final int[] schedule; |
| 506 |
private final int size; |
| 507 |
private final int mod; |
| 508 |
private final int div; |
| 509 |
private final int k; |
| 510 |
private final int kbig; |
| 511 |
private final boolean growable; |
| 512 |
final AbstractKmerTableU[] tables; |
| 513 |
|
| 514 |
} |
| 515 |
|
| 516 |
public WalkerU walk() { |
| 517 |
throw new RuntimeException("Unimplemented"); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
public static boolean FASTA_DUMP=true; |
| 525 |
public static boolean NUMERIC_DUMP=false; |
| 526 |
|
| 527 |
public static final boolean verbose=false; |
| 528 |
public static final boolean TESTMODE=false; |
| 529 |
|
| 530 |
public static final int UNKNOWN=0, ARRAY1D=1, FOREST1D=2, NODE1D=4, ARRAY2D=5, FOREST2D=6, NODE2D=8, ARRAYH=9; |
| 531 |
|
| 532 |
public static final int NOT_PRESENT=-1, HASH_COLLISION=-2; |
| 533 |
public static final int NO_OWNER=-1; |
| 534 |
|
| 535 |
private static final String killMessage=new String("\nThis program ran out of memory. Try increasing the -Xmx flag and setting prealloc."); |
| 536 |
|
| 537 |
} |