diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java index 4d071a0ee..63395707a 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java @@ -52,23 +52,30 @@ public class VCFReader implements Iterator, Iterable { this.parseHeader(); } - private void parseHeader() - { + /** + * parse the header from the top of the file. We read lines that match + * the header pattern, and try to create a header. We then create the first + * record from the first non-header line in the file. + */ + private void parseHeader() { String line = null; // try and parse the header try { ArrayList lines = new ArrayList(); line = mReader.readLine(); - while (line.startsWith("#")) { + while (line != null && line.startsWith("#")) { lines.add(line); line = mReader.readLine(); } + // try to make a header from the lines we parsed mHeader = this.createHeader(lines); - mNextRecord = createRecord(line, mHeader); + + // if the last line we parsed is null, we shouldn't try to make a record of it + if (line != null) mNextRecord = createRecord(line, mHeader); } catch (IOException e) { throw new RuntimeException("VCFReader: Failed to parse VCF File on line: " + line, e); } - } + } /** * open a g-zipped version of the VCF format diff --git a/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFReaderTest.java b/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFReaderTest.java index 0203868c8..1e9c8f608 100644 --- a/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFReaderTest.java +++ b/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFReaderTest.java @@ -22,7 +22,8 @@ public class VCFReaderTest extends BaseTest { private static final File multiSampleVCF = new File("/humgen/gsa-scr1/GATK_Data/Validation_Data/MultiSample.vcf"); private static final String VCF_MIXUP_FILE = "/humgen/gsa-scr1/GATK_Data/Validation_Data/mixedup.v2.vcf"; private static final File complexFile = new File("/humgen/gsa-scr1/GATK_Data/Validation_Data/complexExample.vcf"); - + private static final File headerNoRecordsFile = new File("/humgen/gsa-scr1/GATK_Data/Validation_Data/headerNoRecords.vcf"); + private static IndexedFastaSequenceFile seq; @BeforeClass @@ -325,4 +326,12 @@ public class VCFReaderTest extends BaseTest { if (reader.hasNext()) Assert.fail("The reader should NOT have a record"); } + + public void testHeaderNoRecords() { + VCFReader reader = new VCFReader(headerNoRecordsFile); + Assert.assertTrue(reader.getHeader().getMetaData() != null); + Iterator iter = reader.iterator(); + Assert.assertTrue(!reader.iterator().hasNext()); + + } }