package sketch; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import kmer.AbstractKmerTable; import kmer.HashBuffer; import kmer.KmerTableSet; import shared.Shared; import shared.Timer; import shared.Tools; import structures.AbstractBitSet; import structures.IntHashMap; import structures.IntHashSetList; import structures.IntList; public class SketchIndex extends SketchObject { public SketchIndex(ArrayList refs){ refSketches=refs; tables=new KmerTableSet(new String[] {"ways="+WAYS, "tabletype="+AbstractKmerTable.ARRAYHF, "prealloc="+(prealloc>0 ? ""+prealloc : "f")}, 20+(defaultParams.trackCounts() ? 4 : 0)+18);//An extra 18 bytes per kmer because a lot of them occur multiple times tables.allocateTables(); tableArray=tables.tables(); } public void load(){ spawnIndexThreads(); if(useWhitelist){ assert(!Whitelist.exists()); Whitelist.initialize(tableArray); } } /** Spawn index threads */ private void spawnIndexThreads(){ //Do anything necessary prior to processing //Determine how many threads may be used final int threads=Shared.threads(); ArrayList alht=new ArrayList(threads); AtomicInteger ai=new AtomicInteger(0); AtomicLong totalKeys=new AtomicLong(0); AtomicLong uniqueKeys=new AtomicLong(0); for(int i=0; i=minFakeID){return false;} taxID=taxtree.getIdAtLevelExtended(taxID, taxLevelExtended); return taxSet.add(taxID); } public SketchResults getSketchesList(final Sketch a, DisplayParams params){ final int minHits=params.minHits, contamLevel=params.contamLevel(); final boolean countContamHits=params.needContamCounts();//, metaFilter=params.hasMetaFilters(), taxFilter=params.hasTaxFilters(); final Timer t=(printTime ? new Timer() : null); final int[] singleton=new int[1]; final IntList idList=new IntList(Tools.min(targetSketchSize, indexLimit, 1000)); AbstractBitSet abs=a.indexBitSet(); assert((abs==null)!=countContamHits); final IntHashSetList taxSet; final int[][] taxHits; if(contamLevel>=0){ taxSet=new IntHashSetList(31); taxHits=new int[a.length()][]; assert(taxtree!=null) : "A TaxTree is required for this operation."; }else{ taxSet=null; taxHits=null; } for(int i=0; i=0){ int incr=0; for(int id : ids){ if(id>=0){ final int trueID=id-1;//Minimum id is 1, indicating sketch 0. idList.add(trueID);//Minimum id is 1, indicating sketch 0. incr++; if(taxSet!=null){addToTaxSet(trueID, taxSet, contamLevel);} } } if(countContamHits && incr>0){abs.increment(i, incr);} if(taxSet!=null && taxSet.size()>0){ taxHits[i]=taxSet.toArray(); taxSet.clear(); } } } // assert(abs!=null); // assert(false) : abs.cardinality(); if(printTime){ t.stop("\nTime for searching index: \t"); t.start(); } // System.err.println("idList.size:"+idList.size); if(idList.size list=new ArrayList(Tools.min(8, idList.size)); int last=-1; int hits=0; for(int i=0; i-1 && (hits>=minHits)){ final Sketch ref=refSketches.get(last); list.add(ref); } last=id; hits=0; } } if(last>-1 && (hits>=minHits)){ final Sketch ref=refSketches.get(last); // if((!metaFilter || ref.passesMeta(params)) && (!taxFilter || params.passesFilter(ref))){list.add(ref);} list.add(ref); } if(printTime){ t.stop("Time for fetching sketches: \t"); } return list.isEmpty() ? new SketchResults(a) : new SketchResults(a, list, taxHits); } // static ThreadLocal intMapHolder=new ThreadLocal(); public final int[] getSketchIdsMap(long key, int[] singleton){ AbstractKmerTable set=tableArray[(int)(key%WAYS)]; final int[] ids=set.getValues(key, singleton); return ids; } public SketchResults getSketchesMap(final Sketch a, DisplayParams params){ final int minHits=params.minHits, contamLevel=params.contamLevel(); final boolean countContamHits=params.needContamCounts();//, metaFilter=params.hasMetaFilters(), taxFilter=params.hasTaxFilters(); final Timer t=(printTime ? new Timer() : null); final int[] singleton=new int[1]; final IntHashMap idMap=new IntHashMap(Tools.min(targetSketchSize, indexLimit, intMapSize), 0.7f); AbstractBitSet abs=a.indexBitSet(); assert((abs==null)!=countContamHits); final IntHashSetList taxSet; final int[][] taxHits; if(contamLevel>=0){ taxSet=new IntHashSetList(31); taxHits=new int[a.length()][]; assert(taxtree!=null) : "A TaxTree is required for this operation."; }else{ taxSet=null; taxHits=null; } // assert(false) : (taxHits==null)+", "+contamLevel; if(printTime){ t.stop("\nTime for allocation: \t"); t.start(); } int[] refHitCounts; if(params.printRefHits){ refHitCounts=new int[a.keys.length]; a.setRefHitCounts(refHitCounts); }else{refHitCounts=null;} for(int i=0; i=0){ int incr=0; for(int id : ids){ if(id>=0){ final int trueID=id-1;//Minimum id is 1, indicating sketch 0. idMap.increment(trueID); if(!allToAll || compareSelf){incr++;} if(taxSet!=null){addToTaxSet(trueID, taxSet, contamLevel);} if(refHitCounts!=null && trueID!=a.sketchID){refHitCounts[i]++;} } } if(countContamHits && incr>0){abs.increment(i, incr);} if(taxSet!=null && taxSet.size()>0){ taxHits[i]=taxSet.toArray(); taxSet.clear(); } } } if(printTime){ t.stop("Time for searching index: \t"); System.err.println("Size: \t"+idMap.size()); t.start(); } // System.err.println("idList.size:"+idList.size); final int size=idMap.size(); if(size==0){return new SketchResults(a);}//null breaks some things ArrayList list=new ArrayList(Tools.min(8, size)); final int[] keys=idMap.keys(); final int[] values=idMap.values(); for(int i=0; i=minHits){ int id=keys[i]; final Sketch ref=refSketches.get(id); // if((!metaFilter || ref.passesMeta(params)) && (!taxFilter || params.passesFilter(ref))){list.add(ref);} list.add(ref); } } if(printTime){ t.stop("Time for fetching sketches: \t"); } return list.isEmpty() ? new SketchResults(a) : new SketchResults(a, list, taxHits); } /*--------------------------------------------------------------*/ public class IndexThread extends Thread { public IndexThread(AtomicInteger nextIndex_, AtomicLong keyCount_, AtomicLong uniqueKeyCount_){ buffer=new HashBuffer(tableArray, 1000, 31, true, false); nextIndex=nextIndex_; keyCount=keyCount_; uniqueKeyCount=uniqueKeyCount_; } @Override public void run(){ // System.err.println("Thread running."); int id=nextIndex.getAndIncrement(); final int numSketches=refSketches.size(); final int limit0=Tools.min((AUTOSIZE || AUTOSIZE_LINEAR ? Integer.MAX_VALUE : targetSketchSize), indexLimit); // System.err.println("numSketches="+numSketches); while(id