Check that -compress arguments are within range 0-9

-- Although the original bug report was about SplitSamFile it actually was an engine wide error.  The two places in the that provide compression to the BAM write now check the validity of the compress argument via a static method in ReadUtils
-- delivers #49531009
This commit is contained in:
Mark DePristo 2013-05-31 13:54:33 -04:00
parent a96f48bc39
commit 4b206a3540
3 changed files with 28 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import org.broadinstitute.sting.commandline.*;
import org.broadinstitute.sting.gatk.GenomeAnalysisEngine;
import org.broadinstitute.sting.gatk.io.StingSAMFileWriter;
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
import org.broadinstitute.sting.utils.sam.ReadUtils;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
@ -132,9 +133,9 @@ public class SAMFileWriterArgumentTypeDescriptor extends ArgumentTypeDescriptor
if (writerFileName != null && writerFileName.asFile() != null ) {
stub = new SAMFileWriterStub(engine, writerFileName.asFile());
if ( compressionLevel != null )
stub.setCompressionLevel(compressionLevel);
if ( indexOnTheFly )
if ( compressionLevel != null ) {
stub.setCompressionLevel(ReadUtils.validateCompressionLevel(compressionLevel));
} if ( indexOnTheFly )
stub.setIndexOnTheFly(indexOnTheFly);
if ( generateMD5 )
stub.setGenerateMD5(generateMD5);

View File

@ -36,6 +36,7 @@ import org.broadinstitute.sting.utils.MathUtils;
import org.broadinstitute.sting.utils.NGSPlatform;
import org.broadinstitute.sting.utils.collections.Pair;
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
import org.broadinstitute.sting.utils.exceptions.UserException;
import java.io.File;
import java.util.*;
@ -152,11 +153,18 @@ public class ReadUtils {
* @return a SAMFileWriter with the compression level if it is a bam.
*/
public static SAMFileWriter createSAMFileWriterWithCompression(SAMFileHeader header, boolean presorted, String file, int compression) {
validateCompressionLevel(compression);
if (file.endsWith(".bam"))
return new SAMFileWriterFactory().makeBAMWriter(header, presorted, new File(file), compression);
return new SAMFileWriterFactory().makeSAMOrBAMWriter(header, presorted, new File(file));
}
public static int validateCompressionLevel(final int requestedCompressionLevel) {
if ( requestedCompressionLevel < 0 || requestedCompressionLevel > 9 )
throw new UserException.BadArgumentValue("compress", "Compression level must be 0-9 but got " + requestedCompressionLevel);
return requestedCompressionLevel;
}
/**
* is this base inside the adaptor of the read?
*

View File

@ -174,4 +174,20 @@ public class EngineFeaturesIntegrationTest extends WalkerTest {
1, Arrays.asList("ecf27a776cdfc771defab1c5d19de9ab"));
executeTest("testUserReadFilterAppliedBeforeWalker", spec);
}
@Test
public void testNegativeCompress() {
testBadCompressArgument(-1);
}
@Test
public void testTooBigCompress() {
testBadCompressArgument(100);
}
private void testBadCompressArgument(final int compress) {
WalkerTestSpec spec = new WalkerTestSpec("-T PrintReads -R " + b37KGReference + " -I private/testdata/NA12878.1_10mb_2_10mb.bam -o %s -compress " + compress,
1, UserException.class);
executeTest("badCompress " + compress, spec);
}
}