Search in sources :

Example 16 with VCFCodec

use of htsjdk.variant.vcf.VCFCodec in project gatk by broadinstitute.

the class GenomicsDBImportIntegrationTest method checkGenomicsDBAgainstExpected.

private static void checkGenomicsDBAgainstExpected(final String workspace, final SimpleInterval interval, final String expectedCombinedVCF) throws IOException {
    final GenomicsDBFeatureReader<VariantContext, PositionalBufferedStream> genomicsDBFeatureReader = new GenomicsDBFeatureReader<>(new File(workspace, GenomicsDBConstants.DEFAULT_VIDMAP_FILE_NAME).getAbsolutePath(), new File(workspace, GenomicsDBConstants.DEFAULT_CALLSETMAP_FILE_NAME).getAbsolutePath(), workspace, GenomicsDBConstants.DEFAULT_ARRAY_NAME, b38_reference_20_21, null, new BCF2Codec());
    final AbstractFeatureReader<VariantContext, LineIterator> combinedVCFReader = AbstractFeatureReader.getFeatureReader(expectedCombinedVCF, new VCFCodec(), true);
    try (CloseableTribbleIterator<VariantContext> actualVcs = genomicsDBFeatureReader.query(interval.getContig(), interval.getStart(), interval.getEnd());
        CloseableTribbleIterator<VariantContext> expectedVcs = combinedVCFReader.query(interval.getContig(), interval.getStart(), interval.getEnd())) {
        BaseTest.assertCondition(actualVcs, expectedVcs, (a, e) -> {
            // TODO: Temporary hacks to make this test pass. Must be removed later
            if (// allele order
            e.getStart() != 17967343 && e.getStart() != 17966384 && // split block
            e.getEnd() != 17981447) {
                VariantContextTestUtils.assertVariantContextsAreEqual(a, e, Collections.emptyList());
            }
        });
    }
}
Also used : PositionalBufferedStream(htsjdk.tribble.readers.PositionalBufferedStream) VCFCodec(htsjdk.variant.vcf.VCFCodec) BCF2Codec(htsjdk.variant.bcf2.BCF2Codec) VariantContext(htsjdk.variant.variantcontext.VariantContext) File(java.io.File) LineIterator(htsjdk.tribble.readers.LineIterator) GenomicsDBFeatureReader(com.intel.genomicsdb.GenomicsDBFeatureReader)

Example 17 with VCFCodec

use of htsjdk.variant.vcf.VCFCodec in project gatk-protected by broadinstitute.

the class GenotypeGVCFsIntegrationTest method getVariantContexts.

/**
     * Returns a list of VariantContext records from a VCF file
     *
     * @param vcfFile VCF file
     * @return list of VariantContext records
     * @throws IOException if the file does not exist or can not be opened
     */
private static List<VariantContext> getVariantContexts(final File vcfFile) throws IOException {
    final VCFCodec codec = new VCFCodec();
    final FileInputStream s = new FileInputStream(vcfFile);
    final LineIterator lineIteratorVCF = codec.makeSourceFromStream(new PositionalBufferedStream(s));
    codec.readHeader(lineIteratorVCF);
    final List<VariantContext> VCs = new ArrayList<>();
    while (lineIteratorVCF.hasNext()) {
        final String line = lineIteratorVCF.next();
        Assert.assertFalse(line == null);
        VCs.add(codec.decode(line));
    }
    return VCs;
}
Also used : VCFCodec(htsjdk.variant.vcf.VCFCodec) PositionalBufferedStream(htsjdk.tribble.readers.PositionalBufferedStream) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) LineIterator(htsjdk.tribble.readers.LineIterator) FileInputStream(java.io.FileInputStream)

Example 18 with VCFCodec

use of htsjdk.variant.vcf.VCFCodec in project hmftools by hartwigmedical.

the class SomaticVariantFactoryTest method createTestCodec.

@NotNull
private static VCFCodec createTestCodec() {
    VCFCodec codec = new VCFCodec();
    VCFHeader header = new VCFHeader(Sets.newHashSet(), Sets.newHashSet(SAMPLE));
    codec.setVCFHeader(header, VCFHeaderVersion.VCF4_2);
    return codec;
}
Also used : VCFCodec(htsjdk.variant.vcf.VCFCodec) VCFHeader(htsjdk.variant.vcf.VCFHeader) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with VCFCodec

