Skip to content

Commit

Permalink
Make VCFHeader not throw exception if contig header lines lack length…
Browse files Browse the repository at this point in the history
… field
  • Loading branch information
cwhelan committed Sep 19, 2019
1 parent e1bbd34 commit 16f6355
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/main/java/htsjdk/variant/vcf/VCFContigHeaderLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ public Integer getContigIndex() {
return contigIndex;
}

/**
* Get the SAMSequenceRecord that corresponds to this VCF header line.
* @return The SAMSequenceRecord containing the ID, length, assembly, and index of this contig. Returns null if the
* contig header line does not have a length.
*/
public SAMSequenceRecord getSAMSequenceRecord() {
final String lengthString = this.getGenericFieldValue("length");
if (lengthString == null) throw new TribbleException("Contig " + this.getID() + " does not have a length field.");
if (lengthString == null) return null;
final SAMSequenceRecord record = new SAMSequenceRecord(this.getID(), Integer.parseInt(lengthString));
record.setAssembly(this.getGenericFieldValue("assembly"));
record.setSequenceIndex(this.contigIndex);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/htsjdk/variant/vcf/VCFHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public List<VCFContigHeaderLine> getContigLines() {

/**
* Returns the contigs in this VCF file as a SAMSequenceDictionary. Returns null if contigs lines are
* not present in the header. Throws SAMException if one or more contig lines do not have length
* not present in the header. Returns null if one or more contig lines do not have length
* information.
*/
public SAMSequenceDictionary getSequenceDictionary() {
Expand All @@ -270,7 +270,9 @@ public SAMSequenceDictionary getSequenceDictionary() {

final List<SAMSequenceRecord> sequenceRecords = new ArrayList<SAMSequenceRecord>(contigHeaderLines.size());
for (final VCFContigHeaderLine contigHeaderLine : contigHeaderLines) {
sequenceRecords.add(contigHeaderLine.getSAMSequenceRecord());
final SAMSequenceRecord samSequenceRecord = contigHeaderLine.getSAMSequenceRecord();
if (samSequenceRecord == null) return null;
sequenceRecords.add(samSequenceRecord);
}

return new SAMSequenceDictionary(sequenceRecords);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/htsjdk/variant/vcf/VCFHeaderUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ public void testVCFHeaderAddContigLine() {
}

@Test
public void testVCFHeaderContigLineMissingLength() {
final VCFHeader header = getHiSeqVCFHeader();
final VCFContigHeaderLine contigLine = new VCFContigHeaderLine(
"<ID=chr1>", VCFHeaderVersion.VCF4_0, VCFHeader.CONTIG_KEY, 0);
header.addMetaDataLine(contigLine);
Assert.assertTrue(header.getContigLines().contains(contigLine), "Test contig line not found in contig header lines");
Assert.assertTrue(header.getMetaDataInInputOrder().contains(contigLine), "Test contig line not found in set of all header lines");

Assert.assertNull(header.getSequenceDictionary());
}

@Test
public void testVCFHeaderHonorContigLineOrder() throws IOException {
try (final VCFFileReader vcfReader = new VCFFileReader(new File(variantTestDataRoot + "dbsnp_135.b37.1000.vcf"), false)) {
// start with a header with a bunch of contig lines
Expand Down

0 comments on commit 16f6355

Please sign in to comment.