Skip to content

Commit

Permalink
Merge pull request #1331 from broadinstitute/rhl_doc_null_ptr
Browse files Browse the repository at this point in the history
Add informative exceptions to getSAMFileSamples()
  • Loading branch information
ronlevine committed Mar 31, 2016
2 parents 68b068d + e4003bc commit edbb7e7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Expand Up @@ -235,4 +235,24 @@ public void testSampleGeneSummaryHeaderFix(){

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);
}
}
Expand Up @@ -27,7 +27,7 @@

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/";
Expand Down
Expand Up @@ -32,8 +32,9 @@
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.*;

/**
Expand Down Expand Up @@ -63,11 +64,26 @@ private ReadUtils() {
* @return list of strings representing the sample names
*/
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
final Set<String> samples = new TreeSet<String>();
List<SAMReadGroupRecord> readGroups = header.getReadGroups();
for ( SAMReadGroupRecord readGroup : readGroups )
final List<SAMReadGroupRecord> 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;
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -336,4 +338,18 @@ public Object[][] makeHasWellDefinedFragmentSizeData() throws Exception {
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<SAMReadGroupRecord> list = new ArrayList<>(Arrays.asList(samGroup));
header.setReadGroups(list);
ReadUtils.getSAMFileSamples(header);
}
}

0 comments on commit edbb7e7

Please sign in to comment.