// Path: src/main/java/org/seqdoop/hadoop_bam/util/SAMHeaderReader.java
// public final class SAMHeaderReader {
// 	/** A String property corresponding to a ValidationStringency
// 	 * value. If set, the given stringency is used when any part of the
// 	 * Hadoop-BAM library reads SAM or BAM.
// 	 */
// 	public static final String VALIDATION_STRINGENCY_PROPERTY =
// 		"hadoopbam.samheaderreader.validation-stringency";
// 
// 	public static SAMFileHeader readSAMHeaderFrom(Path path, Configuration conf)
// 		throws IOException
// 	{
// 		InputStream i = path.getFileSystem(conf).open(path);
// 		final SAMFileHeader h = readSAMHeaderFrom(i, conf);
// 		i.close();
// 		return h;
// 	}
// 
// 	/** Does not close the stream. */
// 	public static SAMFileHeader readSAMHeaderFrom(
// 		final InputStream in, final Configuration conf)
// 	{
// 		final ValidationStringency
// 			stringency = getValidationStringency(conf);
// 		SamReaderFactory readerFactory = SamReaderFactory.makeDefault()
// 				.setOption(SamReaderFactory.Option.EAGERLY_DECODE, false)
// 				.setUseAsyncIo(false);
// 		if (stringency != null) {
// 			readerFactory.validationStringency(stringency);
// 		}
// 
// 		final ReferenceSource refSource = getReferenceSource(conf);
// 		if (null != refSource) {
// 			readerFactory.referenceSource(refSource);
// 		}
// 		return readerFactory.open(SamInputResource.of(in)).getFileHeader();
// 	}
// 
// 	public static ValidationStringency getValidationStringency(
// 		final Configuration conf)
// 	{
// 		final String p = conf.get(VALIDATION_STRINGENCY_PROPERTY);
// 		return p == null ? null : ValidationStringency.valueOf(p);
// 	}
// 
// 	public static ReferenceSource getReferenceSource(
// 			final Configuration conf)
// 	{
// 		//TODO: There isn't anything particularly CRAM-specific about reference source or validation
// 		// stringency other than that a reference source is required for CRAM files. We should move
// 		// the reference source and validation stringency property names and utility methods out of
// 		// CRAMInputFormat and SAMHeaderReader and combine them together into a single class for extracting
// 		// configuration params, but it would break backward compatibility with existing code that
// 		// is dependent on the CRAMInputFormat.REFERENCE_SOURCE_PATH_PROPERTY.
// 		final String refSourcePath = conf.get(CRAMInputFormat.REFERENCE_SOURCE_PATH_PROPERTY);
// 		return refSourcePath == null ? null : new ReferenceSource(NIOFileUtil.asPath(refSourcePath));
// 	}
// }


import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.seqdoop.hadoop_bam.util.SAMHeaderReader;
import java.io.IOException;
import java.io.InputStream;
import htsjdk.samtools.SAMFileHeader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.RecordWriter;
// Copyright (c) 2010 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: 2010-08-11 12:19:23

package org.seqdoop.hadoop_bam;





/** Writes only the SAM records, not the key.
 *
 * <p>A {@link SAMFileHeader} must be provided via {@link #setSAMHeader} or
 * {@link #readSAMHeaderFrom} before {@link #getRecordWriter} is called.</p>
 *
 * <p>By default, writes the SAM header to the output file(s). This
 * can be disabled, because in distributed usage one often ends up with (and,
 * for decent performance, wants to end up with) the output split into multiple
 * parts, which are easier to concatenate if the header is not present in each
 * file.</p>
 */
public class KeyIgnoringAnySAMOutputFormat<K> extends AnySAMOutputFormat<K> {

	protected SAMFileHeader header;

	/** Whether the header will be written, defaults to true..
	 */
	public static final String WRITE_HEADER_PROPERTY =
		"hadoopbam.anysam.write-header";

	public KeyIgnoringAnySAMOutputFormat(SAMFormat fmt) {
		super(fmt);
	}
	public KeyIgnoringAnySAMOutputFormat(Configuration conf) {
		super(conf);

		if (format == null)
			throw new IllegalArgumentException(
				"unknown SAM format: OUTPUT_SAM_FORMAT_PROPERTY not set");
	}
	public KeyIgnoringAnySAMOutputFormat(Configuration conf, Path path) {
		super(conf);

		if (format == null) {
			format = SAMFormat.inferFromFilePath(path);

			if (format == null)
				throw new IllegalArgumentException("unknown SAM format: " + path);
		}
	}

	public SAMFileHeader getSAMHeader() { return header; }
	public void setSAMHeader(SAMFileHeader header) { this.header = header; }

