From e4003bc79271a14c96b4445aa7dca43c647a2ecb Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Wed, 30 Mar 2016 16:44:46 -0400 Subject: [PATCH] Add informative exceptions to getSAMFileSamples() --- .../DepthOfCoverageIntegrationTest.java | 20 +++++++++++++++++ .../gatk/utils/help/HelpConstants.java | 2 +- .../gatk/utils/sam/ReadUtils.java | 22 ++++++++++++++++--- .../gatk/utils/sam/ReadUtilsUnitTest.java | 16 ++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/public/gatk-tools-public/src/test/java/org/broadinstitute/gatk/tools/walkers/coverage/DepthOfCoverageIntegrationTest.java b/public/gatk-tools-public/src/test/java/org/broadinstitute/gatk/tools/walkers/coverage/DepthOfCoverageIntegrationTest.java index 038ce5642..9b201b411 100644 --- a/public/gatk-tools-public/src/test/java/org/broadinstitute/gatk/tools/walkers/coverage/DepthOfCoverageIntegrationTest.java +++ b/public/gatk-tools-public/src/test/java/org/broadinstitute/gatk/tools/walkers/coverage/DepthOfCoverageIntegrationTest.java @@ -235,4 +235,24 @@ public class DepthOfCoverageIntegrationTest extends WalkerTest { 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()); + 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()); + execute("testMissingSAMHeaderReadGroupSample", spec); + } } diff --git a/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/help/HelpConstants.java b/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/help/HelpConstants.java index 0f521f653..6c7068b19 100644 --- a/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/help/HelpConstants.java +++ b/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/help/HelpConstants.java @@ -27,7 +27,7 @@ package org.broadinstitute.gatk.utils.help; 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_ARTICLE_URL = BASE_GATK_URL + "/guide/article"; public final static String GATK_FORUM_URL = "http://gatkforums.broadinstitute.org/"; diff --git a/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/sam/ReadUtils.java b/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/sam/ReadUtils.java index 6c1925729..0cd84f5d9 100644 --- a/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/sam/ReadUtils.java +++ b/public/gatk-utils/src/main/java/org/broadinstitute/gatk/utils/sam/ReadUtils.java @@ -32,8 +32,9 @@ import org.apache.log4j.Logger; import org.broadinstitute.gatk.utils.*; import org.broadinstitute.gatk.utils.collections.Pair; 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.*; /** @@ -63,11 +64,26 @@ public class ReadUtils { * @return list of strings representing the sample names */ public static Set 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 final Set samples = new TreeSet(); - List readGroups = header.getReadGroups(); - for ( SAMReadGroupRecord readGroup : readGroups ) + final List readGroups = header.getReadGroups(); + 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()); + } return samples; } diff --git a/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/sam/ReadUtilsUnitTest.java b/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/sam/ReadUtilsUnitTest.java index 211303945..372a711b0 100644 --- a/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/sam/ReadUtilsUnitTest.java +++ b/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/sam/ReadUtilsUnitTest.java @@ -25,11 +25,13 @@ package org.broadinstitute.gatk.utils.sam; +import htsjdk.samtools.SAMReadGroupRecord; import htsjdk.samtools.reference.IndexedFastaSequenceFile; import htsjdk.samtools.SAMFileHeader; import org.broadinstitute.gatk.utils.BaseTest; import org.broadinstitute.gatk.utils.BaseUtils; import org.broadinstitute.gatk.utils.Utils; +import org.broadinstitute.gatk.utils.exceptions.UserException; import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; import org.testng.Assert; 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) { 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 list = new ArrayList<>(Arrays.asList(samGroup)); + header.setReadGroups(list); + ReadUtils.getSAMFileSamples(header); + } }