Add informative exceptions to getSAMFileSamples()

This commit is contained in:
Ron Levine 2016-03-30 16:44:46 -04:00
parent 68b068d2b3
commit e4003bc792
4 changed files with 56 additions and 4 deletions

View File

@ -235,4 +235,24 @@ public class DepthOfCoverageIntegrationTest extends WalkerTest {
execute("testSampleGeneSummaryHeaderFix", spec); execute("testSampleGeneSummaryHeaderFix", spec);
} }
@Test(expectedExceptions = RuntimeException.class)
public void testMissingSAMHeaderReadGroup() {
final String[] intervals = {"chr1:200-300"};
final String[] bams = {privateTestDir + "exampleBAMNoRG.bam"};
final String cmd = buildRootCmd(exampleFASTA,new ArrayList<>(Arrays.asList(bams)),new ArrayList<>(Arrays.asList(intervals)));
final WalkerTestSpec spec = new WalkerTestSpec(cmd, 0, new ArrayList<String>());
execute("testMissingSAMHeaderReadGroup", spec);
}
@Test(expectedExceptions = RuntimeException.class)
public void testMissingSAMHeaderReadGroupSample() {
final String[] intervals = {"chr1:200-300"};
final String[] bams = {privateTestDir + "exampleBAMNoSM.bam"};
final String cmd = buildRootCmd(exampleFASTA,new ArrayList<>(Arrays.asList(bams)),new ArrayList<>(Arrays.asList(intervals)));
final WalkerTestSpec spec = new WalkerTestSpec(cmd, 0, new ArrayList<String>());
execute("testMissingSAMHeaderReadGroupSample", spec);
}
} }

View File

@ -27,7 +27,7 @@ package org.broadinstitute.gatk.utils.help;
public class HelpConstants { public class HelpConstants {
public final static String BASE_GATK_URL = "http://www.broadinstitute.org/gatk"; public final static String BASE_GATK_URL = "https://www.broadinstitute.org/gatk";
public final static String GATK_DOCS_URL = BASE_GATK_URL + "/guide/tooldocs/"; public final static String GATK_DOCS_URL = BASE_GATK_URL + "/guide/tooldocs/";
public final static String GATK_ARTICLE_URL = BASE_GATK_URL + "/guide/article"; public final static String GATK_ARTICLE_URL = BASE_GATK_URL + "/guide/article";
public final static String GATK_FORUM_URL = "http://gatkforums.broadinstitute.org/"; public final static String GATK_FORUM_URL = "http://gatkforums.broadinstitute.org/";

View File

@ -32,8 +32,9 @@ import org.apache.log4j.Logger;
import org.broadinstitute.gatk.utils.*; import org.broadinstitute.gatk.utils.*;
import org.broadinstitute.gatk.utils.collections.Pair; import org.broadinstitute.gatk.utils.collections.Pair;
import org.broadinstitute.gatk.utils.exceptions.ReviewedGATKException; import org.broadinstitute.gatk.utils.exceptions.ReviewedGATKException;
import org.broadinstitute.gatk.utils.exceptions.UserException;
import org.broadinstitute.gatk.utils.help.HelpConstants;
import java.io.File;
import java.util.*; import java.util.*;
/** /**
@ -63,11 +64,26 @@ public class ReadUtils {
* @return list of strings representing the sample names * @return list of strings representing the sample names
*/ */
public static Set<String> getSAMFileSamples(final SAMFileHeader header) { public static Set<String> getSAMFileSamples(final SAMFileHeader header) {
if ( header == null ) {
throw new IllegalArgumentException("Missing SAM file header. " +
"For more information on read groups, see " + HelpConstants.articlePost("6472"));
}
// get all of the unique sample names // get all of the unique sample names
final Set<String> samples = new TreeSet<String>(); final Set<String> samples = new TreeSet<String>();
List<SAMReadGroupRecord> readGroups = header.getReadGroups(); final List<SAMReadGroupRecord> readGroups = header.getReadGroups();
for ( SAMReadGroupRecord readGroup : readGroups ) if ( readGroups == null ) {
throw new UserException("SAM file header is missing the Read Group (@RG). " +
"For more information on read groups, see " + HelpConstants.articlePost("6472"));
}
for ( final SAMReadGroupRecord readGroup : readGroups ) {
final String sample = readGroup.getSample();
if ( sample == null ) {
throw new UserException("SAM file header is missing the sample field (SM) in the Read Group (@RG). " +
"For more information on read groups, see " + HelpConstants.articlePost("6472"));
}
samples.add(readGroup.getSample()); samples.add(readGroup.getSample());
}
return samples; return samples;
} }

View File

@ -25,11 +25,13 @@
package org.broadinstitute.gatk.utils.sam; package org.broadinstitute.gatk.utils.sam;
import htsjdk.samtools.SAMReadGroupRecord;
import htsjdk.samtools.reference.IndexedFastaSequenceFile; import htsjdk.samtools.reference.IndexedFastaSequenceFile;
import htsjdk.samtools.SAMFileHeader; import htsjdk.samtools.SAMFileHeader;
import org.broadinstitute.gatk.utils.BaseTest; import org.broadinstitute.gatk.utils.BaseTest;
import org.broadinstitute.gatk.utils.BaseUtils; import org.broadinstitute.gatk.utils.BaseUtils;
import org.broadinstitute.gatk.utils.Utils; import org.broadinstitute.gatk.utils.Utils;
import org.broadinstitute.gatk.utils.exceptions.UserException;
import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
@ -336,4 +338,18 @@ public class ReadUtilsUnitTest extends BaseTest {
private void testHasWellDefinedFragmentSize(final String name, final GATKSAMRecord read, final boolean expected) { private void testHasWellDefinedFragmentSize(final String name, final GATKSAMRecord read, final boolean expected) {
Assert.assertEquals(ReadUtils.hasWellDefinedFragmentSize(read), expected); Assert.assertEquals(ReadUtils.hasWellDefinedFragmentSize(read), expected);
} }
@Test(expectedExceptions = IllegalArgumentException.class)
public void testGetSAMFileMissingHeader() {
ReadUtils.getSAMFileSamples(null);
}
@Test(expectedExceptions = UserException.class)
public void testGetSAMFileMissingReadGroupsSamples() {
final SAMFileHeader header = ArtificialSAMUtils.createArtificialSamHeader();
final SAMReadGroupRecord samGroup = new SAMReadGroupRecord("id");
final List<SAMReadGroupRecord> list = new ArrayList<>(Arrays.asList(samGroup));
header.setReadGroups(list);
ReadUtils.getSAMFileSamples(header);
}
} }