use of com.github.lindenb.jvarkit.io.NoCloseInputStream in project jvarkit by lindenb.
the class VcfToBed method doWork.
@Override
public int doWork(final List<String> args) {
if (!StringUtil.isBlank(this.maxLengthStr)) {
this.maxLength = new DistanceParser.StringConverter().convert(this.maxLengthStr);
}
if (!StringUtil.isBlank(this.minLengthStr)) {
this.minLength = new DistanceParser.StringConverter().convert(this.minLengthStr);
}
PrintWriter pw = null;
try {
if (this.faidx != null) {
this.samSequenceDictionary = SequenceDictionaryUtils.extractRequired(this.faidx);
}
pw = super.openPathOrStdoutAsPrintWriter(this.outputFile);
if (printHeader) {
switch(this.outputFormat) {
case bed:
{
pw.println("track name=vcf2bed type=bed description=\"__DESCRIPTION__\"");
break;
}
case interval:
{
if (this.samSequenceDictionary != null) {
final SAMTextHeaderCodec codec = new SAMTextHeaderCodec();
codec.encode(pw, new SAMFileHeader(this.samSequenceDictionary));
}
break;
}
default:
{
throw new IllegalStateException("" + this.outputFormat);
}
}
}
if (args.size() == 1 && args.get(0).endsWith(".list")) {
final PrintWriter finalpw = pw;
Files.lines(Paths.get(args.get(0))).filter(L -> !StringUtil.isBlank(L)).forEach(L -> {
scan(L, finalpw);
});
} else if (args.size() == 1 && args.get(0).endsWith(".zip")) {
try (InputStream in = new BufferedInputStream(Files.newInputStream(Paths.get(args.get(0))))) {
ZipInputStream zin = new ZipInputStream(in);
for (; ; ) {
final ZipEntry entry = zin.getNextEntry();
if (entry == null)
break;
if (entry.isDirectory()) {
// zin.closeEntry();
continue;
}
if (!FileExtensions.VCF_LIST.stream().anyMatch(X -> entry.getName().endsWith(X))) {
// zin.closeEntry();
continue;
}
/* prevent zip from being closed */
final InputStream do_not_close_in = new NoCloseInputStream(zin);
try (VCFIterator iter = new VCFIteratorBuilder().open(do_not_close_in)) {
scan(args.get(0) + "!" + entry.getName(), iter, pw);
}
// zin.closeEntry();
}
zin.close();
}
} else if (args.size() == 1 && (args.get(0).endsWith(".tar") || args.get(0).endsWith(".tar.gz"))) {
try (InputStream in = new BufferedInputStream(Files.newInputStream(Paths.get(args.get(0))))) {
InputStream in2 = args.get(0).endsWith(".tar") ? in : new GZIPInputStream(in);
final TarArchiveInputStream tarin = new TarArchiveInputStream(in2);
for (; ; ) {
final TarArchiveEntry entry = tarin.getNextTarEntry();
if (entry == null)
break;
if (!tarin.canReadEntryData(entry))
continue;
if (entry.isDirectory()) {
continue;
}
if (!FileExtensions.VCF_LIST.stream().anyMatch(X -> entry.getName().endsWith(X))) {
continue;
}
/* prevent tar from being closed */
final InputStream do_not_close_in = new NoCloseInputStream(tarin);
try (VCFIterator iter = new VCFIteratorBuilder().open(do_not_close_in)) {
scan(args.get(0) + "!" + entry.getName(), iter, pw);
}
}
tarin.close();
in2.close();
}
} else if (args.isEmpty()) {
LOG.info("reading vcf from stdin");
scan(null, pw);
} else {
final PrintWriter finalpw = pw;
args.stream().forEach(L -> {
scan(L, finalpw);
});
}
if (!this.contigsNotFound.isEmpty()) {
LOG.warn("The following contigs: " + String.join(",", this.contigsNotFound) + " were not found in the dictionaries.");
}
pw.flush();
pw.close();
pw = null;
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(pw);
}
}
Aggregations