	public void readSAMHeaderFrom(Path path, Configuration conf)
		throws IOException
	{
// Path: src/main/java/org/seqdoop/hadoop_bam/util/SAMHeaderReader.java
// public final class SAMHeaderReader {
// 	/** A String property corresponding to a ValidationStringency
// 	 * value. If set, the given stringency is used when any part of the
// 	 * Hadoop-BAM library reads SAM or BAM.
// 	 */
// 	public static final String VALIDATION_STRINGENCY_PROPERTY =
// 		"hadoopbam.samheaderreader.validation-stringency";
// 
// 	public static SAMFileHeader readSAMHeaderFrom(Path path, Configuration conf)
// 		throws IOException
// 	{
// 		InputStream i = path.getFileSystem(conf).open(path);
// 		final SAMFileHeader h = readSAMHeaderFrom(i, conf);
// 		i.close();
// 		return h;
// 	}
// 
// 	/** Does not close the stream. */
// 	public static SAMFileHeader readSAMHeaderFrom(
// 		final InputStream in, final Configuration conf)
// 	{
// 		final ValidationStringency
// 			stringency = getValidationStringency(conf);
// 		SamReaderFactory readerFactory = SamReaderFactory.makeDefault()
// 				.setOption(SamReaderFactory.Option.EAGERLY_DECODE, false)
// 				.setUseAsyncIo(false);
// 		if (stringency != null) {
// 			readerFactory.validationStringency(stringency);
// 		}
// 
// 		final ReferenceSource refSource = getReferenceSource(conf);
// 		if (null != refSource) {
// 			readerFactory.referenceSource(refSource);
// 		}
// 		return readerFactory.open(SamInputResource.of(in)).getFileHeader();
// 	}
// 
// 	public static ValidationStringency getValidationStringency(
// 		final Configuration conf)
// 	{
// 		final String p = conf.get(VALIDATION_STRINGENCY_PROPERTY);
// 		return p == null ? null : ValidationStringency.valueOf(p);
// 	}
// 
// 	public static ReferenceSource getReferenceSource(
// 			final Configuration conf)
// 	{
// 		//TODO: There isn't anything particularly CRAM-specific about reference source or validation
// 		// stringency other than that a reference source is required for CRAM files. We should move
// 		// the reference source and validation stringency property names and utility methods out of
// 		// CRAMInputFormat and SAMHeaderReader and combine them together into a single class for extracting
// 		// configuration params, but it would break backward compatibility with existing code that
// 		// is dependent on the CRAMInputFormat.REFERENCE_SOURCE_PATH_PROPERTY.
// 		final String refSourcePath = conf.get(CRAMInputFormat.REFERENCE_SOURCE_PATH_PROPERTY);
// 		return refSourcePath == null ? null : new ReferenceSource(NIOFileUtil.asPath(refSourcePath));
// 	}
// }


// Path: src/main/java/org/seqdoop/hadoop_bam/KeyIgnoringAnySAMOutputFormat.java
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.seqdoop.hadoop_bam.util.SAMHeaderReader;
import java.io.IOException;
import java.io.InputStream;
import htsjdk.samtools.SAMFileHeader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.RecordWriter;

// Copyright (c) 2010 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: 2010-08-11 12:19:23

package org.seqdoop.hadoop_bam;





/** Writes only the SAM records, not the key.
 *
 * <p>A {@link SAMFileHeader} must be provided via {@link #setSAMHeader} or
 * {@link #readSAMHeaderFrom} before {@link #getRecordWriter} is called.</p>
 *
 * <p>By default, writes the SAM header to the output file(s). This
 * can be disabled, because in distributed usage one often ends up with (and,
 * for decent performance, wants to end up with) the output split into multiple
 * parts, which are easier to concatenate if the header is not present in each
 * file.</p>
 */
public class KeyIgnoringAnySAMOutputFormat<K> extends AnySAMOutputFormat<K> {

	protected SAMFileHeader header;

	/** Whether the header will be written, defaults to true..
	 */
	public static final String WRITE_HEADER_PROPERTY =
		"hadoopbam.anysam.write-header";

	public KeyIgnoringAnySAMOutputFormat(SAMFormat fmt) {
		super(fmt);
	}
	public KeyIgnoringAnySAMOutputFormat(Configuration conf) {
		super(conf);

		if (format == null)
			throw new IllegalArgumentException(
				"unknown SAM format: OUTPUT_SAM_FORMAT_PROPERTY not set");
	}
	public KeyIgnoringAnySAMOutputFormat(Configuration conf, Path path) {
		super(conf);

		if (format == null) {
			format = SAMFormat.inferFromFilePath(path);

			if (format == null)
				throw new IllegalArgumentException("unknown SAM format: " + path);
		}
	}

	public SAMFileHeader getSAMHeader() { return header; }
	public void setSAMHeader(SAMFileHeader header) { this.header = header; }

	public void readSAMHeaderFrom(Path path, Configuration conf)
		throws IOException
	{
		this.header = SAMHeaderReader.readSAMHeaderFrom(path, conf);