Search in sources :

Example 1 with HtsjdkVersion

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);
    }
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) DataInputStream(java.io.DataInputStream) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Arrays(java.util.Arrays) LineIterator(htsjdk.tribble.readers.LineIterator) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) VCFHeader(htsjdk.variant.vcf.VCFHeader) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) ParametersDelegate(com.beust.jcommander.ParametersDelegate) BigDecimal(java.math.BigDecimal) DataOutputStream(java.io.DataOutputStream) AbstractDataCodec(com.github.lindenb.jvarkit.util.picard.AbstractDataCodec) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) BigInteger(java.math.BigInteger) CloserUtil(htsjdk.samtools.util.CloserUtil) SortingCollection(htsjdk.samtools.util.SortingCollection) AbstractVCFCodec(htsjdk.variant.vcf.AbstractVCFCodec) Logger(com.github.lindenb.jvarkit.util.log.Logger) IOException(java.io.IOException) File(java.io.File) List(java.util.List) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) VariantContext(htsjdk.variant.variantcontext.VariantContext) HtsjdkVersion(com.github.lindenb.jvarkit.util.htsjdk.HtsjdkVersion) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) LineIterator(htsjdk.tribble.readers.LineIterator) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) IOException(java.io.IOException) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader)

Aggregations

Parameter (com.beust.jcommander.Parameter)1 ParametersDelegate (com.beust.jcommander.ParametersDelegate)1 IOUtils (com.github.lindenb.jvarkit.io.IOUtils)1 HtsjdkVersion (com.github.lindenb.jvarkit.util.htsjdk.HtsjdkVersion)1 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)1 Program (com.github.lindenb.jvarkit.util.jcommander.Program)1 Logger (com.github.lindenb.jvarkit.util.log.Logger)1 AbstractDataCodec (com.github.lindenb.jvarkit.util.picard.AbstractDataCodec)1 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)1 VCFUtils (com.github.lindenb.jvarkit.util.vcf.VCFUtils)1 CloseableIterator (htsjdk.samtools.util.CloseableIterator)1 CloserUtil (htsjdk.samtools.util.CloserUtil)1 SortingCollection (htsjdk.samtools.util.SortingCollection)1 LineIterator (htsjdk.tribble.readers.LineIterator)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)1 AbstractVCFCodec (htsjdk.variant.vcf.AbstractVCFCodec)1 VCFHeader (htsjdk.variant.vcf.VCFHeader)1 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)1 VCFInfoHeaderLine (htsjdk.variant.vcf.VCFInfoHeaderLine)1