| 1 |
package sketch; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.Collection; |
| 8 |
import java.util.concurrent.ConcurrentLinkedQueue; |
| 9 |
import java.util.concurrent.atomic.AtomicInteger; |
| 10 |
|
| 11 |
import dna.AminoAcid; |
| 12 |
import fileIO.ByteFile; |
| 13 |
import fileIO.ByteStreamWriter; |
| 14 |
import fileIO.FileFormat; |
| 15 |
import fileIO.ReadWrite; |
| 16 |
import kmer.HashArray1D; |
| 17 |
import kmer.HashForest; |
| 18 |
import kmer.KmerNode; |
| 19 |
import kmer.KmerTableSet; |
| 20 |
import shared.KillSwitch; |
| 21 |
import shared.Parse; |
| 22 |
import shared.Shared; |
| 23 |
import shared.Tools; |
| 24 |
import stream.ConcurrentReadInputStream; |
| 25 |
import stream.Read; |
| 26 |
import structures.ByteBuilder; |
| 27 |
import structures.IntList; |
| 28 |
import structures.ListNum; |
| 29 |
import structures.LongList; |
| 30 |
import structures.StringNum; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public final class SketchTool extends SketchObject { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public SketchTool(int size_, DisplayParams params){ |
| 44 |
this(size_, params.minKeyOccuranceCount, params.trackCounts(), params.mergePairs, SketchObject.rcomp); |
| 45 |
} |
| 46 |
|
| 47 |
public SketchTool(int size_, int minKeyOccuranceCount_, boolean trackCounts_, |
| 48 |
boolean mergePairs_, boolean rcomp_){ |
| 49 |
stTargetSketchSize=size_; |
| 50 |
minKeyOccuranceCount=minKeyOccuranceCount_; |
| 51 |
trackCounts=trackCounts_; |
| 52 |
mergePairs=mergePairs_; |
| 53 |
rcomp=rcomp_; |
| 54 |
|
| 55 |
assert(!aminoOrTranslate() || !rcomp) : "rcomp should be false in amino mode."; |
| 56 |
assert(!aminoOrTranslate() || (k*AminoAcid.AMINO_SHIFT<64)) : "Protein sketches require 1 <= K <= "+(63/AminoAcid.AMINO_SHIFT)+"."; |
| 57 |
assert(k>0 && k<=32) : "Sketches require 1 <= K <= 32."; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public Sketch toSketch(KmerTableSet tables, boolean multithreaded){ |
| 65 |
final int threads=(multithreaded ? Tools.mid(1, Shared.threads(), tables.ways()) : 1); |
| 66 |
return (threads<2 ? toSketch_ST(tables) : toSketch_MT(tables, threads)); |
| 67 |
} |
| 68 |
|
| 69 |
private Sketch toSketch_ST(KmerTableSet tables){ |
| 70 |
SketchHeap heap=(stTargetSketchSize>0 ? new SketchHeap(stTargetSketchSize, minKeyOccuranceCount, trackCounts) : null); |
| 71 |
LongList list=new LongList(); |
| 72 |
|
| 73 |
KmerTableSet kts=(KmerTableSet)tables; |
| 74 |
for(int tnum=0; tnum<kts.ways; tnum++){ |
| 75 |
HashArray1D table=kts.getTable(tnum); |
| 76 |
if(stTargetSketchSize>0){ |
| 77 |
toHeap(table, heap); |
| 78 |
}else{ |
| 79 |
toList(table, list); |
| 80 |
} |
| 81 |
} |
| 82 |
return stTargetSketchSize>0 ? new Sketch(heap, false, trackCounts, null) : toSketch(list); |
| 83 |
} |
| 84 |
|
| 85 |
private Sketch toSketch_MT(KmerTableSet tables, final int threads){ |
| 86 |
ArrayList<SketchThread> alst=new ArrayList<SketchThread>(threads); |
| 87 |
AtomicInteger ai=new AtomicInteger(0); |
| 88 |
for(int i=0; i<threads; i++){ |
| 89 |
alst.add(new SketchThread(ai, tables)); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
for(SketchThread pt : alst){ |
| 94 |
pt.start(); |
| 95 |
} |
| 96 |
|
| 97 |
ArrayList<SketchHeap> heaps=new ArrayList<SketchHeap>(threads); |
| 98 |
LongList list=new LongList(); |
| 99 |
|
| 100 |
for(SketchThread pt : alst){ |
| 101 |
|
| 102 |
|
| 103 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 104 |
try { |
| 105 |
|
| 106 |
pt.join(); |
| 107 |
} catch (InterruptedException e) { |
| 108 |
|
| 109 |
e.printStackTrace(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if(stTargetSketchSize>=0){ |
| 114 |
if(pt.heap!=null && pt.heap.size()>0){ |
| 115 |
heaps.add(pt.heap); |
| 116 |
} |
| 117 |
}else{ |
| 118 |
if(pt.list!=null){list.append(pt.list);} |
| 119 |
pt.list=null; |
| 120 |
} |
| 121 |
} |
| 122 |
alst.clear(); |
| 123 |
|
| 124 |
return stTargetSketchSize>=0 ? toSketch(heaps, true) : toSketch(list); |
| 125 |
} |
| 126 |
|
| 127 |
public SketchHeap toHeap(HashArray1D table, SketchHeap heap){ |
| 128 |
|
| 129 |
long[] kmers=table.array(); |
| 130 |
int[] counts=table.values(); |
| 131 |
for(int i=0; i<table.arrayLength(); i++){ |
| 132 |
int count=counts[i]; |
| 133 |
if(count>=minKeyOccuranceCount){ |
| 134 |
heap.genomeSizeKmers++; |
| 135 |
long kmer=kmers[i]; |
| 136 |
long hashcode=hash(kmer); |
| 137 |
if(hashcode>=minHashValue){ |
| 138 |
heap.add(hashcode); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
HashForest forest=table.victims(); |
| 143 |
if(forest!=null){ |
| 144 |
for(KmerNode kn : forest.array()){ |
| 145 |
if(kn!=null){addRecursive(heap, kn);} |
| 146 |
} |
| 147 |
} |
| 148 |
return heap; |
| 149 |
} |
| 150 |
|
| 151 |
public LongList toList(HashArray1D table, LongList list){ |
| 152 |
|
| 153 |
long[] kmers=table.array(); |
| 154 |
int[] counts=table.values(); |
| 155 |
for(int i=0; i<table.arrayLength(); i++){ |
| 156 |
int count=counts[i]; |
| 157 |
if(count>=minKeyOccuranceCount){ |
| 158 |
long kmer=kmers[i]; |
| 159 |
long hashcode=hash(kmer); |
| 160 |
if(hashcode>=minHashValue){ |
| 161 |
list.add(hashcode); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
HashForest forest=table.victims(); |
| 166 |
if(forest!=null){ |
| 167 |
for(KmerNode kn : forest.array()){ |
| 168 |
if(kn!=null){addRecursive(list, kn);} |
| 169 |
} |
| 170 |
} |
| 171 |
return list; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public Sketch toSketch(ArrayList<SketchHeap> heaps, boolean allowZeroSizeSketch){ |
| 187 |
if(heaps==null || heaps.isEmpty()){ |
| 188 |
if(allowZeroSizeSketch){ |
| 189 |
return new Sketch(new long[0], null, null, null, null, null); |
| 190 |
}else{ |
| 191 |
return null; |
| 192 |
} |
| 193 |
} |
| 194 |
SketchHeap a=heaps.get(0); |
| 195 |
for(int i=1; i<heaps.size(); i++){ |
| 196 |
SketchHeap b=heaps.get(i); |
| 197 |
a.add(b); |
| 198 |
} |
| 199 |
if(verbose2){System.err.println("Creating a sketch of size "+a.size()+".");} |
| 200 |
return new Sketch(a, false, trackCounts, null); |
| 201 |
} |
| 202 |
|
| 203 |
Sketch toSketch(LongList list){ |
| 204 |
list.sort(); |
| 205 |
assert(list.size==0 || list.get(list.size()-1)>=minHashValue) : list.size+", "+list.get(list.size()-1)+", "+minHashValue; |
| 206 |
list.shrinkToUnique(); |
| 207 |
list.reverse(); |
| 208 |
for(int i=0; i<list.size; i++){list.array[i]=Long.MAX_VALUE-list.array[i];} |
| 209 |
return new Sketch(list.toArray(), null, null, null, null, null); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private void addRecursive(SketchHeap heap, KmerNode kn){ |
| 217 |
if(kn==null){return;} |
| 218 |
if(kn.count()>=minKeyOccuranceCount){ |
| 219 |
heap.genomeSizeKmers++; |
| 220 |
long kmer=kn.pivot(); |
| 221 |
long hashcode=hash(kmer); |
| 222 |
if(hashcode>=minHashValue){heap.add(hashcode);} |
| 223 |
} |
| 224 |
if(kn.left()!=null){addRecursive(heap, kn.left());} |
| 225 |
if(kn.right()!=null){addRecursive(heap, kn.right());} |
| 226 |
} |
| 227 |
|
| 228 |
private void addRecursive(LongList list, KmerNode kn){ |
| 229 |
if(kn==null){return;} |
| 230 |
if(kn.count()>=minKeyOccuranceCount){ |
| 231 |
long kmer=kn.pivot(); |
| 232 |
long hashcode=hash(kmer); |
| 233 |
if(hashcode>=minHashValue){list.add(hashcode);} |
| 234 |
} |
| 235 |
if(kn.left()!=null){addRecursive(list, kn.left());} |
| 236 |
if(kn.right()!=null){addRecursive(list, kn.right());} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public ArrayList<Sketch> loadSketches_MT(DisplayParams params, Collection<String> fnames){ |
| 263 |
return loadSketches_MT(params.mode, params.samplerate, params.maxReads, |
| 264 |
params.minEntropy, params.minProb, params.minQual, fnames); |
| 265 |
} |
| 266 |
|
| 267 |
public ArrayList<Sketch> loadSketches_MT(DisplayParams params, String...fnames){ |
| 268 |
return loadSketches_MT(params.mode, params.samplerate, params.maxReads, |
| 269 |
params.minEntropy, params.minProb, params.minQual, fnames); |
| 270 |
} |
| 271 |
|
| 272 |
public ArrayList<Sketch> loadSketches_MT(int mode, float samplerate, long reads, float minEntropy, float minProb, byte minQual, Collection<String> fnames){ |
| 273 |
return loadSketches_MT(mode, samplerate, reads, minEntropy, minProb, minQual, fnames.toArray(new String[0])); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
public ArrayList<Sketch> loadSketches_MT(int mode, float samplerate, long reads, float minEntropy, float minProb, byte minQual, String...fnames){ |
| 278 |
|
| 279 |
ConcurrentLinkedQueue<StringNum> decomposedFnames=new ConcurrentLinkedQueue<StringNum>(); |
| 280 |
int num=0; |
| 281 |
for(String s : fnames){ |
| 282 |
if(s.indexOf(',')<0 || s.startsWith("stdin") || new File(s).exists()){ |
| 283 |
num++; |
| 284 |
decomposedFnames.add(new StringNum(s, num)); |
| 285 |
}else{ |
| 286 |
for(String s2 : s.split(",")){ |
| 287 |
num++; |
| 288 |
decomposedFnames.add(new StringNum(s2, num)); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if(decomposedFnames.size()==0){return null;} |
| 294 |
if(decomposedFnames.size()==1){return loadSketchesFromFile(decomposedFnames.poll().s, null, 0, reads, mode, samplerate, minEntropy, minProb, minQual, false);} |
| 295 |
|
| 296 |
|
| 297 |
final int threads=Tools.min(Shared.threads(), decomposedFnames.size()); |
| 298 |
|
| 299 |
|
| 300 |
ArrayList<LoadThread> allt=new ArrayList<LoadThread>(threads); |
| 301 |
|
| 302 |
for(int i=0; i<threads; i++){ |
| 303 |
allt.add(new LoadThread(decomposedFnames, mode, samplerate, reads, minEntropy, minProb, minQual)); |
| 304 |
} |
| 305 |
|
| 306 |
ArrayList<Sketch> sketches=new ArrayList<Sketch>(); |
| 307 |
|
| 308 |
|
| 309 |
for(LoadThread lt : allt){lt.start();} |
| 310 |
|
| 311 |
|
| 312 |
boolean success=true; |
| 313 |
for(LoadThread lt : allt){ |
| 314 |
|
| 315 |
|
| 316 |
while(lt.getState()!=Thread.State.TERMINATED){ |
| 317 |
try { |
| 318 |
|
| 319 |
lt.join(); |
| 320 |
} catch (InterruptedException e) { |
| 321 |
|
| 322 |
e.printStackTrace(); |
| 323 |
} |
| 324 |
} |
| 325 |
sketches.addAll(lt.list); |
| 326 |
success&=lt.success; |
| 327 |
} |
| 328 |
assert(success) : "Failure loading some files."; |
| 329 |
return sketches; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public ArrayList<Sketch> loadSketchesFromFile(final String fname0, SketchMakerMini smm, |
| 335 |
int maxThreads, long reads, int mode, DisplayParams params, boolean allowZeroSizeSketch){ |
| 336 |
return loadSketchesFromFile(fname0, smm, maxThreads, reads, mode, |
| 337 |
params.samplerate, params.minEntropy, params.minProb, params.minQual, allowZeroSizeSketch); |
| 338 |
} |
| 339 |
|
| 340 |
public ArrayList<Sketch> loadSketchesFromFile(final String fname0, SketchMakerMini smm, |
| 341 |
int maxThreads, long reads, int mode, float samplerate, float minEntropy, float minProb, byte minQual, boolean allowZeroSizeSketch){ |
| 342 |
assert(fname0!=null); |
| 343 |
if(fname0==null){return null;} |
| 344 |
FileFormat ff=FileFormat.testInput(fname0, FileFormat.FASTA, null, false, true); |
| 345 |
if(ff.isSequence()){ |
| 346 |
return loadSketchesFromSequenceFile(ff, smm, maxThreads, reads, mode, samplerate, minEntropy, minProb, minQual, allowZeroSizeSketch); |
| 347 |
}else{ |
| 348 |
return SketchObject.LOADER2 ? loadSketchesFromSketchFile2(ff, allowZeroSizeSketch) : loadSketchesFromSketchFile(ff, allowZeroSizeSketch); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
private ArrayList<Sketch> loadSketchesFromSequenceFile(final FileFormat ff, SketchMakerMini smm, |
| 353 |
int maxThreads, long reads, int mode, float samplerate, float minEntropy, float minProb, byte minQual, boolean allowZeroSizeSketch){ |
| 354 |
maxThreads=(maxThreads<1 ? Shared.threads() : Tools.min(maxThreads, Shared.threads())); |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if(ff.fastq() && allowMultithreadedFastq && !forceDisableMultithreadedFastq && (mode==ONE_SKETCH || mode==PER_FILE) && |
| 360 |
maxThreads>1 && Shared.threads()>2 && (reads<1 || reads*samplerate*(mergePairs ? 2 : 1)>=1000)){ |
| 361 |
|
| 362 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 363 |
Read.VALIDATE_IN_CONSTRUCTOR=false; |
| 364 |
|
| 365 |
if(verbose2){System.err.println("Loading a sketch multithreaded.");} |
| 366 |
Sketch sketch=processReadsMT(ff.name(), maxThreads, reads, mode, samplerate, minEntropy, minProb, minQual, allowZeroSizeSketch); |
| 367 |
|
| 368 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 369 |
|
| 370 |
ArrayList<Sketch> list=new ArrayList<Sketch>(1); |
| 371 |
if(sketch!=null && (sketch.length()>0 || allowZeroSizeSketch)){ |
| 372 |
sketch.loadSSU(); |
| 373 |
list.add(sketch); |
| 374 |
} |
| 375 |
return list; |
| 376 |
} |
| 377 |
if(smm==null){smm=new SketchMakerMini(this, mode, minEntropy, minProb, minQual);} |
| 378 |
if(verbose2){System.err.println("Loading sketches via SMM.");} |
| 379 |
ArrayList<Sketch> sketches=smm.toSketches(ff.name(), samplerate, reads); |
| 380 |
if(verbose2){System.err.println("Loaded "+(sketches==null ? 0 : sketches.size())+" sketches via SMM.");} |
| 381 |
return sketches; |
| 382 |
} |
| 383 |
|
| 384 |
private ArrayList<Sketch> loadSketchesFromSketchFile(final FileFormat ff, boolean allowZeroSizeSketch){ |
| 385 |
|
| 386 |
boolean A48=(Sketch.CODING==Sketch.A48), HEX=(Sketch.CODING==Sketch.HEX), NUC=false, delta=true, counts=false, unsorted=false; |
| 387 |
|
| 388 |
if(verbose2){System.err.println("Loading sketches from text.");} |
| 389 |
ArrayList<Sketch> sketches=new ArrayList<Sketch>(); |
| 390 |
ByteFile bf=ByteFile.makeByteFile(ff); |
| 391 |
int currentSketchSize=stTargetSketchSize; |
| 392 |
int taxID=-1; |
| 393 |
long spid=-1; |
| 394 |
long imgID=-1; |
| 395 |
long genomeSizeBases=0, genomeSizeKmers=0, genomeSequences=0; |
| 396 |
long[] baseCounts=null; |
| 397 |
byte[] r16S=null; |
| 398 |
byte[] r18S=null; |
| 399 |
int r16SLen=0; |
| 400 |
int r18SLen=0; |
| 401 |
float probCorrect=-1; |
| 402 |
String name=null, name0=null, fname=ff.simpleName(); |
| 403 |
LongList list=null; |
| 404 |
IntList countList=null; |
| 405 |
ArrayList<String> meta=null; |
| 406 |
long sum=0; |
| 407 |
byte[] lastHeader=null; |
| 408 |
|
| 409 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ |
| 410 |
if(line.length>0){ |
| 411 |
|
| 412 |
if(line[0]=='#'){ |
| 413 |
if(r16SLen>0 || r18SLen>0){ |
| 414 |
byte[] ssu=KillSwitch.copyOfRange(line, 5, line.length); |
| 415 |
if(Tools.startsWith(line, "#16S:") || Tools.startsWith(line, "#SSU:")){ |
| 416 |
assert(r16SLen>0); |
| 417 |
assert(ssu.length==r16SLen) : r16SLen+", "+line.length+"\n"+new String(line)+"\n"; |
| 418 |
r16S=ssu; |
| 419 |
r16SLen=0; |
| 420 |
}else if(Tools.startsWith(line, "#18S:")){ |
| 421 |
assert(r18SLen>0); |
| 422 |
assert(ssu.length==r18SLen) : r18SLen+", "+line.length+"\n"+new String(line)+"\n"; |
| 423 |
r18S=ssu; |
| 424 |
r18SLen=0; |
| 425 |
}else{ |
| 426 |
assert(false) : new String(line); |
| 427 |
} |
| 428 |
}else{ |
| 429 |
lastHeader=line; |
| 430 |
if(list!=null){ |
| 431 |
assert(list.size==list.array.length); |
| 432 |
if(NUC || unsorted){ |
| 433 |
list.sort(); |
| 434 |
list.shrinkToUnique(); |
| 435 |
}else{ |
| 436 |
list.shrink(); |
| 437 |
} |
| 438 |
if(list.size()>0 || allowZeroSizeSketch){ |
| 439 |
int[] keyCounts=countList==null ? null : countList.array; |
| 440 |
Sketch sketch=new Sketch(list.array, keyCounts, baseCounts, r16S, r18S, taxID, imgID, |
| 441 |
genomeSizeBases, genomeSizeKmers, genomeSequences, probCorrect, name, name0, fname, meta); |
| 442 |
sketch.spid=spid; |
| 443 |
sketches.add(sketch); |
| 444 |
} |
| 445 |
|
| 446 |
} |
| 447 |
name=name0=null; |
| 448 |
fname=ff.simpleName(); |
| 449 |
list=null; |
| 450 |
countList=null; |
| 451 |
meta=null; |
| 452 |
baseCounts=null; |
| 453 |
r16S=null; |
| 454 |
r18S=null; |
| 455 |
r16SLen=0; |
| 456 |
r18SLen=0; |
| 457 |
sum=0; |
| 458 |
taxID=-1; |
| 459 |
imgID=-1; |
| 460 |
genomeSizeBases=0; |
| 461 |
genomeSizeKmers=0; |
| 462 |
genomeSequences=0; |
| 463 |
probCorrect=-1; |
| 464 |
int k_sketch=defaultK; |
| 465 |
int k2_sketch=0; |
| 466 |
int hashVersion_sketch=1; |
| 467 |
|
| 468 |
if(line.length>1){ |
| 469 |
String[] split=new String(line, 1, line.length-1).split("\t"); |
| 470 |
for(String s : split){ |
| 471 |
final int colon=s.indexOf(':'); |
| 472 |
final String sub=s.substring(colon+1); |
| 473 |
if(s.startsWith("SZ:") || s.startsWith("SIZE:")){ |
| 474 |
currentSketchSize=Integer.parseInt(sub); |
| 475 |
}else if(s.startsWith("CD:")){ |
| 476 |
A48=HEX=NUC=delta=counts=unsorted=false; |
| 477 |
|
| 478 |
for(int i=0; i<sub.length(); i++){ |
| 479 |
char c=sub.charAt(i); |
| 480 |
if(c=='A'){A48=true;} |
| 481 |
else if(c=='H'){HEX=true;} |
| 482 |
else if(c=='R'){A48=HEX=false;} |
| 483 |
else if(c=='N'){NUC=true;} |
| 484 |
else if(c=='D'){delta=true;} |
| 485 |
else if(c=='C'){counts=true;} |
| 486 |
else if(c=='U'){unsorted=true;} |
| 487 |
else if(c=='M'){assert(aminoOrTranslate()) : "Amino sketch in non-amino mode: "+new String(line);} |
| 488 |
else if(c=='8'){assert(amino8) : "Amino8 sketch in non-amino8 mode: "+new String(line);} |
| 489 |
else{assert(false) : "Unknown coding symbol: "+c+"\t"+new String(line);} |
| 490 |
} |
| 491 |
}else if(s.startsWith("K:")){ |
| 492 |
if(sub.indexOf(',')>=0){ |
| 493 |
String[] subsplit=sub.split(","); |
| 494 |
assert(subsplit.length==2) : "Bad header component "+s; |
| 495 |
int x=Integer.parseInt(subsplit[0]); |
| 496 |
int y=Integer.parseInt(subsplit[1]); |
| 497 |
k_sketch=Tools.max(x, y); |
| 498 |
k2_sketch=Tools.min(x, y); |
| 499 |
}else{ |
| 500 |
k_sketch=Integer.parseInt(sub); |
| 501 |
k2_sketch=0; |
| 502 |
} |
| 503 |
}else if(s.startsWith("H:")){ |
| 504 |
hashVersion_sketch=Integer.parseInt(sub); |
| 505 |
}else if(s.startsWith("BC:") || s.startsWith("BASECOUNTS:")){ |
| 506 |
baseCounts=Parse.parseLongArray(sub); |
| 507 |
}else if(s.startsWith("GS:") || s.startsWith("GSIZE:")){ |
| 508 |
genomeSizeBases=Long.parseLong(sub); |
| 509 |
}else if(s.startsWith("GK:") || s.startsWith("GKMERS:")){ |
| 510 |
genomeSizeKmers=Long.parseLong(sub); |
| 511 |
}else if(s.startsWith("GQ:")){ |
| 512 |
genomeSequences=Long.parseLong(sub); |
| 513 |
}else if(s.startsWith("GE:")){ |
| 514 |
|
| 515 |
}else if(s.startsWith("PC:")){ |
| 516 |
probCorrect=Float.parseFloat(sub); |
| 517 |
}else if(s.startsWith("ID:") || s.startsWith("TAXID:")){ |
| 518 |
taxID=Integer.parseInt(sub); |
| 519 |
}else if(s.startsWith("IMG:")){ |
| 520 |
imgID=Long.parseLong(sub); |
| 521 |
}else if(s.startsWith("SPID:")){ |
| 522 |
spid=Integer.parseInt(sub); |
| 523 |
}else if(s.startsWith("NM:") || s.startsWith("NAME:")){ |
| 524 |
name=sub; |
| 525 |
}else if(s.startsWith("FN:")){ |
| 526 |
fname=sub; |
| 527 |
}else if(s.startsWith("NM0:")){ |
| 528 |
name0=sub; |
| 529 |
}else if(s.startsWith("MT_")){ |
| 530 |
if(meta==null){meta=new ArrayList<String>(1);} |
| 531 |
meta.add(s.substring(3)); |
| 532 |
}else if(s.startsWith("16S:") || s.startsWith("SSU:")){ |
| 533 |
r16SLen=Integer.parseInt(sub); |
| 534 |
}else if(s.startsWith("18S:")){ |
| 535 |
r18SLen=Integer.parseInt(sub); |
| 536 |
}else{ |
| 537 |
assert(false) : "Unsupported header tag "+s; |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
if(KILL_OK){ |
| 543 |
if(k_sketch!=k && !NUC){KillSwitch.kill("Sketch kmer length "+k_sketch+ |
| 544 |
" differs from loaded kmer length "+k+"\n"+new String(line)+"\nfile: "+ff.name());} |
| 545 |
if(k2_sketch!=k2 && !NUC){KillSwitch.kill("Sketch kmer length "+k_sketch+","+k2_sketch+ |
| 546 |
" differs from loaded kmer length "+k+","+k2+"\n"+new String(line)+"\nfile: "+ff.name());} |
| 547 |
if(hashVersion_sketch!=HASH_VERSION && !NUC){KillSwitch.kill("Sketch hash version "+hashVersion_sketch+ |
| 548 |
" differs from loaded hash version "+HASH_VERSION+".\n" |
| 549 |
+ "You may need to download the latest version of BBTools.\n"+new String(line)+"\nfile: "+ff.name());} |
| 550 |
}else{ |
| 551 |
assert(k_sketch==k || NUC) : "Sketch kmer length "+k_sketch+" differs from loaded kmer length "+k+"\n"+new String(line); |
| 552 |
assert(k2_sketch==k2 || NUC) : "Sketch kmer length "+k_sketch+","+k2_sketch+" differs from loaded kmer length "+k+","+k2+"\n"+new String(line); |
| 553 |
assert(hashVersion_sketch==HASH_VERSION || NUC) : "Sketch hash version "+hashVersion_sketch+ |
| 554 |
" differs from loaded hash version "+HASH_VERSION+".\n" |
| 555 |
+ "You may need to download the latest version of BBTools.\n"+new String(line)+"\n"; |
| 556 |
} |
| 557 |
if(currentSketchSize>0 || allowZeroSizeSketch){ |
| 558 |
list=new LongList(Tools.max(1, currentSketchSize)); |
| 559 |
if(counts){countList=new IntList(Tools.max(1, currentSketchSize));} |
| 560 |
} |
| 561 |
} |
| 562 |
}else{ |
| 563 |
long x=(counts ? Sketch.parseA48C(line, countList) : A48 ? Sketch.parseA48(line) : |
| 564 |
HEX ? Sketch.parseHex(line) : NUC ? Sketch.parseNuc(line) : Parse.parseLong(line)); |
| 565 |
|
| 566 |
sum+=x; |
| 567 |
assert(x>=0 || NUC) : "x="+x+"\nline="+new String(line)+"\nheader="+(lastHeader==null ? "null" : new String(lastHeader))+"\nlineNum="+bf.lineNum()+"\n"; |
| 568 |
assert(sum>=0 || !delta) : "The sketch was made with delta compression off. Please regenerate it."; |
| 569 |
assert(list!=null) : new String(line); |
| 570 |
long key=(delta ? sum : x); |
| 571 |
if(key>=0){list.add(key);} |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
if(list!=null && (list.size>0 || allowZeroSizeSketch)){ |
| 576 |
assert(list.size==list.array.length || NUC || unsorted || (allowZeroSizeSketch && list.size==0)) : list.size+"!="+list.array.length; |
| 577 |
if(NUC || unsorted){ |
| 578 |
list.sort(); |
| 579 |
list.shrinkToUnique(); |
| 580 |
}else{ |
| 581 |
list.shrink(); |
| 582 |
} |
| 583 |
int[] keyCounts=countList==null ? null : countList.array; |
| 584 |
Sketch sketch=new Sketch(list.array, keyCounts, baseCounts, r16S, r18S, taxID, imgID, |
| 585 |
genomeSizeBases, genomeSizeKmers, genomeSequences, probCorrect, name, name0, fname, meta); |
| 586 |
sketch.spid=spid; |
| 587 |
sketches.add(sketch); |
| 588 |
} |
| 589 |
if(verbose2){System.err.println("Loaded "+sketches.size()+" sketches from text.");} |
| 590 |
return sketches; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
private ArrayList<Sketch> loadSketchesFromSketchFile2(final FileFormat ff, boolean allowZeroSizeSketch){ |
| 595 |
|
| 596 |
boolean A48=(Sketch.CODING==Sketch.A48), HEX=(Sketch.CODING==Sketch.HEX), NUC=false, delta=true, counts=false, unsorted=false; |
| 597 |
|
| 598 |
if(verbose2){System.err.println("Loading sketches from text.");} |
| 599 |
ArrayList<Sketch> sketches=new ArrayList<Sketch>(); |
| 600 |
|
| 601 |
InputStream is=ReadWrite.getInputStream(ff.name(), BUFFERED_READER, false); |
| 602 |
byte[] buffer=new byte[BUFLEN]; |
| 603 |
int start, limit=0; |
| 604 |
try { |
| 605 |
limit=is.read(buffer); |
| 606 |
} catch (IOException e) { |
| 607 |
KillSwitch.exceptionKill(e); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
int currentSketchSize=stTargetSketchSize; |
| 612 |
int taxID=-1; |
| 613 |
long spid=-1; |
| 614 |
long imgID=-1; |
| 615 |
long genomeSizeBases=0, genomeSizeKmers=0, genomeSequences=0; |
| 616 |
long[] baseCounts=null; |
| 617 |
byte[] r16S=null; |
| 618 |
byte[] r18S=null; |
| 619 |
int r16SLen=0; |
| 620 |
int r18SLen=0; |
| 621 |
float probCorrect=-1; |
| 622 |
String name=null, name0=null, fname=ff.simpleName(); |
| 623 |
LongList list=null; |
| 624 |
IntList countList=null; |
| 625 |
ArrayList<String> meta=null; |
| 626 |
long sum=0; |
| 627 |
byte[] lastHeader=null; |
| 628 |
ByteBuilder bb=new ByteBuilder(256); |
| 629 |
|
| 630 |
for(start=0; start<limit;){ |
| 631 |
|
| 632 |
if(buffer[start]=='#'){ |
| 633 |
bb.clear(); |
| 634 |
try { |
| 635 |
while(buffer[start]!='\n'){ |
| 636 |
bb.append(buffer[start]); |
| 637 |
start++; |
| 638 |
if(start>=limit){start=0; limit=is.read(buffer);} |
| 639 |
} |
| 640 |
} catch (IOException e) { |
| 641 |
KillSwitch.exceptionKill(e); |
| 642 |
} |
| 643 |
start++; |
| 644 |
|
| 645 |
if(r16SLen>0 || r18SLen>0){ |
| 646 |
byte[] ssu=bb.toBytes(5, bb.length()); |
| 647 |
if(bb.startsWith("#16S:") || bb.startsWith("#SSU:")){ |
| 648 |
assert(r16SLen>0); |
| 649 |
assert(ssu.length==r16SLen) : r16SLen+", "+bb.length+"\n"+bb+"\n"; |
| 650 |
r16S=ssu; |
| 651 |
r16SLen=0; |
| 652 |
}else if(bb.startsWith("#18S:")){ |
| 653 |
assert(r18SLen>0); |
| 654 |
assert(ssu.length==r18SLen) : r18SLen+", "+bb.length+"\n"+bb+"\n"; |
| 655 |
r18S=ssu; |
| 656 |
r18SLen=0; |
| 657 |
}else{ |
| 658 |
assert(false) : bb; |
| 659 |
} |
| 660 |
}else{ |
| 661 |
|
| 662 |
|
| 663 |
if(list!=null){ |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
if(NUC || unsorted){ |
| 669 |
list.sort(); |
| 670 |
list.shrinkToUnique(); |
| 671 |
}else{ |
| 672 |
list.shrink(); |
| 673 |
} |
| 674 |
if(list.size()>0 || allowZeroSizeSketch){ |
| 675 |
int[] keyCounts=countList==null ? null : countList.array; |
| 676 |
Sketch sketch=new Sketch(list.array, keyCounts, baseCounts, r16S, r18S, taxID, imgID, |
| 677 |
genomeSizeBases, genomeSizeKmers, genomeSequences, probCorrect, name, name0, fname, meta); |
| 678 |
sketch.spid=spid; |
| 679 |
sketches.add(sketch); |
| 680 |
} |
| 681 |
|
| 682 |
} |
| 683 |
name=name0=null; |
| 684 |
fname=ff.simpleName(); |
| 685 |
list=null; |
| 686 |
countList=null; |
| 687 |
meta=null; |
| 688 |
baseCounts=null; |
| 689 |
r16S=null; |
| 690 |
r18S=null; |
| 691 |
r16SLen=0; |
| 692 |
r18SLen=0; |
| 693 |
sum=0; |
| 694 |
taxID=-1; |
| 695 |
imgID=-1; |
| 696 |
genomeSizeBases=0; |
| 697 |
genomeSizeKmers=0; |
| 698 |
genomeSequences=0; |
| 699 |
probCorrect=-1; |
| 700 |
int k_sketch=defaultK; |
| 701 |
int k2_sketch=0; |
| 702 |
int hashVersion_sketch=1; |
| 703 |
|
| 704 |
if(bb.length>1){ |
| 705 |
String[] split=new String(bb.array, 1, bb.length-1).split("\t"); |
| 706 |
for(String s : split){ |
| 707 |
final int colon=s.indexOf(':'); |
| 708 |
final String sub=s.substring(colon+1); |
| 709 |
if(s.startsWith("SZ:") || s.startsWith("SIZE:")){ |
| 710 |
currentSketchSize=Integer.parseInt(sub); |
| 711 |
}else if(s.startsWith("CD:")){ |
| 712 |
A48=HEX=NUC=delta=counts=unsorted=false; |
| 713 |
|
| 714 |
for(int i=0; i<sub.length(); i++){ |
| 715 |
char c=sub.charAt(i); |
| 716 |
if(c=='A'){A48=true;} |
| 717 |
else if(c=='H'){HEX=true;} |
| 718 |
else if(c=='R'){A48=HEX=false;} |
| 719 |
else if(c=='N'){NUC=true;} |
| 720 |
else if(c=='D'){delta=true;} |
| 721 |
else if(c=='C'){counts=true;} |
| 722 |
else if(c=='U'){unsorted=true;} |
| 723 |
else if(c=='M'){assert(aminoOrTranslate()) : "Amino sketch in non-amino mode: "+bb;} |
| 724 |
else if(c=='8'){assert(amino8) : "Amino8 sketch in non-amino8 mode: "+bb;} |
| 725 |
else{assert(false) : "Unknown coding symbol: "+c+"\t"+bb;} |
| 726 |
} |
| 727 |
}else if(s.startsWith("K:")){ |
| 728 |
if(sub.indexOf(',')>=0){ |
| 729 |
String[] subsplit=sub.split(","); |
| 730 |
assert(subsplit.length==2) : "Bad header component "+s; |
| 731 |
int x=Integer.parseInt(subsplit[0]); |
| 732 |
int y=Integer.parseInt(subsplit[1]); |
| 733 |
k_sketch=Tools.max(x, y); |
| 734 |
k2_sketch=Tools.min(x, y); |
| 735 |
}else{ |
| 736 |
k_sketch=Integer.parseInt(sub); |
| 737 |
k2_sketch=0; |
| 738 |
} |
| 739 |
}else if(s.startsWith("H:")){ |
| 740 |
hashVersion_sketch=Integer.parseInt(sub); |
| 741 |
}else if(s.startsWith("BC:") || s.startsWith("BASECOUNTS:")){ |
| 742 |
baseCounts=Parse.parseLongArray(sub); |
| 743 |
}else if(s.startsWith("GS:") || s.startsWith("GSIZE:")){ |
| 744 |
genomeSizeBases=Long.parseLong(sub); |
| 745 |
}else if(s.startsWith("GK:") || s.startsWith("GKMERS:")){ |
| 746 |
genomeSizeKmers=Long.parseLong(sub); |
| 747 |
}else if(s.startsWith("GQ:")){ |
| 748 |
genomeSequences=Long.parseLong(sub); |
| 749 |
}else if(s.startsWith("GE:")){ |
| 750 |
|
| 751 |
}else if(s.startsWith("PC:")){ |
| 752 |
probCorrect=Float.parseFloat(sub); |
| 753 |
}else if(s.startsWith("ID:") || s.startsWith("TAXID:")){ |
| 754 |
taxID=Integer.parseInt(sub); |
| 755 |
}else if(s.startsWith("IMG:")){ |
| 756 |
imgID=Long.parseLong(sub); |
| 757 |
}else if(s.startsWith("SPID:")){ |
| 758 |
spid=Integer.parseInt(sub); |
| 759 |
}else if(s.startsWith("NM:") || s.startsWith("NAME:")){ |
| 760 |
name=sub; |
| 761 |
}else if(s.startsWith("FN:")){ |
| 762 |
fname=sub; |
| 763 |
}else if(s.startsWith("NM0:")){ |
| 764 |
name0=sub; |
| 765 |
}else if(s.startsWith("MT_")){ |
| 766 |
if(meta==null){meta=new ArrayList<String>(1);} |
| 767 |
meta.add(s.substring(3)); |
| 768 |
}else if(s.startsWith("16S:") || s.startsWith("SSU:")){ |
| 769 |
r16SLen=Integer.parseInt(sub); |
| 770 |
}else if(s.startsWith("18S:")){ |
| 771 |
r18SLen=Integer.parseInt(sub); |
| 772 |
}else{ |
| 773 |
assert(false) : "Unsupported header tag "+s; |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
if(KILL_OK){ |
| 779 |
if(k_sketch!=k && !NUC){KillSwitch.kill("Sketch kmer length "+k_sketch+ |
| 780 |
" differs from loaded kmer length "+k+"\n"+bb+"\nfile: "+ff.name());} |
| 781 |
if(k2_sketch!=k2 && !NUC){KillSwitch.kill("Sketch kmer length "+k_sketch+","+k2_sketch+ |
| 782 |
" differs from loaded kmer length "+k+","+k2+"\n"+bb+"\nfile: "+ff.name());} |
| 783 |
if(hashVersion_sketch!=HASH_VERSION && !NUC){KillSwitch.kill("Sketch hash version "+hashVersion_sketch+ |
| 784 |
" differs from loaded hash version "+HASH_VERSION+".\n" |
| 785 |
+ "You may need to download the latest version of BBTools.\nfile: "+ff.name());} |
| 786 |
}else{ |
| 787 |
assert(k_sketch==k || NUC) : "Sketch kmer length "+k_sketch+" differs from loaded kmer length "+k+"\n"+bb; |
| 788 |
assert(k2_sketch==k2 || NUC) : "Sketch kmer length "+k_sketch+","+k2_sketch+" differs from loaded kmer length "+k+","+k2+"\n"+bb; |
| 789 |
assert(hashVersion_sketch==HASH_VERSION || NUC) : "Sketch hash version "+hashVersion_sketch+ |
| 790 |
" differs from loaded hash version "+HASH_VERSION+".\n" |
| 791 |
+ "You may need to download the latest version of BBTools.\nfile: "+ff.name(); |
| 792 |
} |
| 793 |
if(currentSketchSize>0 || allowZeroSizeSketch){ |
| 794 |
list=new LongList(Tools.max(1, currentSketchSize)); |
| 795 |
if(counts){countList=new IntList(Tools.max(1, currentSketchSize));} |
| 796 |
} |
| 797 |
} |
| 798 |
}else{ |
| 799 |
bb.clear(); |
| 800 |
try { |
| 801 |
while(buffer[start]!='\n'){ |
| 802 |
bb.append(buffer[start]); |
| 803 |
start++; |
| 804 |
if(start>=limit){start=0; limit=is.read(buffer);} |
| 805 |
} |
| 806 |
} catch (IOException e) { |
| 807 |
KillSwitch.exceptionKill(e); |
| 808 |
} |
| 809 |
start++; |
| 810 |
|
| 811 |
long x=(counts ? Sketch.parseA48C(bb, countList) : A48 ? Sketch.parseA48(bb) : |
| 812 |
HEX ? Sketch.parseHex(bb) : NUC ? Sketch.parseNuc(bb) : Parse.parseLong(bb.array, 0, bb.length)); |
| 813 |
|
| 814 |
sum+=x; |
| 815 |
assert(x>=0 || NUC) : "x="+x+"\nline="+bb+"\nheader="+(lastHeader==null ? "null" : new String(lastHeader))+"\n"; |
| 816 |
assert(sum>=0 || !delta) : "The sketch was made with delta compression off. Please regenerate it."; |
| 817 |
assert(list!=null) : bb; |
| 818 |
long key=(delta ? sum : x); |
| 819 |
if(key>=0){list.add(key);} |
| 820 |
} |
| 821 |
|
| 822 |
if(start>=limit){ |
| 823 |
start=0; |
| 824 |
try { |
| 825 |
limit=is.read(buffer); |
| 826 |
} catch (IOException e) { |
| 827 |
KillSwitch.exceptionKill(e); |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
if(list!=null && (list.size>0 || allowZeroSizeSketch)){ |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
if(NUC || unsorted){ |
| 838 |
list.sort(); |
| 839 |
list.shrinkToUnique(); |
| 840 |
}else{ |
| 841 |
list.shrink(); |
| 842 |
} |
| 843 |
int[] keyCounts=countList==null ? null : countList.array; |
| 844 |
Sketch sketch=new Sketch(list.array, keyCounts, baseCounts, r16S, r18S, taxID, imgID, |
| 845 |
genomeSizeBases, genomeSizeKmers, genomeSequences, probCorrect, name, name0, fname, meta); |
| 846 |
sketch.spid=spid; |
| 847 |
sketches.add(sketch); |
| 848 |
} |
| 849 |
if(verbose2){System.err.println("Loaded "+sketches.size()+" sketches from text.");} |
| 850 |
try { |
| 851 |
is.close(); |
| 852 |
} catch (IOException e) { |
| 853 |
e.printStackTrace(); |
| 854 |
} |
| 855 |
return sketches; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
public ArrayList<Sketch> loadSketchesFromString(String sketchString){ |
| 861 |
boolean A48=(Sketch.CODING==Sketch.A48), HEX=(Sketch.CODING==Sketch.HEX), NUC=false, delta=true, counts=false, unsorted=false; |
| 862 |
|
| 863 |
ArrayList<Sketch> sketches=new ArrayList<Sketch>(); |
| 864 |
int currentSketchSize=stTargetSketchSize; |
| 865 |
int taxID=-1; |
| 866 |
long spid=-1; |
| 867 |
long imgID=-1; |
| 868 |
long genomeSizeBases=0, genomeSizeKmers=0, genomeSequences=0; |
| 869 |
long[] baseCounts=null; |
| 870 |
byte[] r16S=null; |
| 871 |
byte[] r18S=null; |
| 872 |
int r16SLen=0; |
| 873 |
int r18SLen=0; |
| 874 |
float probCorrect=-1; |
| 875 |
String name=null, name0=null, fname=null; |
| 876 |
LongList list=null; |
| 877 |
IntList countList=null; |
| 878 |
ArrayList<String> meta=null; |
| 879 |
long sum=0; |
| 880 |
|
| 881 |
String[] split0=sketchString.split("\n"); |
| 882 |
for(String line : split0){ |
| 883 |
if(line.length()>0){ |
| 884 |
|
| 885 |
if(line.charAt(0)=='#'){ |
| 886 |
if(line.length()>1 && line.charAt(1)=='#'){ |
| 887 |
|
| 888 |
}else if(r16SLen>0 || r18SLen>0){ |
| 889 |
byte[] ssu=KillSwitch.copyOfRange(line.getBytes(), 5, line.length()); |
| 890 |
if(line.startsWith("#16S:") || line.startsWith("#SSU:")){ |
| 891 |
assert(r16SLen>0); |
| 892 |
assert(ssu.length==r16SLen) : r16SLen+", "+line.length()+"\n"+line+"\n"; |
| 893 |
r16S=ssu; |
| 894 |
r16SLen=0; |
| 895 |
}else if(line.startsWith("#18S:")){ |
| 896 |
assert(r18SLen>0); |
| 897 |
assert(ssu.length==r18SLen) : r18SLen+", "+line.length()+"\n"+line+"\n"; |
| 898 |
r18S=ssu; |
| 899 |
r18SLen=0; |
| 900 |
}else{ |
| 901 |
assert(false) : line; |
| 902 |
} |
| 903 |
}else{ |
| 904 |
if(list!=null){ |
| 905 |
assert(list.size==list.array.length); |
| 906 |
if(NUC || unsorted){ |
| 907 |
list.sort(); |
| 908 |
list.shrinkToUnique(); |
| 909 |
}else{ |
| 910 |
list.shrink(); |
| 911 |
} |
| 912 |
if(list.size()>0){ |
| 913 |
int[] keyCounts=countList==null ? null : countList.array; |
| 914 |
Sketch sketch=new Sketch(list.array, keyCounts, baseCounts, r16S, r18S, taxID, imgID, |
| 915 |
genomeSizeBases, genomeSizeKmers, genomeSequences, probCorrect, name, name0, fname, meta); |
| 916 |
sketch.spid=spid; |
| 917 |
sketches.add(sketch); |
| 918 |
} |
| 919 |
|
| 920 |
} |
| 921 |
name=name0=null; |
| 922 |
fname=null; |
| 923 |
list=null; |
| 924 |
countList=null; |
| 925 |
meta=null; |
| 926 |
baseCounts=null; |
| 927 |
r16S=null; |
| 928 |
r18S=null; |
| 929 |
r16SLen=0; |
| 930 |
r18SLen=0; |
| 931 |
sum=0; |
| 932 |
taxID=-1; |
| 933 |
imgID=-1; |
| 934 |
genomeSizeBases=0; |
| 935 |
genomeSizeKmers=0; |
| 936 |
genomeSequences=0; |
| 937 |
probCorrect=-1; |
| 938 |
int k_sketch=defaultK; |
| 939 |
int k2_sketch=0; |
| 940 |
int hashVersion_sketch=1; |
| 941 |
|
| 942 |
if(line.length()>1){ |
| 943 |
String[] split=line.substring(1).split("\t"); |
| 944 |
for(String s : split){ |
| 945 |
final int colon=s.indexOf(':'); |
| 946 |
final String sub=s.substring(colon+1); |
| 947 |
if(s.startsWith("SZ:") || s.startsWith("SIZE:")){ |
| 948 |
currentSketchSize=Integer.parseInt(sub); |
| 949 |
}else if(s.startsWith("CD:")){ |
| 950 |
A48=HEX=NUC=delta=counts=false; |
| 951 |
|
| 952 |
for(int i=0; i<sub.length(); i++){ |
| 953 |
char c=sub.charAt(i); |
| 954 |
if(c=='A'){A48=true;} |
| 955 |
else if(c=='H'){HEX=true;} |
| 956 |
else if(c=='R'){A48=HEX=false;} |
| 957 |
else if(c=='N'){NUC=true;} |
| 958 |
else if(c=='D'){delta=true;} |
| 959 |
else if(c=='C'){counts=true;} |
| 960 |
else if(c=='U'){unsorted=true;} |
| 961 |
else if(c=='M'){assert(aminoOrTranslate()) : "Amino sketch in non-amino mode: "+new String(line);} |
| 962 |
else if(c=='8'){assert(amino8) : "Amino8 sketch in non-amino8 mode: "+new String(line);} |
| 963 |
else{assert(false) : "Unknown coding symbol: "+c+"\t"+new String(line);} |
| 964 |
} |
| 965 |
|
| 966 |
}else if(s.startsWith("K:")){ |
| 967 |
if(sub.indexOf(',')>=0){ |
| 968 |
String[] subsplit=sub.split(","); |
| 969 |
assert(subsplit.length==2) : "Bad header component "+s; |
| 970 |
int x=Integer.parseInt(subsplit[0]); |
| 971 |
int y=Integer.parseInt(subsplit[1]); |
| 972 |
k_sketch=Tools.max(x, y); |
| 973 |
k2_sketch=Tools.min(x, y); |
| 974 |
}else{ |
| 975 |
k_sketch=Integer.parseInt(s); |
| 976 |
k2_sketch=0; |
| 977 |
} |
| 978 |
}else if(s.startsWith("H:")){ |
| 979 |
hashVersion_sketch=Integer.parseInt(sub); |
| 980 |
}else if(s.startsWith("BC:") || s.startsWith("BASECOUNTS:")){ |
| 981 |
baseCounts=Parse.parseLongArray(sub); |
| 982 |
}else if(s.startsWith("GS:") || s.startsWith("GSIZE:")){ |
| 983 |
genomeSizeBases=Long.parseLong(sub); |
| 984 |
}else if(s.startsWith("GK:") || s.startsWith("GKMERS:")){ |
| 985 |
genomeSizeKmers=Long.parseLong(sub); |
| 986 |
}else if(s.startsWith("GQ:")){ |
| 987 |
genomeSequences=Long.parseLong(sub); |
| 988 |
}else if(s.startsWith("GE:")){ |
| 989 |
|
| 990 |
}else if(s.startsWith("PC:")){ |
| 991 |
probCorrect=Float.parseFloat(sub); |
| 992 |
}else if(s.startsWith("ID:") || s.startsWith("TAXID:")){ |
| 993 |
taxID=Integer.parseInt(sub); |
| 994 |
}else if(s.startsWith("IMG:")){ |
| 995 |
imgID=Long.parseLong(sub); |
| 996 |
}else if(s.startsWith("SPID:")){ |
| 997 |
spid=Integer.parseInt(sub); |
| 998 |
}else if(s.startsWith("NM:") || s.startsWith("NAME:")){ |
| 999 |
name=sub; |
| 1000 |
}else if(s.startsWith("FN:")){ |
| 1001 |
fname=sub; |
| 1002 |
}else if(s.startsWith("NM0:")){ |
| 1003 |
name0=sub; |
| 1004 |
}else if(s.startsWith("MT_")){ |
| 1005 |
if(meta==null){meta=new ArrayList<String>(1);} |
| 1006 |
meta.add(s.substring(3)); |
| 1007 |
}else if(s.startsWith("16S:") || s.startsWith("SSU:")){ |
| 1008 |
r16SLen=Integer.parseInt(sub); |
| 1009 |
}else if(s.startsWith("18S:")){ |
| 1010 |
r18SLen=Integer.parseInt(sub); |
| 1011 |
}else{ |
| 1012 |
assert(false) : "Unsupported header tag "+s; |
| 1013 |
} |
| 1014 |
} |
| 1015 |
} |
| 1016 |
if(KILL_OK){ |
| 1017 |
if(k_sketch!=k && !NUC){KillSwitch.kill("Sketch kmer length "+k_sketch+" differs from loaded kmer length "+k+"\n"+new String(line));} |
| 1018 |
if(k2_sketch!=k2 && !NUC){KillSwitch.kill("Sketch kmer length "+k_sketch+","+k2_sketch+" differs from loaded kmer length "+k+","+k2+"\n"+new String(line));} |
| 1019 |
if(hashVersion_sketch!=HASH_VERSION && !NUC){KillSwitch.kill("Sketch hash version "+hashVersion_sketch+ |
| 1020 |
" differs from loaded hash version "+HASH_VERSION+".\n" |
| 1021 |
+ "You may need to download the latest version of BBTools.\n"+new String(line)+"\n");} |
| 1022 |
}else{ |
| 1023 |
assert(k_sketch==k && !NUC) : "Sketch kmer length "+k_sketch+" differs from loaded kmer length "+k+"\n"+new String(line); |
| 1024 |
assert(k2_sketch==k2 && !NUC) : "Sketch kmer length "+k_sketch+","+k2_sketch+" differs from loaded kmer length "+k+","+k2+"\n"+new String(line); |
| 1025 |
assert(hashVersion_sketch==HASH_VERSION || NUC) : "Sketch hash version "+hashVersion_sketch+ |
| 1026 |
" differs from loaded hash version "+HASH_VERSION+".\n" |
| 1027 |
+ "You may need to download the latest version of BBTools.\n"+new String(line)+"\n"; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
if(currentSketchSize>0){ |
| 1032 |
list=new LongList(currentSketchSize); |
| 1033 |
if(counts){countList=new IntList(currentSketchSize);} |
| 1034 |
} |
| 1035 |
} |
| 1036 |
}else{ |
| 1037 |
long x=(counts ? Sketch.parseA48C(line, countList) : A48 ? Sketch.parseA48(line) : |
| 1038 |
HEX ? Sketch.parseHex(line) : NUC ? Sketch.parseNuc(line) : Long.parseLong(line)); |
| 1039 |
|
| 1040 |
sum+=x; |
| 1041 |
assert(x>=0 || NUC) : x+"\n"+new String(line); |
| 1042 |
assert(sum>=0 || !delta) : "The sketch was made with delta compression off. Please regenerate it."; |
| 1043 |
long key=(delta ? sum : x); |
| 1044 |
if(key>=0){list.add(key);} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
if(list!=null){ |
| 1050 |
assert(list.size==list.array.length || list.size()==0 || NUC || unsorted); |
| 1051 |
if(NUC || unsorted){ |
| 1052 |
list.sort(); |
| 1053 |
list.shrinkToUnique(); |
| 1054 |
}else{ |
| 1055 |
list.shrink(); |
| 1056 |
} |
| 1057 |
int[] keyCounts=countList==null ? null : countList.array; |
| 1058 |
Sketch sketch=new Sketch(list.array, keyCounts, baseCounts, r16S, r18S, taxID, imgID, |
| 1059 |
genomeSizeBases, genomeSizeKmers, genomeSequences, probCorrect, name, name0, fname, meta); |
| 1060 |
sketch.spid=spid; |
| 1061 |
sketches.add(sketch); |
| 1062 |
} |
| 1063 |
return sketches; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
private class LoadThread extends Thread{ |
| 1073 |
|
| 1074 |
public LoadThread(ConcurrentLinkedQueue<StringNum> queue_, int mode_, float samplerate_, long reads_, float minEntropy, float minProb, byte minQual) { |
| 1075 |
queue=queue_; |
| 1076 |
list=new ArrayList<Sketch>(); |
| 1077 |
smm=new SketchMakerMini(SketchTool.this, mode_, minEntropy, minProb, minQual); |
| 1078 |
samplerate=samplerate_; |
| 1079 |
reads=reads_; |
| 1080 |
} |
| 1081 |
|
| 1082 |
@Override |
| 1083 |
public void run(){ |
| 1084 |
success=false; |
| 1085 |
for(StringNum sn=queue.poll(); sn!=null; sn=queue.poll()){ |
| 1086 |
ArrayList<Sketch> temp=null; |
| 1087 |
try { |
| 1088 |
temp=loadSketchesFromFile(sn.s, smm, 1, reads, smm.mode, samplerate, smm.minEntropy(), smm.minProb(), smm.minQual(), false); |
| 1089 |
} catch (Throwable e) { |
| 1090 |
System.err.println("Failure loading "+sn+":\n"+e); |
| 1091 |
e.printStackTrace(); |
| 1092 |
success=false; |
| 1093 |
} |
| 1094 |
if(temp!=null && temp.size()>0){ |
| 1095 |
if(smm.mode==PER_FILE){ |
| 1096 |
|
| 1097 |
temp.get(0).sketchID=(int)sn.n; |
| 1098 |
} |
| 1099 |
for(Sketch s : temp){add(s);} |
| 1100 |
} |
| 1101 |
} |
| 1102 |
success=true; |
| 1103 |
} |
| 1104 |
|
| 1105 |
private void add(Sketch s){ |
| 1106 |
if(list!=null){ |
| 1107 |
list.add(s); |
| 1108 |
return; |
| 1109 |
} |
| 1110 |
assert(false) : "Unsupported."; |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
} |
| 1131 |
|
| 1132 |
final ConcurrentLinkedQueue<StringNum> queue; |
| 1133 |
ArrayList<Sketch> list; |
| 1134 |
boolean success=false; |
| 1135 |
final SketchMakerMini smm; |
| 1136 |
final float samplerate; |
| 1137 |
final long reads; |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
} |
| 1142 |
|
| 1143 |
private class LoadThread2 extends Thread{ |
| 1144 |
|
| 1145 |
LoadThread2(ConcurrentReadInputStream cris_, float minEntropy, float minProb, byte minQual){ |
| 1146 |
cris=cris_; |
| 1147 |
smm=new SketchMakerMini(SketchTool.this, ONE_SKETCH, minEntropy, minProb, minQual); |
| 1148 |
} |
| 1149 |
|
| 1150 |
@Override |
| 1151 |
public void run(){ |
| 1152 |
|
| 1153 |
|
| 1154 |
ListNum<Read> ln=cris.nextList(); |
| 1155 |
|
| 1156 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 1157 |
|
| 1158 |
|
| 1159 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 1160 |
|
| 1161 |
|
| 1162 |
for(int idx=0; idx<reads.size(); idx++){ |
| 1163 |
final Read r1=reads.get(idx); |
| 1164 |
final Read r2=r1.mate; |
| 1165 |
|
| 1166 |
if(validate){ |
| 1167 |
if(r1!=null){r1.validate(true);} |
| 1168 |
if(r2!=null){r2.validate(true);} |
| 1169 |
} |
| 1170 |
|
| 1171 |
smm.processReadPair(r1, r2); |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
cris.returnList(ln); |
| 1176 |
|
| 1177 |
|
| 1178 |
ln=cris.nextList(); |
| 1179 |
reads=(ln!=null ? ln.list : null); |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
if(ln!=null){ |
| 1184 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
private final boolean validate=!Read.VALIDATE_IN_CONSTRUCTOR; |
| 1189 |
ConcurrentReadInputStream cris; |
| 1190 |
SketchMakerMini smm; |
| 1191 |
|
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
private class SketchThread extends Thread { |
| 1196 |
|
| 1197 |
SketchThread(AtomicInteger next_, KmerTableSet kts_){ |
| 1198 |
next=next_; |
| 1199 |
kts=kts_; |
| 1200 |
} |
| 1201 |
|
| 1202 |
@Override |
| 1203 |
public void run(){ |
| 1204 |
final int ways=kts.ways(); |
| 1205 |
int tnum=next.getAndIncrement(); |
| 1206 |
while(tnum<ways){ |
| 1207 |
HashArray1D table=kts.getTable(tnum); |
| 1208 |
if(stTargetSketchSize>0){ |
| 1209 |
if(heap==null){heap=new SketchHeap(stTargetSketchSize, minKeyOccuranceCount, trackCounts);} |
| 1210 |
toHeap(table, heap); |
| 1211 |
}else{ |
| 1212 |
if(list==null){list=new LongList();} |
| 1213 |
toList(table, list); |
| 1214 |
} |
| 1215 |
tnum=next.getAndIncrement(); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
final AtomicInteger next; |
| 1220 |
final KmerTableSet kts; |
| 1221 |
SketchHeap heap; |
| 1222 |
LongList list; |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
public Sketch processReadsMT(String fname, int maxThreads, long reads, int mode, float samplerate, |
| 1230 |
float minEntropy, float minProb, byte minQual, boolean allowZeroSizeSketch){ |
| 1231 |
if(fname.indexOf('#')>=0 && FileFormat.isFastqExt(ReadWrite.rawExtension(fname)) && !new File(fname).exists()){ |
| 1232 |
return processReadsMT(fname.replaceFirst("#", "1"), fname.replaceFirst("#", "2"), maxThreads, reads, mode, samplerate, |
| 1233 |
minEntropy, minProb, minQual, allowZeroSizeSketch); |
| 1234 |
}else{ |
| 1235 |
return processReadsMT(fname, null, maxThreads, reads, mode, samplerate, minEntropy, minProb, minQual, allowZeroSizeSketch); |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
public Sketch processReadsMT(String fname1, String fname2, int maxThreads, long reads, int mode, float samplerate, |
| 1240 |
float minEntropy, float minProb, byte minQual, boolean allowZeroSizeSketch){ |
| 1241 |
final FileFormat ffin1=FileFormat.testInput(fname1, FileFormat.FASTQ, null, true, true); |
| 1242 |
final FileFormat ffin2=FileFormat.testInput(fname2, FileFormat.FASTQ, null, true, true); |
| 1243 |
return processReadsMT(ffin1, ffin2, maxThreads, reads, mode, samplerate, minEntropy, minProb, minQual, allowZeroSizeSketch); |
| 1244 |
} |
| 1245 |
|
| 1246 |
public Sketch processReadsMT(FileFormat ffin1, FileFormat ffin2, int maxThreads, long reads, int mode, DisplayParams params, boolean allowZeroSizeSketch){ |
| 1247 |
return processReadsMT(ffin1, ffin2, maxThreads, reads, mode, params.samplerate, params.minEntropy, params.minProb, params.minQual, allowZeroSizeSketch); |
| 1248 |
} |
| 1249 |
|
| 1250 |
public Sketch processReadsMT(FileFormat ffin1, FileFormat ffin2, int maxThreads, long reads, int mode, float samplerate, |
| 1251 |
float minEntropy, float minProb, byte minQual, boolean allowZeroSizeSketch){ |
| 1252 |
assert(mode==ONE_SKETCH || mode==PER_FILE); |
| 1253 |
final boolean compressed=ffin1.compressed(); |
| 1254 |
|
| 1255 |
maxThreads=Tools.mid(1, maxThreads, Shared.threads()); |
| 1256 |
|
| 1257 |
|
| 1258 |
final ConcurrentReadInputStream cris; |
| 1259 |
String simpleName; |
| 1260 |
{ |
| 1261 |
simpleName=ffin1.simpleName(); |
| 1262 |
cris=ConcurrentReadInputStream.getReadInputStream(reads, true, ffin1, ffin2, null, null); |
| 1263 |
if(samplerate!=1){cris.setSampleRate(samplerate, sampleseed);} |
| 1264 |
cris.start(); |
| 1265 |
|
| 1266 |
} |
| 1267 |
|
| 1268 |
|
| 1269 |
final int threads=(int)Tools.min(maxThreads, |
| 1270 |
1.75f*(compressed ? 1 : 2)*(ffin2==null ? 4 : 8)*(mergePairs ? 3 : minEntropy>0 ? 2 : 1)); |
| 1271 |
|
| 1272 |
if(verbose2){System.err.println("Starting "+threads+" load threads.");} |
| 1273 |
ArrayList<LoadThread2> list=new ArrayList<LoadThread2>(threads); |
| 1274 |
for(int i=0; i<threads; i++){ |
| 1275 |
list.add(new LoadThread2(cris, minEntropy, minProb, minQual)); |
| 1276 |
list.get(i).start(); |
| 1277 |
} |
| 1278 |
|
| 1279 |
ArrayList<SketchHeap> heaps=new ArrayList<SketchHeap>(threads); |
| 1280 |
|
| 1281 |
for(LoadThread2 pt : list){ |
| 1282 |
|
| 1283 |
|
| 1284 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 1285 |
try { |
| 1286 |
|
| 1287 |
pt.join(); |
| 1288 |
} catch (InterruptedException e) { |
| 1289 |
|
| 1290 |
e.printStackTrace(); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
if(pt.smm.heap!=null && pt.smm.heap.size()>0){ |
| 1295 |
heaps.add(pt.smm.heap); |
| 1296 |
} |
| 1297 |
} |
| 1298 |
list.clear(); |
| 1299 |
ReadWrite.closeStream(cris); |
| 1300 |
|
| 1301 |
if(verbose2){System.err.println("Generating a sketch by combining thread output.");} |
| 1302 |
Sketch sketch=toSketch(heaps, allowZeroSizeSketch); |
| 1303 |
if(verbose2){System.err.println("Resulting sketch: "+((sketch==null) ? "null" : "length="+sketch.length()));} |
| 1304 |
if(sketch!=null){sketch.setFname(simpleName);} |
| 1305 |
return sketch; |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
public static boolean write(ArrayList<Sketch> sketches, FileFormat ff[]){ |
| 1313 |
final int len=ff.length; |
| 1314 |
ByteStreamWriter tsw[]=new ByteStreamWriter[len]; |
| 1315 |
for(int i=0; i<len; i++){ |
| 1316 |
tsw[i]=new ByteStreamWriter(ff[i]); |
| 1317 |
tsw[i].start(); |
| 1318 |
} |
| 1319 |
boolean error=false; |
| 1320 |
for(int i=0; i<sketches.size(); i++){ |
| 1321 |
write(sketches.get(i), tsw[i%len], new ByteBuilder()); |
| 1322 |
} |
| 1323 |
for(int i=0; i<len; i++){ |
| 1324 |
error|=tsw[i].poisonAndWait(); |
| 1325 |
} |
| 1326 |
return error; |
| 1327 |
} |
| 1328 |
|
| 1329 |
public static boolean write(ArrayList<Sketch> sketches, FileFormat ff){ |
| 1330 |
final ByteStreamWriter tsw=new ByteStreamWriter(ff); |
| 1331 |
final ByteBuilder bb=new ByteBuilder(); |
| 1332 |
tsw.start(); |
| 1333 |
for(Sketch sketch : sketches){ |
| 1334 |
write(sketch, tsw, bb); |
| 1335 |
} |
| 1336 |
return tsw.poisonAndWait(); |
| 1337 |
} |
| 1338 |
|
| 1339 |
public static boolean write(Sketch sketch, FileFormat ff){ |
| 1340 |
|
| 1341 |
ByteStreamWriter tsw=new ByteStreamWriter(ff); |
| 1342 |
|
| 1343 |
tsw.start(); |
| 1344 |
write(sketch, tsw, null); |
| 1345 |
return tsw.poisonAndWait(); |
| 1346 |
} |
| 1347 |
|
| 1348 |
public static void write(Sketch sketch, ByteStreamWriter tsw, ByteBuilder bb){ |
| 1349 |
if(bb==null){bb=new ByteBuilder();} |
| 1350 |
else{bb.clear();} |
| 1351 |
sketch.toBytes(bb); |
| 1352 |
tsw.print(bb); |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
final int stTargetSketchSize; |
| 1361 |
public final int minKeyOccuranceCount; |
| 1362 |
|
| 1363 |
public final boolean trackCounts; |
| 1364 |
|
| 1365 |
public final boolean mergePairs; |
| 1366 |
public final boolean rcomp; |
| 1367 |
|
| 1368 |
public static int BUFLEN=16384; |
| 1369 |
public static boolean BUFFERED_READER=false; |
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
} |