Search in sources :

Example 11 with VariantContextComparator

use of htsjdk.variant.variantcontext.VariantContextComparator in project gatk by broadinstitute.

the class AbstractVcfMergingClpTester method validateSnpAndIndelResults.

/**
     * Make sure that the order of the output file is identical to the order
     * of the input files by iterating through the output, making sure that,
     * if the context is an indel (snp), the next genomic position in the indel
     * (snp) queue is the same. Also make sure that the context is in the order
     * specified by the input files.
     */
private void validateSnpAndIndelResults(final File output, final Queue<String> indelContigPositions, final Queue<String> snpContigPositions) {
    final VCFFileReader outputReader = new VCFFileReader(output, false);
    final VariantContextComparator outputComparator = outputReader.getFileHeader().getVCFRecordComparator();
    VariantContext last = null;
    try (final CloseableIterator<VariantContext> iterator = outputReader.iterator()) {
        while (iterator.hasNext()) {
            final VariantContext outputContext = iterator.next();
            if (outputContext.isIndel())
                Assert.assertEquals(getContigPosition(outputContext), indelContigPositions.poll());
            if (outputContext.isSNP())
                Assert.assertEquals(getContigPosition(outputContext), snpContigPositions.poll());
            if (last != null)
                Assert.assertTrue(outputComparator.compare(last, outputContext) <= 0);
            last = outputContext;
        }
    }
    // We should have polled everything off the indel (snp) queues
    Assert.assertEquals(indelContigPositions.size(), 0);
    Assert.assertEquals(snpContigPositions.size(), 0);
}
Also used : VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator)

Example 12 with VariantContextComparator

use of htsjdk.variant.variantcontext.VariantContextComparator in project gatk by broadinstitute.

the class MultiVariantDataSource method getMergedIteratorFromDataSources.

/**
     * Close any existing iterator, create a new iterator and update the local cached iterator reference.
     * @param iteratorFromSource function to retrieve individual iterator, to be applied to each data source
     * @return
     */
private Iterator<VariantContext> getMergedIteratorFromDataSources(final Function<FeatureDataSource<VariantContext>, Iterator<VariantContext>> iteratorFromSource) {
    // Tribble documentation states that having multiple iterators open simultaneously over the same FeatureReader
    // results in undefined behavior
    closeOpenIterationIfNecessary();
    if (featureDataSources.size() > 1) {
        final List<CloseableIterator<VariantContext>> iterators = new ArrayList<>(featureDataSources.size());
        featureDataSources.forEach(ds -> iterators.add(getCloseableIteratorWrapper(iteratorFromSource.apply((ds)))));
        final VariantContextComparator varComparator = new VariantContextComparator(getSequenceDictionary());
        currentIterator = new MergingIterator<>(varComparator, iterators);
    } else {
        currentIterator = getCloseableIteratorWrapper(iteratorFromSource.apply(featureDataSources.get(0)));
    }
    return currentIterator;
}
Also used : CloseableIterator(htsjdk.samtools.util.CloseableIterator) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator)

Aggregations

VariantContextComparator (htsjdk.variant.variantcontext.VariantContextComparator)12 VariantContext (htsjdk.variant.variantcontext.VariantContext)11 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)7 VCFHeader (htsjdk.variant.vcf.VCFHeader)6 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)5 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)4 CloseableIterator (htsjdk.samtools.util.CloseableIterator)4 File (java.io.File)4 MergingIterator (htsjdk.samtools.util.MergingIterator)3 VariantContextWriterBuilder (htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)3 UserException (org.broadinstitute.hellbender.exceptions.UserException)3 RuntimeIOException (htsjdk.samtools.util.RuntimeIOException)2 Path (java.nio.file.Path)2 Lists (com.google.common.collect.Lists)1 QueryInterval (htsjdk.samtools.QueryInterval)1 SAMFileWriter (htsjdk.samtools.SAMFileWriter)1 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)1 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)1 SamReader (htsjdk.samtools.SamReader)1 SamReaderFactory (htsjdk.samtools.SamReaderFactory)1