Debian Codegraph @

current/ukmer/AbstractKmerTableU.java

raw symbols SCIP
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 * @author Brian Bushnell
18 * @date Oct 23, 2013
19 *
20 */
21 public abstract class AbstractKmerTableU {
22
23 /*--------------------------------------------------------------*/
24 /*---------------- Kmer methods ----------------*/
25 /*--------------------------------------------------------------*/
26
27 /** Returns count */
28 public abstract int increment(Kmer kmer);
29
30 /** Returns number of entries created */
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 /** Returns number of kmers added */
38 public abstract int setIfNotPresent(Kmer kmer, int value);
39
40 /**
41 * Fetch the value associated with a kmer.
42 * @param kmer
43 * @return A value. -1 means the kmer was not present.
44 */
45 public abstract int getValue(Kmer kmer);
46
47 /**
48 * Fetch the values associated with a kmer.
49 * @param kmer
50 * @param singleton A blank array of length 1.
51 * @return An array filled with values. Values of -1 are invalid.
52 */
53 public abstract int[] getValues(Kmer kmer, int[] singleton);
54
55 public abstract boolean contains(Kmer kmer);
56
57 // public abstract boolean contains(Kmer kmer, int v);
58 //
59 // public abstract boolean contains(Kmer kmer, int[] vals);
60 //
61 // public abstract Object get(Kmer kmer);
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 /*---------------- Abstract Methods ----------------*/
77 /*--------------------------------------------------------------*/
78
79 public abstract int getValue(long[] key, long xor);
80
81 // /** Returns count */
82 // public final int increment(long[] key){throw new RuntimeException();}
83 //
84 // /** Returns number of entries created */
85 // public final int incrementAndReturnNumCreated(final long[] key){throw new RuntimeException();}
86 //
87 // public final int set(long[] key, int value){throw new RuntimeException();}
88 //
89 // public final int set(long[] key, int[] vals){throw new RuntimeException();}
90 //
91 // /** Returns number of kmers added */
92 // public final int setIfNotPresent(long[] key, int value){throw new RuntimeException();}
93 //
94 // /**
95 // * Fetch the value associated with a kmer.
96 // * @param kmer
97 // * @return A value. -1 means the kmer was not present.
98 // */
99 // final int getValue(long[] key){throw new RuntimeException();}
100 //
101 // /**
102 // * Fetch the values associated with a kmer.
103 // * @param kmer
104 // * @param singleton A blank array of length 1.
105 // * @return An array filled with values. Values of -1 are invalid.
106 // */
107 // public final int[] getValues(long[] key, int[] singleton){throw new RuntimeException();}
108 //
109 // public final boolean contains(long[] key){throw new RuntimeException();}
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 * Removes entries with a value of zero or less.
174 * Rehashes the remainder.
175 * @return Number removed.
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 /*--------------- Allocation Methods ----------------*/
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 /*--------------- Ownership Methods ----------------*/
226 /*--------------------------------------------------------------*/
227
228 /** Set the thread owning this kmer. Return the new owner.
229 * Will only change the owner if newOwner is greater than current owner. */
230 public abstract int setOwner(Kmer kmer, int newOwner);
231
232 /** Reset owner to -1 if this is the current owner. */
233 public abstract boolean clearOwner(Kmer kmer, int owner);
234
235 /** Return the thread ID owning this kmer, or -1. */
236 public abstract int getOwner(Kmer kmer);
237
238 /** Create data structures needed for ownership representation */
239 public abstract void initializeOwnership();
240
241 /** Eliminate ownership data structures or set them to -1. */
242 public abstract void clearOwnership();
243
244 /*--------------------------------------------------------------*/
245 /*---------------- Methods ----------------*/
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 // static void appendKmerText(long kmer, int count, int k, StringBuilder sb){
392 // sb.setLength(0);
393 // toText(kmer, count, k, sb);
394 // sb.append('\n');
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 /** For buffered tables. */
405 long flush(){
406 throw new RuntimeException("Unsupported.");
407 }
408
409 /**
410 * This allocates the data structures in multiple threads. Unfortunately, it does not lead to any speedup, at least for ARRAY type.
411 * @param ways
412 * @param tableType
413 * @param schedule
414 * @return Preallocated tables.
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 // TODO Auto-generated catch block
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 /*---------------- Nested Classes ----------------*/
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 // System.err.println("T"+i+" allocating "+i);
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 // akt=new HashArrayU1D(size, k, kbig, growable);
480 }else if(type==NODE1D){
481 throw new RuntimeException("Must use forest, table, or array data structure. Type="+type);
482 // akt=new KmerNode2(-1, 0);
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 // akt=new HashArrayU2D(size, k, kbig, growable);
488 }else if(type==NODE2D){
489 throw new RuntimeException("Must use forest, table, or array data structure. Type="+type);
490 // akt=new KmerNode(-1, 0);
491 }else if(type==ARRAYH){
492 akt=new HashArrayUHybrid(schedule, k, kbig);
493 // akt=new HashArrayUHybrid(size, k, kbig, growable);
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 // System.err.println("T"+i+" allocated "+i);
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 /*---------------- Fields ----------------*/
522 /*--------------------------------------------------------------*/
523
524 public static boolean FASTA_DUMP=true;
525 public static boolean NUMERIC_DUMP=false;
526
527 public static final boolean verbose=false; //slow
528 public static final boolean TESTMODE=false; //slow
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 }