| 1 |
package sketch; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collections; |
| 5 |
import java.util.Map.Entry; |
| 6 |
import java.util.concurrent.ConcurrentHashMap; |
| 7 |
|
| 8 |
import fileIO.TextStreamWriter; |
| 9 |
import json.JsonObject; |
| 10 |
import shared.Shared; |
| 11 |
import shared.Tools; |
| 12 |
import structures.ByteBuilder; |
| 13 |
import structures.IntHashMap; |
| 14 |
import tax.TaxTree; |
| 15 |
|
| 16 |
public class SketchResults extends SketchObject { |
| 17 |
|
| 18 |
SketchResults(Sketch s){ |
| 19 |
sketch=s; |
| 20 |
} |
| 21 |
|
| 22 |
SketchResults(Sketch s, ArrayList<Sketch> sketchList_, int[][] taxHits_){ |
| 23 |
sketch=s; |
| 24 |
refSketchList=sketchList_; |
| 25 |
taxHits=taxHits_; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public void addMap(ConcurrentHashMap<Integer, Comparison> map, DisplayParams params, CompareBuffer buffer) { |
| 33 |
|
| 34 |
if(map.isEmpty()){return;} |
| 35 |
list=addToList(map, params, list); |
| 36 |
|
| 37 |
if((true || params.needContamCounts())){ |
| 38 |
recompare(buffer, params); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public void recompare(CompareBuffer buffer, DisplayParams params){ |
| 43 |
|
| 44 |
|
| 45 |
assert(!sketch.merged()); |
| 46 |
sketch.mergeBitSets(); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
for(Comparison c : list){ |
| 52 |
c.recompare(buffer, taxHits, params.contamLevel()); |
| 53 |
} |
| 54 |
Collections.sort(list, params.comparator); |
| 55 |
Collections.reverse(list); |
| 56 |
} |
| 57 |
|
| 58 |
private static ArrayList<Comparison> addToList(ConcurrentHashMap<Integer, Comparison> map, DisplayParams params, ArrayList<Comparison> old){ |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
final int size=map.size(); |
| 67 |
ArrayList<Comparison> al=(old==null ? new ArrayList<Comparison>(size) : old); |
| 68 |
for(Entry<Integer, Comparison> e : map.entrySet()){ |
| 69 |
final Comparison c=e.getValue(); |
| 70 |
al.add(c); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
} |
| 75 |
Shared.sort(al, params.comparator); |
| 76 |
Collections.reverse(al); |
| 77 |
|
| 78 |
|
| 79 |
if(params.recordsPerLevel>0 && al.size()>params.recordsPerLevel && al.get(0).hasQueryTaxID()){ |
| 80 |
int[] count=new int[TaxTree.numTaxLevelNamesExtended]; |
| 81 |
int removed=0; |
| 82 |
for(int i=0; i<al.size(); i++){ |
| 83 |
Comparison c=al.get(i); |
| 84 |
int calevel=c.commonAncestorLevelInt(); |
| 85 |
count[calevel]++; |
| 86 |
if(count[calevel]>params.recordsPerLevel){ |
| 87 |
al.set(i, null); |
| 88 |
removed++; |
| 89 |
} |
| 90 |
} |
| 91 |
if(removed>0){Tools.condenseStrict(al);} |
| 92 |
} |
| 93 |
|
| 94 |
final long limit=(params.maxRecords*2+5); |
| 95 |
while(al.size()>limit){ |
| 96 |
al.remove(al.size()-1); |
| 97 |
} |
| 98 |
return al; |
| 99 |
} |
| 100 |
|
| 101 |
public boolean isEmpty(){ |
| 102 |
return list==null || list.isEmpty(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public int primaryTax(int level){ |
| 110 |
|
| 111 |
IntHashMap map=new IntHashMap(); |
| 112 |
assert(false); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
private static String recordBreak="\n"; |
| 121 |
|
| 122 |
void writeResults(DisplayParams params, TextStreamWriter tsw){ |
| 123 |
ByteBuilder sb=toText(params); |
| 124 |
tsw.print(sb); |
| 125 |
} |
| 126 |
|
| 127 |
public ByteBuilder toText(DisplayParams params){ |
| 128 |
assert(params.postParsed); |
| 129 |
if(sketch.hasSSU()){ |
| 130 |
if(params.comparator==Comparison.SSUComparator){ |
| 131 |
alignSSUs(params.maxRecords*4); |
| 132 |
list.sort(params.comparator); |
| 133 |
Collections.reverse(list); |
| 134 |
}else if(params.printSSU()){ |
| 135 |
alignSSUs(params.maxRecords); |
| 136 |
} |
| 137 |
} |
| 138 |
if(params.json()){ |
| 139 |
JsonObject j=params.toJson(this); |
| 140 |
return j.toText(); |
| 141 |
} |
| 142 |
final ByteBuilder sb=params.queryHeader(sketch); |
| 143 |
if(params.format==DisplayParams.FORMAT_QUERY_REF_ANI || params.format==DisplayParams.FORMAT_CONSTELLATION){ |
| 144 |
if(list==null || list.isEmpty()){return sb;} |
| 145 |
int idx=0; |
| 146 |
int prevTaxID=0; |
| 147 |
for(Comparison c : list){ |
| 148 |
assert(!params.printSSU() || !c.needsAlignment()); |
| 149 |
params.formatComparison(c, sb, prevTaxID); |
| 150 |
prevTaxID=c.taxID(); |
| 151 |
idx++; |
| 152 |
if(idx>=params.maxRecords){break;} |
| 153 |
} |
| 154 |
}else{ |
| 155 |
sb.append(recordBreak); |
| 156 |
|
| 157 |
if(list==null || list.isEmpty()){ |
| 158 |
sb.append("No hits.\n"); |
| 159 |
}else{ |
| 160 |
if(params.format==DisplayParams.FORMAT_MULTICOLUMN){sb.append(params.header()).append('\n');} |
| 161 |
int idx=0; |
| 162 |
int prevTaxID=0; |
| 163 |
for(Comparison c : list){ |
| 164 |
params.formatComparison(c, sb, prevTaxID); |
| 165 |
prevTaxID=c.taxID(); |
| 166 |
idx++; |
| 167 |
if(idx>=params.maxRecords){break;} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
return sb; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
void alignSSUs(int maxRecords){ |
| 179 |
if(!sketch.hasSSU()){return;} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
assert(alignerPool!=null); |
| 189 |
alignerPool.addJobs(list, maxRecords); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public final Sketch sketch; |
| 197 |
public ArrayList<Sketch> refSketchList; |
| 198 |
public int[][] taxHits; |
| 199 |
public ArrayList<Comparison> list; |
| 200 |
public int totalRecords=0; |
| 201 |
|
| 202 |
} |