Now that we always output the VCF header, make sure that we correctly handle the situation where there are no records in the file. Added unit tests as well.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2333 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2009-12-11 19:51:05 +00:00
parent 0da2105e3c
commit 09811b9f34
2 changed files with 22 additions and 6 deletions

View File

@ -52,23 +52,30 @@ public class VCFReader implements Iterator<VCFRecord>, Iterable<VCFRecord> {
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<String> lines = new ArrayList<String>();
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

View File

@ -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<VCFRecord> iter = reader.iterator();
Assert.assertTrue(!reader.iterator().hasNext());
}
}