diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java b/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java index f0037b04a..9e11833e6 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java @@ -101,7 +101,8 @@ public class UnifiedGenotyper extends LocusWalker, Integer> { "UnifiedGenotyper", this.getToolkit().getArguments().referenceFile.getName()); else - writer = GenotypeWriterFactory.create(VAR_FORMAT, GenomeAnalysisEngine.instance.getSAMFileHeader(), out); + writer = GenotypeWriterFactory.create(VAR_FORMAT, GenomeAnalysisEngine.instance.getSAMFileHeader(), out, "UnifiedGenotyper", + this.getToolkit().getArguments().referenceFile.getName()); gcm = GenotypeCalculationModelFactory.makeGenotypeCalculation(UAC, samples, writer, logger); } diff --git a/java/src/org/broadinstitute/sting/utils/genotype/GenotypeWriterFactory.java b/java/src/org/broadinstitute/sting/utils/genotype/GenotypeWriterFactory.java index 94c36da8f..5f3e8f460 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/GenotypeWriterFactory.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/GenotypeWriterFactory.java @@ -46,10 +46,14 @@ public class GenotypeWriterFactory { } } - public static GenotypeWriter create(GENOTYPE_FORMAT format, SAMFileHeader header, PrintStream destination) { + public static GenotypeWriter create(GENOTYPE_FORMAT format, SAMFileHeader header, PrintStream destination, String source, String referenceName ) { switch (format) { case GELI: return new GeliTextWriter(destination); + case GLF: + return new GLFWriter(header.toString(), destination); + case VCF: + return new VCFGenotypeWriterAdapter(source, referenceName, destination); default: throw new StingException("Genotype writer to " + format.toString() + " to standard output is not implemented"); } diff --git a/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFWriter.java b/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFWriter.java index 96583f2d3..ae2c05717 100755 --- a/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFWriter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFWriter.java @@ -10,6 +10,7 @@ import org.broadinstitute.sting.utils.genotype.*; import java.io.DataOutputStream; import java.io.File; +import java.io.OutputStream; import java.util.Arrays; import java.util.List; /* @@ -73,6 +74,20 @@ public class GLFWriter implements GenotypeWriter { this.writeHeader(); } + /** + * The public constructor for creating a GLF object + * + * @param headerText the header text (currently unclear what the contents are) + * @param writeTo the location to write to + */ + public GLFWriter(String headerText, OutputStream writeTo) { + this.headerText = headerText; + outputBinaryCodec = new BinaryCodec(writeTo); + outputBinaryCodec.setOutputFileName(writeTo.toString()); + this.writeHeader(); + } + + /** * add a point genotype to the GLF writer * diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java index 4e2c34510..3bea47ec2 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java @@ -8,11 +8,8 @@ import org.broadinstitute.sting.utils.genotype.ReadBacked; import org.broadinstitute.sting.utils.genotype.SampleBacked; import java.io.File; -import java.util.Arrays; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.io.OutputStream; +import java.util.*; /** @@ -31,13 +28,21 @@ public class VCFGenotypeWriterAdapter implements GenotypeWriter { private final Map mSampleNames = new HashMap(); private boolean mInitialized = false; private final File mFile; + private final OutputStream mStream; public VCFGenotypeWriterAdapter(String source, String referenceName, File writeTo) { mReferenceName = referenceName; mSource = source; mFile = writeTo; + mStream = null; } + public VCFGenotypeWriterAdapter(String source, String referenceName, OutputStream writeTo) { + mReferenceName = referenceName; + mSource = source; + mFile = null; + mStream = writeTo; + } /** * initialize this VCF writer @@ -45,7 +50,7 @@ public class VCFGenotypeWriterAdapter implements GenotypeWriter { * @param genotypes the genotypes * @param file the file location to write to */ - private void lazyInitialize(List genotypes, File file) { + private void lazyInitialize(List genotypes, File file, OutputStream stream) { Map hInfo = new HashMap(); List sampleNames = getSampleNames(genotypes); @@ -55,7 +60,10 @@ public class VCFGenotypeWriterAdapter implements GenotypeWriter { // setup the sample names mHeader = new VCFHeader(hInfo, sampleNames); - mWriter = new VCFWriter(mHeader, file); + if (mFile == null) + mWriter = new VCFWriter(mHeader, stream); + else + mWriter = new VCFWriter(mHeader, file); mInitialized = true; } @@ -111,7 +119,7 @@ public class VCFGenotypeWriterAdapter implements GenotypeWriter { @Override public void addMultiSampleCall(List genotypes) { if (!mInitialized) - lazyInitialize(genotypes, mFile); + lazyInitialize(genotypes, mFile, mStream); VCFParamters params = new VCFParamters(); diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFWriter.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFWriter.java index 773bf05f3..a1467ccfa 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFWriter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFWriter.java @@ -1,10 +1,14 @@ package org.broadinstitute.sting.utils.genotype.vcf; +import org.broadinstitute.sting.utils.StingException; + import java.io.*; import java.nio.charset.Charset; -/** this class writers VCF files */ +/** + * this class writers VCF files + */ public class VCFWriter { // the VCF header we're storing @@ -21,16 +25,30 @@ public class VCFWriter { * @param location the file location to write to */ public VCFWriter(VCFHeader header, File location) { - this.mHeader = header; - Charset utf8 = Charset.forName("UTF-8"); + FileOutputStream output; try { - mWriter = new BufferedWriter( - new OutputStreamWriter( - new FileOutputStream(location), - utf8)); + output = new FileOutputStream(location); } catch (FileNotFoundException e) { - throw new RuntimeException("Unable to create VCF file: " + location, e); + throw new RuntimeException("Unable to create VCF file at location: " + location); } + initialize(header, output); + } + + + /** + * create a VCF writer, given a VCF header and a file to write to + * + * @param header the VCF header + * @param location the file location to write to + */ + public VCFWriter(VCFHeader header, OutputStream location) { + initialize(header, location); + } + + private void initialize(VCFHeader header, OutputStream location) { + this.mHeader = header; + mWriter = new BufferedWriter( + new OutputStreamWriter(location)); try { // write the header meta-data out for (String metadata : header.getMetaData().keySet()) { @@ -71,8 +89,9 @@ public class VCFWriter { } - - /** attempt to close the VCF file */ + /** + * attempt to close the VCF file + */ public void close() { try { mWriter.flush();