| 1 |
package bloom; |
| 2 |
|
| 3 |
import java.io.Serializable; |
| 4 |
import java.util.concurrent.atomic.AtomicInteger; |
| 5 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 6 |
|
| 7 |
import dna.AminoAcid; |
| 8 |
import shared.Shared; |
| 9 |
import shared.Tools; |
| 10 |
import structures.ByteBuilder; |
| 11 |
import structures.LongList; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public abstract class KCountArray implements Serializable { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private static final long serialVersionUID = 1590374813059942002L; |
| 23 |
|
| 24 |
public static KCountArray makeNew(long cells_, int cbits_){ |
| 25 |
return makeNew(cells_, cbits_, 1); |
| 26 |
} |
| 27 |
|
| 28 |
public static KCountArray makeNew(long cells_, int cbits_, int hashes_){ |
| 29 |
return makeNew(cells_, cbits_, hashes_, null, 0); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
public static KCountArray makeNew(long cells_, int cbits_, int hashes_, KCountArray prefilter, int prefilterLimit_){ |
| 34 |
KCountArray kca=new KCountArray7MTA(cells_, cbits_, hashes_, prefilter, prefilterLimit_); |
| 35 |
kca.initialize(); |
| 36 |
return kca; |
| 37 |
} |
| 38 |
|
| 39 |
protected KCountArray(final long cells_, int cbits_){ |
| 40 |
assert(cbits_<=32); |
| 41 |
assert(Integer.bitCount(cbits_)==1); |
| 42 |
assert(Long.bitCount(cells_)==1) || this.getClass()==KCountArray7MT.class : this.getClass(); |
| 43 |
|
| 44 |
numArrays=64; |
| 45 |
arrayBits=31-Integer.numberOfLeadingZeros(numArrays); |
| 46 |
arrayMask=numArrays-1; |
| 47 |
|
| 48 |
while(cbits_*cells_<32*numArrays){ |
| 49 |
assert(false) : cells_+", "+cbits_+", "+numArrays+", "+(cbits_*cells_)+"<"+(32*numArrays); |
| 50 |
cbits_*=2; |
| 51 |
} |
| 52 |
|
| 53 |
assert(cbits_<=32); |
| 54 |
|
| 55 |
cells=cells_; |
| 56 |
cellBits=cbits_; |
| 57 |
valueMask=(cellBits==32 ? Integer.MAX_VALUE : ~((-1)<<cellBits)); |
| 58 |
maxValue=min(Integer.MAX_VALUE, ~((-1)<<min(cellBits,31))); |
| 59 |
cellsPerWord=32/cellBits; |
| 60 |
indexShift=Integer.numberOfTrailingZeros(cellsPerWord); |
| 61 |
cellMask=cellsPerWord-1; |
| 62 |
|
| 63 |
if(verbose){ |
| 64 |
System.out.println(description()); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
protected KCountArray(final long cells_, int cbits_, int arrays_){ |
| 69 |
assert(cbits_<=32); |
| 70 |
assert(Integer.bitCount(cbits_)==1); |
| 71 |
assert(Long.bitCount(cells_)==1) || this.getClass()==KCountArray7MT.class || this.getClass()==KCountArray7MTA.class || this.getClass()==KCountArray8MT.class; |
| 72 |
|
| 73 |
numArrays=arrays_; |
| 74 |
assert(Integer.bitCount(numArrays)==1) : numArrays+", "+cells_+", "+cbits_; |
| 75 |
arrayBits=31-Integer.numberOfLeadingZeros(numArrays); |
| 76 |
arrayMask=numArrays-1; |
| 77 |
|
| 78 |
while(cbits_*cells_<32*numArrays){ |
| 79 |
assert(false) : cells_+", "+cbits_+", "+numArrays+", "+(cbits_*cells_)+"<"+(32*numArrays); |
| 80 |
cbits_*=2; |
| 81 |
} |
| 82 |
|
| 83 |
assert(cbits_<=32) : "Why?"; |
| 84 |
|
| 85 |
cells=cells_; |
| 86 |
cellBits=cbits_; |
| 87 |
valueMask=(cellBits==32 ? Integer.MAX_VALUE : ~((-1)<<cellBits)); |
| 88 |
maxValue=min(Integer.MAX_VALUE, ~((-1)<<min(cellBits,31))); |
| 89 |
cellsPerWord=32/cellBits; |
| 90 |
indexShift=Integer.numberOfTrailingZeros(cellsPerWord); |
| 91 |
cellMask=cellsPerWord-1; |
| 92 |
|
| 93 |
if(verbose){ |
| 94 |
System.out.println(description()); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public abstract int read(long key); |
| 99 |
public int read(long keys[]){throw new RuntimeException("Unimplemented.");} |
| 100 |
public final int read(long key, int k, boolean makeCanonical){return read(makeCanonical ? makeCanonical2(key, k) : key);} |
| 101 |
|
| 102 |
public abstract void write(long key, int value); |
| 103 |
|
| 104 |
|
| 105 |
public final void increment(long key){increment(key, 1);} |
| 106 |
public final void decrement(long key){decrement(key, 1);} |
| 107 |
|
| 108 |
|
| 109 |
public abstract void increment(long key, int incr); |
| 110 |
|
| 111 |
|
| 112 |
public abstract int incrementAndReturnUnincremented(long key, int incr); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public int incrementAndReturnUnincremented(long[] keys, int incr){ |
| 121 |
throw new RuntimeException("Unimplemented."); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
public void decrement(long key, int incr){ |
| 126 |
throw new RuntimeException("This class "+getClass().getName()+" does not support decrement."); |
| 127 |
} |
| 128 |
|
| 129 |
public final int readPrecise(long key, int k, boolean makeCanonical){ |
| 130 |
assert(k<=32); |
| 131 |
int b=read(makeCanonical ? makeCanonical2(key, k) : key); |
| 132 |
if(b<1){return b;} |
| 133 |
int a=readLeft(key, k, makeCanonical); |
| 134 |
if(a>=b){return b;} |
| 135 |
int c=readRight(key, k, makeCanonical); |
| 136 |
if(c>=b){return b;} |
| 137 |
return (int)(((long)a+(long)c)/2); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
public final int readPreciseMin(long key, int k, boolean makeCanonical){ |
| 145 |
assert(k<=32); |
| 146 |
int b=read(makeCanonical ? makeCanonical2(key, k) : key); |
| 147 |
if(b<1){return b;} |
| 148 |
int a=readLeft(key, k, makeCanonical); |
| 149 |
if(a<1){return a;} |
| 150 |
int c=readRight(key, k, makeCanonical); |
| 151 |
return Tools.min(a, b, c); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public int readLeft(long key, int k, boolean makeCanonical){throw new RuntimeException("Unsupported.");} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public int readRight(long key, int k, boolean makeCanonical){throw new RuntimeException("Unsupported.");} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public int[] readAllLeft(final long key, final int k, boolean makeCanonical, int[] rvec){throw new RuntimeException("Unsupported.");} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public int[] readAllRight(final long key, final int k, boolean makeCanonical, int[] rvec){throw new RuntimeException("Unsupported.");} |
| 174 |
|
| 175 |
|
| 176 |
@Deprecated |
| 177 |
public void increment(long[] keys){ |
| 178 |
synchronized(this){ |
| 179 |
for(long key : keys){ |
| 180 |
increment(key); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
public void increment(LongList keys){ |
| 187 |
synchronized(this){ |
| 188 |
final long[] array=keys.array; |
| 189 |
for(int i=0; i<keys.size; i++){ |
| 190 |
increment(array[i]); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
public abstract long[] transformToFrequency(); |
| 196 |
public final long[] transformToFrequency(int[][] matrix){ |
| 197 |
long[] freq=new long[100000]; |
| 198 |
int maxFreq=freq.length-1; |
| 199 |
|
| 200 |
if(cellBits!=32){ |
| 201 |
assert(cellBits>0); |
| 202 |
for(int[] array : matrix){ |
| 203 |
for(int i=0; i<array.length; i++){ |
| 204 |
int word=array[i]; |
| 205 |
int j=cellsPerWord; |
| 206 |
|
| 207 |
for(; word!=0; j--){ |
| 208 |
int x=word&valueMask; |
| 209 |
int x2=(int)min(x, maxFreq); |
| 210 |
freq[x2]++; |
| 211 |
word=(word>>>cellBits); |
| 212 |
|
| 213 |
} |
| 214 |
freq[0]+=j; |
| 215 |
} |
| 216 |
} |
| 217 |
}else{ |
| 218 |
for(int[] array : matrix){ |
| 219 |
for(int i=0; i<array.length; i++){ |
| 220 |
int word=array[i]; |
| 221 |
int x2=(int)min(word, maxFreq); |
| 222 |
freq[x2]++; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
return freq; |
| 227 |
} |
| 228 |
|
| 229 |
public final long[] transformToFrequency(AtomicIntegerArray[] matrix){ |
| 230 |
long[] freq=new long[100000]; |
| 231 |
int maxFreq=freq.length-1; |
| 232 |
|
| 233 |
if(cellBits!=32){ |
| 234 |
assert(cellBits>0); |
| 235 |
for(AtomicIntegerArray array : matrix){ |
| 236 |
for(int i=0; i<array.length(); i++){ |
| 237 |
int word=array.get(i); |
| 238 |
int j=cellsPerWord; |
| 239 |
|
| 240 |
for(; word!=0; j--){ |
| 241 |
int x=word&valueMask; |
| 242 |
int x2=(int)min(x, maxFreq); |
| 243 |
freq[x2]++; |
| 244 |
word=(word>>>cellBits); |
| 245 |
|
| 246 |
} |
| 247 |
freq[0]+=j; |
| 248 |
} |
| 249 |
} |
| 250 |
}else{ |
| 251 |
for(AtomicIntegerArray array : matrix){ |
| 252 |
for(int i=0; i<array.length(); i++){ |
| 253 |
int word=array.get(i); |
| 254 |
int x2=(int)min(word, maxFreq); |
| 255 |
freq[x2]++; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
return freq; |
| 260 |
} |
| 261 |
|
| 262 |
public final ByteBuilder description(){ |
| 263 |
ByteBuilder sb=new ByteBuilder(); |
| 264 |
long words=cells/cellsPerWord; |
| 265 |
int wordsPerArray=(int)(words/numArrays); |
| 266 |
sb.append("cells: \t"+cells).append('\n'); |
| 267 |
sb.append("cellBits:\t"+cellBits).append('\n'); |
| 268 |
sb.append("valueMask:\t"+Long.toHexString(valueMask)).append('\n'); |
| 269 |
sb.append("maxValue:\t"+maxValue).append('\n'); |
| 270 |
sb.append("cellsPerWord:\t"+cellsPerWord).append('\n'); |
| 271 |
sb.append("indexShift:\t"+indexShift).append('\n'); |
| 272 |
sb.append("words: \t"+words).append('\n'); |
| 273 |
sb.append("wordsPerArray:\t"+wordsPerArray).append('\n'); |
| 274 |
sb.append("numArrays:\t"+numArrays).append('\n'); |
| 275 |
sb.append("Memory: \t"+mem()).append('\n'); |
| 276 |
sb.append("Usage: \t"+Tools.format("%.3f%%",usedFraction()*100)); |
| 277 |
return sb; |
| 278 |
} |
| 279 |
|
| 280 |
public final String toShortString(){ |
| 281 |
return "mem = "+mem()+" \tcells = "+toKMG(cells)+" \tused = "+Tools.format("%.3f%%",usedFraction()*100); |
| 282 |
} |
| 283 |
|
| 284 |
public final String toShortString(int hashes){ |
| 285 |
return ("hashes = "+hashes+" \t ")+ |
| 286 |
"mem = "+mem()+" \tcells = "+toKMG(cells)+" \tused = "+Tools.format("%.3f%%",usedFraction()*100); |
| 287 |
} |
| 288 |
|
| 289 |
@Override |
| 290 |
public final String toString(){ |
| 291 |
return description().toString(); |
| 292 |
} |
| 293 |
|
| 294 |
public abstract CharSequence toContentsString(); |
| 295 |
|
| 296 |
public abstract double usedFraction(); |
| 297 |
|
| 298 |
public abstract double usedFraction(int mindepth); |
| 299 |
|
| 300 |
public abstract long cellsUsed(int mindepth); |
| 301 |
|
| 302 |
public final double estimateUniqueKmers(int hashes){ |
| 303 |
double f=usedFraction(); |
| 304 |
double f2=(1-Math.pow(1-f, 1.0/hashes)); |
| 305 |
double n=(-cells)*Math.log(1-f2); |
| 306 |
return n; |
| 307 |
} |
| 308 |
|
| 309 |
public final double estimateUniqueKmers(int hashes, int mindepth){ |
| 310 |
|
| 311 |
double f=usedFraction(mindepth); |
| 312 |
double f2=(1-Math.pow(1-f, 1.0/hashes)); |
| 313 |
double n=(-cells)*Math.log(1-f2); |
| 314 |
return n; |
| 315 |
} |
| 316 |
|
| 317 |
public final double estimateUniqueKmersFromUsedFraction(int hashes, double usedFraction){ |
| 318 |
double f=usedFraction; |
| 319 |
double f2=(1-Math.pow(1-f, 1.0/hashes)); |
| 320 |
double n=(-cells)*Math.log(1-f2); |
| 321 |
return n; |
| 322 |
} |
| 323 |
|
| 324 |
public final String mem(){ |
| 325 |
long mem=(cells*cellBits)/8; |
| 326 |
if(mem<(1<<20)){ |
| 327 |
return (Tools.format("%.2f KB", mem*1d/(1<<10))); |
| 328 |
}else if(mem<(1<<30)){ |
| 329 |
return (Tools.format("%.2f MB", mem*1d/(1<<20))); |
| 330 |
}else{ |
| 331 |
return (Tools.format("%.2f GB", mem*1d/(1<<30))); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
public static String toKMG(long x){ |
| 336 |
double div=1; |
| 337 |
String ext=""; |
| 338 |
if(x>10000000000L){ |
| 339 |
div=1000000000L; |
| 340 |
ext="B"; |
| 341 |
}else if(x>10000000){ |
| 342 |
div=1000000; |
| 343 |
ext="M"; |
| 344 |
}else if(x>100000){ |
| 345 |
div=1000; |
| 346 |
ext="K"; |
| 347 |
} |
| 348 |
return Tools.format("%.2f", x/div)+ext; |
| 349 |
} |
| 350 |
|
| 351 |
static final AtomicIntegerArray[] allocMatrix(final int numArrays, final int wordsPerArray){ |
| 352 |
final AtomicIntegerArray[] matrix=new AtomicIntegerArray[numArrays]; |
| 353 |
final AllocThread[] array=new AllocThread[Tools.min(Tools.max(Shared.threads()/2, 1), numArrays)]; |
| 354 |
final AtomicInteger next=new AtomicInteger(0); |
| 355 |
for(int i=0; i<array.length; i++){ |
| 356 |
array[i]=new AllocThread(matrix, next, wordsPerArray); |
| 357 |
} |
| 358 |
for(int i=0; i<array.length; i++){array[i].start();} |
| 359 |
for(AllocThread at : array){ |
| 360 |
while(at.getState()!=Thread.State.TERMINATED){ |
| 361 |
try { |
| 362 |
at.join(); |
| 363 |
} catch (InterruptedException e) { |
| 364 |
|
| 365 |
e.printStackTrace(); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
return matrix; |
| 370 |
} |
| 371 |
|
| 372 |
private static class AllocThread extends Thread{ |
| 373 |
|
| 374 |
AllocThread(AtomicIntegerArray[] matrix_, AtomicInteger next_, int wordsPerArray_){ |
| 375 |
matrix=matrix_; |
| 376 |
next=next_; |
| 377 |
wordsPerArray=wordsPerArray_; |
| 378 |
} |
| 379 |
|
| 380 |
@Override |
| 381 |
public void run(){ |
| 382 |
int x=next.getAndIncrement(); |
| 383 |
while(x<matrix.length){ |
| 384 |
matrix[x]=new AtomicIntegerArray(wordsPerArray); |
| 385 |
x=next.getAndIncrement(); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
private final AtomicIntegerArray[] matrix; |
| 390 |
private final AtomicInteger next; |
| 391 |
private final int wordsPerArray; |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
abstract long hash(long x, int y); |
| 398 |
|
| 399 |
public static final int min(int x, int y){return x<y ? x : y;} |
| 400 |
public static final int max(int x, int y){return x>y ? x : y;} |
| 401 |
public static final long min(long x, long y){return x<y ? x : y;} |
| 402 |
public static final long max(long x, long y){return x>y ? x : y;} |
| 403 |
|
| 404 |
|
| 405 |
public void initialize(){} |
| 406 |
|
| 407 |
|
| 408 |
public void shutdown(){} |
| 409 |
|
| 410 |
public final long cells; |
| 411 |
public final int cellBits; |
| 412 |
|
| 413 |
public final int maxValue; |
| 414 |
|
| 415 |
protected final int cellsPerWord; |
| 416 |
protected final int indexShift; |
| 417 |
protected final int cellMask; |
| 418 |
protected final int valueMask; |
| 419 |
|
| 420 |
protected static int minArrays=calcMinArrays(); |
| 421 |
protected final int arrayBits; |
| 422 |
protected final int numArrays; |
| 423 |
protected final int arrayMask; |
| 424 |
|
| 425 |
public static boolean verbose=false; |
| 426 |
|
| 427 |
private static final int calcMinArrays(){ |
| 428 |
int x=Tools.max(Shared.threads(), 2); |
| 429 |
while(Integer.bitCount(x)!=1){x++;} |
| 430 |
return x; |
| 431 |
} |
| 432 |
|
| 433 |
public static final boolean isCanonical(long key, int k){ |
| 434 |
assert(k>3 && k<=32); |
| 435 |
long b=AminoAcid.reverseComplementBinaryFast(key, k); |
| 436 |
return key>=b; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
public static final long makeCanonical(final long key, final int k){ |
| 441 |
assert(k>3 && k<=32); |
| 442 |
|
| 443 |
final long r=AminoAcid.reverseComplementBinaryFast(key, k); |
| 444 |
assert(r>=key); |
| 445 |
|
| 446 |
|
| 447 |
return r; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
public static final long makeCanonical2(final long key, final int k){ |
| 452 |
assert(k>3 && k<=32); |
| 453 |
if(isCanonical(key, k)){return key;} |
| 454 |
long r=AminoAcid.reverseComplementBinaryFast(key, k); |
| 455 |
|
| 456 |
|
| 457 |
return r; |
| 458 |
} |
| 459 |
|
| 460 |
public KCountArray prefilter(){ |
| 461 |
throw new RuntimeException("TODO: Override"); |
| 462 |
} |
| 463 |
|
| 464 |
public void purgeFilter(){ |
| 465 |
throw new RuntimeException("TODO: Override"); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
public static boolean LOCKED_INCREMENT=false; |
| 470 |
public static boolean SET_LOCKED_INCREMENT=false; |
| 471 |
|
| 472 |
} |