| 1 |
package template; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.concurrent.atomic.AtomicLong; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.FileFormat; |
| 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.ConcurrentReadInputStream; |
| 17 |
import stream.ConcurrentReadOutputStream; |
| 18 |
import stream.FASTQ; |
| 19 |
import stream.FastaReadInputStream; |
| 20 |
import stream.Read; |
| 21 |
import structures.ListNum; |
| 22 |
import tracker.ReadStats; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class A_Sample_Generator { |
| 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 |
A_Sample_Generator x=new A_Sample_Generator(args); |
| 49 |
|
| 50 |
|
| 51 |
x.process(t); |
| 52 |
|
| 53 |
|
| 54 |
Shared.closeStream(x.outstream); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public A_Sample_Generator(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 |
FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false; |
| 73 |
|
| 74 |
{ |
| 75 |
final Parser parser=parse(args); |
| 76 |
Parser.processQuality(); |
| 77 |
|
| 78 |
maxReads=parser.maxReads; |
| 79 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 80 |
append=ReadStats.append=parser.append; |
| 81 |
|
| 82 |
in1=parser.in1; |
| 83 |
extin=parser.extin; |
| 84 |
|
| 85 |
out1=parser.out1; |
| 86 |
out2=parser.out2; |
| 87 |
qfout1=parser.qfout1; |
| 88 |
qfout2=parser.qfout2; |
| 89 |
extout=parser.extout; |
| 90 |
} |
| 91 |
|
| 92 |
validateParams(); |
| 93 |
doPoundReplacement(); |
| 94 |
fixExtensions(); |
| 95 |
checkFileExistence(); |
| 96 |
checkStatics(); |
| 97 |
|
| 98 |
|
| 99 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 100 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 101 |
|
| 102 |
|
| 103 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private Parser parse(String[] args){ |
| 112 |
|
| 113 |
|
| 114 |
Parser parser=new Parser(); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
for(int i=0; i<args.length; i++){ |
| 121 |
String arg=args[i]; |
| 122 |
|
| 123 |
|
| 124 |
String[] split=arg.split("="); |
| 125 |
String a=split[0].toLowerCase(); |
| 126 |
String b=split.length>1 ? split[1] : null; |
| 127 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 128 |
|
| 129 |
if(a.equals("verbose")){ |
| 130 |
verbose=Parse.parseBoolean(b); |
| 131 |
}else if(a.equals("parse_flag_goes_here")){ |
| 132 |
long fake_variable=Parse.parseKMG(b); |
| 133 |
|
| 134 |
}else if(parser.parse(arg, a, b)){ |
| 135 |
|
| 136 |
}else{ |
| 137 |
outstream.println("Unknown parameter "+args[i]); |
| 138 |
assert(false) : "Unknown parameter "+args[i]; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return parser; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
private void doPoundReplacement(){ |
| 147 |
|
| 148 |
|
| 149 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 150 |
out2=out1.replace("#", "2"); |
| 151 |
out1=out1.replace("#", "1"); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
assert(false) : "May or may not be required..."; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
private void fixExtensions(){ |
| 164 |
in1=Tools.fixExtension(in1); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
private void checkFileExistence(){ |
| 169 |
|
| 170 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 171 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 172 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if(!Tools.testInputFiles(false, true, in1)){ |
| 177 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if(!Tools.testForDuplicateFiles(true, in1, out1, out2)){ |
| 182 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
private static void checkStatics(){ |
| 188 |
|
| 189 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 190 |
ByteFile.FORCE_MODE_BF2=true; |
| 191 |
} |
| 192 |
|
| 193 |
assert(FastaReadInputStream.settingsOK()); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
private boolean validateParams(){ |
| 198 |
|
| 199 |
assert(false) : "TODO"; |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
void process(Timer t){ |
| 209 |
|
| 210 |
|
| 211 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 212 |
Read.VALIDATE_IN_CONSTRUCTOR=true; |
| 213 |
|
| 214 |
|
| 215 |
final ConcurrentReadInputStream cris=makeCris(); |
| 216 |
|
| 217 |
|
| 218 |
assert(false) : "TODO: Determine whether output should be paired."; |
| 219 |
final ConcurrentReadOutputStream ros=makeCros(false); |
| 220 |
|
| 221 |
|
| 222 |
readsProcessed=readsOut=0; |
| 223 |
basesProcessed=basesOut=0; |
| 224 |
|
| 225 |
data=loadData(cris); |
| 226 |
|
| 227 |
|
| 228 |
spawnThreads(cris, ros); |
| 229 |
|
| 230 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 231 |
|
| 232 |
|
| 233 |
errorState|=ReadStats.writeAll(); |
| 234 |
|
| 235 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 236 |
|
| 237 |
|
| 238 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 239 |
|
| 240 |
|
| 241 |
t.stop(); |
| 242 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 243 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 244 |
|
| 245 |
|
| 246 |
if(errorState){ |
| 247 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
private ConcurrentReadInputStream makeCris(){ |
| 253 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null); |
| 254 |
cris.start(); |
| 255 |
if(verbose){outstream.println("Started cris");} |
| 256 |
return cris; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
private ConcurrentReadOutputStream makeCros(boolean pairedInput){ |
| 261 |
if(ffout1==null){return null;} |
| 262 |
|
| 263 |
|
| 264 |
final int buff=4; |
| 265 |
|
| 266 |
|
| 267 |
if(pairedInput && out2==null && (in1!=null && !ffin1.samOrBam() && !ffout1.samOrBam())){ |
| 268 |
outstream.println("Writing interleaved."); |
| 269 |
} |
| 270 |
|
| 271 |
final ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream( |
| 272 |
ffout1, ffout2, qfout1, qfout2, buff, null, false); |
| 273 |
ros.start(); |
| 274 |
return ros; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
private void spawnThreads(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros){ |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
final int threads=Shared.threads(); |
| 284 |
|
| 285 |
|
| 286 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 287 |
for(int i=0; i<threads; i++){ |
| 288 |
alpt.add(new ProcessThread(ros, i, maxReads, nextReadID)); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
for(ProcessThread pt : alpt){ |
| 293 |
pt.start(); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
waitForThreads(alpt); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
private void waitForThreads(ArrayList<ProcessThread> alpt){ |
| 305 |
|
| 306 |
|
| 307 |
boolean success=true; |
| 308 |
for(ProcessThread pt : alpt){ |
| 309 |
|
| 310 |
|
| 311 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 312 |
try { |
| 313 |
|
| 314 |
pt.join(); |
| 315 |
} catch (InterruptedException e) { |
| 316 |
|
| 317 |
e.printStackTrace(); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
readsOut+=pt.readsOutT; |
| 323 |
basesOut+=pt.basesOutT; |
| 324 |
success&=pt.success; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if(!success){errorState=true;} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
private ArrayList<Read> loadData(ConcurrentReadInputStream cris){ |
| 336 |
|
| 337 |
ArrayList<Read> input=new ArrayList<Read>(); |
| 338 |
|
| 339 |
|
| 340 |
ListNum<Read> ln=cris.nextList(); |
| 341 |
|
| 342 |
|
| 343 |
while(ln!=null && ln.size()>0){ |
| 344 |
|
| 345 |
|
| 346 |
for(Read r : ln){ |
| 347 |
|
| 348 |
|
| 349 |
if(true){ |
| 350 |
input.add(r); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
readsProcessed+=r.pairCount(); |
| 355 |
basesProcessed+=r.pairLength(); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
cris.returnList(ln); |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
ln=cris.nextList(); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
if(ln!=null){ |
| 368 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 369 |
} |
| 370 |
|
| 371 |
return input; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private static class ProcessThread extends Thread { |
| 381 |
|
| 382 |
|
| 383 |
ProcessThread(final ConcurrentReadOutputStream ros_, final int tid_, |
| 384 |
final long maxReads_, final AtomicLong nextReadID_){ |
| 385 |
ros=ros_; |
| 386 |
tid=tid_; |
| 387 |
maxReads=maxReads_; |
| 388 |
atomicReadID=nextReadID_; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
@Override |
| 393 |
public void run(){ |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
processInner(); |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
success=true; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
void processInner(){ |
| 407 |
|
| 408 |
|
| 409 |
for(long generated=atomicReadID.getAndAdd(readsPerList); generated<maxReads; |
| 410 |
generated=atomicReadID.getAndAdd(readsPerList)){ |
| 411 |
|
| 412 |
|
| 413 |
long toGenerate=Tools.min(readsPerList, maxReads-generated); |
| 414 |
|
| 415 |
ArrayList<Read> reads=generateList((int)toGenerate, generated); |
| 416 |
|
| 417 |
|
| 418 |
if(ros!=null){ros.add(reads, 0);} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
private ArrayList<Read> generateList(int toGenerate, long nextID){ |
| 424 |
|
| 425 |
|
| 426 |
final ArrayList<Read> reads=new ArrayList<Read>(toGenerate); |
| 427 |
|
| 428 |
|
| 429 |
for(int i=0; i<toGenerate; i++, nextID++){ |
| 430 |
Read r=generateRead(nextID); |
| 431 |
readsOutT+=r.pairCount(); |
| 432 |
basesOutT+=r.length(); |
| 433 |
reads.add(r); |
| 434 |
} |
| 435 |
|
| 436 |
return reads; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
private Read generateRead(final long nextID){ |
| 443 |
|
| 444 |
throw new RuntimeException("TODO: Implement this method."); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
protected long readsOutT=0; |
| 449 |
|
| 450 |
protected long basesOutT=0; |
| 451 |
|
| 452 |
|
| 453 |
boolean success=false; |
| 454 |
|
| 455 |
private final AtomicLong atomicReadID; |
| 456 |
private final long maxReads; |
| 457 |
private final int readsPerList=Shared.bufferLen(); |
| 458 |
|
| 459 |
|
| 460 |
private final ConcurrentReadOutputStream ros; |
| 461 |
|
| 462 |
final int tid; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
private String in1=null; |
| 471 |
|
| 472 |
|
| 473 |
private String out1=null; |
| 474 |
|
| 475 |
private String out2=null; |
| 476 |
|
| 477 |
private String qfout1=null; |
| 478 |
private String qfout2=null; |
| 479 |
|
| 480 |
|
| 481 |
private String extin=null; |
| 482 |
|
| 483 |
private String extout=null; |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
protected long readsProcessed=0; |
| 489 |
|
| 490 |
protected long basesProcessed=0; |
| 491 |
|
| 492 |
|
| 493 |
protected long readsOut=0; |
| 494 |
|
| 495 |
protected long basesOut=0; |
| 496 |
|
| 497 |
|
| 498 |
private long maxReads=-1; |
| 499 |
|
| 500 |
|
| 501 |
private ArrayList<Read> data=new ArrayList<Read>(); |
| 502 |
|
| 503 |
private AtomicLong nextReadID=new AtomicLong(0); |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
private final FileFormat ffin1; |
| 511 |
|
| 512 |
|
| 513 |
private final FileFormat ffout1; |
| 514 |
|
| 515 |
private final FileFormat ffout2; |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
private PrintStream outstream=System.err; |
| 523 |
|
| 524 |
public static boolean verbose=false; |
| 525 |
|
| 526 |
public boolean errorState=false; |
| 527 |
|
| 528 |
private boolean overwrite=true; |
| 529 |
|
| 530 |
private boolean append=false; |
| 531 |
|
| 532 |
} |