// Path: src/main/java/org/seqdoop/hadoop_bam/util/WrapSeekable.java
// public class WrapSeekable<S extends InputStream & Seekable>
// 	extends SeekableStream
// {
// 	private final S    stm;
// 	private final long len;
// 	private final Path path;
// 
// 	public WrapSeekable(final S s, long length, Path p) {
// 		stm  = s;
// 		len  = length;
// 		path = p;
// 	}
// 
// 	/** A helper for the common use case. */
// 	public static WrapSeekable<FSDataInputStream> openPath(
// 		FileSystem fs, Path p) throws IOException
// 	{
// 		return new WrapSeekable<FSDataInputStream>(
// 			fs.open(p), fs.getFileStatus(p).getLen(), p);
// 	}
// 	public static WrapSeekable<FSDataInputStream> openPath(
// 		Configuration conf, Path path) throws IOException
// 	{
// 		return openPath(path.getFileSystem(conf), path);
// 	}
// 
// 	@Override public String getSource() { return path.toString(); }
// 	@Override public long   length   () { return len; }
// 
// 	@Override public long position() throws IOException { return stm.getPos(); }
// 	@Override public void    close() throws IOException { stm.close(); }
// 	@Override public boolean eof  () throws IOException {
// 		return stm.getPos() == length();
// 	}
// 	@Override public void seek(long pos) throws IOException {
// 		stm.seek(pos);
// 	}
// 	@Override public int read() throws IOException {
// 		return stm.read();
// 	}
// 	@Override public int read(byte[] buf, int offset, int len)
// 		throws IOException
// 	{
// 		return stm.read(buf, offset, len);
// 	}
// }


import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import htsjdk.variant.variantcontext.GenotypesContext;
import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.variantcontext.writer.Options;
import htsjdk.variant.variantcontext.writer.VariantContextWriter;
import htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder;
import htsjdk.variant.vcf.VCFHeader;
import org.seqdoop.hadoop_bam.util.VCFHeaderReader;
import org.seqdoop.hadoop_bam.util.WrapSeekable;
import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import htsjdk.samtools.util.BlockCompressedOutputStream;
import org.apache.hadoop.conf.Configuration;
// Copyright (c) 2013 Aalto University
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// File created: 2013-06-28 15:58:08

package org.seqdoop.hadoop_bam;





/** A base {@link RecordWriter} for compressed BCF.
 *
 * <p>Handles the output stream, writing the header if requested, and provides
 * the {@link #writeRecord} function for subclasses.</p>
 */
public abstract class BCFRecordWriter<K>
	extends RecordWriter<K,VariantContextWritable>
{
	private VariantContextWriter writer;
	private LazyVCFGenotypesContext.HeaderDataCache vcfHeaderDataCache =
		new LazyVCFGenotypesContext.HeaderDataCache();
	private LazyBCFGenotypesContext.HeaderDataCache bcfHeaderDataCache =
		new LazyBCFGenotypesContext.HeaderDataCache();

	/** A VCF header is read from the input Path, which should refer to a VCF or
	 * BCF file.
	 */
	public BCFRecordWriter(
			Path output, Path input, boolean writeHeader, TaskAttemptContext ctx)
		throws IOException
	{
// Path: src/main/java/org/seqdoop/hadoop_bam/util/WrapSeekable.java
// public class WrapSeekable<S extends InputStream & Seekable>
// 	extends SeekableStream
// {
// 	private final S    stm;
// 	private final long len;
// 	private final Path path;
// 
// 	public WrapSeekable(final S s, long length, Path p) {
// 		stm  = s;
// 		len  = length;
// 		path = p;
// 	}
// 
// 	/** A helper for the common use case. */
// 	public static WrapSeekable<FSDataInputStream> openPath(
// 		FileSystem fs, Path p) throws IOException
// 	{
// 		return new WrapSeekable<FSDataInputStream>(
// 			fs.open(p), fs.getFileStatus(p).getLen(), p);
// 	}
// 	public static WrapSeekable<FSDataInputStream> openPath(
// 		Configuration conf, Path path) throws IOException
// 	{
// 		return openPath(path.getFileSystem(conf), path);
// 	}
// 
// 	@Override public String getSource() { return path.toString(); }
// 	@Override public long   length   () { return len; }
// 
// 	@Override public long position() throws IOException { return stm.getPos(); }
// 	@Override public void    close() throws IOException { stm.close(); }
// 	@Override public boolean eof  () throws IOException {
// 		return stm.getPos() == length();
// 	}
// 	@Override public void seek(long pos) throws IOException {
// 		stm.seek(pos);
// 	}
// 	@Override public int read() throws IOException {
// 		return stm.read();
// 	}
// 	@Override public int read(byte[] buf, int offset, int len)
// 		throws IOException
// 	{
// 		return stm.read(buf, offset, len);
// 	}
// }


// Path: src/main/java/org/seqdoop/hadoop_bam/BCFRecordWriter.java
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import htsjdk.variant.variantcontext.GenotypesContext;
import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.variantcontext.writer.Options;
import htsjdk.variant.variantcontext.writer.VariantContextWriter;
import htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder;
import htsjdk.variant.vcf.VCFHeader;
import org.seqdoop.hadoop_bam.util.VCFHeaderReader;
import org.seqdoop.hadoop_bam.util.WrapSeekable;
import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import htsjdk.samtools.util.BlockCompressedOutputStream;
import org.apache.hadoop.conf.Configuration;

// Copyright (c) 2013 Aalto University
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// File created: 2013-06-28 15:58:08

package org.seqdoop.hadoop_bam;





/** A base {@link RecordWriter} for compressed BCF.
 *
 * <p>Handles the output stream, writing the header if requested, and provides
 * the {@link #writeRecord} function for subclasses.</p>
 */
public abstract class BCFRecordWriter<K>
	extends RecordWriter<K,VariantContextWritable>
{
	private VariantContextWriter writer;
	private LazyVCFGenotypesContext.HeaderDataCache vcfHeaderDataCache =
		new LazyVCFGenotypesContext.HeaderDataCache();
	private LazyBCFGenotypesContext.HeaderDataCache bcfHeaderDataCache =
		new LazyBCFGenotypesContext.HeaderDataCache();

	/** A VCF header is read from the input Path, which should refer to a VCF or
	 * BCF file.
	 */
	public BCFRecordWriter(
			Path output, Path input, boolean writeHeader, TaskAttemptContext ctx)
		throws IOException
	{
		final WrapSeekable in =