VCF reader now creates the correct type of header line for each header type

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2423 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
ebanks 2009-12-21 20:39:06 +00:00
parent c0c9f6c8b6
commit cf303810d3
5 changed files with 87 additions and 7 deletions

View File

@ -26,6 +26,24 @@ public class VCFFilterHeaderLine extends VCFHeaderLine {
mDescription = description;
}
/**
* create a VCF info header line
*
* @param line the header line
*/
protected VCFFilterHeaderLine(String line) {
super("FILTER", "");
String[] pieces = line.split(",");
if ( pieces.length < 2 )
throw new IllegalArgumentException("There are too few values in the VCF FILTER header line: " + line);
mName = pieces[0];
mDescription = pieces[1];
// just in case there were some commas in the description
for (int i = 1; i < pieces.length; i++)
mDescription += "," + pieces[i];
}
protected String makeStringRep() {
return String.format("FILTER=%s,\"%s\"", mName, mDescription);
}

View File

@ -37,6 +37,26 @@ public class VCFFormatHeaderLine extends VCFHeaderLine {
mDescription = description;
}
/**
* create a VCF format header line
*
* @param line the header line
*/
protected VCFFormatHeaderLine(String line) {
super("FORMAT", "");
String[] pieces = line.split(",");
if ( pieces.length < 4 )
throw new IllegalArgumentException("There are too few values in the VCF FORMAT header line: " + line);
mName = pieces[0];
mCount = Integer.valueOf(pieces[1]);
mType = INFO_TYPE.valueOf(pieces[2]);
mDescription = pieces[3];
// just in case there were some commas in the description
for (int i = 4; i < pieces.length; i++)
mDescription += "," + pieces[i];
}
protected String makeStringRep() {
return String.format("FORMAT=%s,%d,%s,\"%s\"", mName, mCount, mType.toString(), mDescription);
}

View File

@ -37,6 +37,26 @@ public class VCFInfoHeaderLine extends VCFHeaderLine {
mDescription = description;
}
/**
* create a VCF info header line
*
* @param line the header line
*/
protected VCFInfoHeaderLine(String line) {
super("INFO", "");
String[] pieces = line.split(",");
if ( pieces.length < 4 )
throw new IllegalArgumentException("There are too few values in the VCF INFO header line: " + line);
mName = pieces[0];
mCount = Integer.valueOf(pieces[1]);
mType = INFO_TYPE.valueOf(pieces[2]);
mDescription = pieces[3];
// just in case there were some commas in the description
for (int i = 4; i < pieces.length; i++)
mDescription += "," + pieces[i];
}
protected String makeStringRep() {
return String.format("INFO=%s,%d,%s,\"%s\"", mName, mCount, mType.toString(), mDescription);
}

View File

@ -169,9 +169,17 @@ public class VCFReader implements Iterator<VCFRecord>, Iterable<VCFRecord> {
arrayIndex++;
}
} else {
int equals = str.indexOf("=");
if ( equals != -1 )
metaData.add(new VCFHeaderLine(str.substring(2, equals), str.substring(equals+1)));
if ( str.startsWith("##INFO=") )
metaData.add(new VCFInfoHeaderLine(str.substring(7)));
else if ( str.startsWith("##FILTER=") )
metaData.add(new VCFFilterHeaderLine(str.substring(7)));
else if ( str.startsWith("##FORMAT=") )
metaData.add(new VCFFormatHeaderLine(str.substring(7)));
else {
int equals = str.indexOf("=");
if ( equals != -1 )
metaData.add(new VCFHeaderLine(str.substring(2, equals), str.substring(equals+1)));
}
}
}

View File

@ -10,10 +10,7 @@ import org.junit.Test;
import org.junit.BeforeClass;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
/** test the VCFReader class test */
public class VCFReaderTest extends BaseTest {
@ -206,6 +203,23 @@ public class VCFReaderTest extends BaseTest {
public void testComplexExample() {
VCFReader reader = new VCFReader(complexFile);
// test header
Set<VCFHeaderLine> headerLines = reader.getHeader().getMetaData();
int formatCount = 0;
int infoCount = 0;
int filterCount = 0;
for ( VCFHeaderLine line : headerLines ) {
if ( line instanceof VCFFormatHeaderLine )
formatCount++;
else if ( line instanceof VCFInfoHeaderLine )
infoCount++;
else if ( line instanceof VCFFilterHeaderLine )
filterCount++;
}
Assert.assertEquals(3, formatCount);
Assert.assertEquals(4, infoCount);
Assert.assertEquals(0, filterCount);
// record #1: test dbsnp id
if (!reader.hasNext()) Assert.fail("The reader should have a record");
VCFRecord rec = reader.next();