| 1 |
package hiseq; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 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 |
public class BGI2Illumina { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public static void main(String[] args){ |
| 42 |
|
| 43 |
Timer t=new Timer(); |
| 44 |
|
| 45 |
|
| 46 |
BGI2Illumina x=new BGI2Illumina(args); |
| 47 |
|
| 48 |
|
| 49 |
x.process(t); |
| 50 |
|
| 51 |
|
| 52 |
Shared.closeStream(x.outstream); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public BGI2Illumina(String[] args){ |
| 60 |
|
| 61 |
{ |
| 62 |
PreParser pp=new PreParser(args, null , false); |
| 63 |
args=pp.args; |
| 64 |
outstream=pp.outstream; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 69 |
ReadWrite.setZipThreads(Shared.threads()); |
| 70 |
Shared.capBuffers(4); |
| 71 |
|
| 72 |
FASTQ.ASCII_OFFSET=33; |
| 73 |
FASTQ.ASCII_OFFSET_OUT=33; |
| 74 |
FASTQ.DETECT_QUALITY=false; |
| 75 |
FASTQ.TEST_INTERLEAVED=true; |
| 76 |
|
| 77 |
{ |
| 78 |
final Parser parser=parse(args); |
| 79 |
Parser.processQuality(); |
| 80 |
|
| 81 |
maxReads=parser.maxReads; |
| 82 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 83 |
append=ReadStats.append=parser.append; |
| 84 |
setInterleaved=parser.setInterleaved; |
| 85 |
|
| 86 |
in1=parser.in1; |
| 87 |
in2=parser.in2; |
| 88 |
qfin1=parser.qfin1; |
| 89 |
qfin2=parser.qfin2; |
| 90 |
extin=parser.extin; |
| 91 |
|
| 92 |
out1=parser.out1; |
| 93 |
out2=parser.out2; |
| 94 |
qfout1=parser.qfout1; |
| 95 |
qfout2=parser.qfout2; |
| 96 |
extout=parser.extout; |
| 97 |
} |
| 98 |
|
| 99 |
doPoundReplacement(); |
| 100 |
adjustInterleaving(); |
| 101 |
fixExtensions(); |
| 102 |
checkFileExistence(); |
| 103 |
checkStatics(); |
| 104 |
|
| 105 |
|
| 106 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 107 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 108 |
|
| 109 |
|
| 110 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 111 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private Parser parse(String[] args){ |
| 120 |
|
| 121 |
|
| 122 |
Parser parser=new Parser(); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
for(int i=0; i<args.length; i++){ |
| 129 |
String arg=args[i]; |
| 130 |
|
| 131 |
|
| 132 |
String[] split=arg.split("="); |
| 133 |
String a=split[0].toLowerCase(); |
| 134 |
String b=split.length>1 ? split[1] : null; |
| 135 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 136 |
|
| 137 |
if(a.equals("verbose")){ |
| 138 |
verbose=Parse.parseBoolean(b); |
| 139 |
}else if(a.equals("parseextra") || a.equals("extra")){ |
| 140 |
BGIHeaderParser2.PARSE_EXTRA=Parse.parseBoolean(b); |
| 141 |
}else if(a.equals("barcode") || a.equals("index")){ |
| 142 |
barcode=b; |
| 143 |
}else if(a.equals("parse_flag_goes_here")){ |
| 144 |
long fake_variable=Parse.parseKMG(b); |
| 145 |
|
| 146 |
}else if(parser.parse(arg, a, b)){ |
| 147 |
|
| 148 |
}else{ |
| 149 |
outstream.println("Unknown parameter "+args[i]); |
| 150 |
assert(false) : "Unknown parameter "+args[i]; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return parser; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
private void doPoundReplacement(){ |
| 159 |
|
| 160 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 161 |
in2=in1.replace("#", "2"); |
| 162 |
in1=in1.replace("#", "1"); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 167 |
out2=out1.replace("#", "2"); |
| 168 |
out1=out1.replace("#", "1"); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 173 |
|
| 174 |
|
| 175 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
private void fixExtensions(){ |
| 180 |
in1=Tools.fixExtension(in1); |
| 181 |
in2=Tools.fixExtension(in2); |
| 182 |
qfin1=Tools.fixExtension(qfin1); |
| 183 |
qfin2=Tools.fixExtension(qfin2); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
private void checkFileExistence(){ |
| 188 |
|
| 189 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 190 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 191 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if(!Tools.testInputFiles(false, true, in1, in2)){ |
| 196 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2)){ |
| 201 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
private void adjustInterleaving(){ |
| 207 |
|
| 208 |
if(in2!=null){ |
| 209 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 210 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
if(!setInterleaved){ |
| 215 |
assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 216 |
if(in2!=null){ |
| 217 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 218 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 219 |
}else{ |
| 220 |
if(out2!=null){ |
| 221 |
FASTQ.FORCE_INTERLEAVED=true; |
| 222 |
FASTQ.TEST_INTERLEAVED=false; |
| 223 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
private static void checkStatics(){ |
| 231 |
|
| 232 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 233 |
ByteFile.FORCE_MODE_BF2=true; |
| 234 |
} |
| 235 |
|
| 236 |
assert(FastaReadInputStream.settingsOK()); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
void process(Timer t){ |
| 245 |
|
| 246 |
|
| 247 |
final ConcurrentReadInputStream cris=makeCris(); |
| 248 |
|
| 249 |
|
| 250 |
final ConcurrentReadOutputStream ros=makeCros(cris.paired()); |
| 251 |
|
| 252 |
|
| 253 |
readsProcessed=readsOut=0; |
| 254 |
basesProcessed=basesOut=0; |
| 255 |
|
| 256 |
|
| 257 |
processInner(cris, ros); |
| 258 |
|
| 259 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 260 |
|
| 261 |
|
| 262 |
errorState|=ReadStats.writeAll(); |
| 263 |
|
| 264 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 265 |
|
| 266 |
|
| 267 |
t.stop(); |
| 268 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 269 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 270 |
|
| 271 |
|
| 272 |
if(errorState){ |
| 273 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
private ConcurrentReadInputStream makeCris(){ |
| 282 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, qfin1, qfin2); |
| 283 |
cris.start(); |
| 284 |
if(verbose){outstream.println("Started cris");} |
| 285 |
boolean paired=cris.paired(); |
| 286 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 287 |
return cris; |
| 288 |
} |
| 289 |
|
| 290 |
private ConcurrentReadOutputStream makeCros(boolean pairedInput){ |
| 291 |
if(ffout1==null){return null;} |
| 292 |
|
| 293 |
|
| 294 |
final int buff=4; |
| 295 |
|
| 296 |
|
| 297 |
if(pairedInput && out2==null && (in1!=null && !ffin1.samOrBam() && !ffout1.samOrBam())){ |
| 298 |
outstream.println("Writing interleaved."); |
| 299 |
} |
| 300 |
|
| 301 |
final ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, qfout1, qfout2, buff, null, false); |
| 302 |
ros.start(); |
| 303 |
return ros; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
void processInner(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros){ |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
{ |
| 312 |
|
| 313 |
ListNum<Read> ln=cris.nextList(); |
| 314 |
|
| 315 |
|
| 316 |
if(ln!=null && !ln.isEmpty()){ |
| 317 |
Read r=ln.get(0); |
| 318 |
assert(ffin1.samOrBam() || (r.mate!=null)==cris.paired()); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
while(ln!=null && ln.size()>0){ |
| 323 |
|
| 324 |
|
| 325 |
processList(ln, cris, ros); |
| 326 |
|
| 327 |
|
| 328 |
ln=cris.nextList(); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
if(ln!=null){ |
| 333 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
void processList(ListNum<Read> ln, final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros){ |
| 348 |
|
| 349 |
|
| 350 |
final ArrayList<Read> reads=ln.list; |
| 351 |
|
| 352 |
|
| 353 |
for(int idx=0; idx<reads.size(); idx++){ |
| 354 |
final Read r1=reads.get(idx); |
| 355 |
final Read r2=r1.mate; |
| 356 |
|
| 357 |
|
| 358 |
if(!r1.validated()){r1.validate(true);} |
| 359 |
if(r2!=null && !r2.validated()){r2.validate(true);} |
| 360 |
|
| 361 |
|
| 362 |
final int initialLength1=r1.length(); |
| 363 |
final int initialLength2=r1.mateLength(); |
| 364 |
|
| 365 |
|
| 366 |
readsProcessed+=r1.pairCount(); |
| 367 |
basesProcessed+=initialLength1+initialLength2; |
| 368 |
|
| 369 |
{ |
| 370 |
|
| 371 |
boolean keep=processReadPair(r1, r2); |
| 372 |
|
| 373 |
if(!keep){reads.set(idx, null);} |
| 374 |
else{ |
| 375 |
readsOut+=r1.pairCount(); |
| 376 |
basesOut+=r1.pairLength(); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
if(ros!=null){ros.add(reads, ln.id);} |
| 383 |
|
| 384 |
|
| 385 |
cris.returnList(ln); |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
boolean processReadPair(final Read r1, final Read r2){ |
| 397 |
bhp.parse(r1.id); |
| 398 |
r1.id=bhp.toIllumina(barcode); |
| 399 |
if(r2!=null) { |
| 400 |
bhp.parse(r2.id); |
| 401 |
r2.id=bhp.toIllumina(barcode); |
| 402 |
} |
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
private String in1=null; |
| 412 |
|
| 413 |
private String in2=null; |
| 414 |
|
| 415 |
private String qfin1=null; |
| 416 |
private String qfin2=null; |
| 417 |
|
| 418 |
|
| 419 |
private String out1=null; |
| 420 |
|
| 421 |
private String out2=null; |
| 422 |
|
| 423 |
private String qfout1=null; |
| 424 |
private String qfout2=null; |
| 425 |
|
| 426 |
|
| 427 |
private String extin=null; |
| 428 |
|
| 429 |
private String extout=null; |
| 430 |
|
| 431 |
|
| 432 |
private boolean setInterleaved=false; |
| 433 |
|
| 434 |
private String barcode=null; |
| 435 |
|
| 436 |
private BGIHeaderParser2 bhp=new BGIHeaderParser2(); |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
protected long readsProcessed=0; |
| 442 |
|
| 443 |
protected long basesProcessed=0; |
| 444 |
|
| 445 |
|
| 446 |
protected long readsOut=0; |
| 447 |
|
| 448 |
protected long basesOut=0; |
| 449 |
|
| 450 |
|
| 451 |
private long maxReads=-1; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
private final FileFormat ffin1; |
| 459 |
|
| 460 |
private final FileFormat ffin2; |
| 461 |
|
| 462 |
|
| 463 |
private final FileFormat ffout1; |
| 464 |
|
| 465 |
private final FileFormat ffout2; |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
private PrintStream outstream=System.err; |
| 473 |
|
| 474 |
public static boolean verbose=false; |
| 475 |
|
| 476 |
public boolean errorState=false; |
| 477 |
|
| 478 |
private boolean overwrite=true; |
| 479 |
|
| 480 |
private boolean append=false; |
| 481 |
|
| 482 |
private final boolean ordered=false; |
| 483 |
|
| 484 |
} |