Search in sources :

Example 1 with IndexedVcfFileReader

use of com.github.lindenb.jvarkit.util.vcf.IndexedVcfFileReader in project jvarkit by lindenb.

the class VcfVcf method doVcfToVcf.

@Override
protected int doVcfToVcf(String inputName, VcfIterator r, VariantContextWriter w) {
    try {
        CloseableIterator<VariantContext> iter = null;
        LOG.info("opening file: " + this.TABIX);
        IndexedVcfFileReader tabix = new IndexedVcfFileReader(this.TABIX);
        VCFHeader header3 = tabix.getHeader();
        VCFHeader header1 = r.getHeader();
        VCFHeader h2 = new VCFHeader(header1.getMetaDataInInputOrder(), header1.getSampleNamesInOrder());
        for (String infoId : this.INFO_IDS) {
            VCFInfoHeaderLine vihl = header3.getInfoHeaderLine(infoId);
            if (vihl == null) {
                LOG.warn("Not INFO=" + infoId + " in " + TABIX);
                continue;
            }
            if (h2.getInfoHeaderLine(infoId) != null) {
                LOG.warn("Input already contains INFO=" + vihl);
            }
            h2.addMetaDataLine(vihl);
        }
        if (ALT_CONFLICT_FLAG != null) {
            h2.addMetaDataLine(new VCFInfoHeaderLine(ALT_CONFLICT_FLAG, 1, VCFHeaderLineType.Flag, "conflict ALT allele with " + this.TABIX));
        }
        w.writeHeader(h2);
        while (r.hasNext()) {
            VariantContext ctx1 = r.next();
            VariantContextBuilder vcb = new VariantContextBuilder(ctx1);
            String BEST_ID = null;
            boolean best_id_match_alt = false;
            List<VariantContext> variantsList = new ArrayList<VariantContext>();
            iter = tabix.iterator(ctx1.getChr(), Math.max(0, ctx1.getStart() - 1), (ctx1.getEnd() + 1));
            while (iter.hasNext()) {
                VariantContext ctx3 = iter.next();
                if (!ctx3.getContig().equals(ctx1.getContig()))
                    continue;
                if (ctx3.getStart() != ctx1.getStart())
                    continue;
                if (ctx3.getEnd() != ctx1.getEnd())
                    continue;
                if (ctx1.getReference().equals(ctx3.getReference()) && ctx1.getAlternateAlleles().equals(ctx3.getAlternateAlleles())) {
                    variantsList.clear();
                    variantsList.add(ctx3);
                    break;
                } else {
                    variantsList.add(ctx3);
                }
            }
            CloserUtil.close(iter);
            iter = null;
            for (VariantContext ctx3 : variantsList) {
                if (this.REF_ALLELE_MATTERS && !ctx1.getReference().equals(ctx3.getReference())) {
                    continue;
                }
                if (this.ALT_ALLELES_MATTERS && !ctx1.getAlternateAlleles().equals(ctx3.getAlternateAlleles())) {
                    continue;
                }
                if (ctx3.getID() != null && this.REPLACE_ID) {
                    if (BEST_ID != null && best_id_match_alt) {
                    // nothing
                    } else {
                        BEST_ID = ctx3.getID();
                        best_id_match_alt = ctx1.getAlternateAlleles().equals(ctx3.getAlternateAlleles());
                    }
                }
                for (String id : this.INFO_IDS) {
                    Object info3 = ctx3.getAttribute(id);
                    if (info3 == null) {
                        continue;
                    }
                    Object info1 = ctx1.getAttribute(id);
                    if (info1 != null && !this.REPLACE_INFO_FIELD) {
                        continue;
                    }
                    vcb.attribute(id, info3);
                }
                if (ALT_CONFLICT_FLAG != null && !ctx1.getAlternateAlleles().equals(ctx3.getAlternateAlleles())) {
                    vcb.attribute(ALT_CONFLICT_FLAG, true);
                }
            }
            if (BEST_ID != null) {
                vcb.id(BEST_ID);
            }
            w.add(vcb.make());
        }
        tabix.close();
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    }
}
Also used : VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) IndexedVcfFileReader(com.github.lindenb.jvarkit.util.vcf.IndexedVcfFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) IOException(java.io.IOException)

Example 2 with IndexedVcfFileReader

use of com.github.lindenb.jvarkit.util.vcf.IndexedVcfFileReader in project jvarkit by lindenb.

the class KnimeVariantHelper method openVcf.

public IndexedVcfFileReader openVcf(final String resourceName, String path) throws IOException {
    failIf(this.vcfReaders.containsKey(resourceName), "duplicate resource " + resourceName);
    final IndexedVcfFileReader reader = new IndexedVcfFileReader(path);
    this.vcfReaders.put(resourceName, reader);
    return reader;
}
Also used : IndexedVcfFileReader(com.github.lindenb.jvarkit.util.vcf.IndexedVcfFileReader)

Aggregations

IndexedVcfFileReader (com.github.lindenb.jvarkit.util.vcf.IndexedVcfFileReader)2 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)1 VCFHeader (htsjdk.variant.vcf.VCFHeader)1 VCFInfoHeaderLine (htsjdk.variant.vcf.VCFInfoHeaderLine)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1