use of com.github.lindenb.jvarkit.util.htsjdk.HtsjdkVersion in project jvarkit by lindenb.
the class SortVcfOnInfo method doWork.
@Override
public int doWork(final List<String> args) {
CloseableIterator<VcfLine> iter = null;
VariantContextWriter w = null;
SortingCollection<VcfLine> sorted = null;
LineIterator r = null;
try {
if (args.isEmpty()) {
LOG.info("reading from stdin");
r = IOUtils.openStreamForLineIterator(stdin());
} else if (args.size() == 1) {
String filename = args.get(0);
LOG.info("Reading " + filename);
r = IOUtils.openURIForLineIterator(filename);
} else {
LOG.error("Illegal number of arguments.");
return -1;
}
final VCFUtils.CodecAndHeader ch = VCFUtils.parseHeader(r);
VCFHeader header = ch.header;
this.codec = ch.codec;
this.infoDecl = header.getInfoHeaderLine(this.infoField);
if (this.infoDecl == null) {
final StringBuilder msg = new StringBuilder("VCF doesn't contain the INFO field :" + infoField + ". Available:");
for (VCFInfoHeaderLine vil : header.getInfoHeaderLines()) msg.append(" ").append(vil.getID());
LOG.error(msg.toString());
return -1;
}
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkVersion", HtsjdkVersion.getVersion()));
header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkHome", HtsjdkVersion.getHome()));
w = super.openVariantContextWriter(this.outputFile);
w.writeHeader(header);
sorted = SortingCollection.newInstance(VcfLine.class, new VariantCodec(), (V1, V2) -> V1.compareTo(V2), this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
sorted.setDestructiveIteration(true);
while (r.hasNext()) {
final VcfLine vc = new VcfLine(r.next());
progress.watch(vc.getContext());
sorted.add(vc);
}
CloserUtil.close(r);
r = null;
sorted.doneAdding();
progress.finish();
LOG.info("now writing...");
iter = sorted.iterator();
while (iter.hasNext()) {
w.add(iter.next().getContext());
}
iter.close();
iter = null;
w.close();
w = null;
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(r);
CloserUtil.close(iter);
try {
if (sorted != null)
sorted.cleanup();
} catch (Exception err) {
}
CloserUtil.close(w);
}
}
Aggregations