Search in sources :

Example 66 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfIndexTabix method doVcfToVcf.

private int doVcfToVcf(String inputName, final VcfIterator vcfIn, final File outFile) throws IOException {
    SortingVCFWriter sortingVCW = null;
    VariantContextWriterBuilder vcwb = new VariantContextWriterBuilder();
    VariantContextWriter w = null;
    try {
        SAMSequenceDictionary dict = vcfIn.getHeader().getSequenceDictionary();
        if (dict != null)
            vcwb.setReferenceDictionary(dict);
        vcwb.setOutputFile(outFile);
        vcwb.setOutputFileType(OutputType.BLOCK_COMPRESSED_VCF);
        w = vcwb.build();
        if (this.sort) {
            LOG.info("Creating a sorting writer");
            sortingVCW = new SortingVCFWriter(w);
            w = sortingVCW;
        }
        w.writeHeader(vcfIn.getHeader());
        SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(vcfIn.getHeader());
        while (vcfIn.hasNext()) {
            w.add(progress.watch(vcfIn.next()));
        }
        progress.finish();
        w.close();
        w = null;
        return RETURN_OK;
    } catch (Exception e) {
        if (outFile.exists() && outFile.isFile()) {
            LOG.warn("Deleting " + outFile);
            outFile.delete();
            File tbi = new File(outFile.getPath() + TabixUtils.STANDARD_INDEX_EXTENSION);
            if (tbi.exists() && tbi.isFile())
                tbi.delete();
        }
        LOG.error(e);
        return -1;
    } finally {
        CloserUtil.close(w);
    }
}
Also used : VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) File(java.io.File) IOException(java.io.IOException)

Example 67 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfMultiToOneAllele method doVcfToVcf.

@Override
public int doVcfToVcf(final String inputName, final VcfIterator r, final VariantContextWriter delegate) {
    final VariantContextWriter w = this.component.open(delegate);
    w.writeHeader(r.getHeader());
    final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(r.getHeader()).logger(LOG);
    while (r.hasNext()) {
        w.add(progress.watch(r.next()));
    }
    progress.finish();
    w.close();
    return 0;
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) DelegateVariantContextWriter(com.github.lindenb.jvarkit.util.vcf.DelegateVariantContextWriter) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) PostponedVariantContextWriter(com.github.lindenb.jvarkit.util.vcf.PostponedVariantContextWriter)

Example 68 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfMultiToOneInfo method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter out) {
    final VCFHeader srcHeader = in.getHeader();
    final VCFInfoHeaderLine srcInfo = srcHeader.getInfoHeaderLine(this.infoTag);
    if (srcInfo == null) {
        LOG.error("Cannot find INFO FIELD '" + this.infoTag + "'");
        return -1;
    }
    switch(srcInfo.getCountType()) {
        case INTEGER:
            break;
        case UNBOUNDED:
            break;
        default:
            {
                LOG.error("CountType is not supported '" + srcInfo.getCountType() + "'");
                return -1;
            }
    }
    switch(srcInfo.getType()) {
        case Flag:
            {
                LOG.error("Type is not supported '" + srcInfo.getType() + "'");
                return -1;
            }
        default:
            break;
    }
    final VCFHeader destHeader = new VCFHeader(srcHeader);
    super.addMetaData(destHeader);
    final SAMSequenceDictionaryProgress progess = new SAMSequenceDictionaryProgress(srcHeader);
    out.writeHeader(destHeader);
    while (in.hasNext()) {
        final VariantContext ctx = progess.watch(in.next());
        final List<Object> L = ctx.getAttributeAsList(srcInfo.getID());
        if (L.isEmpty() || L.size() == 1) {
            out.add(ctx);
            continue;
        }
        for (final Object o : L) {
            final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
            vcb.attribute(srcInfo.getID(), o);
            out.add(vcb.make());
        }
    }
    progess.finish();
    LOG.info("done");
    return RETURN_OK;
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine)

Example 69 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfNoCallToHomRef method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter delegate) {
    final CtxWriterFactory.CtxWriter out = this.component.open(delegate);
    out.writeHeader(in.getHeader());
    final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader()).logger(LOG);
    while (in.hasNext()) {
        out.add(progress.watch(in.next()));
    }
    progress.finish();
    out.close();
    return 0;
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)

Example 70 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfRegulomeDB method doVcfToVcf.

@Override
protected int doVcfToVcf(String inputName, VcfIterator in, VariantContextWriter out) {
    VCFHeader header = in.getHeader();
    SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header.getSequenceDictionary());
    header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
    header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
    header.addMetaDataLine(new VCFInfoHeaderLine(this.infoTag, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.String, "Format: Position|Distance|Rank"));
    out.writeHeader(header);
    while (in.hasNext()) {
        List<String> regDataList = new ArrayList<String>();
        VariantContext ctx = in.next();
        progress.watch(ctx.getContig(), ctx.getStart());
        int start = Math.max(0, ctx.getStart() - this.extend);
        int end = ctx.getEnd() + this.extend;
        for (Iterator<RegData> iter = this.regDataTabixFileReader.iterator(ctx.getContig(), start, end); iter.hasNext(); ) {
            RegData curr = iter.next();
            if (this.acceptRegex != null && !this.acceptRegex.matcher(curr.rank).matches()) {
                continue;
            }
            String str = String.valueOf(curr.chromSart) + "|" + String.valueOf(Math.abs(curr.chromSart - (ctx.getStart() - 1))) + "|" + curr.rank;
            regDataList.add(str);
        }
        if (regDataList.isEmpty()) {
            out.add(ctx);
            continue;
        }
        VariantContextBuilder vcb = new VariantContextBuilder(ctx);
        vcb.attribute(this.infoTag, regDataList.toArray());
        out.add(vcb.make());
    }
    progress.finish();
    return 0;
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine)

Aggregations

SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)146 ArrayList (java.util.ArrayList)64 VariantContext (htsjdk.variant.variantcontext.VariantContext)59 VCFHeader (htsjdk.variant.vcf.VCFHeader)57 SAMRecord (htsjdk.samtools.SAMRecord)54 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)54 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)48 IOException (java.io.IOException)48 File (java.io.File)47 SamReader (htsjdk.samtools.SamReader)40 SAMFileHeader (htsjdk.samtools.SAMFileHeader)38 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)37 HashSet (java.util.HashSet)34 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)32 VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)30 List (java.util.List)30 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)29 HashMap (java.util.HashMap)28 Parameter (com.beust.jcommander.Parameter)27 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)27