package bloom; import java.io.Serializable; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicIntegerArray; import dna.AminoAcid; import shared.Shared; import shared.Tools; import structures.ByteBuilder; import structures.LongList; /** * @author Brian Bushnell * @date Jul 5, 2012 */ public abstract class KCountArray implements Serializable { /** * */ private static final long serialVersionUID = 1590374813059942002L; public static KCountArray makeNew(long cells_, int cbits_){ return makeNew(cells_, cbits_, 1); } public static KCountArray makeNew(long cells_, int cbits_, int hashes_){ return makeNew(cells_, cbits_, hashes_, null, 0); } //TODO: Get rid of keys_ arg. public static KCountArray makeNew(long cells_, int cbits_, int hashes_, KCountArray prefilter, int prefilterLimit_){ KCountArray kca=new KCountArray7MTA(cells_, cbits_, hashes_, prefilter, prefilterLimit_); kca.initialize(); return kca; } protected KCountArray(final long cells_, int cbits_){ assert(cbits_<=32); assert(Integer.bitCount(cbits_)==1); assert(Long.bitCount(cells_)==1) || this.getClass()==KCountArray7MT.class : this.getClass(); numArrays=64; arrayBits=31-Integer.numberOfLeadingZeros(numArrays); arrayMask=numArrays-1; while(cbits_*cells_<32*numArrays){ assert(false) : cells_+", "+cbits_+", "+numArrays+", "+(cbits_*cells_)+"<"+(32*numArrays); cbits_*=2; } //Increases bits per cell so that at minimum each array is size 1 assert(cbits_<=32); cells=cells_; cellBits=cbits_; valueMask=(cellBits==32 ? Integer.MAX_VALUE : ~((-1)<=b){return b;} int c=readRight(key, k, makeCanonical); if(c>=b){return b;} return (int)(((long)a+(long)c)/2); // return max(a, c); // int mid=Tools.min(a, b, c); // System.out.println("a="+a+", b="+b+", c="+c+" -> "+mid); // return mid; } public final int readPreciseMin(long key, int k, boolean makeCanonical){ assert(k<=32); int b=read(makeCanonical ? makeCanonical2(key, k) : key); if(b<1){return b;} int a=readLeft(key, k, makeCanonical); if(a<1){return a;} int c=readRight(key, k, makeCanonical); return Tools.min(a, b, c); } /** * @param key Kmer to evaluate * @return Sum of counts of all 4 possible left-adjacent kmers */ public int readLeft(long key, int k, boolean makeCanonical){throw new RuntimeException("Unsupported.");} /** * @param key Kmer to evaluate * @return Sum of counts of all 4 possible right-adjacent kmers */ public int readRight(long key, int k, boolean makeCanonical){throw new RuntimeException("Unsupported.");} /** * @param key Kmer to evaluate * @return Array of counts of all 4 possible left-adjacent kmers */ public int[] readAllLeft(final long key, final int k, boolean makeCanonical, int[] rvec){throw new RuntimeException("Unsupported.");} /** * @param key Kmer to evaluate * @return Array of counts of all 4 possible right-adjacent kmers */ public int[] readAllRight(final long key, final int k, boolean makeCanonical, int[] rvec){throw new RuntimeException("Unsupported.");} //Appears to never be used? Should be a LongList now anyway @Deprecated public void increment(long[] keys){ synchronized(this){ for(long key : keys){ increment(key); } } } //TODO: Optionally, add flag to eliminate duplicates or bulk-add them here public void increment(LongList keys){ synchronized(this){ final long[] array=keys.array; for(int i=0; i0); for(int[] array : matrix){ for(int i=0; i10000000000L){ div=1000000000L; ext="B"; }else if(x>10000000){ div=1000000; ext="M"; }else if(x>100000){ div=1000; ext="K"; } return Tools.format("%.2f", x/div)+ext; } static final AtomicIntegerArray[] allocMatrix(final int numArrays, final int wordsPerArray){ final AtomicIntegerArray[] matrix=new AtomicIntegerArray[numArrays]; final AllocThread[] array=new AllocThread[Tools.min(Tools.max(Shared.threads()/2, 1), numArrays)]; final AtomicInteger next=new AtomicInteger(0); for(int i=0; iy ? x : y;} public static final long min(long x, long y){return xy ? x : y;} /** Any necessary initialization. */ public void initialize(){} /** Any necessary shutdown steps. */ public void shutdown(){} public final long cells; public final int cellBits; /** Originally this was different than valueMask in the case that valueMask was negative, but now they are the same. */ public final int maxValue; protected final int cellsPerWord; protected final int indexShift; protected final int cellMask; protected final int valueMask; protected static int minArrays=calcMinArrays(); protected final int arrayBits; protected final int numArrays; protected final int arrayMask; public static boolean verbose=false; private static final int calcMinArrays(){ int x=Tools.max(Shared.threads(), 2); while(Integer.bitCount(x)!=1){x++;} return x; } public static final boolean isCanonical(long key, int k){ assert(k>3 && k<=32); long b=AminoAcid.reverseComplementBinaryFast(key, k); return key>=b; } /** Assumes that the key is not canonical */ public static final long makeCanonical(final long key, final int k){ assert(k>3 && k<=32); // assert(!isCanonical(key, k)); final long r=AminoAcid.reverseComplementBinaryFast(key, k); assert(r>=key); // assert(isCanonical(r, k)); // assert(AminoAcid.reverseComplementBinaryFast(r, k)==key); return r; } public static final long makeCanonical2(final long key, final int k){ assert(k>3 && k<=32); if(isCanonical(key, k)){return key;} long r=AminoAcid.reverseComplementBinaryFast(key, k); // assert(isCanonical(r, k)) : k+"\n"+Long.toBinaryString(key)+"\n"+Long.toBinaryString(r)+"\n"+Long.toBinaryString(AminoAcid.reverseComplementBinaryFast(r, k)); // assert(AminoAcid.reverseComplementBinaryFast(r, k)==key) : k+"\n"+Long.toBinaryString(key)+"\n"+Long.toBinaryString(r)+"\n"+Long.toBinaryString(AminoAcid.reverseComplementBinaryFast(r, k)); return r; } public KCountArray prefilter(){ throw new RuntimeException("TODO: Override"); } public void purgeFilter(){ throw new RuntimeException("TODO: Override"); } /** Increases accuracy of overloaded multi-bit tables */ public static boolean LOCKED_INCREMENT=false; public static boolean SET_LOCKED_INCREMENT=false; }