| 1 |
package consensus; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.LinkedHashMap; |
| 6 |
import java.util.Map.Entry; |
| 7 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 8 |
import java.util.concurrent.atomic.AtomicLongArray; |
| 9 |
import java.util.concurrent.locks.ReadWriteLock; |
| 10 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 11 |
|
| 12 |
import fileIO.ByteFile; |
| 13 |
import fileIO.FileFormat; |
| 14 |
import fileIO.ReadWrite; |
| 15 |
import shared.Parse; |
| 16 |
import shared.Parser; |
| 17 |
import shared.PreParser; |
| 18 |
import shared.Shared; |
| 19 |
import shared.Timer; |
| 20 |
import shared.Tools; |
| 21 |
import stream.ConcurrentReadInputStream; |
| 22 |
import stream.ConcurrentReadOutputStream; |
| 23 |
import stream.FastaReadInputStream; |
| 24 |
import stream.Read; |
| 25 |
import stream.SamLine; |
| 26 |
import stream.SamReadStreamer; |
| 27 |
import stream.SamStreamer; |
| 28 |
import structures.ByteBuilder; |
| 29 |
import structures.ListNum; |
| 30 |
import template.Accumulator; |
| 31 |
import template.ThreadWaiter; |
| 32 |
import tracker.ReadStats; |
| 33 |
import var2.SamFilter; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class FixScaffoldGaps implements Accumulator<FixScaffoldGaps.ProcessThread> { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static void main(String[] args){ |
| 54 |
|
| 55 |
Timer t=new Timer(); |
| 56 |
|
| 57 |
|
| 58 |
FixScaffoldGaps x=new FixScaffoldGaps(args); |
| 59 |
|
| 60 |
|
| 61 |
x.process(t); |
| 62 |
|
| 63 |
|
| 64 |
Shared.closeStream(x.outstream); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public FixScaffoldGaps(String[] args){ |
| 72 |
|
| 73 |
{ |
| 74 |
PreParser pp=new PreParser(args, getClass(), false); |
| 75 |
args=pp.args; |
| 76 |
outstream=pp.outstream; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 81 |
ReadWrite.setZipThreads(Shared.threads()); |
| 82 |
|
| 83 |
samFilter.includeUnmapped=false; |
| 84 |
samFilter.includeSupplimentary=false; |
| 85 |
|
| 86 |
samFilter.includeNonPrimary=false; |
| 87 |
samFilter.includeQfail=false; |
| 88 |
|
| 89 |
|
| 90 |
{ |
| 91 |
final Parser parser=parse(args); |
| 92 |
|
| 93 |
Parser.processQuality(); |
| 94 |
|
| 95 |
maxReads=parser.maxReads; |
| 96 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 97 |
append=ReadStats.append=parser.append; |
| 98 |
|
| 99 |
in=parser.in1; |
| 100 |
extin=parser.extin; |
| 101 |
|
| 102 |
out=parser.out1; |
| 103 |
extout=parser.extout; |
| 104 |
} |
| 105 |
|
| 106 |
{ |
| 107 |
|
| 108 |
|
| 109 |
samFilter.setSamtoolsFilter(); |
| 110 |
|
| 111 |
streamerThreads=Tools.max(1, Tools.min(streamerThreads, Shared.threads())); |
| 112 |
assert(streamerThreads>0) : streamerThreads; |
| 113 |
} |
| 114 |
|
| 115 |
validateParams(); |
| 116 |
fixExtensions(); |
| 117 |
checkFileExistence(); |
| 118 |
checkStatics(); |
| 119 |
|
| 120 |
|
| 121 |
ffout=FileFormat.testOutput(out, FileFormat.FASTA, extout, true, overwrite, append, ordered); |
| 122 |
|
| 123 |
|
| 124 |
ffin=FileFormat.testInput(in, FileFormat.SAM, extin, true, true); |
| 125 |
ffref=FileFormat.testInput(ref, FileFormat.FASTA, null, true, true); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private Parser parse(String[] args){ |
| 134 |
|
| 135 |
|
| 136 |
Parser parser=new Parser(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
for(int i=0; i<args.length; i++){ |
| 143 |
String arg=args[i]; |
| 144 |
|
| 145 |
|
| 146 |
String[] split=arg.split("="); |
| 147 |
String a=split[0].toLowerCase(); |
| 148 |
String b=split.length>1 ? split[1] : null; |
| 149 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 150 |
|
| 151 |
if(a.equals("verbose")){ |
| 152 |
verbose=Parse.parseBoolean(b); |
| 153 |
}else if(a.equals("ref") || a.equals("scaffolds")){ |
| 154 |
ref=b; |
| 155 |
}else if(a.equals("insertlist")){ |
| 156 |
insertList=b; |
| 157 |
}else if(a.equals("ordered")){ |
| 158 |
ordered=Parse.parseBoolean(b); |
| 159 |
}else if(a.equalsIgnoreCase("ns") || a.equalsIgnoreCase("n") || a.equalsIgnoreCase("scaffoldbreak") || a.equalsIgnoreCase("gap")){ |
| 160 |
scaffoldBreakNs=Integer.parseInt(b); |
| 161 |
assert(scaffoldBreakNs>0); |
| 162 |
}else if(a.equalsIgnoreCase("mindepth")){ |
| 163 |
minDepth=Integer.parseInt(b); |
| 164 |
assert(minDepth>0); |
| 165 |
}else if(a.equalsIgnoreCase("trim") || a.equalsIgnoreCase("trimFraction") || a.equalsIgnoreCase("border")){ |
| 166 |
trimFraction=Float.parseFloat(b); |
| 167 |
assert(trimFraction>=0 && trimFraction<=1) : "trimFraction should be between 0 and 1"; |
| 168 |
}else if(a.equals("clearfilters") || a.equals("clearfilter")){ |
| 169 |
if(Parse.parseBoolean(b)){ |
| 170 |
samFilter.clear(); |
| 171 |
} |
| 172 |
}else if(a.equals("parse_flag_goes_here")){ |
| 173 |
long fake_variable=Parse.parseKMG(b); |
| 174 |
|
| 175 |
}else if(samFilter.parse(arg, a, b)){ |
| 176 |
|
| 177 |
}else if(parser.parse(arg, a, b)){ |
| 178 |
|
| 179 |
}else{ |
| 180 |
outstream.println("Unknown parameter "+args[i]); |
| 181 |
assert(false) : "Unknown parameter "+args[i]; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return parser; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
private void fixExtensions(){ |
| 190 |
in=Tools.fixExtension(in); |
| 191 |
ref=Tools.fixExtension(ref); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
private void checkFileExistence(){ |
| 196 |
|
| 197 |
|
| 198 |
if(in==null){throw new RuntimeException("Error - an input file is required.");} |
| 199 |
|
| 200 |
|
| 201 |
if(ref==null){throw new RuntimeException("Error - a reference file is required.");} |
| 202 |
|
| 203 |
|
| 204 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 205 |
outstream.println((out==null)+", "+out); |
| 206 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if(!Tools.testInputFiles(false, true, in, ref)){ |
| 211 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
if(!Tools.testForDuplicateFiles(true, in, ref, out)){ |
| 216 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
private static void checkStatics(){ |
| 222 |
|
| 223 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 224 |
ByteFile.FORCE_MODE_BF2=true; |
| 225 |
} |
| 226 |
|
| 227 |
assert(FastaReadInputStream.settingsOK()); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
private boolean validateParams(){ |
| 232 |
|
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
void process(Timer t){ |
| 242 |
|
| 243 |
|
| 244 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 245 |
Read.VALIDATE_IN_CONSTRUCTOR=Shared.threads()<4; |
| 246 |
|
| 247 |
|
| 248 |
final SamStreamer ss=makeStreamer(ffin); |
| 249 |
|
| 250 |
|
| 251 |
loadReferenceCustom(); |
| 252 |
|
| 253 |
|
| 254 |
readsProcessed=readsOut=0; |
| 255 |
basesProcessed=basesOut=0; |
| 256 |
|
| 257 |
|
| 258 |
spawnThreads(ss); |
| 259 |
|
| 260 |
|
| 261 |
final ConcurrentReadOutputStream ros=makeCros(); |
| 262 |
|
| 263 |
if(verbose){outstream.println("Fixing reference.");} |
| 264 |
|
| 265 |
fixScaffolds(ros); |
| 266 |
|
| 267 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 268 |
|
| 269 |
|
| 270 |
errorState|=ReadStats.writeAll(); |
| 271 |
|
| 272 |
errorState|=ReadWrite.closeStream(ros); |
| 273 |
|
| 274 |
|
| 275 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 276 |
|
| 277 |
|
| 278 |
t.stop(); |
| 279 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 280 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, scaffoldsOut, scaffoldLengthOut, 8, false)); |
| 281 |
|
| 282 |
outstream.println(); |
| 283 |
outstream.println(Tools.number("Average Insert", totalAverageInsert, 2, 8)); |
| 284 |
outstream.println(Tools.number("Gaps Unchanged", gapsUnchanged, 8)); |
| 285 |
outstream.println(Tools.number("Gaps Widened ", gapsWidened, 8)); |
| 286 |
outstream.println(Tools.number("Gaps Narrowed ", gapsNarrowed, 8)); |
| 287 |
outstream.println(Tools.number("Ns Total ", nsTotal, 8)); |
| 288 |
outstream.println(Tools.number("Ns Added ", nsAdded, 8)); |
| 289 |
outstream.println(Tools.number("Ns Removed ", nsRemoved, 8)); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if(errorState){ |
| 294 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
private synchronized void loadReferenceCustom(){ |
| 299 |
assert(!loadedRef); |
| 300 |
ConcurrentReadInputStream cris=makeRefCris(); |
| 301 |
for(ListNum<Read> ln=cris.nextList(); ln!=null && ln.size()>0; ln=cris.nextList()) { |
| 302 |
for(Read r : ln){ |
| 303 |
String name=r.id; |
| 304 |
String name2=Tools.trimToWhitespace(r.id); |
| 305 |
Scaffold scaf=new Scaffold(name, r.bases, r.numericID); |
| 306 |
refMap.put(name, scaf); |
| 307 |
refMap2.put(name2, scaf); |
| 308 |
} |
| 309 |
} |
| 310 |
loadedRef=true; |
| 311 |
} |
| 312 |
|
| 313 |
private ConcurrentReadInputStream makeRefCris(){ |
| 314 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffref, null); |
| 315 |
cris.start(); |
| 316 |
if(verbose){outstream.println("Started cris");} |
| 317 |
boolean paired=cris.paired(); |
| 318 |
assert(!paired) : "References should not be paired."; |
| 319 |
return cris; |
| 320 |
} |
| 321 |
|
| 322 |
private SamStreamer makeStreamer(FileFormat ff){ |
| 323 |
if(ff==null){return null;} |
| 324 |
SamStreamer ss=new SamReadStreamer(ff, streamerThreads, true, maxReads); |
| 325 |
ss.start(); |
| 326 |
if(verbose){outstream.println("Started Streamer");} |
| 327 |
return ss; |
| 328 |
} |
| 329 |
|
| 330 |
private ConcurrentReadOutputStream makeCros(){ |
| 331 |
if(ffout==null){return null;} |
| 332 |
|
| 333 |
|
| 334 |
final int buff=(ordered ? Tools.mid(16, 128, (Shared.threads()*2)/3) : 8); |
| 335 |
|
| 336 |
final ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ffout, null, buff, null, false); |
| 337 |
ros.start(); |
| 338 |
return ros; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
private void spawnThreads(final SamStreamer ss){ |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
final int threads=Shared.threads(); |
| 352 |
|
| 353 |
|
| 354 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 355 |
for(int i=0; i<threads; i++){ |
| 356 |
alpt.add(new ProcessThread(ss, i)); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
for(ProcessThread pt : alpt){ |
| 361 |
pt.start(); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
boolean success=ThreadWaiter.waitForThreadsToFinish(alpt, this); |
| 366 |
errorState&=!success; |
| 367 |
|
| 368 |
|
| 369 |
totalAverageInsert=totalInsertSum/(double)totalInsertCount; |
| 370 |
insertByPercentile=Tools.makeHistogram(insertCounts, buckets); |
| 371 |
} |
| 372 |
|
| 373 |
@Override |
| 374 |
public final void accumulate(ProcessThread pt){ |
| 375 |
readsProcessed+=pt.readsProcessedT; |
| 376 |
basesProcessed+=pt.basesProcessedT; |
| 377 |
readsOut+=pt.readsOutT; |
| 378 |
basesOut+=pt.basesOutT; |
| 379 |
|
| 380 |
totalInsertSum+=pt.totalInsertSumT; |
| 381 |
totalInsertCount+=pt.totalInsertCountT; |
| 382 |
|
| 383 |
errorState|=(!pt.success); |
| 384 |
} |
| 385 |
|
| 386 |
@Override |
| 387 |
public final boolean success(){return !errorState;} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
private void fixScaffolds(ConcurrentReadOutputStream ros){ |
| 394 |
ByteBuilder bb=new ByteBuilder(1000000); |
| 395 |
|
| 396 |
ArrayList<Read> list=new ArrayList<Read>(200); |
| 397 |
long num=0; |
| 398 |
long lengthSum=0; |
| 399 |
for(Entry<String, Scaffold> e : refMap.entrySet()){ |
| 400 |
Scaffold scaf=e.getValue(); |
| 401 |
Read r=scaf.fixScaffold(bb); |
| 402 |
lengthSum+=r.length(); |
| 403 |
list.add(r); |
| 404 |
scaffoldsOut++; |
| 405 |
scaffoldLengthOut+=r.length(); |
| 406 |
|
| 407 |
if(list.size()>=200 || lengthSum>=100000){ |
| 408 |
if(ros!=null){ros.add(list, num);} |
| 409 |
list=new ArrayList<Read>(200); |
| 410 |
num++; |
| 411 |
lengthSum=0; |
| 412 |
} |
| 413 |
} |
| 414 |
if(list.size()>0){ |
| 415 |
if(ros!=null){ros.add(list, num);} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
private static int calcInsertSize(SamLine sl) { |
| 420 |
assert(sl.mapped() && sl.pairedOnSameChrom()); |
| 421 |
assert(sl.primary()); |
| 422 |
assert(!sl.supplementary()); |
| 423 |
assert(sl.leftmost()); |
| 424 |
|
| 425 |
assert(sl.tlen>0) : sl.tlen+"\n\n"+sl; |
| 426 |
return sl.tlen>0 ? sl.tlen : -sl.tlen; |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
class ProcessThread extends Thread { |
| 456 |
|
| 457 |
|
| 458 |
ProcessThread(final SamStreamer ss_, final int tid_){ |
| 459 |
ss=ss_; |
| 460 |
tid=tid_; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
@Override |
| 465 |
public void run(){ |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
processInner(); |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
success=true; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
void processInner(){ |
| 479 |
|
| 480 |
|
| 481 |
for(ListNum<Read> ln=ss.nextReads(); ln!=null; ln=ss.nextReads()){ |
| 482 |
|
| 483 |
|
| 484 |
processList(ln); |
| 485 |
} |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
void processList(ListNum<Read> ln){ |
| 490 |
|
| 491 |
|
| 492 |
final ArrayList<Read> reads=ln.list; |
| 493 |
|
| 494 |
|
| 495 |
for(int idx=0; idx<reads.size(); idx++){ |
| 496 |
final Read r=reads.get(idx); |
| 497 |
|
| 498 |
|
| 499 |
if(!r.validated()){r.validate(true);} |
| 500 |
|
| 501 |
|
| 502 |
final int initialLength=r.length(); |
| 503 |
|
| 504 |
|
| 505 |
readsProcessedT+=r.pairCount(); |
| 506 |
basesProcessedT+=initialLength; |
| 507 |
|
| 508 |
processRead(r); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
void processRead(final Read r){ |
| 519 |
final SamLine sl=r.samline; |
| 520 |
assert(sl!=null) : sl; |
| 521 |
if(samFilter!=null && !samFilter.passesFilter(sl)){return;} |
| 522 |
|
| 523 |
|
| 524 |
if(sl.mapped() && sl.pairedOnSameChrom() && sl.properPair() && sl.primary() && !sl.supplementary() && sl.leftmost()){ |
| 525 |
final String rname=sl.rnameS(); |
| 526 |
Scaffold scaf=refMap.get(rname); |
| 527 |
if(scaf==null){scaf=refMap2.get(Tools.trimToWhitespace(rname));} |
| 528 |
assert(scaf!=null) : "Can't find graph for "+rname; |
| 529 |
|
| 530 |
if(scaf!=null){ |
| 531 |
final int insertSize=calcInsertSize(sl); |
| 532 |
insertCounts.incrementAndGet(Tools.mid(0, insertSize, insertCounts.length())); |
| 533 |
scaf.add(sl, insertSize); |
| 534 |
|
| 535 |
readsOutT++; |
| 536 |
basesOutT+=r.length(); |
| 537 |
|
| 538 |
totalInsertSumT+=insertSize; |
| 539 |
totalInsertCountT++; |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
protected long readsProcessedT=0; |
| 546 |
|
| 547 |
protected long basesProcessedT=0; |
| 548 |
|
| 549 |
|
| 550 |
protected long readsOutT=0; |
| 551 |
|
| 552 |
protected long basesOutT=0; |
| 553 |
|
| 554 |
protected long totalInsertSumT=0; |
| 555 |
protected long totalInsertCountT=0; |
| 556 |
|
| 557 |
long insertSum=0; |
| 558 |
|
| 559 |
|
| 560 |
boolean success=false; |
| 561 |
|
| 562 |
|
| 563 |
private final SamStreamer ss; |
| 564 |
|
| 565 |
final int tid; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
private class Scaffold { |
| 571 |
|
| 572 |
Scaffold(String name_, byte[] bases_, long numericID_){ |
| 573 |
name=name_; |
| 574 |
bases=bases_; |
| 575 |
numericID=(int)numericID_; |
| 576 |
depthArray=new AtomicIntegerArray(bases.length); |
| 577 |
insertArray=new AtomicLongArray(bases.length); |
| 578 |
} |
| 579 |
|
| 580 |
void add(SamLine sl, int insertSize){ |
| 581 |
assert(sl.mapped() && sl.pairedOnSameChrom()); |
| 582 |
assert(sl.primary()); |
| 583 |
assert(!sl.supplementary()); |
| 584 |
assert(sl.leftmost()); |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
int start=sl.pos-1; |
| 589 |
int stop=start+sl.tlen; |
| 590 |
|
| 591 |
int trim=(int)(sl.length()*trimFraction); |
| 592 |
start+=trim; |
| 593 |
stop-=trim; |
| 594 |
|
| 595 |
for(int i=start; i<stop; i++){ |
| 596 |
if(i>=0 && i<bases.length){ |
| 597 |
depthArray.incrementAndGet(i); |
| 598 |
insertArray.addAndGet(i, insertSize); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
} |
| 603 |
|
| 604 |
Read fixScaffold(ByteBuilder bb){ |
| 605 |
int streak=0; |
| 606 |
bb.clear(); |
| 607 |
|
| 608 |
if(insertList!=null){ |
| 609 |
for(int i=0; i<bases.length; i++) { |
| 610 |
bb.append(i).tab().append(depthArray.get(i)).tab().append(insertArray.get(i)/(Tools.max(1, depthArray.get(i)))).nl(); |
| 611 |
} |
| 612 |
ReadWrite.writeString(bb, insertList, false); |
| 613 |
bb.clear(); |
| 614 |
} |
| 615 |
|
| 616 |
for(int i=0; i<bases.length; i++){ |
| 617 |
byte b=bases[i]; |
| 618 |
if(b=='N'){ |
| 619 |
streak++; |
| 620 |
}else{ |
| 621 |
if(streak>=scaffoldBreakNs && i-streak>300 && i<bases.length-300){ |
| 622 |
int pivot=i-streak/2-1; |
| 623 |
long depthSum=depthArray.get(pivot); |
| 624 |
long insertSum=insertArray.get(pivot); |
| 625 |
double avgInsert=(insertSum/(double)depthSum); |
| 626 |
|
| 627 |
int avgDepth=((depthArray.get(i-200-streak)+depthArray.get(i+200))/2); |
| 628 |
int percentile=(int)(buckets*Tools.max(0.5, 1.0-depthSum/(double)(avgDepth+depthSum))); |
| 629 |
int insertProxy=insertByPercentile[percentile]; |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
int dif=(int)Math.round(insertProxy-avgInsert); |
| 634 |
int toAdd=Tools.max(scaffoldBreakNs, streak+dif); |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if(dif>0){ |
| 641 |
|
| 642 |
|
| 643 |
gapsWidened++; |
| 644 |
nsAdded+=dif; |
| 645 |
}else if(dif<0){ |
| 646 |
gapsNarrowed++; |
| 647 |
nsRemoved-=dif; |
| 648 |
}else{ |
| 649 |
gapsUnchanged++; |
| 650 |
} |
| 651 |
nsTotal+=toAdd; |
| 652 |
for(int j=0; j<toAdd; j++){ |
| 653 |
bb.append('N'); |
| 654 |
} |
| 655 |
} |
| 656 |
streak=0; |
| 657 |
bb.append(b); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
return new Read(bb.toBytes(), null, name, numericID); |
| 662 |
} |
| 663 |
|
| 664 |
final int numericID; |
| 665 |
final String name; |
| 666 |
final byte[] bases; |
| 667 |
final AtomicIntegerArray depthArray; |
| 668 |
final AtomicLongArray insertArray; |
| 669 |
|
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
private String in=null; |
| 678 |
|
| 679 |
private String ref=null; |
| 680 |
|
| 681 |
|
| 682 |
private String out=null; |
| 683 |
|
| 684 |
|
| 685 |
private String extin=null; |
| 686 |
|
| 687 |
private String extout=null; |
| 688 |
|
| 689 |
private String insertList=null; |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
protected long readsProcessed=0; |
| 695 |
|
| 696 |
protected long basesProcessed=0; |
| 697 |
|
| 698 |
|
| 699 |
protected long readsOut=0; |
| 700 |
|
| 701 |
protected long basesOut=0; |
| 702 |
|
| 703 |
protected long scaffoldsOut=0; |
| 704 |
protected long scaffoldLengthOut=0; |
| 705 |
|
| 706 |
protected long gapsUnchanged=0; |
| 707 |
protected long gapsWidened=0; |
| 708 |
protected long gapsNarrowed=0; |
| 709 |
protected long nsAdded=0; |
| 710 |
protected long nsRemoved=0; |
| 711 |
protected long nsTotal=0; |
| 712 |
|
| 713 |
protected long totalInsertSum=0; |
| 714 |
protected long totalInsertCount=0; |
| 715 |
protected double totalAverageInsert; |
| 716 |
|
| 717 |
protected AtomicLongArray insertCounts=new AtomicLongArray(20000); |
| 718 |
protected int[] insertByPercentile; |
| 719 |
|
| 720 |
|
| 721 |
private long maxReads=-1; |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
private int streamerThreads=SamStreamer.DEFAULT_THREADS; |
| 727 |
|
| 728 |
private boolean loadedRef=false; |
| 729 |
|
| 730 |
private int scaffoldBreakNs=10; |
| 731 |
|
| 732 |
int buckets=1000; |
| 733 |
|
| 734 |
private int minDepth=10; |
| 735 |
|
| 736 |
private float trimFraction=0.4f; |
| 737 |
|
| 738 |
public final SamFilter samFilter=new SamFilter(); |
| 739 |
|
| 740 |
|
| 741 |
public LinkedHashMap<String, Scaffold> refMap=new LinkedHashMap<String, Scaffold>(); |
| 742 |
|
| 743 |
public LinkedHashMap<String, Scaffold> refMap2=new LinkedHashMap<String, Scaffold>();; |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
private final FileFormat ffin; |
| 751 |
|
| 752 |
private final FileFormat ffref; |
| 753 |
|
| 754 |
|
| 755 |
private final FileFormat ffout; |
| 756 |
|
| 757 |
@Override |
| 758 |
public final ReadWriteLock rwlock() {return rwlock;} |
| 759 |
private final ReadWriteLock rwlock=new ReentrantReadWriteLock(); |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
private PrintStream outstream=System.err; |
| 767 |
|
| 768 |
public static boolean verbose=false; |
| 769 |
|
| 770 |
public boolean errorState=false; |
| 771 |
|
| 772 |
private boolean overwrite=true; |
| 773 |
|
| 774 |
private boolean append=false; |
| 775 |
|
| 776 |
private boolean ordered=false; |
| 777 |
|
| 778 |
} |