use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.
the class RenameSampleInVcf method doWork.
@Override
protected Object doWork() {
IOUtil.assertFileIsReadable(INPUT);
IOUtil.assertFileIsWritable(OUTPUT);
try (final VCFFileReader in = new VCFFileReader(INPUT)) {
final VCFHeader header = in.getFileHeader();
Utils.validateArg(header.getGenotypeSamples().size() == 1, "Input VCF must be single-sample.");
Utils.validateArg(OLD_SAMPLE_NAME == null || OLD_SAMPLE_NAME.equals(header.getGenotypeSamples().get(0)), () -> "Input VCF did not contain expected sample. Contained: " + header.getGenotypeSamples().get(0));
final EnumSet<Options> options = EnumSet.copyOf(VariantContextWriterBuilder.DEFAULT_OPTIONS);
if (CREATE_INDEX)
options.add(Options.INDEX_ON_THE_FLY);
else
options.remove(Options.INDEX_ON_THE_FLY);
final VCFHeader outHeader = new VCFHeader(header.getMetaDataInInputOrder(), CollectionUtil.makeList(NEW_SAMPLE_NAME));
try (final VariantContextWriter out = new VariantContextWriterBuilder().setOutputFile(OUTPUT).setReferenceDictionary(outHeader.getSequenceDictionary()).setOptions(options).build()) {
out.writeHeader(outHeader);
for (final VariantContext ctx : in) {
out.add(ctx);
}
}
}
return null;
}
use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.
the class SortVcf method writeSortedOutput.
private void writeSortedOutput(final VCFHeader outputHeader, final SortingCollection<VariantContext> sortedOutput) {
final ProgressLogger writeProgress = new ProgressLogger(logger, 25000, "wrote", "records");
final EnumSet<Options> options = CREATE_INDEX ? EnumSet.of(Options.INDEX_ON_THE_FLY) : EnumSet.noneOf(Options.class);
final VariantContextWriter out = new VariantContextWriterBuilder().setReferenceDictionary(outputHeader.getSequenceDictionary()).setOptions(options).setOutputFile(OUTPUT).build();
out.writeHeader(outputHeader);
for (final VariantContext variantContext : sortedOutput) {
out.add(variantContext);
writeProgress.record(variantContext.getContig(), variantContext.getStart());
}
out.close();
}
use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.
the class GATKVariantContextUtils method createVCFWriter.
/**
* Creates a VariantContextWriter whose outputFile type is based on the extension of the output file name.
* The default options set by VariantContextWriter are cleared before applying ALLOW_MISSING_FIELDS_IN_HEADER (if
* <code>lenientProcessing</code> is set), followed by the set of options specified by any <code>options</code> args.
*
* @param outFile output File for this writer. May not be null.
* @param referenceDictionary required if on the fly indexing is set, otherwise can be null
* @param createMD5 true if an md5 file should be created
* @param options variable length list of additional Options to be set for this writer
* @returns VariantContextWriter must be closed by the caller
*/
public static VariantContextWriter createVCFWriter(final File outFile, final SAMSequenceDictionary referenceDictionary, final boolean createMD5, final Options... options) {
Utils.nonNull(outFile);
VariantContextWriterBuilder vcWriterBuilder = new VariantContextWriterBuilder().clearOptions().setOutputFile(outFile);
if (VariantContextWriterBuilder.OutputType.UNSPECIFIED == getVariantFileTypeFromExtension(outFile)) {
// the only way the user has to specify an output type is by file extension, and htsjdk
// throws if it can't map the file extension to a known vcf type, so fallback to a default
// of VCF
logger.warn(String.format("Can't determine output variant file format from output file extension \"%s\". Defaulting to VCF.", FilenameUtils.getExtension(outFile.getPath())));
vcWriterBuilder = vcWriterBuilder.setOutputFileType(VariantContextWriterBuilder.OutputType.VCF);
}
if (createMD5) {
vcWriterBuilder.setCreateMD5();
}
if (null != referenceDictionary) {
vcWriterBuilder = vcWriterBuilder.setReferenceDictionary(referenceDictionary);
}
for (Options opt : options) {
vcWriterBuilder = vcWriterBuilder.setOption(opt);
}
return vcWriterBuilder.build();
}
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