| 1 |
package kmer; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.BitSet; |
| 7 |
import java.util.concurrent.atomic.AtomicLong; |
| 8 |
|
| 9 |
import assemble.Contig; |
| 10 |
import bloom.KmerCountAbstract; |
| 11 |
import dna.AminoAcid; |
| 12 |
import fileIO.ByteStreamWriter; |
| 13 |
import fileIO.FileFormat; |
| 14 |
import fileIO.ReadWrite; |
| 15 |
import jgi.BBMerge; |
| 16 |
import shared.Parse; |
| 17 |
import shared.Parser; |
| 18 |
import shared.PreParser; |
| 19 |
import shared.Primes; |
| 20 |
import shared.Shared; |
| 21 |
import shared.Timer; |
| 22 |
import shared.Tools; |
| 23 |
import shared.TrimRead; |
| 24 |
import stream.ConcurrentReadInputStream; |
| 25 |
import stream.FastaReadInputStream; |
| 26 |
import stream.Read; |
| 27 |
import structures.ByteBuilder; |
| 28 |
import structures.IntList; |
| 29 |
import structures.ListNum; |
| 30 |
import structures.LongList; |
| 31 |
import tracker.ReadStats; |
| 32 |
import ukmer.Kmer; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class KmerTableSet extends AbstractKmerTableSet { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static void main(String[] args){ |
| 48 |
Timer t=new Timer(), t2=new Timer(); |
| 49 |
t.start(); |
| 50 |
t2.start(); |
| 51 |
KmerTableSet set=new KmerTableSet(args); |
| 52 |
t2.stop(); |
| 53 |
outstream.println("Initialization Time: \t"+t2); |
| 54 |
|
| 55 |
|
| 56 |
set.process(t); |
| 57 |
|
| 58 |
|
| 59 |
Shared.closeStream(outstream); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private KmerTableSet(String[] args){ |
| 68 |
this(args, 12); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public KmerTableSet(String[] args, final int bytesPerKmer_){ |
| 77 |
{ |
| 78 |
PreParser pp=new PreParser(args, getClass(), false); |
| 79 |
args=pp.args; |
| 80 |
outstream=pp.outstream; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
Parser parser=new Parser(); |
| 85 |
boolean prealloc_=false; |
| 86 |
int k_=31; |
| 87 |
int ways_=-1; |
| 88 |
int filterMax_=2; |
| 89 |
boolean ecco_=false, merge_=false; |
| 90 |
boolean rcomp_=true; |
| 91 |
double minProb_=defaultMinprob; |
| 92 |
int tableType_=AbstractKmerTable.ARRAY1D; |
| 93 |
|
| 94 |
for(int i=0; i<args.length; i++){ |
| 95 |
|
| 96 |
final String arg=args[i]; |
| 97 |
String[] split=arg.split("="); |
| 98 |
String a=split[0].toLowerCase(); |
| 99 |
String b=split.length>1 ? split[1] : null; |
| 100 |
|
| 101 |
if(Parser.parseCommonStatic(arg, a, b)){ |
| 102 |
|
| 103 |
}else if(Parser.parseZip(arg, a, b)){ |
| 104 |
|
| 105 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 106 |
|
| 107 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 108 |
|
| 109 |
}else if(parser.parseInterleaved(arg, a, b)){ |
| 110 |
|
| 111 |
}else if(parser.parseTrim(arg, a, b)){ |
| 112 |
|
| 113 |
}else if(a.equals("in") || a.equals("in1")){ |
| 114 |
in1.clear(); |
| 115 |
if(b!=null){ |
| 116 |
String[] s=b.split(","); |
| 117 |
for(String ss : s){ |
| 118 |
in1.add(ss); |
| 119 |
} |
| 120 |
} |
| 121 |
}else if(a.equals("in2")){ |
| 122 |
in2.clear(); |
| 123 |
if(b!=null){ |
| 124 |
String[] s=b.split(","); |
| 125 |
for(String ss : s){ |
| 126 |
in2.add(ss); |
| 127 |
} |
| 128 |
} |
| 129 |
}else if(a.equals("extra")){ |
| 130 |
extra.clear(); |
| 131 |
if(b!=null){ |
| 132 |
String[] s=b.split(","); |
| 133 |
for(String ss : s){ |
| 134 |
extra.add(ss); |
| 135 |
} |
| 136 |
} |
| 137 |
}else if(a.equals("append") || a.equals("app")){ |
| 138 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 139 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 140 |
overwrite=Parse.parseBoolean(b); |
| 141 |
}else if(a.equals("initialsize")){ |
| 142 |
initialSize=Parse.parseIntKMG(b); |
| 143 |
}else if(a.equals("showstats") || a.equals("stats")){ |
| 144 |
showStats=Parse.parseBoolean(b); |
| 145 |
}else if(a.equals("ways")){ |
| 146 |
ways_=Parse.parseIntKMG(b); |
| 147 |
}else if(a.equals("buflen") || a.equals("bufflen") || a.equals("bufferlength")){ |
| 148 |
buflen=Parse.parseIntKMG(b); |
| 149 |
}else if(a.equals("k")){ |
| 150 |
assert(b!=null) : "\nk needs an integer value from 1 to 31, such as k=27. Default is 31.\n"; |
| 151 |
k_=Parse.parseIntKMG(b); |
| 152 |
}else if(a.equals("threads") || a.equals("t")){ |
| 153 |
THREADS=(b==null || b.equalsIgnoreCase("auto") ? Shared.threads() : Integer.parseInt(b)); |
| 154 |
}else if(a.equals("showspeed") || a.equals("ss")){ |
| 155 |
showSpeed=Parse.parseBoolean(b); |
| 156 |
}else if(a.equals("ecco")){ |
| 157 |
ecco_=Parse.parseBoolean(b); |
| 158 |
}else if(a.equals("merge")){ |
| 159 |
merge_=Parse.parseBoolean(b); |
| 160 |
}else if(a.equals("verbose")){ |
| 161 |
|
| 162 |
verbose=Parse.parseBoolean(b); |
| 163 |
}else if(a.equals("verbose2")){ |
| 164 |
|
| 165 |
verbose2=Parse.parseBoolean(b); |
| 166 |
}else if(a.equals("minprob")){ |
| 167 |
minProb_=Double.parseDouble(b); |
| 168 |
}else if(a.equals("minprobprefilter") || a.equals("mpp")){ |
| 169 |
minProbPrefilter=Parse.parseBoolean(b); |
| 170 |
}else if(a.equals("minprobmain") || a.equals("mpm")){ |
| 171 |
minProbMain=Parse.parseBoolean(b); |
| 172 |
}else if(a.equals("reads") || a.startsWith("maxreads")){ |
| 173 |
maxReads=Parse.parseKMG(b); |
| 174 |
}else if(a.equals("prealloc") || a.equals("preallocate")){ |
| 175 |
if(b==null || b.length()<1 || Character.isLetter(b.charAt(0))){ |
| 176 |
prealloc_=Parse.parseBoolean(b); |
| 177 |
}else{ |
| 178 |
preallocFraction=Tools.max(0, Double.parseDouble(b)); |
| 179 |
prealloc_=(preallocFraction>0); |
| 180 |
} |
| 181 |
}else if(a.equals("prefilter")){ |
| 182 |
if(b==null || b.length()<1 || !Tools.isDigit(b.charAt(0))){ |
| 183 |
prefilter=Parse.parseBoolean(b); |
| 184 |
}else{ |
| 185 |
filterMax_=Parse.parseIntKMG(b); |
| 186 |
prefilter=filterMax_>0; |
| 187 |
} |
| 188 |
}else if(a.equals("prefiltersize") || a.equals("prefilterfraction") || a.equals("pff")){ |
| 189 |
prefilterFraction=Tools.max(0, Double.parseDouble(b)); |
| 190 |
assert(prefilterFraction<=1) : "prefiltersize must be 0-1, a fraction of total memory."; |
| 191 |
prefilter=prefilterFraction>0; |
| 192 |
}else if(a.equals("prehashes") || a.equals("hashes")){ |
| 193 |
prehashes=Parse.parseIntKMG(b); |
| 194 |
}else if(a.equals("prefilterpasses") || a.equals("prepasses")){ |
| 195 |
assert(b!=null) : "Bad parameter: "+arg; |
| 196 |
if(b.equalsIgnoreCase("auto")){ |
| 197 |
prepasses=-1; |
| 198 |
}else{ |
| 199 |
prepasses=Parse.parseIntKMG(b); |
| 200 |
} |
| 201 |
}else if(a.equals("onepass")){ |
| 202 |
onePass=Parse.parseBoolean(b); |
| 203 |
}else if(a.equals("passes")){ |
| 204 |
int passes=Parse.parseIntKMG(b); |
| 205 |
onePass=(passes<2); |
| 206 |
}else if(a.equals("rcomp")){ |
| 207 |
rcomp_=Parse.parseBoolean(b); |
| 208 |
}else if(a.equals("tabletype")){ |
| 209 |
tableType_=Integer.parseInt(b); |
| 210 |
} |
| 211 |
|
| 212 |
else if(a.equalsIgnoreCase("filterMemoryOverride") || a.equalsIgnoreCase("filterMemory") || |
| 213 |
a.equalsIgnoreCase("prefilterMemory") || a.equalsIgnoreCase("filtermem")){ |
| 214 |
filterMemoryOverride=Parse.parseKMG(b); |
| 215 |
} |
| 216 |
|
| 217 |
else if(IGNORE_UNKNOWN_ARGS){ |
| 218 |
|
| 219 |
}else{ |
| 220 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
{ |
| 225 |
Parser.processQuality(); |
| 226 |
|
| 227 |
qtrimLeft=parser.qtrimLeft; |
| 228 |
qtrimRight=parser.qtrimRight; |
| 229 |
trimq=parser.trimq; |
| 230 |
trimE=parser.trimE(); |
| 231 |
|
| 232 |
minAvgQuality=parser.minAvgQuality; |
| 233 |
minAvgQualityBases=parser.minAvgQualityBases; |
| 234 |
amino=Shared.AMINO_IN; |
| 235 |
if(amino){k_=Tools.min(k_, 12);} |
| 236 |
} |
| 237 |
|
| 238 |
if(prepasses==0 || !prefilter){ |
| 239 |
prepasses=0; |
| 240 |
prefilter=false; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
{ |
| 247 |
long memory=Runtime.getRuntime().maxMemory(); |
| 248 |
double xmsRatio=Shared.xmsRatio(); |
| 249 |
|
| 250 |
usableMemory=(long)Tools.max(((memory-96000000)*(xmsRatio>0.97 ? 0.82 : 0.72)), memory*0.45); |
| 251 |
if(prepasses==0 || !prefilter){ |
| 252 |
filterMemory0=filterMemory1=0; |
| 253 |
}else if(filterMemoryOverride>0){ |
| 254 |
filterMemory0=filterMemory1=filterMemoryOverride; |
| 255 |
}else{ |
| 256 |
double low=Tools.min(prefilterFraction, 1-prefilterFraction); |
| 257 |
double high=1-low; |
| 258 |
if(prepasses<0 || (prepasses&1)==1){ |
| 259 |
filterMemory0=(long)(usableMemory*low); |
| 260 |
filterMemory1=(long)(usableMemory*high); |
| 261 |
}else{ |
| 262 |
filterMemory0=(long)(usableMemory*high); |
| 263 |
filterMemory1=(long)(usableMemory*low); |
| 264 |
} |
| 265 |
} |
| 266 |
tableMemory=(long)(usableMemory*.95-filterMemory0); |
| 267 |
} |
| 268 |
|
| 269 |
tableType=tableType_; |
| 270 |
prealloc=prealloc_; |
| 271 |
bytesPerKmer=bytesPerKmer_; |
| 272 |
if(ways_<1){ |
| 273 |
long maxKmers=(2*tableMemory)/bytesPerKmer; |
| 274 |
long minWays=Tools.min(10000, maxKmers/Integer.MAX_VALUE); |
| 275 |
ways_=(int)Tools.max(31, (int)(Tools.min(96, Shared.threads())*2.5), minWays); |
| 276 |
ways_=(int)Primes.primeAtLeast(ways_); |
| 277 |
assert(ways_>0); |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
onePass=onePass&prefilter; |
| 284 |
ways=ways_; |
| 285 |
filterMax=Tools.min(filterMax_, 0x7FFFFFFF); |
| 286 |
ecco=ecco_; |
| 287 |
merge=merge_; |
| 288 |
minProb=(float)minProb_; |
| 289 |
rcomp=rcomp_; |
| 290 |
|
| 291 |
estimatedKmerCapacity=(long)((tableMemory*1.0/bytesPerKmer)*((prealloc ? preallocFraction : 0.81))*(HashArray.maxLoadFactorFinal*0.97)); |
| 292 |
|
| 293 |
|
| 294 |
KmerCountAbstract.minProb=(minProbPrefilter ? minProb : 0); |
| 295 |
k=k_; |
| 296 |
k2=k-1; |
| 297 |
int bitsPerBase=(amino ? 5 : 2); |
| 298 |
int mask=(amino ? 31 : 3); |
| 299 |
coreMask=(AbstractKmerTableSet.MASK_CORE ? ~(((-1L)<<(bitsPerBase*(k_-1)))|mask) : -1L); |
| 300 |
|
| 301 |
middleMask=AbstractKmerTableSet.MASK_MIDDLE ? AbstractKmerTable.makeMiddleMask(k, amino) : -1L; |
| 302 |
cmMask=coreMask&middleMask; |
| 303 |
|
| 304 |
if(k<1 || k>31){throw new RuntimeException("\nk needs an integer value from 1 to 31, such as k=27. Default is 31.\n");} |
| 305 |
|
| 306 |
if(initialSize<1){ |
| 307 |
final long memOverWays=tableMemory/(bytesPerKmer*ways); |
| 308 |
final double mem2=(prealloc ? preallocFraction : 1)*tableMemory; |
| 309 |
initialSize=(prealloc || memOverWays<initialSizeDefault ? (int)Tools.min(2142000000, (long)(mem2/(bytesPerKmer*ways))) : initialSizeDefault); |
| 310 |
if(initialSize!=initialSizeDefault){ |
| 311 |
System.err.println("Initial size set to "+initialSize); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
assert(FastaReadInputStream.settingsOK()); |
| 318 |
|
| 319 |
if(in1.isEmpty()){ |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
for(int i=0; i<in1.size(); i++){ |
| 324 |
String s=in1.get(i); |
| 325 |
if(s!=null && s.contains("#") && !new File(s).exists()){ |
| 326 |
int pound=s.lastIndexOf('#'); |
| 327 |
String a=s.substring(0, pound); |
| 328 |
String b=s.substring(pound+1); |
| 329 |
in1.set(i, a+1+b); |
| 330 |
in2.add(a+2+b); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if(!extra.isEmpty()){ |
| 335 |
ArrayList<String> temp=(ArrayList<String>) extra.clone(); |
| 336 |
extra.clear(); |
| 337 |
for(String s : temp){ |
| 338 |
if(s!=null && s.contains("#") && !new File(s).exists()){ |
| 339 |
int pound=s.lastIndexOf('#'); |
| 340 |
String a=s.substring(0, pound); |
| 341 |
String b=s.substring(pound+1); |
| 342 |
extra.add(a); |
| 343 |
extra.add(b); |
| 344 |
}else{ |
| 345 |
extra.add(s); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
{ |
| 351 |
boolean allowDuplicates=true; |
| 352 |
if(!Tools.testInputFiles(allowDuplicates, true, in1, in2, extra)){ |
| 353 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 354 |
} |
| 355 |
} |
| 356 |
assert(THREADS>0); |
| 357 |
|
| 358 |
if(DISPLAY_PROGRESS){ |
| 359 |
|
| 360 |
outstream.println("Initial:"); |
| 361 |
outstream.println("Ways="+ways+", initialSize="+initialSize+", prefilter="+(prefilter ? "t" : "f")+", prealloc="+(prealloc ? (""+preallocFraction) : "f")); |
| 362 |
Shared.printMemory(); |
| 363 |
outstream.println(); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
@Override |
| 372 |
public void clear(){ |
| 373 |
tables=null; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
@Override |
| 381 |
public void allocateTables(){ |
| 382 |
assert(tables==null); |
| 383 |
tables=null; |
| 384 |
final long coreMask=(MASK_CORE ? ~(((-1L)<<(2*(k-1)))|3) : -1L); |
| 385 |
|
| 386 |
ScheduleMaker scheduleMaker=new ScheduleMaker(ways, bytesPerKmer, prealloc, |
| 387 |
(prealloc ? preallocFraction : 1.0), -1, (prefilter ? prepasses : 0), prefilterFraction, filterMemoryOverride); |
| 388 |
int[] schedule=scheduleMaker.makeSchedule(); |
| 389 |
|
| 390 |
|
| 391 |
tables=AbstractKmerTable.preallocate(ways, tableType, schedule, coreMask); |
| 392 |
|
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
@Override |
| 400 |
public long loadKmers(String fname1, String fname2){ |
| 401 |
|
| 402 |
|
| 403 |
final ConcurrentReadInputStream cris; |
| 404 |
{ |
| 405 |
FileFormat ff1=FileFormat.testInput(fname1, FileFormat.FASTQ, null, true, true); |
| 406 |
FileFormat ff2=FileFormat.testInput(fname2, FileFormat.FASTQ, null, true, true); |
| 407 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, false, ff1, ff2); |
| 408 |
cris.start(); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
ArrayList<LoadThread> alpt=new ArrayList<LoadThread>(THREADS); |
| 435 |
for(int i=0; i<THREADS; i++){alpt.add(new LoadThread(cris));} |
| 436 |
for(LoadThread pt : alpt){pt.start();} |
| 437 |
|
| 438 |
long added=0; |
| 439 |
|
| 440 |
|
| 441 |
for(LoadThread pt : alpt){ |
| 442 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 443 |
try { |
| 444 |
pt.join(); |
| 445 |
} catch (InterruptedException e) { |
| 446 |
|
| 447 |
e.printStackTrace(); |
| 448 |
} |
| 449 |
} |
| 450 |
added+=pt.added; |
| 451 |
|
| 452 |
readsIn+=pt.readsInT; |
| 453 |
basesIn+=pt.basesInT; |
| 454 |
lowqReads+=pt.lowqReadsT; |
| 455 |
lowqBases+=pt.lowqBasesT; |
| 456 |
readsTrimmed+=pt.readsTrimmedT; |
| 457 |
basesTrimmed+=pt.basesTrimmedT; |
| 458 |
kmersIn+=pt.kmersInT; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
errorState|=ReadWrite.closeStreams(cris); |
| 463 |
return added; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
private class LoadThread extends Thread{ |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public LoadThread(ConcurrentReadInputStream cris_){ |
| 480 |
cris=cris_; |
| 481 |
table=new HashBuffer(tables, buflen, k, false, true); |
| 482 |
|
| 483 |
} |
| 484 |
|
| 485 |
@Override |
| 486 |
public void run(){ |
| 487 |
ListNum<Read> ln=cris.nextList(); |
| 488 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 489 |
|
| 490 |
|
| 491 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 492 |
|
| 493 |
|
| 494 |
for(int i=0; i<reads.size(); i++){ |
| 495 |
Read r1=reads.get(i); |
| 496 |
Read r2=r1.mate; |
| 497 |
|
| 498 |
if(!r1.validated()){r1.validate(true);} |
| 499 |
if(r2!=null && !r2.validated()){r2.validate(true);} |
| 500 |
|
| 501 |
if(verbose){System.err.println("Considering read "+r1.id+" "+new String(r1.bases));} |
| 502 |
|
| 503 |
readsInT++; |
| 504 |
basesInT+=r1.length(); |
| 505 |
if(r2!=null){ |
| 506 |
readsInT++; |
| 507 |
basesInT+=r2.length(); |
| 508 |
} |
| 509 |
|
| 510 |
if(maxNs<Integer.MAX_VALUE){ |
| 511 |
if(r1!=null && r1.countUndefined()>maxNs){r1.setDiscarded(true);} |
| 512 |
if(r2!=null && r2.countUndefined()>maxNs){r2.setDiscarded(true);} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
if(minAvgQuality>0){ |
| 517 |
if(r1!=null && r1.quality!=null && r1.avgQuality(false, minAvgQualityBases)<minAvgQuality){r1.setDiscarded(true);} |
| 518 |
if(r2!=null && r2.quality!=null && r2.avgQuality(false, minAvgQualityBases)<minAvgQuality){r2.setDiscarded(true);} |
| 519 |
} |
| 520 |
|
| 521 |
if(r1!=null){ |
| 522 |
if(qtrimLeft || qtrimRight){ |
| 523 |
int x=TrimRead.trimFast(r1, qtrimLeft, qtrimRight, trimq, trimE, 1, false); |
| 524 |
basesTrimmedT+=x; |
| 525 |
readsTrimmedT+=(x>0 ? 1 : 0); |
| 526 |
} |
| 527 |
if(r1.length()<k){r1.setDiscarded(true);} |
| 528 |
} |
| 529 |
if(r2!=null){ |
| 530 |
if(qtrimLeft || qtrimRight){ |
| 531 |
int x=TrimRead.trimFast(r2, qtrimLeft, qtrimRight, trimq, trimE, 1, false); |
| 532 |
basesTrimmedT+=x; |
| 533 |
readsTrimmedT+=(x>0 ? 1 : 0); |
| 534 |
} |
| 535 |
if(r2.length()<k){r2.setDiscarded(true);} |
| 536 |
} |
| 537 |
|
| 538 |
if((ecco || merge) && r1!=null && r2!=null && !r1.discarded() && !r2.discarded()){ |
| 539 |
if(merge){ |
| 540 |
final int insert=BBMerge.findOverlapStrict(r1, r2, false); |
| 541 |
if(insert>0){ |
| 542 |
r2.reverseComplement(); |
| 543 |
r1=r1.joinRead(insert); |
| 544 |
r2=null; |
| 545 |
} |
| 546 |
}else if(ecco){ |
| 547 |
BBMerge.findOverlapStrict(r1, r2, true); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if(minLen>0){ |
| 552 |
if(r1!=null && r1.length()<minLen){r1.setDiscarded(true);} |
| 553 |
if(r2!=null && r2.length()<minLen){r2.setDiscarded(true);} |
| 554 |
} |
| 555 |
|
| 556 |
if(r1!=null){ |
| 557 |
if(r1.discarded()){ |
| 558 |
lowqBasesT+=r1.length(); |
| 559 |
lowqReadsT++; |
| 560 |
}else{ |
| 561 |
long temp=addKmersToTable(r1); |
| 562 |
added+=temp; |
| 563 |
if(verbose){System.err.println("A: Added "+temp);} |
| 564 |
} |
| 565 |
} |
| 566 |
if(r2!=null){ |
| 567 |
if(r2.discarded()){ |
| 568 |
lowqBasesT+=r2.length(); |
| 569 |
lowqReadsT++; |
| 570 |
}else{ |
| 571 |
long temp=addKmersToTable(r2); |
| 572 |
added+=temp; |
| 573 |
if(verbose){System.err.println("B: Added "+temp);} |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
cris.returnList(ln); |
| 580 |
ln=cris.nextList(); |
| 581 |
reads=(ln!=null ? ln.list : null); |
| 582 |
} |
| 583 |
cris.returnList(ln); |
| 584 |
long temp=table.flush(); |
| 585 |
if(verbose){System.err.println("Flush: Added "+temp);} |
| 586 |
added+=temp; |
| 587 |
} |
| 588 |
|
| 589 |
private final int addKmersToTableAA(final Read r){ |
| 590 |
if(r==null || r.bases==null){return 0;} |
| 591 |
final float minProb2=(minProbMain ? minProb : 0); |
| 592 |
final byte[] bases=r.bases; |
| 593 |
final byte[] quals=r.quality; |
| 594 |
final int shift=5*k; |
| 595 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 596 |
long kmer=0; |
| 597 |
int created=0; |
| 598 |
int len=0; |
| 599 |
|
| 600 |
if(bases==null || bases.length<k){return -1;} |
| 601 |
|
| 602 |
|
| 603 |
float prob=1; |
| 604 |
for(int i=0; i<bases.length; i++){ |
| 605 |
final byte b=bases[i]; |
| 606 |
final long x=AminoAcid.acidToNumber[b]; |
| 607 |
|
| 608 |
|
| 609 |
kmer=((kmer<<5)|x)&mask; |
| 610 |
|
| 611 |
if(minProb2>0 && quals!=null){ |
| 612 |
prob=prob*PROB_CORRECT[quals[i]]; |
| 613 |
if(len>k){ |
| 614 |
byte oldq=quals[i-k]; |
| 615 |
prob=prob*PROB_CORRECT_INVERSE[oldq]; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
if(x<0){ |
| 621 |
len=0; |
| 622 |
kmer=0; |
| 623 |
prob=1; |
| 624 |
}else{len++;} |
| 625 |
|
| 626 |
if(verbose){System.err.println("Scanning i="+i+", len="+len+", kmer="+kmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 627 |
if(len>=k && prob>=minProb2){ |
| 628 |
kmersInT++; |
| 629 |
final long key=kmer; |
| 630 |
if(!prefilter || prefilterArray.read(key)>filterMax2){ |
| 631 |
int temp=table.incrementAndReturnNumCreated(key, 1); |
| 632 |
created+=temp; |
| 633 |
if(verbose){System.err.println("C: Added "+temp);} |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
return created; |
| 639 |
} |
| 640 |
|
| 641 |
private final int addKmersToTable(final Read r){ |
| 642 |
if(amino){return addKmersToTableAA(r);} |
| 643 |
if(onePass){return addKmersToTable_onePass(r);} |
| 644 |
if(r==null || r.bases==null){return 0;} |
| 645 |
final float minProb2=(minProbMain ? minProb : 0); |
| 646 |
final byte[] bases=r.bases; |
| 647 |
final byte[] quals=r.quality; |
| 648 |
final int shift=2*k; |
| 649 |
final int shift2=shift-2; |
| 650 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 651 |
long kmer=0; |
| 652 |
long rkmer=0; |
| 653 |
int created=0; |
| 654 |
int len=0; |
| 655 |
|
| 656 |
if(bases==null || bases.length<k){return -1;} |
| 657 |
|
| 658 |
|
| 659 |
float prob=1; |
| 660 |
for(int i=0; i<bases.length; i++){ |
| 661 |
final byte b=bases[i]; |
| 662 |
final long x=AminoAcid.baseToNumber[b]; |
| 663 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 664 |
|
| 665 |
|
| 666 |
kmer=((kmer<<2)|x)&mask; |
| 667 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 668 |
|
| 669 |
if(minProb2>0 && quals!=null){ |
| 670 |
prob=prob*PROB_CORRECT[quals[i]]; |
| 671 |
if(len>k){ |
| 672 |
byte oldq=quals[i-k]; |
| 673 |
prob=prob*PROB_CORRECT_INVERSE[oldq]; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
if(x<0){ |
| 679 |
len=0; |
| 680 |
kmer=rkmer=0; |
| 681 |
prob=1; |
| 682 |
}else{len++;} |
| 683 |
|
| 684 |
if(verbose){System.err.println("Scanning i="+i+", len="+len+", kmer="+kmer+", rkmer="+rkmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 685 |
if(len>=k && prob>=minProb2){ |
| 686 |
kmersInT++; |
| 687 |
final long key=toValue(kmer, rkmer); |
| 688 |
if(!prefilter || prefilterArray.read(key)>filterMax2){ |
| 689 |
int temp=table.incrementAndReturnNumCreated(key, 1); |
| 690 |
created+=temp; |
| 691 |
if(verbose){System.err.println("C: Added "+temp);} |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
return created; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
private final int addKmersToTable_onePass(final Read r){ |
| 701 |
assert(prefilter); |
| 702 |
if(r==null || r.bases==null){return 0;} |
| 703 |
final byte[] bases=r.bases; |
| 704 |
final byte[] quals=r.quality; |
| 705 |
final int shift=2*k; |
| 706 |
final int shift2=shift-2; |
| 707 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 708 |
long kmer=0; |
| 709 |
long rkmer=0; |
| 710 |
int created=0; |
| 711 |
int len=0; |
| 712 |
|
| 713 |
if(bases==null || bases.length<k){return -1;} |
| 714 |
|
| 715 |
|
| 716 |
float prob=1; |
| 717 |
for(int i=0; i<bases.length; i++){ |
| 718 |
final byte b=bases[i]; |
| 719 |
final long x=AminoAcid.baseToNumber[b]; |
| 720 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 721 |
|
| 722 |
|
| 723 |
kmer=((kmer<<2)|x)&mask; |
| 724 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 725 |
|
| 726 |
if(minProb>0 && quals!=null){ |
| 727 |
prob=prob*PROB_CORRECT[quals[i]]; |
| 728 |
if(len>k){ |
| 729 |
byte oldq=quals[i-k]; |
| 730 |
prob=prob*PROB_CORRECT_INVERSE[oldq]; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
if(x<0){ |
| 736 |
len=0; |
| 737 |
kmer=rkmer=0; |
| 738 |
prob=1; |
| 739 |
}else{len++;} |
| 740 |
|
| 741 |
if(verbose){System.err.println("Scanning i="+i+", len="+len+", kmer="+kmer+", rkmer="+rkmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 742 |
if(len>=k){ |
| 743 |
final long key=toValue(kmer, rkmer); |
| 744 |
int count=prefilterArray.incrementAndReturnUnincremented(key, 1); |
| 745 |
if(count>=filterMax2){ |
| 746 |
int temp=table.incrementAndReturnNumCreated(key, 1); |
| 747 |
created+=temp; |
| 748 |
if(verbose){System.err.println("D: Added "+temp);} |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
return created; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
private final ConcurrentReadInputStream cris; |
| 759 |
|
| 760 |
private final HashBuffer table; |
| 761 |
|
| 762 |
public long added=0; |
| 763 |
|
| 764 |
private long readsInT=0; |
| 765 |
private long basesInT=0; |
| 766 |
private long lowqReadsT=0; |
| 767 |
private long lowqBasesT=0; |
| 768 |
private long readsTrimmedT=0; |
| 769 |
private long basesTrimmedT=0; |
| 770 |
private long kmersInT=0; |
| 771 |
|
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
public void regenerateKmers(byte[] bases, LongList kmers, IntList counts, final int a){ |
| 780 |
final int loc=a+k; |
| 781 |
final int lim=Tools.min(counts.size, a+k+1); |
| 782 |
final int shift=2*k; |
| 783 |
final int shift2=shift-2; |
| 784 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 785 |
long kmer=kmers.get(a); |
| 786 |
long rkmer=rcomp(kmer); |
| 787 |
int len=k; |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
for(int i=loc, j=a+1; j<lim; i++, j++){ |
| 793 |
final byte b=bases[i]; |
| 794 |
final long x=AminoAcid.baseToNumber[b]; |
| 795 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 796 |
kmer=((kmer<<2)|x)&mask; |
| 797 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 798 |
if(x<0){ |
| 799 |
len=0; |
| 800 |
kmer=rkmer=0; |
| 801 |
}else{len++;} |
| 802 |
|
| 803 |
if(len>=k){ |
| 804 |
assert(kmers.get(j)!=kmer); |
| 805 |
kmers.set(j, kmer); |
| 806 |
int count=getCount(kmer, rkmer); |
| 807 |
counts.set(j, count); |
| 808 |
}else{ |
| 809 |
kmers.set(j, -1); |
| 810 |
counts.set(j, 0); |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
@Override |
| 816 |
public int regenerateCounts(byte[] bases, IntList counts, final Kmer dummy, BitSet changed){ |
| 817 |
assert(!changed.isEmpty()); |
| 818 |
final int firstBase=changed.nextSetBit(0), lastBase=changed.length()-1; |
| 819 |
final int ca=firstBase-k; |
| 820 |
|
| 821 |
final int firstCount=Tools.max(firstBase-k+1, 0), lastCount=Tools.min(counts.size-1, lastBase); |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
final int shift=2*k; |
| 826 |
final int shift2=shift-2; |
| 827 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 828 |
long kmer=0, rkmer=0; |
| 829 |
int len=0; |
| 830 |
int valid=0; |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
for(int i=Tools.max(0, firstBase-k+1), lim=Tools.min(lastBase+k-1, bases.length-1); i<=lim; i++){ |
| 835 |
final byte base=bases[i]; |
| 836 |
final long x=AminoAcid.baseToNumber[base]; |
| 837 |
final long x2=AminoAcid.baseToComplementNumber[base]; |
| 838 |
kmer=((kmer<<2)|x)&mask; |
| 839 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 840 |
|
| 841 |
if(x<0){ |
| 842 |
len=0; |
| 843 |
kmer=rkmer=0; |
| 844 |
}else{ |
| 845 |
len++; |
| 846 |
} |
| 847 |
|
| 848 |
final int c=i-k+1; |
| 849 |
if(i>=firstBase){ |
| 850 |
if(len>=k){ |
| 851 |
valid++; |
| 852 |
int count=getCount(kmer, rkmer); |
| 853 |
counts.set(c, count); |
| 854 |
}else if(c>=0){ |
| 855 |
counts.set(c, 0); |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
return valid; |
| 861 |
} |
| 862 |
|
| 863 |
@Override |
| 864 |
public int fillSpecificCounts(byte[] bases, IntList counts, BitSet positions, final Kmer dummy){ |
| 865 |
final int b=k-1; |
| 866 |
final int lim=(positions==null ? bases.length : Tools.min(bases.length, positions.length()+k-1)); |
| 867 |
final int shift=2*k; |
| 868 |
final int shift2=shift-2; |
| 869 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 870 |
long kmer=0, rkmer=0; |
| 871 |
int len=0; |
| 872 |
int valid=0; |
| 873 |
|
| 874 |
counts.clear(); |
| 875 |
|
| 876 |
for(int i=0, j=-b; i<lim; i++, j++){ |
| 877 |
final byte base=bases[i]; |
| 878 |
final long x=AminoAcid.baseToNumber[base]; |
| 879 |
final long x2=AminoAcid.baseToComplementNumber[base]; |
| 880 |
kmer=((kmer<<2)|x)&mask; |
| 881 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 882 |
|
| 883 |
if(x<0){ |
| 884 |
len=0; |
| 885 |
kmer=rkmer=0; |
| 886 |
}else{ |
| 887 |
len++; |
| 888 |
} |
| 889 |
|
| 890 |
if(j>=0){ |
| 891 |
if(len>=k && (positions==null || positions.get(j))){ |
| 892 |
valid++; |
| 893 |
int count=getCount(kmer, rkmer); |
| 894 |
assert(i-k+1==counts.size()) : "j="+j+", counts="+counts.size()+", b="+(b)+", (i-k+1)="+(i-k+1); |
| 895 |
assert(j==counts.size()); |
| 896 |
counts.add(Tools.max(count, 0)); |
| 897 |
|
| 898 |
}else{ |
| 899 |
counts.add(0); |
| 900 |
|
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
assert((counts.size==0 && bases.length<k) || counts.size==lim-k+1) : bases.length+", "+k+", "+counts.size; |
| 907 |
|
| 908 |
return valid; |
| 909 |
} |
| 910 |
|
| 911 |
public int regenerateCounts(byte[] bases, IntList counts, final int ca){ |
| 912 |
final int b=ca+k-1; |
| 913 |
final int lim=Tools.min(bases.length, b+k+1); |
| 914 |
final int shift=2*k; |
| 915 |
final int shift2=shift-2; |
| 916 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 917 |
long kmer=0, rkmer=0; |
| 918 |
int len=0; |
| 919 |
int valid=0; |
| 920 |
|
| 921 |
final int clen=counts.size; |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
for(int i=b-k+1; i<lim; i++){ |
| 926 |
final byte base=bases[i]; |
| 927 |
final long x=AminoAcid.baseToNumber[base]; |
| 928 |
final long x2=AminoAcid.baseToComplementNumber[base]; |
| 929 |
kmer=((kmer<<2)|x)&mask; |
| 930 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 931 |
|
| 932 |
if(x<0){ |
| 933 |
len=0; |
| 934 |
kmer=rkmer=0; |
| 935 |
}else{ |
| 936 |
len++; |
| 937 |
} |
| 938 |
|
| 939 |
if(i>=b){ |
| 940 |
if(len>=k){ |
| 941 |
valid++; |
| 942 |
int count=getCount(kmer, rkmer); |
| 943 |
counts.set(i-k+1, count); |
| 944 |
}else{ |
| 945 |
counts.set(i-k+1, 0); |
| 946 |
} |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
assert((counts.size==0 && bases.length<k) || counts.size==bases.length-k+1) : bases.length+", "+k+", "+counts.size; |
| 951 |
assert(clen==counts.size); |
| 952 |
|
| 953 |
return valid; |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
public int fillKmers(byte[] bases, LongList kmers){ |
| 958 |
final int blen=bases.length; |
| 959 |
if(blen<k){return 0;} |
| 960 |
final int min=k-1; |
| 961 |
final int shift=2*k; |
| 962 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 963 |
long kmer=0; |
| 964 |
int len=0; |
| 965 |
int valid=0; |
| 966 |
|
| 967 |
kmers.clear(); |
| 968 |
|
| 969 |
|
| 970 |
for(int i=0; i<blen; i++){ |
| 971 |
final byte b=bases[i]; |
| 972 |
assert(b>=0) : Arrays.toString(bases); |
| 973 |
final long x=AminoAcid.baseToNumber[b]; |
| 974 |
kmer=((kmer<<2)|x)&mask; |
| 975 |
if(x<0){ |
| 976 |
len=0; |
| 977 |
kmer=0; |
| 978 |
}else{len++;} |
| 979 |
if(i>=min){ |
| 980 |
if(len>=k){ |
| 981 |
kmers.add(kmer); |
| 982 |
valid++; |
| 983 |
}else{ |
| 984 |
kmers.add(-1); |
| 985 |
} |
| 986 |
} |
| 987 |
} |
| 988 |
return valid; |
| 989 |
} |
| 990 |
|
| 991 |
public void fillCounts(LongList kmers, IntList counts){ |
| 992 |
counts.clear(); |
| 993 |
for(int i=0; i<kmers.size; i++){ |
| 994 |
long kmer=kmers.get(i); |
| 995 |
if(kmer>=0){ |
| 996 |
long rkmer=rcomp(kmer); |
| 997 |
int count=getCount(kmer, rkmer); |
| 998 |
counts.add(count); |
| 999 |
}else{ |
| 1000 |
counts.add(0); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
@Override |
| 1011 |
public long regenerate(final int limit){ |
| 1012 |
long sum=0; |
| 1013 |
for(AbstractKmerTable akt : tables){ |
| 1014 |
sum+=akt.regenerate(limit); |
| 1015 |
} |
| 1016 |
return sum; |
| 1017 |
} |
| 1018 |
|
| 1019 |
public HashArray1D getTableForKey(long key){ |
| 1020 |
return (HashArray1D) tables[kmerToWay(key)]; |
| 1021 |
} |
| 1022 |
|
| 1023 |
@Override |
| 1024 |
public HashArray1D getTable(int tnum){ |
| 1025 |
return (HashArray1D) tables[tnum]; |
| 1026 |
} |
| 1027 |
|
| 1028 |
@Override |
| 1029 |
public long[] fillHistogram(int histMax) { |
| 1030 |
return HistogramMaker.fillHistogram(tables, histMax); |
| 1031 |
} |
| 1032 |
|
| 1033 |
@Override |
| 1034 |
public void countGC(long[] gcCounts, int max) { |
| 1035 |
for(AbstractKmerTable set : tables){ |
| 1036 |
set.countGC(gcCounts, max); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
@Override |
| 1041 |
public void initializeOwnership(){ |
| 1042 |
OwnershipThread.initialize(tables); |
| 1043 |
} |
| 1044 |
|
| 1045 |
@Override |
| 1046 |
public void clearOwnership(){ |
| 1047 |
OwnershipThread.clear(tables); |
| 1048 |
} |
| 1049 |
|
| 1050 |
public long rightmostKmer(final ByteBuilder bb){ |
| 1051 |
return rightmostKmer(bb.array, bb.length()); |
| 1052 |
} |
| 1053 |
|
| 1054 |
public long rightmostKmer(final byte[] bases, final int blen){ |
| 1055 |
if(blen<k){return -1;} |
| 1056 |
final int shift=2*k; |
| 1057 |
final int shift2=shift-2; |
| 1058 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1059 |
long kmer=0; |
| 1060 |
long rkmer=0; |
| 1061 |
int len=0; |
| 1062 |
|
| 1063 |
|
| 1064 |
{ |
| 1065 |
for(int i=blen-k; i<blen; i++){ |
| 1066 |
final byte b=bases[i]; |
| 1067 |
final long x=AminoAcid.baseToNumber[b]; |
| 1068 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1069 |
kmer=((kmer<<2)|x)&mask; |
| 1070 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1071 |
if(x<0){ |
| 1072 |
len=0; |
| 1073 |
kmer=rkmer=0; |
| 1074 |
}else{len++;} |
| 1075 |
if(verbose){outstream.println("Scanning i="+i+", len="+len+", kmer="+kmer+", rkmer="+rkmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
if(len<k){return -1;} |
| 1080 |
else{assert(len==k);} |
| 1081 |
return kmer; |
| 1082 |
} |
| 1083 |
|
| 1084 |
public long leftmostKmer(final ByteBuilder bb){ |
| 1085 |
return leftmostKmer(bb.array, bb.length()); |
| 1086 |
} |
| 1087 |
|
| 1088 |
public long leftmostKmer(final byte[] bases, final int blen){ |
| 1089 |
if(blen<k){return -1;} |
| 1090 |
final int shift=2*k; |
| 1091 |
final int shift2=shift-2; |
| 1092 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1093 |
long kmer=0; |
| 1094 |
long rkmer=0; |
| 1095 |
int len=0; |
| 1096 |
|
| 1097 |
|
| 1098 |
{ |
| 1099 |
for(int i=0; i<k; i++){ |
| 1100 |
final byte b=bases[i]; |
| 1101 |
final long x=AminoAcid.baseToNumber[b]; |
| 1102 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1103 |
kmer=((kmer<<2)|x)&mask; |
| 1104 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1105 |
if(x<0){ |
| 1106 |
len=0; |
| 1107 |
kmer=rkmer=0; |
| 1108 |
}else{len++;} |
| 1109 |
if(verbose){outstream.println("Scanning i="+i+", len="+len+", kmer="+kmer+", rkmer="+rkmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
if(len<k){return -1;} |
| 1114 |
else{assert(len==k);} |
| 1115 |
return kmer; |
| 1116 |
} |
| 1117 |
|
| 1118 |
public boolean doubleClaim(final ByteBuilder bb, final int id){ |
| 1119 |
return doubleClaim(bb.array, bb.length(), id); |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
public boolean doubleClaim(final byte[] bases, final int blength, final int id){ |
| 1124 |
boolean success=claim(bases, blength, id, true); |
| 1125 |
if(verbose){outstream.println("success1="+success+", id="+id+", blength="+blength);} |
| 1126 |
if(!success){return false;} |
| 1127 |
success=claim(bases, blength, id+CLAIM_OFFSET, true); |
| 1128 |
if(verbose){outstream.println("success2="+success+", id="+id+", blength="+blength);} |
| 1129 |
return success; |
| 1130 |
} |
| 1131 |
|
| 1132 |
public boolean claim(final ByteBuilder bb, final int id, final boolean exitEarly){ |
| 1133 |
return claim(bb.array, bb.length(), id, exitEarly); |
| 1134 |
} |
| 1135 |
|
| 1136 |
public float calcCoverage(final byte[] bases, final int blength){ |
| 1137 |
final int shift=2*k; |
| 1138 |
final int shift2=shift-2; |
| 1139 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1140 |
long kmer=0, rkmer=0; |
| 1141 |
int len=0; |
| 1142 |
long sum=0, max=0; |
| 1143 |
int kmers=0; |
| 1144 |
|
| 1145 |
|
| 1146 |
for(int i=0; i<blength; i++){ |
| 1147 |
final byte b=bases[i]; |
| 1148 |
final long x=AminoAcid.baseToNumber[b]; |
| 1149 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1150 |
kmer=((kmer<<2)|x)&mask; |
| 1151 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1152 |
if(x<0){ |
| 1153 |
len=0; |
| 1154 |
kmer=rkmer=0; |
| 1155 |
}else{len++;} |
| 1156 |
if(len>=k){ |
| 1157 |
int count=getCount(kmer, rkmer); |
| 1158 |
sum+=count; |
| 1159 |
max=Tools.max(count, max); |
| 1160 |
kmers++; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
return sum==0 ? 0 : sum/(float)kmers; |
| 1164 |
} |
| 1165 |
|
| 1166 |
public float calcCoverage(final Contig contig){ |
| 1167 |
final byte[] bases=contig.bases; |
| 1168 |
if(bases.length<k){return 0;} |
| 1169 |
final int shift=2*k; |
| 1170 |
final int shift2=shift-2; |
| 1171 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1172 |
long kmer=0, rkmer=0; |
| 1173 |
int len=0; |
| 1174 |
long sum=0; |
| 1175 |
int max=0, min=Integer.MAX_VALUE; |
| 1176 |
int kmers=0; |
| 1177 |
|
| 1178 |
|
| 1179 |
for(int i=0; i<bases.length; i++){ |
| 1180 |
final byte b=bases[i]; |
| 1181 |
final long x=AminoAcid.baseToNumber[b]; |
| 1182 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1183 |
kmer=((kmer<<2)|x)&mask; |
| 1184 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1185 |
if(x<0){ |
| 1186 |
len=0; |
| 1187 |
kmer=rkmer=0; |
| 1188 |
}else{len++;} |
| 1189 |
if(len>=k){ |
| 1190 |
int count=getCount(kmer, rkmer); |
| 1191 |
sum+=count; |
| 1192 |
max=Tools.max(count, max); |
| 1193 |
min=Tools.min(count, min); |
| 1194 |
kmers++; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
contig.coverage=sum==0 ? 0 : sum/(float)kmers; |
| 1198 |
contig.maxCov=max; |
| 1199 |
contig.minCov=sum==0 ? 0 : min; |
| 1200 |
return contig.coverage; |
| 1201 |
} |
| 1202 |
|
| 1203 |
public boolean claim(final byte[] bases, final int blength, final int id, boolean exitEarly){ |
| 1204 |
if(verbose){outstream.println("Thread "+id+" claim start.");} |
| 1205 |
final int shift=2*k; |
| 1206 |
final int shift2=shift-2; |
| 1207 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1208 |
long kmer=0, rkmer=0; |
| 1209 |
int len=0; |
| 1210 |
boolean success=true; |
| 1211 |
|
| 1212 |
for(int i=0; i<blength && success; i++){ |
| 1213 |
final byte b=bases[i]; |
| 1214 |
final long x=AminoAcid.baseToNumber[b]; |
| 1215 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1216 |
kmer=((kmer<<2)|x)&mask; |
| 1217 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1218 |
if(x<0){ |
| 1219 |
len=0; |
| 1220 |
kmer=rkmer=0; |
| 1221 |
}else{len++;} |
| 1222 |
if(verbose){System.err.println("Scanning i="+i+", len="+len+", kmer="+kmer+", rkmer="+rkmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 1223 |
if(len>=k){ |
| 1224 |
success=claim(kmer, rkmer, id); |
| 1225 |
success=(success || !exitEarly); |
| 1226 |
} |
| 1227 |
} |
| 1228 |
return success; |
| 1229 |
} |
| 1230 |
|
| 1231 |
public boolean claim(final long kmer, final long rkmer, final int id){ |
| 1232 |
|
| 1233 |
final long key=toValue(kmer, rkmer); |
| 1234 |
final int way=kmerToWay(key); |
| 1235 |
final AbstractKmerTable table=tables[way]; |
| 1236 |
final int count=table.getValue(key); |
| 1237 |
assert(count==-1 || count>0) : count; |
| 1238 |
|
| 1239 |
if(count<0){return true;} |
| 1240 |
assert(count>0) : count; |
| 1241 |
final int owner=table.setOwner(key, id); |
| 1242 |
if(verbose){outstream.println("owner="+owner+".");} |
| 1243 |
|
| 1244 |
return owner==id; |
| 1245 |
} |
| 1246 |
|
| 1247 |
public void release(ByteBuilder bb, final int id){ |
| 1248 |
release(bb.array, bb.length(), id); |
| 1249 |
} |
| 1250 |
|
| 1251 |
public void release(final byte[] bases, final int blength, final int id){ |
| 1252 |
if(verbose ){outstream.println("*Thread "+id+" release start.");} |
| 1253 |
final int shift=2*k; |
| 1254 |
final int shift2=shift-2; |
| 1255 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1256 |
long kmer=0, rkmer=0; |
| 1257 |
int len=0; |
| 1258 |
|
| 1259 |
for(int i=0; i<blength; i++){ |
| 1260 |
final byte b=bases[i]; |
| 1261 |
final long x=AminoAcid.baseToNumber[b]; |
| 1262 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1263 |
kmer=((kmer<<2)|x)&mask; |
| 1264 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1265 |
if(x<0){ |
| 1266 |
len=0; |
| 1267 |
kmer=rkmer=0; |
| 1268 |
}else{len++;} |
| 1269 |
if(verbose){System.err.println("Scanning i="+i+", len="+len+", kmer="+kmer+", rkmer="+rkmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 1270 |
if(len>=k){ |
| 1271 |
release(kmer, rkmer, id); |
| 1272 |
} |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
public boolean release(final long kmer, final long rkmer, final int id){ |
| 1277 |
return release(toValue(kmer, rkmer), id); |
| 1278 |
} |
| 1279 |
|
| 1280 |
public boolean release(final long key, final int id){ |
| 1281 |
final int way=kmerToWay(key); |
| 1282 |
final AbstractKmerTable table=tables[way]; |
| 1283 |
final int count=table.getValue(key); |
| 1284 |
|
| 1285 |
if(count<1){return true;} |
| 1286 |
return table.clearOwner(key, id); |
| 1287 |
} |
| 1288 |
|
| 1289 |
public int findOwner(ByteBuilder bb, final int id){ |
| 1290 |
return findOwner(bb.array, bb.length(), id); |
| 1291 |
} |
| 1292 |
|
| 1293 |
public int findOwner(final byte[] bases, final int blength, final int id){ |
| 1294 |
final int shift=2*k; |
| 1295 |
final int shift2=shift-2; |
| 1296 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1297 |
long kmer=0, rkmer=0; |
| 1298 |
int len=0; |
| 1299 |
int maxOwner=-1; |
| 1300 |
|
| 1301 |
for(int i=0; i<blength; i++){ |
| 1302 |
final byte b=bases[i]; |
| 1303 |
final long x=AminoAcid.baseToNumber[b]; |
| 1304 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1305 |
kmer=((kmer<<2)|x)&mask; |
| 1306 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1307 |
if(x<0){ |
| 1308 |
len=0; |
| 1309 |
kmer=rkmer=0; |
| 1310 |
}else{len++;} |
| 1311 |
if(verbose){System.err.println("Scanning i="+i+", len="+len+", kmer="+kmer+", rkmer="+rkmer+"\t"+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 1312 |
if(len>=k){ |
| 1313 |
int owner=findOwner(kmer, rkmer); |
| 1314 |
maxOwner=Tools.max(owner, maxOwner); |
| 1315 |
if(maxOwner>id){break;} |
| 1316 |
} |
| 1317 |
} |
| 1318 |
return maxOwner; |
| 1319 |
} |
| 1320 |
|
| 1321 |
public int findOwner(final long kmer){ |
| 1322 |
return findOwner(kmer, rcomp(kmer)); |
| 1323 |
} |
| 1324 |
|
| 1325 |
public int findOwner(final long kmer, final long rkmer){ |
| 1326 |
final long key=toValue(kmer, rkmer); |
| 1327 |
final int way=kmerToWay(key); |
| 1328 |
final AbstractKmerTable table=tables[way]; |
| 1329 |
final int count=table.getValue(key); |
| 1330 |
if(count<0){return -1;} |
| 1331 |
final int owner=table.getOwner(key); |
| 1332 |
return owner; |
| 1333 |
} |
| 1334 |
|
| 1335 |
public int getCount(long kmer, long rkmer){ |
| 1336 |
long key=toValue(kmer, rkmer); |
| 1337 |
int way=kmerToWay(key); |
| 1338 |
return tables[way].getValue(key); |
| 1339 |
} |
| 1340 |
|
| 1341 |
public int getCount(long key){ |
| 1342 |
int way=kmerToWay(key); |
| 1343 |
return tables[way].getValue(key); |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
public int fillRightCounts(long kmer, long rkmer, int[] counts, long mask, int shift2){ |
| 1351 |
if(FAST_FILL && MASK_CORE && k>2){ |
| 1352 |
return fillRightCounts_fast(kmer, rkmer, counts, mask, shift2); |
| 1353 |
}else{ |
| 1354 |
return fillRightCounts_safe(kmer, rkmer, counts, mask, shift2); |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
public int fillRightCounts_fast(final long kmer0, final long rkmer0, int[] counts, |
| 1359 |
long mask, int shift2){ |
| 1360 |
|
| 1361 |
assert(MASK_CORE); |
| 1362 |
final long kmer=(kmer0<<2)&mask; |
| 1363 |
final long rkmer=(rkmer0>>>2); |
| 1364 |
final long a=kmer&coreMask, b=rkmer&coreMask; |
| 1365 |
|
| 1366 |
int max=-1, maxPos=0; |
| 1367 |
if(a==b){ |
| 1368 |
return fillRightCounts_safe(kmer0, rkmer0, counts, mask, shift2); |
| 1369 |
}else if(a>b){ |
| 1370 |
|
| 1371 |
final long core=a; |
| 1372 |
final int way=kmerToWay(core); |
| 1373 |
|
| 1374 |
final HashArray table=(HashArray) tables[way]; |
| 1375 |
final int cell=table.kmerToCell(kmer); |
| 1376 |
for(int i=0; i<=3; i++){ |
| 1377 |
final long key=kmer|i; |
| 1378 |
final int count=Tools.max(0, table.getValue(key, cell)); |
| 1379 |
|
| 1380 |
counts[i]=count; |
| 1381 |
if(count>max){ |
| 1382 |
max=count; |
| 1383 |
maxPos=i; |
| 1384 |
} |
| 1385 |
} |
| 1386 |
}else{ |
| 1387 |
|
| 1388 |
|
| 1389 |
final long core=b; |
| 1390 |
final int way=kmerToWay(core); |
| 1391 |
|
| 1392 |
final HashArray table=(HashArray) tables[way]; |
| 1393 |
final int cell=table.kmerToCell(rkmer); |
| 1394 |
final long incr=1L<<(2*(k-1)); |
| 1395 |
long j=3*incr; |
| 1396 |
for(int i=0; i<=3; i++, j-=incr){ |
| 1397 |
final long key=rkmer|j; |
| 1398 |
final int count=Tools.max(0, table.getValue(key, cell)); |
| 1399 |
|
| 1400 |
counts[i]=count; |
| 1401 |
if(count>max){ |
| 1402 |
max=count; |
| 1403 |
maxPos=i; |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
return maxPos; |
| 1408 |
} |
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
public int fillRightCounts_safe(long kmer, long rkmer, int[] counts, long mask, int shift2){ |
| 1414 |
assert(kmer==rcomp(rkmer)); |
| 1415 |
if(verbose){outstream.println("fillRightCounts: "+toText(kmer)+", "+toText(rkmer));} |
| 1416 |
kmer=(kmer<<2)&mask; |
| 1417 |
rkmer=(rkmer>>>2); |
| 1418 |
int max=-1, maxPos=0; |
| 1419 |
|
| 1420 |
for(int i=0; i<=3; i++){ |
| 1421 |
long kmer2=kmer|((long)i); |
| 1422 |
long rkmer2=rkmer|(((long)AminoAcid.numberToComplement[i])<<shift2); |
| 1423 |
if(verbose){outstream.println("kmer: "+toText(kmer2)+", "+toText(rkmer2));} |
| 1424 |
assert(kmer2==(kmer2&mask)); |
| 1425 |
assert(rkmer2==(rkmer2&mask)); |
| 1426 |
assert(kmer2==rcomp(rkmer2)); |
| 1427 |
long key=toValue(kmer2, rkmer2); |
| 1428 |
int way=kmerToWay(key); |
| 1429 |
int count=tables[way].getValue(key); |
| 1430 |
assert(count==NOT_PRESENT || count>=0); |
| 1431 |
count=Tools.max(count, 0); |
| 1432 |
counts[i]=count; |
| 1433 |
if(count>max){ |
| 1434 |
max=count; |
| 1435 |
maxPos=i; |
| 1436 |
} |
| 1437 |
} |
| 1438 |
return maxPos; |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
public int fillRightCountsRcompOnly(long kmer, long rkmer, int[] counts, long mask, int shift2){ |
| 1443 |
assert(kmer==rcomp(rkmer)); |
| 1444 |
if(verbose){outstream.println("fillRightCounts: "+toText(kmer)+", "+toText(rkmer));} |
| 1445 |
kmer=(kmer<<2)&mask; |
| 1446 |
rkmer=(rkmer>>>2); |
| 1447 |
int max=-1, maxPos=0; |
| 1448 |
|
| 1449 |
for(int i=0; i<=3; i++){ |
| 1450 |
long kmer2=kmer|((long)i); |
| 1451 |
long rkmer2=rkmer|(((long)AminoAcid.numberToComplement[i])<<shift2); |
| 1452 |
if(verbose){outstream.println("kmer: "+toText(kmer2)+", "+toText(rkmer2));} |
| 1453 |
assert(kmer2==(kmer2&mask)); |
| 1454 |
assert(rkmer2==(rkmer2&mask)); |
| 1455 |
assert(kmer2==rcomp(rkmer2)); |
| 1456 |
long key=rkmer2; |
| 1457 |
int way=kmerToWay(key); |
| 1458 |
int count=tables[way].getValue(key); |
| 1459 |
assert(count==NOT_PRESENT || count>=0); |
| 1460 |
count=Tools.max(count, 0); |
| 1461 |
counts[i]=count; |
| 1462 |
if(count>max){ |
| 1463 |
max=count; |
| 1464 |
maxPos=i; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
return maxPos; |
| 1468 |
} |
| 1469 |
|
| 1470 |
public int fillLeftCounts(long kmer, long rkmer, int[] counts, long mask, int shift2){ |
| 1471 |
if(FAST_FILL && MASK_CORE && k>2){ |
| 1472 |
return fillLeftCounts_fast(kmer, rkmer, counts, mask, shift2); |
| 1473 |
}else{ |
| 1474 |
return fillLeftCounts_safe(kmer, rkmer, counts, mask, shift2); |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
public int fillLeftCounts_fast(final long kmer0, final long rkmer0, int[] counts, |
| 1479 |
long mask, int shift2){ |
| 1480 |
|
| 1481 |
assert(MASK_CORE); |
| 1482 |
final long rkmer=(rkmer0<<2)&mask; |
| 1483 |
final long kmer=(kmer0>>>2); |
| 1484 |
final long a=kmer&coreMask, b=rkmer&coreMask; |
| 1485 |
|
| 1486 |
int max=-1, maxPos=0; |
| 1487 |
if(a==b){ |
| 1488 |
return fillLeftCounts_safe(kmer0, rkmer0, counts, mask, shift2); |
| 1489 |
}else if(a>b){ |
| 1490 |
|
| 1491 |
|
| 1492 |
final long core=a; |
| 1493 |
final int way=kmerToWay(core); |
| 1494 |
|
| 1495 |
final HashArray table=(HashArray) tables[way]; |
| 1496 |
final int cell=table.kmerToCell(kmer); |
| 1497 |
final long incr=1L<<(2*(k-1)); |
| 1498 |
long j=0; |
| 1499 |
for(int i=0; i<=3; i++, j+=incr){ |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
final long key=kmer|j; |
| 1508 |
final int count=Tools.max(0, table.getValue(key, cell)); |
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
counts[i]=count; |
| 1518 |
if(count>max){ |
| 1519 |
max=count; |
| 1520 |
maxPos=i; |
| 1521 |
} |
| 1522 |
} |
| 1523 |
return maxPos; |
| 1524 |
}else{ |
| 1525 |
|
| 1526 |
final long core=b; |
| 1527 |
final int way=kmerToWay(core); |
| 1528 |
|
| 1529 |
final HashArray table=(HashArray) tables[way]; |
| 1530 |
final int cell=table.kmerToCell(rkmer); |
| 1531 |
for(int i=0, j=3; i<=3; i++, j--){ |
| 1532 |
final long key=rkmer|j; |
| 1533 |
final int count=Tools.max(0, table.getValue(key, cell)); |
| 1534 |
|
| 1535 |
|
| 1536 |
counts[i]=count; |
| 1537 |
if(count>max){ |
| 1538 |
max=count; |
| 1539 |
maxPos=i; |
| 1540 |
} |
| 1541 |
} |
| 1542 |
return maxPos; |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
public int fillLeftCounts_safe(final long kmer0, final long rkmer0, int[] counts, long mask, int shift2){ |
| 1547 |
assert(kmer0==rcomp(rkmer0)); |
| 1548 |
if(verbose){outstream.println("fillLeftCounts: "+toText(kmer0)+", "+toText(rkmer0));} |
| 1549 |
long rkmer=(rkmer0<<2)&mask; |
| 1550 |
long kmer=(kmer0>>>2); |
| 1551 |
int max=-1, maxPos=0; |
| 1552 |
|
| 1553 |
for(int i=0; i<=3; i++){ |
| 1554 |
final long rkmer2=rkmer|((long)AminoAcid.numberToComplement[i]); |
| 1555 |
final long kmer2=kmer|(((long)i)<<shift2); |
| 1556 |
if(verbose){outstream.println("kmer: "+toText(kmer2)+", "+toText(rkmer2));} |
| 1557 |
assert(kmer2==(kmer2&mask)); |
| 1558 |
assert(rkmer2==(rkmer2&mask)); |
| 1559 |
assert(kmer2==rcomp(rkmer2)) : "\n"+"kmer: \t"+toText(rcomp(rkmer2))+", "+toText(rcomp(kmer2)); |
| 1560 |
long key=toValue(rkmer2, kmer2); |
| 1561 |
int way=kmerToWay(key); |
| 1562 |
int count=tables[way].getValue(key); |
| 1563 |
assert(count==NOT_PRESENT || count>=0); |
| 1564 |
count=Tools.max(count, 0); |
| 1565 |
counts[i]=count; |
| 1566 |
if(count>max){ |
| 1567 |
max=count; |
| 1568 |
maxPos=i; |
| 1569 |
} |
| 1570 |
} |
| 1571 |
return maxPos; |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
@Override |
| 1579 |
public boolean dumpKmersAsBytes(String fname, int mincount, int maxcount, boolean printTime, AtomicLong remaining){ |
| 1580 |
if(fname==null){return false;} |
| 1581 |
Timer t=new Timer(); |
| 1582 |
|
| 1583 |
ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, append, true); |
| 1584 |
bsw.start(); |
| 1585 |
for(AbstractKmerTable set : tables){ |
| 1586 |
set.dumpKmersAsBytes(bsw, k, mincount, maxcount, remaining); |
| 1587 |
} |
| 1588 |
bsw.poisonAndWait(); |
| 1589 |
|
| 1590 |
t.stop(); |
| 1591 |
if(printTime){outstream.println("Kmer Dump Time: \t"+t);} |
| 1592 |
return bsw.errorState; |
| 1593 |
} |
| 1594 |
|
| 1595 |
@Override |
| 1596 |
public boolean dumpKmersAsBytes_MT(String fname, int mincount, int maxcount, boolean printTime, AtomicLong remaining){ |
| 1597 |
final int threads=Tools.min(Shared.threads(), tables.length); |
| 1598 |
if(threads<3 || DumpThread.NUM_THREADS==1){return dumpKmersAsBytes(fname, mincount, maxcount, printTime, remaining);} |
| 1599 |
|
| 1600 |
if(fname==null){return false;} |
| 1601 |
Timer t=new Timer(); |
| 1602 |
|
| 1603 |
ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, append, true); |
| 1604 |
bsw.start(); |
| 1605 |
DumpThread.dump(k, mincount, maxcount, tables, bsw, remaining); |
| 1606 |
bsw.poisonAndWait(); |
| 1607 |
|
| 1608 |
t.stop(); |
| 1609 |
if(printTime){outstream.println("Kmer Dump Time: \t"+t);} |
| 1610 |
return bsw.errorState; |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
private final long rcomp(long kmer){return AminoAcid.reverseComplementBinaryFast(kmer, k);} |
| 1618 |
private final StringBuilder toText(long kmer){return AbstractKmerTable.toText(kmer, k);} |
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
public final long toValue(long kmer, long rkmer){ |
| 1631 |
|
| 1632 |
|
| 1633 |
if(!rcomp){return kmer&middleMask;} |
| 1634 |
final long a=kmer&cmMask, b=rkmer&cmMask; |
| 1635 |
kmer&=middleMask; |
| 1636 |
rkmer&=middleMask; |
| 1637 |
return a>b ? kmer : a<b ? rkmer : Tools.max(kmer, rkmer); |
| 1638 |
} |
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
@Override |
| 1645 |
public int kbig(){return k;} |
| 1646 |
@Override |
| 1647 |
public long filterMemory(int pass){return ((pass&1)==0) ? filterMemory0 : filterMemory1;} |
| 1648 |
@Override |
| 1649 |
public boolean ecco(){return ecco;} |
| 1650 |
@Override |
| 1651 |
public boolean qtrimLeft(){return qtrimLeft;} |
| 1652 |
@Override |
| 1653 |
public boolean qtrimRight(){return qtrimRight;} |
| 1654 |
@Override |
| 1655 |
public float minAvgQuality(){return minAvgQuality;} |
| 1656 |
@Override |
| 1657 |
public long tableMemory(){return tableMemory;} |
| 1658 |
@Override |
| 1659 |
public long estimatedKmerCapacity(){return estimatedKmerCapacity;} |
| 1660 |
@Override |
| 1661 |
public int ways(){return ways;} |
| 1662 |
@Override |
| 1663 |
public boolean rcomp(){return rcomp;} |
| 1664 |
|
| 1665 |
public final int kmerToWay(final long kmer){ |
| 1666 |
final int way=(int)((kmer&cmMask)%ways); |
| 1667 |
return way; |
| 1668 |
} |
| 1669 |
|
| 1670 |
|
| 1671 |
private AbstractKmerTable[] tables; |
| 1672 |
|
| 1673 |
public AbstractKmerTable[] tables(){return tables;} |
| 1674 |
|
| 1675 |
public long filterMemoryOverride=0; |
| 1676 |
|
| 1677 |
public final int tableType; |
| 1678 |
|
| 1679 |
private final int bytesPerKmer; |
| 1680 |
|
| 1681 |
private final long usableMemory; |
| 1682 |
private final long filterMemory0; |
| 1683 |
private final long filterMemory1; |
| 1684 |
private final long tableMemory; |
| 1685 |
private final long estimatedKmerCapacity; |
| 1686 |
|
| 1687 |
|
| 1688 |
private final boolean prealloc; |
| 1689 |
|
| 1690 |
|
| 1691 |
public final int ways; |
| 1692 |
|
| 1693 |
|
| 1694 |
public final int k; |
| 1695 |
|
| 1696 |
public final int k2; |
| 1697 |
|
| 1698 |
public final long coreMask; |
| 1699 |
public final long middleMask; |
| 1700 |
public final long cmMask; |
| 1701 |
|
| 1702 |
|
| 1703 |
private final boolean rcomp; |
| 1704 |
|
| 1705 |
|
| 1706 |
public final boolean qtrimLeft; |
| 1707 |
|
| 1708 |
public final boolean qtrimRight; |
| 1709 |
|
| 1710 |
public final float trimq; |
| 1711 |
|
| 1712 |
private final float trimE; |
| 1713 |
|
| 1714 |
|
| 1715 |
public final float minAvgQuality; |
| 1716 |
|
| 1717 |
public final int minAvgQualityBases; |
| 1718 |
|
| 1719 |
|
| 1720 |
public final float minProb; |
| 1721 |
|
| 1722 |
|
| 1723 |
private final boolean ecco; |
| 1724 |
|
| 1725 |
|
| 1726 |
private final boolean merge; |
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
public Walker walk(){ |
| 1733 |
return new WalkerKST(); |
| 1734 |
} |
| 1735 |
|
| 1736 |
public class WalkerKST extends Walker { |
| 1737 |
|
| 1738 |
WalkerKST(){ |
| 1739 |
w=tables[0].walk(); |
| 1740 |
} |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
public boolean next(){ |
| 1747 |
if(w==null){return false;} |
| 1748 |
if(w.next()){return true;} |
| 1749 |
if(tnum<tables.length){tnum++;} |
| 1750 |
w=(tnum<tables.length ? tables[tnum].walk() : null); |
| 1751 |
return w==null ? false : w.next(); |
| 1752 |
} |
| 1753 |
|
| 1754 |
public long kmer(){return w.kmer();} |
| 1755 |
public int value(){return w.value();} |
| 1756 |
|
| 1757 |
private Walker w=null; |
| 1758 |
|
| 1759 |
|
| 1760 |
private int tnum=0; |
| 1761 |
} |
| 1762 |
|
| 1763 |
} |