| 1 |
package sketch; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import shared.KillSwitch; |
| 7 |
import shared.Tools; |
| 8 |
import structures.LongHashMap; |
| 9 |
import structures.LongHeap; |
| 10 |
import structures.LongHeapMap; |
| 11 |
import structures.LongHeapSet; |
| 12 |
import structures.LongHeapSetInterface; |
| 13 |
|
| 14 |
public class SketchHeap { |
| 15 |
|
| 16 |
SketchHeap(int limit, int minKeyOccuranceCount_, boolean trackCounts){ |
| 17 |
minKeyOccuranceCount=minKeyOccuranceCount_; |
| 18 |
setMode=minKeyOccuranceCount<2 && !trackCounts; |
| 19 |
if(setMode){ |
| 20 |
setOrMap=set=new LongHeapSet(limit); |
| 21 |
map=null; |
| 22 |
heap=set.heap; |
| 23 |
}else{ |
| 24 |
if(minKeyOccuranceCount>1){limit=(int)Tools.min(10000000, limit*SketchObject.sketchHeapFactor);} |
| 25 |
setOrMap=map=new LongHeapMap(limit); |
| 26 |
set=null; |
| 27 |
heap=map.heap; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public void clear(boolean clearFname){ |
| 32 |
taxID=-1; |
| 33 |
imgID=-1; |
| 34 |
genomeSizeBases=0; |
| 35 |
genomeSizeKmers=0; |
| 36 |
genomeSequences=0; |
| 37 |
if(baseCounts!=null){Arrays.fill(baseCounts, 0);} |
| 38 |
r16S=null; |
| 39 |
r18S=null; |
| 40 |
probSum=0; |
| 41 |
taxName=null; |
| 42 |
name0=null; |
| 43 |
if(clearFname){fname=null;} |
| 44 |
setOrMap.clear(); |
| 45 |
} |
| 46 |
|
| 47 |
public void add(SketchHeap b){ |
| 48 |
if(taxID<0){taxID=b.taxID;} |
| 49 |
if(imgID<0){imgID=b.imgID;} |
| 50 |
if(taxName==null){taxName=b.taxName;} |
| 51 |
if(name0==null){name0=b.name0;} |
| 52 |
if(fname==null){fname=b.fname;} |
| 53 |
genomeSizeBases+=b.genomeSizeBases; |
| 54 |
genomeSizeKmers+=b.genomeSizeKmers; |
| 55 |
genomeSequences+=b.genomeSequences; |
| 56 |
if(baseCounts!=null){Tools.add(baseCounts, b.baseCounts);} |
| 57 |
set16S(b.r16S); |
| 58 |
set18S(b.r18S); |
| 59 |
probSum+=b.probSum; |
| 60 |
if(setMode){ |
| 61 |
set.add(b.set); |
| 62 |
}else{ |
| 63 |
map.add(b.map); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public void add(Sketch b){ |
| 68 |
if(taxID<0){taxID=b.taxID;} |
| 69 |
if(imgID<0){imgID=b.imgID;} |
| 70 |
if(taxName==null){taxName=b.taxName();} |
| 71 |
if(name0==null){name0=b.name0();} |
| 72 |
if(fname==null){fname=b.fname();} |
| 73 |
genomeSizeBases+=b.genomeSizeBases; |
| 74 |
genomeSizeKmers+=b.genomeSizeKmers; |
| 75 |
genomeSequences+=b.genomeSequences; |
| 76 |
if(baseCounts!=null && b.baseCounts!=null){Tools.add(baseCounts, b.baseCounts);} |
| 77 |
set16S(b.r16S()); |
| 78 |
set18S(b.r18S()); |
| 79 |
|
| 80 |
long[] keys=b.keys; |
| 81 |
int[] counts=b.keyCounts; |
| 82 |
assert(keys.length==b.length()) : keys.length+", "+b.length(); |
| 83 |
for(int i=0; i<keys.length; i++){ |
| 84 |
long key=Long.MAX_VALUE-keys[i]; |
| 85 |
int count=(counts==null ? 1 : counts[i]); |
| 86 |
assert((key>=SketchObject.minHashValue)==(count>0)); |
| 87 |
increment(key, count); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public StringBuilder toHeader(){ |
| 92 |
StringBuilder sb=new StringBuilder(); |
| 93 |
sb.append("#SZ:"+setOrMap.size()); |
| 94 |
|
| 95 |
sb.append("\tCD:"); |
| 96 |
sb.append(SketchObject.codingArray[SketchObject.CODING]); |
| 97 |
if(SketchObject.deltaOut){sb.append('D');} |
| 98 |
if(SketchObject.aminoOrTranslate()){sb.append('M');} |
| 99 |
if(SketchObject.amino8){sb.append('8');} |
| 100 |
|
| 101 |
sb.append("\tK:").append(SketchObject.k); |
| 102 |
if(SketchObject.k2>0){sb.append(",").append(SketchObject.k2);} |
| 103 |
if(SketchObject.HASH_VERSION>1){sb.append("\tH:").append(SketchObject.HASH_VERSION);} |
| 104 |
|
| 105 |
if(genomeSizeBases>0){sb.append("\tGS:"+genomeSizeBases);} |
| 106 |
if(genomeSizeKmers>0){sb.append("\tGK:"+genomeSizeKmers);} |
| 107 |
final long ge=genomeSizeEstimate(); |
| 108 |
if(ge>0){sb.append("\tGE:").append(ge);} |
| 109 |
if(genomeSequences>0){sb.append("\tGQ:"+genomeSequences);} |
| 110 |
if(baseCounts!=null && !SketchObject.aminoOrTranslate()){ |
| 111 |
sb.append("\tBC:").append(baseCounts[0]).append(',').append(baseCounts[1]).append(','); |
| 112 |
sb.append(baseCounts[2]).append(',').append(baseCounts[3]); |
| 113 |
} |
| 114 |
if(probSum>0){sb.append("\tPC:"+Tools.format("%.4f",probSum/genomeSizeKmers));} |
| 115 |
if(taxID>=0){sb.append("\tID:"+taxID);} |
| 116 |
if(imgID>=0){sb.append("\tIMG:"+imgID);} |
| 117 |
if(taxName!=null){sb.append("\tNM:"+taxName);} |
| 118 |
if(name0!=null){sb.append("\tNM0:"+name0);} |
| 119 |
if(fname!=null){sb.append("\tFN:"+fname);} |
| 120 |
|
| 121 |
if(r16S!=null){sb.append("\t16S:"+r16S.length);} |
| 122 |
if(r18S!=null){sb.append("\t18S:"+r18S.length);} |
| 123 |
if(r16S!=null){ |
| 124 |
sb.append('\n').append("#16S:"); |
| 125 |
for(byte b : r16S){sb.append((char)b);} |
| 126 |
} |
| 127 |
if(r18S!=null){ |
| 128 |
sb.append('\n').append("#18S:"); |
| 129 |
for(byte b : r18S){sb.append((char)b);} |
| 130 |
} |
| 131 |
return sb; |
| 132 |
} |
| 133 |
|
| 134 |
public boolean checkAndAdd(long value){ |
| 135 |
assert(value>=SketchObject.minHashValue); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if(Blacklist.exists() || Whitelist.exists()){ |
| 142 |
if(!heap.hasRoom() && value<=heap.peek()){return false;} |
| 143 |
if(Blacklist.contains(value)){return false;} |
| 144 |
if(!Whitelist.containsRaw(value)){return false;} |
| 145 |
} |
| 146 |
|
| 147 |
return add(value); |
| 148 |
} |
| 149 |
|
| 150 |
public final int maxLen(){ |
| 151 |
return SketchObject.toSketchSize(genomeSizeBases, genomeSizeKmers, genomeSizeEstimate(), SketchObject.targetSketchSize); |
| 152 |
} |
| 153 |
|
| 154 |
public final long[] toSketchArray(){ |
| 155 |
int maxLen=maxLen(); |
| 156 |
return toSketchArray(maxLen, minKeyOccuranceCount); |
| 157 |
} |
| 158 |
|
| 159 |
public final long[] toSketchArray_minCount(int minKeyOccuranceCount_){ |
| 160 |
int maxLen=maxLen(); |
| 161 |
return toSketchArray(maxLen, minKeyOccuranceCount_); |
| 162 |
} |
| 163 |
|
| 164 |
final long[] toSketchArray_maxLen(int maxLen){ |
| 165 |
return toSketchArray(maxLen, minKeyOccuranceCount); |
| 166 |
} |
| 167 |
|
| 168 |
private final long[] toSketchArrayOld(int maxLen){ |
| 169 |
final int initial=heap.size(); |
| 170 |
final int len=Tools.min(maxLen, initial); |
| 171 |
final long[] array=KillSwitch.allocLong1D(len); |
| 172 |
|
| 173 |
int toSkip=heap.size()-len; |
| 174 |
for(int i=0; i<toSkip; i++){heap.poll();} |
| 175 |
for(int i=0; i<len; i++){ |
| 176 |
array[i]=Long.MAX_VALUE-heap.poll(); |
| 177 |
} |
| 178 |
Tools.reverseInPlace(array); |
| 179 |
assert(heap.size()==0) : heap.size()+", "+len+", "+maxLen+", "+initial; |
| 180 |
return array; |
| 181 |
} |
| 182 |
|
| 183 |
private final long[] toSketchArray(int maxLen, int minKeyOccuranceCount_){ |
| 184 |
if(minKeyOccuranceCount_<0){minKeyOccuranceCount_=minKeyOccuranceCount;} |
| 185 |
if(setMode){return toSketchArrayOld(maxLen);} |
| 186 |
long[] keys=map().toArray(minKeyOccuranceCount_); |
| 187 |
for(int i=0; i<keys.length; i++){ |
| 188 |
|
| 189 |
keys[i]=Long.MAX_VALUE-keys[i]; |
| 190 |
|
| 191 |
} |
| 192 |
Arrays.sort(keys); |
| 193 |
if(keys.length>maxLen){ |
| 194 |
keys=Arrays.copyOf(keys, maxLen); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
return keys; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public int hashCode(){ |
| 205 |
long gSize=genomeSizeKmers>0 ? genomeSizeKmers : genomeSizeBases; |
| 206 |
int code=(int) ((gSize^taxID^imgID^(name0==null ? 0 : name0.hashCode()))&Integer.MAX_VALUE); |
| 207 |
return code; |
| 208 |
} |
| 209 |
|
| 210 |
public long genomeSizeEstimate() { |
| 211 |
int size=size(); |
| 212 |
if(size==0){return 0;} |
| 213 |
long min=peek(); |
| 214 |
long est=Tools.min(genomeSizeKmers, SketchObject.genomeSizeEstimate(Long.MAX_VALUE-min, size)); |
| 215 |
|
| 216 |
return est; |
| 217 |
} |
| 218 |
|
| 219 |
public long genomeSizeEstimate(int minCount) { |
| 220 |
if(minCount<2){return genomeSizeEstimate();} |
| 221 |
if(size()==0){return 0;} |
| 222 |
long[] min=map.map.getMin(minCount); |
| 223 |
if(min[1]==0){return 0;} |
| 224 |
long est=Tools.min(genomeSizeKmers, SketchObject.genomeSizeEstimate(Long.MAX_VALUE-min[0], (int)min[1])); |
| 225 |
return est; |
| 226 |
} |
| 227 |
|
| 228 |
public long sketchSizeEstimate(){ |
| 229 |
return SketchObject.toSketchSize(genomeSizeBases, genomeSizeKmers, genomeSizeEstimate(), SketchObject.targetSketchSize); |
| 230 |
} |
| 231 |
|
| 232 |
public boolean contains(long key) { |
| 233 |
return setOrMap.contains(key); |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public String toString(){return toHeader().toString();} |
| 238 |
|
| 239 |
public String name(){return taxName==null ? name0 : taxName;} |
| 240 |
public String taxName(){return taxName;} |
| 241 |
public String name0(){return name0;} |
| 242 |
public String fname(){return fname;} |
| 243 |
public long[] baseCounts(boolean original){return baseCounts==null ? null : original ? baseCounts : baseCounts.clone();} |
| 244 |
public void setTaxName(String s){taxName=s;} |
| 245 |
public void setName0(String s){name0=s;} |
| 246 |
public void setFname(String s){fname=s;} |
| 247 |
|
| 248 |
public byte[] r16S(){return r16S;} |
| 249 |
public int r16SLen(){return r16S==null ? 0 : r16S.length;} |
| 250 |
public void set16S(byte[] b){ |
| 251 |
if(b==null || b.length<SketchObject.min_SSU_len){return;} |
| 252 |
|
| 253 |
if(r16S==null || score16S(b)>score16S(r16S)){ |
| 254 |
r16S=b; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
private float score16S(byte[] seq){return scoreSSU(seq, 1533);} |
| 259 |
private float score18S(byte[] seq){return scoreSSU(seq, 1858);} |
| 260 |
|
| 261 |
private float scoreSSU(byte[] seq, int idealLen){ |
| 262 |
float lengthScore=lengthScore(seq.length, idealLen); |
| 263 |
float definedScore=(seq.length-AminoAcid.countUndefined(seq))/(float)seq.length; |
| 264 |
return lengthScore*definedScore; |
| 265 |
} |
| 266 |
private float lengthScore(int len, int ideal){return Tools.min(len, ideal)/(float)Tools.max(len, ideal);} |
| 267 |
|
| 268 |
public byte[] r18S(){return r18S;} |
| 269 |
public int r18SLen(){return r18S==null ? 0 : r18S.length;} |
| 270 |
public void set18S(byte[] b){ |
| 271 |
if(b==null || b.length<SketchObject.min_SSU_len){return;} |
| 272 |
if(r18S==null || score18S(b)>score16S(r18S)){ |
| 273 |
r18S=b; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
boolean isEukaryote(){ |
| 278 |
if(taxID<1 || taxID>=SketchObject.minFakeID){return false;} |
| 279 |
if(SketchObject.taxtree==null){return false;} |
| 280 |
return SketchObject.taxtree.isEukaryote((int)taxID); |
| 281 |
} |
| 282 |
|
| 283 |
private String taxName; |
| 284 |
private String name0; |
| 285 |
private String fname; |
| 286 |
public long taxID=-1; |
| 287 |
public long imgID=-1; |
| 288 |
public long genomeSizeBases=0; |
| 289 |
public long genomeSizeKmers=0; |
| 290 |
public long genomeSequences=0; |
| 291 |
public final long[] baseCounts=(SketchObject.aminoOrTranslate() ? null : new long[4]); |
| 292 |
private byte[] r16S; |
| 293 |
private byte[] r18S; |
| 294 |
double probSum=0; |
| 295 |
|
| 296 |
public float probCorrect(){return probSum<=0 ? 0f : (float)(probSum/Tools.max(genomeSizeKmers, 1f));} |
| 297 |
public int capacity(){return heap.capacity();} |
| 298 |
public boolean hasRoom(){return heap.hasRoom();} |
| 299 |
public long peek(){return heap.peek();} |
| 300 |
public int size(){return heap.size();} |
| 301 |
public LongHashMap map(){return map.map;} |
| 302 |
|
| 303 |
public void clear(){ |
| 304 |
setOrMap.clear(); |
| 305 |
} |
| 306 |
public void clearSet(){ |
| 307 |
if(set==null){map.map.clear();} |
| 308 |
else{set.set.clear();} |
| 309 |
} |
| 310 |
public boolean add(long key){return setOrMap.add(key);} |
| 311 |
public int increment(long key, int incr){return setOrMap.increment(key, incr);} |
| 312 |
|
| 313 |
private final LongHeapSet set; |
| 314 |
private final LongHeapMap map; |
| 315 |
private final LongHeapSetInterface setOrMap; |
| 316 |
public final LongHeap heap; |
| 317 |
public final int minKeyOccuranceCount; |
| 318 |
|
| 319 |
public final boolean setMode; |
| 320 |
} |