| 1 |
package kmer; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.BitSet; |
| 8 |
import java.util.concurrent.atomic.AtomicLong; |
| 9 |
|
| 10 |
import bloom.KCountArray; |
| 11 |
import bloom.KmerCountAbstract; |
| 12 |
import bloom.ReadCounter; |
| 13 |
import fileIO.ByteStreamWriter; |
| 14 |
import jgi.CallPeaks; |
| 15 |
import shared.Shared; |
| 16 |
import shared.Timer; |
| 17 |
import shared.Tools; |
| 18 |
import stream.Read; |
| 19 |
import structures.IntList; |
| 20 |
import ukmer.Kmer; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public abstract class AbstractKmerTableSet { |
| 29 |
|
| 30 |
public static final boolean isValidArgument(String a){ |
| 31 |
if(a.equals("in") || a.equals("in1")){ |
| 32 |
}else if(a.equals("in2")){ |
| 33 |
}else if(a.equals("append") || a.equals("app")){ |
| 34 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 35 |
}else if(a.equals("initialsize")){ |
| 36 |
}else if(a.equals("showstats") || a.equals("stats")){ |
| 37 |
}else if(a.equals("ways")){ |
| 38 |
}else if(a.equals("buflen") || a.equals("bufflen") || a.equals("bufferlength")){ |
| 39 |
}else if(a.equals("k")){ |
| 40 |
}else if(a.equals("threads") || a.equals("t")){ |
| 41 |
}else if(a.equals("showspeed") || a.equals("ss")){ |
| 42 |
}else if(a.equals("ecco")){ |
| 43 |
}else if(a.equals("merge")){ |
| 44 |
}else if(a.equals("verbose")){ |
| 45 |
}else if(a.equals("verbose2")){ |
| 46 |
}else if(a.equals("minprob")){ |
| 47 |
}else if(a.equals("reads") || a.startsWith("maxreads")){ |
| 48 |
}else if(a.equals("prealloc") || a.equals("preallocate")){ |
| 49 |
}else if(a.equals("prefilter")){ |
| 50 |
}else if(a.equals("prefiltersize") || a.equals("prefilterfraction") || a.equals("pff")){ |
| 51 |
}else if(a.equals("minprobprefilter") || a.equals("mpp")){ |
| 52 |
}else if(a.equals("minprobmain") || a.equals("mpm")){ |
| 53 |
}else if(a.equals("prefilterpasses") || a.equals("prepasses")){ |
| 54 |
}else if(a.equals("prehashes") || a.equals("hashes")){ |
| 55 |
}else if(a.equals("onepass")){ |
| 56 |
}else if(a.equals("passes")){ |
| 57 |
}else if(a.equals("rcomp")){ |
| 58 |
}else if(a.equals("maskmiddle")){ |
| 59 |
}else if(a.equals("filtermemory") || a.equals("prefiltermemory") || a.equals("filtermem")){ |
| 60 |
}else{ |
| 61 |
return false; |
| 62 |
} |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public final void process(Timer t){ |
| 72 |
|
| 73 |
|
| 74 |
long added=processInput(); |
| 75 |
|
| 76 |
|
| 77 |
t.stop(); |
| 78 |
|
| 79 |
showStats(t, added); |
| 80 |
|
| 81 |
|
| 82 |
if(errorState){ |
| 83 |
throw new RuntimeException(getClass().getSimpleName()+" terminated in an error state; the output may be corrupt."); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
public abstract void clear(); |
| 89 |
|
| 90 |
|
| 91 |
public final long processInput(){ |
| 92 |
|
| 93 |
|
| 94 |
Timer t=new Timer(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
prefilterArray=makePrefilter(new KCountArray[1], null); |
| 103 |
if(prefilterArray!=null){ |
| 104 |
prefilterArray.purgeFilter(); |
| 105 |
filterMax2=Tools.min(filterMax, prefilterArray.maxValue-1); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if(DISPLAY_STATS){System.err.println("Estimated kmer capacity: \t"+estimatedKmerCapacity());} |
| 117 |
|
| 118 |
assert(!allocated); |
| 119 |
allocateTables(); |
| 120 |
allocated=true; |
| 121 |
|
| 122 |
if(DISPLAY_PROGRESS){ |
| 123 |
outstream.println("After table allocation:"); |
| 124 |
Shared.printMemory(); |
| 125 |
outstream.println(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
long added=loadKmers(); |
| 130 |
|
| 131 |
|
| 132 |
prefilterArray=null; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
return added; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
public final KCountArray makePrefilter(final KCountArray[] filter, Timer ht){ |
| 145 |
|
| 146 |
if(!prefilter){return null;} |
| 147 |
assert(!MASK_MIDDLE) : "TODO: MaskMiddle not yet supported by prefilter."; |
| 148 |
|
| 149 |
if(filter[0]!=null){ |
| 150 |
filter[0].purgeFilter(); |
| 151 |
assert(filter[0].prefilter()==null); |
| 152 |
} |
| 153 |
|
| 154 |
KmerCountAbstract.CANONICAL=true; |
| 155 |
|
| 156 |
long precells=-1; |
| 157 |
int cbits=1; |
| 158 |
if(onePass){ |
| 159 |
while(filterMax>=(1<<cbits)){cbits*=2;} |
| 160 |
}else{ |
| 161 |
while(filterMax+1>=(1<<cbits)){cbits*=2;} |
| 162 |
} |
| 163 |
if(prepasses>2 && currentPass==prepasses-1){cbits=1;} |
| 164 |
|
| 165 |
byte minq=0; |
| 166 |
if(precells<1){ |
| 167 |
long prebits=(filterMemory(currentPass)-10)*8; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
precells=prebits/cbits; |
| 172 |
if(precells<100000){ |
| 173 |
prefilter=false; |
| 174 |
return null; |
| 175 |
} |
| 176 |
} |
| 177 |
if(prehashes<1){prehashes=2;} |
| 178 |
|
| 179 |
ReadCounter rc=new ReadCounter(kbig(), true, ecco(), false, Shared.AMINO_IN); |
| 180 |
if(onePass){ |
| 181 |
assert(filter==null || filter.length==1) : "Multiple filtering passes are not allowed in onepass mode.\n"+filter.length+","+prepasses+", "+onePass+", "+prefilter; |
| 182 |
filter[0]=rc.makeKca(null, null, null, cbits, precells, prehashes, minq, |
| 183 |
maxReads, 1, 1, 1, null, 0); |
| 184 |
}else{ |
| 185 |
if(ht==null){ht=new Timer();} |
| 186 |
ht.start(); |
| 187 |
filter[0]=rc.makeKca_als(in1, in2, extra, cbits, precells, prehashes, minq, |
| 188 |
maxReads, 1, 1, 1, filter[0], filterMax); |
| 189 |
assert(filterMax<filter[0].maxValue || (currentPass>0 && currentPass==prepasses-1)); |
| 190 |
outstream.println("Made prefilter: \t"+filter[0].toShortString(prehashes)); |
| 191 |
double uf=filter[0].usedFraction(); |
| 192 |
|
| 193 |
if(uf>0.5){ |
| 194 |
outstream.println("Warning: This table is "+(uf>0.995 ? "totally" : uf>0.99 ? "crazy" : uf>0.95 ? "incredibly" : uf>0.9 ? "extremely" : uf>0.8 ? "very" : |
| 195 |
uf>0.7 ? "rather" : uf>0.6 ? "fairly" : "somewhat")+" full. Ideal load is under 50% used." + |
| 196 |
"\nFor better accuracy, run on a node with more memory; quality-trim or error-correct reads; or increase prefiltersize."); |
| 197 |
} |
| 198 |
ht.stop(); |
| 199 |
currentPass++; |
| 200 |
|
| 201 |
final double kmers=filter[0].estimateUniqueKmers(prehashes, Tools.min(filterMax+1, filter[0].maxValue)); |
| 202 |
outstream.println("Estimated valid kmers: \t\t"+(long)kmers); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if(prepasses<0){ |
| 210 |
if((currentPass&1)==0){ |
| 211 |
return makePrefilter(filter, ht); |
| 212 |
}else if(currentPass<5){ |
| 213 |
if(kmers>estimatedKmerCapacity()){ |
| 214 |
return makePrefilter(filter, ht); |
| 215 |
} |
| 216 |
} |
| 217 |
}else if(currentPass<prepasses){ |
| 218 |
return makePrefilter(filter, ht); |
| 219 |
} |
| 220 |
|
| 221 |
if(DISPLAY_PROGRESS){ |
| 222 |
outstream.println("Prefilter time:\t"+ht); |
| 223 |
outstream.println("After prefilter:"); |
| 224 |
Shared.printMemory(); |
| 225 |
outstream.println(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return filter[0]; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
public final void showStats(Timer t, long added){ |
| 234 |
|
| 235 |
if(!DISPLAY_STATS){return;} |
| 236 |
|
| 237 |
if(DISPLAY_PROGRESS){ |
| 238 |
outstream.println("After loading:"); |
| 239 |
Shared.printMemory(); |
| 240 |
outstream.println(); |
| 241 |
} |
| 242 |
|
| 243 |
t.stop(); |
| 244 |
outstream.println("Input: \t"+readsIn+" reads \t\t"+basesIn+" bases."); |
| 245 |
outstream.println("Unique Kmers: \t"+added); |
| 246 |
outstream.println("Load Time: \t"+t); |
| 247 |
|
| 248 |
if(showSpeed){ |
| 249 |
outstream.println(); |
| 250 |
outstream.println(Tools.readsBasesProcessed(t.elapsed, readsIn, basesIn, 8)); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
private final long loadKmers(){ |
| 260 |
|
| 261 |
assert(allocated); |
| 262 |
kmersLoaded=0; |
| 263 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 264 |
Read.VALIDATE_IN_CONSTRUCTOR=false; |
| 265 |
for(int i=0; i<in1.size(); i++){ |
| 266 |
String a=in1.get(i); |
| 267 |
String b=in2.size()>i ? in2.get(i) : null; |
| 268 |
int idx=a.indexOf('#'); |
| 269 |
if(idx>=0 && b==null && !new File(a).exists()){ |
| 270 |
b=a.replaceFirst("#", "2"); |
| 271 |
a=a.replaceFirst("#", "1"); |
| 272 |
} |
| 273 |
kmersLoaded+=loadKmers(a, b); |
| 274 |
} |
| 275 |
for(int i=0; i<extra.size(); i++){ |
| 276 |
String a=extra.get(i); |
| 277 |
String b=null; |
| 278 |
int idx=a.indexOf('#'); |
| 279 |
if(idx>=0 && b==null && !new File(a).exists()){ |
| 280 |
b=a.replaceFirst("#", "2"); |
| 281 |
a=a.replaceFirst("#", "1"); |
| 282 |
} |
| 283 |
kmersLoaded+=loadKmers(a, b); |
| 284 |
} |
| 285 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 286 |
return kmersLoaded; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public abstract long loadKmers(String fname1, String fname2); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public abstract long regenerate(int limit); |
| 299 |
|
| 300 |
public abstract Object getTable(int tnum); |
| 301 |
|
| 302 |
public abstract long[] fillHistogram(int histMax); |
| 303 |
|
| 304 |
public abstract void countGC(long[] gcCounts, int max); |
| 305 |
|
| 306 |
public final long[] fillGcCounts(int histMax){ |
| 307 |
long[] gcCounts=new long[histMax+1]; |
| 308 |
countGC(gcCounts, histMax); |
| 309 |
return gcCounts; |
| 310 |
} |
| 311 |
|
| 312 |
public final float[] makeGcHistogram(long[] counts, long[] gcCounts){ |
| 313 |
float[] gcHist=new float[counts.length]; |
| 314 |
final long k=kbig(); |
| 315 |
for(int i=0; i<counts.length; i++){ |
| 316 |
long gc=gcCounts[i]; |
| 317 |
double bases=Tools.max(counts[i], 1)*k; |
| 318 |
gcHist[i]=(float)(gc/bases); |
| 319 |
} |
| 320 |
return gcHist; |
| 321 |
} |
| 322 |
|
| 323 |
public abstract void initializeOwnership(); |
| 324 |
|
| 325 |
public abstract void clearOwnership(); |
| 326 |
|
| 327 |
public abstract int ways(); |
| 328 |
|
| 329 |
public final int fillCounts(byte[] bases, IntList counts, Kmer kmer){ |
| 330 |
return fillSpecificCounts(bases, counts, null, kmer); |
| 331 |
} |
| 332 |
|
| 333 |
public abstract int fillSpecificCounts(byte[] bases, IntList counts, BitSet positions, Kmer kmer); |
| 334 |
|
| 335 |
public abstract int regenerateCounts(byte[] bases, IntList counts, Kmer kmer, BitSet changed); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public abstract boolean dumpKmersAsBytes(String fname, int mincount, int maxcount, boolean printTime, AtomicLong remaining); |
| 342 |
public abstract boolean dumpKmersAsBytes_MT(String fname, int mincount, int maxcount, boolean printTime, AtomicLong remaining); |
| 343 |
|
| 344 |
public final long[][] makeKhist(String fname, int cols, int max, boolean printHeader, boolean printZeros, boolean printTime, |
| 345 |
boolean smooth, boolean calcGC, boolean doLogScale, double logWidth, int logPasses, int smoothRadius){ |
| 346 |
Timer t=new Timer(); |
| 347 |
|
| 348 |
long[] ca=fillHistogram(max); |
| 349 |
float[] gcHist=null; |
| 350 |
if(calcGC){ |
| 351 |
|
| 352 |
long[] gc=(calcGC ? fillGcCounts(max) : null); |
| 353 |
|
| 354 |
gcHist=makeGcHistogram(ca, gc); |
| 355 |
} |
| 356 |
|
| 357 |
long[] logScale=null; |
| 358 |
|
| 359 |
if(smooth){ |
| 360 |
ca=CallPeaks.smoothProgressive(ca, smoothRadius); |
| 361 |
} |
| 362 |
if(doLogScale){ |
| 363 |
logScale=CallPeaks.logScale(ca, logWidth, 1, logPasses); |
| 364 |
} |
| 365 |
|
| 366 |
long[][] ret=new long[2][]; |
| 367 |
ret[0]=ca; |
| 368 |
if(gcHist!=null){ |
| 369 |
final int k=kbig(); |
| 370 |
ret[1]=new long[ca.length]; |
| 371 |
for(int i=1; i<ca.length; i++){ |
| 372 |
ret[1][i]=Math.round(ca[i]*gcHist[i]*k); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
if(fname==null){return ret;} |
| 377 |
|
| 378 |
ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, false, true); |
| 379 |
bsw.start(); |
| 380 |
if(printHeader){ |
| 381 |
bsw.print("#Depth\t"+(cols==3 ? "RawCount\t" : "")+"Count"+(doLogScale ? "\tlogScale" : "")+(calcGC ? "\tGC%\n" : "\n")); |
| 382 |
} |
| 383 |
|
| 384 |
for(int i=1; i<ca.length; i++){ |
| 385 |
long count=ca[i]; |
| 386 |
if(printZeros || count>0){ |
| 387 |
bsw.print(i); |
| 388 |
bsw.print('\t'); |
| 389 |
if(cols==3){ |
| 390 |
bsw.print(i*count); |
| 391 |
bsw.print('\t'); |
| 392 |
} |
| 393 |
bsw.print(count); |
| 394 |
if(doLogScale){ |
| 395 |
bsw.print('\t').print(logScale[i]); |
| 396 |
} |
| 397 |
if(gcHist!=null){ |
| 398 |
bsw.print(Tools.format("\t%.2f", 100f*gcHist[i])); |
| 399 |
} |
| 400 |
bsw.print('\n'); |
| 401 |
} |
| 402 |
} |
| 403 |
bsw.poisonAndWait(); |
| 404 |
t.stop(); |
| 405 |
if(printTime){outstream.println("Histogram Write Time: \t"+t);} |
| 406 |
return ret; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public boolean showStats=true; |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public boolean errorState=false; |
| 419 |
|
| 420 |
|
| 421 |
public boolean prefilter=false; |
| 422 |
|
| 423 |
public boolean onePass=false; |
| 424 |
|
| 425 |
public boolean amino=false; |
| 426 |
|
| 427 |
public int prehashes=2; |
| 428 |
|
| 429 |
public double prefilterFraction=0.2; |
| 430 |
|
| 431 |
|
| 432 |
public int initialSize=-1; |
| 433 |
|
| 434 |
public double preallocFraction=1.0; |
| 435 |
|
| 436 |
public KCountArray prefilterArray=null; |
| 437 |
|
| 438 |
public boolean minProbPrefilter=true; |
| 439 |
public boolean minProbMain=true; |
| 440 |
|
| 441 |
|
| 442 |
public ArrayList<String> in1=new ArrayList<String>(), in2=new ArrayList<String>(); |
| 443 |
|
| 444 |
|
| 445 |
public ArrayList<String> extra=new ArrayList<String>(); |
| 446 |
|
| 447 |
|
| 448 |
public long maxReads=-1; |
| 449 |
|
| 450 |
public int buflen=1000; |
| 451 |
|
| 452 |
|
| 453 |
protected int filterMax=0; |
| 454 |
protected int filterMax2=0; |
| 455 |
|
| 456 |
public long readsIn=0; |
| 457 |
public long basesIn=0; |
| 458 |
public long lowqReads=0; |
| 459 |
public long lowqBases=0; |
| 460 |
public long readsTrimmed=0; |
| 461 |
public long basesTrimmed=0; |
| 462 |
|
| 463 |
public long kmersIn=0; |
| 464 |
public long kmersLoaded=0; |
| 465 |
|
| 466 |
private int currentPass=0; |
| 467 |
protected int prepasses=1; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public abstract int kbig(); |
| 474 |
public abstract long filterMemory(int pass); |
| 475 |
public abstract long tableMemory(); |
| 476 |
public abstract long estimatedKmerCapacity(); |
| 477 |
public abstract boolean ecco(); |
| 478 |
public abstract boolean qtrimLeft(); |
| 479 |
public abstract boolean qtrimRight(); |
| 480 |
public abstract float minAvgQuality(); |
| 481 |
public final int filterMax(){return filterMax;} |
| 482 |
public abstract boolean rcomp(); |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
protected abstract void allocateTables(); |
| 489 |
protected boolean allocated=false; |
| 490 |
|
| 491 |
|
| 492 |
public static PrintStream outstream=System.err; |
| 493 |
|
| 494 |
public static boolean overwrite=true; |
| 495 |
|
| 496 |
public static boolean append=false; |
| 497 |
|
| 498 |
public static boolean showSpeed=true; |
| 499 |
|
| 500 |
public static boolean DISPLAY_PROGRESS=true; |
| 501 |
|
| 502 |
public static boolean DISPLAY_STATS=true; |
| 503 |
|
| 504 |
public static boolean verbose=false; |
| 505 |
|
| 506 |
public static boolean verbose2=false; |
| 507 |
|
| 508 |
public static int THREADS=Shared.threads(); |
| 509 |
|
| 510 |
public static int maxNs=Integer.MAX_VALUE; |
| 511 |
public static int minLen=0; |
| 512 |
|
| 513 |
|
| 514 |
public static final int CLAIM_OFFSET=100000; |
| 515 |
|
| 516 |
|
| 517 |
public static final int initialSizeDefault=128000; |
| 518 |
|
| 519 |
public static final float[] PROB_CORRECT=Arrays.copyOf(align2.QualityTools.PROB_CORRECT, 127); |
| 520 |
public static final float[] PROB_CORRECT_INVERSE=Arrays.copyOf(align2.QualityTools.PROB_CORRECT_INVERSE, 127); |
| 521 |
|
| 522 |
public static boolean IGNORE_UNKNOWN_ARGS=true; |
| 523 |
|
| 524 |
public static final int NOT_PRESENT=AbstractKmerTable.NOT_PRESENT, HASH_COLLISION=AbstractKmerTable.HASH_COLLISION; |
| 525 |
public static final int NO_OWNER=AbstractKmerTable.NO_OWNER; |
| 526 |
|
| 527 |
public static double defaultMinprob=0; |
| 528 |
|
| 529 |
|
| 530 |
public static boolean MASK_CORE=false; |
| 531 |
public static boolean MASK_MIDDLE=false; |
| 532 |
public static boolean FAST_FILL=true; |
| 533 |
|
| 534 |
} |