jpayne@68: package sketch; jpayne@68: jpayne@68: import java.util.ArrayList; jpayne@68: import java.util.Collections; jpayne@68: import java.util.Map.Entry; jpayne@68: import java.util.concurrent.ConcurrentHashMap; jpayne@68: jpayne@68: import fileIO.TextStreamWriter; jpayne@68: import json.JsonObject; jpayne@68: import shared.Shared; jpayne@68: import shared.Tools; jpayne@68: import structures.ByteBuilder; jpayne@68: import structures.IntHashMap; jpayne@68: import tax.TaxTree; jpayne@68: jpayne@68: public class SketchResults extends SketchObject { jpayne@68: jpayne@68: SketchResults(Sketch s){ jpayne@68: sketch=s; jpayne@68: } jpayne@68: jpayne@68: SketchResults(Sketch s, ArrayList sketchList_, int[][] taxHits_){ jpayne@68: sketch=s; jpayne@68: refSketchList=sketchList_; jpayne@68: taxHits=taxHits_; jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Methods ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: public void addMap(ConcurrentHashMap map, DisplayParams params, CompareBuffer buffer) { jpayne@68: jpayne@68: if(map.isEmpty()){return;} jpayne@68: list=addToList(map, params, list); jpayne@68: jpayne@68: if((true || params.needContamCounts())){ jpayne@68: recompare(buffer, params); jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: public void recompare(CompareBuffer buffer, DisplayParams params){ jpayne@68: // assert(makeIndex || !AUTOSIZE); jpayne@68: jpayne@68: assert(!sketch.merged()); jpayne@68: sketch.mergeBitSets(); jpayne@68: jpayne@68: // System.err.println(sketch.compareBitSet()); jpayne@68: // assert(false) : sketch.compareBitSet().getClass(); jpayne@68: jpayne@68: for(Comparison c : list){ jpayne@68: c.recompare(buffer, taxHits, params.contamLevel()); jpayne@68: } jpayne@68: Collections.sort(list, params.comparator); jpayne@68: Collections.reverse(list); jpayne@68: } jpayne@68: jpayne@68: private static ArrayList addToList(ConcurrentHashMap map, DisplayParams params, ArrayList old){ jpayne@68: jpayne@68: // System.err.println(map.size()); jpayne@68: // System.err.println(map.keySet()); jpayne@68: jpayne@68: // final TaxFilter white=params.taxFilterWhite; jpayne@68: // final TaxFilter black=params.taxFilterBlack; jpayne@68: // final boolean noFilter=(white==null && black==null); jpayne@68: final int size=map.size(); jpayne@68: ArrayList al=(old==null ? new ArrayList(size) : old); jpayne@68: for(Entry e : map.entrySet()){ jpayne@68: final Comparison c=e.getValue(); jpayne@68: al.add(c); jpayne@68: // if(noFilter || c.passesFilter(white, black)){ jpayne@68: // al.add(c); jpayne@68: // } jpayne@68: } jpayne@68: Shared.sort(al, params.comparator); jpayne@68: Collections.reverse(al); jpayne@68: jpayne@68: //Apply records per level filter jpayne@68: if(params.recordsPerLevel>0 && al.size()>params.recordsPerLevel && al.get(0).hasQueryTaxID()){ jpayne@68: int[] count=new int[TaxTree.numTaxLevelNamesExtended]; jpayne@68: int removed=0; jpayne@68: for(int i=0; iparams.recordsPerLevel){ jpayne@68: al.set(i, null); jpayne@68: removed++; jpayne@68: } jpayne@68: } jpayne@68: if(removed>0){Tools.condenseStrict(al);} jpayne@68: } jpayne@68: jpayne@68: final long limit=(params.maxRecords*2+5); jpayne@68: while(al.size()>limit){ jpayne@68: al.remove(al.size()-1); jpayne@68: } jpayne@68: return al; jpayne@68: } jpayne@68: jpayne@68: public boolean isEmpty(){ jpayne@68: return list==null || list.isEmpty(); jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Tax Methods ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: public int primaryTax(int level){ jpayne@68: //I have no idea how to implement this... jpayne@68: IntHashMap map=new IntHashMap(); jpayne@68: assert(false); jpayne@68: return -1; jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Print Methods ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: private static String recordBreak="\n"; //"\n\n" jpayne@68: jpayne@68: void writeResults(DisplayParams params, TextStreamWriter tsw){ jpayne@68: ByteBuilder sb=toText(params); jpayne@68: tsw.print(sb); jpayne@68: } jpayne@68: jpayne@68: public ByteBuilder toText(DisplayParams params){ jpayne@68: assert(params.postParsed); jpayne@68: if(sketch.hasSSU()){ jpayne@68: if(params.comparator==Comparison.SSUComparator){ jpayne@68: alignSSUs(params.maxRecords*4);//This should be enough... jpayne@68: list.sort(params.comparator); jpayne@68: Collections.reverse(list); jpayne@68: }else if(params.printSSU()){ jpayne@68: alignSSUs(params.maxRecords); jpayne@68: } jpayne@68: } jpayne@68: if(params.json()){ jpayne@68: JsonObject j=params.toJson(this); jpayne@68: return j.toText(); jpayne@68: } jpayne@68: final ByteBuilder sb=params.queryHeader(sketch); jpayne@68: if(params.format==DisplayParams.FORMAT_QUERY_REF_ANI || params.format==DisplayParams.FORMAT_CONSTELLATION){ jpayne@68: if(list==null || list.isEmpty()){return sb;} jpayne@68: int idx=0; jpayne@68: int prevTaxID=0; jpayne@68: for(Comparison c : list){ jpayne@68: assert(!params.printSSU() || !c.needsAlignment()); jpayne@68: params.formatComparison(c, sb, prevTaxID); jpayne@68: prevTaxID=c.taxID(); jpayne@68: idx++; jpayne@68: if(idx>=params.maxRecords){break;} jpayne@68: } jpayne@68: }else{ jpayne@68: sb.append(recordBreak); jpayne@68: jpayne@68: if(list==null || list.isEmpty()){ jpayne@68: sb.append("No hits.\n"); jpayne@68: }else{ jpayne@68: if(params.format==DisplayParams.FORMAT_MULTICOLUMN){sb.append(params.header()).append('\n');} jpayne@68: int idx=0; jpayne@68: int prevTaxID=0; jpayne@68: for(Comparison c : list){ jpayne@68: params.formatComparison(c, sb, prevTaxID); jpayne@68: prevTaxID=c.taxID(); jpayne@68: idx++; jpayne@68: if(idx>=params.maxRecords){break;} jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: return sb; jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Alignment ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: void alignSSUs(int maxRecords){ jpayne@68: if(!sketch.hasSSU()){return;} jpayne@68: // if(list!=null && list.size()>0){ jpayne@68: // int idx=0; jpayne@68: // for(Comparison c : list){ jpayne@68: // c.ssuIdentity(); jpayne@68: // idx++; jpayne@68: // if(idx>=maxRecords){break;} jpayne@68: // } jpayne@68: // } jpayne@68: assert(alignerPool!=null); jpayne@68: alignerPool.addJobs(list, maxRecords); jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Fields ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: public final Sketch sketch; jpayne@68: public ArrayList refSketchList; jpayne@68: public int[][] taxHits; jpayne@68: public ArrayList list; jpayne@68: public int totalRecords=0; jpayne@68: jpayne@68: }