diff --git a/java/src/org/broadinstitute/sting/gatk/io/storage/GLFGenotypeWriterStorage.java b/java/src/org/broadinstitute/sting/gatk/io/storage/GLFGenotypeWriterStorage.java new file mode 100644 index 000000000..589c953a1 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/storage/GLFGenotypeWriterStorage.java @@ -0,0 +1,68 @@ +package org.broadinstitute.sting.gatk.io.storage; + +import org.broadinstitute.sting.utils.genotype.glf.GLFReader; +import org.broadinstitute.sting.utils.genotype.glf.GLFRecord; +import org.broadinstitute.sting.utils.genotype.glf.GLFGenotypeWriter; +import org.broadinstitute.sting.gatk.io.stubs.GenotypeWriterStub; + +import java.io.File; + +/** + * Provides temporary and permanent storage for genotypes in GLF format. + * + * @author mhanna + * @version 0.1 + */ +public class GLFGenotypeWriterStorage extends GenotypeWriterStorage implements GLFGenotypeWriter { + /** + * Creates new (permanent) storage for GLF genotype writers. + * @param stub Stub containing appropriate input parameters. + */ + public GLFGenotypeWriterStorage(GenotypeWriterStub stub) { + super(stub); + } + + /** + * Creates new (temporary) storage for GLF genotype writers. + * @param stub Stub containing appropriate input parameters. + * @param target Target file for output data. + */ + public GLFGenotypeWriterStorage(GenotypeWriterStub stub, File target) { + super(stub,target); + } + + /** + * Write the geli header to the target file. + * @param headerText The header to write. + */ + public void writeHeader(String headerText) { + ((GLFGenotypeWriter)writer).writeHeader(headerText); + } + + /** + * add a GLF record to the output file + * + * @param contigName the contig name + * @param contigLength the contig length + * @param rec the GLF record to write. + */ + public void addGLFRecord(String contigName, int contigLength, GLFRecord rec) { + ((GLFGenotypeWriter)writer).addGLFRecord(contigName,contigLength,rec); + } + + /** + * Merges the stream backing up this temporary storage into the target. + * @param target Target stream for the temporary storage. May not be null. + */ + @Override + public void mergeInto(GLFGenotypeWriter target) { + GLFReader reader = new GLFReader(file); + while ( reader.hasNext() ) { + GLFRecord rec = reader.next(); + target.addGLFRecord(rec.getContig(),(int)rec.getPosition(),rec); + } + reader.close(); + + file.delete(); + } +} diff --git a/java/src/org/broadinstitute/sting/gatk/io/storage/GeliBinaryGenotypeWriterStorage.java b/java/src/org/broadinstitute/sting/gatk/io/storage/GeliBinaryGenotypeWriterStorage.java new file mode 100644 index 000000000..04a8a67a6 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/storage/GeliBinaryGenotypeWriterStorage.java @@ -0,0 +1,66 @@ +package org.broadinstitute.sting.gatk.io.storage; + +import org.broadinstitute.sting.utils.genotype.geli.GeliGenotypeWriter; +import org.broadinstitute.sting.gatk.io.stubs.GenotypeWriterStub; + +import java.io.File; + +import edu.mit.broad.picard.genotype.geli.GeliFileReader; +import edu.mit.broad.picard.genotype.geli.GenotypeLikelihoods; +import net.sf.samtools.SAMFileHeader; + +/** + * Provides temporary and permanent storage for genotypes in Geli binary format. + * + * @author mhanna + * @version 0.1 + */ +public class GeliBinaryGenotypeWriterStorage extends GenotypeWriterStorage implements GeliGenotypeWriter { + /** + * Creates new (permanent) storage for geli binary genotype writers. + * @param stub Stub containing appropriate input parameters. + */ + public GeliBinaryGenotypeWriterStorage(GenotypeWriterStub stub) { + super(stub); + } + + /** + * Creates new (temporary) storage for geli binary genotype writers. + * @param stub Stub containing appropriate input parameters. + * @param target Target file for output data. + */ + public GeliBinaryGenotypeWriterStorage(GenotypeWriterStub stub, File target) { + super(stub,target); + } + + /** + * Write the geli header to the target file. + * @param header The header to write. + */ + public void writeHeader(SAMFileHeader header) { + ((GeliGenotypeWriter)writer).writeHeader(header); + } + + /** + * Writes the genotype likelihoods to the output. + * @param gl genotype likelihoods to write. + */ + public void addGenotypeLikelihoods(GenotypeLikelihoods gl) { + ((GeliGenotypeWriter)writer).addGenotypeLikelihoods(gl); + } + + + /** + * Merges the stream backing up this temporary storage into the target. + * @param target Target stream for the temporary storage. May not be null. + */ + @Override + public void mergeInto(GeliGenotypeWriter target) { + GeliFileReader reader = new GeliFileReader(file); + while ( reader.hasNext() ) + target.addGenotypeLikelihoods(reader.next()); + reader.close(); + + file.delete(); + } +} diff --git a/java/src/org/broadinstitute/sting/gatk/io/storage/GeliTextGenotypeWriterStorage.java b/java/src/org/broadinstitute/sting/gatk/io/storage/GeliTextGenotypeWriterStorage.java new file mode 100644 index 000000000..09d960d30 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/storage/GeliTextGenotypeWriterStorage.java @@ -0,0 +1,65 @@ +package org.broadinstitute.sting.gatk.io.storage; + +import org.broadinstitute.sting.utils.genotype.geli.GeliGenotypeWriter; +import org.broadinstitute.sting.gatk.io.stubs.GenotypeWriterStub; + +import java.io.File; + +import edu.mit.broad.picard.genotype.geli.GeliFileReader; +import edu.mit.broad.picard.genotype.geli.GenotypeLikelihoods; +import net.sf.samtools.SAMFileHeader; + +/** + * Provides temporary and permanent storage for genotypes in Geli text format. + * + * @author mhanna + * @version 0.1 + */ +public class GeliTextGenotypeWriterStorage extends GenotypeWriterStorage implements GeliGenotypeWriter { + /** + * Creates new (permanent) storage for geli text genotype writers. + * @param stub Stub containing appropriate input parameters. + */ + public GeliTextGenotypeWriterStorage(GenotypeWriterStub stub) { + super(stub); + } + + /** + * Creates new (temporary) storage for geli text genotype writers. + * @param stub Stub containing appropriate input parameters. + * @param target Target file for output data. + */ + public GeliTextGenotypeWriterStorage(GenotypeWriterStub stub, File target) { + super(stub,target); + } + + /** + * Write the geli header to the target file. + * @param header The header to write. + */ + public void writeHeader(SAMFileHeader header) { + ((GeliGenotypeWriter)writer).writeHeader(header); + } + + /** + * Writes the genotype likelihoods to the output. + * @param gl genotype likelihoods to write. + */ + public void addGenotypeLikelihoods(GenotypeLikelihoods gl) { + ((GeliGenotypeWriter)writer).addGenotypeLikelihoods(gl); + } + + /** + * Merges the stream backing up this temporary storage into the target. + * @param target Target stream for the temporary storage. May not be null. + */ + @Override + public void mergeInto(GeliGenotypeWriter target) { + GeliFileReader reader = new GeliFileReader(file); + while ( reader.hasNext() ) + target.addGenotypeLikelihoods(reader.next()); + reader.close(); + + file.delete(); + } +} diff --git a/java/src/org/broadinstitute/sting/gatk/io/storage/GenotypeWriterStorage.java b/java/src/org/broadinstitute/sting/gatk/io/storage/GenotypeWriterStorage.java index d7c68056c..54fd534e9 100755 --- a/java/src/org/broadinstitute/sting/gatk/io/storage/GenotypeWriterStorage.java +++ b/java/src/org/broadinstitute/sting/gatk/io/storage/GenotypeWriterStorage.java @@ -32,11 +32,8 @@ import java.util.HashSet; import org.broadinstitute.sting.gatk.io.stubs.GenotypeWriterStub; import org.broadinstitute.sting.utils.genotype.*; -import org.broadinstitute.sting.utils.genotype.glf.*; -import org.broadinstitute.sting.utils.genotype.geli.*; import org.broadinstitute.sting.utils.genotype.vcf.*; import org.broadinstitute.sting.utils.SampleUtils; -import edu.mit.broad.picard.genotype.geli.GeliFileReader; /** * Provides temporary storage for GenotypeWriters. @@ -44,76 +41,32 @@ import edu.mit.broad.picard.genotype.geli.GeliFileReader; * @author ebanks * @version 0.1 */ -public class GenotypeWriterStorage implements GenotypeWriter, Storage { - private final GenotypeWriterFactory.GENOTYPE_FORMAT format; - private final File file; - private final GenotypeWriter writer; +public abstract class GenotypeWriterStorage implements GenotypeWriter, Storage { + protected final File file; + protected final GenotypeWriter writer; + /** + * Constructs an object which will write directly into the output file provided by the stub. + * Intentionally delaying the writing of the header -- this should be filled in by the walker. + * @param stub Stub to use when constructing the output file. + */ public GenotypeWriterStorage( GenotypeWriterStub stub ) { - this(stub, stub.getFile()); + this.file = stub.getFile(); + writer = GenotypeWriterFactory.create(stub.getFormat(), file); } + /** + * Constructs an object which will redirect into a different file. + * @param stub Stub to use when synthesizing file / header info. + * @param file File into which to direct the output data. + */ public GenotypeWriterStorage( GenotypeWriterStub stub, File file ) { - this.format = stub.getFormat(); this.file = file; writer = GenotypeWriterFactory.create(stub.getFormat(), file); Set samples = SampleUtils.getSAMFileSamples(stub.getSAMFileHeader()); GenotypeWriterFactory.writeHeader(writer, stub.getSAMFileHeader(), samples, new HashSet()); } - /** - * Reports the format of the given genotyping data, taken directly from the stub. - * @return The format of the genotyping file. - */ - @Override - public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { - return format; - } - - - public void mergeInto( GenotypeWriter targetStream ) { - - // TODO -- This is ugly, but there is no GenotypeWriter interface since - // TODO -- VCFReaders need to be separated out for compatability with Tribble - // TODO -- and the adapters don't all implement a common interface. Fix me. Please. - - // VCF - if ( targetStream instanceof VCFGenotypeWriterAdapter ) { - VCFReader reader = new VCFReader(file); - while ( reader.hasNext() ) - ((VCFGenotypeWriterAdapter)targetStream).addRecord(reader.next()); - reader.close(); - } - - // GELI TEXT - else if ( targetStream instanceof GeliTextWriter ) { - GeliFileReader reader = new GeliFileReader(file); - while ( reader.hasNext() ) - ((GeliTextWriter)targetStream).addGenotypeLikelihoods(reader.next()); - reader.close(); - } - - // GELI BINARY - else if ( targetStream instanceof GeliAdapter ) { - GeliFileReader reader = new GeliFileReader(file); - while ( reader.hasNext() ) - ((GeliAdapter)targetStream).addGenotypeLikelihoods(reader.next()); - reader.close(); - } - - // GLF - else if ( targetStream instanceof GLFWriter ) { - GLFReader reader = new GLFReader(file); - while ( reader.hasNext() ) { - GLFRecord rec = reader.next(); - ((GLFWriter)targetStream).addGLFRecord(rec.getContig(),(int)rec.getPosition(),rec); - } - reader.close(); - } - - file.delete(); - } - public void addGenotypeCall(Genotype call) { writer.addGenotypeCall(call); } diff --git a/java/src/org/broadinstitute/sting/gatk/io/storage/StorageFactory.java b/java/src/org/broadinstitute/sting/gatk/io/storage/StorageFactory.java index 7a76d4b5e..1d3a35e58 100644 --- a/java/src/org/broadinstitute/sting/gatk/io/storage/StorageFactory.java +++ b/java/src/org/broadinstitute/sting/gatk/io/storage/StorageFactory.java @@ -28,9 +28,7 @@ package org.broadinstitute.sting.gatk.io.storage; import org.broadinstitute.sting.gatk.io.stubs.Stub; import org.broadinstitute.sting.gatk.io.stubs.OutputStreamStub; import org.broadinstitute.sting.gatk.io.stubs.SAMFileWriterStub; -import org.broadinstitute.sting.gatk.io.storage.SAMFileWriterStorage; -import org.broadinstitute.sting.gatk.io.storage.Storage; -import org.broadinstitute.sting.gatk.io.storage.OutputStreamStorage; +import org.broadinstitute.sting.gatk.io.stubs.GenotypeWriterStub; import org.broadinstitute.sting.utils.StingException; import java.io.File; @@ -79,6 +77,45 @@ public class StorageFactory { else storage = new SAMFileWriterStorage((SAMFileWriterStub)stub); } + else if(stub instanceof GenotypeWriterStub) { + GenotypeWriterStub genotypeWriterStub = (GenotypeWriterStub)stub; + if( file != null ) { + switch(genotypeWriterStub.getFormat()) { + case GELI: + storage = new GeliTextGenotypeWriterStorage(genotypeWriterStub,file); + break; + case GELI_BINARY: + storage = new GeliBinaryGenotypeWriterStorage(genotypeWriterStub,file); + break; + case GLF: + storage = new GLFGenotypeWriterStorage(genotypeWriterStub,file); + break; + case VCF: + storage = new VCFGenotypeWriterStorage(genotypeWriterStub,file); + break; + default: + throw new StingException("Unsupported genotype file format: " + genotypeWriterStub.getFormat()); + } + } + else { + switch(genotypeWriterStub.getFormat()) { + case GELI: + storage = new GeliTextGenotypeWriterStorage(genotypeWriterStub); + break; + case GELI_BINARY: + storage = new GeliBinaryGenotypeWriterStorage(genotypeWriterStub); + break; + case GLF: + storage = new GLFGenotypeWriterStorage(genotypeWriterStub); + break; + case VCF: + storage = new VCFGenotypeWriterStorage(genotypeWriterStub); + break; + default: + throw new StingException("Unsupported genotype file format: " + genotypeWriterStub.getFormat()); + } + } + } else throw new StingException("Unsupported stub type: " + stub.getClass().getName()); diff --git a/java/src/org/broadinstitute/sting/gatk/io/storage/VCFGenotypeWriterStorage.java b/java/src/org/broadinstitute/sting/gatk/io/storage/VCFGenotypeWriterStorage.java new file mode 100644 index 000000000..5bc02c6c3 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/storage/VCFGenotypeWriterStorage.java @@ -0,0 +1,67 @@ +package org.broadinstitute.sting.gatk.io.storage; + +import org.broadinstitute.sting.utils.genotype.vcf.VCFReader; +import org.broadinstitute.sting.utils.genotype.vcf.VCFGenotypeWriter; +import org.broadinstitute.sting.utils.genotype.vcf.VCFRecord; +import org.broadinstitute.sting.utils.genotype.vcf.VCFHeaderLine; +import org.broadinstitute.sting.gatk.io.stubs.GenotypeWriterStub; + +import java.io.File; +import java.util.Set; + +/** + * Provides temporary and permanent storage for genotypes in VCF format. + * + * @author mhanna + * @version 0.1 + */ +public class VCFGenotypeWriterStorage extends GenotypeWriterStorage implements VCFGenotypeWriter { + /** + * Creates new (permanent) storage for VCF genotype writers. + * @param stub Stub containing appropriate input parameters. + */ + public VCFGenotypeWriterStorage(GenotypeWriterStub stub) { + super(stub); + } + + /** + * Creates new (temporary) storage for VCF genotype writers. + * @param stub Stub containing appropriate input parameters. + * @param target Target file for output data. + */ + public VCFGenotypeWriterStorage(GenotypeWriterStub stub,File target) { + super(stub,target); + } + + /** + * initialize this VCF header + * + * @param sampleNames the sample names + * @param headerInfo the optional header fields + */ + public void writeHeader(Set sampleNames, Set headerInfo) { + ((VCFGenotypeWriter)writer).writeHeader(sampleNames,headerInfo); + } + + /** + * Add a given VCF record to the given output. + * @param vcfRecord Record to add. + */ + public void addRecord(VCFRecord vcfRecord) { + ((VCFGenotypeWriter)writer).addRecord(vcfRecord); + } + + /** + * Merges the stream backing up this temporary storage into the target. + * @param target Target stream for the temporary storage. May not be null. + */ + @Override + public void mergeInto(VCFGenotypeWriter target) { + VCFReader reader = new VCFReader(file); + while ( reader.hasNext() ) + target.addRecord(reader.next()); + reader.close(); + + file.delete(); + } +} diff --git a/java/src/org/broadinstitute/sting/gatk/io/stubs/GLFGenotypeWriterStub.java b/java/src/org/broadinstitute/sting/gatk/io/stubs/GLFGenotypeWriterStub.java new file mode 100644 index 000000000..a898553e7 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/stubs/GLFGenotypeWriterStub.java @@ -0,0 +1,53 @@ +package org.broadinstitute.sting.gatk.io.stubs; + +import org.broadinstitute.sting.utils.genotype.GenotypeWriterFactory; +import org.broadinstitute.sting.utils.genotype.glf.GLFGenotypeWriter; +import org.broadinstitute.sting.utils.genotype.glf.GLFRecord; +import org.broadinstitute.sting.gatk.GenomeAnalysisEngine; + +import java.io.File; + +/** + * Stub providing a passthrough for GLF files. + * + * @author mhanna + * @version 0.1 + */ +public class GLFGenotypeWriterStub extends GenotypeWriterStub implements GLFGenotypeWriter { + /** + * Construct a new stub with the given engine and target file. + * @param engine The engine, for extracting command-line arguments, etc. + * @param genotypeFile Target file into which to write genotyping data. + */ + public GLFGenotypeWriterStub(GenomeAnalysisEngine engine, File genotypeFile) { + super(engine,genotypeFile); + } + + /** + * Gets the format of this stub. We may want to discontinue use of this method and rely on instanceof comparisons. + * @return GLF always. + */ + public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { + return GenotypeWriterFactory.GENOTYPE_FORMAT.GLF; + } + + /** + * Write the GLF header to the target file. + * @param headerText The header to write. + */ + public void writeHeader(String headerText) { + outputTracker.getStorage(this).writeHeader(headerText); + } + + /** + * add a GLF record to the output file + * + * @param contigName the contig name + * @param contigLength the contig length + * @param rec the GLF record to write. + */ + public void addGLFRecord(String contigName, int contigLength, GLFRecord rec) { + outputTracker.getStorage(this).addGLFRecord(contigName, contigLength, rec); + } + +} diff --git a/java/src/org/broadinstitute/sting/gatk/io/stubs/GeliBinaryGenotypeWriterStub.java b/java/src/org/broadinstitute/sting/gatk/io/stubs/GeliBinaryGenotypeWriterStub.java new file mode 100644 index 000000000..7c186c9f1 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/stubs/GeliBinaryGenotypeWriterStub.java @@ -0,0 +1,51 @@ +package org.broadinstitute.sting.gatk.io.stubs; + +import org.broadinstitute.sting.utils.genotype.geli.GeliGenotypeWriter; +import org.broadinstitute.sting.utils.genotype.GenotypeWriterFactory; +import org.broadinstitute.sting.gatk.GenomeAnalysisEngine; +import net.sf.samtools.SAMFileHeader; + +import java.io.File; + +import edu.mit.broad.picard.genotype.geli.GenotypeLikelihoods; + +/** + * Stub providing a passthrough for geli binary files. + * + * @author mhanna + * @version 0.1 + */ +public class GeliBinaryGenotypeWriterStub extends GenotypeWriterStub implements GeliGenotypeWriter { + /** + * Construct a new stub with the given engine and target file. + * @param engine The engine, for extracting command-line arguments, etc. + * @param genotypeFile Target file into which to write genotyping data. + */ + public GeliBinaryGenotypeWriterStub(GenomeAnalysisEngine engine,File genotypeFile) { + super(engine,genotypeFile); + } + + /** + * Gets the format of this stub. We may want to discontinue use of this method and rely on instanceof comparisons. + * @return GELI_BINARY always. + */ + public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { + return GenotypeWriterFactory.GENOTYPE_FORMAT.GELI_BINARY; + } + + /** + * Write the geli header to the target file. + * @param header The header to write. + */ + public void writeHeader(SAMFileHeader header) { + outputTracker.getStorage(this).writeHeader(header); + } + + /** + * Writes the genotype likelihoods to the output. + * @param gl genotype likelihoods to write. + */ + public void addGenotypeLikelihoods(GenotypeLikelihoods gl) { + outputTracker.getStorage(this).addGenotypeLikelihoods(gl); + } +} diff --git a/java/src/org/broadinstitute/sting/gatk/io/stubs/GeliTextGenotypeWriterStub.java b/java/src/org/broadinstitute/sting/gatk/io/stubs/GeliTextGenotypeWriterStub.java new file mode 100644 index 000000000..915901bee --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/stubs/GeliTextGenotypeWriterStub.java @@ -0,0 +1,51 @@ +package org.broadinstitute.sting.gatk.io.stubs; + +import org.broadinstitute.sting.utils.genotype.geli.GeliGenotypeWriter; +import org.broadinstitute.sting.utils.genotype.GenotypeWriterFactory; +import org.broadinstitute.sting.gatk.GenomeAnalysisEngine; + +import java.io.File; + +import net.sf.samtools.SAMFileHeader; +import edu.mit.broad.picard.genotype.geli.GenotypeLikelihoods; + +/** + * Stub providing a passthrough for geli text files. + * + * @author mhanna + * @version 0.1 + */ +public class GeliTextGenotypeWriterStub extends GenotypeWriterStub implements GeliGenotypeWriter { + /** + * Construct a new stub with the given engine and target file. + * @param engine The engine, for extracting command-line arguments, etc. + * @param genotypeFile Target file into which to write genotyping data. + */ + public GeliTextGenotypeWriterStub(GenomeAnalysisEngine engine, File genotypeFile) { + super(engine,genotypeFile); + } + + /** + * Gets the format of this stub. We may want to discontinue use of this method and rely on instanceof comparisons. + * @return GELI always. + */ + public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { + return GenotypeWriterFactory.GENOTYPE_FORMAT.GELI; + } + + /** + * Write the geli header to the target file. + * @param header The header to write. + */ + public void writeHeader(SAMFileHeader header) { + outputTracker.getStorage(this).writeHeader(header); + } + + /** + * Writes the genotype likelihoods to the output. + * @param gl genotype likelihoods to write. + */ + public void addGenotypeLikelihoods(GenotypeLikelihoods gl) { + outputTracker.getStorage(this).addGenotypeLikelihoods(gl); + } +} diff --git a/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterArgumentTypeDescriptor.java b/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterArgumentTypeDescriptor.java index 610c2b4d7..2c051e285 100644 --- a/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterArgumentTypeDescriptor.java +++ b/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterArgumentTypeDescriptor.java @@ -82,7 +82,23 @@ public class GenotypeWriterArgumentTypeDescriptor extends ArgumentTypeDescriptor } } - GenotypeWriterStub stub = new GenotypeWriterStub(engine, new File(writerFileName),genotypeFormat); + GenotypeWriterStub stub = null; + switch(genotypeFormat) { + case GELI: + stub = new GeliTextGenotypeWriterStub(engine, new File(writerFileName)); + break; + case GELI_BINARY: + stub = new GeliBinaryGenotypeWriterStub(engine, new File(writerFileName)); + break; + case GLF: + stub = new GLFGenotypeWriterStub(engine, new File(writerFileName)); + break; + case VCF: + stub = new VCFGenotypeWriterStub(engine, new File(writerFileName)); + break; + default: + throw new StingException("Unable to create stub for file format " + genotypeFormat); + } engine.addOutput(stub); diff --git a/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterStub.java b/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterStub.java index fc04aa8da..94658aa45 100755 --- a/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterStub.java +++ b/java/src/org/broadinstitute/sting/gatk/io/stubs/GenotypeWriterStub.java @@ -42,7 +42,7 @@ import net.sf.samtools.SAMFileHeader; * @author ebanks * @version 0.1 */ -public class GenotypeWriterStub implements Stub, GenotypeWriter { +public abstract class GenotypeWriterStub implements Stub, GenotypeWriter { /** * Engine to use for collecting attributes for the output SAM file. @@ -55,29 +55,20 @@ public class GenotypeWriterStub implements Stub, GenotypeWriter */ private final File genotypeFile; - /** - * The file format for the output - */ - private final GenotypeWriterFactory.GENOTYPE_FORMAT format; - /** * Connects this stub with an external stream capable of serving the * requests of the consumer of this stub. */ - private OutputTracker outputTracker = null; + protected OutputTracker outputTracker = null; /** * Create a new stub given the requested file. * @param engine GATK engine. * @param genotypeFile file to (ultimately) create. - * @param format file format. */ - public GenotypeWriterStub( GenomeAnalysisEngine engine, - File genotypeFile, - GenotypeWriterFactory.GENOTYPE_FORMAT format) { + public GenotypeWriterStub(GenomeAnalysisEngine engine,File genotypeFile) { this.engine = engine; this.genotypeFile = genotypeFile; - this.format = format; } /** @@ -100,9 +91,7 @@ public class GenotypeWriterStub implements Stub, GenotypeWriter * Retrieves the format to use when creating the new file. * @return format to use when creating the new file. */ - public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { - return format; - } + public abstract GenotypeWriterFactory.GENOTYPE_FORMAT getFormat(); /** * Registers the given streamConnector with this stub. diff --git a/java/src/org/broadinstitute/sting/gatk/io/stubs/VCFGenotypeWriterStub.java b/java/src/org/broadinstitute/sting/gatk/io/stubs/VCFGenotypeWriterStub.java new file mode 100644 index 000000000..c2abdfe00 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/io/stubs/VCFGenotypeWriterStub.java @@ -0,0 +1,53 @@ +package org.broadinstitute.sting.gatk.io.stubs; + +import org.broadinstitute.sting.utils.genotype.GenotypeWriterFactory; +import org.broadinstitute.sting.utils.genotype.vcf.VCFGenotypeWriter; +import org.broadinstitute.sting.utils.genotype.vcf.VCFHeaderLine; +import org.broadinstitute.sting.utils.genotype.vcf.VCFRecord; +import org.broadinstitute.sting.gatk.GenomeAnalysisEngine; + +import java.io.File; +import java.util.Set; + +/** + * Stub providing a passthrough for VCF files. + * + * @author mhanna + * @version 0.1 + */ +public class VCFGenotypeWriterStub extends GenotypeWriterStub implements VCFGenotypeWriter { + /** + * Construct a new stub with the given engine and target file. + * @param engine The engine, for extracting command-line arguments, etc. + * @param genotypeFile Target file into which to write genotyping data. + */ + public VCFGenotypeWriterStub(GenomeAnalysisEngine engine, File genotypeFile) { + super(engine,genotypeFile); + } + + /** + * Gets the format of this stub. We may want to discontinue use of this method and rely on instanceof comparisons. + * @return VCF always. + */ + public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { + return GenotypeWriterFactory.GENOTYPE_FORMAT.VCF; + } + + /** + * initialize this VCF header + * + * @param sampleNames the sample names + * @param headerInfo the optional header fields + */ + public void writeHeader(Set sampleNames, Set headerInfo) { + outputTracker.getStorage(this).writeHeader(sampleNames,headerInfo); + } + + /** + * Add a given VCF record to the given output. + * @param vcfRecord Record to add. + */ + public void addRecord(VCFRecord vcfRecord) { + outputTracker.getStorage(this).addRecord(vcfRecord); + } +} 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 ae534f0a4..cb71a95af 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyper.java @@ -35,9 +35,10 @@ import org.broadinstitute.sting.utils.*; import org.broadinstitute.sting.utils.pileup.*; import org.broadinstitute.sting.utils.cmdLine.*; import org.broadinstitute.sting.utils.genotype.*; +import org.broadinstitute.sting.utils.genotype.geli.GeliGenotypeWriter; +import org.broadinstitute.sting.utils.genotype.glf.GLFGenotypeWriter; import org.broadinstitute.sting.utils.genotype.vcf.*; -import java.io.File; import java.util.*; @@ -52,30 +53,21 @@ public class UnifiedGenotyper extends LocusWalker samples; // keep track of some metrics about our calls private CallMetrics callsMetrics; - /** Enable deletions in the pileup **/ public boolean includeReadsWithDeletionAtLoci() { return true; } - /** * Sets the argument collection for the UnifiedGenotyper. * To be used with walkers that call the UnifiedGenotyper's map function @@ -121,11 +113,23 @@ public class UnifiedGenotyper extends LocusWalker headerInfo = getHeaderInfo(); - // create the output writer stream and initialize the header - if ( VARIANTS_FILE != null ) - writer = GenotypeWriterFactory.create(VAR_FORMAT, VARIANTS_FILE); - else - writer = GenotypeWriterFactory.create(VAR_FORMAT, out); + // initialize the header GenotypeWriterFactory.writeHeader(writer, GenomeAnalysisEngine.instance.getSAMFileHeader(), samples, headerInfo); callsMetrics = new CallMetrics(); @@ -152,7 +152,7 @@ public class UnifiedGenotyper extends LocusWalker headerInfo = new HashSet(); // this is only applicable to VCF - if ( VAR_FORMAT != GenotypeWriterFactory.GENOTYPE_FORMAT.VCF ) + if ( !(writer instanceof VCFGenotypeWriter) ) return headerInfo; // first, the basic info @@ -285,7 +285,6 @@ public class UnifiedGenotyper extends LocusWalker sampleNames, Set headerInfo) { // VCF - if ( writer instanceof VCFGenotypeWriterAdapter ) { - ((VCFGenotypeWriterAdapter)writer).writeHeader(sampleNames, headerInfo); + if ( writer instanceof VCFGenotypeWriter ) { + ((VCFGenotypeWriter)writer).writeHeader(sampleNames, headerInfo); } - // GELI BINARY - else if ( writer instanceof GeliAdapter ) { - ((GeliAdapter)writer).writeHeader(header); + // GELI + else if ( writer instanceof GeliGenotypeWriter ) { + ((GeliGenotypeWriter)writer).writeHeader(header); } // GLF - else if ( writer instanceof GLFWriter ) { - ((GLFWriter)writer).writeHeader(header.toString()); + else if ( writer instanceof GLFGenotypeWriter ) { + ((GLFGenotypeWriter)writer).writeHeader(header.toString()); } // nothing to do for GELI TEXT } diff --git a/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliAdapter.java b/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliAdapter.java index 739b408c8..754d81b84 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliAdapter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliAdapter.java @@ -44,7 +44,7 @@ import java.util.List; * Class GeliAdapter * Adapts the Geli file writer to the Genotype writer interface */ -public class GeliAdapter implements GenotypeWriter { +public class GeliAdapter implements GeliGenotypeWriter { // the file we're writing to private File writeTo = null; @@ -61,20 +61,12 @@ public class GeliAdapter implements GenotypeWriter { this.writeTo = writeTo; } - /** - * Indicates that this is a binary-format geli writer. - * @return GENOTYPE_FORMAT.GELI_BINARY always. - */ - @Override - public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { - return GenotypeWriterFactory.GENOTYPE_FORMAT.GELI_BINARY; - } - /** * wrap a GeliFileWriter in the Genotype writer interface * * @param fileHeader the file header to write out */ + @Override public void writeHeader(final SAMFileHeader fileHeader) { this.writer = GeliFileWriter.newInstanceForPresortedRecords(writeTo, fileHeader); } diff --git a/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliGenotypeWriter.java b/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliGenotypeWriter.java new file mode 100644 index 000000000..801f843ab --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliGenotypeWriter.java @@ -0,0 +1,26 @@ +package org.broadinstitute.sting.utils.genotype.geli; + +import org.broadinstitute.sting.utils.genotype.GenotypeWriter; +import net.sf.samtools.SAMFileHeader; +import edu.mit.broad.picard.genotype.geli.GenotypeLikelihoods; + +/** + * An extension of eth GenotypeWriter interface with support + * for adding a header. + * + * @author mhanna + * @version 0.1 + */ +public interface GeliGenotypeWriter extends GenotypeWriter { + /** + * Write the file header. + * @param fileHeader SAM file header from which to derive the geli header. + */ + public void writeHeader(final SAMFileHeader fileHeader); + + /** + * Writes the genotype likelihoods to the output. + * @param gl genotype likelihoods to write. + */ + public void addGenotypeLikelihoods(GenotypeLikelihoods gl); +} diff --git a/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliTextWriter.java b/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliTextWriter.java index 23f4aca22..03aa70dd2 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliTextWriter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/geli/GeliTextWriter.java @@ -1,6 +1,7 @@ package org.broadinstitute.sting.utils.genotype.geli; import net.sf.samtools.SAMRecord; +import net.sf.samtools.SAMFileHeader; import org.broadinstitute.sting.utils.StingException; import org.broadinstitute.sting.utils.genotype.*; @@ -21,7 +22,7 @@ import edu.mit.broad.picard.genotype.geli.GenotypeLikelihoods; *

* write out the geli text file format containing genotype information */ -public class GeliTextWriter implements GenotypeWriter { +public class GeliTextWriter implements GeliGenotypeWriter { // where we write to PrintWriter mWriter; @@ -36,25 +37,23 @@ public class GeliTextWriter implements GenotypeWriter { } catch (FileNotFoundException e) { throw new StingException("Unable to open file " + file.toURI()); } - mWriter.println(headerLine); } public GeliTextWriter(PrintStream out) { mWriter = new PrintWriter(out); - mWriter.println(headerLine); - } - - /** - * Indicates that this is a geli writer. - * @return GENOTYPE_FORMAT.GELI always. - */ - @Override - public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { - return GenotypeWriterFactory.GENOTYPE_FORMAT.GELI; } public final static String headerLine = "#Sequence Position ReferenceBase NumberOfReads MaxMappingQuality BestGenotype BtrLod BtnbLod AA AC AG AT CC CG CT GG GT TT"; + /** + * Write the file header. + * @param fileHeader SAM file header from which to derive the geli header. + */ + public void writeHeader(final SAMFileHeader fileHeader) { + // ignore the SAM header; the geli text header is fixed. + mWriter.println(headerLine); + } + /** * Add a genotype, given a call * diff --git a/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFGenotypeWriter.java b/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFGenotypeWriter.java new file mode 100644 index 000000000..9ad345373 --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFGenotypeWriter.java @@ -0,0 +1,27 @@ +package org.broadinstitute.sting.utils.genotype.glf; + +import org.broadinstitute.sting.utils.genotype.GenotypeWriter; + +/** + * An extension of eth GenotypeWriter interface with support + * for adding header lines. + * + * @author mhanna + * @version 0.1 + */ +public interface GLFGenotypeWriter extends GenotypeWriter { + /** + * Append the given header text to the GLF file. + * @param headerText the file header to write out + */ + public void writeHeader(String headerText); + + /** + * add a GLF record to the output file + * + * @param contigName the contig name + * @param contigLength the contig length + * @param rec the GLF record to write. + */ + public void addGLFRecord(String contigName, int contigLength, GLFRecord rec); +} 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 668f5c446..60a94315f 100755 --- a/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFWriter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/glf/GLFWriter.java @@ -45,7 +45,7 @@ import java.util.List; * single and variable length genotype calls using the provided functions. When you've finished * generating GLF records, make sure you close the file. */ -public class GLFWriter implements GenotypeWriter { +public class GLFWriter implements GLFGenotypeWriter { // our output codec private final BinaryCodec outputBinaryCodec; @@ -83,15 +83,6 @@ public class GLFWriter implements GenotypeWriter { outputBinaryCodec.setOutputFileName(writeTo.toString()); } - /** - * Indicates that this is a GLF writer. - * @return GENOTYPE_FORMAT.GLF always. - */ - @Override - public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { - return GenotypeWriterFactory.GENOTYPE_FORMAT.GLF; - } - /** * Write out the header information for the GLF file. The header contains * the magic number, the length of the header text, the text itself, the reference diff --git a/java/src/org/broadinstitute/sting/utils/genotype/tabular/TabularLFWriter.java b/java/src/org/broadinstitute/sting/utils/genotype/tabular/TabularLFWriter.java index 9078a1e9d..17199dd08 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/tabular/TabularLFWriter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/tabular/TabularLFWriter.java @@ -38,16 +38,6 @@ public class TabularLFWriter implements GenotypeWriter { outStream.println("location sample_name ref alt genotype qhat qstar lodVsRef lodVsNextBest depth bases"); } - /** - * Indicates that this is a tabular genotype writer. - * @return GENOTYPE_FORMAT.TABULAR always. - */ - @Override - public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { - return GenotypeWriterFactory.GENOTYPE_FORMAT.TABULAR; - } - - /** * Add a genotype, given a genotype locus * diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriter.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriter.java new file mode 100644 index 000000000..d82d6c5ce --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriter.java @@ -0,0 +1,28 @@ +package org.broadinstitute.sting.utils.genotype.vcf; + +import org.broadinstitute.sting.utils.genotype.GenotypeWriter; + +import java.util.Set; + +/** + * An extension of eth GenotypeWriter interface with support + * for adding header lines. + * + * @author mhanna + * @version 0.1 + */ +public interface VCFGenotypeWriter extends GenotypeWriter { + /** + * initialize this VCF header + * + * @param sampleNames the sample names + * @param headerInfo the optional header fields + */ + public void writeHeader(Set sampleNames, Set headerInfo); + + /** + * Add a given VCF record to the given output. + * @param vcfRecord Record to add. + */ + public void addRecord(VCFRecord vcfRecord); +} 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 22c79a708..fe9a5be63 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java @@ -15,7 +15,7 @@ import java.util.*; *

* Adapt the VCF writter to the genotype output system */ -public class VCFGenotypeWriterAdapter implements GenotypeWriter { +public class VCFGenotypeWriterAdapter implements VCFGenotypeWriter { // our VCF objects private VCFWriter mWriter = null; private VCFHeader mHeader = null; @@ -35,21 +35,13 @@ public class VCFGenotypeWriterAdapter implements GenotypeWriter { mWriter = new VCFWriter(writeTo); } - /** - * Indicates that this is a VCF writer. - * @return GENOTYPE_FORMAT.VCF always. - */ - @Override - public GenotypeWriterFactory.GENOTYPE_FORMAT getFormat() { - return GenotypeWriterFactory.GENOTYPE_FORMAT.VCF; - } - /** * initialize this VCF header * * @param sampleNames the sample names * @param headerInfo the optional header fields */ + @Override public void writeHeader(Set sampleNames, Set headerInfo) { mSampleNames.addAll(sampleNames);