Search in sources :

Example 1 with VcfIteratorImpl

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

the class FindCorruptedFiles method testVcf.

private void testVcf(File f, InputStream in) throws IOException, TribbleException {
    long n = 0;
    VcfIterator iter = new VcfIteratorImpl(in);
    iter.getHeader();
    while (iter.hasNext() && (NUM < 0 || n < NUM)) {
        iter.next();
        ++n;
    }
    if (n == 0) {
        emptyFile(f);
    }
    iter.close();
}
Also used : VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) VcfIteratorImpl(com.github.lindenb.jvarkit.util.vcf.VcfIteratorImpl)

Example 2 with VcfIteratorImpl

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

the class NgsFilesSummary method readVCF.

@Override
protected void readVCF(final File f) {
    if (!f.canRead())
        return;
    LOG.debug("readVCF  " + f);
    VcfIterator r = null;
    InputStream in = null;
    try {
        in = IOUtils.openFileForReading(f);
        r = new VcfIteratorImpl(in);
        VCFHeader header = r.getHeader();
        for (final String sample : header.getSampleNamesInOrder()) {
            print(sample, InfoType.VCF, f);
        }
    } catch (final Exception err) {
        LOG.error(err);
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(in);
    }
}
Also used : VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) VcfIteratorImpl(com.github.lindenb.jvarkit.util.vcf.VcfIteratorImpl) InputStream(java.io.InputStream) VCFHeader(htsjdk.variant.vcf.VCFHeader) IOException(java.io.IOException)

Example 3 with VcfIteratorImpl

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

the class FixVCF method doWork.

private int doWork(String filenameIn, InputStream vcfStream, VariantContextWriter w) throws IOException {
    final AbstractVCFCodec vcfCodec = VCFUtils.createDefaultVCFCodec();
    LineIterator r = new LineIteratorImpl(new SynchronousLineReader(vcfStream));
    final VCFHeader header = (VCFHeader) vcfCodec.readActualHeader(r);
    // samples names have been changed by picard api and reordered !!!
    // re-create the original order
    List<String> sampleNamesInSameOrder = new ArrayList<String>(header.getSampleNamesInOrder().size());
    for (int col = 0; col < header.getSampleNamesInOrder().size(); ++col) {
        for (String sample : header.getSampleNameToOffset().keySet()) {
            if (header.getSampleNameToOffset().get(sample) == col) {
                sampleNamesInSameOrder.add(sample);
                break;
            }
        }
    }
    if (sampleNamesInSameOrder.size() != header.getSampleNamesInOrder().size()) {
        throw new IllegalStateException();
    }
    VCFHeader h2 = new VCFHeader(header.getMetaDataInInputOrder(), sampleNamesInSameOrder);
    File tmp = IOUtil.newTempFile("tmp", ".vcf.gz", new File[] { tmpDir });
    tmp.deleteOnExit();
    PrintWriter pw = new PrintWriter(new GZIPOutputStream(new FileOutputStream(tmp)));
    while (r.hasNext()) {
        String line = r.next();
        pw.println(line);
        VariantContext ctx = null;
        try {
            ctx = vcfCodec.decode(line);
        } catch (Exception err) {
            pw.close();
            LOG.error(line);
            LOG.error(err);
            return -1;
        }
        for (String f : ctx.getFilters()) {
            if (h2.getFilterHeaderLine(f) != null)
                continue;
            // if(f.equals(VCFConstants.PASSES_FILTERS_v4)) continue; hum...
            if (f.isEmpty() || f.equals(VCFConstants.UNFILTERED))
                continue;
            LOG.info("Fixing missing Filter:" + f);
            h2.addMetaDataLine(new VCFFilterHeaderLine(f));
        }
        for (String tag : ctx.getAttributes().keySet()) {
            if (h2.getInfoHeaderLine(tag) != null)
                continue;
            LOG.info("Fixing missing INFO:" + tag);
            h2.addMetaDataLine(new VCFInfoHeaderLine(tag, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.String, "undefined. Saved by " + getClass()));
        }
    }
    pw.flush();
    pw.close();
    pw = null;
    LOG.info("re-reading VCF frm tmpFile:" + tmp);
    h2.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName(), "Saved VCF FILTER AND INFO from " + filenameIn));
    // save header in memory
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    VariantContextWriter w2 = VCFUtils.createVariantContextWriterToOutputStream(baos);
    w2.writeHeader(h2);
    w2.close();
    baos.close();
    // reopen tmp file
    @SuppressWarnings("resource") VcfIterator in = new VcfIteratorImpl(new SequenceInputStream(new ByteArrayInputStream(baos.toByteArray()), new GZIPInputStream(new FileInputStream(tmp))));
    w.writeHeader(h2);
    while (in.hasNext()) {
        w.add(in.next());
    }
    in.close();
    tmp.delete();
    return 0;
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) LineIterator(htsjdk.tribble.readers.LineIterator) GZIPInputStream(java.util.zip.GZIPInputStream) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) GZIPOutputStream(java.util.zip.GZIPOutputStream) SynchronousLineReader(htsjdk.tribble.readers.SynchronousLineReader) VCFFilterHeaderLine(htsjdk.variant.vcf.VCFFilterHeaderLine) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) LineIteratorImpl(htsjdk.tribble.readers.LineIteratorImpl) VCFHeader(htsjdk.variant.vcf.VCFHeader) PrintWriter(java.io.PrintWriter) VcfIteratorImpl(com.github.lindenb.jvarkit.util.vcf.VcfIteratorImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractVCFCodec(htsjdk.variant.vcf.AbstractVCFCodec) IOException(java.io.IOException) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) FileInputStream(java.io.FileInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)3 VcfIteratorImpl (com.github.lindenb.jvarkit.util.vcf.VcfIteratorImpl)3 VCFHeader (htsjdk.variant.vcf.VCFHeader)2 IOException (java.io.IOException)2 LineIterator (htsjdk.tribble.readers.LineIterator)1 LineIteratorImpl (htsjdk.tribble.readers.LineIteratorImpl)1 SynchronousLineReader (htsjdk.tribble.readers.SynchronousLineReader)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)1 AbstractVCFCodec (htsjdk.variant.vcf.AbstractVCFCodec)1 VCFFilterHeaderLine (htsjdk.variant.vcf.VCFFilterHeaderLine)1 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)1 VCFInfoHeaderLine (htsjdk.variant.vcf.VCFInfoHeaderLine)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1