final boolean createIndex,
final List<File> inputFiles,
final File outputFile) {
final EnumSet<Options> options = EnumSet.copyOf(VariantContextWriterFactory.DEFAULT_OPTIONS);
if (createIndex) options.add(Options.INDEX_ON_THE_FLY); else options.remove(Options.INDEX_ON_THE_FLY);
final VariantContextWriter out = VariantContextWriterFactory.create(outputFile, sequenceDictionary, options);
final ProgressLogger progress = new ProgressLogger(log, 10000);
VariantContext lastContext = null;
File lastFile = null;
VCFHeader firstHeader = null;
VariantContextComparator comparator = null;
for (final File f : inputFiles) {
log.debug("Gathering from file: ", f.getAbsolutePath());
final VCFFileReader variantReader = new VCFFileReader(f, false);
final PeekableIterator<VariantContext> variantIterator = new PeekableIterator<VariantContext>(variantReader.iterator());
final VCFHeader header = variantReader.getFileHeader();
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.getAbsolutePath() + " is at " + vc.getSource() +
" but last variant in earlier file " + lastFile.getAbsolutePath() + " is at " + lastContext.getSource());
}
}
while (variantIterator.hasNext()) {
lastContext = variantIterator.next();
out.add(lastContext);
progress.record(lastContext.getChr(), lastContext.getStart());
}
lastFile = f;
CloserUtil.close(variantIterator);
CloserUtil.close(variantReader);
}
out.close();
}