use of htsjdk.variant.vcf.VCFCodec in project hmftools by hartwigmedical.

the class BlacklistPredicateTest method setup.

@Before
public void setup() throws SAXException {
    BachelorSchema schema = BachelorSchema.make();
    final Program program = schema.processXML(Paths.get(BLACKLIST_XML));
    assertNotNull(program);
    blacklistPredicate = new BlacklistPredicate(Sets.newHashSet("ENST00000380152"), program.getBlacklist());
    VCFHeader header = new VCFHeader(Sets.newHashSet(), Sets.newHashSet(SAMPLE));
    codec = new VCFCodec();
    codec.setVCFHeader(header, VCFHeaderVersion.VCF4_2);
}
Also used : VCFCodec(htsjdk.variant.vcf.VCFCodec) Program(nl.hartwigmedicalfoundation.bachelor.Program) BachelorSchema(com.hartwig.hmftools.bachelor.BachelorSchema) VCFHeader(htsjdk.variant.vcf.VCFHeader) Before(org.junit.Before)

Example 20 with VCFCodec

use of htsjdk.variant.vcf.VCFCodec in project jvarkit by lindenb.

the class VCFComposite method doWork.

@Override
public int doWork(final List<String> args) {
    PrintWriter out = null;
    try {
        out = super.openFileOrStdoutAsPrintWriter(this.outputFile);
        if (listModels) {
            for (final Type t : Type.values()) {
                out.println(t.name());
                out.println("\t" + t.getDescription());
            }
            out.flush();
            return 0;
        }
        this.pedigree = Pedigree.newParser().parse(pedigreeFile);
        if (this.pedigree.getAffected().isEmpty()) {
            LOG.error("No Affected sample in " + this.pedigreeFile);
            return -1;
        }
        if (this.pedigree.getUnaffected().isEmpty()) {
            LOG.error("No Unaffected sample in " + this.pedigreeFile);
            return -1;
        }
        final DiseaseModel model = this.createModel();
        final String inputName = super.oneFileOrNull(args);
        final LineIterator r = (inputName == null ? IOUtils.openStreamForLineIterator(stdin()) : IOUtils.openURIForLineIterator(inputName));
        final VCFCodec codec = new VCFCodec();
        final VCFHeader header = (VCFHeader) codec.readActualHeader(r);
        final AnnPredictionParser annParser = new AnnPredictionParserFactory(header).get();
        final VepPredictionParser vepParser = new VepPredictionParserFactory(header).get();
        // final VCFHeader h2=new VCFHeader(header.getMetaDataInInputOrder(),header.getSampleNamesInOrder());
        // h2.addMetaDataLine(new VCFInfoHeaderLine(this.TAG,1,VCFHeaderLineType.String,"Values from bigwig file: "+BIGWIG));
        SortingCollection<GeneAndVariant> sorting = null;
        String prevContig = null;
        for (; ; ) {
            String line;
            final VariantContext ctx;
            if (r.hasNext()) {
                line = r.next();
                ctx = codec.decode(line);
            } else {
                line = null;
                ctx = null;
            }
            if (ctx == null || !ctx.getContig().equals(prevContig)) {
                if (sorting != null) {
                    LOG.debug("Dump contig " + prevContig);
                    sorting.doneAdding();
                    CloseableIterator<GeneAndVariant> iter2 = sorting.iterator();
                    EqualRangeIterator<GeneAndVariant> eqiter = new EqualRangeIterator<>(iter2, (A, B) -> A.gene.compareTo(B.gene));
                    while (eqiter.hasNext()) {
                        final List<GeneAndVariant> variants = eqiter.next();
                        model.scan(variants.get(0).gene, variants.stream().map(L -> codec.decode(L.ctxLine)).collect(Collectors.toList()), out);
                    }
                    eqiter.close();
                    iter2.close();
                    sorting.cleanup();
                }
                sorting = null;
                if (ctx == null)
                    break;
                prevContig = ctx.getContig();
            }
            if (!ctx.isVariant())
                continue;
            if (!acceptFiltered && ctx.isFiltered())
                continue;
            if (!acceptID && ctx.hasID())
                continue;
            if (!model.accept(ctx))
                continue;
            final Set<String> geneKeys = new HashSet<>();
            for (final AnnPredictionParser.AnnPrediction pred : annParser.getPredictions(ctx)) {
                geneKeys.addAll(pred.getGeneKeys().stream().map(S -> ctx.getContig() + "_" + S).collect(Collectors.toSet()));
            }
            for (final VepPredictionParser.VepPrediction pred : vepParser.getPredictions(ctx)) {
                geneKeys.addAll(pred.getGeneKeys().stream().map(S -> ctx.getContig() + "_" + S).collect(Collectors.toSet()));
            }
            if (sorting == null) {
                sorting = SortingCollection.newInstance(GeneAndVariant.class, new GeneAndVariantCodec(), (A, B) -> {
                    int i = A.gene.compareTo(B.gene);
                    if (i != 0)
                        return i;
                    return A.ctxLine.compareTo(B.ctxLine);
                }, this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
                sorting.setDestructiveIteration(true);
            }
            for (final String gk : geneKeys) {
                final GeneAndVariant gav = new GeneAndVariant();
                gav.gene = gk;
                gav.ctxLine = line;
                sorting.add(gav);
            }
        }
        out.flush();
        out.close();
        out = null;
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(out);
    }
}
Also used : AnnPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParser) Genotype(htsjdk.variant.variantcontext.Genotype) DataInputStream(java.io.DataInputStream) CloseableIterator(htsjdk.samtools.util.CloseableIterator) LineIterator(htsjdk.tribble.readers.LineIterator) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) VCFHeader(htsjdk.variant.vcf.VCFHeader) AnnPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParser) ParametersDelegate(com.beust.jcommander.ParametersDelegate) HashSet(java.util.HashSet) ContigPosRef(com.github.lindenb.jvarkit.util.vcf.ContigPosRef) DataOutputStream(java.io.DataOutputStream) AbstractDataCodec(com.github.lindenb.jvarkit.util.picard.AbstractDataCodec) Pedigree(com.github.lindenb.jvarkit.util.Pedigree) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) VepPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParser) VCFCodec(htsjdk.variant.vcf.VCFCodec) VepPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParserFactory) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) SortingCollection(htsjdk.samtools.util.SortingCollection) Predicate(java.util.function.Predicate) Logger(com.github.lindenb.jvarkit.util.log.Logger) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) EqualRangeIterator(com.github.lindenb.jvarkit.util.iterator.EqualRangeIterator) AnnPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParserFactory) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContext(htsjdk.variant.variantcontext.VariantContext) LineIterator(htsjdk.tribble.readers.LineIterator) EqualRangeIterator(com.github.lindenb.jvarkit.util.iterator.EqualRangeIterator) AnnPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParserFactory) VCFHeader(htsjdk.variant.vcf.VCFHeader) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) VCFCodec(htsjdk.variant.vcf.VCFCodec) IOException(java.io.IOException) VepPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParser) VepPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParserFactory)

Aggregations

VCFCodec (htsjdk.variant.vcf.VCFCodec)21 LineIterator (htsjdk.tribble.readers.LineIterator)12 VariantContext (htsjdk.variant.variantcontext.VariantContext)12 VCFHeader (htsjdk.variant.vcf.VCFHeader)11 File (java.io.File)11 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)6 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)5 PositionalBufferedStream (htsjdk.tribble.readers.PositionalBufferedStream)4 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)3 Index (htsjdk.tribble.index.Index)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 StructuralVariantFactory (com.hartwig.hmftools.common.variant.structural.StructuralVariantFactory)2 GenomicsDBFeatureReader (com.intel.genomicsdb.GenomicsDBFeatureReader)2 BlockCompressedInputStream (htsjdk.samtools.util.BlockCompressedInputStream)2 BlockCompressedOutputStream (htsjdk.samtools.util.BlockCompressedOutputStream)2 CloseableIterator (htsjdk.samtools.util.CloseableIterator)2 AbstractFeatureReader (htsjdk.tribble.AbstractFeatureReader)2 FeatureReader (htsjdk.tribble.FeatureReader)2 BCF2Codec (htsjdk.variant.bcf2.BCF2Codec)2