All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.seqdoop.hadoop_bam.KeyIgnoringVCFOutputFormat Maven / Gradle / Ivy

Go to download

A Java library for the manipulation of files in common bioinformatics formats using the Hadoop MapReduce framework.

There is a newer version: 7.10.0
Show newest version
// 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-26 15:19:41

package org.seqdoop.hadoop_bam;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import htsjdk.samtools.seekablestream.SeekableStream;
import htsjdk.variant.vcf.VCFHeader;

import org.seqdoop.hadoop_bam.util.VCFHeaderReader;
import org.seqdoop.hadoop_bam.util.WrapSeekable;

import hbparquet.hadoop.util.ContextUtil;

/** Writes only the VCF records, not the key.
 *
 * 

A {@link VCFHeader} must be provided via {@link #setHeader} or {@link * #readHeaderFrom} before {@link #getRecordWriter} is called.

* *

By default, writes the VCF 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.

*/ public class KeyIgnoringVCFOutputFormat extends VCFOutputFormat { protected VCFHeader header; public KeyIgnoringVCFOutputFormat(VCFFormat fmt) { super(fmt); } public KeyIgnoringVCFOutputFormat(Configuration conf) { super(conf); if (format == null) throw new IllegalArgumentException( "unknown VCF format: OUTPUT_VCF_FORMAT_PROPERTY not set"); } public KeyIgnoringVCFOutputFormat(Configuration conf, Path path) { super(conf); if (format == null) { format = VCFFormat.inferFromFilePath(path); if (format == null) throw new IllegalArgumentException("unknown VCF format: " + path); } } /** Whether the header will be written, defaults to true. */ public static final String WRITE_HEADER_PROPERTY = "hadoopbam.vcf.write-header"; public VCFHeader getHeader() { return header; } public void setHeader(VCFHeader header) { this.header = header; } public void readHeaderFrom(Path path, FileSystem fs) throws IOException { SeekableStream i = WrapSeekable.openPath(fs, path); readHeaderFrom(i); i.close(); } public void readHeaderFrom(SeekableStream in) throws IOException { this.header = VCFHeaderReader.readHeaderFrom(in); } /** setHeader or readHeaderFrom must have been * called first. */ @Override public RecordWriter getRecordWriter( TaskAttemptContext ctx) throws IOException { return getRecordWriter(ctx, getDefaultWorkFile(ctx, "")); } // Allows wrappers to provide their own work file. public RecordWriter getRecordWriter( TaskAttemptContext ctx, Path out) throws IOException { if (this.header == null) throw new IOException( "Can't create a RecordWriter without the VCF header"); final boolean wh = ContextUtil.getConfiguration(ctx).getBoolean( WRITE_HEADER_PROPERTY, true); switch (format) { case BCF: return new KeyIgnoringBCFRecordWriter(out,header,wh,ctx); case VCF: return new KeyIgnoringVCFRecordWriter(out,header,wh,ctx); default: assert false; return null; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy