| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Collection; |
| 7 |
import java.util.HashSet; |
| 8 |
import java.util.LinkedHashSet; |
| 9 |
|
| 10 |
import dna.AminoAcid; |
| 11 |
import fileIO.ByteFile; |
| 12 |
import fileIO.FileFormat; |
| 13 |
import fileIO.ReadWrite; |
| 14 |
import shared.Parse; |
| 15 |
import shared.Parser; |
| 16 |
import shared.PreParser; |
| 17 |
import shared.Shared; |
| 18 |
import shared.Timer; |
| 19 |
import shared.Tools; |
| 20 |
import stream.ConcurrentReadInputStream; |
| 21 |
import stream.ConcurrentReadOutputStream; |
| 22 |
import stream.FASTQ; |
| 23 |
import stream.FastaReadInputStream; |
| 24 |
import stream.Read; |
| 25 |
import structures.ListNum; |
| 26 |
import tracker.ReadStats; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class FilterBySequence { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static void main(String[] args){ |
| 47 |
|
| 48 |
Timer t=new Timer(); |
| 49 |
|
| 50 |
|
| 51 |
FilterBySequence x=new FilterBySequence(args); |
| 52 |
|
| 53 |
|
| 54 |
x.process(t); |
| 55 |
|
| 56 |
|
| 57 |
Shared.closeStream(x.outstream); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public FilterBySequence(String[] args){ |
| 65 |
|
| 66 |
{ |
| 67 |
PreParser pp=new PreParser(args, getClass(), false); |
| 68 |
args=pp.args; |
| 69 |
outstream=pp.outstream; |
| 70 |
} |
| 71 |
|
| 72 |
boolean setInterleaved=false; |
| 73 |
|
| 74 |
|
| 75 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 76 |
ReadWrite.setZipThreads(Shared.threads()); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
Parser parser=new Parser(); |
| 82 |
|
| 83 |
|
| 84 |
for(int i=0; i<args.length; i++){ |
| 85 |
String arg=args[i]; |
| 86 |
|
| 87 |
|
| 88 |
String[] split=arg.split("="); |
| 89 |
String a=split[0].toLowerCase(); |
| 90 |
String b=split.length>1 ? split[1] : null; |
| 91 |
|
| 92 |
if(a.equals("verbose")){ |
| 93 |
verbose=Parse.parseBoolean(b); |
| 94 |
}else if(a.equals("ordered")){ |
| 95 |
ordered=Parse.parseBoolean(b); |
| 96 |
}else if(a.equals("storebases") || a.equals("keepbases") || a.equals("sb")){ |
| 97 |
storeBases=Parse.parseBoolean(b); |
| 98 |
}else if(a.equals("include")){ |
| 99 |
include=Parse.parseBoolean(b); |
| 100 |
}else if(a.equals("exclude")){ |
| 101 |
include=!Parse.parseBoolean(b); |
| 102 |
}else if(a.equals("rcomp")){ |
| 103 |
rcomp=Parse.parseBoolean(b); |
| 104 |
}else if(a.equals("casesensitive") || a.equals("case")){ |
| 105 |
toUpperCase=!Parse.parseBoolean(b); |
| 106 |
}else if(a.equals("hdist") || a.equals("subs") || a.equals("s")){ |
| 107 |
maxSubs=Integer.parseInt(b); |
| 108 |
}else if(a.equals("mismatchfraction") || a.equals("mf") || a.equals("f") || a.equals("fraction") |
| 109 |
|| a.equals("sf") || a.equals("subfraction")){ |
| 110 |
mismatchFraction=Float.parseFloat(b); |
| 111 |
}else if(a.equals("lengthdif") || a.equals("maxlengthdif") || a.equals("lendif")){ |
| 112 |
maxLengthDif=Integer.parseInt(b); |
| 113 |
}else if(a.equals("parse_flag_goes_here")){ |
| 114 |
long fake_variable=Parse.parseKMG(b); |
| 115 |
|
| 116 |
}else if(a.equals("ref")){ |
| 117 |
if(b==null){ref=null;} |
| 118 |
else{ref=b.split(",");} |
| 119 |
}else if(a.equals("literal")){ |
| 120 |
if(b==null){literal=null;} |
| 121 |
else{literal=b.split(",");} |
| 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 |
{ |
| 131 |
Parser.processQuality(); |
| 132 |
|
| 133 |
maxReads=parser.maxReads; |
| 134 |
|
| 135 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 136 |
append=ReadStats.append=parser.append; |
| 137 |
setInterleaved=parser.setInterleaved; |
| 138 |
|
| 139 |
in1=parser.in1; |
| 140 |
in2=parser.in2; |
| 141 |
qfin1=parser.qfin1; |
| 142 |
qfin2=parser.qfin2; |
| 143 |
|
| 144 |
out1=parser.out1; |
| 145 |
out2=parser.out2; |
| 146 |
qfout1=parser.qfout1; |
| 147 |
qfout2=parser.qfout2; |
| 148 |
|
| 149 |
extin=parser.extin; |
| 150 |
extout=parser.extout; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 155 |
in2=in1.replace("#", "2"); |
| 156 |
in1=in1.replace("#", "1"); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 161 |
out2=out1.replace("#", "2"); |
| 162 |
out1=out1.replace("#", "1"); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
if(in2!=null){ |
| 167 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 168 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 169 |
} |
| 170 |
|
| 171 |
assert(FastaReadInputStream.settingsOK()); |
| 172 |
|
| 173 |
|
| 174 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 175 |
|
| 176 |
|
| 177 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 178 |
ByteFile.FORCE_MODE_BF2=true; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 183 |
|
| 184 |
|
| 185 |
if(!setInterleaved){ |
| 186 |
assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 187 |
if(in2!=null){ |
| 188 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 189 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 190 |
}else{ |
| 191 |
if(out2!=null){ |
| 192 |
FASTQ.FORCE_INTERLEAVED=true; |
| 193 |
FASTQ.TEST_INTERLEAVED=false; |
| 194 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 201 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 202 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
if(!Tools.testInputFiles(false, true, in1, in2)){ |
| 207 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
if(!Tools.testInputFiles(true, true, ref)){ |
| 212 |
throw new RuntimeException("\nCan't read to some reference files.\n"); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2)){ |
| 217 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 222 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 223 |
|
| 224 |
|
| 225 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 226 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 227 |
|
| 228 |
assert(ref!=null || literal!=null) : "No reference sequences."; |
| 229 |
|
| 230 |
if(ref!=null){ |
| 231 |
ffref=new FileFormat[ref.length]; |
| 232 |
for(int i=0; i<ref.length; i++){ |
| 233 |
ffref[i]=FileFormat.testInput(ref[i], FileFormat.FASTQ, null, true, true); |
| 234 |
} |
| 235 |
}else{ |
| 236 |
ffref=null; |
| 237 |
} |
| 238 |
|
| 239 |
refSet=new HashSet<Code>(); |
| 240 |
refList=new ArrayList<byte[]>(); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
void process(Timer t){ |
| 249 |
|
| 250 |
|
| 251 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 252 |
Read.VALIDATE_IN_CONSTRUCTOR=Shared.threads()<4; |
| 253 |
|
| 254 |
{ |
| 255 |
if(ffref!=null){ |
| 256 |
for(FileFormat ff : ffref){ |
| 257 |
processReference(ff); |
| 258 |
} |
| 259 |
} |
| 260 |
if(literal!=null){ |
| 261 |
for(String s : literal){ |
| 262 |
addToRef(new Code(s.getBytes())); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
System.err.println("Loaded "+refSet.size()+" unique reference sequence"+(refSet.size()==1 ? "." : "s.")); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
final ConcurrentReadInputStream cris; |
| 271 |
{ |
| 272 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, qfin1, qfin2); |
| 273 |
cris.start(); |
| 274 |
if(verbose){outstream.println("Started cris");} |
| 275 |
} |
| 276 |
boolean paired=cris.paired(); |
| 277 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 278 |
|
| 279 |
|
| 280 |
final ConcurrentReadOutputStream ros; |
| 281 |
if(ffout1!=null){ |
| 282 |
|
| 283 |
final int buff=(ordered ? Tools.mid(16, 128, (Shared.threads()*2)/3) : 8); |
| 284 |
|
| 285 |
|
| 286 |
if(cris.paired() && out2==null && (in1!=null && !ffin1.samOrBam() && !ffout1.samOrBam())){ |
| 287 |
outstream.println("Writing interleaved."); |
| 288 |
} |
| 289 |
|
| 290 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, qfout1, qfout2, buff, null, false); |
| 291 |
ros.start(); |
| 292 |
}else{ros=null;} |
| 293 |
|
| 294 |
|
| 295 |
readsProcessed=0; |
| 296 |
basesProcessed=0; |
| 297 |
|
| 298 |
|
| 299 |
spawnProcessThreads(cris, ros); |
| 300 |
|
| 301 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 302 |
|
| 303 |
|
| 304 |
errorState|=ReadStats.writeAll(); |
| 305 |
|
| 306 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 307 |
|
| 308 |
|
| 309 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 310 |
|
| 311 |
|
| 312 |
t.stop(); |
| 313 |
outstream.println(); |
| 314 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 12)); |
| 315 |
outstream.println(); |
| 316 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 12, false)); |
| 317 |
|
| 318 |
|
| 319 |
if(errorState){ |
| 320 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
private void processReference(final FileFormat ff){ |
| 326 |
final ConcurrentReadInputStream cris; |
| 327 |
{ |
| 328 |
cris=ConcurrentReadInputStream.getReadInputStream(-1, false, ff, null, null, null); |
| 329 |
cris.start(); |
| 330 |
} |
| 331 |
spawnLoadThreads(cris); |
| 332 |
ReadWrite.closeStream(cris); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
private void spawnLoadThreads(final ConcurrentReadInputStream cris){ |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
final int threads=Shared.threads(); |
| 343 |
|
| 344 |
|
| 345 |
ArrayList<LoadThread> alpt=new ArrayList<LoadThread>(threads); |
| 346 |
for(int i=0; i<threads; i++){ |
| 347 |
alpt.add(new LoadThread(cris, i)); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
for(LoadThread pt : alpt){ |
| 352 |
pt.start(); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
boolean success=true; |
| 357 |
for(LoadThread pt : alpt){ |
| 358 |
|
| 359 |
|
| 360 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 361 |
try { |
| 362 |
|
| 363 |
pt.join(); |
| 364 |
} catch (InterruptedException e) { |
| 365 |
|
| 366 |
e.printStackTrace(); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
readsLoaded+=pt.readsProcessedT; |
| 372 |
basesLoaded+=pt.basesProcessedT; |
| 373 |
success&=pt.success; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
if(!success){errorState=true;} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
private void spawnProcessThreads(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros){ |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
final int threads=Shared.threads(); |
| 390 |
|
| 391 |
|
| 392 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 393 |
for(int i=0; i<threads; i++){ |
| 394 |
alpt.add(new ProcessThread(cris, ros, i)); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
for(ProcessThread pt : alpt){ |
| 399 |
pt.start(); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
boolean success=true; |
| 404 |
for(ProcessThread pt : alpt){ |
| 405 |
|
| 406 |
|
| 407 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 408 |
try { |
| 409 |
|
| 410 |
pt.join(); |
| 411 |
} catch (InterruptedException e) { |
| 412 |
|
| 413 |
e.printStackTrace(); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
readsProcessed+=pt.readsProcessedT; |
| 419 |
basesProcessed+=pt.basesProcessedT; |
| 420 |
readsOut+=pt.readsOutT; |
| 421 |
basesOut+=pt.basesOutT; |
| 422 |
success&=pt.success; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
if(!success){errorState=true;} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
private void addToRef(Collection<Code> codes) { |
| 437 |
synchronized(refSet) { |
| 438 |
for(Code c : codes) {addToRef(c);} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
private void addToRef(Code c) { |
| 443 |
boolean novel=refSet.add(c); |
| 444 |
if(novel) {refList.add(c.bases);} |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
private class ProcessThread extends Thread { |
| 453 |
|
| 454 |
|
| 455 |
ProcessThread(final ConcurrentReadInputStream cris_, final ConcurrentReadOutputStream ros_, final int tid_){ |
| 456 |
cris=cris_; |
| 457 |
ros=ros_; |
| 458 |
tid=tid_; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
@Override |
| 463 |
public void run(){ |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
processInner(); |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
success=true; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
void processInner(){ |
| 477 |
|
| 478 |
|
| 479 |
ListNum<Read> ln=cris.nextList(); |
| 480 |
|
| 481 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 482 |
|
| 483 |
|
| 484 |
if(reads!=null && !reads.isEmpty()){ |
| 485 |
Read r=reads.get(0); |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
for(int idx=0; idx<reads.size(); idx++){ |
| 495 |
final Read r1=reads.get(idx); |
| 496 |
final Read r2=r1.mate; |
| 497 |
|
| 498 |
|
| 499 |
if(!r1.validated()){r1.validate(true);} |
| 500 |
if(r2!=null && !r2.validated()){r2.validate(true);} |
| 501 |
|
| 502 |
|
| 503 |
final int initialLength1=r1.length(); |
| 504 |
final int initialLength2=r1.mateLength(); |
| 505 |
|
| 506 |
|
| 507 |
readsProcessedT+=r1.pairCount(); |
| 508 |
basesProcessedT+=initialLength1+initialLength2; |
| 509 |
|
| 510 |
{ |
| 511 |
|
| 512 |
boolean keep=processReadPair(r1, r2); |
| 513 |
if(!keep){reads.set(idx, null);} |
| 514 |
else{ |
| 515 |
readsOutT+=r1.pairCount(); |
| 516 |
basesOutT+=initialLength1+initialLength2; |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
if(ros!=null){ros.add(reads, ln.id);} |
| 523 |
|
| 524 |
|
| 525 |
cris.returnList(ln); |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
ln=cris.nextList(); |
| 530 |
reads=(ln!=null ? ln.list : null); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
if(ln!=null){ |
| 535 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
boolean processReadPair(final Read r1, final Read r2){ |
| 546 |
boolean contained=containsBoth(r1, r2); |
| 547 |
return contained==include; |
| 548 |
} |
| 549 |
|
| 550 |
boolean containsBoth(final Read r1, final Read r2){ |
| 551 |
boolean match1=contains(r1); |
| 552 |
if(r2==null || !match1) {return match1;} |
| 553 |
boolean match2=contains(r2); |
| 554 |
return match1 && match2; |
| 555 |
} |
| 556 |
|
| 557 |
boolean contains(final Read r){ |
| 558 |
|
| 559 |
if(r==null || r.bases==null || r.bases.length<1) {return true;} |
| 560 |
final Code c=new Code(r.bases); |
| 561 |
|
| 562 |
if(refSet.contains(c)){return true;} |
| 563 |
|
| 564 |
if(maxSubs<1 && maxLengthDif<1 && mismatchFraction<=0) {return false;} |
| 565 |
|
| 566 |
return bruteForce(r.bases); |
| 567 |
} |
| 568 |
|
| 569 |
boolean bruteForce(byte[] bases) { |
| 570 |
if(bases==null) {return false;} |
| 571 |
boolean match=compare(bases); |
| 572 |
|
| 573 |
if(match || !rcomp) {return match;} |
| 574 |
AminoAcid.reverseComplementBasesInPlace(bases); |
| 575 |
match=compare(bases); |
| 576 |
|
| 577 |
AminoAcid.reverseComplementBasesInPlace(bases); |
| 578 |
return match; |
| 579 |
} |
| 580 |
|
| 581 |
boolean compare(byte[] bases) { |
| 582 |
int minlen=bases.length-maxLengthDif, maxlen=bases.length+maxLengthDif; |
| 583 |
|
| 584 |
|
| 585 |
boolean match=false; |
| 586 |
for(byte[] ref : refList) { |
| 587 |
if(ref.length>=minlen && ref.length<=maxlen) { |
| 588 |
|
| 589 |
final byte[] longer, shorter; |
| 590 |
if(bases.length<ref.length) { |
| 591 |
shorter=bases; longer=ref; |
| 592 |
}else { |
| 593 |
shorter=ref; longer=bases; |
| 594 |
} |
| 595 |
match=compare(longer, shorter); |
| 596 |
if(match) {break;} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
return match; |
| 601 |
} |
| 602 |
|
| 603 |
boolean compare(byte[] a, byte[] b) { |
| 604 |
final int maxSubs0=Tools.max(maxSubs, |
| 605 |
(int)Math.round(Tools.min(a.length, b.length)*mismatchFraction)); |
| 606 |
final int aStartMax=a.length-b.length; |
| 607 |
for(int aStart=0; aStart<=aStartMax; aStart++) { |
| 608 |
int subs=0; |
| 609 |
for(int apos=aStart, bpos=0; bpos<b.length && subs<=maxSubs0; apos++, bpos++) { |
| 610 |
subs+=(a[apos]==b[bpos] ? 0 : 1); |
| 611 |
} |
| 612 |
if(subs<=maxSubs0) {return true;} |
| 613 |
|
| 614 |
} |
| 615 |
return false; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
protected long readsProcessedT=0; |
| 620 |
|
| 621 |
protected long basesProcessedT=0; |
| 622 |
|
| 623 |
|
| 624 |
protected long readsOutT=0; |
| 625 |
|
| 626 |
protected long basesOutT=0; |
| 627 |
|
| 628 |
|
| 629 |
boolean success=false; |
| 630 |
|
| 631 |
|
| 632 |
private final ConcurrentReadInputStream cris; |
| 633 |
|
| 634 |
private final ConcurrentReadOutputStream ros; |
| 635 |
|
| 636 |
final int tid; |
| 637 |
} |
| 638 |
|
| 639 |
private class LoadThread extends Thread { |
| 640 |
|
| 641 |
|
| 642 |
LoadThread(final ConcurrentReadInputStream cris_, final int tid_){ |
| 643 |
cris=cris_; |
| 644 |
tid=tid_; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
@Override |
| 649 |
public void run(){ |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
processInner(); |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
success=true; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
void processInner(){ |
| 663 |
|
| 664 |
|
| 665 |
ListNum<Read> ln=cris.nextList(); |
| 666 |
|
| 667 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 668 |
|
| 669 |
|
| 670 |
if(reads!=null && !reads.isEmpty()){ |
| 671 |
Read r=reads.get(0); |
| 672 |
|
| 673 |
} |
| 674 |
|
| 675 |
LinkedHashSet<Code> codes=new LinkedHashSet<Code>(4000); |
| 676 |
|
| 677 |
|
| 678 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
for(int idx=0; idx<reads.size(); idx++){ |
| 683 |
final Read r1=reads.get(idx); |
| 684 |
final Read r2=r1.mate; |
| 685 |
|
| 686 |
|
| 687 |
if(!r1.validated()){r1.validate(true);} |
| 688 |
if(r2!=null && !r2.validated()){r2.validate(true);} |
| 689 |
|
| 690 |
|
| 691 |
final int initialLength1=r1.length(); |
| 692 |
final int initialLength2=r1.mateLength(); |
| 693 |
|
| 694 |
|
| 695 |
readsProcessedT+=r1.pairCount(); |
| 696 |
basesProcessedT+=initialLength1+initialLength2; |
| 697 |
|
| 698 |
if(r1!=null){codes.add(new Code(r1.bases));} |
| 699 |
if(r2!=null){codes.add(new Code(r2.bases));} |
| 700 |
} |
| 701 |
|
| 702 |
if(codes.size()>2000){ |
| 703 |
synchronized(refSet){ |
| 704 |
addToRef(codes); |
| 705 |
codes.clear(); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
cris.returnList(ln); |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
ln=cris.nextList(); |
| 715 |
reads=(ln!=null ? ln.list : null); |
| 716 |
} |
| 717 |
|
| 718 |
if(codes.size()>0){ |
| 719 |
synchronized(refSet){ |
| 720 |
addToRef(codes); |
| 721 |
codes.clear(); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
if(ln!=null){ |
| 727 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
protected long readsProcessedT=0; |
| 733 |
|
| 734 |
protected long basesProcessedT=0; |
| 735 |
|
| 736 |
|
| 737 |
boolean success=false; |
| 738 |
|
| 739 |
|
| 740 |
private final ConcurrentReadInputStream cris; |
| 741 |
|
| 742 |
final int tid; |
| 743 |
} |
| 744 |
|
| 745 |
private class Code { |
| 746 |
|
| 747 |
Code(byte[] bases_){ |
| 748 |
long fwd=Dedupe.hash(bases_); |
| 749 |
long rev=Dedupe.hashReversed(bases_); |
| 750 |
a=(rcomp ? Tools.max(fwd, rev) : fwd); |
| 751 |
b=(rcomp ? Tools.min(fwd, rev) : rev); |
| 752 |
|
| 753 |
if(storeBases){ |
| 754 |
if(a==fwd && !toUpperCase){ |
| 755 |
bases=bases_; |
| 756 |
}else{ |
| 757 |
bases=bases_.clone(); |
| 758 |
if(a!=fwd){AminoAcid.reverseComplementBasesInPlace(bases);} |
| 759 |
for(int i=0; i<bases.length; i++){ |
| 760 |
bases[i]=(byte) Tools.toUpperCase(bases[i]); |
| 761 |
} |
| 762 |
} |
| 763 |
}else{ |
| 764 |
bases=null; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
@Override |
| 769 |
public boolean equals(Object o){ |
| 770 |
return equals((Code)o); |
| 771 |
} |
| 772 |
|
| 773 |
public boolean equals(Code c){ |
| 774 |
if(a!=c.a || b!=c.b){return false;} |
| 775 |
if(bases==null || c.bases==null){return true;} |
| 776 |
return Tools.equals(bases, c.bases); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
@Override |
| 786 |
public int hashCode(){ |
| 787 |
return (int)(a&0x7FFFFFFF); |
| 788 |
} |
| 789 |
|
| 790 |
final long a, b; |
| 791 |
final byte[] bases; |
| 792 |
|
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
private String in1=null; |
| 801 |
|
| 802 |
private String in2=null; |
| 803 |
|
| 804 |
private String qfin1=null; |
| 805 |
private String qfin2=null; |
| 806 |
|
| 807 |
|
| 808 |
private String out1=null; |
| 809 |
|
| 810 |
private String out2=null; |
| 811 |
|
| 812 |
private String qfout1=null; |
| 813 |
private String qfout2=null; |
| 814 |
|
| 815 |
|
| 816 |
private String extin=null; |
| 817 |
|
| 818 |
private String extout=null; |
| 819 |
|
| 820 |
|
| 821 |
private String[] ref=null; |
| 822 |
|
| 823 |
|
| 824 |
private String[] literal=null; |
| 825 |
|
| 826 |
private HashSet<Code> refSet; |
| 827 |
private ArrayList<byte[]> refList; |
| 828 |
|
| 829 |
private boolean storeBases=true; |
| 830 |
|
| 831 |
private boolean include=true; |
| 832 |
|
| 833 |
private boolean rcomp=true; |
| 834 |
|
| 835 |
private boolean toUpperCase=true; |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
protected long readsProcessed=0; |
| 841 |
|
| 842 |
protected long basesProcessed=0; |
| 843 |
|
| 844 |
protected long readsLoaded=0; |
| 845 |
protected long basesLoaded=0; |
| 846 |
|
| 847 |
|
| 848 |
protected long readsOut=0; |
| 849 |
|
| 850 |
protected long basesOut=0; |
| 851 |
|
| 852 |
|
| 853 |
private long maxReads=-1; |
| 854 |
|
| 855 |
private int maxSubs=0; |
| 856 |
private float mismatchFraction=0f; |
| 857 |
private int maxLengthDif=0; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
private final FileFormat ffin1; |
| 865 |
|
| 866 |
private final FileFormat ffin2; |
| 867 |
|
| 868 |
|
| 869 |
private final FileFormat ffout1; |
| 870 |
|
| 871 |
private final FileFormat ffout2; |
| 872 |
|
| 873 |
|
| 874 |
private final FileFormat[] ffref; |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
private PrintStream outstream=System.err; |
| 882 |
|
| 883 |
public static boolean verbose=false; |
| 884 |
|
| 885 |
public boolean errorState=false; |
| 886 |
|
| 887 |
private boolean overwrite=true; |
| 888 |
|
| 889 |
private boolean append=false; |
| 890 |
|
| 891 |
private boolean ordered=false; |
| 892 |
|
| 893 |
} |