| 1 |
package sort; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Collections; |
| 7 |
|
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.ByteFile1; |
| 10 |
import fileIO.ByteFile2; |
| 11 |
import fileIO.ByteStreamWriter; |
| 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.ConcurrentGenericReadInputStream; |
| 21 |
import stream.ConcurrentReadInputStream; |
| 22 |
import stream.FASTQ; |
| 23 |
import stream.FastaReadInputStream; |
| 24 |
import stream.Read; |
| 25 |
import stream.ReadStreamWriter; |
| 26 |
import stream.SamLine; |
| 27 |
import stream.SamReadInputStream; |
| 28 |
import structures.ListNum; |
| 29 |
import tracker.ReadStats; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class Shuffle { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public static void main(String[] args){ |
| 45 |
Timer t=new Timer(); |
| 46 |
Shuffle x=new Shuffle(args); |
| 47 |
x.process(t); |
| 48 |
|
| 49 |
|
| 50 |
Shared.closeStream(x.outstream); |
| 51 |
} |
| 52 |
|
| 53 |
public Shuffle(String[] args){ |
| 54 |
|
| 55 |
{ |
| 56 |
PreParser pp=new PreParser(args, outstream, printClass ? getClass() : null, false); |
| 57 |
args=pp.args; |
| 58 |
outstream=pp.outstream; |
| 59 |
} |
| 60 |
|
| 61 |
boolean setInterleaved=false; |
| 62 |
|
| 63 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 64 |
ReadWrite.setZipThreads(Shared.threads()); |
| 65 |
SamLine.SET_FROM_OK=true; |
| 66 |
ReadStreamWriter.USE_ATTACHED_SAMLINE=true; |
| 67 |
|
| 68 |
int mode_=SHUFFLE; |
| 69 |
|
| 70 |
Parser parser=new Parser(); |
| 71 |
for(int i=0; i<args.length; i++){ |
| 72 |
String arg=args[i]; |
| 73 |
String[] split=arg.split("="); |
| 74 |
String a=split[0].toLowerCase(); |
| 75 |
String b=split.length>1 ? split[1] : null; |
| 76 |
|
| 77 |
if(parser.parse(arg, a, b)){ |
| 78 |
|
| 79 |
}else if(a.equals("verbose")){ |
| 80 |
verbose=Parse.parseBoolean(b); |
| 81 |
ByteFile1.verbose=verbose; |
| 82 |
ByteFile2.verbose=verbose; |
| 83 |
stream.FastaReadInputStream.verbose=verbose; |
| 84 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 85 |
stream.FastqReadInputStream.verbose=verbose; |
| 86 |
ReadWrite.verbose=verbose; |
| 87 |
}else if(a.equals("shuffle")){ |
| 88 |
mode_=SHUFFLE; |
| 89 |
}else if(a.equals("name")){ |
| 90 |
mode_=SORT_NAME; |
| 91 |
}else if(a.equals("coordinate")){ |
| 92 |
mode_=SORT_COORD; |
| 93 |
}else if(a.equals("sequence")){ |
| 94 |
mode_=SORT_SEQ; |
| 95 |
}else if(a.equals("id")){ |
| 96 |
mode_=SORT_ID; |
| 97 |
}else if(a.equals("mode")){ |
| 98 |
if(b==null){ |
| 99 |
throw new RuntimeException("mode must be shuffle, name, coordinate, sequence, or id."); |
| 100 |
}else if(b.equals("shuffle")){ |
| 101 |
mode_=SHUFFLE; |
| 102 |
}else if(b.equals("name")){ |
| 103 |
mode_=SORT_NAME; |
| 104 |
}else if(b.equals("coordinate")){ |
| 105 |
mode_=SORT_COORD; |
| 106 |
}else if(b.equals("sequence")){ |
| 107 |
mode_=SORT_SEQ; |
| 108 |
}else if(b.equals("id")){ |
| 109 |
mode_=SORT_ID; |
| 110 |
}else{ |
| 111 |
throw new RuntimeException("mode must be shuffle, name, coordinate, sequence, or id."); |
| 112 |
} |
| 113 |
}else if(a.equals("showspeed") || a.equals("ss")){ |
| 114 |
showSpeed=Parse.parseBoolean(b); |
| 115 |
}else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 116 |
parser.in1=arg; |
| 117 |
}else{ |
| 118 |
outstream.println("Unknown parameter "+args[i]); |
| 119 |
assert(false) : "Unknown parameter "+args[i]; |
| 120 |
|
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
mode=mode_; |
| 125 |
assert(mode>=1 && mode<=5) : "mode must be shuffle, name, coordinate, sequence, or id."; |
| 126 |
|
| 127 |
{ |
| 128 |
Parser.processQuality(); |
| 129 |
|
| 130 |
maxReads=parser.maxReads; |
| 131 |
|
| 132 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 133 |
append=ReadStats.append=parser.append; |
| 134 |
|
| 135 |
setInterleaved=parser.setInterleaved; |
| 136 |
|
| 137 |
in1=parser.in1; |
| 138 |
in2=parser.in2; |
| 139 |
qfin1=parser.qfin1; |
| 140 |
qfin2=parser.qfin2; |
| 141 |
|
| 142 |
out1=parser.out1; |
| 143 |
out2=parser.out2; |
| 144 |
qfout1=parser.qfout1; |
| 145 |
qfout2=parser.qfout2; |
| 146 |
|
| 147 |
extin=parser.extin; |
| 148 |
extout=parser.extout; |
| 149 |
} |
| 150 |
|
| 151 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 152 |
in2=in1.replace("#", "2"); |
| 153 |
in1=in1.replace("#", "1"); |
| 154 |
} |
| 155 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 156 |
out2=out1.replace("#", "2"); |
| 157 |
out1=out1.replace("#", "1"); |
| 158 |
} |
| 159 |
if(in2!=null){ |
| 160 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 161 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 162 |
} |
| 163 |
|
| 164 |
assert(FastaReadInputStream.settingsOK()); |
| 165 |
|
| 166 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 167 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 168 |
ByteFile.FORCE_MODE_BF2=true; |
| 169 |
} |
| 170 |
|
| 171 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 172 |
|
| 173 |
if(!setInterleaved){ |
| 174 |
assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 175 |
if(in2!=null){ |
| 176 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 177 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 178 |
}else{ |
| 179 |
if(out2!=null){ |
| 180 |
FASTQ.FORCE_INTERLEAVED=true; |
| 181 |
FASTQ.TEST_INTERLEAVED=false; |
| 182 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 188 |
if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;} |
| 189 |
|
| 190 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 191 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 192 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 193 |
} |
| 194 |
|
| 195 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 196 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 197 |
|
| 198 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 199 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 200 |
|
| 201 |
useSharedHeader=(ffout1.samOrBam() && ffout1!=null && ffout1.samOrBam()); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
void process(Timer t){ |
| 209 |
|
| 210 |
ArrayList<Read> bigList=new ArrayList<Read>(65530); |
| 211 |
final ConcurrentReadInputStream cris; |
| 212 |
{ |
| 213 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, useSharedHeader, ffin1, ffin2, qfin1, qfin2); |
| 214 |
if(verbose){outstream.println("Started cris");} |
| 215 |
cris.start(); |
| 216 |
} |
| 217 |
boolean paired=cris.paired(); |
| 218 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 219 |
|
| 220 |
long readsProcessed=0; |
| 221 |
long basesProcessed=0; |
| 222 |
|
| 223 |
{ |
| 224 |
|
| 225 |
ListNum<Read> ln=cris.nextList(); |
| 226 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if(reads!=null && !reads.isEmpty()){ |
| 231 |
Read r=reads.get(0); |
| 232 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 233 |
} |
| 234 |
|
| 235 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 236 |
|
| 237 |
for(int idx=0; idx<reads.size(); idx++){ |
| 238 |
final Read r1=reads.get(idx); |
| 239 |
final Read r2=r1.mate; |
| 240 |
|
| 241 |
final int initialLength1=r1.length(); |
| 242 |
final int initialLength2=(r1.mateLength()); |
| 243 |
|
| 244 |
{ |
| 245 |
readsProcessed++; |
| 246 |
basesProcessed+=initialLength1; |
| 247 |
} |
| 248 |
if(r2!=null){ |
| 249 |
readsProcessed++; |
| 250 |
basesProcessed+=initialLength2; |
| 251 |
} |
| 252 |
bigList.add(r1); |
| 253 |
} |
| 254 |
|
| 255 |
cris.returnList(ln); |
| 256 |
ln=cris.nextList(); |
| 257 |
reads=(ln!=null ? ln.list : null); |
| 258 |
} |
| 259 |
if(ln!=null){ |
| 260 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
errorState|=ReadWrite.closeStream(cris); |
| 265 |
errorState|=ReadStats.writeAll(); |
| 266 |
|
| 267 |
if(mode==SHUFFLE){ |
| 268 |
Collections.shuffle(bigList); |
| 269 |
}else if(mode==SORT_NAME){ |
| 270 |
Shared.sort(bigList, ReadComparatorName.comparator); |
| 271 |
}else if(mode==SORT_SEQ){ |
| 272 |
Shared.sort(bigList, ReadComparatorTopological.comparator); |
| 273 |
}else if(mode==SORT_COORD){ |
| 274 |
Shared.sort(bigList, new ReadComparatorMapping()); |
| 275 |
}else if(mode==SORT_ID){ |
| 276 |
Shared.sort(bigList, ReadComparatorID.comparator); |
| 277 |
}else{ |
| 278 |
assert(false) : "No mode set."; |
| 279 |
} |
| 280 |
|
| 281 |
if(ffout1!=null){ |
| 282 |
final ByteStreamWriter bsw1, bsw2; |
| 283 |
if(ffout1!=null){ |
| 284 |
bsw1=new ByteStreamWriter(ffout1); |
| 285 |
bsw1.start(); |
| 286 |
if(useSharedHeader){writeHeader(bsw1);} |
| 287 |
}else{bsw1=null;} |
| 288 |
if(ffout2!=null){ |
| 289 |
bsw2=new ByteStreamWriter(ffout2); |
| 290 |
bsw2.start(); |
| 291 |
if(useSharedHeader){writeHeader(bsw1);} |
| 292 |
}else{bsw2=null;} |
| 293 |
final boolean b=(bsw2==null); |
| 294 |
for(int i=0, lim=bigList.size(); i<lim; i++){ |
| 295 |
final Read r1=bigList.set(i, null); |
| 296 |
final Read r2=r1.mate; |
| 297 |
bsw1.println(r1, b); |
| 298 |
if(r2!=null && !b){bsw2.println(r2);} |
| 299 |
} |
| 300 |
if(bsw1!=null){errorState|=bsw1.poisonAndWait();} |
| 301 |
if(bsw2!=null){errorState|=bsw2.poisonAndWait();} |
| 302 |
} |
| 303 |
|
| 304 |
t.stop(); |
| 305 |
|
| 306 |
if(showSpeed){ |
| 307 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 308 |
} |
| 309 |
|
| 310 |
if(errorState){ |
| 311 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private void writeHeader(ByteStreamWriter bsw){ |
| 320 |
ArrayList<byte[]> list=SamReadInputStream.getSharedHeader(true); |
| 321 |
if(list==null){ |
| 322 |
System.err.println("Header was null."); |
| 323 |
}else{ |
| 324 |
for(byte[] line : list){ |
| 325 |
bsw.print(line).print('\n'); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public static class ShuffleThread extends Thread{ |
| 336 |
|
| 337 |
public ShuffleThread(String in1_, String in2_, String out1_, String out2_, int mode_, boolean ow_){ |
| 338 |
in1=in1_; |
| 339 |
in2=in2_; |
| 340 |
out1=out1_; |
| 341 |
out2=out2_; |
| 342 |
mode=mode_; |
| 343 |
ow=ow_; |
| 344 |
} |
| 345 |
|
| 346 |
@Override |
| 347 |
public synchronized void start(){ |
| 348 |
addThread(1); |
| 349 |
super.start(); |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
public void run(){ |
| 354 |
ArrayList<String> list=new ArrayList<String>(); |
| 355 |
if(in1!=null){list.add("in1="+in1);} |
| 356 |
if(in2!=null){list.add("in1="+in2);} |
| 357 |
if(out1!=null){list.add("out1="+out1);} |
| 358 |
if(out2!=null){list.add("out2="+out2);} |
| 359 |
list.add("mode="+MODES[mode]); |
| 360 |
list.add("ow="+ow); |
| 361 |
try{ |
| 362 |
Shuffle.main(list.toArray(new String[0])); |
| 363 |
}catch(Throwable e){ |
| 364 |
System.err.println("Failed to shuffle "+in1+"\nException:"+e+"\n"); |
| 365 |
} |
| 366 |
addThread(-1); |
| 367 |
} |
| 368 |
|
| 369 |
final String in1, in2; |
| 370 |
final String out1, out2; |
| 371 |
final int mode; |
| 372 |
final boolean ow; |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
private String in1=null; |
| 382 |
private String in2=null; |
| 383 |
|
| 384 |
private String qfin1=null; |
| 385 |
private String qfin2=null; |
| 386 |
|
| 387 |
private String out1=null; |
| 388 |
private String out2=null; |
| 389 |
|
| 390 |
private String qfout1=null; |
| 391 |
private String qfout2=null; |
| 392 |
|
| 393 |
private String extin=null; |
| 394 |
private String extout=null; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
private long maxReads=-1; |
| 399 |
|
| 400 |
private final int mode; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
private final FileFormat ffin1; |
| 405 |
private final FileFormat ffin2; |
| 406 |
|
| 407 |
private final FileFormat ffout1; |
| 408 |
private final FileFormat ffout2; |
| 409 |
|
| 410 |
private final boolean useSharedHeader; |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
private static int maxShuffleThreads=1; |
| 417 |
private static int currentShuffleThreads=0; |
| 418 |
|
| 419 |
public static void setMaxThreads(final int x){ |
| 420 |
assert(x>0); |
| 421 |
synchronized(SHUFFLE_LOCK){ |
| 422 |
maxShuffleThreads=x; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
public static int addThread(final int x){ |
| 427 |
synchronized(SHUFFLE_LOCK){ |
| 428 |
while(x>0 && currentShuffleThreads>=maxShuffleThreads){ |
| 429 |
try { |
| 430 |
SHUFFLE_LOCK.wait(2000); |
| 431 |
} catch (InterruptedException e) { |
| 432 |
e.printStackTrace(); |
| 433 |
} |
| 434 |
} |
| 435 |
currentShuffleThreads+=x; |
| 436 |
if(currentShuffleThreads<maxShuffleThreads){SHUFFLE_LOCK.notify();} |
| 437 |
return currentShuffleThreads; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
public static void waitForFinish(){ |
| 442 |
synchronized(SHUFFLE_LOCK){ |
| 443 |
while(currentShuffleThreads>=maxShuffleThreads){ |
| 444 |
try { |
| 445 |
SHUFFLE_LOCK.wait(2000); |
| 446 |
} catch (InterruptedException e) { |
| 447 |
e.printStackTrace(); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
private static String SHUFFLE_LOCK=new String("SHUFFLE_LOCK"); |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
private PrintStream outstream=System.err; |
| 460 |
public static boolean verbose=false; |
| 461 |
public boolean errorState=false; |
| 462 |
private boolean overwrite=true; |
| 463 |
private boolean append=false; |
| 464 |
public static boolean showSpeed=true; |
| 465 |
public static boolean printClass=true; |
| 466 |
|
| 467 |
public static final int SHUFFLE=1, SORT_NAME=2, SORT_SEQ=3, SORT_COORD=4, SORT_ID=5; |
| 468 |
public static final String[] MODES={"shuffle", "name", "sequence", "coordinate", "id"}; |
| 469 |
|
| 470 |
|
| 471 |
} |