use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.
the class GATKVariantContextUtilsUnitTest method testCreateVCFWriterLenientTrue.
@Test(dataProvider = "createVCFWriterLenientData")
public void testCreateVCFWriterLenientTrue(final String outputExtension, final String indexExtension, final boolean createIndex, final boolean createMD5) throws IOException {
final File tmpDir = createTempDir("createVCFTest");
final File outputFile = new File(tmpDir.getAbsolutePath(), "createVCFTest" + outputExtension);
Options[] options = createIndex ? new Options[] { Options.ALLOW_MISSING_FIELDS_IN_HEADER, Options.INDEX_ON_THE_FLY } : new Options[] { Options.ALLOW_MISSING_FIELDS_IN_HEADER };
try (final VariantContextWriter vcw = GATKVariantContextUtils.createVCFWriter(outputFile, makeSimpleSequenceDictionary(), createMD5, options)) {
writeHeader(vcw);
// verify leniency by writing a bogus attribute
writeBadVariant(vcw);
}
final File outFileIndex = new File(outputFile.getAbsolutePath() + indexExtension);
final File outFileMD5 = new File(outputFile.getAbsolutePath() + ".md5");
Assert.assertTrue(outputFile.exists(), "No output file was not created");
Assert.assertEquals(outFileIndex.exists(), createIndex, "The createIndex argument was not honored");
Assert.assertEquals(outFileMD5.exists(), createMD5, "The createMD5 argument was not honored");
}
use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.
the class GatherVcfs method gatherConventionally.
/** Code for gathering multiple VCFs that works regardless of input format and output format, but can be slow. */
private static void gatherConventionally(final SAMSequenceDictionary sequenceDictionary, final boolean createIndex, final List<Path> inputFiles, final File outputFile, final int cloudPrefetchBuffer) {
final EnumSet<Options> options = EnumSet.copyOf(VariantContextWriterBuilder.DEFAULT_OPTIONS);
if (createIndex)
options.add(Options.INDEX_ON_THE_FLY);
else
options.remove(Options.INDEX_ON_THE_FLY);
try (final VariantContextWriter out = new VariantContextWriterBuilder().setOutputFile(outputFile).setReferenceDictionary(sequenceDictionary).setOptions(options).build()) {
final ProgressLogger progress = new ProgressLogger(log, 10000);
VariantContext lastContext = null;
Path lastFile = null;
VCFHeader firstHeader = null;
VariantContextComparator comparator = null;
for (final Path f : inputFiles) {
try {
log.debug("Gathering from file: ", f.toUri().toString());
final FeatureReader<VariantContext> variantReader = getReaderFromVCFUri(f, cloudPrefetchBuffer);
final PeekableIterator<VariantContext> variantIterator;
variantIterator = new PeekableIterator<>(variantReader.iterator());
final VCFHeader header = (VCFHeader) variantReader.getHeader();
if (firstHeader == null) {
firstHeader = header;
out.writeHeader(firstHeader);
comparator = new VariantContextComparator(firstHeader.getContigLines());
}
if (lastContext != null && variantIterator.hasNext()) {
final VariantContext vc = variantIterator.peek();
if (comparator.compare(vc, lastContext) <= 0) {
throw new IllegalStateException("First variant in file " + f.toUri().toString() + " is at " + vc.getSource() + " but last variant in earlier file " + lastFile.toUri().toString() + " is at " + lastContext.getSource());
}
}
while (variantIterator.hasNext()) {
lastContext = variantIterator.next();
out.add(lastContext);
progress.record(lastContext.getContig(), lastContext.getStart());
}
lastFile = f;
CloserUtil.close(variantIterator);
CloserUtil.close(variantReader);
} catch (IOException e) {
throw new UserException.CouldNotReadInputFile(f, e.getMessage(), e);
}
}
}
}
use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.
the class GATKTool method createVCFWriter.
/**
* Creates a VariantContextWriter whose outputFile type is determined by
* the outFile's extension, using the best available sequence dictionary for
* this tool, and default index, leniency and md5 generation settings.
*
* @param outFile output File for this writer. May not be null.
* @returns VariantContextWriter must be closed by the caller
*/
public VariantContextWriter createVCFWriter(final File outFile) {
Utils.nonNull(outFile);
final SAMSequenceDictionary sequenceDictionary = getBestAvailableSequenceDictionary();
List<Options> options = new ArrayList<>();
if (lenientVCFProcessing) {
options.add(Options.ALLOW_MISSING_FIELDS_IN_HEADER);
}
if (createOutputVariantIndex) {
if (null == sequenceDictionary) {
logger.warn("An variant index will not be created - a sequence dictionary is required to create an output index");
// fall through and create without an index
} else {
options.add(Options.INDEX_ON_THE_FLY);
}
}
return GATKVariantContextUtils.createVCFWriter(outFile, sequenceDictionary, createOutputVariantMD5, options.toArray(new Options[options.size()]));
}
use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.
the class SVVCFWriter method getVariantContextWriter.
private static VariantContextWriter getVariantContextWriter(final OutputStream outputStream, final SAMSequenceDictionary referenceSequenceDictionary) {
VariantContextWriterBuilder vcWriterBuilder = new VariantContextWriterBuilder().clearOptions().setOutputStream(outputStream);
if (null != referenceSequenceDictionary) {
vcWriterBuilder = vcWriterBuilder.setReferenceDictionary(referenceSequenceDictionary);
}
// todo: remove this when things are solid?
vcWriterBuilder = vcWriterBuilder.setOption(Options.ALLOW_MISSING_FIELDS_IN_HEADER);
for (final Options opt : new Options[] {}) {
vcWriterBuilder = vcWriterBuilder.setOption(opt);
}
return vcWriterBuilder.build();
}
Aggregations