Given that all classes implementing output stubs already have getters for the underlying OutputStream and File, it makes sense to unify that functionality into the Stub interface. Now it is possible to have an Engine utility method that iterates over all registered stubs to find the one representing a given OutputStream and return the File associated with it.

This commit is contained in:
Eric Banks 2012-09-12 11:51:44 -04:00
parent 994a4ff387
commit 4bb7a99f08
6 changed files with 43 additions and 14 deletions

View File

@ -63,6 +63,7 @@ import org.broadinstitute.sting.utils.recalibration.BaseRecalibration;
import org.broadinstitute.sting.utils.threading.ThreadEfficiencyMonitor; import org.broadinstitute.sting.utils.threading.ThreadEfficiencyMonitor;
import java.io.File; import java.io.File;
import java.io.OutputStream;
import java.util.*; import java.util.*;
/** /**
@ -731,6 +732,21 @@ public class GenomeAnalysisEngine {
outputs.add(stub); outputs.add(stub);
} }
/**
* Iterates over all registered output stubs and tries to find the one representing the given OutputStream.
*
* @param output the stream to check for
* @return the file associated with the given stream/stub if available, null otherwise
*/
public File getFilenameFromAssociatedOutputStream(final OutputStream output) {
for ( final Stub<?> stub : outputs ) {
if ( stub.getOutputStream() == output )
return stub.getOutputFile();
}
return null;
}
/** /**
* Returns the tag associated with a given command-line argument. * Returns the tag associated with a given command-line argument.
* @param key Object for which to inspect the tag. * @param key Object for which to inspect the tag.

View File

@ -50,7 +50,7 @@ public class SAMFileWriterStorage implements SAMFileWriter, Storage<SAMFileWrite
private static Logger logger = Logger.getLogger(SAMFileWriterStorage.class); private static Logger logger = Logger.getLogger(SAMFileWriterStorage.class);
public SAMFileWriterStorage( SAMFileWriterStub stub ) { public SAMFileWriterStorage( SAMFileWriterStub stub ) {
this(stub,stub.getSAMFile()); this(stub,stub.getOutputFile());
} }
public SAMFileWriterStorage( SAMFileWriterStub stub, File file ) { public SAMFileWriterStorage( SAMFileWriterStub stub, File file ) {
@ -66,7 +66,7 @@ public class SAMFileWriterStorage implements SAMFileWriter, Storage<SAMFileWrite
if(stub.getMaxRecordsInRam() != null) if(stub.getMaxRecordsInRam() != null)
factory.setMaxRecordsInRam(stub.getMaxRecordsInRam()); factory.setMaxRecordsInRam(stub.getMaxRecordsInRam());
if(stub.getSAMFile() != null) { if(stub.getOutputFile() != null) {
try { try {
this.writer = createBAMWriter(factory,stub.getFileHeader(),stub.isPresorted(),file,stub.getCompressionLevel()); this.writer = createBAMWriter(factory,stub.getFileHeader(),stub.isPresorted(),file,stub.getCompressionLevel());
} }
@ -74,8 +74,8 @@ public class SAMFileWriterStorage implements SAMFileWriter, Storage<SAMFileWrite
throw new UserException.CouldNotCreateOutputFile(file,"file could not be created",ex); throw new UserException.CouldNotCreateOutputFile(file,"file could not be created",ex);
} }
} }
else if(stub.getSAMOutputStream() != null){ else if(stub.getOutputStream() != null){
this.writer = factory.makeSAMWriter( stub.getFileHeader(), stub.isPresorted(), stub.getSAMOutputStream()); this.writer = factory.makeSAMWriter( stub.getFileHeader(), stub.isPresorted(), stub.getOutputStream());
} }
else else
throw new UserException("Unable to write to SAM file; neither a target file nor a stream has been specified"); throw new UserException("Unable to write to SAM file; neither a target file nor a stream has been specified");

View File

@ -69,9 +69,9 @@ public class VariantContextWriterStorage implements Storage<VariantContextWriter
* @param stub Stub to use when constructing the output file. * @param stub Stub to use when constructing the output file.
*/ */
public VariantContextWriterStorage(VariantContextWriterStub stub) { public VariantContextWriterStorage(VariantContextWriterStub stub) {
if ( stub.getFile() != null ) { if ( stub.getOutputFile() != null ) {
this.file = stub.getFile(); this.file = stub.getOutputFile();
writer = vcfWriterToFile(stub,stub.getFile(),true); writer = vcfWriterToFile(stub,stub.getOutputFile(),true);
} }
else if ( stub.getOutputStream() != null ) { else if ( stub.getOutputStream() != null ) {
this.file = null; this.file = null;

View File

@ -150,7 +150,7 @@ public class SAMFileWriterStub implements Stub<SAMFileWriter>, StingSAMFileWrite
* Retrieves the SAM file to (ultimately) be created. * Retrieves the SAM file to (ultimately) be created.
* @return The SAM file. Must not be null. * @return The SAM file. Must not be null.
*/ */
public File getSAMFile() { public File getOutputFile() {
return samFile; return samFile;
} }
@ -162,7 +162,7 @@ public class SAMFileWriterStub implements Stub<SAMFileWriter>, StingSAMFileWrite
simplifyBAM = v; simplifyBAM = v;
} }
public OutputStream getSAMOutputStream() { public OutputStream getOutputStream() {
return samOutputStream; return samOutputStream;
} }
@ -220,7 +220,7 @@ public class SAMFileWriterStub implements Stub<SAMFileWriter>, StingSAMFileWrite
/** /**
* Gets whether to generate an md5 on-the-fly for this BAM. * Gets whether to generate an md5 on-the-fly for this BAM.
* @return True generates the md5. False means skip writing the file. * @param generateMD5 True generates the md5. False means skip writing the file.
*/ */
public void setGenerateMD5(boolean generateMD5) { public void setGenerateMD5(boolean generateMD5) {
if(writeStarted) if(writeStarted)

View File

@ -27,6 +27,9 @@ package org.broadinstitute.sting.gatk.io.stubs;
import org.broadinstitute.sting.gatk.io.OutputTracker; import org.broadinstitute.sting.gatk.io.OutputTracker;
import java.io.File;
import java.io.OutputStream;
/** /**
* A stub used for managing IO. Acts as a proxy for IO streams * A stub used for managing IO. Acts as a proxy for IO streams
* not yet created or streams that need significant external * not yet created or streams that need significant external
@ -43,4 +46,14 @@ public interface Stub<StreamType> {
* @param outputTracker The connector used to provide an appropriate stream. * @param outputTracker The connector used to provide an appropriate stream.
*/ */
public void register( OutputTracker outputTracker ); public void register( OutputTracker outputTracker );
/**
* Returns the OutputStream represented by this stub or null if not available.
*/
public OutputStream getOutputStream();
/**
* Returns the File represented by this stub or null if not available.
*/
public File getOutputFile();
} }

View File

@ -140,7 +140,7 @@ public class VariantContextWriterStub implements Stub<VariantContextWriter>, Var
* Retrieves the file to (ultimately) be created. * Retrieves the file to (ultimately) be created.
* @return The file. Can be null if genotypeStream is not. * @return The file. Can be null if genotypeStream is not.
*/ */
public File getFile() { public File getOutputFile() {
return genotypeFile; return genotypeFile;
} }
@ -148,7 +148,7 @@ public class VariantContextWriterStub implements Stub<VariantContextWriter>, Var
* Retrieves the output stearm to which to (ultimately) write. * Retrieves the output stearm to which to (ultimately) write.
* @return The file. Can be null if genotypeFile is not. * @return The file. Can be null if genotypeFile is not.
*/ */
public PrintStream getOutputStream() { public OutputStream getOutputStream() {
return genotypeStream; return genotypeStream;
} }
@ -196,7 +196,7 @@ public class VariantContextWriterStub implements Stub<VariantContextWriter>, Var
if ( engine.lenientVCFProcessing() ) options.add(Options.ALLOW_MISSING_FIELDS_IN_HEADER); if ( engine.lenientVCFProcessing() ) options.add(Options.ALLOW_MISSING_FIELDS_IN_HEADER);
if ( indexOnTheFly && ! isCompressed() ) options.add(Options.INDEX_ON_THE_FLY); if ( indexOnTheFly && ! isCompressed() ) options.add(Options.INDEX_ON_THE_FLY);
if ( forceBCF || (getFile() != null && VariantContextWriterFactory.isBCFOutput(getFile())) ) if ( forceBCF || (getOutputFile() != null && VariantContextWriterFactory.isBCFOutput(getOutputFile())) )
options.add(Options.FORCE_BCF); options.add(Options.FORCE_BCF);
return options.isEmpty() ? EnumSet.noneOf(Options.class) : EnumSet.copyOf(options); return options.isEmpty() ? EnumSet.noneOf(Options.class) : EnumSet.copyOf(options);
@ -271,7 +271,7 @@ public class VariantContextWriterStub implements Stub<VariantContextWriter>, Var
public boolean alsoWriteBCFForTest() { public boolean alsoWriteBCFForTest() {
return engine.getArguments().numberOfDataThreads == 1 && // only works single threaded return engine.getArguments().numberOfDataThreads == 1 && // only works single threaded
! isCompressed() && // for non-compressed outputs ! isCompressed() && // for non-compressed outputs
getFile() != null && // that are going to disk getOutputFile() != null && // that are going to disk
engine.getArguments().generateShadowBCF; // and we actually want to do it engine.getArguments().generateShadowBCF; // and we actually want to do it
} }