| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.BitSet; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.LinkedHashMap; |
| 10 |
import java.util.concurrent.ArrayBlockingQueue; |
| 11 |
import java.util.concurrent.ConcurrentHashMap; |
| 12 |
|
| 13 |
import dna.AminoAcid; |
| 14 |
import fileIO.ByteFile; |
| 15 |
import fileIO.ByteFile1; |
| 16 |
import fileIO.ByteFile2; |
| 17 |
import fileIO.FileFormat; |
| 18 |
import fileIO.ReadWrite; |
| 19 |
import shared.KillSwitch; |
| 20 |
import shared.Parse; |
| 21 |
import shared.Parser; |
| 22 |
import shared.PreParser; |
| 23 |
import shared.Shared; |
| 24 |
import shared.Timer; |
| 25 |
import shared.Tools; |
| 26 |
import stream.ConcurrentGenericReadInputStream; |
| 27 |
import stream.ConcurrentReadInputStream; |
| 28 |
import stream.ConcurrentReadOutputStream; |
| 29 |
import stream.FASTQ; |
| 30 |
import stream.FastaReadInputStream; |
| 31 |
import stream.Read; |
| 32 |
import stream.SamLine; |
| 33 |
import structures.CoverageArray; |
| 34 |
import structures.CoverageArray2; |
| 35 |
import structures.CoverageArray3; |
| 36 |
import structures.IntList; |
| 37 |
import structures.ListNum; |
| 38 |
import tracker.EntropyTracker; |
| 39 |
import tracker.ReadStats; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class BBMask{ |
| 50 |
|
| 51 |
public static void main(String[] args){ |
| 52 |
Timer t=new Timer(); |
| 53 |
BBMask x=new BBMask(args); |
| 54 |
x.process(t); |
| 55 |
|
| 56 |
|
| 57 |
Shared.closeStream(x.outstream); |
| 58 |
} |
| 59 |
|
| 60 |
public BBMask(String[] args){ |
| 61 |
|
| 62 |
{ |
| 63 |
PreParser pp=new PreParser(args, getClass(), false); |
| 64 |
args=pp.args; |
| 65 |
outstream=pp.outstream; |
| 66 |
} |
| 67 |
|
| 68 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 69 |
ReadWrite.setZipThreads(16); |
| 70 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 71 |
|
| 72 |
boolean setEntropyMode=false; |
| 73 |
|
| 74 |
for(int i=0; i<args.length; i++){ |
| 75 |
String arg=args[i]; |
| 76 |
String[] split=arg.split("="); |
| 77 |
String a=split[0].toLowerCase(); |
| 78 |
String b=split.length>1 ? split[1] : null; |
| 79 |
|
| 80 |
if(Parser.parseCommonStatic(arg, a, b)){ |
| 81 |
|
| 82 |
}else if(Parser.parseZip(arg, a, b)){ |
| 83 |
|
| 84 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 85 |
|
| 86 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 87 |
|
| 88 |
}else if(a.equals("verbose")){ |
| 89 |
verbose=Parse.parseBoolean(b); |
| 90 |
ByteFile1.verbose=verbose; |
| 91 |
ByteFile2.verbose=verbose; |
| 92 |
stream.FastaReadInputStream.verbose=verbose; |
| 93 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 94 |
stream.FastqReadInputStream.verbose=verbose; |
| 95 |
ReadWrite.verbose=verbose; |
| 96 |
}else if(a.equals("reads") || a.equals("maxreads")){ |
| 97 |
maxReads=Parse.parseKMG(b); |
| 98 |
}else if(a.equals("t") || a.equals("threads")){ |
| 99 |
Shared.setThreads(b); |
| 100 |
}else if(a.equals("sampad") || a.equals("sampadding") || a.equals("sp")){ |
| 101 |
samPad=Integer.parseInt(b); |
| 102 |
}else if(a.equals("entropymode")){ |
| 103 |
entropyMode=Parse.parseBoolean(b); |
| 104 |
setEntropyMode=true; |
| 105 |
}else if(a.equals("maskrepeats") || a.equals("mr")){ |
| 106 |
processRepeats=Parse.parseBoolean(b); |
| 107 |
}else if(a.equals("masklowentropy") || a.equals("masklowcomplexity") || a.equals("mlc") || a.equals("mle") || a.equals("me")){ |
| 108 |
processEntropy=Parse.parseBoolean(b); |
| 109 |
}else if(a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1") || a.equals("ref")){ |
| 110 |
inRef=b; |
| 111 |
}else if(a.equals("insam") || a.equals("samin") || a.equals("sam")){ |
| 112 |
if(b!=null){ |
| 113 |
for(String s : b.split(",")){ |
| 114 |
inSam.add(s); |
| 115 |
} |
| 116 |
} |
| 117 |
}else if(a.equals("out") || a.equals("output") || a.equals("out1") || a.equals("output1") || a.equals("output1")){ |
| 118 |
outRef=b; |
| 119 |
}else if(a.equals("qfin") || a.equals("qfin1")){ |
| 120 |
qfinRef=b; |
| 121 |
}else if(a.equals("qfout") || a.equals("qfout1")){ |
| 122 |
qfoutRef=b; |
| 123 |
}else if(a.equals("extin")){ |
| 124 |
extinRef=b; |
| 125 |
}else if(a.equals("extout")){ |
| 126 |
extoutRef=b; |
| 127 |
}else if(a.equals("split")){ |
| 128 |
splitMode=Parse.parseBoolean(b); |
| 129 |
}else if(a.equals("mink") || a.equals("kmin")){ |
| 130 |
mink=mink2=Integer.parseInt(b); |
| 131 |
}else if(a.equals("maxk") || a.equals("kmax")){ |
| 132 |
maxk=maxk2=Integer.parseInt(b); |
| 133 |
}else if(a.equals("k")){ |
| 134 |
mink=maxk=mink2=maxk2=Integer.parseInt(b); |
| 135 |
}else if(a.equals("minkr") || a.equals("krmin")){ |
| 136 |
mink=Integer.parseInt(b); |
| 137 |
}else if(a.equals("maxkr") || a.equals("krmax")){ |
| 138 |
maxk=Integer.parseInt(b); |
| 139 |
}else if(a.equals("kr")){ |
| 140 |
mink=maxk=Integer.parseInt(b); |
| 141 |
}else if(a.equals("mink2") || a.equals("kmin2") || a.equals("minke") || a.equals("kemin")){ |
| 142 |
mink2=Integer.parseInt(b); |
| 143 |
}else if(a.equals("maxk2") || a.equals("kmax2") || a.equals("maxke") || a.equals("kemax")){ |
| 144 |
maxk2=Integer.parseInt(b); |
| 145 |
}else if(a.equals("k2") || a.equals("ke")){ |
| 146 |
mink2=maxk2=Integer.parseInt(b); |
| 147 |
}else if(a.equals("mincov")){ |
| 148 |
mincov=Integer.parseInt(b); |
| 149 |
}else if(a.equals("maxcov")){ |
| 150 |
maxcov=Integer.parseInt(b); |
| 151 |
}else if(a.equals("delcov") || a.equals("delcoverage")){ |
| 152 |
includeDeletionCoverage=Parse.parseBoolean(b); |
| 153 |
}else if(a.equals("window") || a.equals("w")){ |
| 154 |
window=Integer.parseInt(b); |
| 155 |
}else if(a.equals("ratio")){ |
| 156 |
ratio=Float.parseFloat(b); |
| 157 |
if(!setEntropyMode){entropyMode=false;} |
| 158 |
}else if(a.equals("entropy") || a.equals("e")){ |
| 159 |
entropyCutoff=Float.parseFloat(b); |
| 160 |
if(!setEntropyMode){entropyMode=true;} |
| 161 |
}else if(a.equals("lowercase") || a.equals("lc")){ |
| 162 |
MaskByLowercase=Parse.parseBoolean(b); |
| 163 |
}else if(a.equals("minlen")){ |
| 164 |
minlen=Integer.parseInt(b); |
| 165 |
}else if(a.equals("mincount")){ |
| 166 |
mincount=Integer.parseInt(b); |
| 167 |
}else if(a.equals("append") || a.equals("app")){ |
| 168 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 169 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 170 |
overwrite=Parse.parseBoolean(b); |
| 171 |
}else if(a.startsWith("minscaf") || a.startsWith("mincontig")){ |
| 172 |
stream.FastaReadInputStream.MIN_READ_LEN=Integer.parseInt(b); |
| 173 |
}else if(!arg.contains("=") && FileFormat.hasSamOrBamExtension(arg)){ |
| 174 |
inSam.add(arg); |
| 175 |
} |
| 176 |
|
| 177 |
else if(inRef==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 178 |
inRef=arg; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
else{ |
| 184 |
System.err.println("Unknown parameter "+args[i]); |
| 185 |
assert(false) : "Unknown parameter "+args[i]; |
| 186 |
|
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
bits32=(mincov>=Character.MAX_VALUE || maxcov>=Character.MAX_VALUE); |
| 191 |
|
| 192 |
{ |
| 193 |
Parser.processQuality(); |
| 194 |
} |
| 195 |
|
| 196 |
assert(FastaReadInputStream.settingsOK()); |
| 197 |
|
| 198 |
if(inRef==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 199 |
|
| 200 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 201 |
ByteFile.FORCE_MODE_BF2=true; |
| 202 |
} |
| 203 |
|
| 204 |
if(outRef!=null && outRef.equalsIgnoreCase("null")){outRef=null;} |
| 205 |
|
| 206 |
if(!Tools.testOutputFiles(overwrite, append, false, outRef)){ |
| 207 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+outRef+"\n"); |
| 208 |
} |
| 209 |
|
| 210 |
ffoutRef=FileFormat.testOutput(outRef, FileFormat.FASTA, extoutRef, true, overwrite, append, false); |
| 211 |
|
| 212 |
ffinRef=FileFormat.testInput(inRef, FileFormat.FASTA, extinRef, true, true); |
| 213 |
|
| 214 |
if(inSam!=null && inSam.size()>0){ |
| 215 |
ffinSam=new FileFormat[inSam.size()]; |
| 216 |
for(int i=0; i<inSam.size(); i++){ |
| 217 |
ffinSam[i]=FileFormat.testInput(inSam.get(i), FileFormat.SAM, null, true, false); |
| 218 |
} |
| 219 |
}else{ |
| 220 |
ffinSam=null; |
| 221 |
} |
| 222 |
|
| 223 |
SamLine.CONVERT_CIGAR_TO_MATCH=false; |
| 224 |
|
| 225 |
EntropyTracker.verify=verify; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public void process(Timer t0){ |
| 232 |
|
| 233 |
Timer t=new Timer(); |
| 234 |
{ |
| 235 |
t.start(); |
| 236 |
outstream.println("Loading input"); |
| 237 |
|
| 238 |
boolean oldTI=FASTQ.TEST_INTERLEAVED; |
| 239 |
boolean oldFI=FASTQ.FORCE_INTERLEAVED; |
| 240 |
FASTQ.TEST_INTERLEAVED=false; |
| 241 |
FASTQ.FORCE_INTERLEAVED=false; |
| 242 |
map=hashRef(); |
| 243 |
FASTQ.TEST_INTERLEAVED=oldTI; |
| 244 |
FASTQ.FORCE_INTERLEAVED=oldFI; |
| 245 |
t.stop(); |
| 246 |
|
| 247 |
outstream.println("Loading Time: \t"+t); |
| 248 |
} |
| 249 |
|
| 250 |
long repeats=0, lowcomplexity=0; |
| 251 |
long mapping=0; |
| 252 |
|
| 253 |
if(processRepeats && maxk>0){ |
| 254 |
t.start(); |
| 255 |
outstream.println("\nMasking repeats (to disable, set 'mr=f')"); |
| 256 |
|
| 257 |
repeats=maskRepeats(); |
| 258 |
t.stop(); |
| 259 |
|
| 260 |
double rpnano=refReads/(double)(t.elapsed); |
| 261 |
double bpnano=refBases/(double)(t.elapsed); |
| 262 |
|
| 263 |
String rpstring=""+refReads; |
| 264 |
String bpstring=""+refBases; |
| 265 |
String bmstring=""+repeats; |
| 266 |
|
| 267 |
while(rpstring.length()<12){rpstring=" "+rpstring;} |
| 268 |
while(bpstring.length()<12){bpstring=" "+bpstring;} |
| 269 |
while(bmstring.length()<12){bmstring=" "+bmstring;} |
| 270 |
|
| 271 |
outstream.println("Repeat Masking Time: \t"+t); |
| 272 |
|
| 273 |
outstream.println("Ref Bases: "+bpstring+" \t"+Tools.format("%.2fm bases/sec", bpnano*1000)); |
| 274 |
outstream.println("Repeat Bases Masked: "+bmstring); |
| 275 |
} |
| 276 |
|
| 277 |
if(processEntropy && maxk2>0){ |
| 278 |
t.start(); |
| 279 |
if(entropyMode){ |
| 280 |
outstream.println("\nMasking low-entropy (to disable, set 'mle=f')"); |
| 281 |
lowcomplexity=maskLowEntropy(); |
| 282 |
}else{ |
| 283 |
outstream.println("\nMasking low-complexity (to disable, set 'mlc=f')"); |
| 284 |
lowcomplexity=maskLowComplexity(null); |
| 285 |
} |
| 286 |
t.stop(); |
| 287 |
|
| 288 |
double rpnano=refReads/(double)(t.elapsed); |
| 289 |
double bpnano=refBases/(double)(t.elapsed); |
| 290 |
|
| 291 |
String rpstring=""+refReads; |
| 292 |
String bpstring=""+refBases; |
| 293 |
String bmstring=""+lowcomplexity; |
| 294 |
|
| 295 |
while(rpstring.length()<12){rpstring=" "+rpstring;} |
| 296 |
while(bpstring.length()<12){bpstring=" "+bpstring;} |
| 297 |
while(bmstring.length()<12){bmstring=" "+bmstring;} |
| 298 |
|
| 299 |
outstream.println("Low Complexity Masking Time: \t"+t); |
| 300 |
|
| 301 |
outstream.println("Ref Bases: "+bpstring+" \t"+Tools.format("%.2fm bases/sec", bpnano*1000)); |
| 302 |
outstream.println("Low Complexity Bases: "+bmstring); |
| 303 |
} |
| 304 |
|
| 305 |
if(ffinSam!=null){ |
| 306 |
t.start(); |
| 307 |
outstream.println("\nMasking from sam"); |
| 308 |
mapping=maskSam(); |
| 309 |
t.stop(); |
| 310 |
|
| 311 |
double rpnano=samReads/(double)(t.elapsed); |
| 312 |
double bpnano=samBases/(double)(t.elapsed); |
| 313 |
|
| 314 |
String rpstring=""+samReads; |
| 315 |
String bpstring=""+samBases; |
| 316 |
String bmstring=""+mapping; |
| 317 |
|
| 318 |
while(rpstring.length()<12){rpstring=" "+rpstring;} |
| 319 |
while(bpstring.length()<12){bpstring=" "+bpstring;} |
| 320 |
while(bmstring.length()<12){bmstring=" "+bmstring;} |
| 321 |
|
| 322 |
outstream.println("Sam Masking Time: \t"+t); |
| 323 |
outstream.println("Sam Reads Processed: "+rpstring+" \t"+Tools.format("%.2fk reads/sec", rpnano*1000000)); |
| 324 |
outstream.println("Sam Bases Processed: "+bpstring+" \t"+Tools.format("%.2fm bases/sec", bpnano*1000)); |
| 325 |
outstream.println("Sam Bases Masked: "+bmstring); |
| 326 |
} |
| 327 |
long total=repeats+mapping+lowcomplexity, masked=0; |
| 328 |
|
| 329 |
if(total>0 || true){ |
| 330 |
t.start(); |
| 331 |
if(splitMode){ |
| 332 |
masked=splitFromBitsets(); |
| 333 |
}else{ |
| 334 |
masked=maskFromBitsets(MaskByLowercase); |
| 335 |
} |
| 336 |
t.stop(); |
| 337 |
outstream.println("Conversion Time: \t"+t); |
| 338 |
} |
| 339 |
|
| 340 |
assert(total==masked) : repeats+", "+mapping+", "+lowcomplexity+", "+total+", "+masked; |
| 341 |
|
| 342 |
if(outRef!=null){ |
| 343 |
t.start(); |
| 344 |
outstream.println("\nWriting output"); |
| 345 |
writeOutput(); |
| 346 |
t.stop(); |
| 347 |
outstream.println("Writing Time: \t"+t); |
| 348 |
} |
| 349 |
{ |
| 350 |
t0.stop(); |
| 351 |
String tstring=""+total; |
| 352 |
while(tstring.length()<12){tstring=" "+tstring;} |
| 353 |
outstream.println("\nTotal Bases Masked: "+tstring+"/"+refBases+Tools.format("\t%.3f%%", total*100.0/refBases)); |
| 354 |
outstream.println("Total Time: \t"+t0); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if(errorState){ |
| 360 |
throw new RuntimeException("\nBBMask terminated in an error state; the output may be corrupt."); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
private static int setHighCoverage(BitSet bs, CoverageArray ca, int maxAllowedCoverage, int maxLen){ |
| 367 |
int numSet=0; |
| 368 |
for(int i=0; i<maxLen; i++){ |
| 369 |
if(ca.get(i)>maxAllowedCoverage){ |
| 370 |
bs.set(i); |
| 371 |
numSet++; |
| 372 |
} |
| 373 |
} |
| 374 |
return numSet; |
| 375 |
} |
| 376 |
|
| 377 |
private static int setLowCoverage(BitSet bs, CoverageArray ca, int minAllowedCoverage, int maxLen){ |
| 378 |
int numSet=0; |
| 379 |
for(int i=0; i<maxLen; i++){ |
| 380 |
if(ca.get(i)<minAllowedCoverage){ |
| 381 |
bs.set(i); |
| 382 |
numSet++; |
| 383 |
} |
| 384 |
} |
| 385 |
return numSet; |
| 386 |
} |
| 387 |
|
| 388 |
private long maskFromBitsets(final boolean lowercase){ |
| 389 |
System.err.println("\nConverting masked bases to "+(lowercase ? "lower case" : "N")); |
| 390 |
long sum=0; |
| 391 |
for(Read r : map.values()){ |
| 392 |
sum+=maskRead(r, (BitSet)r.obj, lowercase); |
| 393 |
} |
| 394 |
System.err.println("Done Masking"); |
| 395 |
return sum; |
| 396 |
} |
| 397 |
|
| 398 |
public static int maskRead(final Read r, final BitSet bs, final boolean lowercase){ |
| 399 |
final byte[] bases=r.bases; |
| 400 |
int sum=0; |
| 401 |
if(!lowercase){ |
| 402 |
for(int i=0; i<bases.length; i++){ |
| 403 |
if(bs.get(i)){ |
| 404 |
if(bases[i]!='N'){sum++;} |
| 405 |
bases[i]='N'; |
| 406 |
}else if(CONVERT_NON_ACGTN && !AminoAcid.isACGTN(bases[i])){ |
| 407 |
bases[i]='N'; |
| 408 |
} |
| 409 |
} |
| 410 |
}else{ |
| 411 |
for(int i=0; i<bases.length; i++){ |
| 412 |
if(bs.get(i)){ |
| 413 |
if(!Tools.isLowerCase(bases[i]) && bases[i]!='N'){sum++;} |
| 414 |
bases[i]=(byte)Tools.toLowerCase(bases[i]); |
| 415 |
}else if(CONVERT_NON_ACGTN && !AminoAcid.isACGTN(bases[i])){ |
| 416 |
bases[i]='N'; |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
return sum; |
| 421 |
} |
| 422 |
|
| 423 |
private long splitFromBitsets(){ |
| 424 |
System.err.println("\nSplitting reads by removing masked bases"); |
| 425 |
long sum=0; |
| 426 |
|
| 427 |
LinkedHashMap<String, Read> map2=new LinkedHashMap<String, Read>(); |
| 428 |
|
| 429 |
for(String key : map.keySet()){ |
| 430 |
Read r=map.get(key); |
| 431 |
BitSet bs=((BitSet)r.obj); |
| 432 |
int rnum=0; |
| 433 |
if(bs.isEmpty()){ |
| 434 |
map2.put(key, r); |
| 435 |
}else{ |
| 436 |
byte[] bases=r.bases; |
| 437 |
byte[] quals=r.quality; |
| 438 |
|
| 439 |
int lastGood, lastBad; |
| 440 |
if(bs.get(0)){ |
| 441 |
lastGood=-1; |
| 442 |
lastBad=0; |
| 443 |
}else{ |
| 444 |
lastGood=0; |
| 445 |
lastBad=-1; |
| 446 |
} |
| 447 |
|
| 448 |
int i=1; |
| 449 |
for(; i<bases.length; i++){ |
| 450 |
if(bs.get(i)){ |
| 451 |
if(lastGood==i-1){ |
| 452 |
int len=lastGood-lastBad; |
| 453 |
if(len>0){ |
| 454 |
byte[] bases2=KillSwitch.copyOfRange(bases, lastBad+1, i); |
| 455 |
byte[] quals2=(quals==null ? null : KillSwitch.copyOfRange(quals, lastBad+1, i)); |
| 456 |
Read r2=new Read(bases2, quals2, r.id+"_"+rnum, r.numericID, r.flags); |
| 457 |
Read old=map2.put(r2.id, r2); |
| 458 |
assert(old==null) : "Duplicate id "+r2.id; |
| 459 |
} |
| 460 |
} |
| 461 |
lastBad=i; |
| 462 |
}else{ |
| 463 |
lastGood=i; |
| 464 |
} |
| 465 |
} |
| 466 |
if(lastGood==i-1){ |
| 467 |
int len=lastGood-lastBad; |
| 468 |
if(len>0){ |
| 469 |
byte[] bases2=KillSwitch.copyOfRange(bases, lastBad+1, i); |
| 470 |
byte[] quals2=(quals==null ? null : KillSwitch.copyOfRange(quals, lastBad+1, i)); |
| 471 |
Read r2=new Read(bases2, quals2, r.id+"_"+rnum, r.numericID, r.flags); |
| 472 |
Read old=map2.put(r2.id, r2); |
| 473 |
assert(old==null) : "Duplicate id "+r2.id; |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
map.clear(); |
| 480 |
map.putAll(map2); |
| 481 |
map2.clear(); |
| 482 |
|
| 483 |
System.err.println("Done Splitting"); |
| 484 |
return sum; |
| 485 |
} |
| 486 |
|
| 487 |
private void writeOutput(){ |
| 488 |
|
| 489 |
ConcurrentReadOutputStream ros=null; |
| 490 |
if(ffoutRef!=null){ |
| 491 |
final int buff=16; |
| 492 |
ros=ConcurrentReadOutputStream.getStream(ffoutRef, null, qfoutRef, null, buff, null, false); |
| 493 |
ros.start(); |
| 494 |
} |
| 495 |
|
| 496 |
long i=0; |
| 497 |
for(String name : map.keySet()){ |
| 498 |
Read r=map.get(name); |
| 499 |
ArrayList<Read> list=new ArrayList<Read>(1); |
| 500 |
list.add(r); |
| 501 |
if(ros!=null){ros.add(list, i);} |
| 502 |
i++; |
| 503 |
} |
| 504 |
errorState|=ReadWrite.closeStream(ros); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
private long maskSam(){ |
| 510 |
long before=0, after=0; |
| 511 |
for(Read r : map.values()){ |
| 512 |
before+=((BitSet)r.obj).cardinality(); |
| 513 |
} |
| 514 |
for(FileFormat ff : ffinSam){ |
| 515 |
|
| 516 |
maskSam_MT(ff); |
| 517 |
} |
| 518 |
for(Read r : map.values()){ |
| 519 |
after+=((BitSet)r.obj).cardinality(); |
| 520 |
} |
| 521 |
return after-before; |
| 522 |
} |
| 523 |
|
| 524 |
private void maskSam_MT(FileFormat ff){ |
| 525 |
|
| 526 |
final ConcurrentReadInputStream cris; |
| 527 |
{ |
| 528 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff, null, null, null); |
| 529 |
if(verbose){System.err.println("Started cris");} |
| 530 |
cris.start(); |
| 531 |
} |
| 532 |
|
| 533 |
MaskSamThread[] threads=new MaskSamThread[Shared.threads()]; |
| 534 |
|
| 535 |
for(int i=0; i<threads.length; i++){threads[i]=new MaskSamThread(cris);} |
| 536 |
for(int i=0; i<threads.length; i++){threads[i].start();} |
| 537 |
for(int i=0; i<threads.length; i++){ |
| 538 |
while(threads[i].getState()!=Thread.State.TERMINATED){ |
| 539 |
try { |
| 540 |
threads[i].join(); |
| 541 |
} catch (InterruptedException e) { |
| 542 |
|
| 543 |
e.printStackTrace(); |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
if(covmap!=null){ |
| 549 |
for(String rs : map.keySet()){ |
| 550 |
Read r=map.get(rs); |
| 551 |
BitSet bs=(BitSet)r.obj; |
| 552 |
CoverageArray ca=covmap.remove(rs); |
| 553 |
if(maxcov>-1){ |
| 554 |
setHighCoverage(bs, ca, maxcov, r.length()); |
| 555 |
} |
| 556 |
if(mincov>-1){ |
| 557 |
setLowCoverage(bs, ca, mincov, r.length()); |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
errorState|=ReadWrite.closeStreams(cris); |
| 563 |
|
| 564 |
if(errorState){ |
| 565 |
throw new RuntimeException("BBMask terminated in an error state; the output may be corrupt."); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
private class MaskSamThread extends Thread{ |
| 570 |
|
| 571 |
MaskSamThread(ConcurrentReadInputStream cris_){ |
| 572 |
cris=cris_; |
| 573 |
} |
| 574 |
|
| 575 |
@Override |
| 576 |
public void run(){ |
| 577 |
maskSam(cris); |
| 578 |
} |
| 579 |
|
| 580 |
final ConcurrentReadInputStream cris; |
| 581 |
|
| 582 |
|
| 583 |
} |
| 584 |
|
| 585 |
private void maskSam_ST(FileFormat ff){ |
| 586 |
|
| 587 |
final ConcurrentReadInputStream cris; |
| 588 |
{ |
| 589 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff, null, null, null); |
| 590 |
if(verbose){System.err.println("Started cris");} |
| 591 |
cris.start(); |
| 592 |
} |
| 593 |
|
| 594 |
maskSam(cris); |
| 595 |
|
| 596 |
errorState|=ReadWrite.closeStreams(cris); |
| 597 |
|
| 598 |
if(errorState){ |
| 599 |
throw new RuntimeException("BBMask terminated in an error state; the output may be corrupt."); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
private void maskSam(ConcurrentReadInputStream cris){ |
| 604 |
|
| 605 |
long samReads=0; |
| 606 |
long samBases=0; |
| 607 |
IntList ranges=new IntList(16); |
| 608 |
|
| 609 |
{ |
| 610 |
|
| 611 |
ListNum<Read> ln=cris.nextList(); |
| 612 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 613 |
|
| 614 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 615 |
|
| 616 |
for(int idx=0; idx<reads.size(); idx++){ |
| 617 |
final Read r=reads.get(idx); |
| 618 |
assert(r.mate==null); |
| 619 |
|
| 620 |
final int initialLength1=r.length(); |
| 621 |
|
| 622 |
{ |
| 623 |
samReads++; |
| 624 |
samBases+=initialLength1; |
| 625 |
|
| 626 |
if(r.mapped()){ |
| 627 |
SamLine sl=r.samline; |
| 628 |
assert(sl!=null) : "No sam line for read "+r; |
| 629 |
byte[] rname=sl.rname(); |
| 630 |
assert(rname!=null) : "No rname for sam line "+sl; |
| 631 |
final String rs=new String(rname); |
| 632 |
Read ref=map.get(rs); |
| 633 |
if(ref==null){ |
| 634 |
handleNoRef(rs); |
| 635 |
}else{ |
| 636 |
final int reflen=ref.length(); |
| 637 |
assert(ref!=null) : "Could not find reference scaffold '"+rs+"' for samline \n"+sl+"\n in set \n"+map.keySet()+"\n"; |
| 638 |
if(covmap==null){ |
| 639 |
BitSet bs=(BitSet)ref.obj; |
| 640 |
mask(bs, sl, reflen); |
| 641 |
}else{ |
| 642 |
CoverageArray ca=covmap.get(rs); |
| 643 |
increment(ca, sl, r.match, reflen, ranges, includeDeletionCoverage, samPad); |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
cris.returnList(ln); |
| 651 |
ln=cris.nextList(); |
| 652 |
reads=(ln!=null ? ln.list : null); |
| 653 |
} |
| 654 |
if(ln!=null){ |
| 655 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
synchronized(this){ |
| 660 |
this.samBases+=samBases; |
| 661 |
this.samReads+=samReads; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
private void mask(BitSet bs, SamLine sl, int reflen){ |
| 666 |
final int start=Tools.max(0, sl.start(true, false)-samPad); |
| 667 |
final int stop=Tools.min(sl.stop(start, true, false)+1+samPad, reflen); |
| 668 |
if(stop>start){ |
| 669 |
synchronized(bs){ |
| 670 |
bs.set(start, stop); |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
public static void increment(CoverageArray ca, SamLine sl, byte[] match, int reflen, IntList ranges, boolean includeDels, int samPad){ |
| 676 |
final int start=Tools.max(0, sl.start(true, false)); |
| 677 |
final int stop=Tools.min(sl.stop(start, true, false)+1, reflen); |
| 678 |
if(stop>start){ |
| 679 |
ranges.clear(); |
| 680 |
boolean numeric=false; |
| 681 |
if(match==null){ |
| 682 |
assert(sl.cigar!=null); |
| 683 |
match=sl.toShortMatch(true); |
| 684 |
numeric=true; |
| 685 |
}else{ |
| 686 |
for(byte b : match){ |
| 687 |
if(Tools.isDigit(b)){ |
| 688 |
numeric=true; |
| 689 |
break; |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
if(numeric){match=Read.toLongMatchString(match);} |
| 694 |
fillRanges(match, start, stop, ranges, includeDels); |
| 695 |
|
| 696 |
if(ranges.size>0){ |
| 697 |
if(samPad!=0){ |
| 698 |
ranges.set(0, Tools.mid(0, reflen, ranges.get(0)-samPad)); |
| 699 |
ranges.set(1, Tools.mid(0, reflen, ranges.get(1)+samPad)); |
| 700 |
for(int i=2; i<ranges.size; i+=2){ |
| 701 |
ranges.set(i, Tools.mid(ranges.get(i-1), reflen, ranges.get(i)-samPad)); |
| 702 |
ranges.set(i+1, Tools.mid(0, reflen, ranges.get(i+1)+samPad)); |
| 703 |
} |
| 704 |
} |
| 705 |
synchronized(ca){ |
| 706 |
ca.incrementRanges(ranges, 1); |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
public static void fillRanges(byte[] longmatch, int start, int stop, IntList ranges, boolean includeDels){ |
| 713 |
assert(ranges.size==0); |
| 714 |
byte mode='?', lastMode='?'; |
| 715 |
int rpos=start; |
| 716 |
int lastRpos=start; |
| 717 |
int rstart=start; |
| 718 |
for(int mpos=0; mpos<longmatch.length; mpos++){ |
| 719 |
byte m=longmatch[mpos]; |
| 720 |
if(m=='m' || m=='s' || m=='S' || m=='N' || m=='B'){ |
| 721 |
mode='m'; |
| 722 |
rpos++; |
| 723 |
}else if(m=='I' || m=='X' || m=='Y'){ |
| 724 |
mode='I'; |
| 725 |
}else if(m=='D'){ |
| 726 |
mode='D'; |
| 727 |
rpos++; |
| 728 |
}else if(m=='C'){ |
| 729 |
mode='C'; |
| 730 |
rpos++; |
| 731 |
}else{ |
| 732 |
throw new RuntimeException("Invalid match string character '"+(char)m+"' = "+m+" (ascii). " + |
| 733 |
"Match string should be in long format here."); |
| 734 |
} |
| 735 |
if(mode!=lastMode){ |
| 736 |
if(mpos>0){ |
| 737 |
if(lastMode=='m'){ |
| 738 |
ranges.add(rstart); |
| 739 |
ranges.add(lastRpos); |
| 740 |
}else if(mode=='D' && includeDels){ |
| 741 |
ranges.add(rstart); |
| 742 |
ranges.add(lastRpos); |
| 743 |
} |
| 744 |
} |
| 745 |
rstart=lastRpos; |
| 746 |
} |
| 747 |
lastMode=mode; |
| 748 |
lastRpos=rpos; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
if(lastMode=='m'){ |
| 753 |
ranges.add(rstart); |
| 754 |
ranges.add(lastRpos); |
| 755 |
}else if(mode=='D' && includeDels){ |
| 756 |
ranges.add(rstart); |
| 757 |
ranges.add(lastRpos); |
| 758 |
} |
| 759 |
assert(rpos==stop) : start+", "+stop+", "+rpos+", "+rstart+", "+lastRpos+"\n"+new String(longmatch)+"\n"+ranges; |
| 760 |
assert((ranges.size&1)==0); |
| 761 |
} |
| 762 |
|
| 763 |
private void handleNoRef(String rname){ |
| 764 |
assert(rname!=null); |
| 765 |
String ret=norefSet.putIfAbsent(rname, rname); |
| 766 |
if(ret==null){ |
| 767 |
System.err.println("Warning! Scaffold not found in assembly: "+rname); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
private LinkedHashMap<String, Read> hashRef(){ |
| 774 |
|
| 775 |
|
| 776 |
final ConcurrentReadInputStream cris; |
| 777 |
{ |
| 778 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffinRef, null, qfinRef, null); |
| 779 |
if(verbose){System.err.println("Started cris");} |
| 780 |
cris.start(); |
| 781 |
} |
| 782 |
|
| 783 |
final LinkedHashMap<String, Read> hmr=new LinkedHashMap<String, Read>(); |
| 784 |
if(ffinSam!=null && (mincov>=0 || maxcov>=0)){ |
| 785 |
covmap=new HashMap<String, CoverageArray>(); |
| 786 |
} |
| 787 |
|
| 788 |
{ |
| 789 |
|
| 790 |
ListNum<Read> ln=cris.nextList(); |
| 791 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 792 |
|
| 793 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 794 |
|
| 795 |
for(int idx=0; idx<reads.size(); idx++){ |
| 796 |
final Read r=reads.get(idx); |
| 797 |
final int len=r.length(); |
| 798 |
final byte[] bases=r.bases; |
| 799 |
|
| 800 |
|
| 801 |
final BitSet bs=new BitSet(len); |
| 802 |
r.obj=bs; |
| 803 |
|
| 804 |
if(covmap!=null){ |
| 805 |
if(bits32){ |
| 806 |
covmap.put(r.id, new CoverageArray3(covmap.size(), len)); |
| 807 |
}else{ |
| 808 |
covmap.put(r.id, new CoverageArray2(covmap.size(), len)); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
if(MaskByLowercase){ |
| 813 |
for(int i=0; i<len; i++){ |
| 814 |
if(bases[i]=='N' || Tools.isLowerCase(bases[i])){bs.set(i);} |
| 815 |
} |
| 816 |
}else{ |
| 817 |
for(int i=0; i<len; i++){ |
| 818 |
if(bases[i]=='N'){bs.set(i);} |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
refReads++; |
| 823 |
refBases+=len; |
| 824 |
Read old=hmr.put(r.id, r); |
| 825 |
assert(old==null) : "Duplicate reference scaffold name "+r.id; |
| 826 |
} |
| 827 |
|
| 828 |
cris.returnList(ln); |
| 829 |
ln=cris.nextList(); |
| 830 |
reads=(ln!=null ? ln.list : null); |
| 831 |
} |
| 832 |
if(ln!=null){ |
| 833 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
errorState|=ReadWrite.closeStreams(cris); |
| 838 |
|
| 839 |
if(errorState){ |
| 840 |
throw new RuntimeException("BBMask terminated in an error state; the output may be corrupt."); |
| 841 |
} |
| 842 |
|
| 843 |
return hmr; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
private long maskLowComplexity(short[][] matrix){ |
| 850 |
long sum=0; |
| 851 |
if(matrix==null){matrix=new short[16][];} |
| 852 |
for(Read r : map.values()){ |
| 853 |
sum+=maskLowComplexity(r, mink2, maxk2, window, ratio, matrix); |
| 854 |
} |
| 855 |
return sum; |
| 856 |
} |
| 857 |
|
| 858 |
private static int maskLowComplexity(Read r, int mink, int maxk, int window, float ratio, short[][] matrix){ |
| 859 |
|
| 860 |
final byte[] bases=r.bases; |
| 861 |
final BitSet bs=(BitSet)r.obj; |
| 862 |
|
| 863 |
int before=bs.cardinality(); |
| 864 |
|
| 865 |
|
| 866 |
for(int k=mink; k<=maxk; k++){ |
| 867 |
if(matrix[k]==null){matrix[k]=new short[(1<<(2*k))];} |
| 868 |
} |
| 869 |
|
| 870 |
for(int k=mink; k<=maxk; k++){ |
| 871 |
final short[] counts=matrix[k]; |
| 872 |
final int kmerspace=(1<<(2*k)); |
| 873 |
final int mincount=(int)Math.ceil(ratio*Tools.min(window, kmerspace)); |
| 874 |
maskLowComplexity(bases, bs, k, window, mincount, counts); |
| 875 |
} |
| 876 |
|
| 877 |
int after=bs.cardinality(); |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
return after-before; |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
private static void maskLowComplexity(final byte[] bases, final BitSet bs, final int k, final int window, final int mincount, final short[] counts){ |
| 886 |
assert(k>0) : "k must be greater than 0"; |
| 887 |
|
| 888 |
if(verify){ |
| 889 |
for(int c : counts){assert(c==0);} |
| 890 |
} |
| 891 |
|
| 892 |
final int mask=(k>15 ? -1 : ~((-1)<<(2*k))); |
| 893 |
int current=0, ns=0; |
| 894 |
int kmer=0, kmer2=0; |
| 895 |
|
| 896 |
for(int i=0, i2=-window; i2<bases.length; i++, i2++){ |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
if(i<bases.length){ |
| 901 |
final byte b=bases[i]; |
| 902 |
final int n=Dedupe.baseToNumber[b]; |
| 903 |
kmer=((kmer<<2)|n)&mask; |
| 904 |
|
| 905 |
if(!AminoAcid.isFullyDefined(b)){ns++;} |
| 906 |
if(counts[kmer]<1){ |
| 907 |
assert(counts[kmer]==0); |
| 908 |
current++; |
| 909 |
} |
| 910 |
counts[kmer]++; |
| 911 |
if(verify){assert(current==Tools.cardinality(counts)) : current+", "+Tools.cardinality(counts)+"\n"+Arrays.toString(counts);} |
| 912 |
|
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
if(i2>=0){ |
| 917 |
final byte b2=bases[i2]; |
| 918 |
final int n2=Dedupe.baseToNumber[b2]; |
| 919 |
kmer2=((kmer2<<2)|n2)&mask; |
| 920 |
|
| 921 |
if(!AminoAcid.isFullyDefined(b2)){ |
| 922 |
ns--; |
| 923 |
assert(ns>=0); |
| 924 |
} |
| 925 |
counts[kmer2]--; |
| 926 |
if(counts[kmer2]<1){ |
| 927 |
assert(counts[kmer2]==0) : Arrays.toString(counts); |
| 928 |
current--; |
| 929 |
} |
| 930 |
if(verify){assert(current==Tools.cardinality(counts)) : current+", "+Tools.cardinality(counts)+"\n"+Arrays.toString(counts);} |
| 931 |
|
| 932 |
|
| 933 |
} |
| 934 |
|
| 935 |
if(verify && i2>-1 && i<bases.length){ |
| 936 |
assert(shared.Vector.sum(counts)==window); |
| 937 |
} |
| 938 |
|
| 939 |
if(current<mincount && ns<1 && i2>=-1 && i<bases.length){ |
| 940 |
|
| 941 |
bs.set(i2+1, i+1); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
private long maskLowEntropy(){ |
| 954 |
ArrayBlockingQueue<Read> queue=new ArrayBlockingQueue<Read>(map.size()); |
| 955 |
for(Read r : map.values()){queue.add(r);} |
| 956 |
int numThreads=Tools.min(Shared.threads(), queue.size()); |
| 957 |
MaskLowEntropyThread[] threads=new MaskLowEntropyThread[numThreads]; |
| 958 |
long sum=0; |
| 959 |
|
| 960 |
for(int i=0; i<threads.length; i++){threads[i]=new MaskLowEntropyThread(queue, mink2, maxk2, window, entropyCutoff);} |
| 961 |
for(int i=0; i<threads.length; i++){threads[i].start();} |
| 962 |
for(int i=0; i<threads.length; i++){ |
| 963 |
while(threads[i].getState()!=Thread.State.TERMINATED){ |
| 964 |
try { |
| 965 |
threads[i].join(); |
| 966 |
} catch (InterruptedException e) { |
| 967 |
|
| 968 |
e.printStackTrace(); |
| 969 |
} |
| 970 |
} |
| 971 |
sum+=threads[i].masked; |
| 972 |
} |
| 973 |
return sum; |
| 974 |
} |
| 975 |
|
| 976 |
private class MaskLowEntropyThread extends Thread{ |
| 977 |
|
| 978 |
MaskLowEntropyThread(ArrayBlockingQueue<Read> queue_, int mink_, int maxk_, int window_, float cutoff_){ |
| 979 |
queue=queue_; |
| 980 |
minkT=mink_; |
| 981 |
maxkT=maxk_; |
| 982 |
windowT=(short)window_; |
| 983 |
cutoff=cutoff_; |
| 984 |
trackers=new EntropyTracker[maxkT+1]; |
| 985 |
for(int i=minkT; i<=maxkT; i++){ |
| 986 |
trackers[i]=new EntropyTracker(i, windowT, false, cutoff, true); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
@Override |
| 991 |
public void run(){ |
| 992 |
for(Read r=queue.poll(); r!=null; r=queue.poll()){ |
| 993 |
masked+=maskLowEntropy(r, minkT, maxkT, trackers); |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
final ArrayBlockingQueue<Read> queue; |
| 998 |
final int minkT; |
| 999 |
final int maxkT; |
| 1000 |
final float cutoff; |
| 1001 |
final int windowT; |
| 1002 |
final EntropyTracker[] trackers; |
| 1003 |
long masked=0; |
| 1004 |
|
| 1005 |
} |
| 1006 |
|
| 1007 |
private int maskLowEntropy(Read r, int mink, int maxk, EntropyTracker[] trackers){ |
| 1008 |
|
| 1009 |
|
| 1010 |
final byte[] bases=r.bases; |
| 1011 |
final BitSet bs=(BitSet)r.obj; |
| 1012 |
|
| 1013 |
int before=bs.cardinality(); |
| 1014 |
|
| 1015 |
for(int k=mink; k<=maxk; k++){ |
| 1016 |
maskLowEntropy(bases, bs, trackers[k]); |
| 1017 |
} |
| 1018 |
|
| 1019 |
int after=bs.cardinality(); |
| 1020 |
|
| 1021 |
return after-before; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
private static void maskLowEntropy(final byte[] bases, final BitSet bs, final EntropyTracker et){ |
| 1026 |
final int window=et.windowBases(); |
| 1027 |
if(bases.length<window){return;} |
| 1028 |
|
| 1029 |
et.clear(); |
| 1030 |
for(int i=0, min=window-1; i<bases.length; i++){ |
| 1031 |
et.add(bases[i]); |
| 1032 |
if(i>=min && et.ns()<1 && !et.passes()){bs.set(et.leftPos(), et.rightPos()+1);} |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
private long maskRepeats_ST(){ |
| 1042 |
long sum=0; |
| 1043 |
for(Read r : map.values()){ |
| 1044 |
sum+=maskRepeats(r, mink, maxk, mincount, minlen); |
| 1045 |
} |
| 1046 |
return sum; |
| 1047 |
} |
| 1048 |
|
| 1049 |
private long maskRepeats(){ |
| 1050 |
ArrayBlockingQueue<Read> queue=new ArrayBlockingQueue<Read>(map.size()); |
| 1051 |
for(Read r : map.values()){queue.add(r);} |
| 1052 |
int numThreads=Tools.min(Shared.threads(), queue.size()); |
| 1053 |
MaskRepeatThread[] threads=new MaskRepeatThread[numThreads]; |
| 1054 |
long sum=0; |
| 1055 |
for(int i=0; i<threads.length; i++){threads[i]=new MaskRepeatThread(queue, mink, maxk, mincount, minlen);} |
| 1056 |
for(int i=0; i<threads.length; i++){threads[i].start();} |
| 1057 |
for(int i=0; i<threads.length; i++){ |
| 1058 |
while(threads[i].getState()!=Thread.State.TERMINATED){ |
| 1059 |
try { |
| 1060 |
threads[i].join(); |
| 1061 |
} catch (InterruptedException e) { |
| 1062 |
|
| 1063 |
e.printStackTrace(); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
sum+=threads[i].masked; |
| 1067 |
} |
| 1068 |
return sum; |
| 1069 |
} |
| 1070 |
|
| 1071 |
private class MaskRepeatThread extends Thread{ |
| 1072 |
|
| 1073 |
MaskRepeatThread(ArrayBlockingQueue<Read> queue_, int mink_, int maxk_, int mincount_, int minlen_){ |
| 1074 |
queue=queue_; |
| 1075 |
minkT=mink_; |
| 1076 |
maxkT=maxk_; |
| 1077 |
mincountT=mincount_; |
| 1078 |
minlenT=minlen_; |
| 1079 |
} |
| 1080 |
|
| 1081 |
@Override |
| 1082 |
public void run(){ |
| 1083 |
for(Read r=queue.poll(); r!=null; r=queue.poll()){ |
| 1084 |
masked+=maskRepeats(r, minkT, maxkT, mincountT, minlenT); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
final ArrayBlockingQueue<Read> queue; |
| 1089 |
final int minkT; |
| 1090 |
final int maxkT; |
| 1091 |
final int mincountT; |
| 1092 |
final int minlenT; |
| 1093 |
long masked=0; |
| 1094 |
|
| 1095 |
} |
| 1096 |
|
| 1097 |
private static int maskRepeats(Read r, int mink, int maxk, int mincount, int minlen){ |
| 1098 |
final byte[] bases=r.bases; |
| 1099 |
final BitSet bs=(BitSet)r.obj; |
| 1100 |
|
| 1101 |
int before=bs.cardinality(); |
| 1102 |
|
| 1103 |
|
| 1104 |
for(int k=mink; k<=maxk; k++){ |
| 1105 |
maskRepeats(bases, bs, k, Tools.max(minlen, k*mincount)); |
| 1106 |
} |
| 1107 |
|
| 1108 |
int after=bs.cardinality(); |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
return after-before; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
private static void maskRepeats(final byte[] bases, final BitSet bs, final int k, final int minlen){ |
| 1117 |
final int lim=bases.length-k; |
| 1118 |
final int mask=(k>15 ? -1 : ~((-1)<<(2*k))); |
| 1119 |
for(int loc=0; loc<lim; loc++){ |
| 1120 |
int len=repeatLength(bases, k, mask, loc); |
| 1121 |
if(len>=minlen){ |
| 1122 |
int a=loc-k, b=loc-k+len; |
| 1123 |
bs.set(a, b); |
| 1124 |
|
| 1125 |
loc=Tools.max(loc, b-minlen); |
| 1126 |
|
| 1127 |
}else{ |
| 1128 |
|
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
private static int repeatLength(final byte[] bases, final int k, final int mask, final int loc){ |
| 1136 |
|
| 1137 |
final int lim=bases.length; |
| 1138 |
final int key=getInitialKey(bases, loc, k); |
| 1139 |
if(key<0){return 0;} |
| 1140 |
int kmer=key; |
| 1141 |
int gap=0, last=-1; |
| 1142 |
for(int i=loc; i<lim && gap<k; i++){ |
| 1143 |
final byte b=bases[i]; |
| 1144 |
final int n=Dedupe.baseToNumber[b]; |
| 1145 |
kmer=((kmer<<2)|n)&mask; |
| 1146 |
if(kmer==key){ |
| 1147 |
last=i; |
| 1148 |
gap=0; |
| 1149 |
}else{ |
| 1150 |
gap++; |
| 1151 |
} |
| 1152 |
|
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
return (last<0 ? 0 : last-loc+k+1); |
| 1158 |
} |
| 1159 |
|
| 1160 |
private static int getInitialKey(byte[] bases, int loc, int k){ |
| 1161 |
assert(k<16); |
| 1162 |
int start=loc-k; |
| 1163 |
int key=0; |
| 1164 |
if(start<0){return -1;} |
| 1165 |
for(int i=start; i<loc; i++){ |
| 1166 |
final byte b=bases[i]; |
| 1167 |
final int n=Dedupe.baseToNumber[b]; |
| 1168 |
key=(key<<2)|n; |
| 1169 |
} |
| 1170 |
assert(key>=0); |
| 1171 |
return key; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
private LinkedHashMap<String, Read> map=null; |
| 1181 |
private ConcurrentHashMap<String, String> norefSet=new ConcurrentHashMap<String, String>(256, .75f, 16); |
| 1182 |
private HashMap<String, CoverageArray> covmap=null; |
| 1183 |
|
| 1184 |
|
| 1185 |
private long refReads=0; |
| 1186 |
private long refBases=0; |
| 1187 |
|
| 1188 |
|
| 1189 |
private long samReads=0; |
| 1190 |
private long samBases=0; |
| 1191 |
|
| 1192 |
|
| 1193 |
public boolean errorState=false; |
| 1194 |
|
| 1195 |
private String inRef=null; |
| 1196 |
private ArrayList<String> inSam=new ArrayList<String>(); |
| 1197 |
|
| 1198 |
private String qfinRef=null; |
| 1199 |
|
| 1200 |
private String outRef=null; |
| 1201 |
|
| 1202 |
private String qfoutRef=null; |
| 1203 |
|
| 1204 |
private String extinRef=null; |
| 1205 |
private String extoutRef=null; |
| 1206 |
|
| 1207 |
private boolean overwrite=true; |
| 1208 |
private boolean append=false; |
| 1209 |
|
| 1210 |
private long maxReads=-1; |
| 1211 |
|
| 1212 |
private boolean processRepeats=false; |
| 1213 |
private int mink=5; |
| 1214 |
private int maxk=5; |
| 1215 |
private int minlen=40; |
| 1216 |
private int mincount=4; |
| 1217 |
|
| 1218 |
private boolean processEntropy=true; |
| 1219 |
private boolean entropyMode=true; |
| 1220 |
private boolean splitMode=false; |
| 1221 |
private int mink2=5; |
| 1222 |
private int maxk2=5; |
| 1223 |
private int window=80; |
| 1224 |
private float ratio=0.35f; |
| 1225 |
private float entropyCutoff=0.70f; |
| 1226 |
|
| 1227 |
|
| 1228 |
private boolean bits32=true; |
| 1229 |
|
| 1230 |
private boolean includeDeletionCoverage=true; |
| 1231 |
|
| 1232 |
|
| 1233 |
private int mincov=-1; |
| 1234 |
|
| 1235 |
private int maxcov=-1; |
| 1236 |
|
| 1237 |
private int samPad=0; |
| 1238 |
|
| 1239 |
private final FileFormat ffinRef; |
| 1240 |
private final FileFormat[] ffinSam; |
| 1241 |
|
| 1242 |
private final FileFormat ffoutRef; |
| 1243 |
|
| 1244 |
private PrintStream outstream=System.err; |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
public static boolean verbose=false; |
| 1249 |
public static boolean CONVERT_NON_ACGTN=true; |
| 1250 |
private static boolean verify=false; |
| 1251 |
private static boolean MaskByLowercase=false; |
| 1252 |
|
| 1253 |
} |