| 1 |
package fileIO; |
| 2 |
|
| 3 |
import java.io.BufferedInputStream; |
| 4 |
import java.io.BufferedOutputStream; |
| 5 |
import java.io.BufferedReader; |
| 6 |
import java.io.File; |
| 7 |
import java.io.FileInputStream; |
| 8 |
import java.io.FileNotFoundException; |
| 9 |
import java.io.FileOutputStream; |
| 10 |
import java.io.IOException; |
| 11 |
import java.io.InputStream; |
| 12 |
import java.io.InputStreamReader; |
| 13 |
import java.io.ObjectInputStream; |
| 14 |
import java.io.ObjectOutputStream; |
| 15 |
import java.io.OutputStream; |
| 16 |
import java.io.PrintWriter; |
| 17 |
import java.io.Reader; |
| 18 |
import java.lang.ProcessBuilder.Redirect; |
| 19 |
import java.net.MalformedURLException; |
| 20 |
import java.net.URL; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Arrays; |
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.HashSet; |
| 25 |
import java.util.Locale; |
| 26 |
import java.util.zip.GZIPInputStream; |
| 27 |
import java.util.zip.GZIPOutputStream; |
| 28 |
import java.util.zip.ZipEntry; |
| 29 |
import java.util.zip.ZipInputStream; |
| 30 |
import java.util.zip.ZipOutputStream; |
| 31 |
|
| 32 |
import dna.Data; |
| 33 |
import shared.KillSwitch; |
| 34 |
import shared.Shared; |
| 35 |
import shared.Tools; |
| 36 |
import stream.ConcurrentReadOutputStream; |
| 37 |
import stream.ConcurrentReadStreamInterface; |
| 38 |
import stream.MultiCros; |
| 39 |
import structures.ByteBuilder; |
| 40 |
|
| 41 |
public class ReadWrite { |
| 42 |
|
| 43 |
|
| 44 |
public static void main(String[] args){ |
| 45 |
File f=new File(args[1]); |
| 46 |
assert(!f.exists()) : "Destination file already exists."; |
| 47 |
copyFile(args[0], args[1]); |
| 48 |
} |
| 49 |
|
| 50 |
public static void writeStringInThread(CharSequence x, String fname){ |
| 51 |
writeStringInThread(x, fname, false); |
| 52 |
} |
| 53 |
|
| 54 |
public static void writeStringInThread(CharSequence x, String fname, boolean append){ |
| 55 |
addThread(1); |
| 56 |
new Thread(new WriteStringThread(x, fname, append)).start(); |
| 57 |
} |
| 58 |
|
| 59 |
public static void writeObjectInThread(Object x, String fname, boolean allowSubprocess){ |
| 60 |
addThread(1); |
| 61 |
new Thread(new WriteObjectThread(x, fname, allowSubprocess)).start(); |
| 62 |
} |
| 63 |
|
| 64 |
private static class WriteStringThread implements Runnable{ |
| 65 |
|
| 66 |
private final CharSequence x; |
| 67 |
private final String fname; |
| 68 |
private final boolean append; |
| 69 |
WriteStringThread(CharSequence x_, String fname_, boolean append_){ |
| 70 |
x=x_; |
| 71 |
fname=fname_; |
| 72 |
append=append_; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void run() { |
| 77 |
if(verbose){System.err.println("WriteStringThread.run() started for fname "+fname);} |
| 78 |
addRunningThread(1); |
| 79 |
writeStringAsync(x, fname, append); |
| 80 |
addThread(-1); |
| 81 |
if(verbose){System.err.println("WriteStringThread.run() finished for fname "+fname);} |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
private static class WriteObjectThread implements Runnable{ |
| 87 |
|
| 88 |
private final Object x; |
| 89 |
private final String fname; |
| 90 |
private final boolean allowSubprocess; |
| 91 |
WriteObjectThread(Object x_, String fname_, boolean allowSubprocess_){ |
| 92 |
x=x_; |
| 93 |
fname=fname_; |
| 94 |
allowSubprocess=allowSubprocess_; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void run() { |
| 99 |
if(verbose){System.err.println("WriteObjectThread.run() started for fname "+fname);} |
| 100 |
addRunningThread(1); |
| 101 |
|
| 102 |
writeAsync(x, fname, allowSubprocess); |
| 103 |
|
| 104 |
addThread(-1); |
| 105 |
|
| 106 |
if(verbose){System.err.println("WriteObjectThread.run() finished for fname "+fname);} |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public static boolean setPermissions(String fname, boolean read, boolean write, boolean execute, boolean ownerOnly){ |
| 112 |
File f=new File(fname); |
| 113 |
if(!f.exists()){return false;} |
| 114 |
try { |
| 115 |
f.setReadable(read, ownerOnly); |
| 116 |
f.setWritable(write, ownerOnly); |
| 117 |
f.setExecutable(execute, ownerOnly); |
| 118 |
} catch (Exception e) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
public static void writeString(CharSequence x, String fname){writeString(x, fname, false);} |
| 125 |
public static void writeString(CharSequence x, String fname, boolean append){ |
| 126 |
writeString(x, fname, !append, append); |
| 127 |
} |
| 128 |
public static void writeString(CharSequence x, String fname, boolean overwrite, boolean append){ |
| 129 |
if(verbose){System.err.println("writeString(x, "+fname+", "+append+")");} |
| 130 |
File f=new File(fname); |
| 131 |
assert(overwrite || append || !f.exists()) : "File "+fname+" exists and overwrite=f"; |
| 132 |
OutputStream os=getOutputStream(fname, append, true, false); |
| 133 |
|
| 134 |
try { |
| 135 |
|
| 136 |
synchronized(diskSync){ |
| 137 |
PrintWriter out=new PrintWriter(os); |
| 138 |
out.print(x); |
| 139 |
out.flush(); |
| 140 |
|
| 141 |
if(os.getClass()==ZipOutputStream.class){ |
| 142 |
ZipOutputStream zos=(ZipOutputStream)os; |
| 143 |
zos.closeEntry(); |
| 144 |
zos.finish(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
out.close(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
} catch (FileNotFoundException e) { |
| 158 |
throw new RuntimeException(e); |
| 159 |
} catch (IOException e) { |
| 160 |
throw new RuntimeException(e); |
| 161 |
} catch (OutOfMemoryError e) { |
| 162 |
KillSwitch.memKill(e); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public static void writeStringAsync(CharSequence x, String fname){writeStringAsync(x, fname, false);} |
| 167 |
public static void writeStringAsync(CharSequence x, String fname, boolean append){ |
| 168 |
if(verbose){System.err.println("writeStringAsync(x, "+fname+", "+append+")");} |
| 169 |
|
| 170 |
OutputStream os=getOutputStream(fname, append, true, false); |
| 171 |
|
| 172 |
try { |
| 173 |
|
| 174 |
synchronized(diskSync){ |
| 175 |
PrintWriter out=new PrintWriter(os); |
| 176 |
out.print(x); |
| 177 |
out.flush(); |
| 178 |
|
| 179 |
if(os.getClass()==ZipOutputStream.class){ |
| 180 |
ZipOutputStream zos=(ZipOutputStream)os; |
| 181 |
zos.closeEntry(); |
| 182 |
zos.finish(); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
out.close(); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
} catch (FileNotFoundException e) { |
| 196 |
throw new RuntimeException(e); |
| 197 |
} catch (IOException e) { |
| 198 |
throw new RuntimeException(e); |
| 199 |
} catch (OutOfMemoryError e) { |
| 200 |
KillSwitch.memKill(e); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public static <X> void write(X x, String fname, boolean allowSubprocess){ |
| 205 |
if(verbose){System.err.println("write(x, "+fname+", "+allowSubprocess+")");} |
| 206 |
|
| 207 |
OutputStream os=getOutputStream(fname, false, true, allowSubprocess); |
| 208 |
|
| 209 |
try { |
| 210 |
|
| 211 |
synchronized(diskSync){ |
| 212 |
ObjectOutputStream out=new ObjectOutputStream(os); |
| 213 |
out.writeObject(x); |
| 214 |
close(out); |
| 215 |
} |
| 216 |
|
| 217 |
} catch (FileNotFoundException e) { |
| 218 |
throw new RuntimeException(e); |
| 219 |
} catch (IOException e) { |
| 220 |
throw new RuntimeException(e); |
| 221 |
} catch (OutOfMemoryError e) { |
| 222 |
KillSwitch.memKill(e); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
public static <X> void writeAsync(X x, String fname, boolean allowSubprocess){ |
| 227 |
if(verbose){System.err.println("writeAsync(x, "+fname+", "+allowSubprocess+")");} |
| 228 |
|
| 229 |
OutputStream os=getOutputStream(fname, false, true, allowSubprocess); |
| 230 |
|
| 231 |
try { |
| 232 |
|
| 233 |
ObjectOutputStream out=new ObjectOutputStream(os); |
| 234 |
out.writeObject(x); |
| 235 |
close(out); |
| 236 |
|
| 237 |
} catch (FileNotFoundException e) { |
| 238 |
throw new RuntimeException(e); |
| 239 |
} catch (IOException e) { |
| 240 |
throw new RuntimeException(e); |
| 241 |
} catch (OutOfMemoryError e) { |
| 242 |
KillSwitch.memKill(e); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
public static final boolean finishReading(InputStream is, String fname, boolean killProcess, Reader...ra){ |
| 247 |
if(verbose){System.err.println("finishReading("+is+", "+fname+", "+killProcess+", "+ra.length+")");} |
| 248 |
boolean error=false; |
| 249 |
if(ra!=null){ |
| 250 |
for(Reader r : ra){ |
| 251 |
try { |
| 252 |
r.close(); |
| 253 |
} catch (IOException e) { |
| 254 |
error=true; |
| 255 |
e.printStackTrace(); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
error|=finishReading(is, fname, killProcess); |
| 260 |
if(verbose){System.err.println("finishReading("+is+", "+fname+", "+killProcess+", "+ra.length+") returned "+error);} |
| 261 |
return error; |
| 262 |
} |
| 263 |
|
| 264 |
public static final boolean finishReading(InputStream is, String fname, boolean killProcess){ |
| 265 |
if(verbose){System.err.println("finishReading("+is+", "+fname+", "+killProcess+")");} |
| 266 |
boolean error=false; |
| 267 |
if(is!=System.in){ |
| 268 |
try { |
| 269 |
is.close(); |
| 270 |
} catch (IOException e) { |
| 271 |
error=true; |
| 272 |
|
| 273 |
e.printStackTrace(); |
| 274 |
} |
| 275 |
} |
| 276 |
if(killProcess && fname!=null && is!=System.in){error|=ReadWrite.killProcess(fname);} |
| 277 |
if(verbose){System.err.println("finishReading("+is+", "+fname+", "+killProcess+") returned "+error);} |
| 278 |
return error; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public static final boolean finishWriting(PrintWriter writer, OutputStream outStream, String fname, boolean killProcess){ |
| 286 |
if(verbose){System.err.println("finishWriting("+writer+", "+outStream+" , "+fname+", "+killProcess+")");} |
| 287 |
boolean error=false; |
| 288 |
if(writer!=null){writer.flush();} |
| 289 |
close(outStream); |
| 290 |
if(writer!=null && outStream!=System.out && outStream!=System.err){writer.close();} |
| 291 |
if(killProcess && fname!=null && outStream!=System.err && outStream!=System.out){error|=ReadWrite.killProcess(fname);} |
| 292 |
if(verbose){System.err.println("finishWriting("+writer+", "+outStream+" , "+fname+", "+killProcess+") returned "+error);} |
| 293 |
return error; |
| 294 |
} |
| 295 |
|
| 296 |
public static final boolean close(OutputStream os, String fname){ |
| 297 |
if(verbose){System.err.println("close("+os+", "+fname+")");} |
| 298 |
boolean error=false; |
| 299 |
if(os!=null){error|=close(os);} |
| 300 |
if(fname!=null && os!=System.err && os!=System.out){error|=killProcess(fname);} |
| 301 |
if(verbose){System.err.println("close("+os+", "+fname+") returned "+error);} |
| 302 |
return error; |
| 303 |
} |
| 304 |
|
| 305 |
public static final boolean close(OutputStream os){ |
| 306 |
if(verbose){System.err.println("close("+os+")");} |
| 307 |
boolean error=false; |
| 308 |
try { |
| 309 |
os.flush(); |
| 310 |
} catch (IOException e1) { |
| 311 |
|
| 312 |
e1.printStackTrace(); |
| 313 |
error=true; |
| 314 |
} |
| 315 |
if(os.getClass()==ZipOutputStream.class){ |
| 316 |
ZipOutputStream zos=(ZipOutputStream)os; |
| 317 |
try { |
| 318 |
zos.closeEntry(); |
| 319 |
zos.finish(); |
| 320 |
} catch (IOException e) { |
| 321 |
|
| 322 |
e.printStackTrace(); |
| 323 |
error=true; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
if(os!=System.out && os!=System.err){ |
| 336 |
try { |
| 337 |
os.close(); |
| 338 |
} catch (IOException e) { |
| 339 |
|
| 340 |
e.printStackTrace(); |
| 341 |
error=true; |
| 342 |
} |
| 343 |
} |
| 344 |
if(verbose){System.err.println("close("+os+") returned "+error);} |
| 345 |
return error; |
| 346 |
} |
| 347 |
|
| 348 |
public static OutputStream getOutputStream(FileFormat ff, boolean buffered){ |
| 349 |
return getOutputStream(ff.name(), ff.append(), buffered, ff.allowSubprocess()); |
| 350 |
} |
| 351 |
|
| 352 |
public static OutputStream getOutputStream(String fname, boolean append, boolean buffered, boolean allowSubprocess){ |
| 353 |
|
| 354 |
if(verbose){ |
| 355 |
System.err.println("getOutputStream("+fname+", "+append+", "+buffered+", "+allowSubprocess+")"); |
| 356 |
new Exception().printStackTrace(System.err); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
fname=fname.replace('\\', '/'); |
| 362 |
assert(fname.indexOf('\\')<0); |
| 363 |
|
| 364 |
|
| 365 |
{ |
| 366 |
final int index=fname.lastIndexOf('/'); |
| 367 |
if(index>0){ |
| 368 |
File f=new File(fname.substring(0, index+1)); |
| 369 |
if(!f.exists()){f.mkdirs();} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
boolean gzipped=fname.endsWith(".gz") || fname.endsWith(".gzip"); |
| 374 |
boolean zipped=fname.endsWith(".zip"); |
| 375 |
boolean bzipped=PROCESS_BZ2 && fname.endsWith(".bz2"); |
| 376 |
boolean xz=PROCESS_XZ && fname.endsWith(".xz"); |
| 377 |
boolean dsrced=fname.endsWith(".dsrc"); |
| 378 |
boolean fqz=USE_FQZ && fname.endsWith(".fqz"); |
| 379 |
boolean alapy=USE_ALAPY && fname.endsWith(".ac"); |
| 380 |
boolean zst=USE_ALAPY && fname.endsWith(".zst"); |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
allowSubprocess=(allowSubprocess && Shared.threads()>1); |
| 385 |
|
| 386 |
if(gzipped){ |
| 387 |
|
| 388 |
return getGZipOutputStream(fname, append, allowSubprocess); |
| 389 |
}else if(zipped){ |
| 390 |
assert(!append) : "Append is not allowed for zip archives."; |
| 391 |
return getZipOutputStream(fname, buffered, allowSubprocess); |
| 392 |
}else if(bzipped){ |
| 393 |
assert(!append) : "Append is not allowed for bz2 archives."; |
| 394 |
return getBZipOutputStream(fname, buffered, append, allowSubprocess); |
| 395 |
}else if(zst){ |
| 396 |
|
| 397 |
return getZstdStream(fname, append); |
| 398 |
}else if(xz){ |
| 399 |
assert(!append) : "Append is not allowed for xz archives."; |
| 400 |
return getXZOutputStream(fname, buffered, allowSubprocess); |
| 401 |
}else if(dsrced){ |
| 402 |
assert(!append) : "Append is not allowed for dsrc archives."; |
| 403 |
return getDsrcOutputStream(fname, buffered, allowSubprocess); |
| 404 |
}else if(fqz){ |
| 405 |
assert(!append) : "Append is not allowed for fqz archives."; |
| 406 |
return getFqzStream(fname); |
| 407 |
}else if(alapy){ |
| 408 |
assert(!append) : "Append is not allowed for alapy archives."; |
| 409 |
return getAlapyStream(fname); |
| 410 |
} |
| 411 |
return getRawOutputStream(fname, append, buffered); |
| 412 |
} |
| 413 |
|
| 414 |
public static OutputStream getRawOutputStream(String fname, boolean append, boolean buffered){ |
| 415 |
|
| 416 |
if(verbose){System.err.println("getRawOutputStream("+fname+", "+append+", "+buffered+")");} |
| 417 |
|
| 418 |
if(fname.equals("stdout") || fname.startsWith("stdout.")){ |
| 419 |
return System.out; |
| 420 |
}else if(fname.equals("stderr") || fname.startsWith("stderr.")){ |
| 421 |
return System.err; |
| 422 |
}else if(fname.startsWith("/dev/null/")){ |
| 423 |
fname="/dev/null/"; |
| 424 |
} |
| 425 |
|
| 426 |
if(fname.indexOf('|')>=0){fname=fname.replace('|', '_');} |
| 427 |
|
| 428 |
FileOutputStream fos=null; |
| 429 |
try { |
| 430 |
fos = new FileOutputStream(fname, append); |
| 431 |
} catch (FileNotFoundException e) { |
| 432 |
synchronized(ReadWrite.class){ |
| 433 |
try { |
| 434 |
File f=new File(fname); |
| 435 |
String parent=f.getParent(); |
| 436 |
|
| 437 |
if(parent!=null){ |
| 438 |
f=new File(parent); |
| 439 |
if(!f.exists()){ |
| 440 |
boolean b=f.mkdirs(); |
| 441 |
if(!b){System.err.println("Warning - could not create directory "+f.getAbsolutePath());} |
| 442 |
} |
| 443 |
} |
| 444 |
fos = new FileOutputStream(fname, append); |
| 445 |
} catch (Exception e2) { |
| 446 |
throw new RuntimeException(e2); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
assert(fos!=null); |
| 451 |
if(buffered){return new BufferedOutputStream(fos);} |
| 452 |
return fos; |
| 453 |
} |
| 454 |
|
| 455 |
public static OutputStream getXZOutputStream(String fname, boolean buffered, boolean allowSubprocess){ |
| 456 |
final OutputStream raw=getRawOutputStream(fname, false, buffered); |
| 457 |
if(RAWMODE){return raw;} |
| 458 |
throw new RuntimeException("Unsupported format: XZ"); |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
} |
| 471 |
|
| 472 |
public static OutputStream getBZipOutputStream(String fname, boolean buffered, boolean append, boolean allowSubprocess){ |
| 473 |
if(verbose){System.err.println("getBZipOutputStream("+fname+", "+buffered+", "+append+", "+allowSubprocess+")");} |
| 474 |
|
| 475 |
|
| 476 |
if(RAWMODE){ |
| 477 |
final OutputStream raw=getRawOutputStream(fname, false, buffered); |
| 478 |
return raw; |
| 479 |
} |
| 480 |
|
| 481 |
if(USE_LBZIP2 && Data.LBZIP2()){return getLbzip2Stream(fname, append);} |
| 482 |
if(USE_PBZIP2 && Data.PBZIP2()){return getPbzip2Stream(fname, append);} |
| 483 |
if(USE_BZIP2 && Data.BZIP2()){return getBzip2Stream(fname, append);} |
| 484 |
|
| 485 |
throw new RuntimeException("bz2 compression not supported in this version, unless lbzip2, pbzip2 or bzip2 is installed."); |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
} |
| 504 |
|
| 505 |
public static OutputStream getDsrcOutputStream(String fname, boolean buffered, boolean append){ |
| 506 |
if(verbose){System.err.println("getDsrcOutputStream("+fname+", "+buffered+", "+append+")");} |
| 507 |
if(RAWMODE){ |
| 508 |
final OutputStream raw=getRawOutputStream(fname, false, buffered); |
| 509 |
return raw; |
| 510 |
} |
| 511 |
|
| 512 |
if(USE_DSRC && Data.DSRC() ){return getDsrcOutputStream2(fname, append);} |
| 513 |
|
| 514 |
throw new RuntimeException("dsrc compression requires dsrc in the path."); |
| 515 |
} |
| 516 |
|
| 517 |
public static OutputStream getZipOutputStream(String fname, boolean buffered, boolean allowSubprocess){ |
| 518 |
if(verbose){System.err.println("getZipOutputStream("+fname+", "+buffered+", "+allowSubprocess+")");} |
| 519 |
final OutputStream raw=getRawOutputStream(fname, false, buffered); |
| 520 |
if(RAWMODE){return raw;} |
| 521 |
try { |
| 522 |
ZipOutputStream out=new ZipOutputStream(raw); |
| 523 |
out.setLevel(Tools.min(ZIPLEVEL, 9)); |
| 524 |
final String basename=basename(fname); |
| 525 |
out.putNextEntry(new ZipEntry(basename)); |
| 526 |
return out; |
| 527 |
} catch (IOException e) { |
| 528 |
|
| 529 |
e.printStackTrace(); |
| 530 |
} |
| 531 |
assert(false); |
| 532 |
return null; |
| 533 |
} |
| 534 |
|
| 535 |
public static OutputStream getGZipOutputStream(String fname, boolean append, boolean allowSubprocess){ |
| 536 |
if(verbose){System.err.println("getGZipOutputStream("+fname+", "+append+", "+allowSubprocess+"); "+USE_BGZIP+", "+Data.BGZIP()+", "+USE_PIGZ+", "+USE_GZIP+", "+RAWMODE);} |
| 537 |
|
| 538 |
if(FORCE_BGZIP && USE_BGZIP && Data.BGZIP()){return getBgzipStream(fname, append);} |
| 539 |
if(FORCE_PIGZ || (allowSubprocess && Shared.threads()>=2)){ |
| 540 |
if((fname.endsWith(".vcf.gz") || fname.endsWith(".sam.gz") || (PREFER_BGZIP && ZIPLEVEL<10)) && USE_BGZIP && Data.BGZIP()){return getBgzipStream(fname, append);} |
| 541 |
if(USE_PIGZ && Data.PIGZ()){return getPigzStream(fname, append);} |
| 542 |
if(USE_BGZIP && Data.BGZIP()){return getBgzipStream(fname, append);} |
| 543 |
if(USE_GZIP && Data.GZIP()){return getGzipStream(fname, append);} |
| 544 |
} |
| 545 |
final OutputStream raw=getRawOutputStream(fname, append, false); |
| 546 |
if(RAWMODE){return raw;} |
| 547 |
try { |
| 548 |
final GZIPOutputStream out=new GZIPOutputStream(raw, 8192){ |
| 549 |
{ |
| 550 |
|
| 551 |
def.setLevel(Tools.min(ZIPLEVEL, 9)); |
| 552 |
} |
| 553 |
}; |
| 554 |
return out; |
| 555 |
} catch (IOException e) { |
| 556 |
|
| 557 |
e.printStackTrace(); |
| 558 |
} |
| 559 |
assert(false); |
| 560 |
return null; |
| 561 |
} |
| 562 |
|
| 563 |
public static OutputStream getPigzStream(String fname, boolean append){ |
| 564 |
if(verbose){System.err.println("getPigzStream("+fname+")");} |
| 565 |
|
| 566 |
int threads=Tools.min(MAX_ZIP_THREADS, Tools.max((int)((Shared.threads()+1)*ZIP_THREAD_MULT), 1)); |
| 567 |
|
| 568 |
threads=Tools.max(1, Tools.min(Shared.threads(), threads)); |
| 569 |
|
| 570 |
int zl=(ZIPLEVEL<10 ? ZIPLEVEL : Data.PIGZ_VERSION_23plus ? 11 : 9); |
| 571 |
if(ALLOW_ZIPLEVEL_CHANGE && threads>=4 && zl>0 && zl<4){zl=4;} |
| 572 |
if(zl<3){threads=Tools.min(threads, 12);} |
| 573 |
else if(zl<5){threads=Tools.min(threads, 24);} |
| 574 |
else if(zl<7){threads=Tools.min(threads, 40);} |
| 575 |
|
| 576 |
OutputStream out; |
| 577 |
String command="pigz -c -p "+threads+" -"+zl; |
| 578 |
if(PIGZ_BLOCKSIZE!=128){ |
| 579 |
command=command+" -b "+PIGZ_BLOCKSIZE; |
| 580 |
} |
| 581 |
if(PIGZ_ITERATIONS>0 && Data.PIGZ_VERSION_231plus){ |
| 582 |
command=command+" -I "+PIGZ_ITERATIONS; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
out=getOutputStreamFromProcess(fname, command, true, append, true, true); |
| 590 |
|
| 591 |
return out; |
| 592 |
} |
| 593 |
|
| 594 |
public static OutputStream getFqzStream(String fname){ |
| 595 |
if(verbose){System.err.println("getFqzStream("+fname+")");} |
| 596 |
String command="fqz_comp -s"+Tools.mid(1, ZIPLEVEL, 8)+"+"; |
| 597 |
if(ZIPLEVEL>5){command=command+" -q3";} |
| 598 |
|
| 599 |
OutputStream out=getOutputStreamFromProcess(fname, command, true, false, true, true); |
| 600 |
return out; |
| 601 |
} |
| 602 |
|
| 603 |
public static OutputStream getAlapyStream(String fname){ |
| 604 |
if(verbose){System.err.println("getAlapyStream("+fname+")");} |
| 605 |
String compression=(ZIPLEVEL>6 ? "-l best" : ZIPLEVEL<4 ? "-l fast" : "-l medium"); |
| 606 |
|
| 607 |
String command="alapy_arc "+compression+" -n "+fname+" -q -c - "; |
| 608 |
OutputStream out=getOutputStreamFromProcess(fname, command, true, false, true, false); |
| 609 |
return out; |
| 610 |
} |
| 611 |
|
| 612 |
public static OutputStream getGzipStream(String fname, boolean append){ |
| 613 |
if(verbose){System.err.println("getGzipStream("+fname+")");} |
| 614 |
OutputStream out=getOutputStreamFromProcess(fname, "gzip -c -"+Tools.min(ZIPLEVEL, 9), true, append, true, true); |
| 615 |
return out; |
| 616 |
} |
| 617 |
|
| 618 |
public static OutputStream getBgzipStream(String fname, boolean append){ |
| 619 |
if(verbose){System.err.println("getBgzipStream("+fname+")");} |
| 620 |
|
| 621 |
int threads=Tools.min(MAX_ZIP_THREADS, Tools.max((int)((Shared.threads()+1)*ZIP_THREAD_MULT), 1)); |
| 622 |
|
| 623 |
threads=Tools.max(1, Tools.min(Shared.threads(), threads)); |
| 624 |
|
| 625 |
int zl=Tools.mid(ZIPLEVEL, 1, 9); |
| 626 |
if(ALLOW_ZIPLEVEL_CHANGE && threads>=4 && zl>0 && zl<4){zl=4;} |
| 627 |
if(zl<3){threads=Tools.min(threads, 12);} |
| 628 |
else if(zl<5){threads=Tools.min(threads, 16);} |
| 629 |
else if(zl<7){threads=Tools.min(threads, 32);} |
| 630 |
|
| 631 |
|
| 632 |
String command="bgzip -c "+(append ? "" : "-f ")+(Data.BGZIP_VERSION_levelFlag ? "-l "+zl+" " : "")+(Data.BGZIP_VERSION_threadsFlag ? "-@ "+threads+" " : ""); |
| 633 |
if(verbose){System.err.println(command);} |
| 634 |
OutputStream out=getOutputStreamFromProcess(fname, command, true, append, true, true); |
| 635 |
if(verbose){System.err.println("fetched bgzip stream.");} |
| 636 |
return out; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
public static OutputStream getZstdStream(String fname, boolean append){ |
| 659 |
if(verbose){System.err.println("getZstdStream("+fname+")");} |
| 660 |
|
| 661 |
int threads=Tools.min(MAX_ZIP_THREADS, Tools.max((int)((Shared.threads()+1)*ZIP_THREAD_MULT), 1)); |
| 662 |
threads=Tools.max(1, Tools.min(Shared.threads(), threads)); |
| 663 |
int zl=Tools.mid(ZIPLEVEL, 1, 19); |
| 664 |
if(ALLOW_ZIPLEVEL_CHANGE && threads>=4 && zl>0 && zl<4){zl=4;} |
| 665 |
if(zl<3){threads=Tools.min(threads, 12);} |
| 666 |
else if(zl<5){threads=Tools.min(threads, 16);} |
| 667 |
else if(zl<7){threads=Tools.min(threads, 32);} |
| 668 |
|
| 669 |
String command="zstd -"+zl+" -T"+threads; |
| 670 |
if(verbose){System.err.println(command);} |
| 671 |
OutputStream out=getOutputStreamFromProcess(fname, command, true, append, true, true); |
| 672 |
if(verbose){System.err.println("fetched zstd stream.");} |
| 673 |
return out; |
| 674 |
} |
| 675 |
|
| 676 |
public static OutputStream getBzip2Stream(String fname, boolean append){ |
| 677 |
if(verbose){System.err.println("getBzip2Stream("+fname+")");} |
| 678 |
String command="bzip2 -c -"+Tools.min(BZIPLEVEL, 9); |
| 679 |
OutputStream out=getOutputStreamFromProcess(fname, command, true, append, true, true); |
| 680 |
return out; |
| 681 |
} |
| 682 |
|
| 683 |
public static OutputStream getPbzip2Stream(String fname, boolean append){ |
| 684 |
if(verbose){System.err.println("getPbzip2Stream("+fname+")");} |
| 685 |
int threads=Tools.min(MAX_ZIP_THREADS, Tools.max((int)((Shared.threads()+1)*ZIP_THREAD_MULT), 1)); |
| 686 |
threads=Tools.max(1, Tools.min(Shared.threads(), threads)); |
| 687 |
String command="pbzip2 -c -p"+threads+" -"+Tools.min(BZIPLEVEL, 9); |
| 688 |
OutputStream out=getOutputStreamFromProcess(fname, command, true, append, true, true); |
| 689 |
return out; |
| 690 |
} |
| 691 |
|
| 692 |
public static OutputStream getLbzip2Stream(String fname, boolean append){ |
| 693 |
if(verbose){System.err.println("getLbzip2Stream("+fname+")");} |
| 694 |
String command="lbzip2 -"+Tools.min(BZIPLEVEL, 9); |
| 695 |
OutputStream out=getOutputStreamFromProcess(fname, command, true, append, true, true); |
| 696 |
return out; |
| 697 |
} |
| 698 |
|
| 699 |
public static OutputStream getDsrcOutputStream2(String fname, boolean append){ |
| 700 |
if(verbose){System.err.println("getDsrcOutpustream2("+fname+")");} |
| 701 |
int threads=Tools.min(MAX_ZIP_THREADS, Tools.max((int)((Shared.threads()+1)*ZIP_THREAD_MULT), 1)); |
| 702 |
threads=Tools.max(1, Tools.min(Shared.threads()-1, threads)); |
| 703 |
String params=null; |
| 704 |
if(ZIPLEVEL<=2){ |
| 705 |
params="-d0 -q0 -b8"; |
| 706 |
}else if(ZIPLEVEL<=4){ |
| 707 |
params="-d1 -q1 -b16"; |
| 708 |
}else if(ZIPLEVEL<=8){ |
| 709 |
params="-d2 -q2 -b32"; |
| 710 |
}else{ |
| 711 |
params="-d3 -q2 -b64"; |
| 712 |
} |
| 713 |
String command="dsrc c -t"+threads+" "+params+" -s"; |
| 714 |
if(fname.equals("stdout") || fname.startsWith("stdout.")){ |
| 715 |
|
| 716 |
assert(false) : "Undefined dsrc option."; |
| 717 |
}else{ |
| 718 |
command+=" "+fname; |
| 719 |
} |
| 720 |
System.err.println(command); |
| 721 |
|
| 722 |
OutputStream out=getOutputStreamFromProcess(fname, command+" "+fname, true, append, true, false); |
| 723 |
return out; |
| 724 |
} |
| 725 |
|
| 726 |
public static OutputStream getOutputStreamFromProcess(final String fname, final String command, boolean sh, boolean append, boolean useProcessBuilder, boolean useFname){ |
| 727 |
if(verbose){System.err.println("getOutputStreamFromProcess("+fname+", "+command+", "+sh+", "+useProcessBuilder+")");} |
| 728 |
|
| 729 |
OutputStream out=null; |
| 730 |
Process p=null; |
| 731 |
if(useProcessBuilder){ |
| 732 |
ProcessBuilder pb=new ProcessBuilder(); |
| 733 |
pb.redirectError(Redirect.INHERIT); |
| 734 |
|
| 735 |
if(fname.equals("stdout") || fname.startsWith("stdout.")){ |
| 736 |
pb.redirectOutput(Redirect.INHERIT); |
| 737 |
pb.command(command.split(" ")); |
| 738 |
}else{ |
| 739 |
|
| 740 |
if(useFname){ |
| 741 |
if(append){ |
| 742 |
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(new File(fname))); |
| 743 |
}else{ |
| 744 |
pb.redirectOutput(new File(fname)); |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
pb.command(command.split(" ")); |
| 749 |
} |
| 750 |
try { |
| 751 |
p=pb.start(); |
| 752 |
} catch (IOException e) { |
| 753 |
|
| 754 |
e.printStackTrace(); |
| 755 |
} |
| 756 |
assert(p!=null) : "Could not execute "+command; |
| 757 |
addProcess(fname, p); |
| 758 |
out=p.getOutputStream(); |
| 759 |
{ |
| 760 |
out=p.getOutputStream(); |
| 761 |
InputStream es=p.getErrorStream(); |
| 762 |
assert(es!=null); |
| 763 |
PipeThread et=new PipeThread(es, System.err); |
| 764 |
addPipeThread(fname, et); |
| 765 |
et.start(); |
| 766 |
} |
| 767 |
return out; |
| 768 |
} |
| 769 |
|
| 770 |
if(fname.equals("stdout") || fname.startsWith("stdout.")){ |
| 771 |
try { |
| 772 |
p = Runtime.getRuntime().exec(command); |
| 773 |
} catch (IOException e) { |
| 774 |
|
| 775 |
e.printStackTrace(); |
| 776 |
} |
| 777 |
assert(p!=null) : "Could not execute "+command; |
| 778 |
InputStream is=p.getInputStream(); |
| 779 |
PipeThread it=new PipeThread(is, System.out); |
| 780 |
addPipeThread(fname, it); |
| 781 |
it.start(); |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
}else{ |
| 793 |
try { |
| 794 |
if(sh){ |
| 795 |
String[] cmd = { |
| 796 |
"sh", |
| 797 |
"-c", |
| 798 |
command+(useFname ? " 1"+(append ? ">>" : ">")+fname : "") |
| 799 |
}; |
| 800 |
p=Runtime.getRuntime().exec(cmd); |
| 801 |
}else{ |
| 802 |
|
| 803 |
assert(false) : command; |
| 804 |
p=Runtime.getRuntime().exec(command); |
| 805 |
} |
| 806 |
} catch (IOException e) { |
| 807 |
|
| 808 |
e.printStackTrace(); |
| 809 |
} |
| 810 |
} |
| 811 |
assert(p!=null) : "Could not execute "+command; |
| 812 |
addProcess(fname, p); |
| 813 |
out=p.getOutputStream(); |
| 814 |
InputStream es=p.getErrorStream(); |
| 815 |
assert(es!=null); |
| 816 |
PipeThread et=new PipeThread(es, System.err); |
| 817 |
addPipeThread(fname, et); |
| 818 |
et.start(); |
| 819 |
|
| 820 |
return out; |
| 821 |
} |
| 822 |
|
| 823 |
public static String readString(String fname){ |
| 824 |
if(verbose){System.err.println("readString("+fname+")");} |
| 825 |
String x=null; |
| 826 |
InputStream is=getInputStream(fname, false, false); |
| 827 |
|
| 828 |
try { |
| 829 |
|
| 830 |
StringBuilder sb=new StringBuilder(); |
| 831 |
|
| 832 |
|
| 833 |
BufferedReader in=new BufferedReader(new InputStreamReader(is), INBUF); |
| 834 |
String temp=in.readLine(); |
| 835 |
while(temp!=null){ |
| 836 |
sb.append(temp).append('\n'); |
| 837 |
temp=in.readLine(); |
| 838 |
} |
| 839 |
in.close(); |
| 840 |
|
| 841 |
|
| 842 |
x=sb.toString(); |
| 843 |
} catch (FileNotFoundException e) { |
| 844 |
throw new RuntimeException(e); |
| 845 |
} catch (IOException e) { |
| 846 |
throw new RuntimeException(e); |
| 847 |
} catch (OutOfMemoryError e) { |
| 848 |
KillSwitch.memKill(e); |
| 849 |
} |
| 850 |
|
| 851 |
return x; |
| 852 |
} |
| 853 |
|
| 854 |
public static Object readObject(String fname, boolean allowSubprocess){ |
| 855 |
if(verbose){System.err.println("readObject("+fname+")");} |
| 856 |
Object x=null; |
| 857 |
InputStream is=getInputStream(fname, true, allowSubprocess); |
| 858 |
|
| 859 |
try { |
| 860 |
|
| 861 |
ObjectInputStream in=new ObjectInputStream(is); |
| 862 |
x=in.readObject(); |
| 863 |
in.close(); |
| 864 |
|
| 865 |
} catch (IOException e) { |
| 866 |
throw new RuntimeException(e); |
| 867 |
} catch (ClassNotFoundException e) { |
| 868 |
throw new RuntimeException(e); |
| 869 |
} catch (OutOfMemoryError e) { |
| 870 |
KillSwitch.memKill(e); |
| 871 |
} |
| 872 |
|
| 873 |
return x; |
| 874 |
} |
| 875 |
|
| 876 |
public static InputStream getInputStream(String fname, boolean buffer, boolean allowSubprocess){ |
| 877 |
if(verbose){System.err.println("getInputStream("+fname+", "+buffer+", "+allowSubprocess+")");} |
| 878 |
boolean xz=fname.endsWith(".xz"); |
| 879 |
boolean gzipped=fname.endsWith(".gz") || fname.endsWith(".gzip"); |
| 880 |
boolean zipped=fname.endsWith(".zip"); |
| 881 |
boolean bzipped=PROCESS_BZ2 && fname.endsWith(".bz2"); |
| 882 |
boolean dsrced=fname.endsWith(".dsrc"); |
| 883 |
boolean bam=fname.endsWith(".bam") && (SAMBAMBA() || Data.SAMTOOLS()); |
| 884 |
boolean fqz=fname.endsWith(".fqz"); |
| 885 |
boolean alapy=fname.endsWith(".ac"); |
| 886 |
boolean zst=fname.endsWith(".zst"); |
| 887 |
|
| 888 |
allowSubprocess=(allowSubprocess && Shared.threads()>1); |
| 889 |
|
| 890 |
if(!RAWMODE){ |
| 891 |
if(zipped){return getZipInputStream(fname);} |
| 892 |
if(gzipped){return getGZipInputStream(fname, allowSubprocess, false);} |
| 893 |
if(bzipped){return getBZipInputStream(fname, allowSubprocess);} |
| 894 |
if(dsrced){return getDsrcInputStream(fname);} |
| 895 |
if(zst) {return getUnzstdStream(fname);} |
| 896 |
if(bam){ |
| 897 |
if(SAMBAMBA()){ |
| 898 |
String command="sambamba -q view -h"; |
| 899 |
|
| 900 |
if(SAMTOOLS_IGNORE_FLAG!=0){ |
| 901 |
command=command+" --num-filter=0/"+SAMTOOLS_IGNORE_FLAG; |
| 902 |
} |
| 903 |
return getInputStreamFromProcess(fname, command, false, true, true); |
| 904 |
}else{ |
| 905 |
String command="samtools view -h"; |
| 906 |
|
| 907 |
if(SAMTOOLS_IGNORE_FLAG!=0){ |
| 908 |
|
| 909 |
command=command+" -F 0x"+Integer.toHexString(SAMTOOLS_IGNORE_FLAG); |
| 910 |
} |
| 911 |
String version=Data.SAMTOOLS_VERSION; |
| 912 |
if(Shared.threads()>1 && version!=null && version.startsWith("1.") && version.length()>2){ |
| 913 |
try { |
| 914 |
String[] split=version.split("\\."); |
| 915 |
int number=-1; |
| 916 |
try { |
| 917 |
number=Integer.parseInt(split[1]); |
| 918 |
} catch (Exception e) {} |
| 919 |
if(number<0){ |
| 920 |
try { |
| 921 |
number=Integer.parseInt(split[1].substring(0, 1)); |
| 922 |
} catch (Exception e1) {} |
| 923 |
} |
| 924 |
if(number>3){ |
| 925 |
command=command+" -@ 2"; |
| 926 |
} |
| 927 |
} catch (NumberFormatException e) { |
| 928 |
|
| 929 |
e.printStackTrace(); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
return getInputStreamFromProcess(fname, command, false, true, true); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
if(fqz){return getInputStreamFromProcess(fname, "fqz_comp -d ", false, true, true);} |
| 938 |
if(alapy){ |
| 939 |
return getInputStreamFromProcess(fname, "alapy_arc -q -d "+fname+" -", false, false, true); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
return getRawInputStream(fname, buffer); |
| 944 |
} |
| 945 |
|
| 946 |
public static InputStream getRawInputStream(String fname, boolean buffer){ |
| 947 |
if(verbose){System.err.println("getRawInputStream("+fname+", "+buffer+")");} |
| 948 |
|
| 949 |
assert(fname!=null); |
| 950 |
fname=fname.replace('\\', '/'); |
| 951 |
assert(fname.indexOf('\\')<0); |
| 952 |
assert(!fname.contains("\\\\")); |
| 953 |
|
| 954 |
|
| 955 |
final boolean jar=fname.startsWith("jar:"); |
| 956 |
|
| 957 |
if(!jar){ |
| 958 |
boolean failed=false; |
| 959 |
File f=new File(fname); |
| 960 |
if(!f.exists()){ |
| 961 |
String f2=fname.toLowerCase(); |
| 962 |
if(f2.equals("stdin") || f2.startsWith("stdin.")){ |
| 963 |
|
| 964 |
return System.in; |
| 965 |
} |
| 966 |
|
| 967 |
if(fname.indexOf('/')<0){ |
| 968 |
f2=Data.ROOT_CURRENT+"/"+fname; |
| 969 |
if(!new File(f2).exists()){ |
| 970 |
failed=true; |
| 971 |
}else{ |
| 972 |
fname=f2; |
| 973 |
} |
| 974 |
}else{ |
| 975 |
failed=true; |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
if(failed){ |
| 980 |
shared.KillSwitch.exceptionKill(new RuntimeException("Can't find file "+fname)); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
InputStream in=null; |
| 990 |
if(jar){ |
| 991 |
try { |
| 992 |
|
| 993 |
URL url=new URL(fname); |
| 994 |
|
| 995 |
InputStream is=url.openStream(); |
| 996 |
|
| 997 |
if(buffer){ |
| 998 |
BufferedInputStream bis=new BufferedInputStream(is, INBUF); |
| 999 |
in=bis; |
| 1000 |
}else{ |
| 1001 |
in=is; |
| 1002 |
} |
| 1003 |
|
| 1004 |
} catch (FileNotFoundException e) { |
| 1005 |
System.err.println("Error when attempting to read "+fname); |
| 1006 |
throw new RuntimeException(e); |
| 1007 |
} catch (MalformedURLException e) { |
| 1008 |
System.err.println("Error when attempting to read "+fname); |
| 1009 |
throw new RuntimeException(e); |
| 1010 |
} catch (IOException e) { |
| 1011 |
System.err.println("Error when attempting to read "+fname); |
| 1012 |
throw new RuntimeException(e); |
| 1013 |
} |
| 1014 |
}else{ |
| 1015 |
try { |
| 1016 |
|
| 1017 |
FileInputStream fis=new FileInputStream(fname); |
| 1018 |
|
| 1019 |
if(buffer){ |
| 1020 |
BufferedInputStream bis=new BufferedInputStream(fis, INBUF); |
| 1021 |
in=bis; |
| 1022 |
}else{ |
| 1023 |
in=fis; |
| 1024 |
} |
| 1025 |
|
| 1026 |
} catch (FileNotFoundException e) { |
| 1027 |
throw new RuntimeException(e); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
return in; |
| 1032 |
} |
| 1033 |
|
| 1034 |
public static InputStream getZipInputStream(String fname){return getZipInputStream(fname, true);} |
| 1035 |
public static InputStream getZipInputStream(String fname, boolean buffer){ |
| 1036 |
if(verbose){System.err.println("getZipInputStream("+fname+", "+buffer+")");} |
| 1037 |
InputStream raw=getRawInputStream(fname, buffer); |
| 1038 |
InputStream in=null; |
| 1039 |
|
| 1040 |
final String basename=basename(fname); |
| 1041 |
|
| 1042 |
try { |
| 1043 |
|
| 1044 |
ZipInputStream zis=new ZipInputStream(raw); |
| 1045 |
ZipEntry ze=zis.getNextEntry(); |
| 1046 |
assert(ze!=null); |
| 1047 |
assert(basename.equals(ze.getName())) : basename+" != "+ze.getName(); |
| 1048 |
in=zis; |
| 1049 |
|
| 1050 |
} catch (FileNotFoundException e) { |
| 1051 |
System.err.println("Error when attempting to read "+fname); |
| 1052 |
throw new RuntimeException(e); |
| 1053 |
} catch (IOException e) { |
| 1054 |
System.err.println("Error when attempting to read "+fname); |
| 1055 |
throw new RuntimeException(e); |
| 1056 |
} |
| 1057 |
|
| 1058 |
return in; |
| 1059 |
} |
| 1060 |
|
| 1061 |
public static InputStream getGZipInputStream(String fname, boolean allowSubprocess, boolean buffer){ |
| 1062 |
if(verbose){ |
| 1063 |
System.err.println("getGZipInputStream("+fname+", "+allowSubprocess+")"); |
| 1064 |
|
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
if(allowSubprocess && Shared.threads()>2){ |
| 1069 |
if(!fname.startsWith("jar:")){ |
| 1070 |
if(verbose){ |
| 1071 |
System.err.println("Fetching gzip input stream: "+fname+", allowSubprocess="+allowSubprocess+", USE_UNPIGZ="+USE_UNPIGZ+", Data.PIGZ()="+Data.PIGZ()); |
| 1072 |
} |
| 1073 |
if((PREFER_UNBGZIP || fname.endsWith(".vcf.gz")) && USE_UNBGZIP && Data.BGZIP()){ |
| 1074 |
if(!fname.contains("stdin") && new File(fname).exists()){ |
| 1075 |
int magicNumber=getMagicNumber(fname); |
| 1076 |
if(magicNumber==529205252){return getUnbgzipStream(fname);} |
| 1077 |
|
| 1078 |
} |
| 1079 |
} |
| 1080 |
if(USE_UNPIGZ && Data.PIGZ()){return getUnpigzStream(fname);} |
| 1081 |
|
| 1082 |
if(USE_GUNZIP && Data.GUNZIP()){return getGunzipStream(fname);} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
InputStream raw=getRawInputStream(fname, buffer); |
| 1089 |
InputStream in=null; |
| 1090 |
try { |
| 1091 |
in=new GZIPInputStream(raw, INBUF); |
| 1092 |
} catch (FileNotFoundException e) { |
| 1093 |
System.err.println("Error when attempting to read "+fname); |
| 1094 |
throw new RuntimeException(e); |
| 1095 |
} catch (IOException e) { |
| 1096 |
System.err.println("Error when attempting to read "+fname); |
| 1097 |
throw new RuntimeException(e); |
| 1098 |
} |
| 1099 |
|
| 1100 |
return in; |
| 1101 |
} |
| 1102 |
|
| 1103 |
public static InputStream getGunzipStream(String fname){ |
| 1104 |
if(verbose){System.err.println("getGunzipStream("+fname+")");} |
| 1105 |
return getInputStreamFromProcess(fname, "gzip -c -d", false, true, true); |
| 1106 |
} |
| 1107 |
|
| 1108 |
public static InputStream getUnpigzStream(String fname){ |
| 1109 |
if(verbose){System.err.println("getUnpigzStream("+fname+")");} |
| 1110 |
return getInputStreamFromProcess(fname, "pigz -c -d", false, true, true); |
| 1111 |
} |
| 1112 |
|
| 1113 |
public static InputStream getUnbgzipStream(String fname){ |
| 1114 |
if(verbose){System.err.println("getUnbgzipStream("+fname+")");} |
| 1115 |
int threads=Tools.mid(4, 1, Shared.threads()); |
| 1116 |
return getInputStreamFromProcess(fname, "bgzip -c -d"+(Data.BGZIP_VERSION_threadsFlag ? " -@ "+threads : ""), false, true, true); |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
public static InputStream getUnzstdStream(String fname){ |
| 1122 |
if(verbose){System.err.println("getUnzstdStream("+fname+")");} |
| 1123 |
return getInputStreamFromProcess(fname, "zstd -f -d", false, true, true); |
| 1124 |
} |
| 1125 |
|
| 1126 |
public static InputStream getUnpbzip2Stream(String fname){ |
| 1127 |
if(verbose){System.err.println("getUnpbzip2Stream("+fname+")");} |
| 1128 |
return getInputStreamFromProcess(fname, "pbzip2 -c -d", false, true, true); |
| 1129 |
} |
| 1130 |
|
| 1131 |
public static InputStream getUnlbzip2Stream(String fname){ |
| 1132 |
if(verbose){System.err.println("getUnlbzip2Stream("+fname+")");} |
| 1133 |
return getInputStreamFromProcess(fname, "lbzip2 -c -d", false, true, true); |
| 1134 |
} |
| 1135 |
|
| 1136 |
public static InputStream getUnbzip2Stream(String fname){ |
| 1137 |
if(verbose){System.err.println("getUnbzip2Stream("+fname+")");} |
| 1138 |
return getInputStreamFromProcess(fname, "bzip2 -c -d", false, true, true); |
| 1139 |
} |
| 1140 |
|
| 1141 |
public static InputStream getUnDsrcStream(String fname){ |
| 1142 |
if(verbose){System.err.println("getUnDsrcStream("+fname+")");} |
| 1143 |
int threads=Tools.min(MAX_ZIP_THREADS, Tools.max((int)((Shared.threads()+1)*ZIP_THREAD_MULT), 1)); |
| 1144 |
threads=Tools.max(1, Tools.min(Shared.threads()-1, threads)); |
| 1145 |
return getInputStreamFromProcess(fname, "dsrc d -s -t"+threads, false, true, true); |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
public static InputStream getInputStreamFromProcess(final String fname, String command, boolean cat, final boolean appendFname, final boolean useProcessBuilder){ |
| 1150 |
if(verbose){System.err.println("getInputStreamFromProcess("+fname+", "+command+", "+cat+")");} |
| 1151 |
|
| 1152 |
|
| 1153 |
InputStream in=null; |
| 1154 |
|
| 1155 |
Process p=null; |
| 1156 |
|
| 1157 |
if(useProcessBuilder){ |
| 1158 |
ProcessBuilder pb=new ProcessBuilder(); |
| 1159 |
pb.redirectError(Redirect.INHERIT); |
| 1160 |
|
| 1161 |
if(fname.equals("stdin") || fname.startsWith("stdin.")){ |
| 1162 |
pb.redirectInput(Redirect.INHERIT); |
| 1163 |
pb.command(command.split(" ")); |
| 1164 |
}else{ |
| 1165 |
if(appendFname){ |
| 1166 |
command=command+" "+fname; |
| 1167 |
}else{ |
| 1168 |
pb.redirectInput(new File(fname)); |
| 1169 |
} |
| 1170 |
|
| 1171 |
pb.command(command.split(" ")); |
| 1172 |
} |
| 1173 |
try { |
| 1174 |
p=pb.start(); |
| 1175 |
} catch (IOException e) { |
| 1176 |
|
| 1177 |
e.printStackTrace(); |
| 1178 |
} |
| 1179 |
assert(p!=null) : "Could not execute "+command; |
| 1180 |
addProcess(fname, p); |
| 1181 |
in=p.getInputStream(); |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
return in; |
| 1191 |
} |
| 1192 |
|
| 1193 |
if(!appendFname){ |
| 1194 |
try { |
| 1195 |
p=Runtime.getRuntime().exec(command); |
| 1196 |
} catch (IOException e) { |
| 1197 |
|
| 1198 |
e.printStackTrace(); |
| 1199 |
} |
| 1200 |
}else if(fname.equals("stdin") || fname.startsWith("stdin.")){ |
| 1201 |
try { |
| 1202 |
if(cat){ |
| 1203 |
throw new RuntimeException(); |
| 1204 |
}else{ |
| 1205 |
p=Runtime.getRuntime().exec(command); |
| 1206 |
} |
| 1207 |
} catch (IOException e) { |
| 1208 |
|
| 1209 |
e.printStackTrace(); |
| 1210 |
} |
| 1211 |
assert(p!=null) : "Could not execute "+command; |
| 1212 |
OutputStream os=p.getOutputStream(); |
| 1213 |
PipeThread it=new PipeThread(System.in, os); |
| 1214 |
addPipeThread(fname, it); |
| 1215 |
it.start(); |
| 1216 |
}else{ |
| 1217 |
try { |
| 1218 |
if(cat){ |
| 1219 |
assert(false) : "This mode is untested."; |
| 1220 |
String[] cmd = { |
| 1221 |
"sh","cat "+fname, |
| 1222 |
" | "+command |
| 1223 |
}; |
| 1224 |
p=Runtime.getRuntime().exec(cmd); |
| 1225 |
}else{ |
| 1226 |
p = Runtime.getRuntime().exec(command+" "+fname); |
| 1227 |
} |
| 1228 |
} catch (IOException e) { |
| 1229 |
|
| 1230 |
e.printStackTrace(); |
| 1231 |
} |
| 1232 |
} |
| 1233 |
assert(p!=null) : "Could not execute "+command; |
| 1234 |
|
| 1235 |
addProcess(fname, p); |
| 1236 |
in=p.getInputStream(); |
| 1237 |
InputStream es=p.getErrorStream(); |
| 1238 |
assert(es!=null); |
| 1239 |
PipeThread et=new PipeThread(es, System.err); |
| 1240 |
addPipeThread(fname, et); |
| 1241 |
et.start(); |
| 1242 |
|
| 1243 |
return in; |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
public static InputStream getBZipInputStream(String fname, boolean allowSubprocess){ |
| 1248 |
if(verbose){System.err.println("getBZipInputStream("+fname+")");} |
| 1249 |
InputStream in=null; |
| 1250 |
|
| 1251 |
try {in=getBZipInputStream2(fname, allowSubprocess);} |
| 1252 |
catch (IOException e) { |
| 1253 |
System.err.println("Error when attempting to read "+fname); |
| 1254 |
throw new RuntimeException(e); |
| 1255 |
}catch (NullPointerException e) { |
| 1256 |
System.err.println("Error when attempting to read "+fname); |
| 1257 |
throw new RuntimeException(e); |
| 1258 |
} |
| 1259 |
|
| 1260 |
assert(in!=null); |
| 1261 |
return in; |
| 1262 |
} |
| 1263 |
|
| 1264 |
private static InputStream getBZipInputStream2(String fname, boolean allowSubprocess) throws IOException{ |
| 1265 |
if(verbose){ |
| 1266 |
if(verbose){System.err.println("getBZipInputStream("+fname+")");} |
| 1267 |
} |
| 1268 |
|
| 1269 |
if(!fname.startsWith("jar:")){ |
| 1270 |
if(verbose){System.err.println("Fetching bz2 input stream: "+fname+", "+USE_PBZIP2+", "+USE_BZIP2+", "+Data.PBZIP2()+Data.BZIP2());} |
| 1271 |
if(USE_LBZIP2 && Data.LBZIP2()){return getUnlbzip2Stream(fname);} |
| 1272 |
if(USE_PBZIP2 && Data.PBZIP2()){return getUnpbzip2Stream(fname);} |
| 1273 |
if(USE_BZIP2 && Data.BZIP2()){return getUnbzip2Stream(fname);} |
| 1274 |
} |
| 1275 |
|
| 1276 |
throw new IOException("\nlbzip2, pbzip2, or bzip2 must be in the path to read bz2 files:\n"+fname+"\n"); |
| 1277 |
} |
| 1278 |
|
| 1279 |
public static InputStream getDsrcInputStream(String fname){ |
| 1280 |
if(verbose){System.err.println("getDsrcInputStream("+fname+")");} |
| 1281 |
InputStream in=null; |
| 1282 |
|
| 1283 |
try {in=getDsrcInputStream2(fname);} |
| 1284 |
catch (IOException e) { |
| 1285 |
System.err.println("Error when attempting to read "+fname); |
| 1286 |
throw new RuntimeException(e); |
| 1287 |
}catch (NullPointerException e) { |
| 1288 |
System.err.println("Error when attempting to read "+fname); |
| 1289 |
throw new RuntimeException(e); |
| 1290 |
} |
| 1291 |
|
| 1292 |
assert(in!=null); |
| 1293 |
return in; |
| 1294 |
} |
| 1295 |
|
| 1296 |
private static InputStream getDsrcInputStream2(String fname) throws IOException{ |
| 1297 |
if(verbose){ |
| 1298 |
if(verbose){System.err.println("getDsrcInputStream2("+fname+")");} |
| 1299 |
} |
| 1300 |
|
| 1301 |
if(USE_DSRC && Data.DSRC()){return getUnDsrcStream(fname);} |
| 1302 |
|
| 1303 |
throw new IOException("\nDsrc must be in the path to read Dsrc files:\n"+fname+"\n"); |
| 1304 |
} |
| 1305 |
|
| 1306 |
public static InputStream getXZInputStream(String fname){ |
| 1307 |
|
| 1308 |
InputStream in=null; |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
return in; |
| 1322 |
} |
| 1323 |
|
| 1324 |
public static byte[] readRaw(String fname) throws IOException{ |
| 1325 |
InputStream ris=getRawInputStream(fname, false); |
| 1326 |
ByteBuilder bb=new ByteBuilder(); |
| 1327 |
byte[] buffer=new byte[16384]; |
| 1328 |
int x=ris.read(buffer); |
| 1329 |
while(x>0){ |
| 1330 |
bb.append(buffer, x); |
| 1331 |
x=ris.read(buffer); |
| 1332 |
} |
| 1333 |
ris.close(); |
| 1334 |
return bb.toBytes(); |
| 1335 |
} |
| 1336 |
|
| 1337 |
public static <X> X read(Class<X> cx, String fname, boolean allowSubprocess){ |
| 1338 |
X x=(X)readObject(fname, allowSubprocess); |
| 1339 |
return x; |
| 1340 |
} |
| 1341 |
|
| 1342 |
public static <X> X[] readArray(Class<X> cx, String fname, boolean allowSubprocess){ |
| 1343 |
X[] x=(X[])readObject(fname, allowSubprocess); |
| 1344 |
return x; |
| 1345 |
} |
| 1346 |
|
| 1347 |
public static <X> X[][] readArray2(Class<X> cx, String fname, boolean allowSubprocess){ |
| 1348 |
X[][] x=(X[][])readObject(fname, allowSubprocess); |
| 1349 |
return x; |
| 1350 |
} |
| 1351 |
|
| 1352 |
public static <X> X[][][] readArray3(Class<X> cx, String fname, boolean allowSubprocess){ |
| 1353 |
X[][][] x=(X[][][])readObject(fname, allowSubprocess); |
| 1354 |
return x; |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
public static String basename(String fname){ |
| 1359 |
fname=fname.replace('\\', '/'); |
| 1360 |
boolean xz=fname.endsWith(".xz"); |
| 1361 |
boolean gzipped=fname.endsWith(".gz"); |
| 1362 |
boolean zipped=fname.endsWith(".zip"); |
| 1363 |
boolean bzipped=PROCESS_BZ2 && fname.endsWith(".bz2"); |
| 1364 |
boolean dsrced=fname.endsWith(".dsrc"); |
| 1365 |
String basename=fname; |
| 1366 |
|
| 1367 |
if(basename.contains("/")){basename=basename.substring(basename.lastIndexOf('/')+1);} |
| 1368 |
if(zipped || bzipped){basename=basename.substring(0, basename.length()-4);} |
| 1369 |
else if(gzipped){basename=basename.substring(0, basename.length()-3);} |
| 1370 |
else if(dsrced){basename=basename.substring(0, basename.length()-5);} |
| 1371 |
return basename; |
| 1372 |
} |
| 1373 |
|
| 1374 |
public static String rawName(String fname){ |
| 1375 |
for(String s : compressedExtensions){ |
| 1376 |
while(fname.endsWith(s)){fname=fname.substring(0, fname.length()-s.length());} |
| 1377 |
} |
| 1378 |
return fname; |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
public static String stripExtension(String fname){ |
| 1385 |
if(fname==null){return null;} |
| 1386 |
for(String ext : FileFormat.EXTENSION_LIST){ |
| 1387 |
String s="."+ext; |
| 1388 |
if(fname.endsWith(s)){return stripExtension(fname.substring(0, fname.length()-s.length()));} |
| 1389 |
} |
| 1390 |
return fname; |
| 1391 |
} |
| 1392 |
|
| 1393 |
|
| 1394 |
public static String getExtension(String fname){ |
| 1395 |
if(fname==null){return null;} |
| 1396 |
String stripped=stripExtension(fname); |
| 1397 |
if(stripped==null){return fname;} |
| 1398 |
if(stripped.length()==fname.length()){return "";} |
| 1399 |
return fname.substring(stripped.length()); |
| 1400 |
} |
| 1401 |
|
| 1402 |
public static String stripToCore(String fname){ |
| 1403 |
fname=stripPath(fname); |
| 1404 |
return stripExtension(fname); |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
public static String stripPath(String fname){ |
| 1413 |
if(fname==null){return null;} |
| 1414 |
fname=fname.replace('\\', '/'); |
| 1415 |
int idx=fname.lastIndexOf('/'); |
| 1416 |
if(idx>=0){fname=fname.substring(idx+1);} |
| 1417 |
return fname; |
| 1418 |
} |
| 1419 |
|
| 1420 |
public static String getPath(String fname){ |
| 1421 |
if(fname==null){return null;} |
| 1422 |
fname=fname.replace('\\', '/'); |
| 1423 |
int idx=fname.lastIndexOf('/'); |
| 1424 |
if(idx>=0){return fname.substring(0, idx+1);} |
| 1425 |
return ""; |
| 1426 |
} |
| 1427 |
|
| 1428 |
public static String compressionType(String fname){ |
| 1429 |
fname=fname.toLowerCase(Locale.ENGLISH); |
| 1430 |
for(int i=0; i<compressedExtensions.length; i++){ |
| 1431 |
if(fname.endsWith(compressedExtensions[i])){return compressedExtensionMap[i];} |
| 1432 |
} |
| 1433 |
return null; |
| 1434 |
} |
| 1435 |
|
| 1436 |
public static boolean isCompressed(String fname){ |
| 1437 |
return compressionType(fname)!=null; |
| 1438 |
} |
| 1439 |
|
| 1440 |
public static boolean isSam(String fname){ |
| 1441 |
fname=fname.toLowerCase(Locale.ENGLISH); |
| 1442 |
if(fname.endsWith(".sam")){return true;} |
| 1443 |
String s=compressionType(fname); |
| 1444 |
if(s==null){return false;} |
| 1445 |
return fname.substring(0, fname.lastIndexOf('.')).endsWith(".sam"); |
| 1446 |
} |
| 1447 |
|
| 1448 |
|
| 1449 |
public static String rawExtension(String fname){ |
| 1450 |
fname=rawName(fname); |
| 1451 |
int x=fname.lastIndexOf('.'); |
| 1452 |
|
| 1453 |
return fname.substring(x+1).toLowerCase(Locale.ENGLISH); |
| 1454 |
} |
| 1455 |
|
| 1456 |
public static String parseRoot(String path){ |
| 1457 |
File f=new File(path); |
| 1458 |
if(f.isDirectory()){ |
| 1459 |
if(!path.endsWith(FILESEP)){ |
| 1460 |
path=path+FILESEP; |
| 1461 |
} |
| 1462 |
return path; |
| 1463 |
}else if(f.isFile()){ |
| 1464 |
int slash=path.lastIndexOf(FILESEP); |
| 1465 |
if(slash<0){ |
| 1466 |
return ""; |
| 1467 |
}else{ |
| 1468 |
return path.substring(0, slash+1); |
| 1469 |
} |
| 1470 |
}else{ |
| 1471 |
throw new RuntimeException("Can't find "+path); |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
public static String parseRoot2(String path){ |
| 1477 |
File f=new File(path); |
| 1478 |
|
| 1479 |
if(!f.exists()){ |
| 1480 |
if(path.endsWith(FILESEP)){return path;} |
| 1481 |
int slash=path.lastIndexOf(FILESEP); |
| 1482 |
if(slash<0){ |
| 1483 |
return ""; |
| 1484 |
}else{ |
| 1485 |
return path.substring(0, slash+1); |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
if(f.isDirectory()){ |
| 1490 |
if(!path.endsWith(FILESEP)){ |
| 1491 |
path=path+FILESEP; |
| 1492 |
} |
| 1493 |
return path; |
| 1494 |
}else if(f.isFile()){ |
| 1495 |
int slash=path.lastIndexOf(FILESEP); |
| 1496 |
if(slash<0){ |
| 1497 |
return ""; |
| 1498 |
}else{ |
| 1499 |
return path.substring(0, slash+1); |
| 1500 |
} |
| 1501 |
}else{ |
| 1502 |
throw new RuntimeException("Can't find "+path); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
public static String findFileExtension(final String fname){ |
| 1507 |
|
| 1508 |
File file=new File(fname); |
| 1509 |
if(file.exists()){return fname;} |
| 1510 |
|
| 1511 |
String basename=fname, temp; |
| 1512 |
if(fname.endsWith(".zip") || fname.endsWith(".gz") || (PROCESS_BZ2 && fname.endsWith(".bz2")) || (PROCESS_XZ && fname.endsWith(".xz"))){ |
| 1513 |
basename=fname.substring(0, fname.lastIndexOf('.')); |
| 1514 |
} |
| 1515 |
temp=basename; |
| 1516 |
file=new File(temp); |
| 1517 |
if(!file.exists()){ |
| 1518 |
temp=basename+".gz"; |
| 1519 |
file=new File(temp); |
| 1520 |
} |
| 1521 |
|
| 1522 |
if(!file.exists()){ |
| 1523 |
temp=basename+".zip"; |
| 1524 |
file=new File(temp); |
| 1525 |
} |
| 1526 |
|
| 1527 |
if(!file.exists() && PROCESS_BZ2){ |
| 1528 |
temp=basename+".bz2"; |
| 1529 |
file=new File(temp); |
| 1530 |
} |
| 1531 |
|
| 1532 |
if(!file.exists() && PROCESS_XZ){ |
| 1533 |
temp=basename+".xz"; |
| 1534 |
file=new File(temp); |
| 1535 |
} |
| 1536 |
|
| 1537 |
if(!file.exists()){temp=fname;} |
| 1538 |
|
| 1539 |
return temp; |
| 1540 |
} |
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
public static boolean delete(String path, boolean verbose){ |
| 1546 |
if(path==null){return false;} |
| 1547 |
if(verbose){System.err.println("Trying to delete "+path);} |
| 1548 |
File f=new File(path); |
| 1549 |
if(f.exists()){ |
| 1550 |
try { |
| 1551 |
f.delete(); |
| 1552 |
return true; |
| 1553 |
} catch (Exception e) { |
| 1554 |
|
| 1555 |
e.printStackTrace(); |
| 1556 |
} |
| 1557 |
} |
| 1558 |
return false; |
| 1559 |
} |
| 1560 |
|
| 1561 |
public static synchronized void copyFile(String source, String dest){copyFile(source, dest, false);} |
| 1562 |
public static synchronized void copyFile(String source, String dest, boolean createPathIfNeeded){ |
| 1563 |
|
| 1564 |
assert(!new File(dest).exists()) : "Destination file already exists: "+dest; |
| 1565 |
if(createPathIfNeeded){ |
| 1566 |
File parent=new File(dest).getParentFile(); |
| 1567 |
if(parent!=null && !parent.exists()){ |
| 1568 |
parent.mkdirs(); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
final boolean oldRawmode=RAWMODE; |
| 1573 |
if((source.endsWith(".zip") && dest.endsWith(".zip")) |
| 1574 |
|| (source.endsWith(".gz") && dest.endsWith(".gz") |
| 1575 |
|| (source.endsWith(".bz2") && dest.endsWith(".bz2")) |
| 1576 |
|| (source.endsWith(".xz") && dest.endsWith(".xz")))){ |
| 1577 |
RAWMODE=true; |
| 1578 |
} |
| 1579 |
|
| 1580 |
try{ |
| 1581 |
InputStream in=getInputStream(source, false, false); |
| 1582 |
OutputStream out=getOutputStream(dest, false, false, true); |
| 1583 |
|
| 1584 |
byte[] buffer=new byte[INBUF]; |
| 1585 |
int len; |
| 1586 |
|
| 1587 |
while((len = in.read(buffer)) > 0){ |
| 1588 |
out.write(buffer, 0, len); |
| 1589 |
} |
| 1590 |
|
| 1591 |
in.close(); |
| 1592 |
out.flush(); |
| 1593 |
if(out.getClass()==ZipOutputStream.class){ |
| 1594 |
ZipOutputStream zos=(ZipOutputStream)out; |
| 1595 |
zos.closeEntry(); |
| 1596 |
zos.finish(); |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
out.close(); |
| 1603 |
|
| 1604 |
}catch(FileNotFoundException e){ |
| 1605 |
RAWMODE=oldRawmode; |
| 1606 |
throw new RuntimeException(e); |
| 1607 |
}catch(IOException e){ |
| 1608 |
RAWMODE=oldRawmode; |
| 1609 |
throw new RuntimeException(e); |
| 1610 |
} |
| 1611 |
|
| 1612 |
RAWMODE=oldRawmode; |
| 1613 |
} |
| 1614 |
|
| 1615 |
public static void copyDirectoryContents(String from, String to){ |
| 1616 |
assert(!from.equalsIgnoreCase(to)); |
| 1617 |
|
| 1618 |
if(to.indexOf('\\')>0){to=to.replace('\\', '/');} |
| 1619 |
|
| 1620 |
File d1=new File(from); |
| 1621 |
assert(d1.exists()); |
| 1622 |
assert(d1.isDirectory()); |
| 1623 |
|
| 1624 |
File d2=new File(to); |
| 1625 |
assert(!d1.equals(d2)); |
| 1626 |
if(d2.exists()){ |
| 1627 |
assert(d2.isDirectory()); |
| 1628 |
}else{ |
| 1629 |
d2.mkdirs(); |
| 1630 |
} |
| 1631 |
if(!to.endsWith("/")){to=to+"/";} |
| 1632 |
|
| 1633 |
File[] array=d1.listFiles(); |
| 1634 |
|
| 1635 |
for(File f : array){ |
| 1636 |
String name=f.getName(); |
| 1637 |
String dest=to+name; |
| 1638 |
if(f.isFile()){ |
| 1639 |
copyFile(f.getAbsolutePath(), dest); |
| 1640 |
}else{ |
| 1641 |
assert(f.isDirectory()); |
| 1642 |
File f2=new File(dest); |
| 1643 |
if(!f2.exists()){ |
| 1644 |
f2.mkdir(); |
| 1645 |
}else{ |
| 1646 |
assert(f2.isDirectory()); |
| 1647 |
} |
| 1648 |
copyDirectoryContents(f.getAbsolutePath(), f2.getAbsolutePath()); |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
static final int addThread(int x){ |
| 1656 |
if(verbose){System.err.println("addThread("+x+")");} |
| 1657 |
synchronized(activeThreads){ |
| 1658 |
assert(x!=0); |
| 1659 |
if(x>0){ |
| 1660 |
activeThreads[0]+=x; |
| 1661 |
activeThreads[1]+=x; |
| 1662 |
}else{ |
| 1663 |
addRunningThread(x); |
| 1664 |
} |
| 1665 |
assert(activeThreads[0]==(activeThreads[1]+activeThreads[2]) && activeThreads[0]>=0 && activeThreads[1]>=0 && |
| 1666 |
activeThreads[2]>=0 && activeThreads[2]<=maxWriteThreads) : Arrays.toString(activeThreads); |
| 1667 |
|
| 1668 |
return activeThreads[0]; |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
static final int addRunningThread(int x){ |
| 1673 |
if(verbose){System.err.println("addRunningThread("+x+")");} |
| 1674 |
final int max=(Shared.LOW_MEMORY ? 1 : maxWriteThreads); |
| 1675 |
synchronized(activeThreads){ |
| 1676 |
assert(x!=0); |
| 1677 |
if(x>0){ |
| 1678 |
assert(activeThreads[1]>=x); |
| 1679 |
while(activeThreads[2]>=max){ |
| 1680 |
try { |
| 1681 |
activeThreads.wait(); |
| 1682 |
} catch (InterruptedException e) { |
| 1683 |
|
| 1684 |
e.printStackTrace(); |
| 1685 |
} |
| 1686 |
} |
| 1687 |
activeThreads[1]-=x; |
| 1688 |
}else{ |
| 1689 |
activeThreads[0]+=x; |
| 1690 |
} |
| 1691 |
activeThreads[2]+=x; |
| 1692 |
|
| 1693 |
assert(activeThreads[0]==(activeThreads[1]+activeThreads[2]) && activeThreads[0]>=0 && activeThreads[1]>=0 && |
| 1694 |
activeThreads[2]>=0 && activeThreads[2]<=max) : Arrays.toString(activeThreads); |
| 1695 |
|
| 1696 |
if(activeThreads[2]==0 || (activeThreads[2]<max && activeThreads[1]>0)){activeThreads.notify();} |
| 1697 |
return activeThreads[2]; |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
public static final int countActiveThreads(){ |
| 1702 |
if(verbose){System.err.println("countActiveThreads()");} |
| 1703 |
synchronized(activeThreads){ |
| 1704 |
assert(activeThreads[0]==(activeThreads[1]+activeThreads[2]) && activeThreads[0]>=0 && activeThreads[1]>=0 && |
| 1705 |
activeThreads[2]>=0 && activeThreads[2]<=maxWriteThreads) : Arrays.toString(activeThreads); |
| 1706 |
return activeThreads[0]; |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
public static final void waitForWritingToFinish(){ |
| 1711 |
if(verbose){System.err.println("waitForWritingToFinish()");} |
| 1712 |
synchronized(activeThreads){ |
| 1713 |
while(activeThreads[0]>0){ |
| 1714 |
assert(activeThreads[0]==(activeThreads[1]+activeThreads[2]) && activeThreads[0]>=0 && activeThreads[1]>=0 && |
| 1715 |
activeThreads[2]>=0 && activeThreads[2]<=maxWriteThreads) : Arrays.toString(activeThreads); |
| 1716 |
try { |
| 1717 |
activeThreads.wait(8000); |
| 1718 |
} catch (InterruptedException e) { |
| 1719 |
|
| 1720 |
e.printStackTrace(); |
| 1721 |
} |
| 1722 |
if(activeThreads[2]==0 || (activeThreads[2]<maxWriteThreads && activeThreads[1]>0)){activeThreads.notify();} |
| 1723 |
} |
| 1724 |
} |
| 1725 |
} |
| 1726 |
|
| 1727 |
|
| 1728 |
public static final boolean closeStream(ConcurrentReadStreamInterface cris){return closeStreams(cris, (ConcurrentReadOutputStream[])null);} |
| 1729 |
public static final boolean closeStream(ConcurrentReadOutputStream ross){return closeStreams((ConcurrentReadStreamInterface)null, ross);} |
| 1730 |
public static final boolean closeOutputStreams(ConcurrentReadOutputStream...ross){return closeStreams(null, ross);} |
| 1731 |
|
| 1732 |
public static final boolean closeStreams(MultiCros mc){ |
| 1733 |
if(mc==null){return false;} |
| 1734 |
return closeStreams(null, mc.streamList.toArray(new ConcurrentReadOutputStream[0])); |
| 1735 |
} |
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
public static final boolean closeStreams(ConcurrentReadStreamInterface cris, ConcurrentReadOutputStream...ross){ |
| 1744 |
if(verbose){ |
| 1745 |
System.err.println("closeStreams("+cris+", "+(ross==null ? "null" : ross.length)+")"); |
| 1746 |
new Exception().printStackTrace(System.err); |
| 1747 |
} |
| 1748 |
boolean errorState=false; |
| 1749 |
if(cris!=null){ |
| 1750 |
if(verbose){System.err.println("Closing cris; error="+errorState);} |
| 1751 |
cris.close(); |
| 1752 |
errorState|=cris.errorState(); |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
if(verbose){System.err.println("Closed cris; error="+errorState);} |
| 1761 |
} |
| 1762 |
if(ross!=null){ |
| 1763 |
for(ConcurrentReadOutputStream ros : ross){ |
| 1764 |
if(ros!=null){ |
| 1765 |
if(verbose){System.err.println("Closing ros "+ros+"; error="+errorState);} |
| 1766 |
ros.close(); |
| 1767 |
ros.join(); |
| 1768 |
errorState|=(ros.errorState() || !ros.finishedSuccessfully()); |
| 1769 |
if(verbose){System.err.println("Closed ros; error="+errorState);} |
| 1770 |
} |
| 1771 |
} |
| 1772 |
} |
| 1773 |
return errorState; |
| 1774 |
} |
| 1775 |
|
| 1776 |
public static boolean killProcess(String fname){ |
| 1777 |
if(verbose){ |
| 1778 |
System.err.println("killProcess("+fname+")"); |
| 1779 |
new Exception().printStackTrace(System.err); |
| 1780 |
System.err.println("processMap before: "+processMap.keySet()); |
| 1781 |
} |
| 1782 |
if(fname==null || (!isCompressed(fname) && !fname.endsWith(".bam") && !FORCE_KILL)){return false;} |
| 1783 |
|
| 1784 |
boolean error=false; |
| 1785 |
synchronized(processMap){ |
| 1786 |
Process p=processMap.remove(fname); |
| 1787 |
if(p!=null){ |
| 1788 |
if(verbose){System.err.println("Found Process for "+fname);} |
| 1789 |
int x=-1, tries=0; |
| 1790 |
for(; tries<20; tries++){ |
| 1791 |
if(verbose){System.err.println("Trying p.waitFor()");} |
| 1792 |
try { |
| 1793 |
|
| 1794 |
|
| 1795 |
if(verbose){System.err.println("p.isAlive()="+p.isAlive());} |
| 1796 |
x=p.waitFor(); |
| 1797 |
|
| 1798 |
if(verbose){System.err.println("success; return="+x);} |
| 1799 |
break; |
| 1800 |
} catch (InterruptedException e) { |
| 1801 |
if(verbose){System.err.println("Failed.");} |
| 1802 |
e.printStackTrace(); |
| 1803 |
} |
| 1804 |
} |
| 1805 |
error|=(tries>=20 || (x!=0 && x!=141)); |
| 1806 |
if(verbose){System.err.println("killProcess("+fname+") returned "+error+"; tries="+tries+", code="+x);} |
| 1807 |
if(tries>=20){ |
| 1808 |
if(verbose){System.err.println("Calling p.destroy because tries=="+tries+"; error="+error);} |
| 1809 |
p.destroy(); |
| 1810 |
if(verbose){System.err.println("destroyed");} |
| 1811 |
} |
| 1812 |
}else{ |
| 1813 |
if(verbose){System.err.println("WARNING: Could not find process for "+fname);} |
| 1814 |
} |
| 1815 |
if(verbose){ |
| 1816 |
System.err.println("processMap after: "+processMap.keySet()); |
| 1817 |
} |
| 1818 |
} |
| 1819 |
synchronized(pipeThreadMap){ |
| 1820 |
if(verbose){System.err.println("pipeMap before: "+processMap.keySet());} |
| 1821 |
ArrayList<PipeThread> atp=pipeThreadMap.remove(fname); |
| 1822 |
if(atp!=null){ |
| 1823 |
for(PipeThread p : atp){ |
| 1824 |
if(p!=null){ |
| 1825 |
if(verbose){System.err.println("Found PipeThread for "+fname);} |
| 1826 |
p.terminate(); |
| 1827 |
if(verbose){System.err.println("Terminated PipeThread");} |
| 1828 |
}else{ |
| 1829 |
if(verbose){System.err.println("WARNING: Could not find process for "+fname);} |
| 1830 |
} |
| 1831 |
} |
| 1832 |
} |
| 1833 |
if(verbose){System.err.println("pipeMap after: "+processMap.keySet());} |
| 1834 |
} |
| 1835 |
if(verbose){System.err.println("killProcess("+fname+") returned "+error);} |
| 1836 |
return error; |
| 1837 |
} |
| 1838 |
|
| 1839 |
private static void addProcess(String fname, Process p){ |
| 1840 |
if(verbose){ |
| 1841 |
System.err.println("addProcess("+fname+", "+p+")"); |
| 1842 |
new Exception().printStackTrace(); |
| 1843 |
} |
| 1844 |
synchronized(processMap){ |
| 1845 |
Process old=processMap.put(fname, p); |
| 1846 |
if(old!=null){ |
| 1847 |
old.destroy(); |
| 1848 |
|
| 1849 |
KillSwitch.kill("Duplicate process for file "+fname); |
| 1850 |
} |
| 1851 |
} |
| 1852 |
} |
| 1853 |
|
| 1854 |
private static void addPipeThread(String fname, PipeThread pt){ |
| 1855 |
if(verbose){System.err.println("addPipeThread("+fname+", "+pt+")");} |
| 1856 |
synchronized(pipeThreadMap){ |
| 1857 |
|
| 1858 |
ArrayList<PipeThread> atp=pipeThreadMap.get(fname); |
| 1859 |
if(atp==null){ |
| 1860 |
atp=new ArrayList<PipeThread>(2); |
| 1861 |
pipeThreadMap.put(fname, atp); |
| 1862 |
} |
| 1863 |
atp.add(pt); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
public static int getMagicNumber(String fname) { |
| 1882 |
InputStream is=null; |
| 1883 |
try { |
| 1884 |
FileInputStream fis=new FileInputStream(fname); |
| 1885 |
is=new BufferedInputStream(fis); |
| 1886 |
} catch (FileNotFoundException e) { |
| 1887 |
|
| 1888 |
e.printStackTrace(); |
| 1889 |
} |
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
int x=0; |
| 1909 |
for(int i=0; i<4; i++){ |
| 1910 |
try { |
| 1911 |
x=(x<<8)|(is.read()&255); |
| 1912 |
} catch (IOException e) { |
| 1913 |
|
| 1914 |
e.printStackTrace(); |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
return x; |
| 1919 |
} |
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
public static int[] activeThreads={0, 0, 0}; |
| 1925 |
public static int maxWriteThreads=Shared.threads(); |
| 1926 |
|
| 1927 |
public static boolean verbose=false; |
| 1928 |
|
| 1929 |
public static boolean RAWMODE=false; |
| 1930 |
|
| 1931 |
|
| 1932 |
public static boolean FORCE_KILL=false; |
| 1933 |
|
| 1934 |
public static boolean USE_GZIP=false; |
| 1935 |
public static boolean USE_BGZIP=true; |
| 1936 |
public static boolean USE_PIGZ=true; |
| 1937 |
public static boolean USE_GUNZIP=false; |
| 1938 |
public static boolean USE_UNBGZIP=true; |
| 1939 |
public static boolean USE_UNPIGZ=true; |
| 1940 |
|
| 1941 |
public static boolean FORCE_PIGZ=false; |
| 1942 |
public static boolean FORCE_BGZIP=false; |
| 1943 |
|
| 1944 |
public static boolean PREFER_BGZIP=true; |
| 1945 |
public static boolean PREFER_UNBGZIP=true; |
| 1946 |
|
| 1947 |
public static boolean USE_BZIP2=true; |
| 1948 |
public static boolean USE_PBZIP2=true; |
| 1949 |
public static boolean USE_LBZIP2=true; |
| 1950 |
public static boolean USE_DSRC=true; |
| 1951 |
public static boolean USE_FQZ=true; |
| 1952 |
public static boolean USE_ALAPY=true; |
| 1953 |
public static boolean USE_SAMBAMBA=true; |
| 1954 |
public static boolean SAMBAMBA(){return USE_SAMBAMBA && Data.SAMBAMBA();} |
| 1955 |
|
| 1956 |
|
| 1957 |
public static int SAMTOOLS_IGNORE_FLAG=0; |
| 1958 |
public static final int SAM_UNMAPPED=0x4; |
| 1959 |
public static final int SAM_DUPLICATE=0x400; |
| 1960 |
public static final int SAM_SUPPLIMENTARY=0x800; |
| 1961 |
public static final int SAM_SECONDARY=0x100; |
| 1962 |
public static final int SAM_QFAIL=0x200; |
| 1963 |
|
| 1964 |
public static boolean PROCESS_BZ2=true; |
| 1965 |
public static final boolean PROCESS_XZ=false; |
| 1966 |
|
| 1967 |
public static final int INBUF=16384; |
| 1968 |
public static final int OUTBUF=16384; |
| 1969 |
|
| 1970 |
|
| 1971 |
public static int ZIPLEVEL=4; |
| 1972 |
|
| 1973 |
public static int BZIPLEVEL=9; |
| 1974 |
private static int MAX_ZIP_THREADS=96; |
| 1975 |
public static int MAX_SAMTOOLS_THREADS=64; |
| 1976 |
public static int PIGZ_BLOCKSIZE=128; |
| 1977 |
public static int PIGZ_ITERATIONS=-1; |
| 1978 |
|
| 1979 |
public static boolean SET_ZIP_THREAD_MULT=false; |
| 1980 |
public static boolean SET_ZIP_THREADS=false; |
| 1981 |
|
| 1982 |
public static int MAX_ZIP_THREADS() {return MAX_ZIP_THREADS;} |
| 1983 |
public static void setZipThreads(int x){ |
| 1984 |
MAX_ZIP_THREADS=Tools.mid(1, x, 96); |
| 1985 |
SET_ZIP_THREADS=true; |
| 1986 |
} |
| 1987 |
public static float ZIP_THREAD_MULT() {return ZIP_THREAD_MULT;} |
| 1988 |
public static void setZipThreadMult(float x){ |
| 1989 |
ZIP_THREAD_MULT=Tools.mid(0.125f, x, 1f); |
| 1990 |
SET_ZIP_THREAD_MULT=true; |
| 1991 |
} |
| 1992 |
private static float ZIP_THREAD_MULT=1f; |
| 1993 |
public static boolean ALLOW_ZIPLEVEL_CHANGE=true; |
| 1994 |
|
| 1995 |
public static final String FILESEP=System.getProperty("file.separator"); |
| 1996 |
|
| 1997 |
private static final String diskSync=new String("DISKSYNC"); |
| 1998 |
|
| 1999 |
public static final HashSet<String> loadedFiles=new HashSet<String>(); |
| 2000 |
|
| 2001 |
private static final String[] compressedExtensions=new String[] {".gz", ".gzip", ".zip", ".bz2", ".xz", ".dsrc", ".fqz", ".ac", ".7z", ".zst"}; |
| 2002 |
private static final String[] compressedExtensionMap=new String[] {"gz", "gz", "zip", "bz2", "xz", "dsrc", "fqz", "ac", "7z", "zst"}; |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
private static HashMap<String, Process> processMap=new HashMap<String, Process>(8); |
| 2007 |
private static HashMap<String, ArrayList<PipeThread>> pipeThreadMap=new HashMap<String, ArrayList<PipeThread>>(8); |
| 2008 |
|
| 2009 |
} |