| 1 |
package bloom; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.io.Serializable; |
| 6 |
import java.util.ArrayList; |
| 7 |
|
| 8 |
import dna.AminoAcid; |
| 9 |
import fileIO.ReadWrite; |
| 10 |
import shared.Parse; |
| 11 |
import shared.Parser; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Timer; |
| 15 |
import shared.Tools; |
| 16 |
import stream.FastaReadInputStream; |
| 17 |
import stream.Read; |
| 18 |
import structures.IntList; |
| 19 |
import structures.LongList; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class BloomFilter implements Serializable { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private static final long serialVersionUID = -3987955563503838492L; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static void main(String[] args){ |
| 44 |
|
| 45 |
Timer t=new Timer(); |
| 46 |
|
| 47 |
|
| 48 |
BloomFilter x=new BloomFilter(args); |
| 49 |
|
| 50 |
System.err.println(x.filter.toShortString()); |
| 51 |
t.stop("Time: \t"); |
| 52 |
|
| 53 |
|
| 54 |
Shared.closeStream(x.outstream); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public BloomFilter(String[] args){ |
| 62 |
|
| 63 |
{ |
| 64 |
PreParser pp=new PreParser(args, getClass(), false); |
| 65 |
args=pp.args; |
| 66 |
outstream=pp.outstream; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 71 |
ReadWrite.setZipThreads(Shared.threads()); |
| 72 |
KmerCountAbstract.CANONICAL=true; |
| 73 |
|
| 74 |
|
| 75 |
Parser parser=new Parser(); |
| 76 |
|
| 77 |
int k_=31; |
| 78 |
int kbig_=31; |
| 79 |
int bits_=1; |
| 80 |
int hashes_=2; |
| 81 |
int minConsecutiveMatches_=3; |
| 82 |
float memFraction=1; |
| 83 |
boolean rcomp_=true; |
| 84 |
|
| 85 |
|
| 86 |
for(int i=0; i<args.length; i++){ |
| 87 |
String arg=args[i]; |
| 88 |
|
| 89 |
|
| 90 |
String[] split=arg.split("="); |
| 91 |
String a=split[0].toLowerCase(); |
| 92 |
String b=split.length>1 ? split[1] : null; |
| 93 |
|
| 94 |
if(a.equals("verbose")){ |
| 95 |
verbose=Parse.parseBoolean(b); |
| 96 |
}else if(a.equals("k") || a.equals("ksmall")){ |
| 97 |
k_=Integer.parseInt(b); |
| 98 |
assert(k_<=31 && k_>=1); |
| 99 |
}else if(a.equals("kbig")){ |
| 100 |
kbig_=Integer.parseInt(b); |
| 101 |
assert(kbig_>=1); |
| 102 |
}else if(a.equals("hashes")){ |
| 103 |
hashes_=Integer.parseInt(b); |
| 104 |
assert(hashes_<=10000 && hashes_>=1); |
| 105 |
}else if(a.equals("minhits")){ |
| 106 |
minConsecutiveMatches_=Integer.parseInt(b); |
| 107 |
assert(minConsecutiveMatches_>=1); |
| 108 |
}else if(a.equals("bits")){ |
| 109 |
bits_=Integer.parseInt(b); |
| 110 |
}else if(a.equals("memfraction")){ |
| 111 |
memFraction=Float.parseFloat(b); |
| 112 |
}else if(a.equals("extra")){ |
| 113 |
if(b==null){extra.clear();} |
| 114 |
else{ |
| 115 |
for(String s : b.split(",")){extra.add(s);} |
| 116 |
} |
| 117 |
}else if(a.equals("rcomp")){ |
| 118 |
rcomp_=Parse.parseBoolean(b); |
| 119 |
}else if(a.equals("parse_flag_goes_here")){ |
| 120 |
long fake_variable=Parse.parseKMG(b); |
| 121 |
|
| 122 |
}else if(parser.parse(arg, a, b)){ |
| 123 |
|
| 124 |
}else{ |
| 125 |
outstream.println("Unknown parameter "+args[i]); |
| 126 |
assert(false) : "Unknown parameter "+args[i]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
k=k_; |
| 131 |
kbig=Tools.max(k_, kbig_); |
| 132 |
smallPerBig=kbig-k+1; |
| 133 |
bits=bits_; |
| 134 |
hashes=hashes_; |
| 135 |
minConsecutiveMatches=minConsecutiveMatches_; |
| 136 |
rcomp=rcomp_; |
| 137 |
|
| 138 |
|
| 139 |
assert(bits==1 || bits==2 || bits==4 || bits==8 || bits==16 || bits==32) : "Bits must be a power of 2."; |
| 140 |
|
| 141 |
{ |
| 142 |
in1=parser.in1; |
| 143 |
in2=parser.in2; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 148 |
in2=in1.replace("#", "2"); |
| 149 |
in1=in1.replace("#", "1"); |
| 150 |
} |
| 151 |
|
| 152 |
assert(FastaReadInputStream.settingsOK()); |
| 153 |
|
| 154 |
|
| 155 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 156 |
|
| 157 |
|
| 158 |
if(!Tools.testInputFiles(false, true, in1, in2)){ |
| 159 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
if(!Tools.testForDuplicateFiles(true, in1, in2)){ |
| 164 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 165 |
} |
| 166 |
|
| 167 |
filterMemory=setMemory(memFraction); |
| 168 |
|
| 169 |
shift=bitsPerBase*k; |
| 170 |
shift2=shift-bitsPerBase; |
| 171 |
mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 172 |
filter=load(); |
| 173 |
} |
| 174 |
|
| 175 |
public BloomFilter(int k_, int kbig_, int bits_, int hashes_, |
| 176 |
int minConsecutiveMatches_, boolean rcomp_, boolean ecco_, boolean merge_, float memFraction){ |
| 177 |
this(null, null, null, k_, kbig_, bits_, hashes_, minConsecutiveMatches_, rcomp_, ecco_, merge_, memFraction); |
| 178 |
} |
| 179 |
|
| 180 |
public BloomFilter(String in1_, String in2_, ArrayList<String> extra_, int k_, int kbig_, int bits_, int hashes_, |
| 181 |
int minConsecutiveMatches_, boolean rcomp_, boolean ecco_, boolean merge_, float memFraction){ |
| 182 |
if(extra_!=null){ |
| 183 |
for(String s : extra_){extra.add(s);} |
| 184 |
} |
| 185 |
filterMemory=setMemory(memFraction); |
| 186 |
|
| 187 |
in1=in1_; |
| 188 |
in2=in2_; |
| 189 |
k=k_; |
| 190 |
kbig=Tools.max(k_, kbig_); |
| 191 |
smallPerBig=kbig-k+1; |
| 192 |
bits=bits_; |
| 193 |
hashes=hashes_; |
| 194 |
minConsecutiveMatches=minConsecutiveMatches_; |
| 195 |
rcomp=rcomp_; |
| 196 |
ecco=ecco_; |
| 197 |
merge=merge_; |
| 198 |
|
| 199 |
shift=bitsPerBase*k; |
| 200 |
shift2=shift-bitsPerBase; |
| 201 |
mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 202 |
filter=load(); |
| 203 |
} |
| 204 |
|
| 205 |
public BloomFilter(boolean bbmapIndex_, int k_, int kbig_, int bits_, int hashes_, int minConsecutiveMatches_, boolean rcomp_) { |
| 206 |
assert(bbmapIndex_); |
| 207 |
filterMemory=setMemory(0.75); |
| 208 |
|
| 209 |
in1=null; |
| 210 |
in2=null; |
| 211 |
k=k_; |
| 212 |
kbig=Tools.max(k_, kbig_); |
| 213 |
smallPerBig=kbig-k+1; |
| 214 |
bits=bits_; |
| 215 |
hashes=hashes_; |
| 216 |
minConsecutiveMatches=minConsecutiveMatches_; |
| 217 |
rcomp=rcomp_; |
| 218 |
|
| 219 |
shift=bitsPerBase*k; |
| 220 |
shift2=shift-bitsPerBase; |
| 221 |
mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 222 |
filter=loadFromIndex(); |
| 223 |
} |
| 224 |
|
| 225 |
private static long setMemory(double mult){ |
| 226 |
if(printMem) {Shared.printMemory();} |
| 227 |
|
| 228 |
Runtime rt=Runtime.getRuntime(); |
| 229 |
final long mmemory=rt.maxMemory(); |
| 230 |
final long tmemory=rt.totalMemory(); |
| 231 |
final long fmemory=rt.freeMemory(); |
| 232 |
final long umemory=tmemory-fmemory; |
| 233 |
|
| 234 |
double xmsRatio=Shared.xmsRatio(); |
| 235 |
double usableMemory=Tools.max(((mmemory-96000000)*(xmsRatio>0.97 ? 0.82 : 0.72)), mmemory*0.45); |
| 236 |
double availableMemory=usableMemory-umemory; |
| 237 |
double filterMemory=availableMemory*mult; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
return (long)filterMemory; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private KCountArray7MTA load(){ |
| 249 |
final int cbits=bits; |
| 250 |
final long totalBits=8*filterMemory; |
| 251 |
final long cells=(OVERRIDE_CELLS>0 ? OVERRIDE_CELLS : totalBits/cbits); |
| 252 |
|
| 253 |
KCountArray7MTA kca; |
| 254 |
if(in1!=null || (extra!=null && extra.size()>0)) { |
| 255 |
ReadCounter rc=new ReadCounter(k, rcomp, ecco, merge, Shared.AMINO_IN); |
| 256 |
kca=(KCountArray7MTA)rc.makeKca(in1, in2, extra==null || extra.isEmpty() ? null : extra, |
| 257 |
cbits, cells, hashes, minq, |
| 258 |
maxReads, 1, 1, 1, null, 0); |
| 259 |
}else { |
| 260 |
kca=(KCountArray7MTA) KCountArray.makeNew(cells, cbits, hashes, null, 0); |
| 261 |
} |
| 262 |
return kca; |
| 263 |
} |
| 264 |
|
| 265 |
private KCountArray7MTA loadFromIndex(){ |
| 266 |
KmerCountAbstract.CANONICAL=true; |
| 267 |
final int cbits=bits; |
| 268 |
final long totalBits=8*filterMemory; |
| 269 |
final long cells=totalBits/cbits; |
| 270 |
outstream.println("Filter Memory = "+Tools.format("%.2f GB", filterMemory/(double)(1024*1024*1024))); |
| 271 |
IndexCounter ic=new IndexCounter(k, rcomp); |
| 272 |
KCountArray7MTA kca=(KCountArray7MTA)ic.makeKcaFromIndex(cells, cbits, hashes); |
| 273 |
return kca; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public boolean passes(Read r1, Read r2, final int thresh) { |
| 281 |
boolean pass=passes(r1, thresh); |
| 282 |
return pass && passes(r2, thresh); |
| 283 |
} |
| 284 |
|
| 285 |
public float averageCount(final byte[] bases) { |
| 286 |
if(bases==null || bases.length<k-1){return 0;} |
| 287 |
|
| 288 |
long kmer=0; |
| 289 |
long rkmer=0; |
| 290 |
long sum=0; |
| 291 |
int len=0; |
| 292 |
int counted=0; |
| 293 |
|
| 294 |
int prev2=0, prev=0, count=0; |
| 295 |
|
| 296 |
for(int i=0; i<bases.length; i++){ |
| 297 |
byte b=bases[i]; |
| 298 |
long x=AminoAcid.baseToNumber[b]; |
| 299 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 300 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 301 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 302 |
if(x<0){len=0; rkmer=0;}else{len++;} |
| 303 |
if(len>=k){ |
| 304 |
prev2=prev; |
| 305 |
prev=count; |
| 306 |
count=getCount(kmer, rkmer); |
| 307 |
|
| 308 |
prev=Tools.min(prev, Tools.max(prev2, count)); |
| 309 |
sum+=prev; |
| 310 |
counted++; |
| 311 |
} |
| 312 |
} |
| 313 |
sum+=count; |
| 314 |
return sum/Tools.max(counted, 1f); |
| 315 |
} |
| 316 |
|
| 317 |
public int minCount(Read r) { |
| 318 |
if(r==null || r.length()<k-1){return -1;} |
| 319 |
final byte[] bases=r.bases; |
| 320 |
|
| 321 |
long kmer=0; |
| 322 |
long rkmer=0; |
| 323 |
int len=0; |
| 324 |
int min=Integer.MAX_VALUE; |
| 325 |
int counted=0; |
| 326 |
|
| 327 |
for(int i=0; i<bases.length; i++){ |
| 328 |
byte b=bases[i]; |
| 329 |
long x=AminoAcid.baseToNumber[b]; |
| 330 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 331 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 332 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 333 |
if(x<0){len=0; rkmer=0;}else{len++;} |
| 334 |
if(len>=k){ |
| 335 |
counted++; |
| 336 |
int count=getCount(kmer, rkmer); |
| 337 |
min=Tools.min(min, count); |
| 338 |
if(count==0){ |
| 339 |
|
| 340 |
break; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
return counted>0 ? min : -1; |
| 346 |
} |
| 347 |
|
| 348 |
public boolean hasHighCountFraction(Read r, final int thresh, final float fraction) { |
| 349 |
if(r==null || r.length()<k-1){return false;} |
| 350 |
final byte[] bases=r.bases; |
| 351 |
final int kmers=r.length()-k+1; |
| 352 |
|
| 353 |
final int minHigh=Math.round(fraction*kmers); |
| 354 |
final int maxLow=kmers-minHigh; |
| 355 |
|
| 356 |
long kmer=0; |
| 357 |
long rkmer=0; |
| 358 |
int len=0; |
| 359 |
int counted=0; |
| 360 |
int low=0; |
| 361 |
|
| 362 |
for(int i=0; i<bases.length; i++){ |
| 363 |
byte b=bases[i]; |
| 364 |
long x=AminoAcid.baseToNumber[b]; |
| 365 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 366 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 367 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 368 |
if(x<0){len=0; rkmer=0;}else{len++;} |
| 369 |
if(len>=k){ |
| 370 |
counted++; |
| 371 |
int count=getCount(kmer, rkmer); |
| 372 |
if(count<thresh){ |
| 373 |
low++; |
| 374 |
if(low>maxLow){return false;} |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
public float lowCountFraction(final Read r, final int thresh, final boolean smooth) { |
| 382 |
return 1-highCountFraction(r, thresh, smooth); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public float highCountFraction(final Read r, final int thresh, final boolean smooth) { |
| 393 |
return r==null ? 0 : highCountFraction(r.bases, thresh, smooth); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public float highCountFraction(final byte[] bases, final int thresh, boolean smooth) { |
| 404 |
if(bases==null || bases.length<k-1){return 0;} |
| 405 |
|
| 406 |
long kmer=0; |
| 407 |
long rkmer=0; |
| 408 |
int len=0; |
| 409 |
int counted=0; |
| 410 |
int highCount=0; |
| 411 |
int prevHigh=1; |
| 412 |
|
| 413 |
for(int i=0; i<bases.length; i++){ |
| 414 |
byte b=bases[i]; |
| 415 |
long x=AminoAcid.baseToNumber[b]; |
| 416 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 417 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 418 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 419 |
if(x<0){len=0; rkmer=0;}else{len++;} |
| 420 |
if(len>=k){ |
| 421 |
counted++; |
| 422 |
final int count=getCount(kmer, rkmer); |
| 423 |
final int high=count>=thresh ? 1 : 0; |
| 424 |
highCount+=(high&prevHigh); |
| 425 |
prevHigh=(smooth ? high : prevHigh); |
| 426 |
} |
| 427 |
} |
| 428 |
return counted<1 ? 0 : highCount/(float)counted; |
| 429 |
} |
| 430 |
|
| 431 |
public boolean isJunk(Read r1, Read r2, int range){ |
| 432 |
assert(bits>1); |
| 433 |
if(r2==null || r2.length()<k){return isJunk(r1, range);} |
| 434 |
if(r1.length()<k){return isJunk(r2, range);} |
| 435 |
if(getLeftCount(r1.bases, range)>1 || getLeftCount(r2.bases, range)>1){return false;} |
| 436 |
|
| 437 |
return getRightCount(r1.bases, range)<3 || getRightCount(r2.bases, range)<3; |
| 438 |
} |
| 439 |
|
| 440 |
public boolean isJunk(Read r, int range){ |
| 441 |
assert(bits>1); |
| 442 |
if(r.length()<k){return true;} |
| 443 |
return getLeftCount(r.bases, range)<2 && getRightCount(r.bases, range)<2; |
| 444 |
} |
| 445 |
|
| 446 |
private int getLeftCount(byte[] bases, int range){ |
| 447 |
assert(range>0) : range; |
| 448 |
if(bases.length<k){return -1;} |
| 449 |
long kmer=0, rkmer=0; |
| 450 |
int len=0; |
| 451 |
final int stop=Tools.min(bases.length, k+range-1); |
| 452 |
int min=Integer.MAX_VALUE; |
| 453 |
int counted=0; |
| 454 |
for(int i=0; i<stop; i++){ |
| 455 |
byte b=bases[i]; |
| 456 |
long x=AminoAcid.baseToNumber[b]; |
| 457 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 458 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 459 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 460 |
if(x<0){len=0; rkmer=0;} |
| 461 |
else{ |
| 462 |
len++; |
| 463 |
if(len>=k){ |
| 464 |
counted++; |
| 465 |
int count=getCount(kmer, rkmer); |
| 466 |
min=Tools.min(min, count); |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
return counted>0 ? min : -1; |
| 471 |
} |
| 472 |
|
| 473 |
private int getRightCount(byte[] bases, int range){ |
| 474 |
assert(range>0) : range; |
| 475 |
if(bases.length<k){return -1;} |
| 476 |
long kmer=0, rkmer=0; |
| 477 |
int len=0; |
| 478 |
final int start=Tools.max(0, bases.length-k-range+1); |
| 479 |
int min=Integer.MAX_VALUE; |
| 480 |
int counted=0; |
| 481 |
for(int i=start; i<bases.length; i++){ |
| 482 |
byte b=bases[i]; |
| 483 |
long x=AminoAcid.baseToNumber[b]; |
| 484 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 485 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 486 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 487 |
if(x<0){len=0; rkmer=0;} |
| 488 |
else{ |
| 489 |
len++; |
| 490 |
if(len>=k){ |
| 491 |
counted++; |
| 492 |
int count=getCount(kmer, rkmer); |
| 493 |
min=Tools.min(min, count); |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
return counted>0 ? min : -1; |
| 498 |
} |
| 499 |
|
| 500 |
public boolean passes(Read r, final int thresh) { |
| 501 |
if(r==null || r.length()<k+minConsecutiveMatches-1){return true;} |
| 502 |
final byte[] bases=r.bases; |
| 503 |
|
| 504 |
long kmer=0; |
| 505 |
long rkmer=0; |
| 506 |
int len=0; |
| 507 |
int streak=0; |
| 508 |
|
| 509 |
for(int i=0; i<bases.length; i++){ |
| 510 |
byte b=bases[i]; |
| 511 |
long x=AminoAcid.baseToNumber[b]; |
| 512 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 513 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 514 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 515 |
if(x<0){len=0; rkmer=0;}else{len++;} |
| 516 |
if(len>=k){ |
| 517 |
boolean found=contains(kmer, rkmer, thresh); |
| 518 |
if(found){ |
| 519 |
streak++; |
| 520 |
if(streak>=minConsecutiveMatches){return false;} |
| 521 |
}else{streak=0;} |
| 522 |
} |
| 523 |
} |
| 524 |
return true; |
| 525 |
} |
| 526 |
|
| 527 |
public boolean matches(Read r, LongList keys, final int thresh) { |
| 528 |
return !passes(r, keys, thresh); |
| 529 |
} |
| 530 |
|
| 531 |
public boolean matchesEither(Read r1, Read r2, LongList keys, final int thresh) { |
| 532 |
boolean match=!passes(r1, keys, thresh); |
| 533 |
return match || !passes(r2, keys, thresh); |
| 534 |
} |
| 535 |
|
| 536 |
public boolean passes(Read r1, Read r2, LongList keys, final int thresh) { |
| 537 |
boolean pass=passes(r1, keys, thresh); |
| 538 |
return pass && passes(r2, keys, thresh); |
| 539 |
} |
| 540 |
|
| 541 |
public boolean passes(Read r, LongList keys, final int thresh) { |
| 542 |
if(r==null || r.length()<k+minConsecutiveMatches-1){return true;} |
| 543 |
if(minConsecutiveMatches<2){return passes(r, thresh);} |
| 544 |
keys.clear(); |
| 545 |
final byte[] bases=r.bases; |
| 546 |
|
| 547 |
long kmer=0; |
| 548 |
long rkmer=0; |
| 549 |
int len=0; |
| 550 |
|
| 551 |
for(int i=0; i<bases.length; i++){ |
| 552 |
byte b=bases[i]; |
| 553 |
long x=AminoAcid.baseToNumber[b]; |
| 554 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 555 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 556 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 557 |
if(x<0){ |
| 558 |
if(len>=k){keys.add(-1);} |
| 559 |
len=0; |
| 560 |
kmer=rkmer=0; |
| 561 |
}else{ |
| 562 |
len++; |
| 563 |
if(len>=k){keys.add(toKey(kmer, rkmer));} |
| 564 |
} |
| 565 |
} |
| 566 |
return passes(keys, thresh); |
| 567 |
} |
| 568 |
|
| 569 |
public boolean passes(final LongList keys, final int thresh) { |
| 570 |
assert(minConsecutiveMatches>1); |
| 571 |
final long[] array=keys.array; |
| 572 |
final int len=keys.size; |
| 573 |
|
| 574 |
for(int i=minConsecutiveMatches-1; i<len; i+=minConsecutiveMatches){ |
| 575 |
final boolean found; |
| 576 |
{ |
| 577 |
final long key=array[i]; |
| 578 |
found=(key<0 ? false : filter.read(key)>=thresh); |
| 579 |
} |
| 580 |
if(found){ |
| 581 |
int streak=1; |
| 582 |
for(int j=1; j<minConsecutiveMatches; j++){ |
| 583 |
final long key=array[i-j]; |
| 584 |
if(key<0 || filter.read(key)<thresh){break;} |
| 585 |
else{streak++;} |
| 586 |
} |
| 587 |
if(streak>=minConsecutiveMatches){return false;} |
| 588 |
for(int j=1; j<minConsecutiveMatches && j+i<len; j++){ |
| 589 |
final long key=array[i+j]; |
| 590 |
if(key<0 || filter.read(key)<thresh){break;} |
| 591 |
else{streak++;} |
| 592 |
if(streak>=minConsecutiveMatches){return false;} |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
return true; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public int fillCounts(byte[] bases, IntList counts){ |
| 603 |
final int blen=bases.length; |
| 604 |
if(blen<k){return 0;} |
| 605 |
final int min=k-1; |
| 606 |
long kmer=0, rkmer=0; |
| 607 |
int len=0; |
| 608 |
int valid=0; |
| 609 |
|
| 610 |
counts.clear(); |
| 611 |
|
| 612 |
|
| 613 |
for(int i=0; i<blen; i++){ |
| 614 |
final byte base=bases[i]; |
| 615 |
final long x=AminoAcid.baseToNumber[base]; |
| 616 |
final long x2=AminoAcid.baseToComplementNumber[base]; |
| 617 |
kmer=((kmer<<bitsPerBase)|x)&mask; |
| 618 |
rkmer=((rkmer>>>bitsPerBase)|(x2<<shift2))&mask; |
| 619 |
|
| 620 |
if(x<0){ |
| 621 |
len=0; |
| 622 |
kmer=rkmer=0; |
| 623 |
}else{ |
| 624 |
len++; |
| 625 |
} |
| 626 |
|
| 627 |
if(i>=min){ |
| 628 |
if(len>=k){ |
| 629 |
int count=getCount(kmer, rkmer); |
| 630 |
counts.add(count); |
| 631 |
valid++; |
| 632 |
}else{ |
| 633 |
counts.add(0); |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
return valid; |
| 638 |
} |
| 639 |
|
| 640 |
public int getCount(final long kmer, final long rkmer){ |
| 641 |
final long key=toKey(kmer, rkmer); |
| 642 |
return filter.read(key); |
| 643 |
} |
| 644 |
|
| 645 |
public int getCount(final long key){ |
| 646 |
|
| 647 |
return filter.read(key); |
| 648 |
} |
| 649 |
|
| 650 |
public boolean contains(final long kmer, final long rkmer, final int thresh){ |
| 651 |
final long key=toKey(kmer, rkmer); |
| 652 |
return filter.read(key)>=thresh; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
public int fillCountsBig(byte[] bases, IntList counts){ |
| 659 |
assert(smallPerBig>1) : smallPerBig; |
| 660 |
final int valid0=fillCounts(bases, counts); |
| 661 |
if(valid0<smallPerBig){return 0;} |
| 662 |
|
| 663 |
for(int i=0, lim=counts.size()-smallPerBig+1; i<lim; i++){ |
| 664 |
int count=smallToBig(counts, i); |
| 665 |
counts.set(i, count); |
| 666 |
} |
| 667 |
counts.size-=(smallPerBig-1); |
| 668 |
|
| 669 |
return valid0-smallPerBig+1; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
public void fillCountsBig(LongList kmers, IntList counts){ |
| 674 |
assert(smallPerBig>1) : smallPerBig; |
| 675 |
counts.clear(); |
| 676 |
for(int i=0; i<kmers.size; i++){ |
| 677 |
long kmer=kmers.get(i); |
| 678 |
int count=getCountBig(kmer); |
| 679 |
counts.add(count); |
| 680 |
} |
| 681 |
|
| 682 |
} |
| 683 |
|
| 684 |
private int smallToBig(IntList counts, final int start){ |
| 685 |
assert(smallPerBig>1) : smallPerBig; |
| 686 |
final int[] array=counts.array; |
| 687 |
int min=array[start]; |
| 688 |
for(int i=start+1; i<start+smallPerBig && min>0; i++){ |
| 689 |
min=Tools.min(min, array[i]); |
| 690 |
} |
| 691 |
return min; |
| 692 |
} |
| 693 |
|
| 694 |
@SuppressWarnings("unused") |
| 695 |
public int getCountBig(final long kmer, final long rkmer){ |
| 696 |
return getCountBig(kmer); |
| 697 |
} |
| 698 |
|
| 699 |
public int getCountBig(long kmer){ |
| 700 |
int min=Integer.MAX_VALUE; |
| 701 |
for(int i=0; i<smallPerBig && min>0; i++){ |
| 702 |
long small=kmer&mask; |
| 703 |
long key=toKey(small); |
| 704 |
int count=getCount(key); |
| 705 |
min=Tools.min(min, count); |
| 706 |
kmer>>=bitsPerBase; |
| 707 |
} |
| 708 |
return min; |
| 709 |
} |
| 710 |
|
| 711 |
public boolean containsBig(final long kmer, final long rkmer, final int thresh){ |
| 712 |
final long key=toKey(kmer, rkmer); |
| 713 |
return filter.read(key)>=thresh; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
public long toKey(final long kmer){ |
| 719 |
return (rcomp ? toKey(kmer, AminoAcid.reverseComplementBinaryFast(kmer, k)) : kmer); |
| 720 |
} |
| 721 |
|
| 722 |
public long toKey(final long kmer, final long rkmer){ |
| 723 |
return (rcomp ? Tools.max(kmer, rkmer) : kmer); |
| 724 |
} |
| 725 |
|
| 726 |
public static final void toKmers(Read r, final LongList list, int k, final int minQuality, final float minProb, final boolean rcomp){ |
| 727 |
assert(k<=32); |
| 728 |
assert(list!=null); |
| 729 |
|
| 730 |
final byte[] bases=r.bases; |
| 731 |
final byte[] quals=r.quality; |
| 732 |
|
| 733 |
if(bases==null || bases.length<k){return;} |
| 734 |
|
| 735 |
final int shift=bitsPerBase*k; |
| 736 |
final int shift2=shift-bitsPerBase; |
| 737 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 738 |
|
| 739 |
long kmer=0; |
| 740 |
long rkmer=0; |
| 741 |
int len=0; |
| 742 |
float prob=1; |
| 743 |
|
| 744 |
for(int i=0; i<bases.length; i++){ |
| 745 |
final byte b=bases[i]; |
| 746 |
long x=AminoAcid.baseToNumber[b]; |
| 747 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 748 |
kmer=((kmer<<2)|x)&mask; |
| 749 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 750 |
|
| 751 |
final byte q; |
| 752 |
if(quals==null){ |
| 753 |
q=50; |
| 754 |
}else{ |
| 755 |
q=quals[i]; |
| 756 |
prob=prob*align2.QualityTools.PROB_CORRECT[q]; |
| 757 |
if(len>k){ |
| 758 |
byte oldq=quals[i-k]; |
| 759 |
prob=prob*align2.QualityTools.PROB_CORRECT_INVERSE[oldq]; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
if(x<0 || q<minQuality){ |
| 764 |
len=0; |
| 765 |
kmer=rkmer=0; |
| 766 |
prob=1; |
| 767 |
}else{ |
| 768 |
len++; |
| 769 |
if(len>=k && prob>=minProb){ |
| 770 |
long key=(rcomp ? Tools.max(kmer, rkmer) : kmer); |
| 771 |
list.add(key); |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
long maxReads=-1; |
| 786 |
boolean ecco=false; |
| 787 |
boolean merge=false; |
| 788 |
byte minq=0; |
| 789 |
|
| 790 |
|
| 791 |
private String in1=null; |
| 792 |
|
| 793 |
private String in2=null; |
| 794 |
|
| 795 |
private ArrayList<String> extra=new ArrayList<String>(); |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
public final KCountArray7MTA filter; |
| 802 |
final int k; |
| 803 |
final int kbig; |
| 804 |
final int smallPerBig; |
| 805 |
final int bits; |
| 806 |
final int hashes; |
| 807 |
final int minConsecutiveMatches; |
| 808 |
|
| 809 |
final int shift; |
| 810 |
final int shift2; |
| 811 |
final long mask; |
| 812 |
final boolean rcomp; |
| 813 |
|
| 814 |
|
| 815 |
private final long filterMemory; |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public static long OVERRIDE_CELLS=-1; |
| 822 |
static final int bitsPerBase=2; |
| 823 |
public static boolean printMem=true; |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
private transient PrintStream outstream=System.err; |
| 831 |
|
| 832 |
public static boolean verbose=false; |
| 833 |
|
| 834 |
public boolean errorState=false; |
| 835 |
} |